blob: 7601e5b117f3acbc022284e4396fca162f0b0c22 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
SimonBd5800b72016-04-26 07:43:27 +010028#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010031#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010032#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000033#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040034
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/debug.h"
36#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050037#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010038#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020039#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050043#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020044#include "md_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#include "mbedtls/psa_util.h"
46#include "psa/crypto.h"
47#endif
48
Janos Follath23bdca02016-10-07 14:47:14 +010049#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020051#endif
52
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040054/* Define local translating functions to save code size by not using too many
55 * arguments in each translating place. */
56static int local_err_translation(psa_status_t status)
57{
58 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
59 sizeof(psa_to_ssl_errors),
60 psa_generic_status_to_mbedtls);
61}
62#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
63
64static int local_md_translation(psa_status_t status)
65{
66 return psa_status_to_mbedtls(status, psa_to_md_errors,
67 sizeof(psa_to_md_errors),
68 psa_generic_status_to_mbedtls);
69}
70#define PSA_TO_MD_ERR(status) local_md_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050071#endif
72
Ronald Cronad8c17b2022-06-10 17:18:09 +020073#if defined(MBEDTLS_TEST_HOOKS)
74static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
75
76void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010077 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020078{
79 chk_buf_ptr_fail_args.cur = cur;
80 chk_buf_ptr_fail_args.end = end;
81 chk_buf_ptr_fail_args.need = need;
82}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020085{
Gilles Peskine449bd832023-01-11 14:50:10 +010086 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020087}
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020090{
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return (chk_buf_ptr_fail_args.cur != args->cur) ||
92 (chk_buf_ptr_fail_args.end != args->end) ||
93 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020094}
95#endif /* MBEDTLS_TEST_HOOKS */
96
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010098
Hanno Beckera0e20d02019-05-15 14:03:01 +010099#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100100/* Top-level Connection ID API */
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
103 size_t len,
104 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +0100105{
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
108 }
Hanno Beckerad4a1372019-05-03 13:06:44 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
111 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
112 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100113 }
114
115 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100116 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100118}
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
121 int enable,
122 unsigned char const *own_cid,
123 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100124{
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
126 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
127 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100128
Hanno Beckerca092242019-04-25 16:01:49 +0100129 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 if (enable == MBEDTLS_SSL_CID_DISABLED) {
131 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
132 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100133 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
135 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (own_cid_len != ssl->conf->cid_len) {
138 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
139 (unsigned) own_cid_len,
140 (unsigned) ssl->conf->cid_len));
141 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100142 }
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100145 /* Truncation is not an issue here because
146 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
147 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100150}
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
153 int *enabled,
154 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
155 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000156{
157 *enabled = MBEDTLS_SSL_CID_DISABLED;
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
160 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
161 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000162
163 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
164 * zero as this is indistinguishable from not requesting to use
165 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
167 return 0;
168 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000171 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (own_cid != NULL) {
173 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
174 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000175 }
176
177 *enabled = MBEDTLS_SSL_CID_ENABLED;
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000180}
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
183 int *enabled,
184 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
185 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100186{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100187 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
190 mbedtls_ssl_is_handshake_over(ssl) == 0) {
191 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100192 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100193
Hanno Beckerc5f24222019-05-03 12:54:52 +0100194 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
195 * were used, but client and server requested the empty CID.
196 * This is indistinguishable from not using the CID extension
197 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (ssl->transform_in->in_cid_len == 0 &&
199 ssl->transform_in->out_cid_len == 0) {
200 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100201 }
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100204 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (peer_cid != NULL) {
206 memcpy(peer_cid, ssl->transform_in->out_cid,
207 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100208 }
209 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100210
211 *enabled = MBEDTLS_SSL_CID_ENABLED;
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100214}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100215#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200220/*
221 * Convert max_fragment_length codes to length.
222 * RFC 6066 says:
223 * enum{
224 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
225 * } MaxFragmentLength;
226 * and we add 0 -> extension unused
227 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200229{
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 switch (mfl) {
231 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
232 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
234 return 512;
235 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
236 return 1024;
237 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
238 return 2048;
239 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
240 return 4096;
241 default:
242 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000243 }
244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
248 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249{
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_ssl_session_free(dst);
251 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800252#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
253 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000254#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
255 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000256 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800257#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000258#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000261
262#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
267 if (dst->peer_cert == NULL) {
268 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
269 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
274 src->peer_cert->raw.len)) != 0) {
275 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200276 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278 }
279 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000280#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000282 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 mbedtls_calloc(1, src->peer_cert_digest_len);
284 if (dst->peer_cert_digest == NULL) {
285 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
286 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
289 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000290 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000291 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000292 }
293#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200297#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (src->ticket != NULL) {
299 dst->ticket = mbedtls_calloc(1, src->ticket_len);
300 if (dst->ticket == NULL) {
301 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
302 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000306
307#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
308 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
312 if (ret != 0) {
313 return ret;
314 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000315 }
316#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
317 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200318#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200321}
322
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500323#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200324MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100325static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500326{
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
328 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500329 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500331
332 /* We want to copy len_new bytes when downsizing the buffer, and
333 * len_old bytes when upsizing, so we choose the smaller of two sizes,
334 * to fit one buffer into another. Size checks, ensuring that no data is
335 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 memcpy(resized_buffer, *buffer,
337 (len_new < *len_old) ? len_new : *len_old);
338 mbedtls_platform_zeroize(*buffer, *len_old);
339 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500340
341 *buffer = resized_buffer;
342 *len_old = len_new;
343
344 return 0;
345}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
348 size_t in_buf_new_len,
349 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350{
351 int modified = 0;
352 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
353 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200355 written_in = ssl->in_msg - ssl->in_buf;
356 iv_offset_in = ssl->in_iv - ssl->in_buf;
357 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200359 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 ssl->in_buf_len < in_buf_new_len) {
361 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
362 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
363 } else {
364 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
365 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200366 modified = 1;
367 }
368 }
369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 written_out = ssl->out_msg - ssl->out_buf;
373 iv_offset_out = ssl->out_iv - ssl->out_buf;
374 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200376 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 ssl->out_buf_len < out_buf_new_len) {
378 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
379 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
380 } else {
381 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
382 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200383 modified = 1;
384 }
385 }
386 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200388 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200390 /* Fields below might not be properly updated with record
391 * splitting or with CID, so they are manually updated here. */
392 ssl->out_msg = ssl->out_buf + written_out;
393 ssl->out_len = ssl->out_buf + len_offset_out;
394 ssl->out_iv = ssl->out_buf + iv_offset_out;
395
396 ssl->in_msg = ssl->in_buf + written_in;
397 ssl->in_len = ssl->in_buf + len_offset_in;
398 ssl->in_iv = ssl->in_buf + iv_offset_in;
399 }
400}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500401#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
402
Jerry Yudb8c48a2022-01-27 14:54:54 +0800403#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800404
405#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100406typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
407 const char *label,
408 const unsigned char *random, size_t rlen,
409 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800412
413#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
414
415/* Type for the TLS PRF */
416typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
417 const unsigned char *, size_t,
418 unsigned char *, size_t);
419
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
422 int ciphersuite,
423 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200424#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200426#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 ssl_tls_prf_t tls_prf,
428 const unsigned char randbytes[64],
429 mbedtls_ssl_protocol_version tls_version,
430 unsigned endpoint,
431 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800432
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100433#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200434MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300436 const char *label,
437 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100439static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
440static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100441
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100442#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100443
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100444#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100445MBEDTLS_CHECK_RETURN_CRITICAL
446static int tls_prf_sha384(const unsigned char *secret, size_t slen,
447 const char *label,
448 const unsigned char *random, size_t rlen,
449 unsigned char *dstbuf, size_t dlen);
450
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100451static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
452static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100453#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100454
455static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
456 unsigned char *buf,
457 size_t buf_len);
458
459MBEDTLS_CHECK_RETURN_CRITICAL
460static int ssl_tls12_session_load(mbedtls_ssl_session *session,
461 const unsigned char *buf,
462 size_t len);
463#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
464
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100465static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100466
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100467#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100468static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100469#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100470
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100471#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100472static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100473#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100474
475int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
476 const unsigned char *secret, size_t slen,
477 const char *label,
478 const unsigned char *random, size_t rlen,
479 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480{
481 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300484#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100485#if defined(MBEDTLS_MD_CAN_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300486 case MBEDTLS_SSL_TLS_PRF_SHA384:
487 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100489#endif /* MBEDTLS_MD_CAN_SHA384*/
490#if defined(MBEDTLS_MD_CAN_SHA256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300491 case MBEDTLS_SSL_TLS_PRF_SHA256:
492 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100494#endif /* MBEDTLS_MD_CAN_SHA256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300495#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 default:
497 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300498 }
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300501}
502
Jerry Yuc73c6182022-02-08 20:29:25 +0800503#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100504static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800505{
506#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (session->peer_cert != NULL) {
508 mbedtls_x509_crt_free(session->peer_cert);
509 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800510 session->peer_cert = NULL;
511 }
512#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800514 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800525{
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800527 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800592
593 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800595
596 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800598
599 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800601
602 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800604
Jan Bruckner151f6422023-02-10 12:45:19 +0100605 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
606 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
607
Jerry Yu7a485c12022-10-31 13:08:18 +0800608 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800610
611 }
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800614}
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800617{
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800619}
620
Jerry Yud25cab02022-10-31 12:48:30 +0800621#if defined(MBEDTLS_DEBUG_C)
622static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800623 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800624 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
625 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
626 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
627 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
628 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
629 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
630 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
631 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
632 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
633 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
634 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
635 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
636 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
637 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
638 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
639 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
640 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
641 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
642 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
643 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
644 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
645 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
646 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
647 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
648 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
649 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100650 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
651 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800652};
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800655 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
656 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
657 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
658 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
659 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
660 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
661 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
662 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
663 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
664 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
665 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
666 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
667 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
668 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
669 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
670 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
671 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
672 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
673 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
674 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
675 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
676 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
677 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
678 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
679 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
680 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
681 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100682 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
683 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800684};
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800687{
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return extension_name_table[
689 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800690}
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800693{
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800695 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800697 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800699 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800701 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800703 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800705 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800707 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800709 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800711}
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
714 int level, const char *file, int line,
715 int hs_msg_type, unsigned int extension_type,
716 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800717{
718 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800720 mbedtls_debug_print_msg(
721 ssl, level, file, line,
722 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 ssl_tls13_get_hs_msg_name(hs_msg_type),
724 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800725 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800727 return;
728 }
729
730 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800732 mbedtls_debug_print_msg(
733 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
735 mbedtls_ssl_get_extension_name(extension_type), extension_type,
736 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800737 return;
738 }
739
740 mbedtls_debug_print_msg(
741 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
743 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800744}
745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
747 int level, const char *file, int line,
748 int hs_msg_type, uint32_t extensions_mask,
749 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800750{
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 for (unsigned i = 0;
753 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
754 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
Pengyu Lvee455c02023-01-12 14:37:24 +0800761#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
762#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
763
764static const char *ticket_flag_name_table[] =
765{
766 [0] = "ALLOW_PSK_RESUMPTION",
767 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
768 [3] = "ALLOW_EARLY_DATA",
769};
770
Pengyu Lv4938a562023-01-16 11:28:49 +0800771void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
772 int level, const char *file, int line,
773 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800774{
775 size_t i;
776
777 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800778 "print ticket_flags (0x%02x)", flags);
779
780 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800781
782 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800783 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800784 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
785 ticket_flag_name_table[i]);
786 }
787 }
788}
789#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
790
Jerry Yud25cab02022-10-31 12:48:30 +0800791#endif /* MBEDTLS_DEBUG_C */
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
794 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800795{
796 ((void) ciphersuite_info);
797
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100798#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800800 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800802#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100803#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800805 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800807#endif
808 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800810 return;
811 }
812}
813
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100815 unsigned hs_type,
816 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817{
818 unsigned char hs_hdr[4];
819
820 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
822 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
823 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
824 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100825
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100826 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100827}
828
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100829int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100830 unsigned hs_type,
831 unsigned char const *msg,
832 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100833{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100834 int ret;
835 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100836 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100837 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100838 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100839 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100840}
841
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100842int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800843{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100844#if defined(MBEDTLS_MD_CAN_SHA256) || \
845 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100846#if defined(MBEDTLS_USE_PSA_CRYPTO)
847 psa_status_t status;
848#else
849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100851#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800852 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100853#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100854#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100856 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
857 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200858 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100859 }
860 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
861 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200862 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100863 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800864#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100865 mbedtls_md_free(&ssl->handshake->fin_sha256);
866 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100867 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
868 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
869 0);
870 if (ret != 0) {
871 return ret;
872 }
873 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100874 if (ret != 0) {
875 return ret;
876 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800877#endif
878#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100879#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800880#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100881 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
882 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200883 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100884 }
885 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
886 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200887 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100888 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800889#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100890 mbedtls_md_free(&ssl->handshake->fin_sha384);
891 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100892 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
893 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
894 if (ret != 0) {
895 return ret;
896 }
897 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100898 if (ret != 0) {
899 return ret;
900 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800901#endif
902#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100903 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800904}
905
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100906static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100907 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800908{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100909#if defined(MBEDTLS_MD_CAN_SHA256) || \
910 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100911#if defined(MBEDTLS_USE_PSA_CRYPTO)
912 psa_status_t status;
913#else
914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
915#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100916#else /* SHA-256 or SHA-384 */
917 ((void) ssl);
918 (void) buf;
919 (void) len;
920#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100921#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800922#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100923 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
924 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200925 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100926 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800927#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100928 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100929 if (ret != 0) {
930 return ret;
931 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800932#endif
933#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100934#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800935#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100936 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
937 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200938 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100939 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800940#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100941 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100942 if (ret != 0) {
943 return ret;
944 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#endif
946#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100947 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800948}
949
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100950#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100951static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100952 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800953{
954#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200955 return mbedtls_md_error_from_psa(psa_hash_update(
956 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800957#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100958 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800959#endif
960}
961#endif
962
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100963#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100964static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100965 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800966{
967#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200968 return mbedtls_md_error_from_psa(psa_hash_update(
969 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800970#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100971 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800972#endif
973}
974#endif
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200977{
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100980#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500981#if defined(MBEDTLS_USE_PSA_CRYPTO)
982 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500983#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100984 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500986#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100987#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500988#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500989 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500990#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100991 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200992#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500993#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200994
995 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200999#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02001000#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001002#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001003#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02001004#if defined(MBEDTLS_USE_PSA_CRYPTO)
1005 handshake->psa_pake_ctx = psa_pake_operation_init();
1006 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1007#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001009#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001010#if defined(MBEDTLS_SSL_CLI_C)
1011 handshake->ecjpake_cache = NULL;
1012 handshake->ecjpake_cache_len = 0;
1013#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001014#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001015
Gilles Peskineeccd8882020-03-10 12:19:08 +01001016#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001018#endif
1019
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001020#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1021 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1022#endif
Hanno Becker75173122019-02-06 16:18:31 +00001023
1024#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1025 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001027#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001028}
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001031{
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001033
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001034#if defined(MBEDTLS_USE_PSA_CRYPTO)
1035 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1036 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001037#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1039 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001040#endif
1041
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001042#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001043#if defined(MBEDTLS_USE_PSA_CRYPTO)
1044 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1045 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001046#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_md_init(&transform->md_ctx_enc);
1048 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001049#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001050#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001051}
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001054{
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001056}
1057
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001058MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001059static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001060{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1062
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001063 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (ssl->transform_negotiate) {
1066 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1067 }
Jerry Yu2e199812022-12-01 18:57:19 +08001068#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (ssl->session_negotiate) {
1070 mbedtls_ssl_session_free(ssl->session_negotiate);
1071 }
1072 if (ssl->handshake) {
1073 mbedtls_ssl_handshake_free(ssl);
1074 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001075
Jerry Yu2e199812022-12-01 18:57:19 +08001076#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001077 /*
1078 * Either the pointers are now NULL or cleared properly and can be freed.
1079 * Now allocate missing structures.
1080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (ssl->transform_negotiate == NULL) {
1082 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001083 }
Jerry Yu2e199812022-12-01 18:57:19 +08001084#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (ssl->session_negotiate == NULL) {
1087 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001088 }
Paul Bakker48916f92012-09-16 19:57:18 +00001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (ssl->handshake == NULL) {
1091 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001092 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001093#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1094 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1097 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001098#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001099
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001100 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001103 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001104#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 ssl->session_negotiate == NULL) {
1106 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001109 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001110
1111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001113 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001114#endif
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001117 ssl->session_negotiate = NULL;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001120 }
1121
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001122 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 mbedtls_ssl_session_init(ssl->session_negotiate);
1124 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001125
Jerry Yu2e199812022-12-01 18:57:19 +08001126#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001128#endif
1129
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001130 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001131 ret = mbedtls_ssl_reset_checksum(ssl);
1132 if (ret != 0) {
1133 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1134 return ret;
1135 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001136
Jerry Yud0766ec2022-09-22 10:46:57 +08001137#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1138 defined(MBEDTLS_SSL_SRV_C) && \
1139 defined(MBEDTLS_SSL_SESSION_TICKETS)
1140 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001142#endif
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001146 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001149 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001151 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001155 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001156#endif
1157
Brett Warrene0edc842021-08-17 09:53:13 +01001158/*
1159 * curve_list is translated to IANA TLS group identifiers here because
1160 * mbedtls_ssl_conf_curves returns void and so can't return
1161 * any error codes.
1162 */
Valerio Setti6c496a12023-04-07 15:53:51 +02001163#if defined(MBEDTLS_ECP_LIGHT)
Brett Warrene0edc842021-08-17 09:53:13 +01001164#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1165 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001167 size_t length;
1168 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1169
Thomas Daubney850a0792023-05-22 12:05:03 +01001170 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
Brett Warrene0edc842021-08-17 09:53:13 +01001172
1173 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1175 if (group_list == NULL) {
1176 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1177 }
Brett Warrene0edc842021-08-17 09:53:13 +01001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001180 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 curve_list[i]);
1182 if (tls_id == 0) {
1183 mbedtls_free(group_list);
1184 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001185 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001186 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001187 }
1188
1189 group_list[length] = 0;
1190
1191 ssl->handshake->group_list = group_list;
1192 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001194 ssl->handshake->group_list = ssl->conf->group_list;
1195 ssl->handshake->group_list_heap_allocated = 0;
1196 }
1197#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6c496a12023-04-07 15:53:51 +02001198#endif /* MBEDTLS_ECP_LIGHT */
Brett Warrene0edc842021-08-17 09:53:13 +01001199
Ronald Crone68ab4f2022-10-05 12:46:29 +02001200#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001201#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001202#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001203 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1204 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1206 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001207 const int *md;
1208 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001209 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001210 uint16_t *p;
1211
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001212 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1213 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1214 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1217 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001218 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 }
Valerio Setti75fba322023-02-23 17:35:09 +01001220#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001222#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001223
Jerry Yub476a442022-01-21 18:14:45 +08001224#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001226#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1228 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1229 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001230 }
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1233 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1234 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1237 sizeof(uint16_t));
1238 if (ssl->handshake->sig_algs == NULL) {
1239 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1240 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 p = (uint16_t *) ssl->handshake->sig_algs;
1243 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1244 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1245 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001246 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 }
Valerio Setti75fba322023-02-23 17:35:09 +01001248#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001250 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001251#endif
1252#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001254 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001255#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001256 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001257 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001258 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001260#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001261 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001262 ssl->handshake->sig_algs_heap_allocated = 0;
1263 }
Jerry Yucc539102022-06-27 16:27:35 +08001264#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001265#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001267}
1268
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001269#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001270/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001271MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001272static int ssl_cookie_write_dummy(void *ctx,
1273 unsigned char **p, unsigned char *end,
1274 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001275{
1276 ((void) ctx);
1277 ((void) p);
1278 ((void) end);
1279 ((void) cli_id);
1280 ((void) cli_id_len);
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001283}
1284
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001285MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001286static int ssl_cookie_check_dummy(void *ctx,
1287 const unsigned char *cookie, size_t cookie_len,
1288 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001289{
1290 ((void) ctx);
1291 ((void) cookie);
1292 ((void) cookie_len);
1293 ((void) cli_id);
1294 ((void) cli_id_len);
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001297}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001298#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001299
Paul Bakker5121ce52009-01-03 21:22:43 +00001300/*
1301 * Initialize an SSL context
1302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001304{
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001306}
1307
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001308MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001309static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001310{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001311 const mbedtls_ssl_config *conf = ssl->conf;
1312
Ronald Cron6f135e12021-12-08 16:57:54 +01001313#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1315 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1316 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1317 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001318 }
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1321 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001322 }
1323#endif
1324
1325#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1327 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1328 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001329 }
1330#endif
1331
Ronald Cron6f135e12021-12-08 16:57:54 +01001332#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1334 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1335 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1336 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001337 }
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1340 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001341 }
1342#endif
1343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1345 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001346}
1347
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001348MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001349static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1350{
1351 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 ret = ssl_conf_version_check(ssl);
1353 if (ret != 0) {
1354 return ret;
1355 }
Jerry Yu60835a82021-08-04 10:13:52 +08001356
Jerry Yudef7ae42022-10-30 14:13:19 +08001357#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1358 /* RFC 8446 section 4.4.3
1359 *
1360 * If the verification fails, the receiver MUST terminate the handshake with
1361 * a "decrypt_error" alert.
1362 *
1363 * If the client is configured as TLS 1.3 only with optional verify, return
1364 * bad config.
1365 *
1366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1368 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001369 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1370 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1371 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001373 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 1, ("Optional verify auth mode "
1375 "is not available for TLS 1.3 client"));
1376 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001377 }
1378#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1379
Jerry Yu60835a82021-08-04 10:13:52 +08001380 /* Space for further checks */
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001383}
1384
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001385/*
1386 * Setup an SSL context
1387 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1390 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001391{
Janos Follath865b3eb2019-12-16 11:46:15 +00001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001393 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1394 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001396 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if ((ret = ssl_conf_check(ssl)) != 0) {
1399 return ret;
1400 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001401 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001402
Paul Bakker62f2dee2012-09-28 07:31:51 +00001403 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001404 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001405 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001406
1407 /* Set to NULL in case of an error condition */
1408 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001409
Darryl Greenb33cc762019-11-28 14:29:44 +00001410#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1411 ssl->in_buf_len = in_buf_len;
1412#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1414 if (ssl->in_buf == NULL) {
1415 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001416 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001417 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001418 }
1419
Darryl Greenb33cc762019-11-28 14:29:44 +00001420#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1421 ssl->out_buf_len = out_buf_len;
1422#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1424 if (ssl->out_buf == NULL) {
1425 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001426 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001427 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 }
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001431
Johan Pascalb62bb512015-12-03 21:56:45 +01001432#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001434#endif
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001437 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001441
1442error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 mbedtls_free(ssl->in_buf);
1444 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001445
1446 ssl->conf = NULL;
1447
Darryl Greenb33cc762019-11-28 14:29:44 +00001448#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1449 ssl->in_buf_len = 0;
1450 ssl->out_buf_len = 0;
1451#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001452 ssl->in_buf = NULL;
1453 ssl->out_buf = NULL;
1454
1455 ssl->in_hdr = NULL;
1456 ssl->in_ctr = NULL;
1457 ssl->in_len = NULL;
1458 ssl->in_iv = NULL;
1459 ssl->in_msg = NULL;
1460
1461 ssl->out_hdr = NULL;
1462 ssl->out_ctr = NULL;
1463 ssl->out_len = NULL;
1464 ssl->out_iv = NULL;
1465 ssl->out_msg = NULL;
1466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001468}
1469
1470/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001471 * Reset an initialized and used SSL context for re-use while retaining
1472 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001473 *
1474 * If partial is non-zero, keep data in the input buffer and client ID.
1475 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001476 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1478 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001479{
Darryl Greenb33cc762019-11-28 14:29:44 +00001480#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1481 size_t in_buf_len = ssl->in_buf_len;
1482 size_t out_buf_len = ssl->out_buf_len;
1483#else
1484 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1485 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1486#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001487
Hanno Beckerb0302c42021-08-03 09:39:42 +01001488#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1489 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001490#endif
1491
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001492 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001496
1497 /* Reset incoming message parsing */
1498 ssl->in_offt = NULL;
1499 ssl->nb_zero = 0;
1500 ssl->in_msgtype = 0;
1501 ssl->in_msglen = 0;
1502 ssl->in_hslen = 0;
1503 ssl->keep_current_message = 0;
1504 ssl->transform_in = NULL;
1505
1506#if defined(MBEDTLS_SSL_PROTO_DTLS)
1507 ssl->next_record_offset = 0;
1508 ssl->in_epoch = 0;
1509#endif
1510
1511 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001513 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001515 }
1516
Ronald Cronad8c17b2022-06-10 17:18:09 +02001517 ssl->send_alert = 0;
1518
Hanno Beckerb0302c42021-08-03 09:39:42 +01001519 /* Reset outgoing message writing */
1520 ssl->out_msgtype = 0;
1521 ssl->out_msglen = 0;
1522 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 memset(ssl->out_buf, 0, out_buf_len);
1524 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001525 ssl->transform_out = NULL;
1526
1527#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001529#endif
1530
XiaokangQian2b01dc32022-01-21 02:53:13 +00001531#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if (ssl->transform) {
1533 mbedtls_ssl_transform_free(ssl->transform);
1534 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001535 ssl->transform = NULL;
1536 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001537#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1538
XiaokangQian2b01dc32022-01-21 02:53:13 +00001539#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 mbedtls_ssl_transform_free(ssl->transform_application);
1541 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001542 ssl->transform_application = NULL;
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001545#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1547 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001548 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001549#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1552 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001553 ssl->handshake->transform_handshake = NULL;
1554 }
1555
XiaokangQian2b01dc32022-01-21 02:53:13 +00001556#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001557}
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001560{
1561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1562
1563 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001566
1567 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_SSL_RENEGOTIATION)
1569 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001570 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001571
1572 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1574 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001575#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001577
Hanno Beckerb0302c42021-08-03 09:39:42 +01001578 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001579 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (ssl->session) {
1581 mbedtls_ssl_session_free(ssl->session);
1582 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001583 ssl->session = NULL;
1584 }
1585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001587 ssl->alpn_chosen = NULL;
1588#endif
1589
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001590#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001591 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001592#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001594#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 if (free_cli_id) {
1596 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001597 ssl->cli_id = NULL;
1598 ssl->cli_id_len = 0;
1599 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001600#endif
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if ((ret = ssl_handshake_init(ssl)) != 0) {
1603 return ret;
1604 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001607}
1608
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001609/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001610 * Reset an initialized and used SSL context for re-use while retaining
1611 * all application-set variables, function pointers and data.
1612 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001614{
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001616}
1617
1618/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 * SSL set accessors
1620 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001623 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001624}
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001628 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001629}
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001634 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001635}
1636#endif
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001640 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001641}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1646 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001647{
1648 ssl->disable_datagram_packing = !allow_packing;
1649}
1650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1652 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001653{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001654 conf->hs_timeout_min = min;
1655 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001656}
1657#endif
1658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001660{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001661 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662}
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001665void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1666 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1667 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001669 conf->f_vrfy = f_vrfy;
1670 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1675 int (*f_rng)(void *, unsigned char *, size_t),
1676 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001677{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001678 conf->f_rng = f_rng;
1679 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001680}
1681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1683 void (*f_dbg)(void *, int, const char *, int, const char *),
1684 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001686 conf->f_dbg = f_dbg;
1687 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001688}
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1691 void *p_bio,
1692 mbedtls_ssl_send_t *f_send,
1693 mbedtls_ssl_recv_t *f_recv,
1694 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001695{
1696 ssl->p_bio = p_bio;
1697 ssl->f_send = f_send;
1698 ssl->f_recv = f_recv;
1699 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001700}
1701
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001703void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001704{
1705 ssl->mtu = mtu;
1706}
1707#endif
1708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001710{
1711 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001712}
1713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1715 void *p_timer,
1716 mbedtls_ssl_set_timer_t *f_set_timer,
1717 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001718{
1719 ssl->p_timer = p_timer;
1720 ssl->f_set_timer = f_set_timer;
1721 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001722
1723 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001725}
1726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001728void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1729 void *p_cache,
1730 mbedtls_ssl_cache_get_t *f_get_cache,
1731 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001732{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001733 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001734 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001735 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001740int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001741{
Janos Follath865b3eb2019-12-16 11:46:15 +00001742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001745 session == NULL ||
1746 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1748 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001749 }
1750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (ssl->handshake->resume == 1) {
1752 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1753 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001754
Jerry Yu21092062022-10-10 21:21:31 +08001755#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001757 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001761 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1763 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1764 session->ciphersuite));
1765 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001766 }
Jerry Yu40afab62022-10-08 10:42:13 +08001767 }
Jerry Yu21092062022-10-10 21:21:31 +08001768#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1771 session)) != 0) {
1772 return ret;
1773 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001774
Paul Bakker0a597072012-09-25 21:55:46 +00001775 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1782 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001783{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001784 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001785}
1786
Ronald Cron6f135e12021-12-08 16:57:54 +01001787#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001788void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1789 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001790{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001791 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001792}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001793
1794#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001795void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1796 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001797{
1798 conf->early_data_enabled = early_data_enabled;
1799}
Jerry Yucc4e0072022-11-22 17:22:22 +08001800
1801#if defined(MBEDTLS_SSL_SRV_C)
1802void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001804{
Jerry Yu39da9852022-12-06 16:58:36 +08001805 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001806}
1807#endif /* MBEDTLS_SSL_SRV_C */
1808
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001809#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001810#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001813void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1814 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001815{
1816 conf->cert_profile = profile;
1817}
1818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001820{
1821 mbedtls_ssl_key_cert *cur = key_cert, *next;
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001824 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001826 cur = next;
1827 }
1828}
1829
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001830/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001831MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001832static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1833 mbedtls_x509_crt *cert,
1834 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001835{
niisato8ee24222018-06-25 19:05:48 +09001836 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001839 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001841 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001843 }
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1846 if (new_cert == NULL) {
1847 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1848 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001849
niisato8ee24222018-06-25 19:05:48 +09001850 new_cert->cert = cert;
1851 new_cert->key = key;
1852 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001853
Glenn Strauss36872db2022-01-22 05:06:31 -05001854 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001856 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001858 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001860 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 }
niisato8ee24222018-06-25 19:05:48 +09001862 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001863 }
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001866}
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001869 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001871{
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001873}
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001876 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001878{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001879 conf->ca_chain = ca_chain;
1880 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001881
1882#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1883 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1884 * cannot be used together. */
1885 conf->f_ca_cb = NULL;
1886 conf->p_ca_cb = NULL;
1887#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001888}
Hanno Becker5adaad92019-03-27 16:54:37 +00001889
1890#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001891void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1892 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1893 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001894{
1895 conf->f_ca_cb = f_ca_cb;
1896 conf->p_ca_cb = p_ca_cb;
1897
1898 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1899 * cannot be used together. */
1900 conf->ca_chain = NULL;
1901 conf->ca_crl = NULL;
1902}
1903#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001905
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001906#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001907const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1908 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001909{
1910 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001912}
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1915 mbedtls_x509_crt *own_cert,
1916 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001917{
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1919 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001920}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1923 mbedtls_x509_crt *ca_chain,
1924 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001925{
1926 ssl->handshake->sni_ca_chain = ca_chain;
1927 ssl->handshake->sni_ca_crl = ca_crl;
1928}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001929
Glenn Strauss999ef702022-03-11 01:37:23 -05001930#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001931void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1932 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001933{
1934 ssl->handshake->dn_hints = crt;
1935}
1936#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1939 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001940{
1941 ssl->handshake->sni_authmode = authmode;
1942}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001943#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1944
Hanno Becker8927c832019-04-03 12:52:50 +01001945#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001946void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1947 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1948 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001949{
1950 ssl->f_vrfy = f_vrfy;
1951 ssl->p_vrfy = p_vrfy;
1952}
1953#endif
1954
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001955#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001956
Valerio Setti016f6822022-12-09 14:17:50 +01001957#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001958static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1959static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1960
Valerio Setti016f6822022-12-09 14:17:50 +01001961static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 mbedtls_ssl_context *ssl,
1963 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001964{
1965 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001966 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001967 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001968 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001969 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001970 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1972 psa_pake_cs_set_primitive(&cipher_suite,
1973 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1974 PSA_ECC_FAMILY_SECP_R1,
1975 256));
1976 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1979 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001980 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001984 user = jpake_server_id;
1985 user_len = sizeof(jpake_server_id);
1986 peer = jpake_client_id;
1987 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001989 user = jpake_client_id;
1990 user_len = sizeof(jpake_client_id);
1991 peer = jpake_server_id;
1992 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001994
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001995 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1996 if (status != PSA_SUCCESS) {
1997 return status;
1998 }
1999
2000 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002002 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 }
Valerio Setti016f6822022-12-09 14:17:50 +01002004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2006 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002007 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 }
Valerio Setti016f6822022-12-09 14:17:50 +01002009
2010 ssl->handshake->psa_pake_ctx_is_ok = 1;
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002013}
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2016 const unsigned char *pw,
2017 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002018{
2019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2020 psa_status_t status;
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (ssl->handshake == NULL || ssl->conf == NULL) {
2023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002024 }
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 /* Empty password is not valid */
2027 if ((pw == NULL) || (pw_len == 0)) {
2028 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2029 }
2030
2031 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2032 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2033 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2034
2035 status = psa_import_key(&attributes, pw, pw_len,
2036 &ssl->handshake->psa_pake_password);
2037 if (status != PSA_SUCCESS) {
2038 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2039 }
2040
2041 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2042 ssl->handshake->psa_pake_password);
2043 if (status != PSA_SUCCESS) {
2044 psa_destroy_key(ssl->handshake->psa_pake_password);
2045 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2046 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2047 }
2048
2049 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002050}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2053 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002054{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002055 psa_status_t status;
2056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (ssl->handshake == NULL || ssl->conf == NULL) {
2058 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002059 }
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (mbedtls_svc_key_id_is_null(pwd)) {
2062 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2063 }
2064
2065 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2066 if (status != PSA_SUCCESS) {
2067 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2068 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2069 }
2070
2071 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002072}
2073#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002074int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2075 const unsigned char *pw,
2076 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002077{
2078 mbedtls_ecjpake_role role;
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (ssl->handshake == NULL || ssl->conf == NULL) {
2081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2082 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002083
Valerio Settic689ed82022-12-07 14:40:38 +01002084 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if ((pw == NULL) || (pw_len == 0)) {
2086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2087 }
Valerio Settic689ed82022-12-07 14:40:38 +01002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002090 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002092 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2096 role,
2097 MBEDTLS_MD_SHA256,
2098 MBEDTLS_ECP_DP_SECP256R1,
2099 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002100}
Valerio Setti02c25b52022-11-15 14:08:42 +01002101#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002102#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002103
Ronald Cron73fe8df2022-10-05 14:31:43 +02002104#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002105int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002106{
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (conf->psk_identity == NULL ||
2108 conf->psk_identity_len == 0) {
2109 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002110 }
2111
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002112#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2114 return 1;
2115 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002116#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (conf->psk != NULL && conf->psk_len != 0) {
2119 return 1;
2120 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002123}
2124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002126{
2127 /* Remove reference to existing PSK, if any. */
2128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002130 /* The maintenance of the PSK key slot is the
2131 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002132 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002133 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002134#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (conf->psk != NULL) {
2136 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002139 conf->psk = NULL;
2140 conf->psk_len = 0;
2141 }
2142
2143 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (conf->psk_identity != NULL) {
2145 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002146 conf->psk_identity = NULL;
2147 conf->psk_identity_len = 0;
2148 }
2149}
2150
Hanno Becker7390c712018-11-15 13:33:04 +00002151/* This function assumes that PSK identity in the SSL config is unset.
2152 * It checks that the provided identity is well-formed and attempts
2153 * to make a copy of it in the SSL config.
2154 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002155MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002156static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2157 unsigned char const *psk_identity,
2158 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002159{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002160 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002162 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 (psk_identity_len >> 16) != 0 ||
2164 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2165 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002166 }
2167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2169 if (conf->psk_identity == NULL) {
2170 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2171 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002172
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002173 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002177}
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2180 const unsigned char *psk, size_t psk_len,
2181 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002182{
Janos Follath865b3eb2019-12-16 11:46:15 +00002183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002184
2185 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2187 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2188 }
Hanno Becker7390c712018-11-15 13:33:04 +00002189
2190 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (psk == NULL) {
2192 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2193 }
2194 if (psk_len == 0) {
2195 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2196 }
2197 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2198 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2199 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2202 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2203 }
Hanno Becker7390c712018-11-15 13:33:04 +00002204 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002206
2207 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2209 if (ret != 0) {
2210 ssl_conf_remove_psk(conf);
2211 }
Hanno Becker7390c712018-11-15 13:33:04 +00002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002214}
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002217{
2218#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002220 /* The maintenance of the external PSK key slot is the
2221 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (ssl->handshake->psk_opaque_is_internal) {
2223 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002224 ssl->handshake->psk_opaque_is_internal = 0;
2225 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002226 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002227 }
Neil Armstronge952a302022-05-03 10:22:14 +02002228#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (ssl->handshake->psk != NULL) {
2230 mbedtls_platform_zeroize(ssl->handshake->psk,
2231 ssl->handshake->psk_len);
2232 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002233 ssl->handshake->psk_len = 0;
2234 }
Neil Armstronge952a302022-05-03 10:22:14 +02002235#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002236}
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2239 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002240{
Neil Armstrong501c9322022-05-03 09:35:09 +02002241#if defined(MBEDTLS_USE_PSA_CRYPTO)
2242 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002244 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002246#endif /* MBEDTLS_USE_PSA_CRYPTO */
2247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (psk == NULL || ssl->handshake == NULL) {
2249 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2250 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2253 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2254 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002257
Neil Armstrong501c9322022-05-03 09:35:09 +02002258#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2261 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2262 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2263 } else {
2264 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2265 }
2266 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002267 }
2268#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002269
Jerry Yu568ec252022-07-22 21:27:34 +08002270#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2272 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2273 psa_set_key_usage_flags(&key_attributes,
2274 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002275 }
2276#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 psa_set_key_algorithm(&key_attributes, alg);
2279 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2282 if (status != PSA_SUCCESS) {
2283 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2284 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002285
2286 /* Allow calling psa_destroy_key() on psk remove */
2287 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002289#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2291 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2292 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002293
2294 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002298#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002299}
2300
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002301#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002302int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2303 mbedtls_svc_key_id_t psk,
2304 const unsigned char *psk_identity,
2305 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002306{
Janos Follath865b3eb2019-12-16 11:46:15 +00002307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002308
2309 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2311 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2312 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002313
Hanno Becker7390c712018-11-15 13:33:04 +00002314 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (mbedtls_svc_key_id_is_null(psk)) {
2316 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2317 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002318 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002319
2320 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2322 psk_identity_len);
2323 if (ret != 0) {
2324 ssl_conf_remove_psk(conf);
2325 }
Hanno Becker7390c712018-11-15 13:33:04 +00002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002328}
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2331 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002332{
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if ((mbedtls_svc_key_id_is_null(psk)) ||
2334 (ssl->handshake == NULL)) {
2335 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2336 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002339 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002341}
2342#endif /* MBEDTLS_USE_PSA_CRYPTO */
2343
Jerry Yu8897c072022-08-12 13:56:53 +08002344#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002345void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2346 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2347 size_t),
2348 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002349{
2350 conf->f_psk = f_psk;
2351 conf->p_psk = p_psk;
2352}
Jerry Yu8897c072022-08-12 13:56:53 +08002353#endif /* MBEDTLS_SSL_SRV_C */
2354
Ronald Cron73fe8df2022-10-05 14:31:43 +02002355#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002356
2357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002358static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002360{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002361#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (alg == PSA_ALG_CBC_NO_PADDING) {
2363 return MBEDTLS_SSL_MODE_CBC;
2364 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002365#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (PSA_ALG_IS_AEAD(alg)) {
2367 return MBEDTLS_SSL_MODE_AEAD;
2368 }
2369 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002370}
2371
2372#else /* MBEDTLS_USE_PSA_CRYPTO */
2373
2374static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002376{
2377#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (mode == MBEDTLS_MODE_CBC) {
2379 return MBEDTLS_SSL_MODE_CBC;
2380 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002381#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2382
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002383#if defined(MBEDTLS_GCM_C) || \
2384 defined(MBEDTLS_CCM_C) || \
2385 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002387 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 mode == MBEDTLS_MODE_CHACHAPOLY) {
2389 return MBEDTLS_SSL_MODE_AEAD;
2390 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002391#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002394}
Gilles Peskine301711e2022-04-26 16:57:05 +02002395#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002396
Gilles Peskinee108d982022-04-26 16:50:40 +02002397static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2398 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002400{
2401#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2403 base_mode == MBEDTLS_SSL_MODE_CBC) {
2404 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002405 }
2406#else
2407 (void) encrypt_then_mac;
2408#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002410}
2411
Neil Armstrongab555e02022-04-04 11:07:59 +02002412mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002414{
Gilles Peskinee108d982022-04-26 16:50:40 +02002415 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002416#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002418#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002420#endif
2421 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002422
Gilles Peskinee108d982022-04-26 16:50:40 +02002423 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002424#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002425 encrypt_then_mac = transform->encrypt_then_mac;
2426#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002428}
2429
Neil Armstrongab555e02022-04-04 11:07:59 +02002430mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002431#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002433#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002435{
Gilles Peskinee108d982022-04-26 16:50:40 +02002436 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2437
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002438#if defined(MBEDTLS_USE_PSA_CRYPTO)
2439 psa_status_t status;
2440 psa_algorithm_t alg;
2441 psa_key_type_t type;
2442 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2444 if (status == PSA_SUCCESS) {
2445 base_mode = mbedtls_ssl_get_base_mode(alg);
2446 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002447#else
2448 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 mbedtls_cipher_info_from_type(suite->cipher);
2450 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002451 base_mode =
2452 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002454 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002455#endif /* MBEDTLS_USE_PSA_CRYPTO */
2456
Gilles Peskinee108d982022-04-26 16:50:40 +02002457#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2458 int encrypt_then_mac = 0;
2459#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002461}
2462
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002463#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002464
2465#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002466/* Serialization of TLS 1.3 sessions:
2467 *
2468 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002469 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002470 * uint64 ticket_received;
2471 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002472 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002473 * } ClientOnlyData;
2474 *
2475 * struct {
2476 * uint8 endpoint;
2477 * uint8 ciphersuite[2];
2478 * uint32 ticket_age_add;
2479 * uint8 ticket_flags;
2480 * opaque resumption_key<0..255>;
2481 * select ( endpoint ) {
2482 * case client: ClientOnlyData;
2483 * case server: uint64 start_time;
2484 * };
2485 * } serialized_session_tls13;
2486 *
2487 */
2488#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002489MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002490static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2491 unsigned char *buf,
2492 size_t buf_len,
2493 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002494{
2495 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002496#if defined(MBEDTLS_SSL_CLI_C) && \
2497 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 size_t hostname_len = (session->hostname == NULL) ?
2499 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002500#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002501 size_t needed = 1 /* endpoint */
2502 + 2 /* ciphersuite */
2503 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002504 + 1 /* ticket_flags */
2505 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002506 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2509 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2510 }
Jerry Yu34191072022-08-18 10:32:09 +08002511 needed += session->resumption_key_len; /* resumption_key */
2512
Jerry Yu438ddd82022-07-07 06:55:50 +00002513#if defined(MBEDTLS_HAVE_TIME)
2514 needed += 8; /* start_time or ticket_received */
2515#endif
2516
2517#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002519#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002520 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002522#endif
2523
Jerry Yu438ddd82022-07-07 06:55:50 +00002524 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002525 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002526
2527 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (session->ticket_len > SIZE_MAX - needed) {
2529 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2530 }
Jerry Yu34191072022-08-18 10:32:09 +08002531
Jerry Yue28d9742022-08-18 15:44:03 +08002532 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002533 }
2534#endif /* MBEDTLS_SSL_CLI_C */
2535
Jerry Yue36fdd62022-08-17 21:31:36 +08002536 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (needed > buf_len) {
2538 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2539 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002540
2541 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2543 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002544 p[7] = session->ticket_flags;
2545
2546 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002547 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002548 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002550 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002551
2552#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2554 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002555 p += 8;
2556 }
2557#endif /* MBEDTLS_HAVE_TIME */
2558
2559#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002561#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002563 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002565 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002567 p += hostname_len;
2568 }
2569#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2570
Jerry Yu438ddd82022-07-07 06:55:50 +00002571#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002573 p += 8;
2574#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002576 p += 4;
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002579 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (session->ticket != NULL && session->ticket_len > 0) {
2582 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002583 p += session->ticket_len;
2584 }
2585 }
2586#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002588}
2589
2590MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002591static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2592 const unsigned char *buf,
2593 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002594{
2595 const unsigned char *p = buf;
2596 const unsigned char *end = buf + len;
2597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (end - p < 9) {
2599 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2600 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002601 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2603 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002604 session->ticket_flags = p[7];
2605
2606 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002607 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002608 p += 9;
2609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (end - p < session->resumption_key_len) {
2611 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2612 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2615 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2616 }
2617 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002618 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002619
2620#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2622 if (end - p < 8) {
2623 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2624 }
2625 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002626 p += 8;
2627 }
2628#endif /* MBEDTLS_HAVE_TIME */
2629
2630#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002632#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002634 size_t hostname_len;
2635 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if (end - p < 2) {
2637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2638 }
2639 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002640 p += 2;
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (end - p < (long int) hostname_len) {
2643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2644 }
2645 if (hostname_len > 0) {
2646 session->hostname = mbedtls_calloc(1, hostname_len);
2647 if (session->hostname == NULL) {
2648 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2649 }
2650 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002651 p += hostname_len;
2652 }
2653#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2654 MBEDTLS_SSL_SESSION_TICKETS */
2655
Jerry Yu438ddd82022-07-07 06:55:50 +00002656#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if (end - p < 8) {
2658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2659 }
2660 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002661 p += 8;
2662#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (end - p < 4) {
2664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2665 }
2666 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002667 p += 4;
2668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (end - p < 2) {
2670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2671 }
2672 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002673 p += 2;
2674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 if (end - p < (long int) session->ticket_len) {
2676 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2677 }
2678 if (session->ticket_len > 0) {
2679 session->ticket = mbedtls_calloc(1, session->ticket_len);
2680 if (session->ticket == NULL) {
2681 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2682 }
2683 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002684 p += session->ticket_len;
2685 }
2686 }
2687#endif /* MBEDTLS_SSL_CLI_C */
2688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002690
2691}
2692#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002693MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002694static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2695 unsigned char *buf,
2696 size_t buf_len,
2697 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002698{
2699 ((void) session);
2700 ((void) buf);
2701 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002702 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002704}
Jerry Yu438ddd82022-07-07 06:55:50 +00002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2707 unsigned char *buf,
2708 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002709{
2710 ((void) session);
2711 ((void) buf);
2712 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002714}
2715#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002716#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2719 size_t taglen,
2720 psa_algorithm_t *alg,
2721 psa_key_type_t *key_type,
2722 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002723{
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002725 case MBEDTLS_CIPHER_AES_128_CBC:
2726 *alg = PSA_ALG_CBC_NO_PADDING;
2727 *key_type = PSA_KEY_TYPE_AES;
2728 *key_size = 128;
2729 break;
2730 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002732 *key_type = PSA_KEY_TYPE_AES;
2733 *key_size = 128;
2734 break;
2735 case MBEDTLS_CIPHER_AES_128_GCM:
2736 *alg = PSA_ALG_GCM;
2737 *key_type = PSA_KEY_TYPE_AES;
2738 *key_size = 128;
2739 break;
2740 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002742 *key_type = PSA_KEY_TYPE_AES;
2743 *key_size = 192;
2744 break;
2745 case MBEDTLS_CIPHER_AES_192_GCM:
2746 *alg = PSA_ALG_GCM;
2747 *key_type = PSA_KEY_TYPE_AES;
2748 *key_size = 192;
2749 break;
2750 case MBEDTLS_CIPHER_AES_256_CBC:
2751 *alg = PSA_ALG_CBC_NO_PADDING;
2752 *key_type = PSA_KEY_TYPE_AES;
2753 *key_size = 256;
2754 break;
2755 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002757 *key_type = PSA_KEY_TYPE_AES;
2758 *key_size = 256;
2759 break;
2760 case MBEDTLS_CIPHER_AES_256_GCM:
2761 *alg = PSA_ALG_GCM;
2762 *key_type = PSA_KEY_TYPE_AES;
2763 *key_size = 256;
2764 break;
2765 case MBEDTLS_CIPHER_ARIA_128_CBC:
2766 *alg = PSA_ALG_CBC_NO_PADDING;
2767 *key_type = PSA_KEY_TYPE_ARIA;
2768 *key_size = 128;
2769 break;
2770 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002772 *key_type = PSA_KEY_TYPE_ARIA;
2773 *key_size = 128;
2774 break;
2775 case MBEDTLS_CIPHER_ARIA_128_GCM:
2776 *alg = PSA_ALG_GCM;
2777 *key_type = PSA_KEY_TYPE_ARIA;
2778 *key_size = 128;
2779 break;
2780 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002782 *key_type = PSA_KEY_TYPE_ARIA;
2783 *key_size = 192;
2784 break;
2785 case MBEDTLS_CIPHER_ARIA_192_GCM:
2786 *alg = PSA_ALG_GCM;
2787 *key_type = PSA_KEY_TYPE_ARIA;
2788 *key_size = 192;
2789 break;
2790 case MBEDTLS_CIPHER_ARIA_256_CBC:
2791 *alg = PSA_ALG_CBC_NO_PADDING;
2792 *key_type = PSA_KEY_TYPE_ARIA;
2793 *key_size = 256;
2794 break;
2795 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002797 *key_type = PSA_KEY_TYPE_ARIA;
2798 *key_size = 256;
2799 break;
2800 case MBEDTLS_CIPHER_ARIA_256_GCM:
2801 *alg = PSA_ALG_GCM;
2802 *key_type = PSA_KEY_TYPE_ARIA;
2803 *key_size = 256;
2804 break;
2805 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2806 *alg = PSA_ALG_CBC_NO_PADDING;
2807 *key_type = PSA_KEY_TYPE_CAMELLIA;
2808 *key_size = 128;
2809 break;
2810 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002812 *key_type = PSA_KEY_TYPE_CAMELLIA;
2813 *key_size = 128;
2814 break;
2815 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2816 *alg = PSA_ALG_GCM;
2817 *key_type = PSA_KEY_TYPE_CAMELLIA;
2818 *key_size = 128;
2819 break;
2820 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002822 *key_type = PSA_KEY_TYPE_CAMELLIA;
2823 *key_size = 192;
2824 break;
2825 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2826 *alg = PSA_ALG_GCM;
2827 *key_type = PSA_KEY_TYPE_CAMELLIA;
2828 *key_size = 192;
2829 break;
2830 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2831 *alg = PSA_ALG_CBC_NO_PADDING;
2832 *key_type = PSA_KEY_TYPE_CAMELLIA;
2833 *key_size = 256;
2834 break;
2835 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002837 *key_type = PSA_KEY_TYPE_CAMELLIA;
2838 *key_size = 256;
2839 break;
2840 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2841 *alg = PSA_ALG_GCM;
2842 *key_type = PSA_KEY_TYPE_CAMELLIA;
2843 *key_size = 256;
2844 break;
2845 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2846 *alg = PSA_ALG_CHACHA20_POLY1305;
2847 *key_type = PSA_KEY_TYPE_CHACHA20;
2848 *key_size = 256;
2849 break;
2850 case MBEDTLS_CIPHER_NULL:
2851 *alg = MBEDTLS_SSL_NULL_CIPHER;
2852 *key_type = 0;
2853 *key_size = 0;
2854 break;
2855 default:
2856 return PSA_ERROR_NOT_SUPPORTED;
2857 }
2858
2859 return PSA_SUCCESS;
2860}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002861#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002862
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002863#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002864int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2865 const unsigned char *dhm_P, size_t P_len,
2866 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002867{
Janos Follath865b3eb2019-12-16 11:46:15 +00002868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 mbedtls_mpi_free(&conf->dhm_P);
2871 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2874 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2875 mbedtls_mpi_free(&conf->dhm_P);
2876 mbedtls_mpi_free(&conf->dhm_G);
2877 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002878 }
2879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002881}
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002884{
Janos Follath865b3eb2019-12-16 11:46:15 +00002885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 mbedtls_mpi_free(&conf->dhm_P);
2888 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2891 &conf->dhm_P)) != 0 ||
2892 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2893 &conf->dhm_G)) != 0) {
2894 mbedtls_mpi_free(&conf->dhm_P);
2895 mbedtls_mpi_free(&conf->dhm_G);
2896 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002897 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002900}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002901#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002902
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002903#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2904/*
2905 * Set the minimum length for Diffie-Hellman parameters
2906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2908 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002909{
2910 conf->dhm_min_bitlen = bitlen;
2911}
2912#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2913
Ronald Crone68ab4f2022-10-05 12:46:29 +02002914#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002915#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002916/*
2917 * Set allowed/preferred hashes for handshake signatures
2918 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2920 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002921{
2922 conf->sig_hashes = hashes;
2923}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002924#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002925
Jerry Yuf017ee42022-01-12 15:49:48 +08002926/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002927void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2928 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002929{
Jerry Yuf017ee42022-01-12 15:49:48 +08002930#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2931 conf->sig_hashes = NULL;
2932#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2933 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002934}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002935#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002936
Valerio Setti6c496a12023-04-07 15:53:51 +02002937#if defined(MBEDTLS_ECP_LIGHT)
Brett Warrene0edc842021-08-17 09:53:13 +01002938#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002939/*
2940 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002941 *
2942 * mbedtls_ssl_setup() takes the provided list
2943 * and translates it to a list of IANA TLS group identifiers,
2944 * stored in ssl->handshake->group_list.
2945 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002946 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002947void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2948 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002949{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002950 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002951 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002952}
Brett Warrene0edc842021-08-17 09:53:13 +01002953#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6c496a12023-04-07 15:53:51 +02002954#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002955
Brett Warrene0edc842021-08-17 09:53:13 +01002956/*
2957 * Set the allowed groups
2958 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002959void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2960 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002961{
2962#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2963 conf->curve_list = NULL;
2964#endif
2965 conf->group_list = group_list;
2966}
2967
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002968#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002969int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002970{
Hanno Becker947194e2017-04-07 13:25:49 +01002971 /* Initialize to suppress unnecessary compiler warning */
2972 size_t hostname_len = 0;
2973
2974 /* Check if new hostname is valid before
2975 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 if (hostname != NULL) {
2977 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2980 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2981 }
Hanno Becker947194e2017-04-07 13:25:49 +01002982 }
2983
2984 /* Now it's clear that we will overwrite the old hostname,
2985 * so we can free it safely */
2986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (ssl->hostname != NULL) {
2988 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2989 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002990 }
2991
2992 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002995 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 } else {
2997 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2998 if (ssl->hostname == NULL) {
2999 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3000 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003003
Hanno Becker947194e2017-04-07 13:25:49 +01003004 ssl->hostname[hostname_len] = '\0';
3005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003008}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01003009#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01003011#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003012void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
3013 int (*f_sni)(void *, mbedtls_ssl_context *,
3014 const unsigned char *, size_t),
3015 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00003016{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003017 conf->f_sni = f_sni;
3018 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00003019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01003023int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003024{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003025 size_t cur_len, tot_len;
3026 const char **p;
3027
3028 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08003029 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
3030 * MUST NOT be truncated."
3031 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003032 */
3033 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 for (p = protos; *p != NULL; p++) {
3035 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003036 tot_len += cur_len;
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if ((cur_len == 0) ||
3039 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3040 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3041 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3042 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003043 }
3044
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003045 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003048}
3049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003051{
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003055
Johan Pascalb62bb512015-12-03 21:56:45 +01003056#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003057void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3058 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003059{
3060 conf->dtls_srtp_mki_support = support_mki_value;
3061}
3062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3064 unsigned char *mki_value,
3065 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003066{
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003069 }
Ron Eldor591f1622018-01-22 12:30:04 +02003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3072 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003073 }
Ron Eldor591f1622018-01-22 12:30:04 +02003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003076 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003078}
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3081 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003082{
Johan Pascal253d0262020-09-22 13:04:45 +02003083 const mbedtls_ssl_srtp_profile *p;
3084 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003085
Johan Pascal253d0262020-09-22 13:04:45 +02003086 /* check the profiles list: all entry must be valid,
3087 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3089 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3090 p++) {
3091 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003092 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003094 /* unsupported value, stop parsing and set the size to an error value */
3095 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003096 }
3097 }
3098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3100 conf->dtls_srtp_profile_list = NULL;
3101 conf->dtls_srtp_profile_list_len = 0;
3102 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003103 }
3104
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003105 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003106 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003109}
3110
Gilles Peskine449bd832023-01-11 14:50:10 +01003111void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3112 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003113{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003114 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3115 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003117 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003119 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3121 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003122 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003123}
Johan Pascalb62bb512015-12-03 21:56:45 +01003124#endif /* MBEDTLS_SSL_DTLS_SRTP */
3125
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303126#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003127void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003128{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003129 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003130}
3131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003133{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003134 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003135}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303136#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003137
Janos Follath088ce432017-04-10 12:42:31 +01003138#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003139void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3140 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003141{
3142 conf->cert_req_ca_list = cert_req_ca_list;
3143}
3144#endif
3145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003147void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003148{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003149 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003150}
3151#endif
3152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003154void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003155{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003156 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003157}
3158#endif
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003161int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003162{
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3164 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3165 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003166 }
3167
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003168 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003173
Gilles Peskine449bd832023-01-11 14:50:10 +01003174void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003176 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003177}
3178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003180void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003182 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003183}
3184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003186{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003187 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003188}
3189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3191 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003192{
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003198#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003199void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003200{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003201 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003202}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003203#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003204
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003205#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003206
Jerry Yud0766ec2022-09-22 10:46:57 +08003207#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003208void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3209 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003210{
Jerry Yud0766ec2022-09-22 10:46:57 +08003211 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003212}
3213#endif
3214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3216 mbedtls_ssl_ticket_write_t *f_ticket_write,
3217 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3218 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003219{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003220 conf->f_ticket_write = f_ticket_write;
3221 conf->f_ticket_parse = f_ticket_parse;
3222 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003223}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003226
Gilles Peskine449bd832023-01-11 14:50:10 +01003227void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3228 mbedtls_ssl_export_keys_t *f_export_keys,
3229 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003230{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003231 ssl->f_export_keys = f_export_keys;
3232 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003233}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003234
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003235#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003236void mbedtls_ssl_conf_async_private_cb(
3237 mbedtls_ssl_config *conf,
3238 mbedtls_ssl_async_sign_t *f_async_sign,
3239 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3240 mbedtls_ssl_async_resume_t *f_async_resume,
3241 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003243{
3244 conf->f_async_sign_start = f_async_sign;
3245 conf->f_async_decrypt_start = f_async_decrypt;
3246 conf->f_async_resume = f_async_resume;
3247 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003248 conf->p_async_config_data = async_config_data;
3249}
3250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003252{
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003254}
3255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003257{
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 if (ssl->handshake == NULL) {
3259 return NULL;
3260 } else {
3261 return ssl->handshake->user_async_ctx;
3262 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003263}
3264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3266 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003267{
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003269 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003271}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003272#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003273
Paul Bakker5121ce52009-01-03 21:22:43 +00003274/*
3275 * SSL get accessors
3276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003277uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003278{
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (ssl->session != NULL) {
3280 return ssl->session->verify_result;
3281 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 if (ssl->session_negotiate != NULL) {
3284 return ssl->session_negotiate->verify_result;
3285 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003288}
3289
Gilles Peskine449bd832023-01-11 14:50:10 +01003290int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003291{
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (ssl == NULL || ssl->session == NULL) {
3293 return 0;
3294 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003297}
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003300{
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (ssl == NULL || ssl->session == NULL) {
3302 return NULL;
3303 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003304
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003306}
3307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3312 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003313 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003315 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003317 }
3318 }
3319#endif
3320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003322 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003324 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003326 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003328 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003329}
3330
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003331#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003332size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003333{
David Horstmann95d516f2021-05-04 18:36:56 +01003334 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003335 size_t read_mfl;
3336
Jerry Yuddda0502022-12-01 19:43:12 +08003337#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003338 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3340 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3341 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003342 }
Jerry Yuddda0502022-12-01 19:43:12 +08003343#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003344
3345 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ssl->session_out != NULL) {
3347 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3348 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003349 max_len = read_mfl;
3350 }
3351 }
3352
Jerry Yuddda0502022-12-01 19:43:12 +08003353 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 if (ssl->session_negotiate != NULL) {
3355 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3356 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003357 max_len = read_mfl;
3358 }
3359 }
3360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003362}
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003365{
3366 size_t max_len;
3367
3368 /*
3369 * Assume mfl_code is correct since it was checked when set
3370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003372
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003373 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 if (ssl->session_out != NULL &&
3375 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3376 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003377 }
3378
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003379 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 if (ssl->session_negotiate != NULL &&
3381 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3382 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003383 }
3384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003386}
3387#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3388
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003389#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003390size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003391{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003392 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3394 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3395 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3396 return 0;
3397 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3400 return ssl->mtu;
3401 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 if (ssl->mtu == 0) {
3404 return ssl->handshake->mtu;
3405 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 return ssl->mtu < ssl->handshake->mtu ?
3408 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003409}
3410#endif /* MBEDTLS_SSL_PROTO_DTLS */
3411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003413{
3414 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3415
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003416#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3417 !defined(MBEDTLS_SSL_PROTO_DTLS)
3418 (void) ssl;
3419#endif
3420
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003421#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003425 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003427#endif
3428
3429#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3431 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3432 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003433 const size_t overhead = (size_t) ret;
3434
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 if (ret < 0) {
3436 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003437 }
3438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 if (mtu <= overhead) {
3440 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3441 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3442 }
3443
3444 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003445 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003447 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003448#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003449
Hanno Becker0defedb2018-08-10 12:35:02 +01003450#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3451 !defined(MBEDTLS_SSL_PROTO_DTLS)
3452 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003453#endif
3454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003456}
3457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003459{
3460 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3461
3462#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3463 (void) ssl;
3464#endif
3465
3466#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003470 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003472#endif
3473
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003475}
3476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003478const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003479{
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 if (ssl == NULL || ssl->session == NULL) {
3481 return NULL;
3482 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003483
Hanno Beckere6824572019-02-07 13:18:46 +00003484#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003486#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003488#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003493int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3494 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003495{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003496 int ret;
3497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003499 dst == NULL ||
3500 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3502 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003503 }
3504
Hanno Beckere810bbc2021-05-14 16:01:05 +01003505 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3506 * idempotent: Each session can only be exported once.
3507 *
3508 * (This is in preparation for TLS 1.3 support where we will
3509 * need the ability to export multiple sessions (aka tickets),
3510 * which will be achieved by calling mbedtls_ssl_get_session()
3511 * multiple times until it fails.)
3512 *
3513 * Check whether we have already exported the current session,
3514 * and fail if so.
3515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 if (ssl->session->exported == 1) {
3517 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3518 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003519
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3521 if (ret != 0) {
3522 return ret;
3523 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003524
3525 /* Remember that we've exported the session. */
3526 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003530
Paul Bakker5121ce52009-01-03 21:22:43 +00003531/*
Hanno Beckera835da52019-05-16 12:39:07 +01003532 * Define ticket header determining Mbed TLS version
3533 * and structure of the ticket.
3534 */
3535
Hanno Becker94ef3b32019-05-16 12:50:45 +01003536/*
Hanno Becker50b59662019-05-28 14:30:45 +01003537 * Define bitflag determining compile-time settings influencing
3538 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003539 */
3540
Hanno Becker50b59662019-05-28 14:30:45 +01003541#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003542#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003543#else
Hanno Becker3e088662019-05-29 11:10:18 +01003544#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003545#endif /* MBEDTLS_HAVE_TIME */
3546
3547#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003548#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003549#else
Hanno Becker3e088662019-05-29 11:10:18 +01003550#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003551#endif /* MBEDTLS_X509_CRT_PARSE_C */
3552
3553#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003554#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003555#else
Hanno Becker3e088662019-05-29 11:10:18 +01003556#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003557#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3558
3559#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003560#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003561#else
Hanno Becker3e088662019-05-29 11:10:18 +01003562#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003563#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3564
Hanno Becker94ef3b32019-05-16 12:50:45 +01003565#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003566#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003567#else
Hanno Becker3e088662019-05-29 11:10:18 +01003568#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003569#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3570
Hanno Becker94ef3b32019-05-16 12:50:45 +01003571#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3572#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3573#else
3574#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3575#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3576
Hanno Becker3e088662019-05-29 11:10:18 +01003577#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3578#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3579#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3580#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003581#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3582#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003583
Hanno Becker50b59662019-05-28 14:30:45 +01003584#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 ((uint16_t) ( \
3586 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3587 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3588 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3589 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3590 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3591 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3592 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003593
Hanno Beckerf8787072019-05-16 12:41:07 +01003594static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003595 MBEDTLS_VERSION_MAJOR,
3596 MBEDTLS_VERSION_MINOR,
3597 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3599 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003600};
Hanno Beckera835da52019-05-16 12:39:07 +01003601
3602/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003603 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003604 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003605 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003607 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003608 * opaque mbedtls_version[3]; // library version: major, minor, patch
3609 * opaque session_format[2]; // library-version specific 16-bit field
3610 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003611 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003612 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003613 * Note: When updating the format, remember to keep
3614 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003615 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003616 * // In this version, `session_format` determines
3617 * // the setting of those compile-time
3618 * // configuration options which influence
3619 * // the structure of mbedtls_ssl_session.
3620 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003621 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003622 * // - TLS 1.2 (0x0303)
3623 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003624 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003625 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003627 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003628 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003629 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003630 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003631 *
3632 * };
3633 *
3634 * } serialized_session;
3635 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003636 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003637
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003638MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003639static int ssl_session_save(const mbedtls_ssl_session *session,
3640 unsigned char omit_header,
3641 unsigned char *buf,
3642 size_t buf_len,
3643 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003644{
3645 unsigned char *p = buf;
3646 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003647 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003648#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3649 size_t out_len;
3650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3651#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003652 if (session == NULL) {
3653 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3654 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003655
Gilles Peskine449bd832023-01-11 14:50:10 +01003656 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003657 /*
3658 * Add Mbed TLS version identifier
3659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003660 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661
Gilles Peskine449bd832023-01-11 14:50:10 +01003662 if (used <= buf_len) {
3663 memcpy(p, ssl_serialized_session_header,
3664 sizeof(ssl_serialized_session_header));
3665 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003666 }
3667 }
3668
3669 /*
3670 * TLS version identifier
3671 */
3672 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 if (used <= buf_len) {
3674 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003675 }
3676
3677 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003678 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003679 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003680#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 case MBEDTLS_SSL_VERSION_TLS1_2:
3682 used += ssl_tls12_session_save(session, p, remaining_len);
3683 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003684#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3685
Jerry Yu251a12e2022-07-13 15:15:48 +08003686#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003687 case MBEDTLS_SSL_VERSION_TLS1_3:
3688 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3689 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3690 return ret;
3691 }
3692 used += out_len;
3693 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003694#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3695
Gilles Peskine449bd832023-01-11 14:50:10 +01003696 default:
3697 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003698 }
3699
3700 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 if (used > buf_len) {
3702 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3703 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003704
Gilles Peskine449bd832023-01-11 14:50:10 +01003705 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003706}
3707
3708/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003709 * Public wrapper for ssl_session_save()
3710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003711int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3712 unsigned char *buf,
3713 size_t buf_len,
3714 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003715{
Gilles Peskine449bd832023-01-11 14:50:10 +01003716 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003717}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003718
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003719/*
3720 * Deserialize session, see mbedtls_ssl_session_save() for format.
3721 *
3722 * This internal version is wrapped by a public function that cleans up in
3723 * case of error, and has an extra option omit_header.
3724 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003725MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003726static int ssl_session_load(mbedtls_ssl_session *session,
3727 unsigned char omit_header,
3728 const unsigned char *buf,
3729 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003730{
3731 const unsigned char *p = buf;
3732 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003733 size_t remaining_len;
3734
3735
Gilles Peskine449bd832023-01-11 14:50:10 +01003736 if (session == NULL) {
3737 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3738 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003739
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003741 /*
3742 * Check Mbed TLS version identifier
3743 */
3744
Gilles Peskine449bd832023-01-11 14:50:10 +01003745 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3746 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003747 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003748
3749 if (memcmp(p, ssl_serialized_session_header,
3750 sizeof(ssl_serialized_session_header)) != 0) {
3751 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3752 }
3753 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003754 }
3755
3756 /*
3757 * TLS version identifier
3758 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 if (1 > (size_t) (end - p)) {
3760 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3761 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003762 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003763
3764 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003765 remaining_len = (end - p);
3766 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003767#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003768 case MBEDTLS_SSL_VERSION_TLS1_2:
3769 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003770#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3771
Jerry Yu438ddd82022-07-07 06:55:50 +00003772#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 case MBEDTLS_SSL_VERSION_TLS1_3:
3774 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003775#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3776
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 default:
3778 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003779 }
3780}
3781
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003782/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003783 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003784 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003785int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3786 const unsigned char *buf,
3787 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003788{
Gilles Peskine449bd832023-01-11 14:50:10 +01003789 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003790
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 if (ret != 0) {
3792 mbedtls_ssl_session_free(session);
3793 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003794
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003796}
3797
3798/*
Paul Bakker1961b702013-01-25 14:49:24 +01003799 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003801MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003802static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003803{
3804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3805
Ronald Cron66dbf912022-02-02 15:33:46 +01003806 /*
3807 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003808 * that were written into the output buffer by the previous handshake step,
3809 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003810 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3811 * We proceed to the next handshake step only when all data from the
3812 * previous one have been sent to the peer, thus we make sure that this is
3813 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3814 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3815 * we have to wait before to go ahead.
3816 * In the case of TLS 1.3, handshake step handlers do not send data to the
3817 * peer. Data are only sent here and through
3818 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003819 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3822 return ret;
3823 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003824
3825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3827 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3828 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3829 return ret;
3830 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003831 }
3832#endif /* MBEDTLS_SSL_PROTO_DTLS */
3833
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003835}
3836
Gilles Peskine449bd832023-01-11 14:50:10 +01003837int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003838{
Hanno Becker41934dd2021-08-07 19:13:43 +01003839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003840
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003842 ssl->conf == NULL ||
3843 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3845 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003846 }
3847
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 ret = ssl_prepare_handshake_step(ssl);
3849 if (ret != 0) {
3850 return ret;
3851 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 ret = mbedtls_ssl_handle_pending_alert(ssl);
3854 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003855 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 }
Jerry Yue7047812021-09-13 19:26:39 +08003857
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003858 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3859 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3860 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3864 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3865 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003866
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003868 case MBEDTLS_SSL_HELLO_REQUEST:
3869 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003870 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003871 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003872
Ronald Cron9f0fba32022-02-10 16:45:15 +01003873 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003874 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003875 break;
3876
3877 default:
3878#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3880 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3881 } else {
3882 ret = mbedtls_ssl_handshake_client_step(ssl);
3883 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003884#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003886#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003888#endif
3889 }
Jerry Yub9930e72021-08-06 17:11:51 +08003890 }
Ronald Cron6291b232023-03-08 15:51:25 +01003891#endif /* MBEDTLS_SSL_CLI_C */
3892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01003895#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
3896 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01003898 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 ret = mbedtls_ssl_handshake_server_step(ssl);
3900 }
Ronald Cron6291b232023-03-08 15:51:25 +01003901#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3902 ret = mbedtls_ssl_handshake_server_step(ssl);
3903#else
3904 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003905#endif
Ronald Cron6291b232023-03-08 15:51:25 +01003906 }
3907#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003908
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003910 /* handshake_step return error. And it is same
3911 * with alert_reason.
3912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 if (ssl->send_alert) {
3914 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003915 goto cleanup;
3916 }
3917 }
3918
3919cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003921}
3922
3923/*
3924 * Perform the SSL handshake
3925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003926int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003927{
3928 int ret = 0;
3929
Hanno Beckera817ea42020-10-20 15:20:23 +01003930 /* Sanity checks */
3931
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 if (ssl == NULL || ssl->conf == NULL) {
3933 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3934 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003935
Hanno Beckera817ea42020-10-20 15:20:23 +01003936#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3938 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3939 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3940 "mbedtls_ssl_set_timer_cb() for DTLS"));
3941 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003942 }
3943#endif /* MBEDTLS_SSL_PROTO_DTLS */
3944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003946
Hanno Beckera817ea42020-10-20 15:20:23 +01003947 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3949 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003950
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003952 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 }
Paul Bakker1961b702013-01-25 14:49:24 +01003954 }
3955
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003959}
3960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961#if defined(MBEDTLS_SSL_RENEGOTIATION)
3962#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003963/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003964 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003965 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003966MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003967static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003968{
Janos Follath865b3eb2019-12-16 11:46:15 +00003969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003972
3973 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3975 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3978 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3979 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003980 }
3981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003987
3988/*
3989 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 * - any side: calling mbedtls_ssl_renegotiate(),
3991 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3992 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003993 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003994 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003996 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003997int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003998{
Janos Follath865b3eb2019-12-16 11:46:15 +00003999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004002
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 if ((ret = ssl_handshake_init(ssl)) != 0) {
4004 return ret;
4005 }
Paul Bakker48916f92012-09-16 19:57:18 +00004006
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004007 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4008 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4011 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4012 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004013 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004015 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004017 }
4018#endif
4019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4021 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4024 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4025 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004026 }
4027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004031}
4032
4033/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004034 * Renegotiate current connection on client,
4035 * or request renegotiation on server
4036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004037int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 if (ssl == NULL || ssl->conf == NULL) {
4042 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4043 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004046 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4048 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4049 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4050 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004053
4054 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (ssl->out_left != 0) {
4056 return mbedtls_ssl_flush_output(ssl);
4057 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004060 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004064 /*
4065 * On client, either start the renegotiation process or,
4066 * if already in progress, continue the handshake
4067 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4069 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4070 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004072
4073 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4074 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4075 return ret;
4076 }
4077 } else {
4078 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4079 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4080 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004081 }
4082 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004088
Gilles Peskine449bd832023-01-11 14:50:10 +01004089void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004090{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004091 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004094 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004096
Valerio Setti6c496a12023-04-07 15:53:51 +02004097#if defined(MBEDTLS_ECP_LIGHT)
Brett Warrene0edc842021-08-17 09:53:13 +01004098#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (ssl->handshake->group_list_heap_allocated) {
4100 mbedtls_free((void *) handshake->group_list);
4101 }
Brett Warrene0edc842021-08-17 09:53:13 +01004102 handshake->group_list = NULL;
4103#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6c496a12023-04-07 15:53:51 +02004104#endif /* MBEDTLS_ECP_LIGHT */
Brett Warrene0edc842021-08-17 09:53:13 +01004105
Ronald Crone68ab4f2022-10-05 12:46:29 +02004106#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004107#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (ssl->handshake->sig_algs_heap_allocated) {
4109 mbedtls_free((void *) handshake->sig_algs);
4110 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004111 handshake->sig_algs = NULL;
4112#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004113#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (ssl->handshake->certificate_request_context) {
4115 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004116 }
4117#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004118#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004119
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004120#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4122 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004123 handshake->async_in_progress = 0;
4124 }
4125#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4126
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004127#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004130#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004131 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004132#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004133#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004134#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004135#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004137#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004138 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004139#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004140#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004144#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004145#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004147#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004148
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004149#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004150#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004152 /*
4153 * Opaque keys are not stored in the handshake's data and it's the user
4154 * responsibility to destroy them. Clear ones, instead, are created by
4155 * the TLS library and should be destroyed at the same level
4156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4158 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004159 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004160 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4161#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004163#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004164#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004166 handshake->ecjpake_cache = NULL;
4167 handshake->ecjpake_cache_len = 0;
4168#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004169#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004170
Janos Follath4ae5c292016-02-10 11:27:43 +00004171#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4172 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004173 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004175#endif
4176
Ronald Cron73fe8df2022-10-05 14:31:43 +02004177#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004178#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004180 /* The maintenance of the external PSK key slot is the
4181 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 if (ssl->handshake->psk_opaque_is_internal) {
4183 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004184 ssl->handshake->psk_opaque_is_internal = 0;
4185 }
4186 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4187 }
Neil Armstronge952a302022-05-03 10:22:14 +02004188#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (handshake->psk != NULL) {
4190 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4191 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004192 }
Neil Armstronge952a302022-05-03 10:22:14 +02004193#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004194#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4197 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004198 /*
4199 * Free only the linked list wrapper, not the keys themselves
4200 * since the belong to the SNI callback
4201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004204
Gilles Peskineeccd8882020-03-10 12:19:08 +01004205#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4207 if (handshake->ecrs_peer_cert != NULL) {
4208 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4209 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004210 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004211#endif
4212
Hanno Becker75173122019-02-06 16:18:31 +00004213#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4214 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004216#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4217
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004218#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4220 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004221#endif /* MBEDTLS_SSL_CLI_C &&
4222 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004223
4224#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 mbedtls_ssl_flight_free(handshake->flight);
4226 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004228
Valerio Setti080a22b2023-03-20 15:22:47 +01004229#if defined(PSA_WANT_ALG_ECDH) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4231 if (handshake->ecdh_psa_privkey_is_external == 0) {
4232 psa_destroy_key(handshake->ecdh_psa_privkey);
4233 }
Valerio Setti080a22b2023-03-20 15:22:47 +01004234#endif /* PSA_WANT_ALG_ECDH && (MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3) */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004235
Ronald Cron6f135e12021-12-08 16:57:54 +01004236#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 mbedtls_ssl_transform_free(handshake->transform_handshake);
4238 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004239#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4241 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004242#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004243#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004244
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004245
4246#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4247 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4248 * processes datagrams and the fact that a datagram is allowed to have
4249 * several records in it, it is possible that the I/O buffers are not
4250 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4252 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004253#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004254
Jerry Yuba9c7272021-10-30 11:54:10 +08004255 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 mbedtls_platform_zeroize(handshake,
4257 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004258}
4259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004261{
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004263 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004268#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004269
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004270#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004271#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4272 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004274#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004276#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004279}
4280
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004281#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004282
4283#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4284#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4285#else
4286#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4287#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4288
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004289#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004290
4291#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4292#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4293#else
4294#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4295#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4296
4297#if defined(MBEDTLS_SSL_ALPN)
4298#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4299#else
4300#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4301#endif /* MBEDTLS_SSL_ALPN */
4302
4303#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4304#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4305#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4306#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4307
4308#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 ((uint32_t) ( \
4310 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4311 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4312 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4313 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4314 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4315 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4316 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4317 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004318
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004319static unsigned char ssl_serialized_context_header[] = {
4320 MBEDTLS_VERSION_MAJOR,
4321 MBEDTLS_VERSION_MINOR,
4322 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4324 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4325 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4326 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4327 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004328};
4329
Paul Bakker5121ce52009-01-03 21:22:43 +00004330/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004331 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004332 *
4333 * The format of the serialized data is:
4334 * (in the presentation language of TLS, RFC 8446 section 3)
4335 *
4336 * // header
4337 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004338 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004339 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004340 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004341 * Note: When updating the format, remember to keep these
4342 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004343 *
4344 * // session sub-structure
4345 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4346 * // transform sub-structure
4347 * uint8 random[64]; // ServerHello.random+ClientHello.random
4348 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4349 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4350 * // fields from ssl_context
4351 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4352 * uint64 in_window_top; // DTLS: last validated record seq_num
4353 * uint64 in_window; // DTLS: bitmask for replay protection
4354 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4355 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4356 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4357 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4358 *
4359 * Note that many fields of the ssl_context or sub-structures are not
4360 * serialized, as they fall in one of the following categories:
4361 *
4362 * 1. forced value (eg in_left must be 0)
4363 * 2. pointer to dynamically-allocated memory (eg session, transform)
4364 * 3. value can be re-derived from other data (eg session keys from MS)
4365 * 4. value was temporary (eg content of input buffer)
4366 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004368int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4369 unsigned char *buf,
4370 size_t buf_len,
4371 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004372{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004373 unsigned char *p = buf;
4374 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004375 size_t session_len;
4376 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004377
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004378 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004379 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4380 * this function's documentation.
4381 *
4382 * These are due to assumptions/limitations in the implementation. Some of
4383 * them are likely to stay (no handshake in progress) some might go away
4384 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004385 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004386 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4388 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4389 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004390 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (ssl->handshake != NULL) {
4392 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4393 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004394 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004395 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (ssl->transform == NULL || ssl->session == NULL) {
4397 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004399 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004400 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (mbedtls_ssl_check_pending(ssl) != 0) {
4402 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4403 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004404 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (ssl->out_left != 0) {
4406 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4407 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004408 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004409 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4411 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4412 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004413 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004414 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4416 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004418 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004419 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4421 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4422 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004423 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004424 /* Renegotiation must not be enabled */
4425#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4427 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4428 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004429 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004430#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004431
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004432 /*
4433 * Version and format identifier
4434 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 if (used <= buf_len) {
4438 memcpy(p, ssl_serialized_context_header,
4439 sizeof(ssl_serialized_context_header));
4440 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004441 }
4442
4443 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004444 * Session (length + data)
4445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4447 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4448 return ret;
4449 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004450
4451 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (used <= buf_len) {
4453 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004454 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 ret = ssl_session_save(ssl->session, 1,
4457 p, session_len, &session_len);
4458 if (ret != 0) {
4459 return ret;
4460 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004461
4462 p += session_len;
4463 }
4464
4465 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004466 * Transform
4467 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 used += sizeof(ssl->transform->randbytes);
4469 if (used <= buf_len) {
4470 memcpy(p, ssl->transform->randbytes,
4471 sizeof(ssl->transform->randbytes));
4472 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004473 }
4474
4475#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4476 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004478 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004480 p += ssl->transform->in_cid_len;
4481
4482 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004484 p += ssl->transform->out_cid_len;
4485 }
4486#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4487
4488 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004489 * Saved fields from top-level ssl_context structure
4490 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004491 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 if (used <= buf_len) {
4493 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004494 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004495 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004496
4497#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4498 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (used <= buf_len) {
4500 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004501 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004504 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004505 }
4506#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4507
4508#if defined(MBEDTLS_SSL_PROTO_DTLS)
4509 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004511 *p++ = ssl->disable_datagram_packing;
4512 }
4513#endif /* MBEDTLS_SSL_PROTO_DTLS */
4514
Jerry Yuae0b2e22021-10-08 15:21:19 +08004515 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (used <= buf_len) {
4517 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004518 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004519 }
4520
4521#if defined(MBEDTLS_SSL_PROTO_DTLS)
4522 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 if (used <= buf_len) {
4524 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004525 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004526 }
4527#endif /* MBEDTLS_SSL_PROTO_DTLS */
4528
4529#if defined(MBEDTLS_SSL_ALPN)
4530 {
4531 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004533 : 0;
4534
4535 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004537 *p++ = alpn_len;
4538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (ssl->alpn_chosen != NULL) {
4540 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004541 p += alpn_len;
4542 }
4543 }
4544 }
4545#endif /* MBEDTLS_SSL_ALPN */
4546
4547 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004548 * Done
4549 */
4550 *olen = used;
4551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 if (used > buf_len) {
4553 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4554 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004559}
4560
4561/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004562 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004563 *
4564 * This internal version is wrapped by a public function that cleans up in
4565 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004566 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004567MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004568static int ssl_context_load(mbedtls_ssl_context *ssl,
4569 const unsigned char *buf,
4570 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004571{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004572 const unsigned char *p = buf;
4573 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004574 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004576#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004577 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004578#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004579
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004580 /*
4581 * The context should have been freshly setup or reset.
4582 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004583 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004584 * renegotiating, or if the user mistakenly loaded a session first.)
4585 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4587 ssl->session != NULL) {
4588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004589 }
4590
4591 /*
4592 * We can't check that the config matches the initial one, but we can at
4593 * least check it matches the requirements for serializing.
4594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004596 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4597 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004598#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004599 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004600#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 0) {
4602 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004603 }
4604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004606
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004607 /*
4608 * Check version identifier
4609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4611 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004612 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004613
4614 if (memcmp(p, ssl_serialized_context_header,
4615 sizeof(ssl_serialized_context_header)) != 0) {
4616 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4617 }
4618 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004619
4620 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004621 * Session
4622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if ((size_t) (end - p) < 4) {
4624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4625 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004626
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004627 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004628 p += 4;
4629
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004630 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004631 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004632 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004633 ssl->session_in = ssl->session;
4634 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004635 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 if ((size_t) (end - p) < session_len) {
4638 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4639 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 ret = ssl_session_load(ssl->session, 1, p, session_len);
4642 if (ret != 0) {
4643 mbedtls_ssl_session_free(ssl->session);
4644 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004645 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004646
4647 p += session_len;
4648
4649 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004650 * Transform
4651 */
4652
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004653 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004654 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004656 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004657 ssl->transform_in = ssl->transform;
4658 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004659 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004660#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004661
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004662#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4664 if (prf_func == NULL) {
4665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4666 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004667
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004668 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4671 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 ret = ssl_tls12_populate_transform(ssl->transform,
4674 ssl->session->ciphersuite,
4675 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004676#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004678#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 prf_func,
4680 p, /* currently pointing to randbytes */
4681 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4682 ssl->conf->endpoint,
4683 ssl);
4684 if (ret != 0) {
4685 return ret;
4686 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004687#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004689
4690#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4691 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if ((size_t) (end - p) < 1) {
4693 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4694 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004695
4696 ssl->transform->in_cid_len = *p++;
4697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4699 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4700 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004703 p += ssl->transform->in_cid_len;
4704
4705 ssl->transform->out_cid_len = *p++;
4706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4708 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4709 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004712 p += ssl->transform->out_cid_len;
4713#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4714
4715 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004716 * Saved fields from top-level ssl_context structure
4717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if ((size_t) (end - p) < 4) {
4719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4720 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004721
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004722 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004723 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004724
4725#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if ((size_t) (end - p) < 16) {
4727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4728 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004729
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004730 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004731 p += 8;
4732
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004733 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004734 p += 8;
4735#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4736
4737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if ((size_t) (end - p) < 1) {
4739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4740 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004741
4742 ssl->disable_datagram_packing = *p++;
4743#endif /* MBEDTLS_SSL_PROTO_DTLS */
4744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4746 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4747 }
4748 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4749 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004750
4751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if ((size_t) (end - p) < 2) {
4753 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4754 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004757 p += 2;
4758#endif /* MBEDTLS_SSL_PROTO_DTLS */
4759
4760#if defined(MBEDTLS_SSL_ALPN)
4761 {
4762 uint8_t alpn_len;
4763 const char **cur;
4764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if ((size_t) (end - p) < 1) {
4766 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4767 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004768
4769 alpn_len = *p++;
4770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004772 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4774 if (strlen(*cur) == alpn_len &&
4775 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004776 ssl->alpn_chosen = *cur;
4777 break;
4778 }
4779 }
4780 }
4781
4782 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4784 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4785 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004786
4787 p += alpn_len;
4788 }
4789#endif /* MBEDTLS_SSL_ALPN */
4790
4791 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004792 * Forced fields from top-level ssl_context structure
4793 *
4794 * Most of them already set to the correct value by mbedtls_ssl_init() and
4795 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4796 */
4797 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004798 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004799
Hanno Becker361b10d2019-08-30 10:42:49 +01004800 /* Adjust pointers for header fields of outgoing records to
4801 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004803
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004804#if defined(MBEDTLS_SSL_PROTO_DTLS)
4805 ssl->in_epoch = 1;
4806#endif
4807
4808 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4809 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004810 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4811 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 if (ssl->handshake != NULL) {
4813 mbedtls_ssl_handshake_free(ssl);
4814 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004815 ssl->handshake = NULL;
4816 }
4817
4818 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004819 * Done - should have consumed entire buffer
4820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (p != end) {
4822 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4823 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004826}
4827
4828/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004829 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004830 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004831int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4832 const unsigned char *buf,
4833 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004834{
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (ret != 0) {
4838 mbedtls_ssl_free(context);
4839 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004842}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004843#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004844
4845/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 * Free an SSL context
4847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004848void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004849{
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004851 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004857#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4858 size_t out_buf_len = ssl->out_buf_len;
4859#else
4860 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4861#endif
4862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4864 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004865 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004866 }
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004869#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4870 size_t in_buf_len = ssl->in_buf_len;
4871#else
4872 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4873#endif
4874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4876 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004877 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004878 }
4879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (ssl->transform) {
4881 mbedtls_ssl_transform_free(ssl->transform);
4882 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004883 }
4884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (ssl->handshake) {
4886 mbedtls_ssl_handshake_free(ssl);
4887 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004888
4889#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4891 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004892#endif
4893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 mbedtls_ssl_session_free(ssl->session_negotiate);
4895 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004896 }
4897
Ronald Cron6f135e12021-12-08 16:57:54 +01004898#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 mbedtls_ssl_transform_free(ssl->transform_application);
4900 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004901#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if (ssl->session) {
4904 mbedtls_ssl_session_free(ssl->session);
4905 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004906 }
4907
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004908#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 if (ssl->hostname != NULL) {
4910 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4911 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004913#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004914
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004915#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004917#endif
4918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004920
Paul Bakker86f04f42013-02-14 11:20:09 +01004921 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004923}
4924
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004925/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004926 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004927 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004928void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004929{
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004931}
4932
Gilles Peskineae270bf2021-06-02 00:05:29 +02004933/* The selection should be the same as mbedtls_x509_crt_profile_default in
4934 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004935 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004936 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004937 * about this list.
4938 */
Brett Warrene0edc842021-08-17 09:53:13 +01004939static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004940#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004941 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004942#endif
4943#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004944 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004945#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004946#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004947 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004948#endif
4949#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004950 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004951#endif
4952#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004953 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004954#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004955#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004956 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004957#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004958#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004959 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004960#endif
4961#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004962 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004963#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004964 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004965};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004966
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03004967static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004968 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4969 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4970 0
4971};
4972
Ronald Crone68ab4f2022-10-05 12:46:29 +02004973#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004974
Jerry Yu909df7b2022-01-22 11:56:27 +08004975/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004976 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004977 * rules SHOULD be upheld.
4978 * - No duplicate entries.
4979 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004980 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004981 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004982 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004983static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004984
Valerio Setti75fba322023-02-23 17:35:09 +01004985#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004986 defined(MBEDTLS_MD_CAN_SHA256) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004987 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4988 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004989#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_MD_CAN_SHA256 &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004990 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004991
Valerio Setti75fba322023-02-23 17:35:09 +01004992#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004993 defined(MBEDTLS_MD_CAN_SHA384) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004994 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4995 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004996#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_MD_CAN_SHA384&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004997 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4998
Valerio Setti75fba322023-02-23 17:35:09 +01004999#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005000 defined(MBEDTLS_MD_CAN_SHA512) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08005001 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
5002 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005003#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_MD_CAN_SHA384&&
Jerry Yued5e9f42022-01-26 11:21:34 +08005004 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005005
Gilles Peskine449bd832023-01-11 14:50:10 +01005006#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005007 defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005008 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01005009#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005010 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005013 defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005014 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01005015#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005016 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005019 defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005020 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005021#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005022 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005023
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005024#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005025 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005026#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005027
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005028#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005029 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005030#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005031
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005032#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005033 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005034#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005035
Gabor Mezei15b95a62022-05-09 16:37:58 +02005036 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005037};
5038
5039/* NOTICE: see above */
5040#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5041static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005042#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Setti75fba322023-02-23 17:35:09 +01005043#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005045#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005046#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5047 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5048#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005049#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005051#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005052#endif /* MBEDTLS_MD_CAN_SHA512*/
5053#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Setti75fba322023-02-23 17:35:09 +01005054#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005056#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005057#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5058 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5059#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005060#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005062#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005063#endif /* MBEDTLS_MD_CAN_SHA384*/
5064#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Setti75fba322023-02-23 17:35:09 +01005065#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005067#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005068#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5069 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5070#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005071#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005073#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005074#endif /* MBEDTLS_MD_CAN_SHA256*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005075 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005076};
5077#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5078/* NOTICE: see above */
5079static uint16_t ssl_preset_suiteb_sig_algs[] = {
5080
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005081#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_MD_CAN_SHA256) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08005082 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5083 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005084#endif /* MBEDTLS_ECDSA_C && MBEDTLS_MD_CAN_SHA256&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005085 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5086
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005087#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_MD_CAN_SHA384) && \
Jerry Yu53037892022-01-25 11:02:06 +08005088 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5089 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005090#endif /* MBEDTLS_ECDSA_C && MBEDTLS_MD_CAN_SHA384&&
Jerry Yu53037892022-01-25 11:02:06 +08005091 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005092
Gilles Peskine449bd832023-01-11 14:50:10 +01005093#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005094 defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu909df7b2022-01-22 11:56:27 +08005095 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005096#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005097 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08005098
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005099#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu53037892022-01-25 11:02:06 +08005100 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005101#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256*/
Jerry Yu53037892022-01-25 11:02:06 +08005102
Gabor Mezei15b95a62022-05-09 16:37:58 +02005103 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005104};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005105
Jerry Yu909df7b2022-01-22 11:56:27 +08005106/* NOTICE: see above */
5107#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5108static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005109#if defined(MBEDTLS_MD_CAN_SHA256)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005110#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005112#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005113#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005115#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005116#endif /* MBEDTLS_MD_CAN_SHA256*/
5117#if defined(MBEDTLS_MD_CAN_SHA384)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005118#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005120#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005121#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005123#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005124#endif /* MBEDTLS_MD_CAN_SHA256*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005125 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005126};
Jerry Yu909df7b2022-01-22 11:56:27 +08005127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5128
Ronald Crone68ab4f2022-10-05 12:46:29 +02005129#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005130
Brett Warrene0edc842021-08-17 09:53:13 +01005131static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005132#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005133 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005134#endif
5135#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005136 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005137#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005138 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005139};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005140
Ronald Crone68ab4f2022-10-05 12:46:29 +02005141#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005142/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005143 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005144MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005145static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005146{
5147 size_t i, j;
5148 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5151 for (j = 0; j < i; j++) {
5152 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005153 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 }
5155 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5156 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5157 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005158 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005159 }
5160 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005162}
5163
Ronald Crone68ab4f2022-10-05 12:46:29 +02005164#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005165
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005166/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005167 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005169int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5170 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005171{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005172#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005174#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005175
Ronald Crone68ab4f2022-10-05 12:46:29 +02005176#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5178 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5179 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005180 }
5181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5183 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5184 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005185 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005186
5187#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5189 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5190 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005191 }
5192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5194 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5195 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005196 }
5197#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005198#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005199
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005200 /* Use the functions here so that they are covered in tests,
5201 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 mbedtls_ssl_conf_endpoint(conf, endpoint);
5203 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005204
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005205 /*
5206 * Things that are common to all presets
5207 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005208#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005210 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5211#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5212 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5213#endif
5214 }
5215#endif
5216
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005217#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5218 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5219#endif
5220
5221#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5222 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5223#endif
5224
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005225#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005226 conf->f_cookie_write = ssl_cookie_write_dummy;
5227 conf->f_cookie_check = ssl_cookie_check_dummy;
5228#endif
5229
5230#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5231 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5232#endif
5233
Janos Follath088ce432017-04-10 12:42:31 +01005234#if defined(MBEDTLS_SSL_SRV_C)
5235 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005236 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005237#endif
5238
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005239#if defined(MBEDTLS_SSL_PROTO_DTLS)
5240 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5241 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5242#endif
5243
5244#if defined(MBEDTLS_SSL_RENEGOTIATION)
5245 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 memset(conf->renego_period, 0x00, 2);
5247 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005248#endif
5249
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005250#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005252 const unsigned char dhm_p[] =
5253 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5254 const unsigned char dhm_g[] =
5255 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5258 dhm_p, sizeof(dhm_p),
5259 dhm_g, sizeof(dhm_g))) != 0) {
5260 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005261 }
5262 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005263#endif
5264
Ronald Cron6f135e12021-12-08 16:57:54 +01005265#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005266
5267#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005269#if defined(MBEDTLS_SSL_SRV_C)
5270 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005272#endif
5273#endif /* MBEDTLS_SSL_EARLY_DATA */
5274
Jerry Yud0766ec2022-09-22 10:46:57 +08005275#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005276 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005278#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005279 /*
5280 * Allow all TLS 1.3 key exchange modes by default.
5281 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005282 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005283#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005286#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005287 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005288 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005289#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005291#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005293#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005294 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5295 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005296#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5297 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5298 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5299#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5300 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5301 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5302#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005304#endif
5305 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005306
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005307 /*
5308 * Preset-specific defaults
5309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005311 /*
5312 * NSA Suite B
5313 */
5314 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005315
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005316 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005317
5318#if defined(MBEDTLS_X509_CRT_PARSE_C)
5319 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005320#endif
5321
Ronald Crone68ab4f2022-10-05 12:46:29 +02005322#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005323#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005325 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005327#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005329#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005330
Brett Warrene0edc842021-08-17 09:53:13 +01005331#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5332 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005333#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005334 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005335 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005336
5337 /*
5338 * Default
5339 */
5340 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005341
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005342 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005343
5344#if defined(MBEDTLS_X509_CRT_PARSE_C)
5345 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5346#endif
5347
Ronald Crone68ab4f2022-10-05 12:46:29 +02005348#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005349#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005351 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005353#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005355#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005356
Brett Warrene0edc842021-08-17 09:53:13 +01005357#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5358 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005359#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005360 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005361
5362#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5363 conf->dhm_min_bitlen = 1024;
5364#endif
5365 }
5366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005368}
5369
5370/*
5371 * Free mbedtls_ssl_config
5372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005373void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005374{
5375#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 mbedtls_mpi_free(&conf->dhm_P);
5377 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005378#endif
5379
Ronald Cron73fe8df2022-10-05 14:31:43 +02005380#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005381#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005383 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5384 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005385#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 if (conf->psk != NULL) {
5387 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5388 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005389 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005390 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005391 }
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 if (conf->psk_identity != NULL) {
5394 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5395 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005396 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005397 conf->psk_identity_len = 0;
5398 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005399#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005400
5401#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005403#endif
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005406}
5407
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005408#if defined(MBEDTLS_PK_C) && \
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005409 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_CAN_ECDSA_SOME))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005410/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5417 return MBEDTLS_SSL_SIG_RSA;
5418 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005419#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005420#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5422 return MBEDTLS_SSL_SIG_ECDSA;
5423 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005424#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005426}
5427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005429{
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005431 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005433 case MBEDTLS_PK_ECDSA:
5434 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005436 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005438 }
5439}
5440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005442{
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444#if defined(MBEDTLS_RSA_C)
5445 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005447#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005448#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005451#endif
5452 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005454 }
5455}
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005456#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_PK_CAN_ECDSA_SOME ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005457
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005458/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005459 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005462{
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005464#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005467#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005468#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005471#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005472#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005475#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005476#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005479#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005480#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005483#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005484#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005487#endif
5488 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005490 }
5491}
5492
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005493/*
5494 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005496unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005497{
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005499#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005500 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005502#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005503#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005504 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005506#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005507#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005508 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005510#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005511#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005512 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005514#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005515#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005516 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005518#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005519#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005520 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005522#endif
5523 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005525 }
5526}
5527
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005528/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005529 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005530 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005531 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005532int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005533{
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 if (group_list == NULL) {
5537 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005538 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 for (; *group_list != 0; group_list++) {
5541 if (*group_list == tls_id) {
5542 return 0;
5543 }
5544 }
5545
5546 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005547}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005548
Valerio Settid4a5d462023-04-05 18:19:01 +02005549#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005550/*
5551 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005553int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005554{
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005558 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005562}
Valerio Settid4a5d462023-04-05 18:19:01 +02005563#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005566#define EC_NAME(_name_) _name_
5567#else
5568#define EC_NAME(_name_) NULL
5569#endif
5570
Valerio Setti18c9fed2022-12-30 17:44:24 +01005571static const struct {
5572 uint16_t tls_id;
5573 mbedtls_ecp_group_id ecp_group_id;
5574 psa_ecc_family_t psa_family;
5575 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005577} tls_id_match_table[] =
5578{
5579#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581#endif
5582#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005585#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005588#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005591#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593#endif
5594#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005596#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005597#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599#endif
5600#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005602#endif
5603#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005605#endif
5606#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005608#endif
5609#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005611#endif
5612#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005614#endif
5615#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005617#endif
5618 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5619};
5620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5622 psa_ecc_family_t *family,
5623 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005624{
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5626 if (tls_id_match_table[i].tls_id == tls_id) {
5627 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 }
5630 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005631 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005633 return PSA_SUCCESS;
5634 }
5635 }
5636
5637 return PSA_ERROR_NOT_SUPPORTED;
5638}
5639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005641{
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5643 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005644 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005646 }
5647
5648 return MBEDTLS_ECP_DP_NONE;
5649}
5650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005652{
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5654 i++) {
5655 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005656 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005658 }
5659
5660 return 0;
5661}
5662
Valerio Setti67419f02023-01-04 16:12:42 +01005663#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005664const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005665{
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5667 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005668 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005670 }
5671
5672 return NULL;
5673}
Valerio Setti67419f02023-01-04 16:12:42 +01005674#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005677int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5678 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5679 int cert_endpoint,
5680 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005681{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005682 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005683 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005684 const char *ext_oid;
5685 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005688 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 case MBEDTLS_KEY_EXCHANGE_RSA:
5691 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005692 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005693 break;
5694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5696 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5697 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5698 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005699 break;
5700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5702 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005703 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005704 break;
5705
5706 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 case MBEDTLS_KEY_EXCHANGE_NONE:
5708 case MBEDTLS_KEY_EXCHANGE_PSK:
5709 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5710 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005711 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005712 usage = 0;
5713 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5716 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005717 }
5718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005720 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005721 ret = -1;
5722 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5727 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005730 }
5731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005733 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005734 ret = -1;
5735 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005738}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005740
Jerry Yu148165c2021-09-24 23:20:59 +08005741#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005742int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5743 const mbedtls_md_type_t md,
5744 unsigned char *dst,
5745 size_t dst_len,
5746 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005747{
Ronald Cronf6893e12022-01-07 22:09:01 +01005748 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5749 psa_hash_operation_t *hash_operation_to_clone;
5750 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5751
Jerry Yu148165c2021-09-24 23:20:59 +08005752 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005755#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 case MBEDTLS_MD_SHA384:
5757 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5758 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005759#endif
5760
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005761#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 case MBEDTLS_MD_SHA256:
5763 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5764 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005765#endif
5766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 default:
5768 goto exit;
5769 }
5770
5771 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5772 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005773 goto exit;
5774 }
5775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5777 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005780
5781exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005782#if !defined(MBEDTLS_MD_CAN_SHA384) && \
5783 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005784 (void) ssl;
5785#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005786 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005787}
5788#else /* MBEDTLS_USE_PSA_CRYPTO */
5789
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005790#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005792static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5793 unsigned char *dst,
5794 size_t dst_len,
5795 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005796{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005797 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005798 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 if (dst_len < 48) {
5801 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5802 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005803
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005804 mbedtls_md_init(&sha384);
5805 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005806 if (ret != 0) {
5807 goto exit;
5808 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005809 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005810 if (ret != 0) {
5811 goto exit;
5812 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005813
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005814 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005815 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005816 goto exit;
5817 }
5818
5819 *olen = 48;
5820
5821exit:
5822
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005823 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005825}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005826#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005827
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005828#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005829MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005830static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5831 unsigned char *dst,
5832 size_t dst_len,
5833 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005834{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005835 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005836 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 if (dst_len < 32) {
5839 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5840 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005841
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005842 mbedtls_md_init(&sha256);
5843 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5844 if (ret != 0) {
5845 goto exit;
5846 }
5847 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5848 if (ret != 0) {
5849 goto exit;
5850 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005851
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005852 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5853 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005854 goto exit;
5855 }
5856
5857 *olen = 32;
5858
5859exit:
5860
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005861 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005863}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005864#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5867 const mbedtls_md_type_t md,
5868 unsigned char *dst,
5869 size_t dst_len,
5870 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005871{
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005873
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005874#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 case MBEDTLS_MD_SHA384:
5876 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005877#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005878
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005879#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 case MBEDTLS_MD_SHA256:
5881 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005882#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005885#if !defined(MBEDTLS_MD_CAN_SHA384) && \
5886 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 (void) ssl;
5888 (void) dst;
5889 (void) dst_len;
5890 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005891#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005893 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005895}
XiaokangQian647719a2021-12-07 09:16:29 +00005896
Jerry Yu148165c2021-09-24 23:20:59 +08005897#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005898
Ronald Crone68ab4f2022-10-05 12:46:29 +02005899#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005900/* mbedtls_ssl_parse_sig_alg_ext()
5901 *
5902 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5903 * value (TLS 1.3 RFC8446):
5904 * enum {
5905 * ....
5906 * ecdsa_secp256r1_sha256( 0x0403 ),
5907 * ecdsa_secp384r1_sha384( 0x0503 ),
5908 * ecdsa_secp521r1_sha512( 0x0603 ),
5909 * ....
5910 * } SignatureScheme;
5911 *
5912 * struct {
5913 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5914 * } SignatureSchemeList;
5915 *
5916 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5917 * value (TLS 1.2 RFC5246):
5918 * enum {
5919 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5920 * sha512(6), (255)
5921 * } HashAlgorithm;
5922 *
5923 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5924 * SignatureAlgorithm;
5925 *
5926 * struct {
5927 * HashAlgorithm hash;
5928 * SignatureAlgorithm signature;
5929 * } SignatureAndHashAlgorithm;
5930 *
5931 * SignatureAndHashAlgorithm
5932 * supported_signature_algorithms<2..2^16-2>;
5933 *
5934 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5935 * generalization of the TLS 1.2 signature algorithm extension.
5936 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5937 * `SignatureScheme` field of TLS 1.3
5938 *
5939 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005940int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5941 const unsigned char *buf,
5942 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005943{
5944 const unsigned char *p = buf;
5945 size_t supported_sig_algs_len = 0;
5946 const unsigned char *supported_sig_algs_end;
5947 uint16_t sig_alg;
5948 uint32_t common_idx = 0;
5949
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5951 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005952 p += 2;
5953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 memset(ssl->handshake->received_sig_algs, 0,
5955 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005958 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 while (p < supported_sig_algs_end) {
5960 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5961 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005962 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5964 sig_alg,
5965 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005966#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5968 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5969 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005970 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005971 }
5972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5975 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005978 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5979 common_idx += 1;
5980 }
5981 }
5982 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (p != end) {
5984 MBEDTLS_SSL_DEBUG_MSG(1,
5985 ("Signature algorithms extension length misaligned"));
5986 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5987 MBEDTLS_ERR_SSL_DECODE_ERROR);
5988 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005989 }
5990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (common_idx == 0) {
5992 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5993 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5994 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5995 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005996 }
5997
Gabor Mezei15b95a62022-05-09 16:37:58 +02005998 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006000}
6001
Ronald Crone68ab4f2022-10-05 12:46:29 +02006002#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006003
Jerry Yudc7bd172022-02-17 13:44:15 +08006004#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6005
6006#if defined(MBEDTLS_USE_PSA_CRYPTO)
6007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6009 mbedtls_svc_key_id_t key,
6010 psa_algorithm_t alg,
6011 const unsigned char *raw_psk, size_t raw_psk_length,
6012 const unsigned char *seed, size_t seed_length,
6013 const unsigned char *label, size_t label_length,
6014 const unsigned char *other_secret,
6015 size_t other_secret_length,
6016 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006017{
6018 psa_status_t status;
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 status = psa_key_derivation_setup(derivation, alg);
6021 if (status != PSA_SUCCESS) {
6022 return status;
6023 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6026 status = psa_key_derivation_input_bytes(derivation,
6027 PSA_KEY_DERIVATION_INPUT_SEED,
6028 seed, seed_length);
6029 if (status != PSA_SUCCESS) {
6030 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006031 }
6032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (other_secret != NULL) {
6034 status = psa_key_derivation_input_bytes(derivation,
6035 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6036 other_secret, other_secret_length);
6037 if (status != PSA_SUCCESS) {
6038 return status;
6039 }
6040 }
6041
6042 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006043 status = psa_key_derivation_input_bytes(
6044 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 raw_psk, raw_psk_length);
6046 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006047 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006049 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (status != PSA_SUCCESS) {
6051 return status;
6052 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 status = psa_key_derivation_input_bytes(derivation,
6055 PSA_KEY_DERIVATION_INPUT_LABEL,
6056 label, label_length);
6057 if (status != PSA_SUCCESS) {
6058 return status;
6059 }
6060 } else {
6061 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006062 }
6063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 status = psa_key_derivation_set_capacity(derivation, capacity);
6065 if (status != PSA_SUCCESS) {
6066 return status;
6067 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006070}
6071
Andrzej Kurek57d10632022-10-24 10:32:01 -04006072#if defined(PSA_WANT_ALG_SHA_384) || \
6073 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006075static int tls_prf_generic(mbedtls_md_type_t md_type,
6076 const unsigned char *secret, size_t slen,
6077 const char *label,
6078 const unsigned char *random, size_t rlen,
6079 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006080{
6081 psa_status_t status;
6082 psa_algorithm_t alg;
6083 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6084 psa_key_derivation_operation_t derivation =
6085 PSA_KEY_DERIVATION_OPERATION_INIT;
6086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006088 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006090 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006092
6093 /* Normally a "secret" should be long enough to be impossible to
6094 * find by brute force, and in particular should not be empty. But
6095 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6096 * and for this use case it makes sense to have a 0-length "secret".
6097 * Since the key API doesn't allow importing a key of length 0,
6098 * keep master_key=0, which setup_psa_key_derivation() understands
6099 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006101 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6103 psa_set_key_algorithm(&key_attributes, alg);
6104 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6107 if (status != PSA_SUCCESS) {
6108 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6109 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006110 }
6111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 status = setup_psa_key_derivation(&derivation,
6113 master_key, alg,
6114 NULL, 0,
6115 random, rlen,
6116 (unsigned char const *) label,
6117 (size_t) strlen(label),
6118 NULL, 0,
6119 dlen);
6120 if (status != PSA_SUCCESS) {
6121 psa_key_derivation_abort(&derivation);
6122 psa_destroy_key(master_key);
6123 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006124 }
6125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6127 if (status != PSA_SUCCESS) {
6128 psa_key_derivation_abort(&derivation);
6129 psa_destroy_key(master_key);
6130 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006131 }
6132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 status = psa_key_derivation_abort(&derivation);
6134 if (status != PSA_SUCCESS) {
6135 psa_destroy_key(master_key);
6136 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006137 }
6138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (!mbedtls_svc_key_id_is_null(master_key)) {
6140 status = psa_destroy_key(master_key);
6141 }
6142 if (status != PSA_SUCCESS) {
6143 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6144 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006147}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006148#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006149#else /* MBEDTLS_USE_PSA_CRYPTO */
6150
Andrzej Kurek57d10632022-10-24 10:32:01 -04006151#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006152 (defined(MBEDTLS_MD_CAN_SHA256) || \
6153 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006154MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006155static int tls_prf_generic(mbedtls_md_type_t md_type,
6156 const unsigned char *secret, size_t slen,
6157 const char *label,
6158 const unsigned char *random, size_t rlen,
6159 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006160{
6161 size_t nb;
6162 size_t i, j, k, md_len;
6163 unsigned char *tmp;
6164 size_t tmp_len = 0;
6165 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6166 const mbedtls_md_info_t *md_info;
6167 mbedtls_md_context_t md_ctx;
6168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6173 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6174 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 tmp_len = md_len + strlen(label) + rlen;
6179 tmp = mbedtls_calloc(1, tmp_len);
6180 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006181 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6182 goto exit;
6183 }
6184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 nb = strlen(label);
6186 memcpy(tmp + md_len, label, nb);
6187 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006188 nb += rlen;
6189
6190 /*
6191 * Compute P_<hash>(secret, label + random)[0..dlen]
6192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6198 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006199 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 }
6201 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6202 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 }
6205 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6206 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 for (i = 0; i < dlen; i += md_len) {
6211 ret = mbedtls_md_hmac_reset(&md_ctx);
6212 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 }
6215 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6216 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 }
6219 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6220 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 ret = mbedtls_md_hmac_reset(&md_ctx);
6225 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 }
6228 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6229 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 }
6232 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6233 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006240 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006242 }
6243
6244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 if (tmp != NULL) {
6248 mbedtls_platform_zeroize(tmp, tmp_len);
6249 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006256}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006257#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006258#endif /* MBEDTLS_USE_PSA_CRYPTO */
6259
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006260#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006261MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006262static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6263 const char *label,
6264 const unsigned char *random, size_t rlen,
6265 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006266{
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6268 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006269}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006270#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006271
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006272#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006274static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6275 const char *label,
6276 const unsigned char *random, size_t rlen,
6277 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006278{
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6280 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006281}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006282#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006283
Jerry Yuf009d862022-02-17 14:01:37 +08006284/*
6285 * Set appropriate PRF function and other SSL / TLS1.2 functions
6286 *
6287 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006288 * - hash associated with the ciphersuite (only used by TLS 1.2)
6289 *
6290 * Outputs:
6291 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6292 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006294static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6295 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006296{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006297#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006299 handshake->tls_prf = tls_prf_sha384;
6300 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6301 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006303#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006304#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006305 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006306 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006307 handshake->tls_prf = tls_prf_sha256;
6308 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6309 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6310 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006311#else
Jerry Yuf009d862022-02-17 14:01:37 +08006312 {
Jerry Yuf009d862022-02-17 14:01:37 +08006313 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006314 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006316 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006317#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006320}
Jerry Yud6ab2352022-02-17 14:03:43 +08006321
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006322/*
6323 * Compute master secret if needed
6324 *
6325 * Parameters:
6326 * [in/out] handshake
6327 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6328 * (PSA-PSK) ciphersuite_info, psk_opaque
6329 * [out] premaster (cleared)
6330 * [out] master
6331 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6332 * debug: conf->f_dbg, conf->p_dbg
6333 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006334 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006335 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006337static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6338 unsigned char *master,
6339 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006340{
6341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6342
6343 /* cf. RFC 5246, Section 8.1:
6344 * "The master secret is always exactly 48 bytes in length." */
6345 size_t const master_secret_len = 48;
6346
6347#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6348 unsigned char session_hash[48];
6349#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6350
6351 /* The label for the KDF used for key expansion.
6352 * This is either "master secret" or "extended master secret"
6353 * depending on whether the Extended Master Secret extension
6354 * is used. */
6355 char const *lbl = "master secret";
6356
Przemek Stekielae4ed302022-04-05 17:15:55 +02006357 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006358 * - If the Extended Master Secret extension is not used,
6359 * this is ClientHello.Random + ServerHello.Random
6360 * (see Sect. 8.1 in RFC 5246).
6361 * - If the Extended Master Secret extension is used,
6362 * this is the transcript of the handshake so far.
6363 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006364 unsigned char const *seed = handshake->randbytes;
6365 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006366
6367#if !defined(MBEDTLS_DEBUG_C) && \
6368 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6369 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006371 ssl = NULL; /* make sure we don't use it except for those cases */
6372 (void) ssl;
6373#endif
6374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (handshake->resume != 0) {
6376 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6377 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006378 }
6379
6380#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006382 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006383 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006384 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6385 if (ret != 0) {
6386 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6387 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6390 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006391 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006392#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006393
Przemek Stekiel99114f32022-04-22 11:20:09 +02006394#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006395 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006397 /* Perform PSK-to-MS expansion in a single step. */
6398 psa_status_t status;
6399 psa_algorithm_t alg;
6400 mbedtls_svc_key_id_t psk;
6401 psa_key_derivation_operation_t derivation =
6402 PSA_KEY_DERIVATION_OPERATION_INIT;
6403 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006410 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006412 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006414
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006415 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006419 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006420 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006421 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006422 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006423 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006424 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006425 other_secret_len = 48;
6426 other_secret = handshake->premaster + 2;
6427 break;
6428 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006429 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006430 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6431 other_secret = handshake->premaster + 2;
6432 break;
6433 default:
6434 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006435 }
6436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 status = setup_psa_key_derivation(&derivation, psk, alg,
6438 ssl->conf->psk, ssl->conf->psk_len,
6439 seed, seed_len,
6440 (unsigned char const *) lbl,
6441 (size_t) strlen(lbl),
6442 other_secret, other_secret_len,
6443 master_secret_len);
6444 if (status != PSA_SUCCESS) {
6445 psa_key_derivation_abort(&derivation);
6446 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006447 }
6448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 status = psa_key_derivation_output_bytes(&derivation,
6450 master,
6451 master_secret_len);
6452 if (status != PSA_SUCCESS) {
6453 psa_key_derivation_abort(&derivation);
6454 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006455 }
6456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 status = psa_key_derivation_abort(&derivation);
6458 if (status != PSA_SUCCESS) {
6459 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6460 }
6461 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006462#endif
6463 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006464#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6466 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006467 psa_status_t status;
6468 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6469 psa_key_derivation_operation_t derivation =
6470 PSA_KEY_DERIVATION_OPERATION_INIT;
6471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006473
6474 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 status = psa_key_derivation_setup(&derivation, alg);
6477 if (status != PSA_SUCCESS) {
6478 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006479 }
6480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 status = psa_key_derivation_set_capacity(&derivation,
6482 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6483 if (status != PSA_SUCCESS) {
6484 psa_key_derivation_abort(&derivation);
6485 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006486 }
6487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6489 &derivation);
6490 if (status != PSA_SUCCESS) {
6491 psa_key_derivation_abort(&derivation);
6492 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006493 }
6494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 status = psa_key_derivation_output_bytes(&derivation,
6496 handshake->premaster,
6497 handshake->pmslen);
6498 if (status != PSA_SUCCESS) {
6499 psa_key_derivation_abort(&derivation);
6500 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6501 }
6502
6503 status = psa_key_derivation_abort(&derivation);
6504 if (status != PSA_SUCCESS) {
6505 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006506 }
6507 }
6508#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6510 lbl, seed, seed_len,
6511 master,
6512 master_secret_len);
6513 if (ret != 0) {
6514 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6515 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006516 }
6517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6519 handshake->premaster,
6520 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 mbedtls_platform_zeroize(handshake->premaster,
6523 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006524 }
6525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006527}
6528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006530{
6531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6532 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6533 ssl->handshake->ciphersuite_info;
6534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006536
6537 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 ret = ssl_set_handshake_prfs(ssl->handshake,
6539 ciphersuite_info->mac);
6540 if (ret != 0) {
6541 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6542 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006543 }
6544
6545 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 ret = ssl_compute_master(ssl->handshake,
6547 ssl->session_negotiate->master,
6548 ssl);
6549 if (ret != 0) {
6550 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6551 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006552 }
6553
6554 /* Swap the client and server random values:
6555 * - MS derivation wanted client+server (RFC 5246 8.1)
6556 * - key derivation wants server+client (RFC 5246 6.3) */
6557 {
6558 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 memcpy(tmp, ssl->handshake->randbytes, 64);
6560 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6561 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6562 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006563 }
6564
6565 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6567 ssl->session_negotiate->ciphersuite,
6568 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006569#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006571#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 ssl->handshake->tls_prf,
6573 ssl->handshake->randbytes,
6574 ssl->tls_version,
6575 ssl->conf->endpoint,
6576 ssl);
6577 if (ret != 0) {
6578 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6579 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006580 }
6581
6582 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6584 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006589}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006592{
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006594#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006595 case MBEDTLS_SSL_HASH_SHA384:
6596 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6597 break;
6598#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006599#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006600 case MBEDTLS_SSL_HASH_SHA256:
6601 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6602 break;
6603#endif
6604 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006606 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006607#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6608 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006609 (void) ssl;
6610#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006612}
6613
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006614#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006615int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6616 unsigned char *hash,
6617 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006618{
6619#if defined(MBEDTLS_USE_PSA_CRYPTO)
6620 size_t hash_size;
6621 psa_status_t status;
6622 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6625 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6626 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006627 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006628 }
6629
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6631 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006632 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006633 }
6634
6635 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6637 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006638
6639exit:
6640 psa_hash_abort(&sha256_psa);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02006641 return mbedtls_md_error_from_psa(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006642#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006644 mbedtls_md_context_t sha256;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006645
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006646 mbedtls_md_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006649
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006650 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6651 if (ret != 0) {
6652 goto exit;
6653 }
6654 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6655 if (ret != 0) {
6656 goto exit;
6657 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006658
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006659 ret = mbedtls_md_finish(&sha256, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006660 if (ret != 0) {
6661 goto exit;
6662 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006663
6664 *hlen = 32;
6665
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6667 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006668
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006669exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006670 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006671 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006672#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006673}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006674#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006675
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006676#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006677int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6678 unsigned char *hash,
6679 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006680{
6681#if defined(MBEDTLS_USE_PSA_CRYPTO)
6682 size_t hash_size;
6683 psa_status_t status;
6684 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6687 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6688 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006689 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006690 }
6691
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6693 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006694 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006695 }
6696
6697 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6699 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006700
6701exit:
6702 psa_hash_abort(&sha384_psa);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02006703 return mbedtls_md_error_from_psa(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006704#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006706 mbedtls_md_context_t sha384;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006707
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006708 mbedtls_md_init(&sha384);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006709
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006711
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006712 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006713 if (ret != 0) {
6714 goto exit;
6715 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006716 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006717 if (ret != 0) {
6718 goto exit;
6719 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006720
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006721 ret = mbedtls_md_finish(&sha384, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006722 if (ret != 0) {
6723 goto exit;
6724 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006725
6726 *hlen = 48;
6727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6729 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006730
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006731exit:
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006732 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006733 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006734#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006735}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006736#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006737
Neil Armstrong80f6f322022-05-03 17:56:38 +02006738#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6739 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006740int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006741{
6742 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006744 const unsigned char *psk = NULL;
6745 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006749 /*
6750 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006751 * checked before calling this function.
6752 *
6753 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006754 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6757 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6758 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006759 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006760 }
6761
6762 /*
6763 * PMS = struct {
6764 * opaque other_secret<0..2^16-1>;
6765 * opaque psk<0..2^16-1>;
6766 * };
6767 * with "other_secret" depending on the particular key exchange
6768 */
6769#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6771 if (end - p < 2) {
6772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6773 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006774
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006776 p += 2;
6777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 if (end < p || (size_t) (end - p) < psk_len) {
6779 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6780 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006783 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006785#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6786#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006788 /*
6789 * other_secret already set by the ClientKeyExchange message,
6790 * and is 48 bytes long
6791 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 if (end - p < 2) {
6793 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6794 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006795
6796 *p++ = 0;
6797 *p++ = 48;
6798 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006800#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6801#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6804 size_t len;
6805
6806 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6808 p + 2, end - (p + 2), &len,
6809 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6810 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6811 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006814 p += 2 + len;
6815
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6817 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006818#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006819#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6822 size_t zlen;
6823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6825 p + 2, end - (p + 2),
6826 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6827 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6828 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006829 }
6830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006832 p += 2 + zlen;
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6835 MBEDTLS_DEBUG_ECDH_Z);
6836 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006837#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006838 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6840 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006841 }
6842
6843 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 if (end - p < 2) {
6845 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6846 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006849 p += 2;
6850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 if (end < p || (size_t) (end - p) < psk_len) {
6852 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6853 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006856 p += psk_len;
6857
6858 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6859
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006861}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006862#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006863
6864#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006866static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006867
6868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006869int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006870{
6871 /* If renegotiation is not enforced, retransmit until we would reach max
6872 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006874 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6875 unsigned char doublings = 1;
6876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006878 ++doublings;
6879 ratio >>= 1;
6880 }
6881
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 if (++ssl->renego_records_seen > doublings) {
6883 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6884 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006885 }
6886 }
6887
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006889}
6890#endif
6891#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006892
Jerry Yud9526692022-02-17 14:23:47 +08006893/*
6894 * Handshake functions
6895 */
6896#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6897/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006898int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006899{
6900 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6901 ssl->handshake->ciphersuite_info;
6902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6906 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006907 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006909 }
6910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6912 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006913}
6914
Gilles Peskine449bd832023-01-11 14:50:10 +01006915int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006916{
6917 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6918 ssl->handshake->ciphersuite_info;
6919
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006921
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6923 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006924 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006926 }
6927
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6929 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006930}
6931
6932#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6933/* Some certificate support -> implement write and parse */
6934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006936{
6937 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6938 size_t i, n;
6939 const mbedtls_x509_crt *crt;
6940 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6941 ssl->handshake->ciphersuite_info;
6942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6946 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006947 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006949 }
6950
6951#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6953 if (ssl->handshake->client_auth == 0) {
6954 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006955 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006957 }
6958 }
6959#endif /* MBEDTLS_SSL_CLI_C */
6960#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6962 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006963 /* Should never happen because we shouldn't have picked the
6964 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006966 }
6967 }
6968#endif
6969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006971
6972 /*
6973 * 0 . 0 handshake type
6974 * 1 . 3 handshake length
6975 * 4 . 6 length of all certs
6976 * 7 . 9 length of cert. 1
6977 * 10 . n-1 peer certificate
6978 * n . n+2 length of cert. 2
6979 * n+3 . ... upper level cert, etc.
6980 */
6981 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006985 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6987 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6988 " > %" MBEDTLS_PRINTF_SIZET,
6989 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6990 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006991 }
6992
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6994 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6995 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006996
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006998 i += n; crt = crt->next;
6999 }
7000
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7002 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7003 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007004
7005 ssl->out_msglen = i;
7006 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7007 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7008
7009 ssl->state++;
7010
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7012 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7013 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007014 }
7015
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007017
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007019}
7020
7021#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7022
7023#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007024MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007025static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7026 unsigned char *crt_buf,
7027 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007028{
7029 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 if (peer_crt == NULL) {
7032 return -1;
7033 }
Jerry Yud9526692022-02-17 14:23:47 +08007034
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 if (peer_crt->raw.len != crt_buf_len) {
7036 return -1;
7037 }
Jerry Yud9526692022-02-17 14:23:47 +08007038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007040}
7041#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007042MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007043static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7044 unsigned char *crt_buf,
7045 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007046{
7047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7048 unsigned char const * const peer_cert_digest =
7049 ssl->session->peer_cert_digest;
7050 mbedtls_md_type_t const peer_cert_digest_type =
7051 ssl->session->peer_cert_digest_type;
7052 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007054 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7055 size_t digest_len;
7056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 if (peer_cert_digest == NULL || digest_info == NULL) {
7058 return -1;
7059 }
Jerry Yud9526692022-02-17 14:23:47 +08007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 digest_len = mbedtls_md_get_size(digest_info);
7062 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7063 return -1;
7064 }
Jerry Yud9526692022-02-17 14:23:47 +08007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7067 if (ret != 0) {
7068 return -1;
7069 }
Jerry Yud9526692022-02-17 14:23:47 +08007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007072}
7073#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7074#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7075
7076/*
7077 * Once the certificate message is read, parse it into a cert chain and
7078 * perform basic checks, but leave actual verification to the caller
7079 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007080MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007081static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7082 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007083{
7084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7085#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007087#endif
7088 size_t i, n;
7089 uint8_t alert;
7090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7092 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7093 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7094 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7095 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007096 }
7097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7099 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7100 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7101 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007102 }
7103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7105 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7106 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7107 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7108 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007109 }
7110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007112
7113 /*
7114 * Same message structure as in mbedtls_ssl_write_certificate()
7115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007117
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 if (ssl->in_msg[i] != 0 ||
7119 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7120 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7121 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7122 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7123 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007124 }
7125
7126 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7127 i += 3;
7128
7129 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007131 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 if (i + 3 > ssl->in_hslen) {
7133 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7134 mbedtls_ssl_send_alert_message(ssl,
7135 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7136 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7137 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007138 }
7139 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7140 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 if (ssl->in_msg[i] != 0) {
7142 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7143 mbedtls_ssl_send_alert_message(ssl,
7144 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7145 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7146 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007147 }
7148
7149 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007151 | (unsigned int) ssl->in_msg[i + 2];
7152 i += 3;
7153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 if (n < 128 || i + n > ssl->in_hslen) {
7155 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7156 mbedtls_ssl_send_alert_message(ssl,
7157 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7158 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7159 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007160 }
7161
7162 /* Check if we're handling the first CRT in the chain. */
7163#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007165 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007167 /* During client-side renegotiation, check that the server's
7168 * end-CRTs hasn't changed compared to the initial handshake,
7169 * mitigating the triple handshake attack. On success, reuse
7170 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7172 if (ssl_check_peer_crt_unchanged(ssl,
7173 &ssl->in_msg[i],
7174 n) != 0) {
7175 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7176 mbedtls_ssl_send_alert_message(ssl,
7177 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7178 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7179 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007180 }
7181
7182 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007184 }
7185#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7186
7187 /* Parse the next certificate in the chain. */
7188#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007190#else
7191 /* If we don't need to store the CRT chain permanently, parse
7192 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007194#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007196 case 0: /*ok*/
7197 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7198 /* Ignore certificate with an unknown algorithm: maybe a
7199 prior certificate was already trusted. */
7200 break;
7201
7202 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7203 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7204 goto crt_parse_der_failed;
7205
7206 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7207 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7208 goto crt_parse_der_failed;
7209
7210 default:
7211 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007212crt_parse_der_failed:
7213 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7214 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7215 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007216 }
7217
7218 i += n;
7219 }
7220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7222 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007223}
7224
7225#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007226MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007227static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007228{
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7230 return -1;
7231 }
Jerry Yud9526692022-02-17 14:23:47 +08007232
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007234 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7235 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7237 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7238 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007239 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007241}
7242#endif /* MBEDTLS_SSL_SRV_C */
7243
7244/* Check if a certificate message is expected.
7245 * Return either
7246 * - SSL_CERTIFICATE_EXPECTED, or
7247 * - SSL_CERTIFICATE_SKIP
7248 * indicating whether a Certificate message is expected or not.
7249 */
7250#define SSL_CERTIFICATE_EXPECTED 0
7251#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007252MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007253static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7254 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007255{
7256 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7257 ssl->handshake->ciphersuite_info;
7258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7260 return SSL_CERTIFICATE_SKIP;
7261 }
Jerry Yud9526692022-02-17 14:23:47 +08007262
7263#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7265 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7266 return SSL_CERTIFICATE_SKIP;
7267 }
Jerry Yud9526692022-02-17 14:23:47 +08007268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007270 ssl->session_negotiate->verify_result =
7271 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007273 }
7274 }
7275#else
7276 ((void) authmode);
7277#endif /* MBEDTLS_SSL_SRV_C */
7278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007280}
7281
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007282MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007283static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7284 int authmode,
7285 mbedtls_x509_crt *chain,
7286 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007287{
7288 int ret = 0;
7289 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7290 ssl->handshake->ciphersuite_info;
7291 int have_ca_chain = 0;
7292
7293 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7294 void *p_vrfy;
7295
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7297 return 0;
7298 }
Jerry Yud9526692022-02-17 14:23:47 +08007299
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 if (ssl->f_vrfy != NULL) {
7301 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007302 f_vrfy = ssl->f_vrfy;
7303 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 } else {
7305 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007306 f_vrfy = ssl->conf->f_vrfy;
7307 p_vrfy = ssl->conf->p_vrfy;
7308 }
7309
7310 /*
7311 * Main check: verify certificate
7312 */
7313#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007315 ((void) rs_ctx);
7316 have_ca_chain = 1;
7317
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007319 ret = mbedtls_x509_crt_verify_with_ca_cb(
7320 chain,
7321 ssl->conf->f_ca_cb,
7322 ssl->conf->p_ca_cb,
7323 ssl->conf->cert_profile,
7324 ssl->hostname,
7325 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 f_vrfy, p_vrfy);
7327 } else
Jerry Yud9526692022-02-17 14:23:47 +08007328#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7329 {
7330 mbedtls_x509_crt *ca_chain;
7331 mbedtls_x509_crl *ca_crl;
7332
7333#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007335 ca_chain = ssl->handshake->sni_ca_chain;
7336 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 } else
Jerry Yud9526692022-02-17 14:23:47 +08007338#endif
7339 {
7340 ca_chain = ssl->conf->ca_chain;
7341 ca_crl = ssl->conf->ca_crl;
7342 }
7343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007345 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 }
Jerry Yud9526692022-02-17 14:23:47 +08007347
7348 ret = mbedtls_x509_crt_verify_restartable(
7349 chain,
7350 ca_chain, ca_crl,
7351 ssl->conf->cert_profile,
7352 ssl->hostname,
7353 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007355 }
7356
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 if (ret != 0) {
7358 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007359 }
7360
7361#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7363 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7364 }
Jerry Yud9526692022-02-17 14:23:47 +08007365#endif
7366
7367 /*
7368 * Secondary checks: always done, but change 'ret' only if it was 0
7369 */
7370
Valerio Setti6c496a12023-04-07 15:53:51 +02007371#if defined(MBEDTLS_ECP_LIGHT)
Jerry Yud9526692022-02-17 14:23:47 +08007372 {
7373 const mbedtls_pk_context *pk = &chain->pk;
7374
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007375 /* If certificate uses an EC key, make sure the curve is OK.
7376 * This is a public key, so it can't be opaque, so can_do() is a good
7377 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007379 /* and in the unlikely case the above assumption no longer holds
7380 * we are making sure that pk_ec() here does not return a NULL
7381 */
Valerio Settid0405092023-05-24 13:16:40 +02007382 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(pk);
7383 if (grp_id == MBEDTLS_ECP_DP_NONE) {
7384 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid group ID"));
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007386 }
Valerio Setti97207782023-05-18 18:59:06 +02007387 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007388 ssl->session_negotiate->verify_result |=
7389 MBEDTLS_X509_BADCERT_BAD_KEY;
7390
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7392 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007393 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007395 }
Jerry Yud9526692022-02-17 14:23:47 +08007396 }
7397 }
Valerio Setti6c496a12023-04-07 15:53:51 +02007398#endif /* MBEDTLS_ECP_LIGHT */
Jerry Yud9526692022-02-17 14:23:47 +08007399
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 if (mbedtls_ssl_check_cert_usage(chain,
7401 ciphersuite_info,
7402 !ssl->conf->endpoint,
7403 &ssl->session_negotiate->verify_result) != 0) {
7404 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7405 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007406 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 }
Jerry Yud9526692022-02-17 14:23:47 +08007408 }
7409
7410 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7411 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7412 * with details encoded in the verification flags. All other kinds
7413 * of error codes, including those from the user provided f_vrfy
7414 * functions, are treated as fatal and lead to a failure of
7415 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7417 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7418 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007419 ret = 0;
7420 }
7421
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7423 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007424 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7425 }
7426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007428 uint8_t alert;
7429
7430 /* The certificate may have been rejected for several reasons.
7431 Pick one and send the corresponding alert. Which alert to send
7432 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007434 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007436 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007438 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007442 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007444 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007446 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007448 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007450 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007452 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007454 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 }
7456 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7457 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007458 }
7459
7460#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 if (ssl->session_negotiate->verify_result != 0) {
7462 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7463 (unsigned int) ssl->session_negotiate->verify_result));
7464 } else {
7465 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007466 }
7467#endif /* MBEDTLS_DEBUG_C */
7468
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007470}
7471
7472#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007473MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007474static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7475 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007476{
7477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7478 /* Remember digest of the peer's end-CRT. */
7479 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7481 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7482 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7483 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7484 mbedtls_ssl_send_alert_message(ssl,
7485 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7486 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007487
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007489 }
7490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 ret = mbedtls_md(mbedtls_md_info_from_type(
7492 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7493 start, len,
7494 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007495
7496 ssl->session_negotiate->peer_cert_digest_type =
7497 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7498 ssl->session_negotiate->peer_cert_digest_len =
7499 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7500
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007502}
7503
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007504MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007505static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7506 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007507{
7508 unsigned char *end = start + len;
7509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7510
7511 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7513 ret = mbedtls_pk_parse_subpubkey(&start, end,
7514 &ssl->handshake->peer_pubkey);
7515 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007516 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007518 }
7519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007521}
7522#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7523
Gilles Peskine449bd832023-01-11 14:50:10 +01007524int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007525{
7526 int ret = 0;
7527 int crt_expected;
7528#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7529 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7530 ? ssl->handshake->sni_authmode
7531 : ssl->conf->authmode;
7532#else
7533 const int authmode = ssl->conf->authmode;
7534#endif
7535 void *rs_ctx = NULL;
7536 mbedtls_x509_crt *chain = NULL;
7537
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007539
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7541 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7542 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007543 goto exit;
7544 }
7545
7546#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 if (ssl->handshake->ecrs_enabled &&
7548 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007549 chain = ssl->handshake->ecrs_peer_cert;
7550 ssl->handshake->ecrs_peer_cert = NULL;
7551 goto crt_verify;
7552 }
7553#endif
7554
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007556 /* mbedtls_ssl_read_record may have sent an alert already. We
7557 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007559 goto exit;
7560 }
7561
7562#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007564 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7565
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007567 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 }
Jerry Yud9526692022-02-17 14:23:47 +08007569
7570 goto exit;
7571 }
7572#endif /* MBEDTLS_SSL_SRV_C */
7573
7574 /* Clear existing peer CRT structure in case we tried to
7575 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7579 if (chain == NULL) {
7580 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7581 sizeof(mbedtls_x509_crt)));
7582 mbedtls_ssl_send_alert_message(ssl,
7583 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7584 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007585
7586 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7587 goto exit;
7588 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007590
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 ret = ssl_parse_certificate_chain(ssl, chain);
7592 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007593 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 }
Jerry Yud9526692022-02-17 14:23:47 +08007595
7596#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007598 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 }
Jerry Yud9526692022-02-17 14:23:47 +08007600
7601crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007602 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007603 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 }
Jerry Yud9526692022-02-17 14:23:47 +08007605#endif
7606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 ret = ssl_parse_certificate_verify(ssl, authmode,
7608 chain, rs_ctx);
7609 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007610 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 }
Jerry Yud9526692022-02-17 14:23:47 +08007612
7613#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7614 {
7615 unsigned char *crt_start, *pk_start;
7616 size_t crt_len, pk_len;
7617
7618 /* We parse the CRT chain without copying, so
7619 * these pointers point into the input buffer,
7620 * and are hence still valid after freeing the
7621 * CRT chain. */
7622
7623 crt_start = chain->raw.p;
7624 crt_len = chain->raw.len;
7625
7626 pk_start = chain->pk_raw.p;
7627 pk_len = chain->pk_raw.len;
7628
7629 /* Free the CRT structures before computing
7630 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 mbedtls_x509_crt_free(chain);
7632 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007633 chain = NULL;
7634
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7636 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007637 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 }
Jerry Yud9526692022-02-17 14:23:47 +08007639
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7641 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 }
Jerry Yud9526692022-02-17 14:23:47 +08007644 }
7645#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7646 /* Pass ownership to session structure. */
7647 ssl->session_negotiate->peer_cert = chain;
7648 chain = NULL;
7649#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7650
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007652
7653exit:
7654
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007656 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 }
Jerry Yud9526692022-02-17 14:23:47 +08007658
7659#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007661 ssl->handshake->ecrs_peer_cert = chain;
7662 chain = NULL;
7663 }
7664#endif
7665
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 if (chain != NULL) {
7667 mbedtls_x509_crt_free(chain);
7668 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007669 }
7670
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007672}
7673#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7674
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007675#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007676static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007678{
7679 int len = 12;
7680 const char *sender;
7681 unsigned char padbuf[32];
7682#if defined(MBEDTLS_USE_PSA_CRYPTO)
7683 size_t hash_size;
7684 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7685 psa_status_t status;
7686#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007688 mbedtls_md_context_t sha256;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007689#endif
7690
7691 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007693 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007694 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007695
Gilles Peskine449bd832023-01-11 14:50:10 +01007696 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007697 ? "client finished"
7698 : "server finished";
7699
7700#if defined(MBEDTLS_USE_PSA_CRYPTO)
7701 sha256_psa = psa_hash_operation_init();
7702
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007704
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7706 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007707 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007708 }
7709
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7711 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007712 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007713 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007714 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007715#else
7716
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007717 mbedtls_md_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007718
Gilles Peskine449bd832023-01-11 14:50:10 +01007719 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007720
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007721 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
7722 if (ret != 0) {
7723 goto exit;
7724 }
7725 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
7726 if (ret != 0) {
7727 goto exit;
7728 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007729
7730 /*
7731 * TLSv1.2:
7732 * hash = PRF( master, finished_label,
7733 * Hash( handshake ) )[0.11]
7734 */
7735
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007736 ret = mbedtls_md_finish(&sha256, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007737 if (ret != 0) {
7738 goto exit;
7739 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007740#endif /* MBEDTLS_USE_PSA_CRYPTO */
7741
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007742 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7743
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 ssl->handshake->tls_prf(session->master, 48, sender,
7745 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007746
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007748
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007750
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007752
7753exit:
7754#if defined(MBEDTLS_USE_PSA_CRYPTO)
7755 psa_hash_abort(&sha256_psa);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02007756 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007757#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007758 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007759 return ret;
7760#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007761}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007762#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007763
Jerry Yub7ba49e2022-02-17 14:25:53 +08007764
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007765#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007766static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007767 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007768{
7769 int len = 12;
7770 const char *sender;
7771 unsigned char padbuf[48];
7772#if defined(MBEDTLS_USE_PSA_CRYPTO)
7773 size_t hash_size;
7774 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7775 psa_status_t status;
7776#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007778 mbedtls_md_context_t sha384;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007779#endif
7780
7781 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007783 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007785
Gilles Peskine449bd832023-01-11 14:50:10 +01007786 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007787 ? "client finished"
7788 : "server finished";
7789
7790#if defined(MBEDTLS_USE_PSA_CRYPTO)
7791 sha384_psa = psa_hash_operation_init();
7792
Gilles Peskine449bd832023-01-11 14:50:10 +01007793 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007794
Gilles Peskine449bd832023-01-11 14:50:10 +01007795 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7796 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007797 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007798 }
7799
Gilles Peskine449bd832023-01-11 14:50:10 +01007800 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7801 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007802 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007803 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007805#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007806 mbedtls_md_init(&sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007807
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007809
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007810 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007811 if (ret != 0) {
7812 goto exit;
7813 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007814 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007815 if (ret != 0) {
7816 goto exit;
7817 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007818
7819 /*
7820 * TLSv1.2:
7821 * hash = PRF( master, finished_label,
7822 * Hash( handshake ) )[0.11]
7823 */
7824
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007825 ret = mbedtls_md_finish(&sha384, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007826 if (ret != 0) {
7827 goto exit;
7828 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007829#endif
7830
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007831 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7832
Gilles Peskine449bd832023-01-11 14:50:10 +01007833 ssl->handshake->tls_prf(session->master, 48, sender,
7834 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007835
Gilles Peskine449bd832023-01-11 14:50:10 +01007836 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007837
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007839
Gilles Peskine449bd832023-01-11 14:50:10 +01007840 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007841
7842exit:
7843#if defined(MBEDTLS_USE_PSA_CRYPTO)
7844 psa_hash_abort(&sha384_psa);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02007845 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007846#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007847 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007848 return ret;
7849#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007850}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007851#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007852
Gilles Peskine449bd832023-01-11 14:50:10 +01007853void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007854{
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007856
7857 /*
7858 * Free our handshake params
7859 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 mbedtls_ssl_handshake_free(ssl);
7861 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007862 ssl->handshake = NULL;
7863
7864 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007865 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007867 if (ssl->transform) {
7868 mbedtls_ssl_transform_free(ssl->transform);
7869 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007870 }
7871 ssl->transform = ssl->transform_negotiate;
7872 ssl->transform_negotiate = NULL;
7873
Gilles Peskine449bd832023-01-11 14:50:10 +01007874 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007875}
7876
Gilles Peskine449bd832023-01-11 14:50:10 +01007877void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007878{
7879 int resume = ssl->handshake->resume;
7880
Gilles Peskine449bd832023-01-11 14:50:10 +01007881 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007882
7883#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007885 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7886 ssl->renego_records_seen = 0;
7887 }
7888#endif
7889
7890 /*
7891 * Free the previous session and switch in the current one
7892 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007894#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7895 /* RFC 7366 3.1: keep the EtM state */
7896 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007898#endif
7899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 mbedtls_ssl_session_free(ssl->session);
7901 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007902 }
7903 ssl->session = ssl->session_negotiate;
7904 ssl->session_negotiate = NULL;
7905
7906 /*
7907 * Add cache entry
7908 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007910 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 resume == 0) {
7912 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7913 ssl->session->id,
7914 ssl->session->id_len,
7915 ssl->session) != 0) {
7916 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7917 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007918 }
7919
7920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7922 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007923 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007925
7926 /* Keep last flight around in case we need to resend it:
7927 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7929 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007930#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007932
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007933 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007934
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007936}
7937
Gilles Peskine449bd832023-01-11 14:50:10 +01007938int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007939{
7940 int ret, hash_len;
7941
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007943
Gilles Peskine449bd832023-01-11 14:50:10 +01007944 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007945
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007946 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7947 if (ret != 0) {
7948 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7949 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007950
7951 /*
7952 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7953 * may define some other value. Currently (early 2016), no defined
7954 * ciphersuite does this (and this is unlikely to change as activity has
7955 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7956 */
7957 hash_len = 12;
7958
7959#if defined(MBEDTLS_SSL_RENEGOTIATION)
7960 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007962#endif
7963
7964 ssl->out_msglen = 4 + hash_len;
7965 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7966 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7967
7968 /*
7969 * In case of session resuming, invert the client and server
7970 * ChangeCipherSpec messages order.
7971 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007973#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007974 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007975 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007977#endif
7978#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007980 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007982#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007984 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007986
7987 /*
7988 * Switch to our negotiated transform and session parameters for outbound
7989 * data.
7990 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007991 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007992
7993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007995 unsigned char i;
7996
7997 /* Remember current epoch settings for resending */
7998 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8000 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008001
8002 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008004
8005
8006 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008007 for (i = 2; i > 0; i--) {
8008 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 }
8011 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008012
8013 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 if (i == 0) {
8015 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8016 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008017 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008019#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008020 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008021
8022 ssl->transform_out = ssl->transform_negotiate;
8023 ssl->session_out = ssl->session_negotiate;
8024
8025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008026 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8027 mbedtls_ssl_send_flight_completed(ssl);
8028 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008029#endif
8030
Gilles Peskine449bd832023-01-11 14:50:10 +01008031 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8032 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8033 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008034 }
8035
8036#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008037 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8038 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8039 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8040 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008041 }
8042#endif
8043
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008045
Gilles Peskine449bd832023-01-11 14:50:10 +01008046 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008047}
8048
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008049#define SSL_MAX_HASH_LEN 12
8050
Gilles Peskine449bd832023-01-11 14:50:10 +01008051int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008052{
8053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8054 unsigned int hash_len = 12;
8055 unsigned char buf[SSL_MAX_HASH_LEN];
8056
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008058
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008059 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8060 if (ret != 0) {
8061 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8062 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008063
Gilles Peskine449bd832023-01-11 14:50:10 +01008064 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8065 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008066 goto exit;
8067 }
8068
Gilles Peskine449bd832023-01-11 14:50:10 +01008069 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8070 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8071 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8072 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008073 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8074 goto exit;
8075 }
8076
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8078 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8079 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008080 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8081 goto exit;
8082 }
8083
Gilles Peskine449bd832023-01-11 14:50:10 +01008084 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8085 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8086 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8087 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008088 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8089 goto exit;
8090 }
8091
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8093 buf, hash_len) != 0) {
8094 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8095 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8096 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008097 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8098 goto exit;
8099 }
8100
8101#if defined(MBEDTLS_SSL_RENEGOTIATION)
8102 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008104#endif
8105
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008107#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008109 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008110 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008111#endif
8112#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008114 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008116#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008117 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008118 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008120
8121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8123 mbedtls_ssl_recv_flight_completed(ssl);
8124 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008125#endif
8126
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008128
8129exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 mbedtls_platform_zeroize(buf, hash_len);
8131 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008132}
8133
Jerry Yu392112c2022-02-17 14:34:10 +08008134#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8135/*
8136 * Helper to get TLS 1.2 PRF from ciphersuite
8137 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008139static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008140{
Jerry Yu392112c2022-02-17 14:34:10 +08008141 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008143#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8145 return tls_prf_sha384;
8146 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008147#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008148#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008149 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008150 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8151 return tls_prf_sha256;
8152 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008153 }
8154#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008155#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8156 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008157 (void) ciphersuite_info;
8158#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008159
Gilles Peskine449bd832023-01-11 14:50:10 +01008160 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008161}
8162#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008163
Gilles Peskine449bd832023-01-11 14:50:10 +01008164static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008165{
8166 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008167#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 if (tls_prf == tls_prf_sha384) {
8169 return MBEDTLS_SSL_TLS_PRF_SHA384;
8170 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008171#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008172#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008173 if (tls_prf == tls_prf_sha256) {
8174 return MBEDTLS_SSL_TLS_PRF_SHA256;
8175 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008176#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008178}
8179
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008180/*
8181 * Populate a transform structure with session keys and all the other
8182 * necessary information.
8183 *
8184 * Parameters:
8185 * - [in/out]: transform: structure to populate
8186 * [in] must be just initialised with mbedtls_ssl_transform_init()
8187 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8188 * - [in] ciphersuite
8189 * - [in] master
8190 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008191 * - [in] tls_prf: pointer to PRF to use for key derivation
8192 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008193 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008194 * - [in] endpoint: client or server
8195 * - [in] ssl: used for:
8196 * - ssl->conf->{f,p}_export_keys
8197 * [in] optionally used for:
8198 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8199 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008200MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008201static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8202 int ciphersuite,
8203 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008204#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008206#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008207 ssl_tls_prf_t tls_prf,
8208 const unsigned char randbytes[64],
8209 mbedtls_ssl_protocol_version tls_version,
8210 unsigned endpoint,
8211 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008212{
8213 int ret = 0;
8214 unsigned char keyblk[256];
8215 unsigned char *key1;
8216 unsigned char *key2;
8217 unsigned char *mac_enc;
8218 unsigned char *mac_dec;
8219 size_t mac_key_len = 0;
8220 size_t iv_copy_len;
8221 size_t keylen;
8222 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008223 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008224#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008225 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008226 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008227#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008228
8229#if defined(MBEDTLS_USE_PSA_CRYPTO)
8230 psa_key_type_t key_type;
8231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8232 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008233 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008234 size_t key_bits;
8235 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8236#endif
8237
8238#if !defined(MBEDTLS_DEBUG_C) && \
8239 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008241 ssl = NULL; /* make sure we don't use it except for these cases */
8242 (void) ssl;
8243 }
8244#endif
8245
8246 /*
8247 * Some data just needs copying into the structure
8248 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008249#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008250 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008251#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008252 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008253
8254#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008256#endif
8257
8258#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008260 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8261 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008262 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008263 }
8264#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8265
8266 /*
8267 * Get various info structures
8268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8270 if (ciphersuite_info == NULL) {
8271 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8272 ciphersuite));
8273 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008274 }
8275
Neil Armstrongab555e02022-04-04 11:07:59 +02008276 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008277#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008279#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008281
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008283 transform->taglen =
8284 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008286
8287#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8289 transform->taglen,
8290 &alg,
8291 &key_type,
8292 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008293 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008294 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008295 goto end;
8296 }
8297#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8299 if (cipher_info == NULL) {
8300 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8301 ciphersuite_info->cipher));
8302 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008303 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008304#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008305
Neil Armstronge4512952022-03-08 09:08:22 +01008306#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008307 mac_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008309 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 (unsigned) ciphersuite_info->mac));
8311 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008312 }
8313#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8315 if (md_info == NULL) {
8316 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8317 (unsigned) ciphersuite_info->mac));
8318 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008319 }
Neil Armstronge4512952022-03-08 09:08:22 +01008320#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008321
8322#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8323 /* Copy own and peer's CID if the use of the CID
8324 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008325 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8326 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008327
8328 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8330 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8331 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008332
8333 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008334 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8335 ssl->handshake->peer_cid_len);
8336 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8337 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008338 }
8339#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8340
8341 /*
8342 * Compute key block using the PRF
8343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8345 if (ret != 0) {
8346 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8347 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008348 }
8349
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8351 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8352 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8353 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8354 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008355
8356 /*
8357 * Determine the appropriate key, IV and MAC length.
8358 */
8359
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008360#if defined(MBEDTLS_USE_PSA_CRYPTO)
8361 keylen = PSA_BITS_TO_BYTES(key_bits);
8362#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008364#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008365
8366#if defined(MBEDTLS_GCM_C) || \
8367 defined(MBEDTLS_CCM_C) || \
8368 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008370 size_t explicit_ivlen;
8371
8372 transform->maclen = 0;
8373 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008374
8375 /* All modes haves 96-bit IVs, but the length of the static parts vary
8376 * with mode and version:
8377 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8378 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8379 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8380 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8381 * sequence number).
8382 */
8383 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008384
8385 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008386#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008388#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8390 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008391#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008392
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008394 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008396 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398
8399 /* Minimum length of encrypted record */
8400 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8401 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008403#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8404#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008406 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008408#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008410#else
8411 size_t block_size = cipher_info->block_size;
8412#endif /* MBEDTLS_USE_PSA_CRYPTO */
8413
8414#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008415 /* Get MAC length */
8416 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8417#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008418 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8420 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8421 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008422 goto end;
8423 }
8424
8425 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008427#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008428 transform->maclen = mac_key_len;
8429
8430 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008431#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008433#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008434 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008435#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008436
8437 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008439 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008441 /*
8442 * GenericBlockCipher:
8443 * 1. if EtM is in use: one block plus MAC
8444 * otherwise: * first multiple of blocklen greater than maclen
8445 * 2. IV
8446 */
8447#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008449 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 + block_size;
8451 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008452#endif
8453 {
8454 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 + block_size
8456 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008457 }
8458
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008460 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 } else {
8462 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008463 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8464 goto end;
8465 }
8466 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008467 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008468#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8469 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8471 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008472 }
8473
Gilles Peskine449bd832023-01-11 14:50:10 +01008474 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8475 (unsigned) keylen,
8476 (unsigned) transform->minlen,
8477 (unsigned) transform->ivlen,
8478 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008479
8480 /*
8481 * Finally setup the cipher contexts, IVs and MAC secrets.
8482 */
8483#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008485 key1 = keyblk + mac_key_len * 2;
8486 key2 = keyblk + mac_key_len * 2 + keylen;
8487
8488 mac_enc = keyblk;
8489 mac_dec = keyblk + mac_key_len;
8490
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 iv_copy_len = (transform->fixed_ivlen) ?
8492 transform->fixed_ivlen : transform->ivlen;
8493 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8494 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8495 iv_copy_len);
8496 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008497#endif /* MBEDTLS_SSL_CLI_C */
8498#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008500 key1 = keyblk + mac_key_len * 2 + keylen;
8501 key2 = keyblk + mac_key_len * 2;
8502
8503 mac_enc = keyblk + mac_key_len;
8504 mac_dec = keyblk;
8505
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 iv_copy_len = (transform->fixed_ivlen) ?
8507 transform->fixed_ivlen : transform->ivlen;
8508 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8509 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8510 iv_copy_len);
8511 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008512#endif /* MBEDTLS_SSL_SRV_C */
8513 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008515 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8516 goto end;
8517 }
8518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 if (ssl != NULL && ssl->f_export_keys != NULL) {
8520 ssl->f_export_keys(ssl->p_export_keys,
8521 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8522 master, 48,
8523 randbytes + 32,
8524 randbytes,
8525 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008526 }
8527
8528#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008529 transform->psa_alg = alg;
8530
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8532 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8533 psa_set_key_algorithm(&attributes, alg);
8534 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008535
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 if ((status = psa_import_key(&attributes,
8537 key1,
8538 PSA_BITS_TO_BYTES(key_bits),
8539 &transform->psa_key_enc)) != PSA_SUCCESS) {
8540 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008541 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008543 goto end;
8544 }
8545
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008547
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 if ((status = psa_import_key(&attributes,
8549 key2,
8550 PSA_BITS_TO_BYTES(key_bits),
8551 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008552 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008554 goto end;
8555 }
8556 }
8557#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8559 cipher_info)) != 0) {
8560 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008561 goto end;
8562 }
8563
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8565 cipher_info)) != 0) {
8566 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008567 goto end;
8568 }
8569
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8571 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8572 MBEDTLS_ENCRYPT)) != 0) {
8573 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008574 goto end;
8575 }
8576
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8578 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8579 MBEDTLS_DECRYPT)) != 0) {
8580 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008581 goto end;
8582 }
8583
8584#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8586 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8587 MBEDTLS_PADDING_NONE)) != 0) {
8588 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008589 goto end;
8590 }
8591
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8593 MBEDTLS_PADDING_NONE)) != 0) {
8594 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008595 goto end;
8596 }
8597 }
8598#endif /* MBEDTLS_CIPHER_MODE_CBC */
8599#endif /* MBEDTLS_USE_PSA_CRYPTO */
8600
Neil Armstrong29c0c042022-03-17 17:47:28 +01008601#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8602 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8603 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008605#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008607
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8609 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8610 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008611
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 if ((status = psa_import_key(&attributes,
8613 mac_enc, mac_key_len,
8614 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008615 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008617 goto end;
8618 }
8619
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8621 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008622#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008624#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008626 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8628 PSA_KEY_USAGE_VERIFY_HASH);
8629 } else {
8630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8631 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if ((status = psa_import_key(&attributes,
8634 mac_dec, mac_key_len,
8635 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008636 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008638 goto end;
8639 }
8640#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8642 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008643 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 }
8645 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8646 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008647 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008649#endif /* MBEDTLS_USE_PSA_CRYPTO */
8650 }
8651#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8652
8653 ((void) mac_dec);
8654 ((void) mac_enc);
8655
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008656end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008657 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8658 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008659}
8660
Valerio Settia08b1a42022-11-17 15:10:02 +01008661#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8662 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008663int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 psa_pake_operation_t *pake_ctx,
8665 const unsigned char *buf,
8666 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008667{
8668 psa_status_t status;
8669 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008670 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008671 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8672 * At round two perform a single cycle
8673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008675
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 for (; remaining_steps > 0; remaining_steps--) {
8677 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008678 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008680 /* Length is stored at the first byte */
8681 size_t length = buf[input_offset];
8682 input_offset += 1;
8683
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008685 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8686 }
8687
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 status = psa_pake_input(pake_ctx, step,
8689 buf + input_offset, length);
8690 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008691 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008692 }
8693
8694 input_offset += length;
8695 }
8696 }
8697
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008699 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008701
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008703}
8704
Valerio Setti6b3dab02022-11-17 17:14:54 +01008705int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 psa_pake_operation_t *pake_ctx,
8707 unsigned char *buf,
8708 size_t len, size_t *olen,
8709 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008710{
8711 psa_status_t status;
8712 size_t output_offset = 0;
8713 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008714 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008715 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8716 * At round two perform a single cycle
8717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008719
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 for (; remaining_steps > 0; remaining_steps--) {
8721 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8722 step <= PSA_PAKE_STEP_ZK_PROOF;
8723 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008724 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008725 * For each step, prepend 1 byte with the length of the data as
8726 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 status = psa_pake_output(pake_ctx, step,
8729 buf + output_offset + 1,
8730 len - output_offset - 1,
8731 &output_len);
8732 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008733 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008734 }
8735
Valerio Setti99d88c12022-11-22 16:03:43 +01008736 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008737
8738 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008739 }
8740 }
8741
8742 *olen = output_offset;
8743
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008745}
Valerio Settia08b1a42022-11-17 15:10:02 +01008746#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8747
Jerry Yuee40f9d2022-02-17 14:55:16 +08008748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008749int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8750 unsigned char *hash, size_t *hashlen,
8751 unsigned char *data, size_t data_len,
8752 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008753{
8754 psa_status_t status;
8755 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008756 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008757
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 if ((status = psa_hash_setup(&hash_operation,
8761 hash_alg)) != PSA_SUCCESS) {
8762 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008763 goto exit;
8764 }
8765
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8767 64)) != PSA_SUCCESS) {
8768 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008769 goto exit;
8770 }
8771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 if ((status = psa_hash_update(&hash_operation,
8773 data, data_len)) != PSA_SUCCESS) {
8774 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008775 goto exit;
8776 }
8777
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8779 hashlen)) != PSA_SUCCESS) {
8780 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8781 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008782 }
8783
8784exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 if (status != PSA_SUCCESS) {
8786 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8787 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8788 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008789 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008791 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8792 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008794 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008796 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008798 }
8799 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008801}
8802
8803#else
8804
Gilles Peskine449bd832023-01-11 14:50:10 +01008805int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8806 unsigned char *hash, size_t *hashlen,
8807 unsigned char *data, size_t data_len,
8808 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008809{
8810 int ret = 0;
8811 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8813 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008816
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008818
8819 /*
8820 * digitally-signed struct {
8821 * opaque client_random[32];
8822 * opaque server_random[32];
8823 * ServerDHParams params;
8824 * };
8825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8827 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008828 goto exit;
8829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8831 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008832 goto exit;
8833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008836 goto exit;
8837 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8839 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008840 goto exit;
8841 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8843 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008844 goto exit;
8845 }
8846
8847exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008849
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 if (ret != 0) {
8851 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8852 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8853 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008856}
8857#endif /* MBEDTLS_USE_PSA_CRYPTO */
8858
Jerry Yud9d91da2022-02-17 14:57:06 +08008859#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8860
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008861/* Find the preferred hash for a given signature algorithm. */
8862unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 mbedtls_ssl_context *ssl,
8864 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008865{
Gabor Mezei078e8032022-04-27 21:17:56 +02008866 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008867 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008868
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8870 return MBEDTLS_SSL_HASH_NONE;
8871 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008874 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8876 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008877 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8879 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008882#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008884 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008885 mbedtls_md_psa_alg_from_type(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008886
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8888 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8889 PSA_ALG_ECDSA(psa_hash_alg),
8890 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008891 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8895 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8896 PSA_ALG_RSA_PKCS1V15_SIGN(
8897 psa_hash_alg),
8898 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008899 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008901 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008902#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008905 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008906 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008909}
8910
8911#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8912
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008913/* Serialization of TLS 1.2 sessions:
8914 *
8915 * struct {
8916 * uint64 start_time;
8917 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008918 * uint8 session_id_len; // at most 32
8919 * opaque session_id[32];
8920 * opaque master[48]; // fixed length in the standard
8921 * uint32 verify_result;
8922 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8923 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8924 * uint32 ticket_lifetime;
8925 * uint8 mfl_code; // up to 255 according to standard
8926 * uint8 encrypt_then_mac; // 0 or 1
8927 * } serialized_session_tls12;
8928 *
8929 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008930static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8931 unsigned char *buf,
8932 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008933{
8934 unsigned char *p = buf;
8935 size_t used = 0;
8936
8937#if defined(MBEDTLS_HAVE_TIME)
8938 uint64_t start;
8939#endif
8940#if defined(MBEDTLS_X509_CRT_PARSE_C)
8941#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8942 size_t cert_len;
8943#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8944#endif /* MBEDTLS_X509_CRT_PARSE_C */
8945
8946 /*
8947 * Time
8948 */
8949#if defined(MBEDTLS_HAVE_TIME)
8950 used += 8;
8951
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008953 start = (uint64_t) session->start;
8954
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008956 p += 8;
8957 }
8958#endif /* MBEDTLS_HAVE_TIME */
8959
8960 /*
8961 * Basic mandatory fields
8962 */
8963 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 + 1 /* id_len */
8965 + sizeof(session->id)
8966 + sizeof(session->master)
8967 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008968
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 if (used <= buf_len) {
8970 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008971 p += 2;
8972
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 *p++ = MBEDTLS_BYTE_0(session->id_len);
8974 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008975 p += 32;
8976
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008978 p += 48;
8979
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008981 p += 4;
8982 }
8983
8984 /*
8985 * Peer's end-entity certificate
8986 */
8987#if defined(MBEDTLS_X509_CRT_PARSE_C)
8988#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008990 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008992 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008994
8995 used += 3 + cert_len;
8996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 if (used <= buf_len) {
8998 *p++ = MBEDTLS_BYTE_2(cert_len);
8999 *p++ = MBEDTLS_BYTE_1(cert_len);
9000 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 if (session->peer_cert != NULL) {
9003 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009004 p += cert_len;
9005 }
9006 }
9007#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009009 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009011 *p++ = (unsigned char) session->peer_cert_digest_type;
9012 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 memcpy(p, session->peer_cert_digest,
9014 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009015 p += session->peer_cert_digest_len;
9016 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009018 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009020 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9021 *p++ = 0;
9022 }
9023 }
9024#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9025#endif /* MBEDTLS_X509_CRT_PARSE_C */
9026
9027 /*
9028 * Session ticket if any, plus associated data
9029 */
9030#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9031 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9032
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 if (used <= buf_len) {
9034 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9035 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9036 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009037
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 if (session->ticket != NULL) {
9039 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009040 p += session->ticket_len;
9041 }
9042
Gilles Peskine449bd832023-01-11 14:50:10 +01009043 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009044 p += 4;
9045 }
9046#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9047
9048 /*
9049 * Misc extension-related info
9050 */
9051#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9052 used += 1;
9053
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009055 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009057#endif
9058
9059#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9060 used += 1;
9061
Gilles Peskine449bd832023-01-11 14:50:10 +01009062 if (used <= buf_len) {
9063 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9064 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009065#endif
9066
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009068}
9069
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009070MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009071static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9072 const unsigned char *buf,
9073 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009074{
9075#if defined(MBEDTLS_HAVE_TIME)
9076 uint64_t start;
9077#endif
9078#if defined(MBEDTLS_X509_CRT_PARSE_C)
9079#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9080 size_t cert_len;
9081#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9082#endif /* MBEDTLS_X509_CRT_PARSE_C */
9083
9084 const unsigned char *p = buf;
9085 const unsigned char * const end = buf + len;
9086
9087 /*
9088 * Time
9089 */
9090#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009091 if (8 > (size_t) (end - p)) {
9092 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9093 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009094
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01009095 start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009096 p += 8;
9097
9098 session->start = (time_t) start;
9099#endif /* MBEDTLS_HAVE_TIME */
9100
9101 /*
9102 * Basic mandatory fields
9103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9105 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9106 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009107
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009109 p += 2;
9110
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009111 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009113 p += 32;
9114
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009116 p += 48;
9117
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01009118 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009119 p += 4;
9120
9121 /* Immediately clear invalid pointer values that have been read, in case
9122 * we exit early before we replaced them with valid ones. */
9123#if defined(MBEDTLS_X509_CRT_PARSE_C)
9124#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9125 session->peer_cert = NULL;
9126#else
9127 session->peer_cert_digest = NULL;
9128#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9129#endif /* MBEDTLS_X509_CRT_PARSE_C */
9130#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9131 session->ticket = NULL;
9132#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9133
9134 /*
9135 * Peer certificate
9136 */
9137#if defined(MBEDTLS_X509_CRT_PARSE_C)
9138#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9139 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 if (3 > (size_t) (end - p)) {
9141 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9142 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009143
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009145 p += 3;
9146
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009148 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9149
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 if (cert_len > (size_t) (end - p)) {
9151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9152 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009153
Gilles Peskine449bd832023-01-11 14:50:10 +01009154 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 if (session->peer_cert == NULL) {
9157 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9158 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9163 p, cert_len)) != 0) {
9164 mbedtls_x509_crt_free(session->peer_cert);
9165 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009166 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009168 }
9169
9170 p += cert_len;
9171 }
9172#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9173 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 if (2 > (size_t) (end - p)) {
9175 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9176 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009177
9178 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9179 session->peer_cert_digest_len = (size_t) *p++;
9180
Gilles Peskine449bd832023-01-11 14:50:10 +01009181 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009182 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9184 if (md_info == NULL) {
9185 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9186 }
9187 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9188 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9189 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009190
Gilles Peskine449bd832023-01-11 14:50:10 +01009191 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9192 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9193 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009194
9195 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 mbedtls_calloc(1, session->peer_cert_digest_len);
9197 if (session->peer_cert_digest == NULL) {
9198 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9199 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009200
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 memcpy(session->peer_cert_digest, p,
9202 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009203 p += session->peer_cert_digest_len;
9204 }
9205#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9206#endif /* MBEDTLS_X509_CRT_PARSE_C */
9207
9208 /*
9209 * Session ticket and associated data
9210 */
9211#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if (3 > (size_t) (end - p)) {
9213 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9214 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009215
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009217 p += 3;
9218
Gilles Peskine449bd832023-01-11 14:50:10 +01009219 if (session->ticket_len != 0) {
9220 if (session->ticket_len > (size_t) (end - p)) {
9221 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9222 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009223
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 session->ticket = mbedtls_calloc(1, session->ticket_len);
9225 if (session->ticket == NULL) {
9226 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9227 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009230 p += session->ticket_len;
9231 }
9232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 if (4 > (size_t) (end - p)) {
9234 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9235 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009236
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01009237 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009238 p += 4;
9239#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9240
9241 /*
9242 * Misc extension-related info
9243 */
9244#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 if (1 > (size_t) (end - p)) {
9246 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9247 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009248
9249 session->mfl_code = *p++;
9250#endif
9251
9252#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 if (1 > (size_t) (end - p)) {
9254 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9255 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009256
9257 session->encrypt_then_mac = *p++;
9258#endif
9259
9260 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 if (p != end) {
9262 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9263 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009264
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009266}
Jerry Yudc7bd172022-02-17 13:44:15 +08009267#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9268
XiaokangQian75d40ef2022-04-20 11:05:24 +00009269int mbedtls_ssl_validate_ciphersuite(
9270 const mbedtls_ssl_context *ssl,
9271 const mbedtls_ssl_ciphersuite_t *suite_info,
9272 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009274{
9275 (void) ssl;
9276
Gilles Peskine449bd832023-01-11 14:50:10 +01009277 if (suite_info == NULL) {
9278 return -1;
9279 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009280
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 if ((suite_info->min_tls_version > max_tls_version) ||
9282 (suite_info->max_tls_version < min_tls_version)) {
9283 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009284 }
9285
XiaokangQian060d8672022-04-21 09:24:56 +00009286#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009287#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009288#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009289 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9290 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009291#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009292 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9293 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009294#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009295 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009297 }
9298#endif
9299
9300 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9301#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9303 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9304 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009305 }
9306#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9307#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9308
Gilles Peskine449bd832023-01-11 14:50:10 +01009309 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009310}
9311
Ronald Crone68ab4f2022-10-05 12:46:29 +02009312#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009313/*
9314 * Function for writing a signature algorithm extension.
9315 *
9316 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9317 * value (TLS 1.3 RFC8446):
9318 * enum {
9319 * ....
9320 * ecdsa_secp256r1_sha256( 0x0403 ),
9321 * ecdsa_secp384r1_sha384( 0x0503 ),
9322 * ecdsa_secp521r1_sha512( 0x0603 ),
9323 * ....
9324 * } SignatureScheme;
9325 *
9326 * struct {
9327 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9328 * } SignatureSchemeList;
9329 *
9330 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9331 * value (TLS 1.2 RFC5246):
9332 * enum {
9333 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9334 * sha512(6), (255)
9335 * } HashAlgorithm;
9336 *
9337 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9338 * SignatureAlgorithm;
9339 *
9340 * struct {
9341 * HashAlgorithm hash;
9342 * SignatureAlgorithm signature;
9343 * } SignatureAndHashAlgorithm;
9344 *
9345 * SignatureAndHashAlgorithm
9346 * supported_signature_algorithms<2..2^16-2>;
9347 *
9348 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9349 * generalization of the TLS 1.2 signature algorithm extension.
9350 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9351 * `SignatureScheme` field of TLS 1.3
9352 *
9353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009354int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9355 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009356{
9357 unsigned char *p = buf;
9358 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9359 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9360
9361 *out_len = 0;
9362
Gilles Peskine449bd832023-01-11 14:50:10 +01009363 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009364
9365 /* Check if we have space for header and length field:
9366 * - extension_type (2 bytes)
9367 * - extension_data_length (2 bytes)
9368 * - supported_signature_algorithms_length (2 bytes)
9369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009371 p += 6;
9372
9373 /*
9374 * Write supported_signature_algorithms
9375 */
9376 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9378 if (sig_alg == NULL) {
9379 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9380 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009381
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9383 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9384 *sig_alg,
9385 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9386 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009387 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009388 }
9389 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9390 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009391 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009392 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9393 *sig_alg,
9394 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009395 }
9396
9397 /* Length of supported_signature_algorithms */
9398 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 if (supported_sig_alg_len == 0) {
9400 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9401 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009402 }
9403
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9405 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9406 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009407
XiaokangQianeaf36512022-04-24 09:07:44 +00009408 *out_len = p - buf;
9409
9410#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009412#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009413
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009415}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009416#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009417
XiaokangQian40a35232022-05-07 09:02:40 +00009418#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009419/*
9420 * mbedtls_ssl_parse_server_name_ext
9421 *
9422 * Structure of server_name extension:
9423 *
9424 * enum {
9425 * host_name(0), (255)
9426 * } NameType;
9427 * opaque HostName<1..2^16-1>;
9428 *
9429 * struct {
9430 * NameType name_type;
9431 * select (name_type) {
9432 * case host_name: HostName;
9433 * } name;
9434 * } ServerName;
9435 * struct {
9436 * ServerName server_name_list<1..2^16-1>
9437 * } ServerNameList;
9438 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009439MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009440int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9441 const unsigned char *buf,
9442 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009443{
9444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9445 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009446 size_t server_name_list_len, hostname_len;
9447 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009450
Gilles Peskine449bd832023-01-11 14:50:10 +01009451 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9452 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009453 p += 2;
9454
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009456 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 while (p < server_name_list_end) {
9458 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9459 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9460 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9461 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009462
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009464 /* sni_name is intended to be used only during the parsing of the
9465 * ClientHello message (it is reset to NULL before the end of
9466 * the message parsing). Thus it is ok to just point to the
9467 * reception buffer and not make a copy of it.
9468 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009469 ssl->handshake->sni_name = p + 3;
9470 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 if (ssl->conf->f_sni == NULL) {
9472 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009473 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9475 ssl, p + 3, hostname_len);
9476 if (ret != 0) {
9477 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9478 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9479 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9480 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9481 }
9482 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009483 }
9484
9485 p += hostname_len + 3;
9486 }
9487
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009489}
9490#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9491
XiaokangQianacb39922022-06-17 10:18:48 +00009492#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009493MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009494int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9495 const unsigned char *buf,
9496 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009497{
9498 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009499 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009500 const unsigned char *protocol_name_list;
9501 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009502 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009503
9504 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009505 if (ssl->conf->alpn_list == NULL) {
9506 return 0;
9507 }
XiaokangQianacb39922022-06-17 10:18:48 +00009508
9509 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009510 * RFC7301, section 3.1
9511 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009512 *
XiaokangQianc7403452022-06-23 03:24:12 +00009513 * struct {
9514 * ProtocolName protocol_name_list<2..2^16-1>
9515 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009516 */
9517
XiaokangQianc7403452022-06-23 03:24:12 +00009518 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009519 * protocol_name_list_len 2 bytes
9520 * protocol_name_len 1 bytes
9521 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009524
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009526 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009528 protocol_name_list = p;
9529 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009530
9531 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009533 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9535 protocol_name_len);
9536 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009537 MBEDTLS_SSL_PEND_FATAL_ALERT(
9538 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9540 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009541 }
9542
9543 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009544 }
9545
9546 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9548 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009549 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009551 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 if (protocol_name_len == alpn_len &&
9553 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009554 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009556 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009557
9558 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009559 }
9560 }
9561
XiaokangQian95d5f542022-06-24 02:29:26 +00009562 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009563 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9565 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9566 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009567}
9568
Gilles Peskine449bd832023-01-11 14:50:10 +01009569int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9570 unsigned char *buf,
9571 unsigned char *end,
9572 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009573{
9574 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009575 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009576 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009577
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 if (ssl->alpn_chosen == NULL) {
9579 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009580 }
9581
Gilles Peskine449bd832023-01-11 14:50:10 +01009582 protocol_name_len = strlen(ssl->alpn_chosen);
9583 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009584
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009586 /*
9587 * 0 . 1 ext identifier
9588 * 2 . 3 ext length
9589 * 4 . 5 protocol list length
9590 * 6 . 6 protocol name length
9591 * 7 . 7+n protocol name
9592 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009593 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009594
XiaokangQian95d5f542022-06-24 02:29:26 +00009595 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009596
Gilles Peskine449bd832023-01-11 14:50:10 +01009597 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9598 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009599 /* Note: the length of the chosen protocol has been checked to be less
9600 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009603
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009605
9606#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009608#endif
9609
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009611}
9612#endif /* MBEDTLS_SSL_ALPN */
9613
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009614#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009615 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009616 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009617 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009618int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9619 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009620{
9621 /* Initialize to suppress unnecessary compiler warning */
9622 size_t hostname_len = 0;
9623
9624 /* Check if new hostname is valid before
9625 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 if (hostname != NULL) {
9627 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009628
Gilles Peskine449bd832023-01-11 14:50:10 +01009629 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9631 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009632 }
9633
9634 /* Now it's clear that we will overwrite the old hostname,
9635 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009636 if (session->hostname != NULL) {
9637 mbedtls_platform_zeroize(session->hostname,
9638 strlen(session->hostname));
9639 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009640 }
9641
9642 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009644 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 } else {
9646 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9647 if (session->hostname == NULL) {
9648 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9649 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009650
Gilles Peskine449bd832023-01-11 14:50:10 +01009651 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009652 }
9653
Gilles Peskine449bd832023-01-11 14:50:10 +01009654 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009655}
Xiaokang Qian03409292022-10-12 02:49:52 +00009656#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9657 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009658 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009659 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661#endif /* MBEDTLS_SSL_TLS_C */