blob: e1d944c6f22cc725017ab4cb42ce8e6580c76135 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Valerio Setti1ad9ef22023-02-22 12:38:07 +010055#include "pk_wrap.h"
56
Ronald Cronad8c17b2022-06-10 17:18:09 +020057#if defined(MBEDTLS_TEST_HOOKS)
58static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
59
60void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010061 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020062{
63 chk_buf_ptr_fail_args.cur = cur;
64 chk_buf_ptr_fail_args.end = end;
65 chk_buf_ptr_fail_args.need = need;
66}
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020069{
Gilles Peskine449bd832023-01-11 14:50:10 +010070 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020071}
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020074{
Gilles Peskine449bd832023-01-11 14:50:10 +010075 return (chk_buf_ptr_fail_args.cur != args->cur) ||
76 (chk_buf_ptr_fail_args.end != args->end) ||
77 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020078}
79#endif /* MBEDTLS_TEST_HOOKS */
80
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010082
Hanno Beckera0e20d02019-05-15 14:03:01 +010083#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010084/* Top-level Connection ID API */
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
87 size_t len,
88 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010089{
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
91 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
92 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
95 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
96 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010097 }
98
99 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100102}
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
105 int enable,
106 unsigned char const *own_cid,
107 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100108{
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
110 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
111 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100112
Hanno Beckerca092242019-04-25 16:01:49 +0100113 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (enable == MBEDTLS_SSL_CID_DISABLED) {
115 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
116 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100117 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
119 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (own_cid_len != ssl->conf->cid_len) {
122 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
123 (unsigned) own_cid_len,
124 (unsigned) ssl->conf->cid_len));
125 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100126 }
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100129 /* Truncation is not an issue here because
130 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
131 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100134}
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
137 int *enabled,
138 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
139 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000140{
141 *enabled = MBEDTLS_SSL_CID_DISABLED;
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
144 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
145 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000146
147 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
148 * zero as this is indistinguishable from not requesting to use
149 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
151 return 0;
152 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000155 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (own_cid != NULL) {
157 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
158 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000159 }
160
161 *enabled = MBEDTLS_SSL_CID_ENABLED;
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000164}
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
167 int *enabled,
168 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
169 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100170{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
174 mbedtls_ssl_is_handshake_over(ssl) == 0) {
175 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100176 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Beckerc5f24222019-05-03 12:54:52 +0100178 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
179 * were used, but client and server requested the empty CID.
180 * This is indistinguishable from not using the CID extension
181 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (ssl->transform_in->in_cid_len == 0 &&
183 ssl->transform_in->out_cid_len == 0) {
184 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100185 }
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100188 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (peer_cid != NULL) {
190 memcpy(peer_cid, ssl->transform_in->out_cid,
191 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100192 }
193 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100194
195 *enabled = MBEDTLS_SSL_CID_ENABLED;
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100199#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200204/*
205 * Convert max_fragment_length codes to length.
206 * RFC 6066 says:
207 * enum{
208 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
209 * } MaxFragmentLength;
210 * and we add 0 -> extension unused
211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213{
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 switch (mfl) {
215 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
216 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
218 return 512;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
220 return 1024;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
222 return 2048;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
224 return 4096;
225 default:
226 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000227 }
228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
232 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200233{
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_ssl_session_free(dst);
235 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800236#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
237 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000238#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
239 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000240 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800241#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000242#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000245
246#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
251 if (dst->peer_cert == NULL) {
252 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
253 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
258 src->peer_cert->raw.len)) != 0) {
259 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200262 }
263 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000264#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000266 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_calloc(1, src->peer_cert_digest_len);
268 if (dst->peer_cert_digest == NULL) {
269 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
270 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
273 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000275 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000276 }
277#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200280
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200281#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (src->ticket != NULL) {
283 dst->ticket = mbedtls_calloc(1, src->ticket_len);
284 if (dst->ticket == NULL) {
285 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
286 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000290
291#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
292 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
296 if (ret != 0) {
297 return ret;
298 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000299 }
300#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
301 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200302#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305}
306
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500307#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200308MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100309static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310{
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
312 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500315
316 /* We want to copy len_new bytes when downsizing the buffer, and
317 * len_old bytes when upsizing, so we choose the smaller of two sizes,
318 * to fit one buffer into another. Size checks, ensuring that no data is
319 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 memcpy(resized_buffer, *buffer,
321 (len_new < *len_old) ? len_new : *len_old);
322 mbedtls_platform_zeroize(*buffer, *len_old);
323 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500324
325 *buffer = resized_buffer;
326 *len_old = len_new;
327
328 return 0;
329}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
332 size_t in_buf_new_len,
333 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200334{
335 int modified = 0;
336 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
337 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200339 written_in = ssl->in_msg - ssl->in_buf;
340 iv_offset_in = ssl->in_iv - ssl->in_buf;
341 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200343 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 ssl->in_buf_len < in_buf_new_len) {
345 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
346 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
347 } else {
348 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
349 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 modified = 1;
351 }
352 }
353 }
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200356 written_out = ssl->out_msg - ssl->out_buf;
357 iv_offset_out = ssl->out_iv - ssl->out_buf;
358 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200360 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 ssl->out_buf_len < out_buf_new_len) {
362 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
363 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
364 } else {
365 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
366 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200367 modified = 1;
368 }
369 }
370 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200374 /* Fields below might not be properly updated with record
375 * splitting or with CID, so they are manually updated here. */
376 ssl->out_msg = ssl->out_buf + written_out;
377 ssl->out_len = ssl->out_buf + len_offset_out;
378 ssl->out_iv = ssl->out_buf + iv_offset_out;
379
380 ssl->in_msg = ssl->in_buf + written_in;
381 ssl->in_len = ssl->in_buf + len_offset_in;
382 ssl->in_iv = ssl->in_buf + iv_offset_in;
383 }
384}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500385#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
386
Jerry Yudb8c48a2022-01-27 14:54:54 +0800387#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800388
389#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100390typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
391 const char *label,
392 const unsigned char *random, size_t rlen,
393 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800396
397#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
398
399/* Type for the TLS PRF */
400typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
401 const unsigned char *, size_t,
402 unsigned char *, size_t);
403
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200404MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100405static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
406 int ciphersuite,
407 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200410#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 ssl_tls_prf_t tls_prf,
412 const unsigned char randbytes[64],
413 mbedtls_ssl_protocol_version tls_version,
414 unsigned endpoint,
415 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800416
Andrzej Kurek25f27152022-08-17 16:09:31 -0400417#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200418MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100419static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300420 const char *label,
421 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 unsigned char *dstbuf, size_t dlen);
423static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
424static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
425
426#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
427
428#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
429MBEDTLS_CHECK_RETURN_CRITICAL
430static int tls_prf_sha384(const unsigned char *secret, size_t slen,
431 const char *label,
432 const unsigned char *random, size_t rlen,
433 unsigned char *dstbuf, size_t dlen);
434
435static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
436static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
437#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
438
439static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
440 unsigned char *buf,
441 size_t buf_len);
442
443MBEDTLS_CHECK_RETURN_CRITICAL
444static int ssl_tls12_session_load(mbedtls_ssl_session *session,
445 const unsigned char *buf,
446 size_t len);
447#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
448
449static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
450
451#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
452static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
453#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
454
455#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
456static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
457#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
458
459int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
460 const unsigned char *secret, size_t slen,
461 const char *label,
462 const unsigned char *random, size_t rlen,
463 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300464{
465 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300468#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400469#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300470 case MBEDTLS_SSL_TLS_PRF_SHA384:
471 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400473#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400474#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300475 case MBEDTLS_SSL_TLS_PRF_SHA256:
476 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400478#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300479#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 default:
481 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482 }
483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300485}
486
Jerry Yuc73c6182022-02-08 20:29:25 +0800487#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100488static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800489{
490#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (session->peer_cert != NULL) {
492 mbedtls_x509_crt_free(session->peer_cert);
493 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800494 session->peer_cert = NULL;
495 }
496#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800500 session->peer_cert_digest = NULL;
501 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
502 session->peer_cert_digest_len = 0;
503 }
504#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
505}
506#endif /* MBEDTLS_X509_CRT_PARSE_C */
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800509{
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800511 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800513
514 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800516
517 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800519
520 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800522
523 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800525
526 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800528
529 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800531
532 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800534
535 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800537
538 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800540
541 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800543
544 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800546
547 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800549
550 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800552
553 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800555
556 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800558
559 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800561
562 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800564
565 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800567
568 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800570
571 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800573
574 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800576
577 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800579
580 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800582
583 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800585
586 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800588
589 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800591
592 }
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800595}
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800598{
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800600}
601
Jerry Yud25cab02022-10-31 12:48:30 +0800602#if defined(MBEDTLS_DEBUG_C)
603static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800604 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800605 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
606 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
607 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
608 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
609 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
610 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
611 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
612 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
613 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
614 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
615 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
616 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
617 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
618 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
619 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
620 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
621 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
622 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
623 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
624 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
625 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
626 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
627 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
628 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
629 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
630 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
631 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
632};
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800635 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
636 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
637 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
638 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
639 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
640 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
641 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
642 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
643 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
644 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
645 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
646 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
647 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
648 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
649 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
650 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
651 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
652 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
653 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
654 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
655 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
656 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
657 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
658 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
659 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
660 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
661 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
662 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
663};
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800666{
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 return extension_name_table[
668 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800669}
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800672{
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800688 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800690}
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
693 int level, const char *file, int line,
694 int hs_msg_type, unsigned int extension_type,
695 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800696{
697 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800699 mbedtls_debug_print_msg(
700 ssl, level, file, line,
701 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 ssl_tls13_get_hs_msg_name(hs_msg_type),
703 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800704 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800706 return;
707 }
708
709 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800711 mbedtls_debug_print_msg(
712 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
714 mbedtls_ssl_get_extension_name(extension_type), extension_type,
715 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800716 return;
717 }
718
719 mbedtls_debug_print_msg(
720 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
722 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800723}
724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
726 int level, const char *file, int line,
727 int hs_msg_type, uint32_t extensions_mask,
728 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800729{
730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 for (unsigned i = 0;
732 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
733 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800734 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800735 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800737 }
738}
739
Pengyu Lvee455c02023-01-12 14:37:24 +0800740#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
741#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
742
743static const char *ticket_flag_name_table[] =
744{
745 [0] = "ALLOW_PSK_RESUMPTION",
746 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
747 [3] = "ALLOW_EARLY_DATA",
748};
749
Pengyu Lv4938a562023-01-16 11:28:49 +0800750void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
751 int level, const char *file, int line,
752 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800753{
754 size_t i;
755
756 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800757 "print ticket_flags (0x%02x)", flags);
758
759 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800760
761 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800762 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800763 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
764 ticket_flag_name_table[i]);
765 }
766 }
767}
768#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
769
Jerry Yud25cab02022-10-31 12:48:30 +0800770#endif /* MBEDTLS_DEBUG_C */
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
773 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774{
775 ((void) ciphersuite_info);
776
Andrzej Kurek25f27152022-08-17 16:09:31 -0400777#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800779 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800781#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400782#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800784 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800786#endif
787 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800789 return;
790 }
791}
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
794 unsigned hs_type,
795 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100796{
797 unsigned char hs_hdr[4];
798
799 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
801 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
802 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
803 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100806}
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
809 unsigned hs_type,
810 unsigned char const *msg,
811 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100812{
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
814 ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100815}
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800818{
819 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400820#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800821#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
823 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Jerry Yuc73c6182022-02-08 20:29:25 +0800824#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
Jerry Yuc73c6182022-02-08 20:29:25 +0800826#endif
827#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400828#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800829#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
831 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Jerry Yuc73c6182022-02-08 20:29:25 +0800832#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
Jerry Yuc73c6182022-02-08 20:29:25 +0800834#endif
835#endif
836}
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
839 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400841#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800842#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800846#endif
847#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400848#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800849#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800851#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800853#endif
854#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400855#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
856 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
857 (void) ssl;
858 (void) buf;
859 (void) len;
860#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800861}
862
Andrzej Kurek25f27152022-08-17 16:09:31 -0400863#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100864static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
865 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800866{
867#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800869#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800871#endif
872}
873#endif
874
Andrzej Kurek25f27152022-08-17 16:09:31 -0400875#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100876static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
877 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800878{
879#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800881#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800883#endif
884}
885#endif
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200888{
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200890
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
893 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 mbedtls_sha256_init(&handshake->fin_sha256);
897 mbedtls_sha256_starts(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400900#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500901#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500902 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500904#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 mbedtls_sha512_init(&handshake->fin_sha384);
906 mbedtls_sha512_starts(&handshake->fin_sha384, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200907#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500908#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200909
910 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200914#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200915#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200917#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200918#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200919#if defined(MBEDTLS_USE_PSA_CRYPTO)
920 handshake->psa_pake_ctx = psa_pake_operation_init();
921 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
922#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200924#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200925#if defined(MBEDTLS_SSL_CLI_C)
926 handshake->ecjpake_cache = NULL;
927 handshake->ecjpake_cache_len = 0;
928#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200929#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200930
Gilles Peskineeccd8882020-03-10 12:19:08 +0100931#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200933#endif
934
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200935#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
936 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
937#endif
Hanno Becker75173122019-02-06 16:18:31 +0000938
939#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
940 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000942#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200943}
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200946{
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200948
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
951 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100952#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_cipher_init(&transform->cipher_ctx_enc);
954 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100955#endif
956
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000957#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100958#if defined(MBEDTLS_USE_PSA_CRYPTO)
959 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
960 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100961#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_md_init(&transform->md_ctx_enc);
963 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +0000964#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100965#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200966}
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969{
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971}
972
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200973MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100974static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000975{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200976 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800977#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (ssl->transform_negotiate) {
979 mbedtls_ssl_transform_free(ssl->transform_negotiate);
980 }
Jerry Yu2e199812022-12-01 18:57:19 +0800981#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if (ssl->session_negotiate) {
983 mbedtls_ssl_session_free(ssl->session_negotiate);
984 }
985 if (ssl->handshake) {
986 mbedtls_ssl_handshake_free(ssl);
987 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988
Jerry Yu2e199812022-12-01 18:57:19 +0800989#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200990 /*
991 * Either the pointers are now NULL or cleared properly and can be freed.
992 * Now allocate missing structures.
993 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (ssl->transform_negotiate == NULL) {
995 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200996 }
Jerry Yu2e199812022-12-01 18:57:19 +0800997#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +0000998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (ssl->session_negotiate == NULL) {
1000 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001001 }
Paul Bakker48916f92012-09-16 19:57:18 +00001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if (ssl->handshake == NULL) {
1004 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001005 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001006#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1007 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1010 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001011#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001012
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001013 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001015#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001016 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001017#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 ssl->session_negotiate == NULL) {
1019 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001022 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001023
1024#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001026 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001027#endif
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001030 ssl->session_negotiate = NULL;
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001033 }
1034
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001035 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 mbedtls_ssl_session_init(ssl->session_negotiate);
1037 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001038
Jerry Yu2e199812022-12-01 18:57:19 +08001039#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001041#endif
1042
Jerry Yud0766ec2022-09-22 10:46:57 +08001043#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1044 defined(MBEDTLS_SSL_SRV_C) && \
1045 defined(MBEDTLS_SSL_SESSION_TICKETS)
1046 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001048#endif
1049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001052 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001055 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001057 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001061 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001062#endif
1063
Brett Warrene0edc842021-08-17 09:53:13 +01001064/*
1065 * curve_list is translated to IANA TLS group identifiers here because
1066 * mbedtls_ssl_conf_curves returns void and so can't return
1067 * any error codes.
1068 */
1069#if defined(MBEDTLS_ECP_C)
1070#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1071 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001073 size_t length;
1074 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1077 (length < MBEDTLS_ECP_DP_MAX); length++) {
1078 }
Brett Warrene0edc842021-08-17 09:53:13 +01001079
1080 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1082 if (group_list == NULL) {
1083 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1084 }
Brett Warrene0edc842021-08-17 09:53:13 +01001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001087 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 curve_list[i]);
1089 if (tls_id == 0) {
1090 mbedtls_free(group_list);
1091 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001092 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001093 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001094 }
1095
1096 group_list[length] = 0;
1097
1098 ssl->handshake->group_list = group_list;
1099 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001101 ssl->handshake->group_list = ssl->conf->group_list;
1102 ssl->handshake->group_list_heap_allocated = 0;
1103 }
1104#endif /* MBEDTLS_DEPRECATED_REMOVED */
1105#endif /* MBEDTLS_ECP_C */
1106
Ronald Crone68ab4f2022-10-05 12:46:29 +02001107#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001108#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001109#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001110 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1111 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1113 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001114 const int *md;
1115 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001116 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001117 uint16_t *p;
1118
Jerry Yub476a442022-01-21 18:14:45 +08001119#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1121 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1122 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001123#endif
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1126 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001127 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 }
Jerry Yub476a442022-01-21 18:14:45 +08001129#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001131#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001132
Jerry Yub476a442022-01-21 18:14:45 +08001133#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001135#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1137 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1138 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001139 }
1140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1142 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1143 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1146 sizeof(uint16_t));
1147 if (ssl->handshake->sig_algs == NULL) {
1148 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1149 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 p = (uint16_t *) ssl->handshake->sig_algs;
1152 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1153 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1154 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001155 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001157#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001159 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001160#endif
1161#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001163 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001164#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001165 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001166 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001167 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001169#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001170 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001171 ssl->handshake->sig_algs_heap_allocated = 0;
1172 }
Jerry Yucc539102022-06-27 16:27:35 +08001173#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001174#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001176}
1177
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001178#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001179/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001180MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001181static int ssl_cookie_write_dummy(void *ctx,
1182 unsigned char **p, unsigned char *end,
1183 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001184{
1185 ((void) ctx);
1186 ((void) p);
1187 ((void) end);
1188 ((void) cli_id);
1189 ((void) cli_id_len);
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001192}
1193
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001194MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001195static int ssl_cookie_check_dummy(void *ctx,
1196 const unsigned char *cookie, size_t cookie_len,
1197 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001198{
1199 ((void) ctx);
1200 ((void) cookie);
1201 ((void) cookie_len);
1202 ((void) cli_id);
1203 ((void) cli_id_len);
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001206}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001207#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001208
Paul Bakker5121ce52009-01-03 21:22:43 +00001209/*
1210 * Initialize an SSL context
1211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001213{
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001215}
1216
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001217MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001218static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001219{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001220 const mbedtls_ssl_config *conf = ssl->conf;
1221
Ronald Cron6f135e12021-12-08 16:57:54 +01001222#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1224 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1225 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1226 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001227 }
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1230 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001231 }
1232#endif
1233
1234#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1236 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1237 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001238 }
1239#endif
1240
Ronald Cron6f135e12021-12-08 16:57:54 +01001241#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1243 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1244 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1245 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001246 }
1247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1249 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1250 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001251 }
1252
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1255 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001256 }
1257#endif
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1260 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001261}
1262
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001263MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001264static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1265{
1266 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 ret = ssl_conf_version_check(ssl);
1268 if (ret != 0) {
1269 return ret;
1270 }
Jerry Yu60835a82021-08-04 10:13:52 +08001271
Jerry Yudef7ae42022-10-30 14:13:19 +08001272#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1273 /* RFC 8446 section 4.4.3
1274 *
1275 * If the verification fails, the receiver MUST terminate the handshake with
1276 * a "decrypt_error" alert.
1277 *
1278 * If the client is configured as TLS 1.3 only with optional verify, return
1279 * bad config.
1280 *
1281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1283 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001284 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1285 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1286 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001288 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 1, ("Optional verify auth mode "
1290 "is not available for TLS 1.3 client"));
1291 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001292 }
1293#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1294
Jerry Yu60835a82021-08-04 10:13:52 +08001295 /* Space for further checks */
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001298}
1299
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001300/*
1301 * Setup an SSL context
1302 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1305 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001306{
Janos Follath865b3eb2019-12-16 11:46:15 +00001307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001308 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1309 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001311 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if ((ret = ssl_conf_check(ssl)) != 0) {
1314 return ret;
1315 }
Jerry Yu60835a82021-08-04 10:13:52 +08001316
Paul Bakker62f2dee2012-09-28 07:31:51 +00001317 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001318 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001319 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001320
1321 /* Set to NULL in case of an error condition */
1322 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001323
Darryl Greenb33cc762019-11-28 14:29:44 +00001324#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1325 ssl->in_buf_len = in_buf_len;
1326#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1328 if (ssl->in_buf == NULL) {
1329 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001330 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001331 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001332 }
1333
Darryl Greenb33cc762019-11-28 14:29:44 +00001334#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1335 ssl->out_buf_len = out_buf_len;
1336#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1338 if (ssl->out_buf == NULL) {
1339 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001340 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001341 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
1343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001345
Johan Pascalb62bb512015-12-03 21:56:45 +01001346#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001348#endif
1349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001351 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001355
1356error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 mbedtls_free(ssl->in_buf);
1358 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001359
1360 ssl->conf = NULL;
1361
Darryl Greenb33cc762019-11-28 14:29:44 +00001362#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1363 ssl->in_buf_len = 0;
1364 ssl->out_buf_len = 0;
1365#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001366 ssl->in_buf = NULL;
1367 ssl->out_buf = NULL;
1368
1369 ssl->in_hdr = NULL;
1370 ssl->in_ctr = NULL;
1371 ssl->in_len = NULL;
1372 ssl->in_iv = NULL;
1373 ssl->in_msg = NULL;
1374
1375 ssl->out_hdr = NULL;
1376 ssl->out_ctr = NULL;
1377 ssl->out_len = NULL;
1378 ssl->out_iv = NULL;
1379 ssl->out_msg = NULL;
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382}
1383
1384/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001385 * Reset an initialized and used SSL context for re-use while retaining
1386 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001387 *
1388 * If partial is non-zero, keep data in the input buffer and client ID.
1389 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1392 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001393{
Darryl Greenb33cc762019-11-28 14:29:44 +00001394#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1395 size_t in_buf_len = ssl->in_buf_len;
1396 size_t out_buf_len = ssl->out_buf_len;
1397#else
1398 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1399 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1400#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001401
Hanno Beckerb0302c42021-08-03 09:39:42 +01001402#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1403 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001404#endif
1405
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001406 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001410
1411 /* Reset incoming message parsing */
1412 ssl->in_offt = NULL;
1413 ssl->nb_zero = 0;
1414 ssl->in_msgtype = 0;
1415 ssl->in_msglen = 0;
1416 ssl->in_hslen = 0;
1417 ssl->keep_current_message = 0;
1418 ssl->transform_in = NULL;
1419
1420#if defined(MBEDTLS_SSL_PROTO_DTLS)
1421 ssl->next_record_offset = 0;
1422 ssl->in_epoch = 0;
1423#endif
1424
1425 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001427 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001429 }
1430
Ronald Cronad8c17b2022-06-10 17:18:09 +02001431 ssl->send_alert = 0;
1432
Hanno Beckerb0302c42021-08-03 09:39:42 +01001433 /* Reset outgoing message writing */
1434 ssl->out_msgtype = 0;
1435 ssl->out_msglen = 0;
1436 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 memset(ssl->out_buf, 0, out_buf_len);
1438 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001439 ssl->transform_out = NULL;
1440
1441#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001443#endif
1444
XiaokangQian2b01dc32022-01-21 02:53:13 +00001445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if (ssl->transform) {
1447 mbedtls_ssl_transform_free(ssl->transform);
1448 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001449 ssl->transform = NULL;
1450 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1452
XiaokangQian2b01dc32022-01-21 02:53:13 +00001453#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 mbedtls_ssl_transform_free(ssl->transform_application);
1455 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001456 ssl->transform_application = NULL;
1457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001459#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1461 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001462 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001463#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1466 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001467 ssl->handshake->transform_handshake = NULL;
1468 }
1469
XiaokangQian2b01dc32022-01-21 02:53:13 +00001470#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001471}
1472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001474{
1475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1476
1477 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001480
1481 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_SSL_RENEGOTIATION)
1483 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001484 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001485
1486 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1488 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001489#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001491
Hanno Beckerb0302c42021-08-03 09:39:42 +01001492 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001493 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (ssl->session) {
1495 mbedtls_ssl_session_free(ssl->session);
1496 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001497 ssl->session = NULL;
1498 }
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001501 ssl->alpn_chosen = NULL;
1502#endif
1503
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001504#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001505 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001506#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001508#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (free_cli_id) {
1510 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001511 ssl->cli_id = NULL;
1512 ssl->cli_id_len = 0;
1513 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001514#endif
1515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if ((ret = ssl_handshake_init(ssl)) != 0) {
1517 return ret;
1518 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001521}
1522
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001523/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001524 * Reset an initialized and used SSL context for re-use while retaining
1525 * all application-set variables, function pointers and data.
1526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001527int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001528{
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001530}
1531
1532/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 * SSL set accessors
1534 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001536{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001537 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001538}
1539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001541{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001542 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001543}
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001546void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001547{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001548 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001549}
1550#endif
1551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001553{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001554 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001555}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1560 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001561{
1562 ssl->disable_datagram_packing = !allow_packing;
1563}
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1566 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001568 conf->hs_timeout_min = min;
1569 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001570}
1571#endif
1572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001575 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001576}
1577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001579void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1580 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1581 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001582{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001583 conf->f_vrfy = f_vrfy;
1584 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001585}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1589 int (*f_rng)(void *, unsigned char *, size_t),
1590 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001591{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001592 conf->f_rng = f_rng;
1593 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594}
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1597 void (*f_dbg)(void *, int, const char *, int, const char *),
1598 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001599{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001600 conf->f_dbg = f_dbg;
1601 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602}
1603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1605 void *p_bio,
1606 mbedtls_ssl_send_t *f_send,
1607 mbedtls_ssl_recv_t *f_recv,
1608 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001609{
1610 ssl->p_bio = p_bio;
1611 ssl->f_send = f_send;
1612 ssl->f_recv = f_recv;
1613 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001614}
1615
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001617void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001618{
1619 ssl->mtu = mtu;
1620}
1621#endif
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001624{
1625 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001626}
1627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1629 void *p_timer,
1630 mbedtls_ssl_set_timer_t *f_set_timer,
1631 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001632{
1633 ssl->p_timer = p_timer;
1634 ssl->f_set_timer = f_set_timer;
1635 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001636
1637 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001639}
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001642void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1643 void *p_cache,
1644 mbedtls_ssl_cache_get_t *f_get_cache,
1645 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001646{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001647 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001648 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001649 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001654int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001655{
Janos Follath865b3eb2019-12-16 11:46:15 +00001656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001659 session == NULL ||
1660 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1662 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001663 }
1664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (ssl->handshake->resume == 1) {
1666 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1667 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001668
Jerry Yu21092062022-10-10 21:21:31 +08001669#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001671 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001675 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1677 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1678 session->ciphersuite));
1679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001680 }
Jerry Yu40afab62022-10-08 10:42:13 +08001681 }
Jerry Yu21092062022-10-10 21:21:31 +08001682#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1685 session)) != 0) {
1686 return ret;
1687 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001688
Paul Bakker0a597072012-09-25 21:55:46 +00001689 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1696 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001697{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001698 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001699}
1700
Ronald Cron6f135e12021-12-08 16:57:54 +01001701#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001702void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1703 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001704{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001705 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001706}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001707
1708#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001709void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1710 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001711{
1712 conf->early_data_enabled = early_data_enabled;
1713}
Jerry Yucc4e0072022-11-22 17:22:22 +08001714
1715#if defined(MBEDTLS_SSL_SRV_C)
1716void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001718{
Jerry Yu39da9852022-12-06 16:58:36 +08001719 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001720}
1721#endif /* MBEDTLS_SSL_SRV_C */
1722
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001723#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001724#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001727void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1728 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001729{
1730 conf->cert_profile = profile;
1731}
1732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001734{
1735 mbedtls_ssl_key_cert *cur = key_cert, *next;
1736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001738 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001740 cur = next;
1741 }
1742}
1743
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001744/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001745MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001746static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1747 mbedtls_x509_crt *cert,
1748 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001749{
niisato8ee24222018-06-25 19:05:48 +09001750 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001753 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001755 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001757 }
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1760 if (new_cert == NULL) {
1761 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1762 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001763
niisato8ee24222018-06-25 19:05:48 +09001764 new_cert->cert = cert;
1765 new_cert->key = key;
1766 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001767
Glenn Strauss36872db2022-01-22 05:06:31 -05001768 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001770 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001772 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001774 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 }
niisato8ee24222018-06-25 19:05:48 +09001776 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001777 }
1778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001780}
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001783 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001785{
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001787}
1788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001790 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001792{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001793 conf->ca_chain = ca_chain;
1794 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001795
1796#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1797 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1798 * cannot be used together. */
1799 conf->f_ca_cb = NULL;
1800 conf->p_ca_cb = NULL;
1801#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001802}
Hanno Becker5adaad92019-03-27 16:54:37 +00001803
1804#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001805void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1806 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1807 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001808{
1809 conf->f_ca_cb = f_ca_cb;
1810 conf->p_ca_cb = p_ca_cb;
1811
1812 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1813 * cannot be used together. */
1814 conf->ca_chain = NULL;
1815 conf->ca_crl = NULL;
1816}
1817#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001819
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001820#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001821const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1822 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001823{
1824 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001826}
1827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1829 mbedtls_x509_crt *own_cert,
1830 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001831{
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1833 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001834}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1837 mbedtls_x509_crt *ca_chain,
1838 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001839{
1840 ssl->handshake->sni_ca_chain = ca_chain;
1841 ssl->handshake->sni_ca_crl = ca_crl;
1842}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001843
Glenn Strauss999ef702022-03-11 01:37:23 -05001844#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001845void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1846 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001847{
1848 ssl->handshake->dn_hints = crt;
1849}
1850#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1853 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001854{
1855 ssl->handshake->sni_authmode = authmode;
1856}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001857#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1858
Hanno Becker8927c832019-04-03 12:52:50 +01001859#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001860void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1861 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1862 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001863{
1864 ssl->f_vrfy = f_vrfy;
1865 ssl->p_vrfy = p_vrfy;
1866}
1867#endif
1868
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001869#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001870
Valerio Setti016f6822022-12-09 14:17:50 +01001871#if defined(MBEDTLS_USE_PSA_CRYPTO)
1872static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 mbedtls_ssl_context *ssl,
1874 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001875{
1876 psa_status_t status;
1877 psa_pake_role_t psa_role;
1878 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1881 psa_pake_cs_set_primitive(&cipher_suite,
1882 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1883 PSA_ECC_FAMILY_SECP_R1,
1884 256));
1885 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1888 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001889 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001893 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001895 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1899 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001900 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 }
Valerio Setti016f6822022-12-09 14:17:50 +01001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1904 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001905 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 }
Valerio Setti016f6822022-12-09 14:17:50 +01001907
1908 ssl->handshake->psa_pake_ctx_is_ok = 1;
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001911}
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1914 const unsigned char *pw,
1915 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001916{
1917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1918 psa_status_t status;
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 if (ssl->handshake == NULL || ssl->conf == NULL) {
1921 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001922 }
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 /* Empty password is not valid */
1925 if ((pw == NULL) || (pw_len == 0)) {
1926 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1927 }
1928
1929 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1930 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1931 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1932
1933 status = psa_import_key(&attributes, pw, pw_len,
1934 &ssl->handshake->psa_pake_password);
1935 if (status != PSA_SUCCESS) {
1936 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1937 }
1938
1939 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1940 ssl->handshake->psa_pake_password);
1941 if (status != PSA_SUCCESS) {
1942 psa_destroy_key(ssl->handshake->psa_pake_password);
1943 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1944 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1945 }
1946
1947 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001948}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1951 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001952{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001953 psa_status_t status;
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (ssl->handshake == NULL || ssl->conf == NULL) {
1956 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001957 }
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (mbedtls_svc_key_id_is_null(pwd)) {
1960 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1961 }
1962
1963 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1964 if (status != PSA_SUCCESS) {
1965 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1966 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1967 }
1968
1969 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001970}
1971#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1973 const unsigned char *pw,
1974 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001975{
1976 mbedtls_ecjpake_role role;
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (ssl->handshake == NULL || ssl->conf == NULL) {
1979 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1980 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001981
Valerio Settic689ed82022-12-07 14:40:38 +01001982 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if ((pw == NULL) || (pw_len == 0)) {
1984 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1985 }
Valerio Settic689ed82022-12-07 14:40:38 +01001986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001988 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001990 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
1994 role,
1995 MBEDTLS_MD_SHA256,
1996 MBEDTLS_ECP_DP_SECP256R1,
1997 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001998}
Valerio Setti02c25b52022-11-15 14:08:42 +01001999#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002000#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002001
Ronald Cron73fe8df2022-10-05 14:31:43 +02002002#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002003int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002004{
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 if (conf->psk_identity == NULL ||
2006 conf->psk_identity_len == 0) {
2007 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002008 }
2009
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002010#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2012 return 1;
2013 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002014#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if (conf->psk != NULL && conf->psk_len != 0) {
2017 return 1;
2018 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002021}
2022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002024{
2025 /* Remove reference to existing PSK, if any. */
2026#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002028 /* The maintenance of the PSK key slot is the
2029 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002030 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002031 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002032#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (conf->psk != NULL) {
2034 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002037 conf->psk = NULL;
2038 conf->psk_len = 0;
2039 }
2040
2041 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 if (conf->psk_identity != NULL) {
2043 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002044 conf->psk_identity = NULL;
2045 conf->psk_identity_len = 0;
2046 }
2047}
2048
Hanno Becker7390c712018-11-15 13:33:04 +00002049/* This function assumes that PSK identity in the SSL config is unset.
2050 * It checks that the provided identity is well-formed and attempts
2051 * to make a copy of it in the SSL config.
2052 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002054static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2055 unsigned char const *psk_identity,
2056 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002057{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002058 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002060 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 (psk_identity_len >> 16) != 0 ||
2062 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002064 }
2065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2067 if (conf->psk_identity == NULL) {
2068 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2069 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002070
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002071 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002075}
2076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2078 const unsigned char *psk, size_t psk_len,
2079 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002080{
Janos Follath865b3eb2019-12-16 11:46:15 +00002081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002082
2083 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2085 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2086 }
Hanno Becker7390c712018-11-15 13:33:04 +00002087
2088 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (psk == NULL) {
2090 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2091 }
2092 if (psk_len == 0) {
2093 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2094 }
2095 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2097 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2100 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2101 }
Hanno Becker7390c712018-11-15 13:33:04 +00002102 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002104
2105 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2107 if (ret != 0) {
2108 ssl_conf_remove_psk(conf);
2109 }
Hanno Becker7390c712018-11-15 13:33:04 +00002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002112}
2113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002115{
2116#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002118 /* The maintenance of the external PSK key slot is the
2119 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (ssl->handshake->psk_opaque_is_internal) {
2121 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002122 ssl->handshake->psk_opaque_is_internal = 0;
2123 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002124 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002125 }
Neil Armstronge952a302022-05-03 10:22:14 +02002126#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (ssl->handshake->psk != NULL) {
2128 mbedtls_platform_zeroize(ssl->handshake->psk,
2129 ssl->handshake->psk_len);
2130 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002131 ssl->handshake->psk_len = 0;
2132 }
Neil Armstronge952a302022-05-03 10:22:14 +02002133#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002134}
2135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2137 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002138{
Neil Armstrong501c9322022-05-03 09:35:09 +02002139#if defined(MBEDTLS_USE_PSA_CRYPTO)
2140 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002142 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002144#endif /* MBEDTLS_USE_PSA_CRYPTO */
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (psk == NULL || ssl->handshake == NULL) {
2147 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2148 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2152 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002155
Neil Armstrong501c9322022-05-03 09:35:09 +02002156#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002157#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2159 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2160 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2161 } else {
2162 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2163 }
2164 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002165 }
2166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002167
Jerry Yu568ec252022-07-22 21:27:34 +08002168#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2170 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2171 psa_set_key_usage_flags(&key_attributes,
2172 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002173 }
2174#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 psa_set_key_algorithm(&key_attributes, alg);
2177 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2180 if (status != PSA_SUCCESS) {
2181 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2182 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002183
2184 /* Allow calling psa_destroy_key() on psk remove */
2185 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002187#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2189 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2190 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002191
2192 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002196#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002197}
2198
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002199#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002200int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2201 mbedtls_svc_key_id_t psk,
2202 const unsigned char *psk_identity,
2203 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002204{
Janos Follath865b3eb2019-12-16 11:46:15 +00002205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002206
2207 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2209 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2210 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002211
Hanno Becker7390c712018-11-15 13:33:04 +00002212 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (mbedtls_svc_key_id_is_null(psk)) {
2214 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2215 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002216 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002217
2218 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2220 psk_identity_len);
2221 if (ret != 0) {
2222 ssl_conf_remove_psk(conf);
2223 }
Hanno Becker7390c712018-11-15 13:33:04 +00002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002226}
2227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2229 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002230{
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if ((mbedtls_svc_key_id_is_null(psk)) ||
2232 (ssl->handshake == NULL)) {
2233 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2234 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002237 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002239}
2240#endif /* MBEDTLS_USE_PSA_CRYPTO */
2241
Jerry Yu8897c072022-08-12 13:56:53 +08002242#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002243void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2244 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2245 size_t),
2246 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002247{
2248 conf->f_psk = f_psk;
2249 conf->p_psk = p_psk;
2250}
Jerry Yu8897c072022-08-12 13:56:53 +08002251#endif /* MBEDTLS_SSL_SRV_C */
2252
Ronald Cron73fe8df2022-10-05 14:31:43 +02002253#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002254
2255#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002256static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002258{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002259#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (alg == PSA_ALG_CBC_NO_PADDING) {
2261 return MBEDTLS_SSL_MODE_CBC;
2262 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002263#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (PSA_ALG_IS_AEAD(alg)) {
2265 return MBEDTLS_SSL_MODE_AEAD;
2266 }
2267 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002268}
2269
2270#else /* MBEDTLS_USE_PSA_CRYPTO */
2271
2272static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002274{
2275#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (mode == MBEDTLS_MODE_CBC) {
2277 return MBEDTLS_SSL_MODE_CBC;
2278 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002279#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2280
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002281#if defined(MBEDTLS_GCM_C) || \
2282 defined(MBEDTLS_CCM_C) || \
2283 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002285 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 mode == MBEDTLS_MODE_CHACHAPOLY) {
2287 return MBEDTLS_SSL_MODE_AEAD;
2288 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002289#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002292}
Gilles Peskine301711e2022-04-26 16:57:05 +02002293#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002294
Gilles Peskinee108d982022-04-26 16:50:40 +02002295static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2296 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002298{
2299#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2301 base_mode == MBEDTLS_SSL_MODE_CBC) {
2302 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002303 }
2304#else
2305 (void) encrypt_then_mac;
2306#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002308}
2309
Neil Armstrongab555e02022-04-04 11:07:59 +02002310mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002312{
Gilles Peskinee108d982022-04-26 16:50:40 +02002313 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002314#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002316#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002318#endif
2319 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002320
Gilles Peskinee108d982022-04-26 16:50:40 +02002321 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002322#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002323 encrypt_then_mac = transform->encrypt_then_mac;
2324#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002326}
2327
Neil Armstrongab555e02022-04-04 11:07:59 +02002328mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002329#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002331#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002333{
Gilles Peskinee108d982022-04-26 16:50:40 +02002334 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2335
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002336#if defined(MBEDTLS_USE_PSA_CRYPTO)
2337 psa_status_t status;
2338 psa_algorithm_t alg;
2339 psa_key_type_t type;
2340 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2342 if (status == PSA_SUCCESS) {
2343 base_mode = mbedtls_ssl_get_base_mode(alg);
2344 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002345#else
2346 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 mbedtls_cipher_info_from_type(suite->cipher);
2348 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002349 base_mode =
2350 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002352 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002353#endif /* MBEDTLS_USE_PSA_CRYPTO */
2354
Gilles Peskinee108d982022-04-26 16:50:40 +02002355#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2356 int encrypt_then_mac = 0;
2357#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002359}
2360
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002361#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002362
2363#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002364/* Serialization of TLS 1.3 sessions:
2365 *
2366 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002367 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002368 * uint64 ticket_received;
2369 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002370 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002371 * } ClientOnlyData;
2372 *
2373 * struct {
2374 * uint8 endpoint;
2375 * uint8 ciphersuite[2];
2376 * uint32 ticket_age_add;
2377 * uint8 ticket_flags;
2378 * opaque resumption_key<0..255>;
2379 * select ( endpoint ) {
2380 * case client: ClientOnlyData;
2381 * case server: uint64 start_time;
2382 * };
2383 * } serialized_session_tls13;
2384 *
2385 */
2386#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002387MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002388static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2389 unsigned char *buf,
2390 size_t buf_len,
2391 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002392{
2393 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002394#if defined(MBEDTLS_SSL_CLI_C) && \
2395 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 size_t hostname_len = (session->hostname == NULL) ?
2397 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002398#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002399 size_t needed = 1 /* endpoint */
2400 + 2 /* ciphersuite */
2401 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002402 + 1 /* ticket_flags */
2403 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002404 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2407 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2408 }
Jerry Yu34191072022-08-18 10:32:09 +08002409 needed += session->resumption_key_len; /* resumption_key */
2410
Jerry Yu438ddd82022-07-07 06:55:50 +00002411#if defined(MBEDTLS_HAVE_TIME)
2412 needed += 8; /* start_time or ticket_received */
2413#endif
2414
2415#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002417#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002418 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002420#endif
2421
Jerry Yu438ddd82022-07-07 06:55:50 +00002422 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002423 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002424
2425 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (session->ticket_len > SIZE_MAX - needed) {
2427 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2428 }
Jerry Yu34191072022-08-18 10:32:09 +08002429
Jerry Yue28d9742022-08-18 15:44:03 +08002430 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002431 }
2432#endif /* MBEDTLS_SSL_CLI_C */
2433
Jerry Yue36fdd62022-08-17 21:31:36 +08002434 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (needed > buf_len) {
2436 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2437 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002438
2439 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2441 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002442 p[7] = session->ticket_flags;
2443
2444 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002445 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002446 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002448 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002449
2450#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2452 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002453 p += 8;
2454 }
2455#endif /* MBEDTLS_HAVE_TIME */
2456
2457#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002459#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002461 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002463 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002465 p += hostname_len;
2466 }
2467#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2468
Jerry Yu438ddd82022-07-07 06:55:50 +00002469#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002471 p += 8;
2472#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002474 p += 4;
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002477 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (session->ticket != NULL && session->ticket_len > 0) {
2480 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002481 p += session->ticket_len;
2482 }
2483 }
2484#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002486}
2487
2488MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002489static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2490 const unsigned char *buf,
2491 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002492{
2493 const unsigned char *p = buf;
2494 const unsigned char *end = buf + len;
2495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (end - p < 9) {
2497 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2498 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002499 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2501 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002502 session->ticket_flags = p[7];
2503
2504 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002505 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002506 p += 9;
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (end - p < session->resumption_key_len) {
2509 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2510 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2513 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2514 }
2515 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002516 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002517
2518#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2520 if (end - p < 8) {
2521 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2522 }
2523 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002524 p += 8;
2525 }
2526#endif /* MBEDTLS_HAVE_TIME */
2527
2528#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002530#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002532 size_t hostname_len;
2533 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (end - p < 2) {
2535 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2536 }
2537 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002538 p += 2;
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (end - p < (long int) hostname_len) {
2541 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2542 }
2543 if (hostname_len > 0) {
2544 session->hostname = mbedtls_calloc(1, hostname_len);
2545 if (session->hostname == NULL) {
2546 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2547 }
2548 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002549 p += hostname_len;
2550 }
2551#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2552 MBEDTLS_SSL_SESSION_TICKETS */
2553
Jerry Yu438ddd82022-07-07 06:55:50 +00002554#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (end - p < 8) {
2556 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2557 }
2558 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002559 p += 8;
2560#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (end - p < 4) {
2562 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2563 }
2564 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002565 p += 4;
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (end - p < 2) {
2568 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2569 }
2570 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002571 p += 2;
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (end - p < (long int) session->ticket_len) {
2574 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2575 }
2576 if (session->ticket_len > 0) {
2577 session->ticket = mbedtls_calloc(1, session->ticket_len);
2578 if (session->ticket == NULL) {
2579 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2580 }
2581 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002582 p += session->ticket_len;
2583 }
2584 }
2585#endif /* MBEDTLS_SSL_CLI_C */
2586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002588
2589}
2590#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002591MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002592static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2593 unsigned char *buf,
2594 size_t buf_len,
2595 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002596{
2597 ((void) session);
2598 ((void) buf);
2599 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002600 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002602}
Jerry Yu438ddd82022-07-07 06:55:50 +00002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2605 unsigned char *buf,
2606 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002607{
2608 ((void) session);
2609 ((void) buf);
2610 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002612}
2613#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002614#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2617 size_t taglen,
2618 psa_algorithm_t *alg,
2619 psa_key_type_t *key_type,
2620 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002621{
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002623 case MBEDTLS_CIPHER_AES_128_CBC:
2624 *alg = PSA_ALG_CBC_NO_PADDING;
2625 *key_type = PSA_KEY_TYPE_AES;
2626 *key_size = 128;
2627 break;
2628 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002630 *key_type = PSA_KEY_TYPE_AES;
2631 *key_size = 128;
2632 break;
2633 case MBEDTLS_CIPHER_AES_128_GCM:
2634 *alg = PSA_ALG_GCM;
2635 *key_type = PSA_KEY_TYPE_AES;
2636 *key_size = 128;
2637 break;
2638 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002640 *key_type = PSA_KEY_TYPE_AES;
2641 *key_size = 192;
2642 break;
2643 case MBEDTLS_CIPHER_AES_192_GCM:
2644 *alg = PSA_ALG_GCM;
2645 *key_type = PSA_KEY_TYPE_AES;
2646 *key_size = 192;
2647 break;
2648 case MBEDTLS_CIPHER_AES_256_CBC:
2649 *alg = PSA_ALG_CBC_NO_PADDING;
2650 *key_type = PSA_KEY_TYPE_AES;
2651 *key_size = 256;
2652 break;
2653 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002655 *key_type = PSA_KEY_TYPE_AES;
2656 *key_size = 256;
2657 break;
2658 case MBEDTLS_CIPHER_AES_256_GCM:
2659 *alg = PSA_ALG_GCM;
2660 *key_type = PSA_KEY_TYPE_AES;
2661 *key_size = 256;
2662 break;
2663 case MBEDTLS_CIPHER_ARIA_128_CBC:
2664 *alg = PSA_ALG_CBC_NO_PADDING;
2665 *key_type = PSA_KEY_TYPE_ARIA;
2666 *key_size = 128;
2667 break;
2668 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002670 *key_type = PSA_KEY_TYPE_ARIA;
2671 *key_size = 128;
2672 break;
2673 case MBEDTLS_CIPHER_ARIA_128_GCM:
2674 *alg = PSA_ALG_GCM;
2675 *key_type = PSA_KEY_TYPE_ARIA;
2676 *key_size = 128;
2677 break;
2678 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002680 *key_type = PSA_KEY_TYPE_ARIA;
2681 *key_size = 192;
2682 break;
2683 case MBEDTLS_CIPHER_ARIA_192_GCM:
2684 *alg = PSA_ALG_GCM;
2685 *key_type = PSA_KEY_TYPE_ARIA;
2686 *key_size = 192;
2687 break;
2688 case MBEDTLS_CIPHER_ARIA_256_CBC:
2689 *alg = PSA_ALG_CBC_NO_PADDING;
2690 *key_type = PSA_KEY_TYPE_ARIA;
2691 *key_size = 256;
2692 break;
2693 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002695 *key_type = PSA_KEY_TYPE_ARIA;
2696 *key_size = 256;
2697 break;
2698 case MBEDTLS_CIPHER_ARIA_256_GCM:
2699 *alg = PSA_ALG_GCM;
2700 *key_type = PSA_KEY_TYPE_ARIA;
2701 *key_size = 256;
2702 break;
2703 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2704 *alg = PSA_ALG_CBC_NO_PADDING;
2705 *key_type = PSA_KEY_TYPE_CAMELLIA;
2706 *key_size = 128;
2707 break;
2708 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002710 *key_type = PSA_KEY_TYPE_CAMELLIA;
2711 *key_size = 128;
2712 break;
2713 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2714 *alg = PSA_ALG_GCM;
2715 *key_type = PSA_KEY_TYPE_CAMELLIA;
2716 *key_size = 128;
2717 break;
2718 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002720 *key_type = PSA_KEY_TYPE_CAMELLIA;
2721 *key_size = 192;
2722 break;
2723 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2724 *alg = PSA_ALG_GCM;
2725 *key_type = PSA_KEY_TYPE_CAMELLIA;
2726 *key_size = 192;
2727 break;
2728 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2729 *alg = PSA_ALG_CBC_NO_PADDING;
2730 *key_type = PSA_KEY_TYPE_CAMELLIA;
2731 *key_size = 256;
2732 break;
2733 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002735 *key_type = PSA_KEY_TYPE_CAMELLIA;
2736 *key_size = 256;
2737 break;
2738 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2739 *alg = PSA_ALG_GCM;
2740 *key_type = PSA_KEY_TYPE_CAMELLIA;
2741 *key_size = 256;
2742 break;
2743 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2744 *alg = PSA_ALG_CHACHA20_POLY1305;
2745 *key_type = PSA_KEY_TYPE_CHACHA20;
2746 *key_size = 256;
2747 break;
2748 case MBEDTLS_CIPHER_NULL:
2749 *alg = MBEDTLS_SSL_NULL_CIPHER;
2750 *key_type = 0;
2751 *key_size = 0;
2752 break;
2753 default:
2754 return PSA_ERROR_NOT_SUPPORTED;
2755 }
2756
2757 return PSA_SUCCESS;
2758}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002759#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002760
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002761#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002762int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2763 const unsigned char *dhm_P, size_t P_len,
2764 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002765{
Janos Follath865b3eb2019-12-16 11:46:15 +00002766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 mbedtls_mpi_free(&conf->dhm_P);
2769 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2772 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2773 mbedtls_mpi_free(&conf->dhm_P);
2774 mbedtls_mpi_free(&conf->dhm_G);
2775 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002776 }
2777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002779}
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002782{
Janos Follath865b3eb2019-12-16 11:46:15 +00002783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 mbedtls_mpi_free(&conf->dhm_P);
2786 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2789 &conf->dhm_P)) != 0 ||
2790 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2791 &conf->dhm_G)) != 0) {
2792 mbedtls_mpi_free(&conf->dhm_P);
2793 mbedtls_mpi_free(&conf->dhm_G);
2794 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002795 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002798}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002799#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002800
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002801#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2802/*
2803 * Set the minimum length for Diffie-Hellman parameters
2804 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2806 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002807{
2808 conf->dhm_min_bitlen = bitlen;
2809}
2810#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2811
Ronald Crone68ab4f2022-10-05 12:46:29 +02002812#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002813#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002814/*
2815 * Set allowed/preferred hashes for handshake signatures
2816 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002817void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2818 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002819{
2820 conf->sig_hashes = hashes;
2821}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002822#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002823
Jerry Yuf017ee42022-01-12 15:49:48 +08002824/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002825void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2826 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002827{
Jerry Yuf017ee42022-01-12 15:49:48 +08002828#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2829 conf->sig_hashes = NULL;
2830#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2831 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002832}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002833#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002834
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002835#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002836#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002837/*
2838 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002839 *
2840 * mbedtls_ssl_setup() takes the provided list
2841 * and translates it to a list of IANA TLS group identifiers,
2842 * stored in ssl->handshake->group_list.
2843 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002845void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2846 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002847{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002848 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002849 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002850}
Brett Warrene0edc842021-08-17 09:53:13 +01002851#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002852#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002853
Brett Warrene0edc842021-08-17 09:53:13 +01002854/*
2855 * Set the allowed groups
2856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002857void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2858 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002859{
2860#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2861 conf->curve_list = NULL;
2862#endif
2863 conf->group_list = group_list;
2864}
2865
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002866#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002867int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002868{
Hanno Becker947194e2017-04-07 13:25:49 +01002869 /* Initialize to suppress unnecessary compiler warning */
2870 size_t hostname_len = 0;
2871
2872 /* Check if new hostname is valid before
2873 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (hostname != NULL) {
2875 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2878 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2879 }
Hanno Becker947194e2017-04-07 13:25:49 +01002880 }
2881
2882 /* Now it's clear that we will overwrite the old hostname,
2883 * so we can free it safely */
2884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 if (ssl->hostname != NULL) {
2886 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2887 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002888 }
2889
2890 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002893 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 } else {
2895 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2896 if (ssl->hostname == NULL) {
2897 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2898 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002899
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002901
Hanno Becker947194e2017-04-07 13:25:49 +01002902 ssl->hostname[hostname_len] = '\0';
2903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002906}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002907#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002909#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002910void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2911 int (*f_sni)(void *, mbedtls_ssl_context *,
2912 const unsigned char *, size_t),
2913 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002915 conf->f_sni = f_sni;
2916 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002921int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002922{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002923 size_t cur_len, tot_len;
2924 const char **p;
2925
2926 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002927 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2928 * MUST NOT be truncated."
2929 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002930 */
2931 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 for (p = protos; *p != NULL; p++) {
2933 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002934 tot_len += cur_len;
2935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if ((cur_len == 0) ||
2937 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2938 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2939 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2940 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002941 }
2942
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002943 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002946}
2947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002949{
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002953
Johan Pascalb62bb512015-12-03 21:56:45 +01002954#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002955void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2956 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002957{
2958 conf->dtls_srtp_mki_support = support_mki_value;
2959}
2960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2962 unsigned char *mki_value,
2963 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002964{
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2966 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002967 }
Ron Eldor591f1622018-01-22 12:30:04 +02002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2970 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002971 }
Ron Eldor591f1622018-01-22 12:30:04 +02002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002974 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002976}
2977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2979 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002980{
Johan Pascal253d0262020-09-22 13:04:45 +02002981 const mbedtls_ssl_srtp_profile *p;
2982 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002983
Johan Pascal253d0262020-09-22 13:04:45 +02002984 /* check the profiles list: all entry must be valid,
2985 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2987 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2988 p++) {
2989 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002990 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002992 /* unsupported value, stop parsing and set the size to an error value */
2993 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002994 }
2995 }
2996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2998 conf->dtls_srtp_profile_list = NULL;
2999 conf->dtls_srtp_profile_list_len = 0;
3000 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003001 }
3002
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003003 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003004 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003007}
3008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3010 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003011{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003012 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3013 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003015 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003017 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3019 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003020 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003021}
Johan Pascalb62bb512015-12-03 21:56:45 +01003022#endif /* MBEDTLS_SSL_DTLS_SRTP */
3023
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303024#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003025void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003026{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003027 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003028}
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003031{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003032 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003033}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303034#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003035
Janos Follath088ce432017-04-10 12:42:31 +01003036#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003037void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3038 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003039{
3040 conf->cert_req_ca_list = cert_req_ca_list;
3041}
3042#endif
3043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003045void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003046{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003047 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003048}
3049#endif
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003052void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003054 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003055}
3056#endif
3057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003059int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003060{
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3062 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003064 }
3065
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003066 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003073{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003074 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003075}
3076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003078void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003079{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003080 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003081}
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003084{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003085 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003086}
3087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3089 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003090{
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003096#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003097void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003098{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003099 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003100}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003101#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003102
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003103#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003104
Jerry Yud0766ec2022-09-22 10:46:57 +08003105#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003106void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3107 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003108{
Jerry Yud0766ec2022-09-22 10:46:57 +08003109 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003110}
3111#endif
3112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3114 mbedtls_ssl_ticket_write_t *f_ticket_write,
3115 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3116 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003117{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003118 conf->f_ticket_write = f_ticket_write;
3119 conf->f_ticket_parse = f_ticket_parse;
3120 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003121}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3126 mbedtls_ssl_export_keys_t *f_export_keys,
3127 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003128{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003129 ssl->f_export_keys = f_export_keys;
3130 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003131}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003132
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003133#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003134void mbedtls_ssl_conf_async_private_cb(
3135 mbedtls_ssl_config *conf,
3136 mbedtls_ssl_async_sign_t *f_async_sign,
3137 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3138 mbedtls_ssl_async_resume_t *f_async_resume,
3139 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003141{
3142 conf->f_async_sign_start = f_async_sign;
3143 conf->f_async_decrypt_start = f_async_decrypt;
3144 conf->f_async_resume = f_async_resume;
3145 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003146 conf->p_async_config_data = async_config_data;
3147}
3148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003150{
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003152}
3153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003155{
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 if (ssl->handshake == NULL) {
3157 return NULL;
3158 } else {
3159 return ssl->handshake->user_async_ctx;
3160 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003161}
3162
Gilles Peskine449bd832023-01-11 14:50:10 +01003163void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3164 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003165{
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003167 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003169}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003170#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003171
Paul Bakker5121ce52009-01-03 21:22:43 +00003172/*
3173 * SSL get accessors
3174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003176{
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 if (ssl->session != NULL) {
3178 return ssl->session->verify_result;
3179 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003180
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 if (ssl->session_negotiate != NULL) {
3182 return ssl->session_negotiate->verify_result;
3183 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003186}
3187
Gilles Peskine449bd832023-01-11 14:50:10 +01003188int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003189{
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 if (ssl == NULL || ssl->session == NULL) {
3191 return 0;
3192 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003195}
3196
Gilles Peskine449bd832023-01-11 14:50:10 +01003197const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003198{
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 if (ssl == NULL || ssl->session == NULL) {
3200 return NULL;
3201 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003204}
3205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3210 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003211 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003213 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003215 }
3216 }
3217#endif
3218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003220 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003222 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003224 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003226 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003227}
3228
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003229#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003230size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003231{
David Horstmann95d516f2021-05-04 18:36:56 +01003232 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003233 size_t read_mfl;
3234
Jerry Yuddda0502022-12-01 19:43:12 +08003235#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003236 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3238 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3239 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003240 }
Jerry Yuddda0502022-12-01 19:43:12 +08003241#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003242
3243 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 if (ssl->session_out != NULL) {
3245 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3246 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003247 max_len = read_mfl;
3248 }
3249 }
3250
Jerry Yuddda0502022-12-01 19:43:12 +08003251 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 if (ssl->session_negotiate != NULL) {
3253 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3254 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003255 max_len = read_mfl;
3256 }
3257 }
3258
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003260}
3261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003263{
3264 size_t max_len;
3265
3266 /*
3267 * Assume mfl_code is correct since it was checked when set
3268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003270
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003271 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 if (ssl->session_out != NULL &&
3273 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3274 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003275 }
3276
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003277 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (ssl->session_negotiate != NULL &&
3279 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3280 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003281 }
3282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003284}
3285#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3286
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003287#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003288size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003289{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003290 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3292 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3293 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3294 return 0;
3295 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3298 return ssl->mtu;
3299 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (ssl->mtu == 0) {
3302 return ssl->handshake->mtu;
3303 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003304
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 return ssl->mtu < ssl->handshake->mtu ?
3306 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003307}
3308#endif /* MBEDTLS_SSL_PROTO_DTLS */
3309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003311{
3312 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3313
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003314#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3315 !defined(MBEDTLS_SSL_PROTO_DTLS)
3316 (void) ssl;
3317#endif
3318
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003321
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003323 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003325#endif
3326
3327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3329 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3330 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003331 const size_t overhead = (size_t) ret;
3332
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 if (ret < 0) {
3334 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003335 }
3336
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 if (mtu <= overhead) {
3338 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3339 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3340 }
3341
3342 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003343 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003345 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003346#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003347
Hanno Becker0defedb2018-08-10 12:35:02 +01003348#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3349 !defined(MBEDTLS_SSL_PROTO_DTLS)
3350 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003351#endif
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003354}
3355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003357{
3358 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3359
3360#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3361 (void) ssl;
3362#endif
3363
3364#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003368 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003370#endif
3371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003373}
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003376const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003377{
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 if (ssl == NULL || ssl->session == NULL) {
3379 return NULL;
3380 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003381
Hanno Beckere6824572019-02-07 13:18:46 +00003382#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003384#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003386#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003391int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3392 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003393{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003394 int ret;
3395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003397 dst == NULL ||
3398 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3400 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003401 }
3402
Hanno Beckere810bbc2021-05-14 16:01:05 +01003403 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3404 * idempotent: Each session can only be exported once.
3405 *
3406 * (This is in preparation for TLS 1.3 support where we will
3407 * need the ability to export multiple sessions (aka tickets),
3408 * which will be achieved by calling mbedtls_ssl_get_session()
3409 * multiple times until it fails.)
3410 *
3411 * Check whether we have already exported the current session,
3412 * and fail if so.
3413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 if (ssl->session->exported == 1) {
3415 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3416 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3419 if (ret != 0) {
3420 return ret;
3421 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003422
3423 /* Remember that we've exported the session. */
3424 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003428
Paul Bakker5121ce52009-01-03 21:22:43 +00003429/*
Hanno Beckera835da52019-05-16 12:39:07 +01003430 * Define ticket header determining Mbed TLS version
3431 * and structure of the ticket.
3432 */
3433
Hanno Becker94ef3b32019-05-16 12:50:45 +01003434/*
Hanno Becker50b59662019-05-28 14:30:45 +01003435 * Define bitflag determining compile-time settings influencing
3436 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003437 */
3438
Hanno Becker50b59662019-05-28 14:30:45 +01003439#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003440#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003441#else
Hanno Becker3e088662019-05-29 11:10:18 +01003442#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003443#endif /* MBEDTLS_HAVE_TIME */
3444
3445#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003446#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003447#else
Hanno Becker3e088662019-05-29 11:10:18 +01003448#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003449#endif /* MBEDTLS_X509_CRT_PARSE_C */
3450
3451#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003452#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003453#else
Hanno Becker3e088662019-05-29 11:10:18 +01003454#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003455#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3456
3457#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003458#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003459#else
Hanno Becker3e088662019-05-29 11:10:18 +01003460#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003461#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3462
Hanno Becker94ef3b32019-05-16 12:50:45 +01003463#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003464#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003465#else
Hanno Becker3e088662019-05-29 11:10:18 +01003466#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003467#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3468
Hanno Becker94ef3b32019-05-16 12:50:45 +01003469#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3470#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3471#else
3472#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3473#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3474
Hanno Becker3e088662019-05-29 11:10:18 +01003475#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3476#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3477#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3478#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003479#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3480#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003481
Hanno Becker50b59662019-05-28 14:30:45 +01003482#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 ((uint16_t) ( \
3484 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3485 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3486 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3487 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3488 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3489 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3490 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003491
Hanno Beckerf8787072019-05-16 12:41:07 +01003492static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003493 MBEDTLS_VERSION_MAJOR,
3494 MBEDTLS_VERSION_MINOR,
3495 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3497 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003498};
Hanno Beckera835da52019-05-16 12:39:07 +01003499
3500/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003501 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003502 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003503 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003504 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003505 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003506 * opaque mbedtls_version[3]; // library version: major, minor, patch
3507 * opaque session_format[2]; // library-version specific 16-bit field
3508 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003509 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003510 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003511 * Note: When updating the format, remember to keep
3512 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003513 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003514 * // In this version, `session_format` determines
3515 * // the setting of those compile-time
3516 * // configuration options which influence
3517 * // the structure of mbedtls_ssl_session.
3518 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003519 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003520 * // - TLS 1.2 (0x0303)
3521 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003522 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003523 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003524 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003525 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003526 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003527 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003528 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003529 *
3530 * };
3531 *
3532 * } serialized_session;
3533 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003534 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003535
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003536MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003537static int ssl_session_save(const mbedtls_ssl_session *session,
3538 unsigned char omit_header,
3539 unsigned char *buf,
3540 size_t buf_len,
3541 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003542{
3543 unsigned char *p = buf;
3544 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003545 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003546#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3547 size_t out_len;
3548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3549#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 if (session == NULL) {
3551 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3552 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003553
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003555 /*
3556 * Add Mbed TLS version identifier
3557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003559
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 if (used <= buf_len) {
3561 memcpy(p, ssl_serialized_session_header,
3562 sizeof(ssl_serialized_session_header));
3563 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003564 }
3565 }
3566
3567 /*
3568 * TLS version identifier
3569 */
3570 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 if (used <= buf_len) {
3572 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003573 }
3574
3575 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003576 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003578#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 case MBEDTLS_SSL_VERSION_TLS1_2:
3580 used += ssl_tls12_session_save(session, p, remaining_len);
3581 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003582#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3583
Jerry Yu251a12e2022-07-13 15:15:48 +08003584#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 case MBEDTLS_SSL_VERSION_TLS1_3:
3586 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3587 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3588 return ret;
3589 }
3590 used += out_len;
3591 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003592#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3593
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 default:
3595 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003596 }
3597
3598 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 if (used > buf_len) {
3600 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3601 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003602
Gilles Peskine449bd832023-01-11 14:50:10 +01003603 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003604}
3605
3606/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003607 * Public wrapper for ssl_session_save()
3608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003609int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3610 unsigned char *buf,
3611 size_t buf_len,
3612 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003613{
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003615}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003616
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003617/*
3618 * Deserialize session, see mbedtls_ssl_session_save() for format.
3619 *
3620 * This internal version is wrapped by a public function that cleans up in
3621 * case of error, and has an extra option omit_header.
3622 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003623MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003624static int ssl_session_load(mbedtls_ssl_session *session,
3625 unsigned char omit_header,
3626 const unsigned char *buf,
3627 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003628{
3629 const unsigned char *p = buf;
3630 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003631 size_t remaining_len;
3632
3633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 if (session == NULL) {
3635 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3636 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003637
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003639 /*
3640 * Check Mbed TLS version identifier
3641 */
3642
Gilles Peskine449bd832023-01-11 14:50:10 +01003643 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3644 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003645 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003646
3647 if (memcmp(p, ssl_serialized_session_header,
3648 sizeof(ssl_serialized_session_header)) != 0) {
3649 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3650 }
3651 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003652 }
3653
3654 /*
3655 * TLS version identifier
3656 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 if (1 > (size_t) (end - p)) {
3658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3659 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003660 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661
3662 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 remaining_len = (end - p);
3664 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003665#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003666 case MBEDTLS_SSL_VERSION_TLS1_2:
3667 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003668#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3669
Jerry Yu438ddd82022-07-07 06:55:50 +00003670#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003671 case MBEDTLS_SSL_VERSION_TLS1_3:
3672 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003673#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3674
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 default:
3676 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003677 }
3678}
3679
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003680/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003681 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003683int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3684 const unsigned char *buf,
3685 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003686{
Gilles Peskine449bd832023-01-11 14:50:10 +01003687 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003688
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 if (ret != 0) {
3690 mbedtls_ssl_session_free(session);
3691 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003692
Gilles Peskine449bd832023-01-11 14:50:10 +01003693 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003694}
3695
3696/*
Paul Bakker1961b702013-01-25 14:49:24 +01003697 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003699MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003700static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003701{
3702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3703
Ronald Cron66dbf912022-02-02 15:33:46 +01003704 /*
3705 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003706 * that were written into the output buffer by the previous handshake step,
3707 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003708 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3709 * We proceed to the next handshake step only when all data from the
3710 * previous one have been sent to the peer, thus we make sure that this is
3711 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3712 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3713 * we have to wait before to go ahead.
3714 * In the case of TLS 1.3, handshake step handlers do not send data to the
3715 * peer. Data are only sent here and through
3716 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003717 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3720 return ret;
3721 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003722
3723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003724 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3725 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3726 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3727 return ret;
3728 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003729 }
3730#endif /* MBEDTLS_SSL_PROTO_DTLS */
3731
Gilles Peskine449bd832023-01-11 14:50:10 +01003732 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003733}
3734
Gilles Peskine449bd832023-01-11 14:50:10 +01003735int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003736{
Hanno Becker41934dd2021-08-07 19:13:43 +01003737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003738
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003740 ssl->conf == NULL ||
3741 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003744 }
3745
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 ret = ssl_prepare_handshake_step(ssl);
3747 if (ret != 0) {
3748 return ret;
3749 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003750
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 ret = mbedtls_ssl_handle_pending_alert(ssl);
3752 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003753 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 }
Jerry Yue7047812021-09-13 19:26:39 +08003755
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003756 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3757 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3758 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3762 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3763 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003764
Gilles Peskine449bd832023-01-11 14:50:10 +01003765 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003766 case MBEDTLS_SSL_HELLO_REQUEST:
3767 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003768 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003769 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003770
Ronald Cron9f0fba32022-02-10 16:45:15 +01003771 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003773 break;
3774
3775 default:
3776#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3778 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3779 } else {
3780 ret = mbedtls_ssl_handshake_client_step(ssl);
3781 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003782#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003784#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003786#endif
3787 }
Jerry Yub9930e72021-08-06 17:11:51 +08003788 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003792#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3794 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3795 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003796#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003797
3798#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3800 ret = mbedtls_ssl_handshake_server_step(ssl);
3801 }
Jerry Yub9930e72021-08-06 17:11:51 +08003802#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3803 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003804#endif
3805
Gilles Peskine449bd832023-01-11 14:50:10 +01003806 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003807 /* handshake_step return error. And it is same
3808 * with alert_reason.
3809 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 if (ssl->send_alert) {
3811 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003812 goto cleanup;
3813 }
3814 }
3815
3816cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003818}
3819
3820/*
3821 * Perform the SSL handshake
3822 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003823int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003824{
3825 int ret = 0;
3826
Hanno Beckera817ea42020-10-20 15:20:23 +01003827 /* Sanity checks */
3828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 if (ssl == NULL || ssl->conf == NULL) {
3830 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3831 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003832
Hanno Beckera817ea42020-10-20 15:20:23 +01003833#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3835 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3836 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3837 "mbedtls_ssl_set_timer_cb() for DTLS"));
3838 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003839 }
3840#endif /* MBEDTLS_SSL_PROTO_DTLS */
3841
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003843
Hanno Beckera817ea42020-10-20 15:20:23 +01003844 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3846 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003847
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003849 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 }
Paul Bakker1961b702013-01-25 14:49:24 +01003851 }
3852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003854
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003856}
3857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_RENEGOTIATION)
3859#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003860/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003861 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003862 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003863MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003864static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003865{
Janos Follath865b3eb2019-12-16 11:46:15 +00003866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003869
3870 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3872 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003873
Gilles Peskine449bd832023-01-11 14:50:10 +01003874 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3875 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3876 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003877 }
3878
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003880
Gilles Peskine449bd832023-01-11 14:50:10 +01003881 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003884
3885/*
3886 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 * - any side: calling mbedtls_ssl_renegotiate(),
3888 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3889 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003890 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003891 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003894int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003895{
Janos Follath865b3eb2019-12-16 11:46:15 +00003896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003899
Gilles Peskine449bd832023-01-11 14:50:10 +01003900 if ((ret = ssl_handshake_init(ssl)) != 0) {
3901 return ret;
3902 }
Paul Bakker48916f92012-09-16 19:57:18 +00003903
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003904 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3905 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3908 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3909 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003910 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003912 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003914 }
3915#endif
3916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3918 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003919
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3921 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3922 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003923 }
3924
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003928}
3929
3930/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003931 * Renegotiate current connection on client,
3932 * or request renegotiation on server
3933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003934int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003935{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003937
Gilles Peskine449bd832023-01-11 14:50:10 +01003938 if (ssl == NULL || ssl->conf == NULL) {
3939 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3940 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003943 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3945 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3946 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3947 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003950
3951 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 if (ssl->out_left != 0) {
3953 return mbedtls_ssl_flush_output(ssl);
3954 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003955
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003961 /*
3962 * On client, either start the renegotiation process or,
3963 * if already in progress, continue the handshake
3964 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3966 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3967 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003968 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003969
3970 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
3971 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
3972 return ret;
3973 }
3974 } else {
3975 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3976 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3977 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003978 }
3979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003987{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003988 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003991 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003993
Brett Warrene0edc842021-08-17 09:53:13 +01003994#if defined(MBEDTLS_ECP_C)
3995#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 if (ssl->handshake->group_list_heap_allocated) {
3997 mbedtls_free((void *) handshake->group_list);
3998 }
Brett Warrene0edc842021-08-17 09:53:13 +01003999 handshake->group_list = NULL;
4000#endif /* MBEDTLS_DEPRECATED_REMOVED */
4001#endif /* MBEDTLS_ECP_C */
4002
Ronald Crone68ab4f2022-10-05 12:46:29 +02004003#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004004#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 if (ssl->handshake->sig_algs_heap_allocated) {
4006 mbedtls_free((void *) handshake->sig_algs);
4007 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004008 handshake->sig_algs = NULL;
4009#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004010#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 if (ssl->handshake->certificate_request_context) {
4012 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004013 }
4014#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004015#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004016
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004017#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4019 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004020 handshake->async_in_progress = 0;
4021 }
4022#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4023
Andrzej Kurek25f27152022-08-17 16:09:31 -04004024#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004025#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004027#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004029#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004030#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004031#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004032#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004034#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004036#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004037#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004041#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004042#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004044#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004045
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004046#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004047#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004049 /*
4050 * Opaque keys are not stored in the handshake's data and it's the user
4051 * responsibility to destroy them. Clear ones, instead, are created by
4052 * the TLS library and should be destroyed at the same level
4053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4055 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004056 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004057 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4058#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004060#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004061#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004063 handshake->ecjpake_cache = NULL;
4064 handshake->ecjpake_cache_len = 0;
4065#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004066#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004067
Janos Follath4ae5c292016-02-10 11:27:43 +00004068#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4069 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004070 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004072#endif
4073
Ronald Cron73fe8df2022-10-05 14:31:43 +02004074#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004075#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004077 /* The maintenance of the external PSK key slot is the
4078 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 if (ssl->handshake->psk_opaque_is_internal) {
4080 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004081 ssl->handshake->psk_opaque_is_internal = 0;
4082 }
4083 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4084 }
Neil Armstronge952a302022-05-03 10:22:14 +02004085#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (handshake->psk != NULL) {
4087 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4088 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004089 }
Neil Armstronge952a302022-05-03 10:22:14 +02004090#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004091#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4094 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004095 /*
4096 * Free only the linked list wrapper, not the keys themselves
4097 * since the belong to the SNI callback
4098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004101
Gilles Peskineeccd8882020-03-10 12:19:08 +01004102#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4104 if (handshake->ecrs_peer_cert != NULL) {
4105 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4106 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004107 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004108#endif
4109
Hanno Becker75173122019-02-06 16:18:31 +00004110#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4111 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004113#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4114
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004115#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4117 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004118#endif /* MBEDTLS_SSL_CLI_C &&
4119 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004120
4121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 mbedtls_ssl_flight_free(handshake->flight);
4123 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004124#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004125
Ronald Cronf12b81d2022-03-15 10:42:41 +01004126#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4128 if (handshake->ecdh_psa_privkey_is_external == 0) {
4129 psa_destroy_key(handshake->ecdh_psa_privkey);
4130 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004131#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4132
Ronald Cron6f135e12021-12-08 16:57:54 +01004133#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 mbedtls_ssl_transform_free(handshake->transform_handshake);
4135 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004136#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4138 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004139#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004140#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004141
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004142
4143#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4144 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4145 * processes datagrams and the fact that a datagram is allowed to have
4146 * several records in it, it is possible that the I/O buffers are not
4147 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4149 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004150#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004151
Jerry Yuba9c7272021-10-30 11:54:10 +08004152 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 mbedtls_platform_zeroize(handshake,
4154 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004155}
4156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004158{
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004160 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004165#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004166
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004167#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004168#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4169 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004171#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004173#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004176}
4177
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004178#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004179
4180#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4181#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4182#else
4183#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4184#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4185
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004186#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004187
4188#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4189#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4190#else
4191#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4192#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4193
4194#if defined(MBEDTLS_SSL_ALPN)
4195#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4196#else
4197#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4198#endif /* MBEDTLS_SSL_ALPN */
4199
4200#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4201#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4202#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4203#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4204
4205#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 ((uint32_t) ( \
4207 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4208 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4209 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4210 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4211 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4212 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4213 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4214 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004215
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004216static unsigned char ssl_serialized_context_header[] = {
4217 MBEDTLS_VERSION_MAJOR,
4218 MBEDTLS_VERSION_MINOR,
4219 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4221 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4222 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4223 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4224 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004225};
4226
Paul Bakker5121ce52009-01-03 21:22:43 +00004227/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004228 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004229 *
4230 * The format of the serialized data is:
4231 * (in the presentation language of TLS, RFC 8446 section 3)
4232 *
4233 * // header
4234 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004235 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004236 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004237 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004238 * Note: When updating the format, remember to keep these
4239 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004240 *
4241 * // session sub-structure
4242 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4243 * // transform sub-structure
4244 * uint8 random[64]; // ServerHello.random+ClientHello.random
4245 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4246 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4247 * // fields from ssl_context
4248 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4249 * uint64 in_window_top; // DTLS: last validated record seq_num
4250 * uint64 in_window; // DTLS: bitmask for replay protection
4251 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4252 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4253 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4254 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4255 *
4256 * Note that many fields of the ssl_context or sub-structures are not
4257 * serialized, as they fall in one of the following categories:
4258 *
4259 * 1. forced value (eg in_left must be 0)
4260 * 2. pointer to dynamically-allocated memory (eg session, transform)
4261 * 3. value can be re-derived from other data (eg session keys from MS)
4262 * 4. value was temporary (eg content of input buffer)
4263 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004265int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4266 unsigned char *buf,
4267 size_t buf_len,
4268 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004269{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004270 unsigned char *p = buf;
4271 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004272 size_t session_len;
4273 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004274
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004275 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004276 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4277 * this function's documentation.
4278 *
4279 * These are due to assumptions/limitations in the implementation. Some of
4280 * them are likely to stay (no handshake in progress) some might go away
4281 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004282 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004283 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4285 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4286 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004287 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 if (ssl->handshake != NULL) {
4289 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4290 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004291 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004292 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 if (ssl->transform == NULL || ssl->session == NULL) {
4294 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4295 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004296 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004297 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (mbedtls_ssl_check_pending(ssl) != 0) {
4299 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4300 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004301 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (ssl->out_left != 0) {
4303 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4304 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004305 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004306 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4308 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4309 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004310 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004311 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4313 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4314 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004315 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004316 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4318 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004320 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004321 /* Renegotiation must not be enabled */
4322#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4324 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4325 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004326 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004327#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004328
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004329 /*
4330 * Version and format identifier
4331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 if (used <= buf_len) {
4335 memcpy(p, ssl_serialized_context_header,
4336 sizeof(ssl_serialized_context_header));
4337 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004338 }
4339
4340 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004341 * Session (length + data)
4342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4344 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4345 return ret;
4346 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004347
4348 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (used <= buf_len) {
4350 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004351 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 ret = ssl_session_save(ssl->session, 1,
4354 p, session_len, &session_len);
4355 if (ret != 0) {
4356 return ret;
4357 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004358
4359 p += session_len;
4360 }
4361
4362 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004363 * Transform
4364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 used += sizeof(ssl->transform->randbytes);
4366 if (used <= buf_len) {
4367 memcpy(p, ssl->transform->randbytes,
4368 sizeof(ssl->transform->randbytes));
4369 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004370 }
4371
4372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4373 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004375 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004377 p += ssl->transform->in_cid_len;
4378
4379 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004381 p += ssl->transform->out_cid_len;
4382 }
4383#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4384
4385 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004386 * Saved fields from top-level ssl_context structure
4387 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004388 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 if (used <= buf_len) {
4390 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004391 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004392 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004393
4394#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4395 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (used <= buf_len) {
4397 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004398 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004401 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004402 }
4403#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4404
4405#if defined(MBEDTLS_SSL_PROTO_DTLS)
4406 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004408 *p++ = ssl->disable_datagram_packing;
4409 }
4410#endif /* MBEDTLS_SSL_PROTO_DTLS */
4411
Jerry Yuae0b2e22021-10-08 15:21:19 +08004412 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 if (used <= buf_len) {
4414 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004415 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004416 }
4417
4418#if defined(MBEDTLS_SSL_PROTO_DTLS)
4419 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (used <= buf_len) {
4421 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004422 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004423 }
4424#endif /* MBEDTLS_SSL_PROTO_DTLS */
4425
4426#if defined(MBEDTLS_SSL_ALPN)
4427 {
4428 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004430 : 0;
4431
4432 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004434 *p++ = alpn_len;
4435
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 if (ssl->alpn_chosen != NULL) {
4437 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004438 p += alpn_len;
4439 }
4440 }
4441 }
4442#endif /* MBEDTLS_SSL_ALPN */
4443
4444 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004445 * Done
4446 */
4447 *olen = used;
4448
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 if (used > buf_len) {
4450 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4451 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004456}
4457
4458/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004459 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004460 *
4461 * This internal version is wrapped by a public function that cleans up in
4462 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004463 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004464MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004465static int ssl_context_load(mbedtls_ssl_context *ssl,
4466 const unsigned char *buf,
4467 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004468{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004469 const unsigned char *p = buf;
4470 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004471 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004473#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004474 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004475#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004476
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004477 /*
4478 * The context should have been freshly setup or reset.
4479 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004480 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004481 * renegotiating, or if the user mistakenly loaded a session first.)
4482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4484 ssl->session != NULL) {
4485 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004486 }
4487
4488 /*
4489 * We can't check that the config matches the initial one, but we can at
4490 * least check it matches the requirements for serializing.
4491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004493 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4494 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004495#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004496 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004497#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 0) {
4499 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004500 }
4501
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004503
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004504 /*
4505 * Check version identifier
4506 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4508 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004509 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004510
4511 if (memcmp(p, ssl_serialized_context_header,
4512 sizeof(ssl_serialized_context_header)) != 0) {
4513 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4514 }
4515 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004516
4517 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004518 * Session
4519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 if ((size_t) (end - p) < 4) {
4521 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4522 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 session_len = ((size_t) p[0] << 24) |
4525 ((size_t) p[1] << 16) |
4526 ((size_t) p[2] << 8) |
4527 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004528 p += 4;
4529
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004530 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004531 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004532 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004533 ssl->session_in = ssl->session;
4534 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004535 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if ((size_t) (end - p) < session_len) {
4538 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4539 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 ret = ssl_session_load(ssl->session, 1, p, session_len);
4542 if (ret != 0) {
4543 mbedtls_ssl_session_free(ssl->session);
4544 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004545 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004546
4547 p += session_len;
4548
4549 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004550 * Transform
4551 */
4552
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004553 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004554 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004555#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004556 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004557 ssl->transform_in = ssl->transform;
4558 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004559 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004560#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004561
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004562#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4564 if (prf_func == NULL) {
4565 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4566 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004567
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004568 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4570 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4571 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 ret = ssl_tls12_populate_transform(ssl->transform,
4574 ssl->session->ciphersuite,
4575 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004576#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004578#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 prf_func,
4580 p, /* currently pointing to randbytes */
4581 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4582 ssl->conf->endpoint,
4583 ssl);
4584 if (ret != 0) {
4585 return ret;
4586 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004587#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004589
4590#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4591 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 if ((size_t) (end - p) < 1) {
4593 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4594 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004595
4596 ssl->transform->in_cid_len = *p++;
4597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4599 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4600 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004601
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004603 p += ssl->transform->in_cid_len;
4604
4605 ssl->transform->out_cid_len = *p++;
4606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4608 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4609 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004612 p += ssl->transform->out_cid_len;
4613#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4614
4615 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004616 * Saved fields from top-level ssl_context structure
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é-Gonnardc86c5df2019-07-15 11:23:03 +02004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4623 ((uint32_t) p[1] << 16) |
4624 ((uint32_t) p[2] << 8) |
4625 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004626 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004627
4628#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if ((size_t) (end - p) < 16) {
4630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4631 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4634 ((uint64_t) p[1] << 48) |
4635 ((uint64_t) p[2] << 40) |
4636 ((uint64_t) p[3] << 32) |
4637 ((uint64_t) p[4] << 24) |
4638 ((uint64_t) p[5] << 16) |
4639 ((uint64_t) p[6] << 8) |
4640 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004641 p += 8;
4642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 ssl->in_window = ((uint64_t) p[0] << 56) |
4644 ((uint64_t) p[1] << 48) |
4645 ((uint64_t) p[2] << 40) |
4646 ((uint64_t) p[3] << 32) |
4647 ((uint64_t) p[4] << 24) |
4648 ((uint64_t) p[5] << 16) |
4649 ((uint64_t) p[6] << 8) |
4650 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004651 p += 8;
4652#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4653
4654#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 if ((size_t) (end - p) < 1) {
4656 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4657 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004658
4659 ssl->disable_datagram_packing = *p++;
4660#endif /* MBEDTLS_SSL_PROTO_DTLS */
4661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4664 }
4665 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4666 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004667
4668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if ((size_t) (end - p) < 2) {
4670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4671 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004674 p += 2;
4675#endif /* MBEDTLS_SSL_PROTO_DTLS */
4676
4677#if defined(MBEDTLS_SSL_ALPN)
4678 {
4679 uint8_t alpn_len;
4680 const char **cur;
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 if ((size_t) (end - p) < 1) {
4683 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4684 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004685
4686 alpn_len = *p++;
4687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004689 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4691 if (strlen(*cur) == alpn_len &&
4692 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004693 ssl->alpn_chosen = *cur;
4694 break;
4695 }
4696 }
4697 }
4698
4699 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4701 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4702 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004703
4704 p += alpn_len;
4705 }
4706#endif /* MBEDTLS_SSL_ALPN */
4707
4708 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004709 * Forced fields from top-level ssl_context structure
4710 *
4711 * Most of them already set to the correct value by mbedtls_ssl_init() and
4712 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4713 */
4714 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004715 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004716
Hanno Becker361b10d2019-08-30 10:42:49 +01004717 /* Adjust pointers for header fields of outgoing records to
4718 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004720
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004721#if defined(MBEDTLS_SSL_PROTO_DTLS)
4722 ssl->in_epoch = 1;
4723#endif
4724
4725 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4726 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004727 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4728 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if (ssl->handshake != NULL) {
4730 mbedtls_ssl_handshake_free(ssl);
4731 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004732 ssl->handshake = NULL;
4733 }
4734
4735 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004736 * Done - should have consumed entire buffer
4737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (p != end) {
4739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4740 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004743}
4744
4745/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004746 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004747 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004748int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4749 const unsigned char *buf,
4750 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004751{
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (ret != 0) {
4755 mbedtls_ssl_free(context);
4756 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004759}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004760#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004761
4762/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004763 * Free an SSL context
4764 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004766{
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004768 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004774#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4775 size_t out_buf_len = ssl->out_buf_len;
4776#else
4777 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4778#endif
4779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4781 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004782 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004783 }
4784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004786#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4787 size_t in_buf_len = ssl->in_buf_len;
4788#else
4789 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4790#endif
4791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4793 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004794 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 }
4796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (ssl->transform) {
4798 mbedtls_ssl_transform_free(ssl->transform);
4799 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004800 }
4801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 if (ssl->handshake) {
4803 mbedtls_ssl_handshake_free(ssl);
4804 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004805
4806#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4808 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004809#endif
4810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 mbedtls_ssl_session_free(ssl->session_negotiate);
4812 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004813 }
4814
Ronald Cron6f135e12021-12-08 16:57:54 +01004815#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 mbedtls_ssl_transform_free(ssl->transform_application);
4817 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004818#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 if (ssl->session) {
4821 mbedtls_ssl_session_free(ssl->session);
4822 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004823 }
4824
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004825#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (ssl->hostname != NULL) {
4827 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4828 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004830#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004831
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004832#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004834#endif
4835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004837
Paul Bakker86f04f42013-02-14 11:20:09 +01004838 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004840}
4841
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004842/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004843 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004846{
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004848}
4849
Gilles Peskineae270bf2021-06-02 00:05:29 +02004850/* The selection should be the same as mbedtls_x509_crt_profile_default in
4851 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004852 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004853 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004854 * about this list.
4855 */
Brett Warrene0edc842021-08-17 09:53:13 +01004856static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004857#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004858 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004859#endif
4860#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004861 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004862#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004863#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004864 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004865#endif
4866#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004867 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004868#endif
4869#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004870 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004871#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004872#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004873 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004874#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004875#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004876 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004877#endif
4878#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004879 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004880#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004881 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004882};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004883
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004884static int ssl_preset_suiteb_ciphersuites[] = {
4885 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4886 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4887 0
4888};
4889
Ronald Crone68ab4f2022-10-05 12:46:29 +02004890#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004891
Jerry Yu909df7b2022-01-22 11:56:27 +08004892/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004893 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004894 * rules SHOULD be upheld.
4895 * - No duplicate entries.
4896 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004897 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004898 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004899 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004900static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004901
Andrzej Kurek25f27152022-08-17 16:09:31 -04004902#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004903 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4904 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004905#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004906 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004907
Andrzej Kurek25f27152022-08-17 16:09:31 -04004908#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004909 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4910 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004911#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004912 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4913
Andrzej Kurek25f27152022-08-17 16:09:31 -04004914#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004915 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4916 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004917#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004918 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004919
Gilles Peskine449bd832023-01-11 14:50:10 +01004920#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4921 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004922 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004923#endif \
4924 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4927 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004928 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004929#endif \
4930 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4933 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004934 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004935#endif \
4936 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004937
Andrzej Kurek25f27152022-08-17 16:09:31 -04004938#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004939 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4940#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4941
Andrzej Kurek25f27152022-08-17 16:09:31 -04004942#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004943 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4944#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4945
Andrzej Kurek25f27152022-08-17 16:09:31 -04004946#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004947 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4948#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4949
Gabor Mezei15b95a62022-05-09 16:37:58 +02004950 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004951};
4952
4953/* NOTICE: see above */
4954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4955static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004956#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004957#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004959#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004960#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4961 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4962#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004963#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004965#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004966#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004967#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004968#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004970#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004971#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4972 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4973#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004974#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004976#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004977#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004978#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004979#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004981#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004982#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4983 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4984#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004985#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004987#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004988#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004989 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004990};
4991#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4992/* NOTICE: see above */
4993static uint16_t ssl_preset_suiteb_sig_algs[] = {
4994
Andrzej Kurek25f27152022-08-17 16:09:31 -04004995#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004996 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4997 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004998#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004999 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5000
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu53037892022-01-25 11:02:06 +08005002 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5003 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005004#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005005 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005006
Gilles Peskine449bd832023-01-11 14:50:10 +01005007#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5008 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005009 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005010#endif \
5011 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08005012
Andrzej Kurek25f27152022-08-17 16:09:31 -04005013#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu53037892022-01-25 11:02:06 +08005014 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005015#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005016
Gabor Mezei15b95a62022-05-09 16:37:58 +02005017 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005018};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005019
Jerry Yu909df7b2022-01-22 11:56:27 +08005020/* NOTICE: see above */
5021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5022static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005023#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005024#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005026#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005027#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005029#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005030#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005031#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005032#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005034#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005035#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005037#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005038#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005039 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005040};
Jerry Yu909df7b2022-01-22 11:56:27 +08005041#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5042
Ronald Crone68ab4f2022-10-05 12:46:29 +02005043#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005044
Brett Warrene0edc842021-08-17 09:53:13 +01005045static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005046#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005047 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005048#endif
5049#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005050 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005051#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005052 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005053};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005054
Ronald Crone68ab4f2022-10-05 12:46:29 +02005055#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005056/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005057 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005058MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005059static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005060{
5061 size_t i, j;
5062 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5065 for (j = 0; j < i; j++) {
5066 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005067 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 }
5069 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5070 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5071 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005072 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005073 }
5074 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005076}
5077
Ronald Crone68ab4f2022-10-05 12:46:29 +02005078#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005079
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005080/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005081 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005083int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5084 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005085{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005086#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005088#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005089
Ronald Crone68ab4f2022-10-05 12:46:29 +02005090#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5092 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5093 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005094 }
5095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5097 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5098 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005099 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005100
5101#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5103 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5104 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005105 }
5106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5108 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5109 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005110 }
5111#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005112#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005113
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005114 /* Use the functions here so that they are covered in tests,
5115 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 mbedtls_ssl_conf_endpoint(conf, endpoint);
5117 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005118
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005119 /*
5120 * Things that are common to all presets
5121 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005122#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005124 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5125#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5126 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5127#endif
5128 }
5129#endif
5130
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005131#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5132 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5133#endif
5134
5135#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5136 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5137#endif
5138
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005139#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005140 conf->f_cookie_write = ssl_cookie_write_dummy;
5141 conf->f_cookie_check = ssl_cookie_check_dummy;
5142#endif
5143
5144#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5145 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5146#endif
5147
Janos Follath088ce432017-04-10 12:42:31 +01005148#if defined(MBEDTLS_SSL_SRV_C)
5149 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005150 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005151#endif
5152
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005153#if defined(MBEDTLS_SSL_PROTO_DTLS)
5154 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5155 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5156#endif
5157
5158#if defined(MBEDTLS_SSL_RENEGOTIATION)
5159 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 memset(conf->renego_period, 0x00, 2);
5161 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005162#endif
5163
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005164#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005166 const unsigned char dhm_p[] =
5167 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5168 const unsigned char dhm_g[] =
5169 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5172 dhm_p, sizeof(dhm_p),
5173 dhm_g, sizeof(dhm_g))) != 0) {
5174 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005175 }
5176 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005177#endif
5178
Ronald Cron6f135e12021-12-08 16:57:54 +01005179#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005180
5181#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005183#if defined(MBEDTLS_SSL_SRV_C)
5184 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005186#endif
5187#endif /* MBEDTLS_SSL_EARLY_DATA */
5188
Jerry Yud0766ec2022-09-22 10:46:57 +08005189#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005190 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005192#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005193 /*
5194 * Allow all TLS 1.3 key exchange modes by default.
5195 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005196 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005197#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005200#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005201 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005202 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005203#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005205#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005207#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005209 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5210 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 } else {
5212 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005213 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5214 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5215 }
5216#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5217 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5218 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5219#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5220 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5221 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5222#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005224#endif
5225 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005226
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005227 /*
5228 * Preset-specific defaults
5229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005231 /*
5232 * NSA Suite B
5233 */
5234 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005235
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005236 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005237
5238#if defined(MBEDTLS_X509_CRT_PARSE_C)
5239 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005240#endif
5241
Ronald Crone68ab4f2022-10-05 12:46:29 +02005242#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005243#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005245 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005247#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005249#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005250
Brett Warrene0edc842021-08-17 09:53:13 +01005251#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5252 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005253#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005254 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005255 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005256
5257 /*
5258 * Default
5259 */
5260 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005261
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005262 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005263
5264#if defined(MBEDTLS_X509_CRT_PARSE_C)
5265 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5266#endif
5267
Ronald Crone68ab4f2022-10-05 12:46:29 +02005268#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005269#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005271 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005273#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005275#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005276
Brett Warrene0edc842021-08-17 09:53:13 +01005277#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5278 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005279#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005280 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005281
5282#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5283 conf->dhm_min_bitlen = 1024;
5284#endif
5285 }
5286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005288}
5289
5290/*
5291 * Free mbedtls_ssl_config
5292 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005293void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005294{
5295#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 mbedtls_mpi_free(&conf->dhm_P);
5297 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005298#endif
5299
Ronald Cron73fe8df2022-10-05 14:31:43 +02005300#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005301#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005303 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5304 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005305#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 if (conf->psk != NULL) {
5307 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5308 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005309 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005310 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005311 }
5312
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (conf->psk_identity != NULL) {
5314 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5315 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005316 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005317 conf->psk_identity_len = 0;
5318 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005319#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005320
5321#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005323#endif
5324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005326}
5327
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005328#if defined(MBEDTLS_PK_C) && \
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005329 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_CAN_ECDSA_SOME))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005330/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005333unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5337 return MBEDTLS_SSL_SIG_RSA;
5338 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005339#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005340#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5342 return MBEDTLS_SSL_SIG_ECDSA;
5343 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005344#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005346}
5347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005349{
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005351 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005353 case MBEDTLS_PK_ECDSA:
5354 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005356 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005358 }
5359}
5360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005362{
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364#if defined(MBEDTLS_RSA_C)
5365 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005367#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005368#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005371#endif
5372 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005374 }
5375}
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005376#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_PK_CAN_ECDSA_SOME ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005377
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005378/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005379 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005381mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005382{
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005384#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005387#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005388#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005391#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005392#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005395#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005396#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005399#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005400#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005403#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005404#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005407#endif
5408 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005410 }
5411}
5412
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005413/*
5414 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005416unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005417{
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005419#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005420 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005422#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005423#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005424 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005426#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005427#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005428 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005430#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005431#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005432 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005434#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005435#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005436 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005438#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005439#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005440 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005442#endif
5443 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005445 }
5446}
5447
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005448/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005449 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005450 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005451 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005453{
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 if (group_list == NULL) {
5457 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005458 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 for (; *group_list != 0; group_list++) {
5461 if (*group_list == tls_id) {
5462 return 0;
5463 }
5464 }
5465
5466 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005467}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005468
5469#if defined(MBEDTLS_ECP_C)
5470/*
5471 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005473int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005474{
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005478 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005482}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005483#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005484
Gilles Peskine449bd832023-01-11 14:50:10 +01005485#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005486#define EC_NAME(_name_) _name_
5487#else
5488#define EC_NAME(_name_) NULL
5489#endif
5490
Valerio Setti18c9fed2022-12-30 17:44:24 +01005491static const struct {
5492 uint16_t tls_id;
5493 mbedtls_ecp_group_id ecp_group_id;
5494 psa_ecc_family_t psa_family;
5495 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005497} tls_id_match_table[] =
5498{
5499#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005501#endif
5502#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005504#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005505#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005507#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005508#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005510#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005511#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005513#endif
5514#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005516#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005517#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005519#endif
5520#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005522#endif
5523#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005525#endif
5526#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005528#endif
5529#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005531#endif
5532#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005534#endif
5535#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005537#endif
5538 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5539};
5540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5542 psa_ecc_family_t *family,
5543 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005544{
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5546 if (tls_id_match_table[i].tls_id == tls_id) {
5547 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005548 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 }
5550 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005551 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005553 return PSA_SUCCESS;
5554 }
5555 }
5556
5557 return PSA_ERROR_NOT_SUPPORTED;
5558}
5559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005561{
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5563 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005564 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005566 }
5567
5568 return MBEDTLS_ECP_DP_NONE;
5569}
5570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005572{
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5574 i++) {
5575 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005576 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005578 }
5579
5580 return 0;
5581}
5582
Valerio Setti67419f02023-01-04 16:12:42 +01005583#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005584const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005585{
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5587 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005588 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590 }
5591
5592 return NULL;
5593}
Valerio Setti67419f02023-01-04 16:12:42 +01005594#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005597int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5598 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5599 int cert_endpoint,
5600 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005601{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005602 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005603 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005604 const char *ext_oid;
5605 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005608 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 case MBEDTLS_KEY_EXCHANGE_RSA:
5611 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005612 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005613 break;
5614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5616 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5617 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5618 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005619 break;
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5622 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005623 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005624 break;
5625
5626 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 case MBEDTLS_KEY_EXCHANGE_NONE:
5628 case MBEDTLS_KEY_EXCHANGE_PSK:
5629 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5630 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005631 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005632 usage = 0;
5633 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5636 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005637 }
5638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005640 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005641 ret = -1;
5642 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5647 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005650 }
5651
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005653 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005654 ret = -1;
5655 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005660
Jerry Yu148165c2021-09-24 23:20:59 +08005661#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005662int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5663 const mbedtls_md_type_t md,
5664 unsigned char *dst,
5665 size_t dst_len,
5666 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005667{
Ronald Cronf6893e12022-01-07 22:09:01 +01005668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5669 psa_hash_operation_t *hash_operation_to_clone;
5670 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5671
Jerry Yu148165c2021-09-24 23:20:59 +08005672 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005675#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 case MBEDTLS_MD_SHA384:
5677 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5678 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005679#endif
5680
Andrzej Kurek25f27152022-08-17 16:09:31 -04005681#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 case MBEDTLS_MD_SHA256:
5683 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5684 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005685#endif
5686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 default:
5688 goto exit;
5689 }
5690
5691 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5692 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005693 goto exit;
5694 }
5695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5697 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005698 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005700
5701exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005702#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5703 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5704 (void) ssl;
5705#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005707}
5708#else /* MBEDTLS_USE_PSA_CRYPTO */
5709
Andrzej Kurek25f27152022-08-17 16:09:31 -04005710#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005711MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005712static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5713 unsigned char *dst,
5714 size_t dst_len,
5715 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005716{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005717 int ret;
5718 mbedtls_sha512_context sha512;
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 if (dst_len < 48) {
5721 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5722 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 mbedtls_sha512_init(&sha512);
5725 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5728 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005729 goto exit;
5730 }
5731
5732 *olen = 48;
5733
5734exit:
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 mbedtls_sha512_free(&sha512);
5737 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005738}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005739#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005740
Andrzej Kurek25f27152022-08-17 16:09:31 -04005741#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005742MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005743static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5744 unsigned char *dst,
5745 size_t dst_len,
5746 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005747{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005748 int ret;
5749 mbedtls_sha256_context sha256;
5750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 if (dst_len < 32) {
5752 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5753 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 mbedtls_sha256_init(&sha256);
5756 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5759 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005760 goto exit;
5761 }
5762
5763 *olen = 32;
5764
5765exit:
5766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 mbedtls_sha256_free(&sha256);
5768 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005769}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005770#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5773 const mbedtls_md_type_t md,
5774 unsigned char *dst,
5775 size_t dst_len,
5776 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005777{
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005779
Andrzej Kurek25f27152022-08-17 16:09:31 -04005780#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 case MBEDTLS_MD_SHA384:
5782 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005783#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005784
Andrzej Kurek25f27152022-08-17 16:09:31 -04005785#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 case MBEDTLS_MD_SHA256:
5787 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005788#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005791#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5793 (void) ssl;
5794 (void) dst;
5795 (void) dst_len;
5796 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005797#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005799 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005801}
XiaokangQian647719a2021-12-07 09:16:29 +00005802
Jerry Yu148165c2021-09-24 23:20:59 +08005803#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005804
Ronald Crone68ab4f2022-10-05 12:46:29 +02005805#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005806/* mbedtls_ssl_parse_sig_alg_ext()
5807 *
5808 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5809 * value (TLS 1.3 RFC8446):
5810 * enum {
5811 * ....
5812 * ecdsa_secp256r1_sha256( 0x0403 ),
5813 * ecdsa_secp384r1_sha384( 0x0503 ),
5814 * ecdsa_secp521r1_sha512( 0x0603 ),
5815 * ....
5816 * } SignatureScheme;
5817 *
5818 * struct {
5819 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5820 * } SignatureSchemeList;
5821 *
5822 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5823 * value (TLS 1.2 RFC5246):
5824 * enum {
5825 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5826 * sha512(6), (255)
5827 * } HashAlgorithm;
5828 *
5829 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5830 * SignatureAlgorithm;
5831 *
5832 * struct {
5833 * HashAlgorithm hash;
5834 * SignatureAlgorithm signature;
5835 * } SignatureAndHashAlgorithm;
5836 *
5837 * SignatureAndHashAlgorithm
5838 * supported_signature_algorithms<2..2^16-2>;
5839 *
5840 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5841 * generalization of the TLS 1.2 signature algorithm extension.
5842 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5843 * `SignatureScheme` field of TLS 1.3
5844 *
5845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005846int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5847 const unsigned char *buf,
5848 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005849{
5850 const unsigned char *p = buf;
5851 size_t supported_sig_algs_len = 0;
5852 const unsigned char *supported_sig_algs_end;
5853 uint16_t sig_alg;
5854 uint32_t common_idx = 0;
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5857 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005858 p += 2;
5859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 memset(ssl->handshake->received_sig_algs, 0,
5861 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005864 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 while (p < supported_sig_algs_end) {
5866 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5867 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005868 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5870 sig_alg,
5871 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005872#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5874 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5875 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005876 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005877 }
5878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5881 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005884 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5885 common_idx += 1;
5886 }
5887 }
5888 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 if (p != end) {
5890 MBEDTLS_SSL_DEBUG_MSG(1,
5891 ("Signature algorithms extension length misaligned"));
5892 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5893 MBEDTLS_ERR_SSL_DECODE_ERROR);
5894 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005895 }
5896
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 if (common_idx == 0) {
5898 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5899 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5900 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5901 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005902 }
5903
Gabor Mezei15b95a62022-05-09 16:37:58 +02005904 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005906}
5907
Ronald Crone68ab4f2022-10-05 12:46:29 +02005908#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005909
Jerry Yudc7bd172022-02-17 13:44:15 +08005910#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5911
5912#if defined(MBEDTLS_USE_PSA_CRYPTO)
5913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5915 mbedtls_svc_key_id_t key,
5916 psa_algorithm_t alg,
5917 const unsigned char *raw_psk, size_t raw_psk_length,
5918 const unsigned char *seed, size_t seed_length,
5919 const unsigned char *label, size_t label_length,
5920 const unsigned char *other_secret,
5921 size_t other_secret_length,
5922 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005923{
5924 psa_status_t status;
5925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 status = psa_key_derivation_setup(derivation, alg);
5927 if (status != PSA_SUCCESS) {
5928 return status;
5929 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5932 status = psa_key_derivation_input_bytes(derivation,
5933 PSA_KEY_DERIVATION_INPUT_SEED,
5934 seed, seed_length);
5935 if (status != PSA_SUCCESS) {
5936 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005937 }
5938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 if (other_secret != NULL) {
5940 status = psa_key_derivation_input_bytes(derivation,
5941 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5942 other_secret, other_secret_length);
5943 if (status != PSA_SUCCESS) {
5944 return status;
5945 }
5946 }
5947
5948 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005949 status = psa_key_derivation_input_bytes(
5950 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 raw_psk, raw_psk_length);
5952 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005953 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005955 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 if (status != PSA_SUCCESS) {
5957 return status;
5958 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 status = psa_key_derivation_input_bytes(derivation,
5961 PSA_KEY_DERIVATION_INPUT_LABEL,
5962 label, label_length);
5963 if (status != PSA_SUCCESS) {
5964 return status;
5965 }
5966 } else {
5967 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005968 }
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 status = psa_key_derivation_set_capacity(derivation, capacity);
5971 if (status != PSA_SUCCESS) {
5972 return status;
5973 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08005976}
5977
Andrzej Kurek57d10632022-10-24 10:32:01 -04005978#if defined(PSA_WANT_ALG_SHA_384) || \
5979 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005980MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005981static int tls_prf_generic(mbedtls_md_type_t md_type,
5982 const unsigned char *secret, size_t slen,
5983 const char *label,
5984 const unsigned char *random, size_t rlen,
5985 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08005986{
5987 psa_status_t status;
5988 psa_algorithm_t alg;
5989 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5990 psa_key_derivation_operation_t derivation =
5991 PSA_KEY_DERIVATION_OPERATION_INIT;
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005994 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005996 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005998
5999 /* Normally a "secret" should be long enough to be impossible to
6000 * find by brute force, and in particular should not be empty. But
6001 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6002 * and for this use case it makes sense to have a 0-length "secret".
6003 * Since the key API doesn't allow importing a key of length 0,
6004 * keep master_key=0, which setup_psa_key_derivation() understands
6005 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006007 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6009 psa_set_key_algorithm(&key_attributes, alg);
6010 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6013 if (status != PSA_SUCCESS) {
6014 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6015 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006016 }
6017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 status = setup_psa_key_derivation(&derivation,
6019 master_key, alg,
6020 NULL, 0,
6021 random, rlen,
6022 (unsigned char const *) label,
6023 (size_t) strlen(label),
6024 NULL, 0,
6025 dlen);
6026 if (status != PSA_SUCCESS) {
6027 psa_key_derivation_abort(&derivation);
6028 psa_destroy_key(master_key);
6029 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006030 }
6031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6033 if (status != PSA_SUCCESS) {
6034 psa_key_derivation_abort(&derivation);
6035 psa_destroy_key(master_key);
6036 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006037 }
6038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 status = psa_key_derivation_abort(&derivation);
6040 if (status != PSA_SUCCESS) {
6041 psa_destroy_key(master_key);
6042 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006043 }
6044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 if (!mbedtls_svc_key_id_is_null(master_key)) {
6046 status = psa_destroy_key(master_key);
6047 }
6048 if (status != PSA_SUCCESS) {
6049 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6050 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006053}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006054#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006055#else /* MBEDTLS_USE_PSA_CRYPTO */
6056
Andrzej Kurek57d10632022-10-24 10:32:01 -04006057#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 (defined(MBEDTLS_SHA256_C) || \
6059 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006060MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006061static int tls_prf_generic(mbedtls_md_type_t md_type,
6062 const unsigned char *secret, size_t slen,
6063 const char *label,
6064 const unsigned char *random, size_t rlen,
6065 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006066{
6067 size_t nb;
6068 size_t i, j, k, md_len;
6069 unsigned char *tmp;
6070 size_t tmp_len = 0;
6071 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6072 const mbedtls_md_info_t *md_info;
6073 mbedtls_md_context_t md_ctx;
6074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6079 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6080 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 tmp_len = md_len + strlen(label) + rlen;
6085 tmp = mbedtls_calloc(1, tmp_len);
6086 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006087 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6088 goto exit;
6089 }
6090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 nb = strlen(label);
6092 memcpy(tmp + md_len, label, nb);
6093 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006094 nb += rlen;
6095
6096 /*
6097 * Compute P_<hash>(secret, label + random)[0..dlen]
6098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006100 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6104 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 }
6107 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6108 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 }
6111 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6112 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 for (i = 0; i < dlen; i += md_len) {
6117 ret = mbedtls_md_hmac_reset(&md_ctx);
6118 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 }
6121 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6122 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 }
6125 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6126 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006129
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 ret = mbedtls_md_hmac_reset(&md_ctx);
6131 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 }
6134 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6135 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 }
6138 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6139 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006146 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006148 }
6149
6150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (tmp != NULL) {
6154 mbedtls_platform_zeroize(tmp, tmp_len);
6155 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006162}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006163#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006164#endif /* MBEDTLS_USE_PSA_CRYPTO */
6165
Andrzej Kurek25f27152022-08-17 16:09:31 -04006166#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006167MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006168static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6169 const char *label,
6170 const unsigned char *random, size_t rlen,
6171 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006172{
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6174 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006175}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006176#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006177
Andrzej Kurek25f27152022-08-17 16:09:31 -04006178#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006179MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006180static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6181 const char *label,
6182 const unsigned char *random, size_t rlen,
6183 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006184{
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6186 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006187}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006188#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006189
Jerry Yuf009d862022-02-17 14:01:37 +08006190/*
6191 * Set appropriate PRF function and other SSL / TLS1.2 functions
6192 *
6193 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006194 * - hash associated with the ciphersuite (only used by TLS 1.2)
6195 *
6196 * Outputs:
6197 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6198 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006199MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006200static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6201 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006202{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006203#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006205 handshake->tls_prf = tls_prf_sha384;
6206 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6207 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006209#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006210#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006211 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006212 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006213 handshake->tls_prf = tls_prf_sha256;
6214 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6215 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6216 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006217#else
Jerry Yuf009d862022-02-17 14:01:37 +08006218 {
Jerry Yuf009d862022-02-17 14:01:37 +08006219 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006220 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006222 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006223#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006226}
Jerry Yud6ab2352022-02-17 14:03:43 +08006227
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006228/*
6229 * Compute master secret if needed
6230 *
6231 * Parameters:
6232 * [in/out] handshake
6233 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6234 * (PSA-PSK) ciphersuite_info, psk_opaque
6235 * [out] premaster (cleared)
6236 * [out] master
6237 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6238 * debug: conf->f_dbg, conf->p_dbg
6239 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006240 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006241 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006242MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006243static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6244 unsigned char *master,
6245 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006246{
6247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6248
6249 /* cf. RFC 5246, Section 8.1:
6250 * "The master secret is always exactly 48 bytes in length." */
6251 size_t const master_secret_len = 48;
6252
6253#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6254 unsigned char session_hash[48];
6255#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6256
6257 /* The label for the KDF used for key expansion.
6258 * This is either "master secret" or "extended master secret"
6259 * depending on whether the Extended Master Secret extension
6260 * is used. */
6261 char const *lbl = "master secret";
6262
Przemek Stekielae4ed302022-04-05 17:15:55 +02006263 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006264 * - If the Extended Master Secret extension is not used,
6265 * this is ClientHello.Random + ServerHello.Random
6266 * (see Sect. 8.1 in RFC 5246).
6267 * - If the Extended Master Secret extension is used,
6268 * this is the transcript of the handshake so far.
6269 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006270 unsigned char const *seed = handshake->randbytes;
6271 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006272
6273#if !defined(MBEDTLS_DEBUG_C) && \
6274 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6275 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006277 ssl = NULL; /* make sure we don't use it except for those cases */
6278 (void) ssl;
6279#endif
6280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 if (handshake->resume != 0) {
6282 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6283 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006284 }
6285
6286#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006288 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006289 seed = session_hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 handshake->calc_verify(ssl, session_hash, &seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6293 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006294 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006295#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006296
Przemek Stekiel99114f32022-04-22 11:20:09 +02006297#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006298 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006300 /* Perform PSK-to-MS expansion in a single step. */
6301 psa_status_t status;
6302 psa_algorithm_t alg;
6303 mbedtls_svc_key_id_t psk;
6304 psa_key_derivation_operation_t derivation =
6305 PSA_KEY_DERIVATION_OPERATION_INIT;
6306 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006313 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006315 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006317
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006318 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006322 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006323 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006324 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006325 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006326 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006327 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006328 other_secret_len = 48;
6329 other_secret = handshake->premaster + 2;
6330 break;
6331 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006332 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006333 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6334 other_secret = handshake->premaster + 2;
6335 break;
6336 default:
6337 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006338 }
6339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 status = setup_psa_key_derivation(&derivation, psk, alg,
6341 ssl->conf->psk, ssl->conf->psk_len,
6342 seed, seed_len,
6343 (unsigned char const *) lbl,
6344 (size_t) strlen(lbl),
6345 other_secret, other_secret_len,
6346 master_secret_len);
6347 if (status != PSA_SUCCESS) {
6348 psa_key_derivation_abort(&derivation);
6349 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006350 }
6351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 status = psa_key_derivation_output_bytes(&derivation,
6353 master,
6354 master_secret_len);
6355 if (status != PSA_SUCCESS) {
6356 psa_key_derivation_abort(&derivation);
6357 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006358 }
6359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 status = psa_key_derivation_abort(&derivation);
6361 if (status != PSA_SUCCESS) {
6362 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6363 }
6364 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006365#endif
6366 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006367#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6369 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006370 psa_status_t status;
6371 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6372 psa_key_derivation_operation_t derivation =
6373 PSA_KEY_DERIVATION_OPERATION_INIT;
6374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006376
6377 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 status = psa_key_derivation_setup(&derivation, alg);
6380 if (status != PSA_SUCCESS) {
6381 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006382 }
6383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 status = psa_key_derivation_set_capacity(&derivation,
6385 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6386 if (status != PSA_SUCCESS) {
6387 psa_key_derivation_abort(&derivation);
6388 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006389 }
6390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6392 &derivation);
6393 if (status != PSA_SUCCESS) {
6394 psa_key_derivation_abort(&derivation);
6395 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006396 }
6397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 status = psa_key_derivation_output_bytes(&derivation,
6399 handshake->premaster,
6400 handshake->pmslen);
6401 if (status != PSA_SUCCESS) {
6402 psa_key_derivation_abort(&derivation);
6403 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6404 }
6405
6406 status = psa_key_derivation_abort(&derivation);
6407 if (status != PSA_SUCCESS) {
6408 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006409 }
6410 }
6411#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6413 lbl, seed, seed_len,
6414 master,
6415 master_secret_len);
6416 if (ret != 0) {
6417 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6418 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006419 }
6420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6422 handshake->premaster,
6423 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 mbedtls_platform_zeroize(handshake->premaster,
6426 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006427 }
6428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006430}
6431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006433{
6434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6435 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6436 ssl->handshake->ciphersuite_info;
6437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006439
6440 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 ret = ssl_set_handshake_prfs(ssl->handshake,
6442 ciphersuite_info->mac);
6443 if (ret != 0) {
6444 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6445 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006446 }
6447
6448 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 ret = ssl_compute_master(ssl->handshake,
6450 ssl->session_negotiate->master,
6451 ssl);
6452 if (ret != 0) {
6453 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6454 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006455 }
6456
6457 /* Swap the client and server random values:
6458 * - MS derivation wanted client+server (RFC 5246 8.1)
6459 * - key derivation wants server+client (RFC 5246 6.3) */
6460 {
6461 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 memcpy(tmp, ssl->handshake->randbytes, 64);
6463 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6464 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6465 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006466 }
6467
6468 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6470 ssl->session_negotiate->ciphersuite,
6471 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006472#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006474#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 ssl->handshake->tls_prf,
6476 ssl->handshake->randbytes,
6477 ssl->tls_version,
6478 ssl->conf->endpoint,
6479 ssl);
6480 if (ret != 0) {
6481 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6482 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006483 }
6484
6485 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6487 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006492}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006495{
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006497#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006498 case MBEDTLS_SSL_HASH_SHA384:
6499 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6500 break;
6501#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006502#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006503 case MBEDTLS_SSL_HASH_SHA256:
6504 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6505 break;
6506#endif
6507 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006509 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006510#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6511 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6512 (void) ssl;
6513#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006515}
6516
Andrzej Kurek25f27152022-08-17 16:09:31 -04006517#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006518void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6519 unsigned char *hash,
6520 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006521{
6522#if defined(MBEDTLS_USE_PSA_CRYPTO)
6523 size_t hash_size;
6524 psa_status_t status;
6525 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6528 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6529 if (status != PSA_SUCCESS) {
6530 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006531 return;
6532 }
6533
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6535 if (status != PSA_SUCCESS) {
6536 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006537 return;
6538 }
6539
6540 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6542 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006543#else
6544 mbedtls_sha256_context sha256;
6545
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6551 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006552
6553 *hlen = 32;
6554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6556 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006557
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006559#endif /* MBEDTLS_USE_PSA_CRYPTO */
6560 return;
6561}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006562#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006563
Andrzej Kurek25f27152022-08-17 16:09:31 -04006564#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006565void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6566 unsigned char *hash,
6567 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006568{
6569#if defined(MBEDTLS_USE_PSA_CRYPTO)
6570 size_t hash_size;
6571 psa_status_t status;
6572 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6573
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6575 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6576 if (status != PSA_SUCCESS) {
6577 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006578 return;
6579 }
6580
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6582 if (status != PSA_SUCCESS) {
6583 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006584 return;
6585 }
6586
6587 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6589 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006590#else
6591 mbedtls_sha512_context sha512;
6592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6598 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006599
6600 *hlen = 48;
6601
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6603 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006606#endif /* MBEDTLS_USE_PSA_CRYPTO */
6607 return;
6608}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006609#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006610
Neil Armstrong80f6f322022-05-03 17:56:38 +02006611#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6612 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006613int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006614{
6615 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006617 const unsigned char *psk = NULL;
6618 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006622 /*
6623 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006624 * checked before calling this function.
6625 *
6626 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006627 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6630 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6631 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006632 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006633 }
6634
6635 /*
6636 * PMS = struct {
6637 * opaque other_secret<0..2^16-1>;
6638 * opaque psk<0..2^16-1>;
6639 * };
6640 * with "other_secret" depending on the particular key exchange
6641 */
6642#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6644 if (end - p < 2) {
6645 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6646 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006649 p += 2;
6650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 if (end < p || (size_t) (end - p) < psk_len) {
6652 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6653 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006656 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006658#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6659#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006661 /*
6662 * other_secret already set by the ClientKeyExchange message,
6663 * and is 48 bytes long
6664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 if (end - p < 2) {
6666 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6667 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006668
6669 *p++ = 0;
6670 *p++ = 48;
6671 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006673#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6674#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6677 size_t len;
6678
6679 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6681 p + 2, end - (p + 2), &len,
6682 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6684 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006685 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006687 p += 2 + len;
6688
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6690 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006691#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006692#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6695 size_t zlen;
6696
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6698 p + 2, end - (p + 2),
6699 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6700 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6701 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006702 }
6703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006705 p += 2 + zlen;
6706
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6708 MBEDTLS_DEBUG_ECDH_Z);
6709 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006710#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006711 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6713 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006714 }
6715
6716 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 if (end - p < 2) {
6718 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6719 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006720
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006722 p += 2;
6723
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 if (end < p || (size_t) (end - p) < psk_len) {
6725 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6726 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006729 p += psk_len;
6730
6731 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6732
Gilles Peskine449bd832023-01-11 14:50:10 +01006733 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006734}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006735#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006736
6737#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006738MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006739static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006740
6741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006742int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006743{
6744 /* If renegotiation is not enforced, retransmit until we would reach max
6745 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006747 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6748 unsigned char doublings = 1;
6749
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006751 ++doublings;
6752 ratio >>= 1;
6753 }
6754
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 if (++ssl->renego_records_seen > doublings) {
6756 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6757 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006758 }
6759 }
6760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006762}
6763#endif
6764#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006765
Jerry Yud9526692022-02-17 14:23:47 +08006766/*
6767 * Handshake functions
6768 */
6769#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6770/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006771int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006772{
6773 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6774 ssl->handshake->ciphersuite_info;
6775
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6779 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006780 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006782 }
6783
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6785 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006786}
6787
Gilles Peskine449bd832023-01-11 14:50:10 +01006788int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006789{
6790 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6791 ssl->handshake->ciphersuite_info;
6792
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006794
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6796 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006797 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006799 }
6800
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6802 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006803}
6804
6805#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6806/* Some certificate support -> implement write and parse */
6807
Gilles Peskine449bd832023-01-11 14:50:10 +01006808int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006809{
6810 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6811 size_t i, n;
6812 const mbedtls_x509_crt *crt;
6813 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6814 ssl->handshake->ciphersuite_info;
6815
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006817
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6819 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006820 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006822 }
6823
6824#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6826 if (ssl->handshake->client_auth == 0) {
6827 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006828 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006830 }
6831 }
6832#endif /* MBEDTLS_SSL_CLI_C */
6833#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6835 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006836 /* Should never happen because we shouldn't have picked the
6837 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006839 }
6840 }
6841#endif
6842
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006844
6845 /*
6846 * 0 . 0 handshake type
6847 * 1 . 3 handshake length
6848 * 4 . 6 length of all certs
6849 * 7 . 9 length of cert. 1
6850 * 10 . n-1 peer certificate
6851 * n . n+2 length of cert. 2
6852 * n+3 . ... upper level cert, etc.
6853 */
6854 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006858 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6860 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6861 " > %" MBEDTLS_PRINTF_SIZET,
6862 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6863 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006864 }
6865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6867 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6868 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006869
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006871 i += n; crt = crt->next;
6872 }
6873
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6875 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6876 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006877
6878 ssl->out_msglen = i;
6879 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6880 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6881
6882 ssl->state++;
6883
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6885 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6886 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006887 }
6888
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006890
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006892}
6893
6894#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6895
6896#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006897MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006898static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6899 unsigned char *crt_buf,
6900 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006901{
6902 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6903
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 if (peer_crt == NULL) {
6905 return -1;
6906 }
Jerry Yud9526692022-02-17 14:23:47 +08006907
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 if (peer_crt->raw.len != crt_buf_len) {
6909 return -1;
6910 }
Jerry Yud9526692022-02-17 14:23:47 +08006911
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006913}
6914#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006915MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006916static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6917 unsigned char *crt_buf,
6918 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006919{
6920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6921 unsigned char const * const peer_cert_digest =
6922 ssl->session->peer_cert_digest;
6923 mbedtls_md_type_t const peer_cert_digest_type =
6924 ssl->session->peer_cert_digest_type;
6925 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006927 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6928 size_t digest_len;
6929
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 if (peer_cert_digest == NULL || digest_info == NULL) {
6931 return -1;
6932 }
Jerry Yud9526692022-02-17 14:23:47 +08006933
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 digest_len = mbedtls_md_get_size(digest_info);
6935 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6936 return -1;
6937 }
Jerry Yud9526692022-02-17 14:23:47 +08006938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6940 if (ret != 0) {
6941 return -1;
6942 }
Jerry Yud9526692022-02-17 14:23:47 +08006943
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006945}
6946#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6947#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6948
6949/*
6950 * Once the certificate message is read, parse it into a cert chain and
6951 * perform basic checks, but leave actual verification to the caller
6952 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006953MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006954static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6955 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006956{
6957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6958#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006960#endif
6961 size_t i, n;
6962 uint8_t alert;
6963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
6965 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6966 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6967 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6968 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006969 }
6970
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
6972 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6973 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6974 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006975 }
6976
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
6978 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6979 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6981 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006982 }
6983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006985
6986 /*
6987 * Same message structure as in mbedtls_ssl_write_certificate()
6988 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08006990
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 if (ssl->in_msg[i] != 0 ||
6992 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
6993 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6994 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6995 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6996 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006997 }
6998
6999 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7000 i += 3;
7001
7002 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007004 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (i + 3 > ssl->in_hslen) {
7006 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7007 mbedtls_ssl_send_alert_message(ssl,
7008 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7009 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7010 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007011 }
7012 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7013 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 if (ssl->in_msg[i] != 0) {
7015 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7016 mbedtls_ssl_send_alert_message(ssl,
7017 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7018 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7019 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007020 }
7021
7022 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007024 | (unsigned int) ssl->in_msg[i + 2];
7025 i += 3;
7026
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 if (n < 128 || i + n > ssl->in_hslen) {
7028 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7029 mbedtls_ssl_send_alert_message(ssl,
7030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7031 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7032 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007033 }
7034
7035 /* Check if we're handling the first CRT in the chain. */
7036#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007038 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007040 /* During client-side renegotiation, check that the server's
7041 * end-CRTs hasn't changed compared to the initial handshake,
7042 * mitigating the triple handshake attack. On success, reuse
7043 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7045 if (ssl_check_peer_crt_unchanged(ssl,
7046 &ssl->in_msg[i],
7047 n) != 0) {
7048 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7049 mbedtls_ssl_send_alert_message(ssl,
7050 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7051 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7052 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007053 }
7054
7055 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007057 }
7058#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7059
7060 /* Parse the next certificate in the chain. */
7061#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007063#else
7064 /* If we don't need to store the CRT chain permanently, parse
7065 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007067#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007069 case 0: /*ok*/
7070 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7071 /* Ignore certificate with an unknown algorithm: maybe a
7072 prior certificate was already trusted. */
7073 break;
7074
7075 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7076 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7077 goto crt_parse_der_failed;
7078
7079 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7080 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7081 goto crt_parse_der_failed;
7082
7083 default:
7084 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007085crt_parse_der_failed:
7086 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7087 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7088 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007089 }
7090
7091 i += n;
7092 }
7093
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7095 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007096}
7097
7098#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007099MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007100static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007101{
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7103 return -1;
7104 }
Jerry Yud9526692022-02-17 14:23:47 +08007105
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007107 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7108 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7110 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7111 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007112 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007114}
7115#endif /* MBEDTLS_SSL_SRV_C */
7116
7117/* Check if a certificate message is expected.
7118 * Return either
7119 * - SSL_CERTIFICATE_EXPECTED, or
7120 * - SSL_CERTIFICATE_SKIP
7121 * indicating whether a Certificate message is expected or not.
7122 */
7123#define SSL_CERTIFICATE_EXPECTED 0
7124#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007125MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007126static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7127 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007128{
7129 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7130 ssl->handshake->ciphersuite_info;
7131
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7133 return SSL_CERTIFICATE_SKIP;
7134 }
Jerry Yud9526692022-02-17 14:23:47 +08007135
7136#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7138 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7139 return SSL_CERTIFICATE_SKIP;
7140 }
Jerry Yud9526692022-02-17 14:23:47 +08007141
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007143 ssl->session_negotiate->verify_result =
7144 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007146 }
7147 }
7148#else
7149 ((void) authmode);
7150#endif /* MBEDTLS_SSL_SRV_C */
7151
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007153}
7154
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007155MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007156static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7157 int authmode,
7158 mbedtls_x509_crt *chain,
7159 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007160{
7161 int ret = 0;
7162 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7163 ssl->handshake->ciphersuite_info;
7164 int have_ca_chain = 0;
7165
7166 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7167 void *p_vrfy;
7168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7170 return 0;
7171 }
Jerry Yud9526692022-02-17 14:23:47 +08007172
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (ssl->f_vrfy != NULL) {
7174 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007175 f_vrfy = ssl->f_vrfy;
7176 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 } else {
7178 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007179 f_vrfy = ssl->conf->f_vrfy;
7180 p_vrfy = ssl->conf->p_vrfy;
7181 }
7182
7183 /*
7184 * Main check: verify certificate
7185 */
7186#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007188 ((void) rs_ctx);
7189 have_ca_chain = 1;
7190
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007192 ret = mbedtls_x509_crt_verify_with_ca_cb(
7193 chain,
7194 ssl->conf->f_ca_cb,
7195 ssl->conf->p_ca_cb,
7196 ssl->conf->cert_profile,
7197 ssl->hostname,
7198 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 f_vrfy, p_vrfy);
7200 } else
Jerry Yud9526692022-02-17 14:23:47 +08007201#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7202 {
7203 mbedtls_x509_crt *ca_chain;
7204 mbedtls_x509_crl *ca_crl;
7205
7206#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007208 ca_chain = ssl->handshake->sni_ca_chain;
7209 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 } else
Jerry Yud9526692022-02-17 14:23:47 +08007211#endif
7212 {
7213 ca_chain = ssl->conf->ca_chain;
7214 ca_crl = ssl->conf->ca_crl;
7215 }
7216
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007218 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 }
Jerry Yud9526692022-02-17 14:23:47 +08007220
7221 ret = mbedtls_x509_crt_verify_restartable(
7222 chain,
7223 ca_chain, ca_crl,
7224 ssl->conf->cert_profile,
7225 ssl->hostname,
7226 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007228 }
7229
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 if (ret != 0) {
7231 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007232 }
7233
7234#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7236 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7237 }
Jerry Yud9526692022-02-17 14:23:47 +08007238#endif
7239
7240 /*
7241 * Secondary checks: always done, but change 'ret' only if it was 0
7242 */
7243
7244#if defined(MBEDTLS_ECP_C)
7245 {
7246 const mbedtls_pk_context *pk = &chain->pk;
7247
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007248 /* If certificate uses an EC key, make sure the curve is OK.
7249 * This is a public key, so it can't be opaque, so can_do() is a good
7250 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007252 /* and in the unlikely case the above assumption no longer holds
7253 * we are making sure that pk_ec() here does not return a NULL
7254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7256 if (ec == NULL) {
7257 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7258 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007259 }
Jerry Yud9526692022-02-17 14:23:47 +08007260
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007262 ssl->session_negotiate->verify_result |=
7263 MBEDTLS_X509_BADCERT_BAD_KEY;
7264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7266 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007267 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007269 }
Jerry Yud9526692022-02-17 14:23:47 +08007270 }
7271 }
7272#endif /* MBEDTLS_ECP_C */
7273
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 if (mbedtls_ssl_check_cert_usage(chain,
7275 ciphersuite_info,
7276 !ssl->conf->endpoint,
7277 &ssl->session_negotiate->verify_result) != 0) {
7278 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7279 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007280 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 }
Jerry Yud9526692022-02-17 14:23:47 +08007282 }
7283
7284 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7285 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7286 * with details encoded in the verification flags. All other kinds
7287 * of error codes, including those from the user provided f_vrfy
7288 * functions, are treated as fatal and lead to a failure of
7289 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007290 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7291 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7292 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007293 ret = 0;
7294 }
7295
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7297 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007298 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7299 }
7300
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007302 uint8_t alert;
7303
7304 /* The certificate may have been rejected for several reasons.
7305 Pick one and send the corresponding alert. Which alert to send
7306 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007308 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007310 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007312 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007314 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007316 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007318 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007320 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007322 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007324 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007326 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007328 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 }
7330 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7331 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007332 }
7333
7334#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 if (ssl->session_negotiate->verify_result != 0) {
7336 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7337 (unsigned int) ssl->session_negotiate->verify_result));
7338 } else {
7339 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007340 }
7341#endif /* MBEDTLS_DEBUG_C */
7342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007344}
7345
7346#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007347MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007348static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7349 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007350{
7351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7352 /* Remember digest of the peer's end-CRT. */
7353 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7355 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7356 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7357 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7358 mbedtls_ssl_send_alert_message(ssl,
7359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7360 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007361
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007363 }
7364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 ret = mbedtls_md(mbedtls_md_info_from_type(
7366 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7367 start, len,
7368 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007369
7370 ssl->session_negotiate->peer_cert_digest_type =
7371 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7372 ssl->session_negotiate->peer_cert_digest_len =
7373 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7374
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007376}
7377
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007378MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007379static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7380 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007381{
7382 unsigned char *end = start + len;
7383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7384
7385 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7387 ret = mbedtls_pk_parse_subpubkey(&start, end,
7388 &ssl->handshake->peer_pubkey);
7389 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007390 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007392 }
7393
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007395}
7396#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7397
Gilles Peskine449bd832023-01-11 14:50:10 +01007398int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007399{
7400 int ret = 0;
7401 int crt_expected;
7402#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7403 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7404 ? ssl->handshake->sni_authmode
7405 : ssl->conf->authmode;
7406#else
7407 const int authmode = ssl->conf->authmode;
7408#endif
7409 void *rs_ctx = NULL;
7410 mbedtls_x509_crt *chain = NULL;
7411
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7415 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7416 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007417 goto exit;
7418 }
7419
7420#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 if (ssl->handshake->ecrs_enabled &&
7422 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007423 chain = ssl->handshake->ecrs_peer_cert;
7424 ssl->handshake->ecrs_peer_cert = NULL;
7425 goto crt_verify;
7426 }
7427#endif
7428
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007430 /* mbedtls_ssl_read_record may have sent an alert already. We
7431 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007433 goto exit;
7434 }
7435
7436#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007438 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7439
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007441 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 }
Jerry Yud9526692022-02-17 14:23:47 +08007443
7444 goto exit;
7445 }
7446#endif /* MBEDTLS_SSL_SRV_C */
7447
7448 /* Clear existing peer CRT structure in case we tried to
7449 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007451
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7453 if (chain == NULL) {
7454 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7455 sizeof(mbedtls_x509_crt)));
7456 mbedtls_ssl_send_alert_message(ssl,
7457 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7458 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007459
7460 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7461 goto exit;
7462 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007464
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 ret = ssl_parse_certificate_chain(ssl, chain);
7466 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 }
Jerry Yud9526692022-02-17 14:23:47 +08007469
7470#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007472 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 }
Jerry Yud9526692022-02-17 14:23:47 +08007474
7475crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007477 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 }
Jerry Yud9526692022-02-17 14:23:47 +08007479#endif
7480
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 ret = ssl_parse_certificate_verify(ssl, authmode,
7482 chain, rs_ctx);
7483 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007484 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 }
Jerry Yud9526692022-02-17 14:23:47 +08007486
7487#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7488 {
7489 unsigned char *crt_start, *pk_start;
7490 size_t crt_len, pk_len;
7491
7492 /* We parse the CRT chain without copying, so
7493 * these pointers point into the input buffer,
7494 * and are hence still valid after freeing the
7495 * CRT chain. */
7496
7497 crt_start = chain->raw.p;
7498 crt_len = chain->raw.len;
7499
7500 pk_start = chain->pk_raw.p;
7501 pk_len = chain->pk_raw.len;
7502
7503 /* Free the CRT structures before computing
7504 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 mbedtls_x509_crt_free(chain);
7506 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007507 chain = NULL;
7508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7510 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007511 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 }
Jerry Yud9526692022-02-17 14:23:47 +08007513
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7515 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007516 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 }
Jerry Yud9526692022-02-17 14:23:47 +08007518 }
7519#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7520 /* Pass ownership to session structure. */
7521 ssl->session_negotiate->peer_cert = chain;
7522 chain = NULL;
7523#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7524
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007526
7527exit:
7528
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007530 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 }
Jerry Yud9526692022-02-17 14:23:47 +08007532
7533#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007535 ssl->handshake->ecrs_peer_cert = chain;
7536 chain = NULL;
7537 }
7538#endif
7539
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 if (chain != NULL) {
7541 mbedtls_x509_crt_free(chain);
7542 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007543 }
7544
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007546}
7547#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7548
Andrzej Kurek25f27152022-08-17 16:09:31 -04007549#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007550static void ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007552{
7553 int len = 12;
7554 const char *sender;
7555 unsigned char padbuf[32];
7556#if defined(MBEDTLS_USE_PSA_CRYPTO)
7557 size_t hash_size;
7558 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7559 psa_status_t status;
7560#else
7561 mbedtls_sha256_context sha256;
7562#endif
7563
7564 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007566 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007568
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007570 ? "client finished"
7571 : "server finished";
7572
7573#if defined(MBEDTLS_USE_PSA_CRYPTO)
7574 sha256_psa = psa_hash_operation_init();
7575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7579 if (status != PSA_SUCCESS) {
7580 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007581 return;
7582 }
7583
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7585 if (status != PSA_SUCCESS) {
7586 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007587 return;
7588 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007590#else
7591
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007593
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007595
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007597
7598 /*
7599 * TLSv1.2:
7600 * hash = PRF( master, finished_label,
7601 * Hash( handshake ) )[0.11]
7602 */
7603
7604#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7606 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007607#endif
7608
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 mbedtls_sha256_finish(&sha256, padbuf);
7610 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007611#endif /* MBEDTLS_USE_PSA_CRYPTO */
7612
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 ssl->handshake->tls_prf(session->master, 48, sender,
7614 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007615
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007621}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007622#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007623
Jerry Yub7ba49e2022-02-17 14:25:53 +08007624
Andrzej Kurek25f27152022-08-17 16:09:31 -04007625#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007626static void ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007628{
7629 int len = 12;
7630 const char *sender;
7631 unsigned char padbuf[48];
7632#if defined(MBEDTLS_USE_PSA_CRYPTO)
7633 size_t hash_size;
7634 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7635 psa_status_t status;
7636#else
7637 mbedtls_sha512_context sha512;
7638#endif
7639
7640 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007642 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007646 ? "client finished"
7647 : "server finished";
7648
7649#if defined(MBEDTLS_USE_PSA_CRYPTO)
7650 sha384_psa = psa_hash_operation_init();
7651
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7655 if (status != PSA_SUCCESS) {
7656 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007657 return;
7658 }
7659
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7661 if (status != PSA_SUCCESS) {
7662 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007663 return;
7664 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007666#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007668
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007670
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007672
7673 /*
7674 * TLSv1.2:
7675 * hash = PRF( master, finished_label,
7676 * Hash( handshake ) )[0.11]
7677 */
7678
7679#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7681 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007682#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007684
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007686#endif
7687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 ssl->handshake->tls_prf(session->master, 48, sender,
7689 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007692
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007696}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007697#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007698
Gilles Peskine449bd832023-01-11 14:50:10 +01007699void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007700{
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007702
7703 /*
7704 * Free our handshake params
7705 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 mbedtls_ssl_handshake_free(ssl);
7707 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007708 ssl->handshake = NULL;
7709
7710 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007711 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007713 if (ssl->transform) {
7714 mbedtls_ssl_transform_free(ssl->transform);
7715 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007716 }
7717 ssl->transform = ssl->transform_negotiate;
7718 ssl->transform_negotiate = NULL;
7719
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007721}
7722
Gilles Peskine449bd832023-01-11 14:50:10 +01007723void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007724{
7725 int resume = ssl->handshake->resume;
7726
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007728
7729#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007731 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7732 ssl->renego_records_seen = 0;
7733 }
7734#endif
7735
7736 /*
7737 * Free the previous session and switch in the current one
7738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007740#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7741 /* RFC 7366 3.1: keep the EtM state */
7742 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007744#endif
7745
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 mbedtls_ssl_session_free(ssl->session);
7747 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007748 }
7749 ssl->session = ssl->session_negotiate;
7750 ssl->session_negotiate = NULL;
7751
7752 /*
7753 * Add cache entry
7754 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007756 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007757 resume == 0) {
7758 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7759 ssl->session->id,
7760 ssl->session->id_len,
7761 ssl->session) != 0) {
7762 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7763 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007764 }
7765
7766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007767 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7768 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007769 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007771
7772 /* Keep last flight around in case we need to resend it:
7773 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7775 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007776#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007778
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007779 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007780
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007782}
7783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007785{
7786 int ret, hash_len;
7787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007789
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007791
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007793
7794 /*
7795 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7796 * may define some other value. Currently (early 2016), no defined
7797 * ciphersuite does this (and this is unlikely to change as activity has
7798 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7799 */
7800 hash_len = 12;
7801
7802#if defined(MBEDTLS_SSL_RENEGOTIATION)
7803 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007805#endif
7806
7807 ssl->out_msglen = 4 + hash_len;
7808 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7809 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7810
7811 /*
7812 * In case of session resuming, invert the client and server
7813 * ChangeCipherSpec messages order.
7814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007816#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007818 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007820#endif
7821#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007823 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007824 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007825#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007827 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007829
7830 /*
7831 * Switch to our negotiated transform and session parameters for outbound
7832 * data.
7833 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007834 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007835
7836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007838 unsigned char i;
7839
7840 /* Remember current epoch settings for resending */
7841 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7843 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007844
7845 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007847
7848
7849 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007850 for (i = 2; i > 0; i--) {
7851 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007852 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007853 }
7854 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007855
7856 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007857 if (i == 0) {
7858 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7859 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007860 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007862#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007863 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007864
7865 ssl->transform_out = ssl->transform_negotiate;
7866 ssl->session_out = ssl->session_negotiate;
7867
7868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007869 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7870 mbedtls_ssl_send_flight_completed(ssl);
7871 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007872#endif
7873
Gilles Peskine449bd832023-01-11 14:50:10 +01007874 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7875 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7876 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007877 }
7878
7879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7881 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7882 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7883 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007884 }
7885#endif
7886
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007888
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007890}
7891
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007892#define SSL_MAX_HASH_LEN 12
7893
Gilles Peskine449bd832023-01-11 14:50:10 +01007894int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007895{
7896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7897 unsigned int hash_len = 12;
7898 unsigned char buf[SSL_MAX_HASH_LEN];
7899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7905 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007906 goto exit;
7907 }
7908
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7910 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7911 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7912 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007913 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7914 goto exit;
7915 }
7916
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7918 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7919 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007920 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7921 goto exit;
7922 }
7923
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7925 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7926 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7927 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007928 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7929 goto exit;
7930 }
7931
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7933 buf, hash_len) != 0) {
7934 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7935 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7936 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007937 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7938 goto exit;
7939 }
7940
7941#if defined(MBEDTLS_SSL_RENEGOTIATION)
7942 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007943 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007944#endif
7945
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007947#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007949 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007951#endif
7952#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007954 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007956#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007957 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007958 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007959 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007960
7961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7963 mbedtls_ssl_recv_flight_completed(ssl);
7964 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007965#endif
7966
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007968
7969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007970 mbedtls_platform_zeroize(buf, hash_len);
7971 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007972}
7973
Jerry Yu392112c2022-02-17 14:34:10 +08007974#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7975/*
7976 * Helper to get TLS 1.2 PRF from ciphersuite
7977 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007979static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08007980{
Jerry Yu392112c2022-02-17 14:34:10 +08007981 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04007983#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007984 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
7985 return tls_prf_sha384;
7986 } else
Jerry Yu392112c2022-02-17 14:34:10 +08007987#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007988#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7989 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
7991 return tls_prf_sha256;
7992 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04007993 }
7994#endif
7995#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7996 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7997 (void) ciphersuite_info;
7998#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007999
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008001}
8002#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008005{
8006 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008007#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008008 if (tls_prf == tls_prf_sha384) {
8009 return MBEDTLS_SSL_TLS_PRF_SHA384;
8010 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008011#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008012#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008013 if (tls_prf == tls_prf_sha256) {
8014 return MBEDTLS_SSL_TLS_PRF_SHA256;
8015 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008016#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008017 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008018}
8019
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008020/*
8021 * Populate a transform structure with session keys and all the other
8022 * necessary information.
8023 *
8024 * Parameters:
8025 * - [in/out]: transform: structure to populate
8026 * [in] must be just initialised with mbedtls_ssl_transform_init()
8027 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8028 * - [in] ciphersuite
8029 * - [in] master
8030 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008031 * - [in] tls_prf: pointer to PRF to use for key derivation
8032 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008033 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008034 * - [in] endpoint: client or server
8035 * - [in] ssl: used for:
8036 * - ssl->conf->{f,p}_export_keys
8037 * [in] optionally used for:
8038 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8039 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008040MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008041static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8042 int ciphersuite,
8043 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008044#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008046#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 ssl_tls_prf_t tls_prf,
8048 const unsigned char randbytes[64],
8049 mbedtls_ssl_protocol_version tls_version,
8050 unsigned endpoint,
8051 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008052{
8053 int ret = 0;
8054 unsigned char keyblk[256];
8055 unsigned char *key1;
8056 unsigned char *key2;
8057 unsigned char *mac_enc;
8058 unsigned char *mac_dec;
8059 size_t mac_key_len = 0;
8060 size_t iv_copy_len;
8061 size_t keylen;
8062 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008063 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008064#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008065 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008066 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008067#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008068
8069#if defined(MBEDTLS_USE_PSA_CRYPTO)
8070 psa_key_type_t key_type;
8071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8072 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008073 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008074 size_t key_bits;
8075 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8076#endif
8077
8078#if !defined(MBEDTLS_DEBUG_C) && \
8079 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008080 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008081 ssl = NULL; /* make sure we don't use it except for these cases */
8082 (void) ssl;
8083 }
8084#endif
8085
8086 /*
8087 * Some data just needs copying into the structure
8088 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008089#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008090 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008091#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008092 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008093
8094#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008096#endif
8097
8098#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008099 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008100 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8101 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008102 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008103 }
8104#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8105
8106 /*
8107 * Get various info structures
8108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8110 if (ciphersuite_info == NULL) {
8111 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8112 ciphersuite));
8113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008114 }
8115
Neil Armstrongab555e02022-04-04 11:07:59 +02008116 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008117#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008118 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008119#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008121
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008123 transform->taglen =
8124 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008125 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008126
8127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8129 transform->taglen,
8130 &alg,
8131 &key_type,
8132 &key_bits)) != PSA_SUCCESS) {
8133 ret = psa_ssl_status_to_mbedtls(status);
8134 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008135 goto end;
8136 }
8137#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8139 if (cipher_info == NULL) {
8140 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8141 ciphersuite_info->cipher));
8142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008143 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008144#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008145
Neil Armstronge4512952022-03-08 09:08:22 +01008146#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8148 if (mac_alg == 0) {
8149 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8150 (unsigned) ciphersuite_info->mac));
8151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008152 }
8153#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8155 if (md_info == NULL) {
8156 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8157 (unsigned) ciphersuite_info->mac));
8158 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008159 }
Neil Armstronge4512952022-03-08 09:08:22 +01008160#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008161
8162#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8163 /* Copy own and peer's CID if the use of the CID
8164 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8166 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008167
8168 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8170 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8171 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008172
8173 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008174 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8175 ssl->handshake->peer_cid_len);
8176 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8177 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008178 }
8179#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8180
8181 /*
8182 * Compute key block using the PRF
8183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008184 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8185 if (ret != 0) {
8186 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8187 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008188 }
8189
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8191 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8192 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8193 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8194 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008195
8196 /*
8197 * Determine the appropriate key, IV and MAC length.
8198 */
8199
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008200#if defined(MBEDTLS_USE_PSA_CRYPTO)
8201 keylen = PSA_BITS_TO_BYTES(key_bits);
8202#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008204#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008205
8206#if defined(MBEDTLS_GCM_C) || \
8207 defined(MBEDTLS_CCM_C) || \
8208 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008210 size_t explicit_ivlen;
8211
8212 transform->maclen = 0;
8213 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008214
8215 /* All modes haves 96-bit IVs, but the length of the static parts vary
8216 * with mode and version:
8217 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8218 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8219 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8220 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8221 * sequence number).
8222 */
8223 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008224
8225 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008226#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008228#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8230 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008231#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008232
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008234 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008236 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238
8239 /* Minimum length of encrypted record */
8240 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8241 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008243#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8244#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008246 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008248#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008250#else
8251 size_t block_size = cipher_info->block_size;
8252#endif /* MBEDTLS_USE_PSA_CRYPTO */
8253
8254#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008255 /* Get MAC length */
8256 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8257#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008258 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8260 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8261 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008262 goto end;
8263 }
8264
8265 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008267#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008268 transform->maclen = mac_key_len;
8269
8270 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008271#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008272 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008273#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008274 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008275#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008276
8277 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008279 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008281 /*
8282 * GenericBlockCipher:
8283 * 1. if EtM is in use: one block plus MAC
8284 * otherwise: * first multiple of blocklen greater than maclen
8285 * 2. IV
8286 */
8287#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008289 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 + block_size;
8291 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008292#endif
8293 {
8294 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 + block_size
8296 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008297 }
8298
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 } else {
8302 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008303 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8304 goto end;
8305 }
8306 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008308#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8309 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8311 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008312 }
8313
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8315 (unsigned) keylen,
8316 (unsigned) transform->minlen,
8317 (unsigned) transform->ivlen,
8318 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008319
8320 /*
8321 * Finally setup the cipher contexts, IVs and MAC secrets.
8322 */
8323#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008325 key1 = keyblk + mac_key_len * 2;
8326 key2 = keyblk + mac_key_len * 2 + keylen;
8327
8328 mac_enc = keyblk;
8329 mac_dec = keyblk + mac_key_len;
8330
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 iv_copy_len = (transform->fixed_ivlen) ?
8332 transform->fixed_ivlen : transform->ivlen;
8333 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8334 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8335 iv_copy_len);
8336 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008337#endif /* MBEDTLS_SSL_CLI_C */
8338#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340 key1 = keyblk + mac_key_len * 2 + keylen;
8341 key2 = keyblk + mac_key_len * 2;
8342
8343 mac_enc = keyblk + mac_key_len;
8344 mac_dec = keyblk;
8345
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 iv_copy_len = (transform->fixed_ivlen) ?
8347 transform->fixed_ivlen : transform->ivlen;
8348 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8349 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8350 iv_copy_len);
8351 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008352#endif /* MBEDTLS_SSL_SRV_C */
8353 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008355 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8356 goto end;
8357 }
8358
Gilles Peskine449bd832023-01-11 14:50:10 +01008359 if (ssl != NULL && ssl->f_export_keys != NULL) {
8360 ssl->f_export_keys(ssl->p_export_keys,
8361 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8362 master, 48,
8363 randbytes + 32,
8364 randbytes,
8365 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008366 }
8367
8368#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369 transform->psa_alg = alg;
8370
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8372 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8373 psa_set_key_algorithm(&attributes, alg);
8374 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008375
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 if ((status = psa_import_key(&attributes,
8377 key1,
8378 PSA_BITS_TO_BYTES(key_bits),
8379 &transform->psa_key_enc)) != PSA_SUCCESS) {
8380 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8381 ret = psa_ssl_status_to_mbedtls(status);
8382 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008383 goto end;
8384 }
8385
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008387
Gilles Peskine449bd832023-01-11 14:50:10 +01008388 if ((status = psa_import_key(&attributes,
8389 key2,
8390 PSA_BITS_TO_BYTES(key_bits),
8391 &transform->psa_key_dec)) != PSA_SUCCESS) {
8392 ret = psa_ssl_status_to_mbedtls(status);
8393 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008394 goto end;
8395 }
8396 }
8397#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8399 cipher_info)) != 0) {
8400 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008401 goto end;
8402 }
8403
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8405 cipher_info)) != 0) {
8406 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008407 goto end;
8408 }
8409
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8411 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8412 MBEDTLS_ENCRYPT)) != 0) {
8413 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008414 goto end;
8415 }
8416
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8418 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8419 MBEDTLS_DECRYPT)) != 0) {
8420 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008421 goto end;
8422 }
8423
8424#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008425 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8426 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8427 MBEDTLS_PADDING_NONE)) != 0) {
8428 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008429 goto end;
8430 }
8431
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8433 MBEDTLS_PADDING_NONE)) != 0) {
8434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008435 goto end;
8436 }
8437 }
8438#endif /* MBEDTLS_CIPHER_MODE_CBC */
8439#endif /* MBEDTLS_USE_PSA_CRYPTO */
8440
Neil Armstrong29c0c042022-03-17 17:47:28 +01008441#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8442 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8443 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008445#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008447
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8449 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8450 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 if ((status = psa_import_key(&attributes,
8453 mac_enc, mac_key_len,
8454 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8455 ret = psa_ssl_status_to_mbedtls(status);
8456 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008457 goto end;
8458 }
8459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8461 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008462#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008464#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008466 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8468 PSA_KEY_USAGE_VERIFY_HASH);
8469 } else {
8470 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8471 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008472
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 if ((status = psa_import_key(&attributes,
8474 mac_dec, mac_key_len,
8475 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8476 ret = psa_ssl_status_to_mbedtls(status);
8477 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008478 goto end;
8479 }
8480#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8482 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008483 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 }
8485 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8486 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008487 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008489#endif /* MBEDTLS_USE_PSA_CRYPTO */
8490 }
8491#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8492
8493 ((void) mac_dec);
8494 ((void) mac_enc);
8495
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008496end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8498 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008499}
8500
Valerio Settia08b1a42022-11-17 15:10:02 +01008501#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8502 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008503int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 psa_pake_operation_t *pake_ctx,
8505 const unsigned char *buf,
8506 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008507{
8508 psa_status_t status;
8509 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008510 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008511 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8512 * At round two perform a single cycle
8513 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008515
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 for (; remaining_steps > 0; remaining_steps--) {
8517 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008518 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008520 /* Length is stored at the first byte */
8521 size_t length = buf[input_offset];
8522 input_offset += 1;
8523
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008525 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8526 }
8527
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 status = psa_pake_input(pake_ctx, step,
8529 buf + input_offset, length);
8530 if (status != PSA_SUCCESS) {
8531 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008532 }
8533
8534 input_offset += length;
8535 }
8536 }
8537
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008539 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008541
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008543}
8544
Valerio Setti6b3dab02022-11-17 17:14:54 +01008545int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 psa_pake_operation_t *pake_ctx,
8547 unsigned char *buf,
8548 size_t len, size_t *olen,
8549 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008550{
8551 psa_status_t status;
8552 size_t output_offset = 0;
8553 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008554 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008555 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8556 * At round two perform a single cycle
8557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008559
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 for (; remaining_steps > 0; remaining_steps--) {
8561 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8562 step <= PSA_PAKE_STEP_ZK_PROOF;
8563 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008564 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008565 * For each step, prepend 1 byte with the length of the data as
8566 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008567 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 status = psa_pake_output(pake_ctx, step,
8569 buf + output_offset + 1,
8570 len - output_offset - 1,
8571 &output_len);
8572 if (status != PSA_SUCCESS) {
8573 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008574 }
8575
Valerio Setti99d88c12022-11-22 16:03:43 +01008576 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008577
8578 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008579 }
8580 }
8581
8582 *olen = output_offset;
8583
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008585}
Valerio Settia08b1a42022-11-17 15:10:02 +01008586#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8587
Jerry Yuee40f9d2022-02-17 14:55:16 +08008588#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008589int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8590 unsigned char *hash, size_t *hashlen,
8591 unsigned char *data, size_t data_len,
8592 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008593{
8594 psa_status_t status;
8595 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008599
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 if ((status = psa_hash_setup(&hash_operation,
8601 hash_alg)) != PSA_SUCCESS) {
8602 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008603 goto exit;
8604 }
8605
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8607 64)) != PSA_SUCCESS) {
8608 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008609 goto exit;
8610 }
8611
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 if ((status = psa_hash_update(&hash_operation,
8613 data, data_len)) != PSA_SUCCESS) {
8614 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008615 goto exit;
8616 }
8617
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8619 hashlen)) != PSA_SUCCESS) {
8620 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8621 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008622 }
8623
8624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 if (status != PSA_SUCCESS) {
8626 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8627 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8628 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008629 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008631 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8632 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008634 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008636 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008638 }
8639 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008641}
8642
8643#else
8644
Gilles Peskine449bd832023-01-11 14:50:10 +01008645int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8646 unsigned char *hash, size_t *hashlen,
8647 unsigned char *data, size_t data_len,
8648 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008649{
8650 int ret = 0;
8651 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8653 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008656
Gilles Peskine449bd832023-01-11 14:50:10 +01008657 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008658
8659 /*
8660 * digitally-signed struct {
8661 * opaque client_random[32];
8662 * opaque server_random[32];
8663 * ServerDHParams params;
8664 * };
8665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8667 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008668 goto exit;
8669 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008670 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8671 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008672 goto exit;
8673 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8675 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008676 goto exit;
8677 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8679 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008680 goto exit;
8681 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008684 goto exit;
8685 }
8686
8687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008689
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 if (ret != 0) {
8691 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8692 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8693 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008696}
8697#endif /* MBEDTLS_USE_PSA_CRYPTO */
8698
Jerry Yud9d91da2022-02-17 14:57:06 +08008699#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8700
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008701/* Find the preferred hash for a given signature algorithm. */
8702unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 mbedtls_ssl_context *ssl,
8704 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008705{
Gabor Mezei078e8032022-04-27 21:17:56 +02008706 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008707 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008708
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8710 return MBEDTLS_SSL_HASH_NONE;
8711 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008712
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008714 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8716 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008717 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8719 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008720
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008722#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008724 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008726
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8728 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8729 PSA_ALG_ECDSA(psa_hash_alg),
8730 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008731 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8735 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8736 PSA_ALG_RSA_PKCS1V15_SIGN(
8737 psa_hash_alg),
8738 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008739 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008740 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008741 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008742#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008743
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008745 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008746 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008747
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008749}
8750
8751#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8752
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008753/* Serialization of TLS 1.2 sessions:
8754 *
8755 * struct {
8756 * uint64 start_time;
8757 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008758 * uint8 session_id_len; // at most 32
8759 * opaque session_id[32];
8760 * opaque master[48]; // fixed length in the standard
8761 * uint32 verify_result;
8762 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8763 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8764 * uint32 ticket_lifetime;
8765 * uint8 mfl_code; // up to 255 according to standard
8766 * uint8 encrypt_then_mac; // 0 or 1
8767 * } serialized_session_tls12;
8768 *
8769 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008770static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8771 unsigned char *buf,
8772 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008773{
8774 unsigned char *p = buf;
8775 size_t used = 0;
8776
8777#if defined(MBEDTLS_HAVE_TIME)
8778 uint64_t start;
8779#endif
8780#if defined(MBEDTLS_X509_CRT_PARSE_C)
8781#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8782 size_t cert_len;
8783#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8784#endif /* MBEDTLS_X509_CRT_PARSE_C */
8785
8786 /*
8787 * Time
8788 */
8789#if defined(MBEDTLS_HAVE_TIME)
8790 used += 8;
8791
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008793 start = (uint64_t) session->start;
8794
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008796 p += 8;
8797 }
8798#endif /* MBEDTLS_HAVE_TIME */
8799
8800 /*
8801 * Basic mandatory fields
8802 */
8803 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 + 1 /* id_len */
8805 + sizeof(session->id)
8806 + sizeof(session->master)
8807 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008808
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 if (used <= buf_len) {
8810 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008811 p += 2;
8812
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 *p++ = MBEDTLS_BYTE_0(session->id_len);
8814 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008815 p += 32;
8816
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008818 p += 48;
8819
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008821 p += 4;
8822 }
8823
8824 /*
8825 * Peer's end-entity certificate
8826 */
8827#if defined(MBEDTLS_X509_CRT_PARSE_C)
8828#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008830 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008832 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008834
8835 used += 3 + cert_len;
8836
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 if (used <= buf_len) {
8838 *p++ = MBEDTLS_BYTE_2(cert_len);
8839 *p++ = MBEDTLS_BYTE_1(cert_len);
8840 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008841
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 if (session->peer_cert != NULL) {
8843 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008844 p += cert_len;
8845 }
8846 }
8847#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008849 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008851 *p++ = (unsigned char) session->peer_cert_digest_type;
8852 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 memcpy(p, session->peer_cert_digest,
8854 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008855 p += session->peer_cert_digest_len;
8856 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008858 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008860 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8861 *p++ = 0;
8862 }
8863 }
8864#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8865#endif /* MBEDTLS_X509_CRT_PARSE_C */
8866
8867 /*
8868 * Session ticket if any, plus associated data
8869 */
8870#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8871 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 if (used <= buf_len) {
8874 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8875 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8876 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008877
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 if (session->ticket != NULL) {
8879 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008880 p += session->ticket_len;
8881 }
8882
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008884 p += 4;
8885 }
8886#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8887
8888 /*
8889 * Misc extension-related info
8890 */
8891#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8892 used += 1;
8893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008895 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008897#endif
8898
8899#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8900 used += 1;
8901
Gilles Peskine449bd832023-01-11 14:50:10 +01008902 if (used <= buf_len) {
8903 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8904 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008905#endif
8906
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008908}
8909
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008910MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008911static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8912 const unsigned char *buf,
8913 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008914{
8915#if defined(MBEDTLS_HAVE_TIME)
8916 uint64_t start;
8917#endif
8918#if defined(MBEDTLS_X509_CRT_PARSE_C)
8919#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8920 size_t cert_len;
8921#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8922#endif /* MBEDTLS_X509_CRT_PARSE_C */
8923
8924 const unsigned char *p = buf;
8925 const unsigned char * const end = buf + len;
8926
8927 /*
8928 * Time
8929 */
8930#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 if (8 > (size_t) (end - p)) {
8932 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8933 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008934
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 start = ((uint64_t) p[0] << 56) |
8936 ((uint64_t) p[1] << 48) |
8937 ((uint64_t) p[2] << 40) |
8938 ((uint64_t) p[3] << 32) |
8939 ((uint64_t) p[4] << 24) |
8940 ((uint64_t) p[5] << 16) |
8941 ((uint64_t) p[6] << 8) |
8942 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008943 p += 8;
8944
8945 session->start = (time_t) start;
8946#endif /* MBEDTLS_HAVE_TIME */
8947
8948 /*
8949 * Basic mandatory fields
8950 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8952 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8953 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008954
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008956 p += 2;
8957
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008958 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008960 p += 32;
8961
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008963 p += 48;
8964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 session->verify_result = ((uint32_t) p[0] << 24) |
8966 ((uint32_t) p[1] << 16) |
8967 ((uint32_t) p[2] << 8) |
8968 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008969 p += 4;
8970
8971 /* Immediately clear invalid pointer values that have been read, in case
8972 * we exit early before we replaced them with valid ones. */
8973#if defined(MBEDTLS_X509_CRT_PARSE_C)
8974#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8975 session->peer_cert = NULL;
8976#else
8977 session->peer_cert_digest = NULL;
8978#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8979#endif /* MBEDTLS_X509_CRT_PARSE_C */
8980#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8981 session->ticket = NULL;
8982#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8983
8984 /*
8985 * Peer certificate
8986 */
8987#if defined(MBEDTLS_X509_CRT_PARSE_C)
8988#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8989 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 if (3 > (size_t) (end - p)) {
8991 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8992 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008993
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008995 p += 3;
8996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8999
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 if (cert_len > (size_t) (end - p)) {
9001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9002 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009005
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 if (session->peer_cert == NULL) {
9007 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9008 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009009
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009011
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9013 p, cert_len)) != 0) {
9014 mbedtls_x509_crt_free(session->peer_cert);
9015 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009016 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009018 }
9019
9020 p += cert_len;
9021 }
9022#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9023 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 if (2 > (size_t) (end - p)) {
9025 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9026 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009027
9028 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9029 session->peer_cert_digest_len = (size_t) *p++;
9030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009032 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9034 if (md_info == NULL) {
9035 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9036 }
9037 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9038 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9039 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009040
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9042 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9043 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009044
9045 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 mbedtls_calloc(1, session->peer_cert_digest_len);
9047 if (session->peer_cert_digest == NULL) {
9048 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9049 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009050
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 memcpy(session->peer_cert_digest, p,
9052 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009053 p += session->peer_cert_digest_len;
9054 }
9055#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9056#endif /* MBEDTLS_X509_CRT_PARSE_C */
9057
9058 /*
9059 * Session ticket and associated data
9060 */
9061#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009062 if (3 > (size_t) (end - p)) {
9063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9064 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009067 p += 3;
9068
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 if (session->ticket_len != 0) {
9070 if (session->ticket_len > (size_t) (end - p)) {
9071 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9072 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009073
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 session->ticket = mbedtls_calloc(1, session->ticket_len);
9075 if (session->ticket == NULL) {
9076 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9077 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009078
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009080 p += session->ticket_len;
9081 }
9082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 if (4 > (size_t) (end - p)) {
9084 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9085 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9088 ((uint32_t) p[1] << 16) |
9089 ((uint32_t) p[2] << 8) |
9090 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009091 p += 4;
9092#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9093
9094 /*
9095 * Misc extension-related info
9096 */
9097#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 if (1 > (size_t) (end - p)) {
9099 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9100 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009101
9102 session->mfl_code = *p++;
9103#endif
9104
9105#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 if (1 > (size_t) (end - p)) {
9107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9108 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009109
9110 session->encrypt_then_mac = *p++;
9111#endif
9112
9113 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009114 if (p != end) {
9115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9116 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009117
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009119}
Jerry Yudc7bd172022-02-17 13:44:15 +08009120#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9121
XiaokangQian75d40ef2022-04-20 11:05:24 +00009122int mbedtls_ssl_validate_ciphersuite(
9123 const mbedtls_ssl_context *ssl,
9124 const mbedtls_ssl_ciphersuite_t *suite_info,
9125 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009127{
9128 (void) ssl;
9129
Gilles Peskine449bd832023-01-11 14:50:10 +01009130 if (suite_info == NULL) {
9131 return -1;
9132 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 if ((suite_info->min_tls_version > max_tls_version) ||
9135 (suite_info->max_tls_version < min_tls_version)) {
9136 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009137 }
9138
XiaokangQian060d8672022-04-21 09:24:56 +00009139#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009140#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009141#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9143 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009144#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9146 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009147#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009148 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009150 }
9151#endif
9152
9153 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9154#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9156 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9157 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009158 }
9159#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9160#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009163}
9164
Ronald Crone68ab4f2022-10-05 12:46:29 +02009165#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009166/*
9167 * Function for writing a signature algorithm extension.
9168 *
9169 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9170 * value (TLS 1.3 RFC8446):
9171 * enum {
9172 * ....
9173 * ecdsa_secp256r1_sha256( 0x0403 ),
9174 * ecdsa_secp384r1_sha384( 0x0503 ),
9175 * ecdsa_secp521r1_sha512( 0x0603 ),
9176 * ....
9177 * } SignatureScheme;
9178 *
9179 * struct {
9180 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9181 * } SignatureSchemeList;
9182 *
9183 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9184 * value (TLS 1.2 RFC5246):
9185 * enum {
9186 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9187 * sha512(6), (255)
9188 * } HashAlgorithm;
9189 *
9190 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9191 * SignatureAlgorithm;
9192 *
9193 * struct {
9194 * HashAlgorithm hash;
9195 * SignatureAlgorithm signature;
9196 * } SignatureAndHashAlgorithm;
9197 *
9198 * SignatureAndHashAlgorithm
9199 * supported_signature_algorithms<2..2^16-2>;
9200 *
9201 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9202 * generalization of the TLS 1.2 signature algorithm extension.
9203 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9204 * `SignatureScheme` field of TLS 1.3
9205 *
9206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009207int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9208 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009209{
9210 unsigned char *p = buf;
9211 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9212 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9213
9214 *out_len = 0;
9215
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009217
9218 /* Check if we have space for header and length field:
9219 * - extension_type (2 bytes)
9220 * - extension_data_length (2 bytes)
9221 * - supported_signature_algorithms_length (2 bytes)
9222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009224 p += 6;
9225
9226 /*
9227 * Write supported_signature_algorithms
9228 */
9229 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9231 if (sig_alg == NULL) {
9232 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9233 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9236 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9237 *sig_alg,
9238 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9239 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009240 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 }
9242 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9243 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009244 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9246 *sig_alg,
9247 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009248 }
9249
9250 /* Length of supported_signature_algorithms */
9251 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009252 if (supported_sig_alg_len == 0) {
9253 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9254 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009255 }
9256
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9258 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9259 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009260
XiaokangQianeaf36512022-04-24 09:07:44 +00009261 *out_len = p - buf;
9262
9263#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009265#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009266
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009268}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009269#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009270
XiaokangQian40a35232022-05-07 09:02:40 +00009271#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009272/*
9273 * mbedtls_ssl_parse_server_name_ext
9274 *
9275 * Structure of server_name extension:
9276 *
9277 * enum {
9278 * host_name(0), (255)
9279 * } NameType;
9280 * opaque HostName<1..2^16-1>;
9281 *
9282 * struct {
9283 * NameType name_type;
9284 * select (name_type) {
9285 * case host_name: HostName;
9286 * } name;
9287 * } ServerName;
9288 * struct {
9289 * ServerName server_name_list<1..2^16-1>
9290 * } ServerNameList;
9291 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009293int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9294 const unsigned char *buf,
9295 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009296{
9297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9298 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009299 size_t server_name_list_len, hostname_len;
9300 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009301
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009303
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9305 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009306 p += 2;
9307
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009309 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 while (p < server_name_list_end) {
9311 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9312 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9313 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9314 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009315
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009317 /* sni_name is intended to be used only during the parsing of the
9318 * ClientHello message (it is reset to NULL before the end of
9319 * the message parsing). Thus it is ok to just point to the
9320 * reception buffer and not make a copy of it.
9321 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009322 ssl->handshake->sni_name = p + 3;
9323 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 if (ssl->conf->f_sni == NULL) {
9325 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009326 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009327 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9328 ssl, p + 3, hostname_len);
9329 if (ret != 0) {
9330 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9331 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9332 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9333 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9334 }
9335 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009336 }
9337
9338 p += hostname_len + 3;
9339 }
9340
Gilles Peskine449bd832023-01-11 14:50:10 +01009341 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009342}
9343#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9344
XiaokangQianacb39922022-06-17 10:18:48 +00009345#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009346MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009347int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9348 const unsigned char *buf,
9349 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009350{
9351 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009352 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009353 const unsigned char *protocol_name_list;
9354 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009355 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009356
9357 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009358 if (ssl->conf->alpn_list == NULL) {
9359 return 0;
9360 }
XiaokangQianacb39922022-06-17 10:18:48 +00009361
9362 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009363 * RFC7301, section 3.1
9364 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009365 *
XiaokangQianc7403452022-06-23 03:24:12 +00009366 * struct {
9367 * ProtocolName protocol_name_list<2..2^16-1>
9368 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009369 */
9370
XiaokangQianc7403452022-06-23 03:24:12 +00009371 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009372 * protocol_name_list_len 2 bytes
9373 * protocol_name_len 1 bytes
9374 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009377
Gilles Peskine449bd832023-01-11 14:50:10 +01009378 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009379 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009380 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009381 protocol_name_list = p;
9382 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009383
9384 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009386 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9388 protocol_name_len);
9389 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009390 MBEDTLS_SSL_PEND_FATAL_ALERT(
9391 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009392 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9393 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009394 }
9395
9396 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009397 }
9398
9399 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009400 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9401 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009402 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009404 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 if (protocol_name_len == alpn_len &&
9406 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009407 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009409 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009410
9411 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009412 }
9413 }
9414
XiaokangQian95d5f542022-06-24 02:29:26 +00009415 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009416 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009417 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9418 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9419 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009420}
9421
Gilles Peskine449bd832023-01-11 14:50:10 +01009422int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9423 unsigned char *buf,
9424 unsigned char *end,
9425 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009426{
9427 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009428 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009429 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009430
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 if (ssl->alpn_chosen == NULL) {
9432 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009433 }
9434
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 protocol_name_len = strlen(ssl->alpn_chosen);
9436 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009437
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009439 /*
9440 * 0 . 1 ext identifier
9441 * 2 . 3 ext length
9442 * 4 . 5 protocol list length
9443 * 6 . 6 protocol name length
9444 * 7 . 7+n protocol name
9445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009446 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009447
XiaokangQian95d5f542022-06-24 02:29:26 +00009448 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9451 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009452 /* Note: the length of the chosen protocol has been checked to be less
9453 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009456
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009458
9459#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009460 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009461#endif
9462
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009464}
9465#endif /* MBEDTLS_SSL_ALPN */
9466
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009467#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009468 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009469 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009470 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009471int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9472 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009473{
9474 /* Initialize to suppress unnecessary compiler warning */
9475 size_t hostname_len = 0;
9476
9477 /* Check if new hostname is valid before
9478 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 if (hostname != NULL) {
9480 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009481
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9483 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9484 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009485 }
9486
9487 /* Now it's clear that we will overwrite the old hostname,
9488 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009489 if (session->hostname != NULL) {
9490 mbedtls_platform_zeroize(session->hostname,
9491 strlen(session->hostname));
9492 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009493 }
9494
9495 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009497 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 } else {
9499 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9500 if (session->hostname == NULL) {
9501 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9502 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009503
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009505 }
9506
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009508}
Xiaokang Qian03409292022-10-12 02:49:52 +00009509#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9510 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009511 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009512 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#endif /* MBEDTLS_SSL_TLS_C */