blob: 981ec7b6811c4b21bc3065b7e25506cb98b002de [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 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
524void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
525 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
526{
527 ((void) ciphersuite_info);
528
Andrzej Kurek25f27152022-08-17 16:09:31 -0400529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800530 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
531 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
532 else
533#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400534#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800535 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
536 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
537 else
538#endif
539 {
540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
541 return;
542 }
543}
544
XiaokangQianadab9a62022-07-18 07:41:26 +0000545void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
546 unsigned hs_type,
547 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100548{
549 unsigned char hs_hdr[4];
550
551 /* Build HS header for checksum update. */
552 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
553 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
554 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
555 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
556
557 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
558}
559
560void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
561 unsigned hs_type,
562 unsigned char const *msg,
563 size_t msg_len )
564{
565 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
566 ssl->handshake->update_checksum( ssl, msg, msg_len );
567}
568
Jerry Yuc73c6182022-02-08 20:29:25 +0800569void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
570{
571 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400572#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800573#if defined(MBEDTLS_USE_PSA_CRYPTO)
574 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
575 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
576#else
577 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
578#endif
579#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400580#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800581#if defined(MBEDTLS_USE_PSA_CRYPTO)
582 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
583 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
584#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400585 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800586#endif
587#endif
588}
589
590static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
591 const unsigned char *buf, size_t len )
592{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
596#else
597 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
598#endif
599#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400600#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800601#if defined(MBEDTLS_USE_PSA_CRYPTO)
602 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
603#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400604 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800605#endif
606#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400607#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
608 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
609 (void) ssl;
610 (void) buf;
611 (void) len;
612#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800613}
614
Andrzej Kurek25f27152022-08-17 16:09:31 -0400615#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800616static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
617 const unsigned char *buf, size_t len )
618{
619#if defined(MBEDTLS_USE_PSA_CRYPTO)
620 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
621#else
622 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
623#endif
624}
625#endif
626
Andrzej Kurek25f27152022-08-17 16:09:31 -0400627#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800628static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
629 const unsigned char *buf, size_t len )
630{
631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
633#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400634 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800635#endif
636}
637#endif
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200642
Andrzej Kurek25f27152022-08-17 16:09:31 -0400643#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500644#if defined(MBEDTLS_USE_PSA_CRYPTO)
645 handshake->fin_sha256_psa = psa_hash_operation_init();
646 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
647#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200649 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200650#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500651#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400652#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500653#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500654 handshake->fin_sha384_psa = psa_hash_operation_init();
655 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500656#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400657 mbedtls_sha512_init( &handshake->fin_sha384 );
658 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500660#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200661
662 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_DHM_C)
665 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200666#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200667#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200669#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200670#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200671 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200672#if defined(MBEDTLS_SSL_CLI_C)
673 handshake->ecjpake_cache = NULL;
674 handshake->ecjpake_cache_len = 0;
675#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200676#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200677
Gilles Peskineeccd8882020-03-10 12:19:08 +0100678#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200679 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200680#endif
681
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200682#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
683 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
684#endif
Hanno Becker75173122019-02-06 16:18:31 +0000685
686#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
687 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
688 mbedtls_pk_init( &handshake->peer_pubkey );
689#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200690}
691
Hanno Beckera18d1322018-01-03 14:27:32 +0000692void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200695
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100696#if defined(MBEDTLS_USE_PSA_CRYPTO)
697 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
698 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100699#else
700 mbedtls_cipher_init( &transform->cipher_ctx_enc );
701 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100702#endif
703
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000704#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100705#if defined(MBEDTLS_USE_PSA_CRYPTO)
706 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
707 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100708#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_md_init( &transform->md_ctx_enc );
710 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000711#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100712#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713}
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200718}
719
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200720MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000722{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000724 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200726 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200729 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200730
731 /*
732 * Either the pointers are now NULL or cleared properly and can be freed.
733 * Now allocate missing structures.
734 */
735 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200737 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200738 }
Paul Bakker48916f92012-09-16 19:57:18 +0000739
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200740 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200742 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200743 }
Paul Bakker48916f92012-09-16 19:57:18 +0000744
Paul Bakker82788fb2014-10-20 13:59:19 +0200745 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200747 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200748 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500749#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
750 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400751
Andrzej Kurek4a063792020-10-21 15:08:44 +0200752 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
753 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500754#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000755
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200756 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000757 if( ssl->handshake == NULL ||
758 ssl->transform_negotiate == NULL ||
759 ssl->session_negotiate == NULL )
760 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_free( ssl->handshake );
764 mbedtls_free( ssl->transform_negotiate );
765 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200766
767 ssl->handshake = NULL;
768 ssl->transform_negotiate = NULL;
769 ssl->session_negotiate = NULL;
770
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200771 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000772 }
773
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200774 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000776 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200777 ssl_handshake_params_init( ssl->handshake );
778
Jerry Yud0766ec2022-09-22 10:46:57 +0800779#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
780 defined(MBEDTLS_SSL_SRV_C) && \
781 defined(MBEDTLS_SSL_SESSION_TICKETS)
782 ssl->handshake->new_session_tickets_count =
783 ssl->conf->new_session_tickets_count ;
784#endif
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
788 {
789 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200790
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200791 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
792 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
793 else
794 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200795
Hanno Becker0f57a652020-02-05 10:37:26 +0000796 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200797 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200798#endif
799
Brett Warrene0edc842021-08-17 09:53:13 +0100800/*
801 * curve_list is translated to IANA TLS group identifiers here because
802 * mbedtls_ssl_conf_curves returns void and so can't return
803 * any error codes.
804 */
805#if defined(MBEDTLS_ECP_C)
806#if !defined(MBEDTLS_DEPRECATED_REMOVED)
807 /* Heap allocate and translate curve_list from internal to IANA group ids */
808 if ( ssl->conf->curve_list != NULL )
809 {
810 size_t length;
811 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
812
813 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
814 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
815
816 /* Leave room for zero termination */
817 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
818 if ( group_list == NULL )
819 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
820
821 for( size_t i = 0; i < length; i++ )
822 {
823 const mbedtls_ecp_curve_info *info =
824 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
825 if ( info == NULL )
826 {
827 mbedtls_free( group_list );
828 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
829 }
830 group_list[i] = info->tls_id;
831 }
832
833 group_list[length] = 0;
834
835 ssl->handshake->group_list = group_list;
836 ssl->handshake->group_list_heap_allocated = 1;
837 }
838 else
839 {
840 ssl->handshake->group_list = ssl->conf->group_list;
841 ssl->handshake->group_list_heap_allocated = 0;
842 }
843#endif /* MBEDTLS_DEPRECATED_REMOVED */
844#endif /* MBEDTLS_ECP_C */
845
Ronald Crone68ab4f2022-10-05 12:46:29 +0200846#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +0800847#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800849 /* Heap allocate and translate sig_hashes from internal hash identifiers to
850 signature algorithms IANA identifiers. */
851 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800852 ssl->conf->sig_hashes != NULL )
853 {
854 const int *md;
855 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800856 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800857 uint16_t *p;
858
Jerry Yub476a442022-01-21 18:14:45 +0800859#if defined(static_assert)
860 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
861 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
862 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800863#endif
864
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
866 {
867 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
868 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800869#if defined(MBEDTLS_ECDSA_C)
870 sig_algs_len += sizeof( uint16_t );
871#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800872
Jerry Yub476a442022-01-21 18:14:45 +0800873#if defined(MBEDTLS_RSA_C)
874 sig_algs_len += sizeof( uint16_t );
875#endif
876 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
877 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800878 }
879
Jerry Yub476a442022-01-21 18:14:45 +0800880 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800881 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
882
Jerry Yub476a442022-01-21 18:14:45 +0800883 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
884 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800885 if( ssl->handshake->sig_algs == NULL )
886 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
887
888 p = (uint16_t *)ssl->handshake->sig_algs;
889 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
890 {
891 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
892 if( hash == MBEDTLS_SSL_HASH_NONE )
893 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800894#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
896 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800897#endif
898#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800899 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
900 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800901#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200903 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800904 ssl->handshake->sig_algs_heap_allocated = 1;
905 }
906 else
Jerry Yua69269a2022-01-17 21:06:01 +0800907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800908 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800909 ssl->handshake->sig_algs_heap_allocated = 0;
910 }
Jerry Yucc539102022-06-27 16:27:35 +0800911#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +0200912#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000913 return( 0 );
914}
915
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200916#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200918MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200919static int ssl_cookie_write_dummy( void *ctx,
920 unsigned char **p, unsigned char *end,
921 const unsigned char *cli_id, size_t cli_id_len )
922{
923 ((void) ctx);
924 ((void) p);
925 ((void) end);
926 ((void) cli_id);
927 ((void) cli_id_len);
928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200930}
931
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200933static int ssl_cookie_check_dummy( void *ctx,
934 const unsigned char *cookie, size_t cookie_len,
935 const unsigned char *cli_id, size_t cli_id_len )
936{
937 ((void) ctx);
938 ((void) cookie);
939 ((void) cookie_len);
940 ((void) cli_id);
941 ((void) cli_id_len);
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200944}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200945#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200946
Paul Bakker5121ce52009-01-03 21:22:43 +0000947/*
948 * Initialize an SSL context
949 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200950void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
951{
952 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
953}
954
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200955MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800956static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
957{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100958 const mbedtls_ssl_config *conf = ssl->conf;
959
Ronald Cron6f135e12021-12-08 16:57:54 +0100960#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100961 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
962 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000963 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
967 }
968
Jerry Yu60835a82021-08-04 10:13:52 +0800969 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
970 return( 0 );
971 }
972#endif
973
974#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100975 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800976 {
977 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
978 return( 0 );
979 }
980#endif
981
Ronald Cron6f135e12021-12-08 16:57:54 +0100982#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100983 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800984 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
988 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
989 }
990
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000991 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
992 {
993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
994 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
995 }
996
997
Ronald Crone1d3f062022-02-10 14:50:54 +0100998 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
999 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001000 }
1001#endif
1002
1003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1004 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1005}
1006
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001007MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001008static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1009{
1010 int ret;
1011 ret = ssl_conf_version_check( ssl );
1012 if( ret != 0 )
1013 return( ret );
1014
Jerry Yudef7ae42022-10-30 14:13:19 +08001015#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1016 /* RFC 8446 section 4.4.3
1017 *
1018 * If the verification fails, the receiver MUST terminate the handshake with
1019 * a "decrypt_error" alert.
1020 *
1021 * If the client is configured as TLS 1.3 only with optional verify, return
1022 * bad config.
1023 *
1024 */
1025 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1026 (mbedtls_ssl_context *)ssl ) &&
1027 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1028 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1029 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1030 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1031 {
1032 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001033 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001034 "is not available for TLS 1.3 client" ) );
1035 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1036 }
1037#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1038
Jerry Yu60835a82021-08-04 10:13:52 +08001039 /* Space for further checks */
1040
1041 return( 0 );
1042}
1043
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001044/*
1045 * Setup an SSL context
1046 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001047
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001048int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001049 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050{
Janos Follath865b3eb2019-12-16 11:46:15 +00001051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001052 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1053 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001055 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001056
Jerry Yu60835a82021-08-04 10:13:52 +08001057 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1058 return( ret );
1059
Paul Bakker62f2dee2012-09-28 07:31:51 +00001060 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001061 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001062 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001063
1064 /* Set to NULL in case of an error condition */
1065 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001066
Darryl Greenb33cc762019-11-28 14:29:44 +00001067#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1068 ssl->in_buf_len = in_buf_len;
1069#endif
1070 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001071 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001074 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001075 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001076 }
1077
Darryl Greenb33cc762019-11-28 14:29:44 +00001078#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1079 ssl->out_buf_len = out_buf_len;
1080#endif
1081 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001082 if( ssl->out_buf == NULL )
1083 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001085 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001086 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 }
1088
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001089 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001090
Johan Pascalb62bb512015-12-03 21:56:45 +01001091#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001092 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001093#endif
1094
Paul Bakker48916f92012-09-16 19:57:18 +00001095 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001096 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
1098 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001099
1100error:
1101 mbedtls_free( ssl->in_buf );
1102 mbedtls_free( ssl->out_buf );
1103
1104 ssl->conf = NULL;
1105
Darryl Greenb33cc762019-11-28 14:29:44 +00001106#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1107 ssl->in_buf_len = 0;
1108 ssl->out_buf_len = 0;
1109#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001110 ssl->in_buf = NULL;
1111 ssl->out_buf = NULL;
1112
1113 ssl->in_hdr = NULL;
1114 ssl->in_ctr = NULL;
1115 ssl->in_len = NULL;
1116 ssl->in_iv = NULL;
1117 ssl->in_msg = NULL;
1118
1119 ssl->out_hdr = NULL;
1120 ssl->out_ctr = NULL;
1121 ssl->out_len = NULL;
1122 ssl->out_iv = NULL;
1123 ssl->out_msg = NULL;
1124
k-stachowiak9f7798e2018-07-31 16:52:32 +02001125 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001126}
1127
1128/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001129 * Reset an initialized and used SSL context for re-use while retaining
1130 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001131 *
1132 * If partial is non-zero, keep data in the input buffer and client ID.
1133 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001134 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001135void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1136 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001137{
Darryl Greenb33cc762019-11-28 14:29:44 +00001138#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1139 size_t in_buf_len = ssl->in_buf_len;
1140 size_t out_buf_len = ssl->out_buf_len;
1141#else
1142 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1143 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1144#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001145
Hanno Beckerb0302c42021-08-03 09:39:42 +01001146#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1147 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001148#endif
1149
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001150 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001151 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001152
Hanno Beckerb0302c42021-08-03 09:39:42 +01001153 mbedtls_ssl_reset_in_out_pointers( ssl );
1154
1155 /* Reset incoming message parsing */
1156 ssl->in_offt = NULL;
1157 ssl->nb_zero = 0;
1158 ssl->in_msgtype = 0;
1159 ssl->in_msglen = 0;
1160 ssl->in_hslen = 0;
1161 ssl->keep_current_message = 0;
1162 ssl->transform_in = NULL;
1163
1164#if defined(MBEDTLS_SSL_PROTO_DTLS)
1165 ssl->next_record_offset = 0;
1166 ssl->in_epoch = 0;
1167#endif
1168
1169 /* Keep current datagram if partial == 1 */
1170 if( partial == 0 )
1171 {
1172 ssl->in_left = 0;
1173 memset( ssl->in_buf, 0, in_buf_len );
1174 }
1175
Ronald Cronad8c17b2022-06-10 17:18:09 +02001176 ssl->send_alert = 0;
1177
Hanno Beckerb0302c42021-08-03 09:39:42 +01001178 /* Reset outgoing message writing */
1179 ssl->out_msgtype = 0;
1180 ssl->out_msglen = 0;
1181 ssl->out_left = 0;
1182 memset( ssl->out_buf, 0, out_buf_len );
1183 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1184 ssl->transform_out = NULL;
1185
1186#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1187 mbedtls_ssl_dtls_replay_reset( ssl );
1188#endif
1189
XiaokangQian2b01dc32022-01-21 02:53:13 +00001190#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001191 if( ssl->transform )
1192 {
1193 mbedtls_ssl_transform_free( ssl->transform );
1194 mbedtls_free( ssl->transform );
1195 ssl->transform = NULL;
1196 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001197#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1198
XiaokangQian2b01dc32022-01-21 02:53:13 +00001199#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1200 mbedtls_ssl_transform_free( ssl->transform_application );
1201 mbedtls_free( ssl->transform_application );
1202 ssl->transform_application = NULL;
1203
1204 if( ssl->handshake != NULL )
1205 {
1206 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1207 mbedtls_free( ssl->handshake->transform_earlydata );
1208 ssl->handshake->transform_earlydata = NULL;
1209
1210 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1211 mbedtls_free( ssl->handshake->transform_handshake );
1212 ssl->handshake->transform_handshake = NULL;
1213 }
1214
XiaokangQian2b01dc32022-01-21 02:53:13 +00001215#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001216}
1217
1218int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1219{
1220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1221
1222 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1223
XiaokangQian78b1fa72022-01-19 06:56:30 +00001224 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001225
1226 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_SSL_RENEGOTIATION)
1228 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001229 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001230
1231 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1233 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001236
Hanno Beckerb0302c42021-08-03 09:39:42 +01001237 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001238 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001239 if( ssl->session )
1240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_ssl_session_free( ssl->session );
1242 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001243 ssl->session = NULL;
1244 }
1245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001247 ssl->alpn_chosen = NULL;
1248#endif
1249
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001250#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001251 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001252#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001253 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001254#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001255 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001256 {
1257 mbedtls_free( ssl->cli_id );
1258 ssl->cli_id = NULL;
1259 ssl->cli_id_len = 0;
1260 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001261#endif
1262
Paul Bakker48916f92012-09-16 19:57:18 +00001263 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1264 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001265
1266 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001267}
1268
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001269/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001270 * Reset an initialized and used SSL context for re-use while retaining
1271 * all application-set variables, function pointers and data.
1272 */
1273int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1274{
Hanno Becker43aefe22020-02-05 10:44:56 +00001275 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001276}
1277
1278/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 * SSL set accessors
1280 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001281void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001283 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001284}
1285
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001286void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001287{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001288 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001289}
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001292void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001293{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001294 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001295}
1296#endif
1297
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001298void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001300 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001301}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001304
Hanno Becker1841b0a2018-08-24 11:13:57 +01001305void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1306 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001307{
1308 ssl->disable_datagram_packing = !allow_packing;
1309}
1310
1311void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1312 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001314 conf->hs_timeout_min = min;
1315 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001316}
1317#endif
1318
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001319void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001321 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322}
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001325void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001326 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001327 void *p_vrfy )
1328{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001329 conf->f_vrfy = f_vrfy;
1330 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001331}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001333
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001334void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001335 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 void *p_rng )
1337{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001338 conf->f_rng = f_rng;
1339 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340}
1341
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001342void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001343 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 void *p_dbg )
1345{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001346 conf->f_dbg = f_dbg;
1347 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348}
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001351 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001352 mbedtls_ssl_send_t *f_send,
1353 mbedtls_ssl_recv_t *f_recv,
1354 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001355{
1356 ssl->p_bio = p_bio;
1357 ssl->f_send = f_send;
1358 ssl->f_recv = f_recv;
1359 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001360}
1361
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001362#if defined(MBEDTLS_SSL_PROTO_DTLS)
1363void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1364{
1365 ssl->mtu = mtu;
1366}
1367#endif
1368
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001369void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001370{
1371 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001372}
1373
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001374void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1375 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001376 mbedtls_ssl_set_timer_t *f_set_timer,
1377 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001378{
1379 ssl->p_timer = p_timer;
1380 ssl->f_set_timer = f_set_timer;
1381 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001382
1383 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001384 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001385}
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001388void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001389 void *p_cache,
1390 mbedtls_ssl_cache_get_t *f_get_cache,
1391 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001392{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001393 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001394 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001395 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_SSL_CLI_C)
1400int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401{
Janos Follath865b3eb2019-12-16 11:46:15 +00001402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001403
1404 if( ssl == NULL ||
1405 session == NULL ||
1406 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001407 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001410 }
1411
Hanno Beckere810bbc2021-05-14 16:01:05 +01001412 if( ssl->handshake->resume == 1 )
1413 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1414
Jerry Yu21092062022-10-10 21:21:31 +08001415#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1416 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001417 {
Jerry Yu21092062022-10-10 21:21:31 +08001418 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1419 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1420
1421 if( mbedtls_ssl_validate_ciphersuite(
1422 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1423 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1424 {
1425 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1426 session->ciphersuite ) );
1427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1428 }
Jerry Yu40afab62022-10-08 10:42:13 +08001429 }
Jerry Yu21092062022-10-10 21:21:31 +08001430#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001431
Hanno Becker52055ae2019-02-06 14:30:46 +00001432 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1433 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001434 return( ret );
1435
Paul Bakker0a597072012-09-25 21:55:46 +00001436 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001437
1438 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001442void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1443 const int *ciphersuites )
1444{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001445 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001446}
1447
Ronald Cron6f135e12021-12-08 16:57:54 +01001448#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001449void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001450 const int kex_modes )
1451{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001452 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001453}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001454
1455#if defined(MBEDTLS_SSL_EARLY_DATA)
1456void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001457 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001458{
1459 conf->early_data_enabled = early_data_enabled;
1460}
1461#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001462#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001465void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001466 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001467{
1468 conf->cert_profile = profile;
1469}
1470
Glenn Strauss36872db2022-01-22 05:06:31 -05001471static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1472{
1473 mbedtls_ssl_key_cert *cur = key_cert, *next;
1474
1475 while( cur != NULL )
1476 {
1477 next = cur->next;
1478 mbedtls_free( cur );
1479 cur = next;
1480 }
1481}
1482
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001483/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001484MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001485static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1486 mbedtls_x509_crt *cert,
1487 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001488{
niisato8ee24222018-06-25 19:05:48 +09001489 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001490
Glenn Strauss36872db2022-01-22 05:06:31 -05001491 if( cert == NULL )
1492 {
1493 /* Free list if cert is null */
1494 ssl_key_cert_free( *head );
1495 *head = NULL;
1496 return( 0 );
1497 }
1498
niisato8ee24222018-06-25 19:05:48 +09001499 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1500 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001501 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001502
niisato8ee24222018-06-25 19:05:48 +09001503 new_cert->cert = cert;
1504 new_cert->key = key;
1505 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001506
Glenn Strauss36872db2022-01-22 05:06:31 -05001507 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001508 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001509 {
niisato8ee24222018-06-25 19:05:48 +09001510 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001511 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001512 else
1513 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001514 mbedtls_ssl_key_cert *cur = *head;
1515 while( cur->next != NULL )
1516 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001517 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001518 }
1519
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001520 return( 0 );
1521}
1522
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001523int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001524 mbedtls_x509_crt *own_cert,
1525 mbedtls_pk_context *pk_key )
1526{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001527 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001528}
1529
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001530void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001531 mbedtls_x509_crt *ca_chain,
1532 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001533{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001534 conf->ca_chain = ca_chain;
1535 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001536
1537#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1538 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1539 * cannot be used together. */
1540 conf->f_ca_cb = NULL;
1541 conf->p_ca_cb = NULL;
1542#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001543}
Hanno Becker5adaad92019-03-27 16:54:37 +00001544
1545#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1546void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001547 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001548 void *p_ca_cb )
1549{
1550 conf->f_ca_cb = f_ca_cb;
1551 conf->p_ca_cb = p_ca_cb;
1552
1553 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1554 * cannot be used together. */
1555 conf->ca_chain = NULL;
1556 conf->ca_crl = NULL;
1557}
1558#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001560
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001561#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001562const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1563 size_t *name_len )
1564{
1565 *name_len = ssl->handshake->sni_name_len;
1566 return( ssl->handshake->sni_name );
1567}
1568
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001569int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1570 mbedtls_x509_crt *own_cert,
1571 mbedtls_pk_context *pk_key )
1572{
1573 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1574 own_cert, pk_key ) );
1575}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001576
1577void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1578 mbedtls_x509_crt *ca_chain,
1579 mbedtls_x509_crl *ca_crl )
1580{
1581 ssl->handshake->sni_ca_chain = ca_chain;
1582 ssl->handshake->sni_ca_crl = ca_crl;
1583}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001584
Glenn Strauss999ef702022-03-11 01:37:23 -05001585#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1586void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1587 const mbedtls_x509_crt *crt)
1588{
1589 ssl->handshake->dn_hints = crt;
1590}
1591#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1592
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001593void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1594 int authmode )
1595{
1596 ssl->handshake->sni_authmode = authmode;
1597}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001598#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1599
Hanno Becker8927c832019-04-03 12:52:50 +01001600#if defined(MBEDTLS_X509_CRT_PARSE_C)
1601void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1602 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1603 void *p_vrfy )
1604{
1605 ssl->f_vrfy = f_vrfy;
1606 ssl->p_vrfy = p_vrfy;
1607}
1608#endif
1609
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001610#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001611/*
1612 * Set EC J-PAKE password for current handshake
1613 */
1614int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1615 const unsigned char *pw,
1616 size_t pw_len )
1617{
1618 mbedtls_ecjpake_role role;
1619
Janos Follath8eb64132016-06-03 15:40:57 +01001620 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1622
1623 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1624 role = MBEDTLS_ECJPAKE_SERVER;
1625 else
1626 role = MBEDTLS_ECJPAKE_CLIENT;
1627
1628 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1629 role,
1630 MBEDTLS_MD_SHA256,
1631 MBEDTLS_ECP_DP_SECP256R1,
1632 pw, pw_len ) );
1633}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001634#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001635
Ronald Cron73fe8df2022-10-05 14:31:43 +02001636#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001637int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001638{
Ronald Crond29e13e2022-10-19 10:33:48 +02001639 if( conf->psk_identity == NULL ||
1640 conf->psk_identity_len == 0 )
1641 {
1642 return( 0 );
1643 }
1644
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001645#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001646 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001647 return( 1 );
1648#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001649
1650 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001651 return( 1 );
1652
1653 return( 0 );
1654}
1655
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001656static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1657{
1658 /* Remove reference to existing PSK, if any. */
1659#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001660 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001661 {
1662 /* The maintenance of the PSK key slot is the
1663 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001664 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001665 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001666#endif /* MBEDTLS_USE_PSA_CRYPTO */
1667 if( conf->psk != NULL )
1668 {
1669 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1670
1671 mbedtls_free( conf->psk );
1672 conf->psk = NULL;
1673 conf->psk_len = 0;
1674 }
1675
1676 /* Remove reference to PSK identity, if any. */
1677 if( conf->psk_identity != NULL )
1678 {
1679 mbedtls_free( conf->psk_identity );
1680 conf->psk_identity = NULL;
1681 conf->psk_identity_len = 0;
1682 }
1683}
1684
Hanno Becker7390c712018-11-15 13:33:04 +00001685/* This function assumes that PSK identity in the SSL config is unset.
1686 * It checks that the provided identity is well-formed and attempts
1687 * to make a copy of it in the SSL config.
1688 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001689MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001690static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1691 unsigned char const *psk_identity,
1692 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001693{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001694 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001695 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001696 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001697 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001698 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001699 {
1700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1701 }
1702
Hanno Becker7390c712018-11-15 13:33:04 +00001703 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1704 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001705 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001706
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001707 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001708 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001709
1710 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001711}
1712
Hanno Becker7390c712018-11-15 13:33:04 +00001713int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1714 const unsigned char *psk, size_t psk_len,
1715 const unsigned char *psk_identity, size_t psk_identity_len )
1716{
Janos Follath865b3eb2019-12-16 11:46:15 +00001717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001718
1719 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001720 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001721 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001722
1723 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001724 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001726 if( psk_len == 0 )
1727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1728 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1729 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1730
Hanno Becker7390c712018-11-15 13:33:04 +00001731 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1732 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1733 conf->psk_len = psk_len;
1734 memcpy( conf->psk, psk, conf->psk_len );
1735
1736 /* Check and set PSK Identity */
1737 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1738 if( ret != 0 )
1739 ssl_conf_remove_psk( conf );
1740
1741 return( ret );
1742}
1743
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001744static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1745{
1746#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001747 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001748 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001749 /* The maintenance of the external PSK key slot is the
1750 * user's responsibility. */
1751 if( ssl->handshake->psk_opaque_is_internal )
1752 {
1753 psa_destroy_key( ssl->handshake->psk_opaque );
1754 ssl->handshake->psk_opaque_is_internal = 0;
1755 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001756 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001757 }
Neil Armstronge952a302022-05-03 10:22:14 +02001758#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001759 if( ssl->handshake->psk != NULL )
1760 {
1761 mbedtls_platform_zeroize( ssl->handshake->psk,
1762 ssl->handshake->psk_len );
1763 mbedtls_free( ssl->handshake->psk );
1764 ssl->handshake->psk_len = 0;
1765 }
Neil Armstronge952a302022-05-03 10:22:14 +02001766#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001767}
1768
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001769int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1770 const unsigned char *psk, size_t psk_len )
1771{
Neil Armstrong501c9322022-05-03 09:35:09 +02001772#if defined(MBEDTLS_USE_PSA_CRYPTO)
1773 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001775 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001777#endif /* MBEDTLS_USE_PSA_CRYPTO */
1778
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001779 if( psk == NULL || ssl->handshake == NULL )
1780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1781
1782 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1784
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001785 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001786
Neil Armstrong501c9322022-05-03 09:35:09 +02001787#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001788#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1789 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1790 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001791 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001792 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1793 else
1794 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1795 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1796 }
1797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001798
Jerry Yu568ec252022-07-22 21:27:34 +08001799#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001800 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1801 {
1802 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1803 psa_set_key_usage_flags( &key_attributes,
1804 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1805 }
1806#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1807
Neil Armstrong501c9322022-05-03 09:35:09 +02001808 psa_set_key_algorithm( &key_attributes, alg );
1809 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1810
1811 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1812 if( status != PSA_SUCCESS )
1813 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1814
1815 /* Allow calling psa_destroy_key() on psk remove */
1816 ssl->handshake->psk_opaque_is_internal = 1;
1817 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1818#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001819 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001820 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001821
1822 ssl->handshake->psk_len = psk_len;
1823 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1824
1825 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001827}
1828
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001829#if defined(MBEDTLS_USE_PSA_CRYPTO)
1830int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001831 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001832 const unsigned char *psk_identity,
1833 size_t psk_identity_len )
1834{
Janos Follath865b3eb2019-12-16 11:46:15 +00001835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001836
1837 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001838 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001839 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001840
Hanno Becker7390c712018-11-15 13:33:04 +00001841 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001842 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001844 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001845
1846 /* Check and set PSK Identity */
1847 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1848 psk_identity_len );
1849 if( ret != 0 )
1850 ssl_conf_remove_psk( conf );
1851
1852 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001853}
1854
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001855int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1856 mbedtls_svc_key_id_t psk )
1857{
1858 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1859 ( ssl->handshake == NULL ) )
1860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1861
1862 ssl_remove_psk( ssl );
1863 ssl->handshake->psk_opaque = psk;
1864 return( 0 );
1865}
1866#endif /* MBEDTLS_USE_PSA_CRYPTO */
1867
Jerry Yu8897c072022-08-12 13:56:53 +08001868#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001869void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1870 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1871 size_t),
1872 void *p_psk )
1873{
1874 conf->f_psk = f_psk;
1875 conf->p_psk = p_psk;
1876}
Jerry Yu8897c072022-08-12 13:56:53 +08001877#endif /* MBEDTLS_SSL_SRV_C */
1878
Ronald Cron73fe8df2022-10-05 14:31:43 +02001879#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001880
1881#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001882static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1883 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001884{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001885#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001886 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001887 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001888#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001889 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001890 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001891 return( MBEDTLS_SSL_MODE_STREAM );
1892}
1893
1894#else /* MBEDTLS_USE_PSA_CRYPTO */
1895
1896static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1897 mbedtls_cipher_mode_t mode )
1898{
1899#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1900 if( mode == MBEDTLS_MODE_CBC )
1901 return( MBEDTLS_SSL_MODE_CBC );
1902#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1903
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001904#if defined(MBEDTLS_GCM_C) || \
1905 defined(MBEDTLS_CCM_C) || \
1906 defined(MBEDTLS_CHACHAPOLY_C)
1907 if( mode == MBEDTLS_MODE_GCM ||
1908 mode == MBEDTLS_MODE_CCM ||
1909 mode == MBEDTLS_MODE_CHACHAPOLY )
1910 return( MBEDTLS_SSL_MODE_AEAD );
1911#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001912
1913 return( MBEDTLS_SSL_MODE_STREAM );
1914}
Gilles Peskine301711e2022-04-26 16:57:05 +02001915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001916
Gilles Peskinee108d982022-04-26 16:50:40 +02001917static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1918 mbedtls_ssl_mode_t base_mode,
1919 int encrypt_then_mac )
1920{
1921#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1922 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1923 base_mode == MBEDTLS_SSL_MODE_CBC )
1924 {
1925 return( MBEDTLS_SSL_MODE_CBC_ETM );
1926 }
1927#else
1928 (void) encrypt_then_mac;
1929#endif
1930 return( base_mode );
1931}
1932
Neil Armstrongab555e02022-04-04 11:07:59 +02001933mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001934 const mbedtls_ssl_transform *transform )
1935{
Gilles Peskinee108d982022-04-26 16:50:40 +02001936 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001937#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001938 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001939#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001940 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1941#endif
1942 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001943
Gilles Peskinee108d982022-04-26 16:50:40 +02001944 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001945#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001946 encrypt_then_mac = transform->encrypt_then_mac;
1947#endif
1948 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001949}
1950
Neil Armstrongab555e02022-04-04 11:07:59 +02001951mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001952#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001953 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001954#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001955 const mbedtls_ssl_ciphersuite_t *suite )
1956{
Gilles Peskinee108d982022-04-26 16:50:40 +02001957 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1958
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001959#if defined(MBEDTLS_USE_PSA_CRYPTO)
1960 psa_status_t status;
1961 psa_algorithm_t alg;
1962 psa_key_type_t type;
1963 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001964 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1965 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001966 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001967#else
1968 const mbedtls_cipher_info_t *cipher =
1969 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001970 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001971 {
1972 base_mode =
1973 mbedtls_ssl_get_base_mode(
1974 mbedtls_cipher_info_get_mode( cipher ) );
1975 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001976#endif /* MBEDTLS_USE_PSA_CRYPTO */
1977
Gilles Peskinee108d982022-04-26 16:50:40 +02001978#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1979 int encrypt_then_mac = 0;
1980#endif
1981 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001982}
1983
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001984#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001985
1986#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001987/* Serialization of TLS 1.3 sessions:
1988 *
1989 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001990 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001991 * uint64 ticket_received;
1992 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001993 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001994 * } ClientOnlyData;
1995 *
1996 * struct {
1997 * uint8 endpoint;
1998 * uint8 ciphersuite[2];
1999 * uint32 ticket_age_add;
2000 * uint8 ticket_flags;
2001 * opaque resumption_key<0..255>;
2002 * select ( endpoint ) {
2003 * case client: ClientOnlyData;
2004 * case server: uint64 start_time;
2005 * };
2006 * } serialized_session_tls13;
2007 *
2008 */
2009#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002010MBEDTLS_CHECK_RETURN_CRITICAL
2011static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2012 unsigned char *buf,
2013 size_t buf_len,
2014 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002015{
2016 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002017#if defined(MBEDTLS_SSL_CLI_C) && \
2018 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2019 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002020 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002021#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002022 size_t needed = 1 /* endpoint */
2023 + 2 /* ciphersuite */
2024 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002025 + 1 /* ticket_flags */
2026 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002027 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002028
2029 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2031 needed += session->resumption_key_len; /* resumption_key */
2032
Jerry Yu438ddd82022-07-07 06:55:50 +00002033#if defined(MBEDTLS_HAVE_TIME)
2034 needed += 8; /* start_time or ticket_received */
2035#endif
2036
2037#if defined(MBEDTLS_SSL_CLI_C)
2038 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2039 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002040#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002041 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002042 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002043#endif
2044
Jerry Yu438ddd82022-07-07 06:55:50 +00002045 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002046 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002047
2048 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002049 if( session->ticket_len > SIZE_MAX - needed )
2050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002051
Jerry Yue28d9742022-08-18 15:44:03 +08002052 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002053 }
2054#endif /* MBEDTLS_SSL_CLI_C */
2055
Jerry Yue36fdd62022-08-17 21:31:36 +08002056 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002057 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002058 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002059
2060 p[0] = session->endpoint;
2061 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2062 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2063 p[7] = session->ticket_flags;
2064
2065 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002066 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002067 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002068 memcpy( p, session->resumption_key, session->resumption_key_len );
2069 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002070
2071#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2072 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2073 {
2074 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2075 p += 8;
2076 }
2077#endif /* MBEDTLS_HAVE_TIME */
2078
2079#if defined(MBEDTLS_SSL_CLI_C)
2080 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2081 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002082#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2083 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2084 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002085 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002086 {
2087 /* save host name */
2088 memcpy( p, session->hostname, hostname_len );
2089 p += hostname_len;
2090 }
2091#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2092
Jerry Yu438ddd82022-07-07 06:55:50 +00002093#if defined(MBEDTLS_HAVE_TIME)
2094 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2095 p += 8;
2096#endif
2097 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2098 p += 4;
2099
2100 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2101 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002102
2103 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002104 {
2105 memcpy( p, session->ticket, session->ticket_len );
2106 p += session->ticket_len;
2107 }
2108 }
2109#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002110 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002111}
2112
2113MBEDTLS_CHECK_RETURN_CRITICAL
2114static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2115 const unsigned char *buf,
2116 size_t len )
2117{
2118 const unsigned char *p = buf;
2119 const unsigned char *end = buf + len;
2120
2121 if( end - p < 9 )
2122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2123 session->endpoint = p[0];
2124 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2125 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2126 session->ticket_flags = p[7];
2127
2128 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002129 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002130 p += 9;
2131
Jerry Yubc7c1a42022-07-21 22:57:37 +08002132 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2134
Jerry Yubc7c1a42022-07-21 22:57:37 +08002135 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002137 memcpy( session->resumption_key, p, session->resumption_key_len );
2138 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002139
2140#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2141 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2142 {
2143 if( end - p < 8 )
2144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2145 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2146 p += 8;
2147 }
2148#endif /* MBEDTLS_HAVE_TIME */
2149
2150#if defined(MBEDTLS_SSL_CLI_C)
2151 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2152 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002153#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2154 defined(MBEDTLS_SSL_SESSION_TICKETS)
2155 size_t hostname_len;
2156 /* load host name */
2157 if( end - p < 2 )
2158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2159 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2160 p += 2;
2161
2162 if( end - p < ( long int )hostname_len )
2163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2164 if( hostname_len > 0 )
2165 {
2166 session->hostname = mbedtls_calloc( 1, hostname_len );
2167 if( session->hostname == NULL )
2168 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2169 memcpy( session->hostname, p, hostname_len );
2170 p += hostname_len;
2171 }
2172#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2173 MBEDTLS_SSL_SESSION_TICKETS */
2174
Jerry Yu438ddd82022-07-07 06:55:50 +00002175#if defined(MBEDTLS_HAVE_TIME)
2176 if( end - p < 8 )
2177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2178 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2179 p += 8;
2180#endif
2181 if( end - p < 4 )
2182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2183 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2184 p += 4;
2185
2186 if( end - p < 2 )
2187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2188 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2189 p += 2;
2190
2191 if( end - p < ( long int )session->ticket_len )
2192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2193 if( session->ticket_len > 0 )
2194 {
2195 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2196 if( session->ticket == NULL )
2197 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2198 memcpy( session->ticket, p, session->ticket_len );
2199 p += session->ticket_len;
2200 }
2201 }
2202#endif /* MBEDTLS_SSL_CLI_C */
2203
2204 return( 0 );
2205
2206}
2207#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002208MBEDTLS_CHECK_RETURN_CRITICAL
2209static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2210 unsigned char *buf,
2211 size_t buf_len,
2212 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002213{
2214 ((void) session);
2215 ((void) buf);
2216 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002217 *olen = 0;
2218 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002219}
Jerry Yu438ddd82022-07-07 06:55:50 +00002220
2221static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2222 unsigned char *buf,
2223 size_t buf_len )
2224{
2225 ((void) session);
2226 ((void) buf);
2227 ((void) buf_len);
2228 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2229}
2230#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002231#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2232
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002233psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002234 size_t taglen,
2235 psa_algorithm_t *alg,
2236 psa_key_type_t *key_type,
2237 size_t *key_size )
2238{
2239 switch ( mbedtls_cipher_type )
2240 {
2241 case MBEDTLS_CIPHER_AES_128_CBC:
2242 *alg = PSA_ALG_CBC_NO_PADDING;
2243 *key_type = PSA_KEY_TYPE_AES;
2244 *key_size = 128;
2245 break;
2246 case MBEDTLS_CIPHER_AES_128_CCM:
2247 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2248 *key_type = PSA_KEY_TYPE_AES;
2249 *key_size = 128;
2250 break;
2251 case MBEDTLS_CIPHER_AES_128_GCM:
2252 *alg = PSA_ALG_GCM;
2253 *key_type = PSA_KEY_TYPE_AES;
2254 *key_size = 128;
2255 break;
2256 case MBEDTLS_CIPHER_AES_192_CCM:
2257 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2258 *key_type = PSA_KEY_TYPE_AES;
2259 *key_size = 192;
2260 break;
2261 case MBEDTLS_CIPHER_AES_192_GCM:
2262 *alg = PSA_ALG_GCM;
2263 *key_type = PSA_KEY_TYPE_AES;
2264 *key_size = 192;
2265 break;
2266 case MBEDTLS_CIPHER_AES_256_CBC:
2267 *alg = PSA_ALG_CBC_NO_PADDING;
2268 *key_type = PSA_KEY_TYPE_AES;
2269 *key_size = 256;
2270 break;
2271 case MBEDTLS_CIPHER_AES_256_CCM:
2272 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2273 *key_type = PSA_KEY_TYPE_AES;
2274 *key_size = 256;
2275 break;
2276 case MBEDTLS_CIPHER_AES_256_GCM:
2277 *alg = PSA_ALG_GCM;
2278 *key_type = PSA_KEY_TYPE_AES;
2279 *key_size = 256;
2280 break;
2281 case MBEDTLS_CIPHER_ARIA_128_CBC:
2282 *alg = PSA_ALG_CBC_NO_PADDING;
2283 *key_type = PSA_KEY_TYPE_ARIA;
2284 *key_size = 128;
2285 break;
2286 case MBEDTLS_CIPHER_ARIA_128_CCM:
2287 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2288 *key_type = PSA_KEY_TYPE_ARIA;
2289 *key_size = 128;
2290 break;
2291 case MBEDTLS_CIPHER_ARIA_128_GCM:
2292 *alg = PSA_ALG_GCM;
2293 *key_type = PSA_KEY_TYPE_ARIA;
2294 *key_size = 128;
2295 break;
2296 case MBEDTLS_CIPHER_ARIA_192_CCM:
2297 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2298 *key_type = PSA_KEY_TYPE_ARIA;
2299 *key_size = 192;
2300 break;
2301 case MBEDTLS_CIPHER_ARIA_192_GCM:
2302 *alg = PSA_ALG_GCM;
2303 *key_type = PSA_KEY_TYPE_ARIA;
2304 *key_size = 192;
2305 break;
2306 case MBEDTLS_CIPHER_ARIA_256_CBC:
2307 *alg = PSA_ALG_CBC_NO_PADDING;
2308 *key_type = PSA_KEY_TYPE_ARIA;
2309 *key_size = 256;
2310 break;
2311 case MBEDTLS_CIPHER_ARIA_256_CCM:
2312 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2313 *key_type = PSA_KEY_TYPE_ARIA;
2314 *key_size = 256;
2315 break;
2316 case MBEDTLS_CIPHER_ARIA_256_GCM:
2317 *alg = PSA_ALG_GCM;
2318 *key_type = PSA_KEY_TYPE_ARIA;
2319 *key_size = 256;
2320 break;
2321 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2322 *alg = PSA_ALG_CBC_NO_PADDING;
2323 *key_type = PSA_KEY_TYPE_CAMELLIA;
2324 *key_size = 128;
2325 break;
2326 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2327 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2328 *key_type = PSA_KEY_TYPE_CAMELLIA;
2329 *key_size = 128;
2330 break;
2331 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2332 *alg = PSA_ALG_GCM;
2333 *key_type = PSA_KEY_TYPE_CAMELLIA;
2334 *key_size = 128;
2335 break;
2336 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2337 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2338 *key_type = PSA_KEY_TYPE_CAMELLIA;
2339 *key_size = 192;
2340 break;
2341 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2342 *alg = PSA_ALG_GCM;
2343 *key_type = PSA_KEY_TYPE_CAMELLIA;
2344 *key_size = 192;
2345 break;
2346 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2347 *alg = PSA_ALG_CBC_NO_PADDING;
2348 *key_type = PSA_KEY_TYPE_CAMELLIA;
2349 *key_size = 256;
2350 break;
2351 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2352 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2353 *key_type = PSA_KEY_TYPE_CAMELLIA;
2354 *key_size = 256;
2355 break;
2356 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2357 *alg = PSA_ALG_GCM;
2358 *key_type = PSA_KEY_TYPE_CAMELLIA;
2359 *key_size = 256;
2360 break;
2361 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2362 *alg = PSA_ALG_CHACHA20_POLY1305;
2363 *key_type = PSA_KEY_TYPE_CHACHA20;
2364 *key_size = 256;
2365 break;
2366 case MBEDTLS_CIPHER_NULL:
2367 *alg = MBEDTLS_SSL_NULL_CIPHER;
2368 *key_type = 0;
2369 *key_size = 0;
2370 break;
2371 default:
2372 return PSA_ERROR_NOT_SUPPORTED;
2373 }
2374
2375 return PSA_SUCCESS;
2376}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002377#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002378
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002379#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002380int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2381 const unsigned char *dhm_P, size_t P_len,
2382 const unsigned char *dhm_G, size_t G_len )
2383{
Janos Follath865b3eb2019-12-16 11:46:15 +00002384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002385
Glenn Strausscee11292021-12-20 01:43:17 -05002386 mbedtls_mpi_free( &conf->dhm_P );
2387 mbedtls_mpi_free( &conf->dhm_G );
2388
Hanno Beckera90658f2017-10-04 15:29:08 +01002389 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2390 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2391 {
2392 mbedtls_mpi_free( &conf->dhm_P );
2393 mbedtls_mpi_free( &conf->dhm_G );
2394 return( ret );
2395 }
2396
2397 return( 0 );
2398}
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002400int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002401{
Janos Follath865b3eb2019-12-16 11:46:15 +00002402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002403
Glenn Strausscee11292021-12-20 01:43:17 -05002404 mbedtls_mpi_free( &conf->dhm_P );
2405 mbedtls_mpi_free( &conf->dhm_G );
2406
Gilles Peskinee5702482021-06-11 21:59:08 +02002407 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2408 &conf->dhm_P ) ) != 0 ||
2409 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2410 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002411 {
2412 mbedtls_mpi_free( &conf->dhm_P );
2413 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002414 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002415 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002416
2417 return( 0 );
2418}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002419#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002420
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002421#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2422/*
2423 * Set the minimum length for Diffie-Hellman parameters
2424 */
2425void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2426 unsigned int bitlen )
2427{
2428 conf->dhm_min_bitlen = bitlen;
2429}
2430#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2431
Ronald Crone68ab4f2022-10-05 12:46:29 +02002432#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002433#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002434/*
2435 * Set allowed/preferred hashes for handshake signatures
2436 */
2437void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2438 const int *hashes )
2439{
2440 conf->sig_hashes = hashes;
2441}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002442#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002443
Jerry Yuf017ee42022-01-12 15:49:48 +08002444/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002445void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2446 const uint16_t* sig_algs )
2447{
Jerry Yuf017ee42022-01-12 15:49:48 +08002448#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2449 conf->sig_hashes = NULL;
2450#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2451 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002452}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002453#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002454
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002455#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002456#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002457/*
2458 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002459 *
2460 * mbedtls_ssl_setup() takes the provided list
2461 * and translates it to a list of IANA TLS group identifiers,
2462 * stored in ssl->handshake->group_list.
2463 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002464 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002465void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002466 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002468 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002469 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002470}
Brett Warrene0edc842021-08-17 09:53:13 +01002471#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002472#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002473
Brett Warrene0edc842021-08-17 09:53:13 +01002474/*
2475 * Set the allowed groups
2476 */
2477void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2478 const uint16_t *group_list )
2479{
2480#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2481 conf->curve_list = NULL;
2482#endif
2483 conf->group_list = group_list;
2484}
2485
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002486#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488{
Hanno Becker947194e2017-04-07 13:25:49 +01002489 /* Initialize to suppress unnecessary compiler warning */
2490 size_t hostname_len = 0;
2491
2492 /* Check if new hostname is valid before
2493 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002494 if( hostname != NULL )
2495 {
2496 hostname_len = strlen( hostname );
2497
2498 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2500 }
2501
2502 /* Now it's clear that we will overwrite the old hostname,
2503 * so we can free it safely */
2504
2505 if( ssl->hostname != NULL )
2506 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002507 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002508 mbedtls_free( ssl->hostname );
2509 }
2510
2511 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002512
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002514 {
2515 ssl->hostname = NULL;
2516 }
2517 else
2518 {
2519 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002520 if( ssl->hostname == NULL )
2521 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002522
Hanno Becker947194e2017-04-07 13:25:49 +01002523 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002524
Hanno Becker947194e2017-04-07 13:25:49 +01002525 ssl->hostname[hostname_len] = '\0';
2526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 return( 0 );
2529}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002530#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002532#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002533void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002535 const unsigned char *, size_t),
2536 void *p_sni )
2537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002538 conf->f_sni = f_sni;
2539 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002544int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002545{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002546 size_t cur_len, tot_len;
2547 const char **p;
2548
2549 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002550 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2551 * MUST NOT be truncated."
2552 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002553 */
2554 tot_len = 0;
2555 for( p = protos; *p != NULL; p++ )
2556 {
2557 cur_len = strlen( *p );
2558 tot_len += cur_len;
2559
Ronald Cron8216dd32020-04-23 16:41:44 +02002560 if( ( cur_len == 0 ) ||
2561 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2562 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002564 }
2565
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002566 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002567
2568 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002569}
2570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002572{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002573 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002576
Johan Pascalb62bb512015-12-03 21:56:45 +01002577#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002578void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2579 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002580{
2581 conf->dtls_srtp_mki_support = support_mki_value;
2582}
2583
Ron Eldoref72faf2018-07-12 11:54:20 +03002584int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2585 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002586 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002587{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002588 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002589 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002591 }
Ron Eldor591f1622018-01-22 12:30:04 +02002592
2593 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002594 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002595 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002596 }
Ron Eldor591f1622018-01-22 12:30:04 +02002597
2598 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2599 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002600 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002601}
2602
Ron Eldoref72faf2018-07-12 11:54:20 +03002603int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002604 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002605{
Johan Pascal253d0262020-09-22 13:04:45 +02002606 const mbedtls_ssl_srtp_profile *p;
2607 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002608
Johan Pascal253d0262020-09-22 13:04:45 +02002609 /* check the profiles list: all entry must be valid,
2610 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002611 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2612 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2613 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002614 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002615 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002616 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002617 list_size++;
2618 }
2619 else
2620 {
2621 /* unsupported value, stop parsing and set the size to an error value */
2622 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002623 }
2624 }
2625
Johan Pascal5ef72d22020-10-28 17:05:47 +01002626 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002627 {
Johan Pascal253d0262020-09-22 13:04:45 +02002628 conf->dtls_srtp_profile_list = NULL;
2629 conf->dtls_srtp_profile_list_len = 0;
2630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2631 }
2632
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002633 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002634 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002635
2636 return( 0 );
2637}
2638
Johan Pascal5ef72d22020-10-28 17:05:47 +01002639void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2640 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002641{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002642 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2643 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002644 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002645 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002646 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002647 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002648 else
2649 {
2650 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002651 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2652 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002653 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002654}
Johan Pascalb62bb512015-12-03 21:56:45 +01002655#endif /* MBEDTLS_SSL_DTLS_SRTP */
2656
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302657#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002658void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002659{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002660 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002661}
2662
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002663void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002664{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002665 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002666}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302667#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002668
Janos Follath088ce432017-04-10 12:42:31 +01002669#if defined(MBEDTLS_SSL_SRV_C)
2670void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2671 char cert_req_ca_list )
2672{
2673 conf->cert_req_ca_list = cert_req_ca_list;
2674}
2675#endif
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002678void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002680 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002681}
2682#endif
2683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002685void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002687 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002688}
2689#endif
2690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002692int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002695 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002698 }
2699
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002700 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002701
2702 return( 0 );
2703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002705
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002706void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002707{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002708 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002709}
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002712void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002713{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002714 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002715}
2716
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002717void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002718{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002719 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002720}
2721
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002722void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002723 const unsigned char period[8] )
2724{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002725 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002730#if defined(MBEDTLS_SSL_CLI_C)
2731void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002732{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002733 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002734}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002735#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002736
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002737#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002738
Jerry Yud0766ec2022-09-22 10:46:57 +08002739#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002740void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2741 uint16_t num_tickets )
2742{
Jerry Yud0766ec2022-09-22 10:46:57 +08002743 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002744}
2745#endif
2746
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002747void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2748 mbedtls_ssl_ticket_write_t *f_ticket_write,
2749 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2750 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002751{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002752 conf->f_ticket_write = f_ticket_write;
2753 conf->f_ticket_parse = f_ticket_parse;
2754 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002755}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002758
Hanno Becker7e6c1782021-06-08 09:24:55 +01002759void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2760 mbedtls_ssl_export_keys_t *f_export_keys,
2761 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002762{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002763 ssl->f_export_keys = f_export_keys;
2764 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002765}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002766
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002767#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002768void mbedtls_ssl_conf_async_private_cb(
2769 mbedtls_ssl_config *conf,
2770 mbedtls_ssl_async_sign_t *f_async_sign,
2771 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2772 mbedtls_ssl_async_resume_t *f_async_resume,
2773 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002774 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002775{
2776 conf->f_async_sign_start = f_async_sign;
2777 conf->f_async_decrypt_start = f_async_decrypt;
2778 conf->f_async_resume = f_async_resume;
2779 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002780 conf->p_async_config_data = async_config_data;
2781}
2782
Gilles Peskine8f97af72018-04-26 11:46:10 +02002783void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2784{
2785 return( conf->p_async_config_data );
2786}
2787
Gilles Peskine1febfef2018-04-30 11:54:39 +02002788void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002789{
2790 if( ssl->handshake == NULL )
2791 return( NULL );
2792 else
2793 return( ssl->handshake->user_async_ctx );
2794}
2795
Gilles Peskine1febfef2018-04-30 11:54:39 +02002796void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002797 void *ctx )
2798{
2799 if( ssl->handshake != NULL )
2800 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002801}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002802#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002803
Paul Bakker5121ce52009-01-03 21:22:43 +00002804/*
2805 * SSL get accessors
2806 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002807uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002808{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002809 if( ssl->session != NULL )
2810 return( ssl->session->verify_result );
2811
2812 if( ssl->session_negotiate != NULL )
2813 return( ssl->session_negotiate->verify_result );
2814
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002815 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816}
2817
Glenn Strauss8f526902022-01-13 00:04:49 -05002818int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2819{
2820 if( ssl == NULL || ssl->session == NULL )
2821 return( 0 );
2822
2823 return( ssl->session->ciphersuite );
2824}
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002827{
Paul Bakker926c8e42013-03-06 10:23:34 +01002828 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002829 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002832}
2833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002837 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002838 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002839 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002840 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002841 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002842 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002843 default:
2844 return( "unknown (DTLS)" );
2845 }
2846 }
2847#endif
2848
Glenn Strauss60bfe602022-03-14 19:04:24 -04002849 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002850 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002851 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002853 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002854 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002855 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002856 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002857 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002858}
2859
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002860#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002861size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2862{
David Horstmann95d516f2021-05-04 18:36:56 +01002863 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002864 size_t read_mfl;
2865
2866 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2867 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2868 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2869 {
2870 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2871 }
2872
2873 /* Check if a smaller max length was negotiated */
2874 if( ssl->session_out != NULL )
2875 {
2876 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2877 if( read_mfl < max_len )
2878 {
2879 max_len = read_mfl;
2880 }
2881 }
2882
2883 // During a handshake, use the value being negotiated
2884 if( ssl->session_negotiate != NULL )
2885 {
2886 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2887 if( read_mfl < max_len )
2888 {
2889 max_len = read_mfl;
2890 }
2891 }
2892
2893 return( max_len );
2894}
2895
2896size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002897{
2898 size_t max_len;
2899
2900 /*
2901 * Assume mfl_code is correct since it was checked when set
2902 */
Angus Grattond8213d02016-05-25 20:56:48 +10002903 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002904
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002905 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002906 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002907 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002908 {
Angus Grattond8213d02016-05-25 20:56:48 +10002909 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002910 }
2911
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002912 /* During a handshake, use the value being negotiated */
2913 if( ssl->session_negotiate != NULL &&
2914 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2915 {
2916 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2917 }
2918
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002919 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002920}
2921#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2922
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002924size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002925{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002926 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2927 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2928 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2929 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2930 return ( 0 );
2931
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002932 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2933 return( ssl->mtu );
2934
2935 if( ssl->mtu == 0 )
2936 return( ssl->handshake->mtu );
2937
2938 return( ssl->mtu < ssl->handshake->mtu ?
2939 ssl->mtu : ssl->handshake->mtu );
2940}
2941#endif /* MBEDTLS_SSL_PROTO_DTLS */
2942
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002943int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2944{
2945 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2946
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002947#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2948 !defined(MBEDTLS_SSL_PROTO_DTLS)
2949 (void) ssl;
2950#endif
2951
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002952#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002953 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002954
2955 if( max_len > mfl )
2956 max_len = mfl;
2957#endif
2958
2959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002960 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002961 {
Hanno Becker89490712020-02-05 10:50:12 +00002962 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002963 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2964 const size_t overhead = (size_t) ret;
2965
2966 if( ret < 0 )
2967 return( ret );
2968
2969 if( mtu <= overhead )
2970 {
2971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2972 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2973 }
2974
2975 if( max_len > mtu - overhead )
2976 max_len = mtu - overhead;
2977 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002978#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002979
Hanno Becker0defedb2018-08-10 12:35:02 +01002980#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2981 !defined(MBEDTLS_SSL_PROTO_DTLS)
2982 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002983#endif
2984
2985 return( (int) max_len );
2986}
2987
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002988int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2989{
2990 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2991
2992#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2993 (void) ssl;
2994#endif
2995
2996#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2997 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2998
2999 if( max_len > mfl )
3000 max_len = mfl;
3001#endif
3002
3003 return( (int) max_len );
3004}
3005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#if defined(MBEDTLS_X509_CRT_PARSE_C)
3007const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003008{
3009 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003010 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003011
Hanno Beckere6824572019-02-07 13:18:46 +00003012#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003013 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003014#else
3015 return( NULL );
3016#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003017}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003021int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3022 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003023{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003024 int ret;
3025
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003026 if( ssl == NULL ||
3027 dst == NULL ||
3028 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003029 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003032 }
3033
Hanno Beckere810bbc2021-05-14 16:01:05 +01003034 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3035 * idempotent: Each session can only be exported once.
3036 *
3037 * (This is in preparation for TLS 1.3 support where we will
3038 * need the ability to export multiple sessions (aka tickets),
3039 * which will be achieved by calling mbedtls_ssl_get_session()
3040 * multiple times until it fails.)
3041 *
3042 * Check whether we have already exported the current session,
3043 * and fail if so.
3044 */
3045 if( ssl->session->exported == 1 )
3046 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3047
3048 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3049 if( ret != 0 )
3050 return( ret );
3051
3052 /* Remember that we've exported the session. */
3053 ssl->session->exported = 1;
3054 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003057
Paul Bakker5121ce52009-01-03 21:22:43 +00003058/*
Hanno Beckera835da52019-05-16 12:39:07 +01003059 * Define ticket header determining Mbed TLS version
3060 * and structure of the ticket.
3061 */
3062
Hanno Becker94ef3b32019-05-16 12:50:45 +01003063/*
Hanno Becker50b59662019-05-28 14:30:45 +01003064 * Define bitflag determining compile-time settings influencing
3065 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003066 */
3067
Hanno Becker50b59662019-05-28 14:30:45 +01003068#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003069#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003070#else
Hanno Becker3e088662019-05-29 11:10:18 +01003071#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003072#endif /* MBEDTLS_HAVE_TIME */
3073
3074#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003075#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003076#else
Hanno Becker3e088662019-05-29 11:10:18 +01003077#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003078#endif /* MBEDTLS_X509_CRT_PARSE_C */
3079
3080#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003081#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003082#else
Hanno Becker3e088662019-05-29 11:10:18 +01003083#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003084#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3085
3086#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003087#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003088#else
Hanno Becker3e088662019-05-29 11:10:18 +01003089#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003090#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3091
Hanno Becker94ef3b32019-05-16 12:50:45 +01003092#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003093#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003094#else
Hanno Becker3e088662019-05-29 11:10:18 +01003095#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003096#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3097
Hanno Becker94ef3b32019-05-16 12:50:45 +01003098#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3099#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3100#else
3101#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3102#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3103
Hanno Becker3e088662019-05-29 11:10:18 +01003104#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3105#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3106#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3107#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003108#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3109#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003110
Hanno Becker50b59662019-05-28 14:30:45 +01003111#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003112 ( (uint16_t) ( \
3113 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3114 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3115 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3116 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003117 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003118 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003119
Hanno Beckerf8787072019-05-16 12:41:07 +01003120static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003121 MBEDTLS_VERSION_MAJOR,
3122 MBEDTLS_VERSION_MINOR,
3123 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003124 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3125 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003126};
Hanno Beckera835da52019-05-16 12:39:07 +01003127
3128/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003129 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003130 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003131 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003132 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003133 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003134 * opaque mbedtls_version[3]; // library version: major, minor, patch
3135 * opaque session_format[2]; // library-version specific 16-bit field
3136 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003137 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003138 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003139 * Note: When updating the format, remember to keep
3140 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003141 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003142 * // In this version, `session_format` determines
3143 * // the setting of those compile-time
3144 * // configuration options which influence
3145 * // the structure of mbedtls_ssl_session.
3146 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003147 * uint8_t minor_ver; // Protocol minor version. Possible values:
3148 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003149 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003150 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003151 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003152 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003153 * serialized_session_tls12 data;
3154 *
3155 * };
3156 *
3157 * } serialized_session;
3158 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003159 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003160
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003161MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003162static int ssl_session_save( const mbedtls_ssl_session *session,
3163 unsigned char omit_header,
3164 unsigned char *buf,
3165 size_t buf_len,
3166 size_t *olen )
3167{
3168 unsigned char *p = buf;
3169 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003170 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003171#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3172 size_t out_len;
3173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3174#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003175 if( session == NULL )
3176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3177
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003178 if( !omit_header )
3179 {
3180 /*
3181 * Add Mbed TLS version identifier
3182 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003183 used += sizeof( ssl_serialized_session_header );
3184
3185 if( used <= buf_len )
3186 {
3187 memcpy( p, ssl_serialized_session_header,
3188 sizeof( ssl_serialized_session_header ) );
3189 p += sizeof( ssl_serialized_session_header );
3190 }
3191 }
3192
3193 /*
3194 * TLS version identifier
3195 */
3196 used += 1;
3197 if( used <= buf_len )
3198 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003199 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003200 }
3201
3202 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003203 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003204 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003205 {
3206#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003207 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003208 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003209 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003210#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3211
Jerry Yu251a12e2022-07-13 15:15:48 +08003212#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3213 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003214 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3215 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003216 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003217 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003218 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003219#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3220
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003221 default:
3222 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3223 }
3224
3225 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003226 if( used > buf_len )
3227 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003228
3229 return( 0 );
3230}
3231
3232/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003233 * Public wrapper for ssl_session_save()
3234 */
3235int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3236 unsigned char *buf,
3237 size_t buf_len,
3238 size_t *olen )
3239{
3240 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3241}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003242
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003243/*
3244 * Deserialize session, see mbedtls_ssl_session_save() for format.
3245 *
3246 * This internal version is wrapped by a public function that cleans up in
3247 * case of error, and has an extra option omit_header.
3248 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003249MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003250static int ssl_session_load( mbedtls_ssl_session *session,
3251 unsigned char omit_header,
3252 const unsigned char *buf,
3253 size_t len )
3254{
3255 const unsigned char *p = buf;
3256 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003257 size_t remaining_len;
3258
3259
3260 if( session == NULL )
3261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003262
3263 if( !omit_header )
3264 {
3265 /*
3266 * Check Mbed TLS version identifier
3267 */
3268
3269 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3271
3272 if( memcmp( p, ssl_serialized_session_header,
3273 sizeof( ssl_serialized_session_header ) ) != 0 )
3274 {
3275 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3276 }
3277 p += sizeof( ssl_serialized_session_header );
3278 }
3279
3280 /*
3281 * TLS version identifier
3282 */
3283 if( 1 > (size_t)( end - p ) )
3284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003285 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003286
3287 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003288 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003289 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003290 {
3291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003292 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003293 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003294#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3295
Jerry Yu438ddd82022-07-07 06:55:50 +00003296#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3297 case MBEDTLS_SSL_VERSION_TLS1_3:
3298 return( ssl_tls13_session_load( session, p, remaining_len ) );
3299#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3300
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003301 default:
3302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3303 }
3304}
3305
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003306/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003307 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003308 */
3309int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3310 const unsigned char *buf,
3311 size_t len )
3312{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003313 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003314
3315 if( ret != 0 )
3316 mbedtls_ssl_session_free( session );
3317
3318 return( ret );
3319}
3320
3321/*
Paul Bakker1961b702013-01-25 14:49:24 +01003322 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003324MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003325static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3326{
3327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3328
Ronald Cron66dbf912022-02-02 15:33:46 +01003329 /*
3330 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003331 * that were written into the output buffer by the previous handshake step,
3332 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003333 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3334 * We proceed to the next handshake step only when all data from the
3335 * previous one have been sent to the peer, thus we make sure that this is
3336 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3337 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3338 * we have to wait before to go ahead.
3339 * In the case of TLS 1.3, handshake step handlers do not send data to the
3340 * peer. Data are only sent here and through
3341 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003342 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003343 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003344 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3345 return( ret );
3346
3347#if defined(MBEDTLS_SSL_PROTO_DTLS)
3348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3349 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3350 {
3351 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3352 return( ret );
3353 }
3354#endif /* MBEDTLS_SSL_PROTO_DTLS */
3355
3356 return( ret );
3357}
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003360{
Hanno Becker41934dd2021-08-07 19:13:43 +01003361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
Hanno Becker41934dd2021-08-07 19:13:43 +01003363 if( ssl == NULL ||
3364 ssl->conf == NULL ||
3365 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003366 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003367 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003369 }
3370
3371 ret = ssl_prepare_handshake_step( ssl );
3372 if( ret != 0 )
3373 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003374
Jerry Yue7047812021-09-13 19:26:39 +08003375 ret = mbedtls_ssl_handle_pending_alert( ssl );
3376 if( ret != 0 )
3377 goto cleanup;
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003380 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003381 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3383 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003384
Ronald Cron9f0fba32022-02-10 16:45:15 +01003385 switch( ssl->state )
3386 {
3387 case MBEDTLS_SSL_HELLO_REQUEST:
3388 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3389 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003390
Ronald Cron9f0fba32022-02-10 16:45:15 +01003391 case MBEDTLS_SSL_CLIENT_HELLO:
3392 ret = mbedtls_ssl_write_client_hello( ssl );
3393 break;
3394
3395 default:
3396#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003397 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003398 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3399 else
3400 ret = mbedtls_ssl_handshake_client_step( ssl );
3401#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3402 ret = mbedtls_ssl_handshake_client_step( ssl );
3403#else
3404 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3405#endif
3406 }
Jerry Yub9930e72021-08-06 17:11:51 +08003407 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003408#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003410 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003411 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003412#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003413 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003414 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003415#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003416
3417#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3418 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3419 ret = mbedtls_ssl_handshake_server_step( ssl );
3420#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003422#endif
3423
Jerry Yue7047812021-09-13 19:26:39 +08003424 if( ret != 0 )
3425 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003426 /* handshake_step return error. And it is same
3427 * with alert_reason.
3428 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003429 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003430 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003431 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003432 goto cleanup;
3433 }
3434 }
3435
3436cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003437 return( ret );
3438}
3439
3440/*
3441 * Perform the SSL handshake
3442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003444{
3445 int ret = 0;
3446
Hanno Beckera817ea42020-10-20 15:20:23 +01003447 /* Sanity checks */
3448
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003449 if( ssl == NULL || ssl->conf == NULL )
3450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3451
Hanno Beckera817ea42020-10-20 15:20:23 +01003452#if defined(MBEDTLS_SSL_PROTO_DTLS)
3453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3454 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3455 {
3456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3457 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3459 }
3460#endif /* MBEDTLS_SSL_PROTO_DTLS */
3461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003463
Hanno Beckera817ea42020-10-20 15:20:23 +01003464 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003465 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003468
3469 if( ret != 0 )
3470 break;
3471 }
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003474
3475 return( ret );
3476}
3477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#if defined(MBEDTLS_SSL_RENEGOTIATION)
3479#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003480/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003481 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003482 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003483MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003485{
Janos Follath865b3eb2019-12-16 11:46:15 +00003486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003489
3490 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3492 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003493
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003494 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003495 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003497 return( ret );
3498 }
3499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003501
3502 return( 0 );
3503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003505
3506/*
3507 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 * - any side: calling mbedtls_ssl_renegotiate(),
3509 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3510 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003511 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003512 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003514 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003515int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003516{
Janos Follath865b3eb2019-12-16 11:46:15 +00003517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003520
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003521 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3522 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003523
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003524 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3525 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003527 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003529 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003531 ssl->handshake->out_msg_seq = 1;
3532 else
3533 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003534 }
3535#endif
3536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3538 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003543 return( ret );
3544 }
3545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003547
3548 return( 0 );
3549}
3550
3551/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003552 * Renegotiate current connection on client,
3553 * or request renegotiation on server
3554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003559 if( ssl == NULL || ssl->conf == NULL )
3560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003563 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003564 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003565 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003566 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003570
3571 /* Did we already try/start sending HelloRequest? */
3572 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003574
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003575 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003580 /*
3581 * On client, either start the renegotiation process or,
3582 * if already in progress, continue the handshake
3583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003585 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003586 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003588
Hanno Becker40cdaa12020-02-05 10:48:27 +00003589 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003590 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003592 return( ret );
3593 }
3594 }
3595 else
3596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003600 return( ret );
3601 }
3602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003604
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003605 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003608
Gilles Peskine9b562d52018-04-25 20:32:43 +02003609void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003610{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003611 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3612
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003613 if( handshake == NULL )
3614 return;
3615
Brett Warrene0edc842021-08-17 09:53:13 +01003616#if defined(MBEDTLS_ECP_C)
3617#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3618 if ( ssl->handshake->group_list_heap_allocated )
3619 mbedtls_free( (void*) handshake->group_list );
3620 handshake->group_list = NULL;
3621#endif /* MBEDTLS_DEPRECATED_REMOVED */
3622#endif /* MBEDTLS_ECP_C */
3623
Ronald Crone68ab4f2022-10-05 12:46:29 +02003624#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003625#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3626 if ( ssl->handshake->sig_algs_heap_allocated )
3627 mbedtls_free( (void*) handshake->sig_algs );
3628 handshake->sig_algs = NULL;
3629#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003630#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3631 if( ssl->handshake->certificate_request_context )
3632 {
3633 mbedtls_free( (void*) handshake->certificate_request_context );
3634 }
3635#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003636#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003637
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003638#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3639 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3640 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003641 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003642 handshake->async_in_progress = 0;
3643 }
3644#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3645
Andrzej Kurek25f27152022-08-17 16:09:31 -04003646#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003647#if defined(MBEDTLS_USE_PSA_CRYPTO)
3648 psa_hash_abort( &handshake->fin_sha256_psa );
3649#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003650 mbedtls_sha256_free( &handshake->fin_sha256 );
3651#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003652#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003653#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003654#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003655 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003656#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003657 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003658#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003659#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_DHM_C)
3662 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003663#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003664#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003666#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003667#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003668 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003669#if defined(MBEDTLS_SSL_CLI_C)
3670 mbedtls_free( handshake->ecjpake_cache );
3671 handshake->ecjpake_cache = NULL;
3672 handshake->ecjpake_cache_len = 0;
3673#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003674#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003675
Janos Follath4ae5c292016-02-10 11:27:43 +00003676#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3677 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003678 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003680#endif
3681
Ronald Cron73fe8df2022-10-05 14:31:43 +02003682#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003683#if defined(MBEDTLS_USE_PSA_CRYPTO)
3684 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3685 {
3686 /* The maintenance of the external PSK key slot is the
3687 * user's responsibility. */
3688 if( ssl->handshake->psk_opaque_is_internal )
3689 {
3690 psa_destroy_key( ssl->handshake->psk_opaque );
3691 ssl->handshake->psk_opaque_is_internal = 0;
3692 }
3693 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3694 }
Neil Armstronge952a302022-05-03 10:22:14 +02003695#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003696 if( handshake->psk != NULL )
3697 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003698 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003699 mbedtls_free( handshake->psk );
3700 }
Neil Armstronge952a302022-05-03 10:22:14 +02003701#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003702#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3705 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003706 /*
3707 * Free only the linked list wrapper, not the keys themselves
3708 * since the belong to the SNI callback
3709 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003710 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003712
Gilles Peskineeccd8882020-03-10 12:19:08 +01003713#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003714 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003715 if( handshake->ecrs_peer_cert != NULL )
3716 {
3717 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3718 mbedtls_free( handshake->ecrs_peer_cert );
3719 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003720#endif
3721
Hanno Becker75173122019-02-06 16:18:31 +00003722#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3723 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3724 mbedtls_pk_free( &handshake->peer_pubkey );
3725#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3726
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003727#if defined(MBEDTLS_SSL_CLI_C) && \
3728 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3729 mbedtls_free( handshake->cookie );
3730#endif /* MBEDTLS_SSL_CLI_C &&
3731 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003732
3733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003734 mbedtls_ssl_flight_free( handshake->flight );
3735 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003736#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003737
Ronald Cronf12b81d2022-03-15 10:42:41 +01003738#if defined(MBEDTLS_ECDH_C) && \
3739 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003740 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003741 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003742#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3743
Ronald Cron6f135e12021-12-08 16:57:54 +01003744#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003745 mbedtls_ssl_transform_free( handshake->transform_handshake );
3746 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003747 mbedtls_free( handshake->transform_earlydata );
3748 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003749#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003750
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003751
3752#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3753 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3754 * processes datagrams and the fact that a datagram is allowed to have
3755 * several records in it, it is possible that the I/O buffers are not
3756 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003757 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3758 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003759#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003760
Jerry Yuba9c7272021-10-30 11:54:10 +08003761 /* mbedtls_platform_zeroize MUST be last one in this function */
3762 mbedtls_platform_zeroize( handshake,
3763 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003764}
3765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003767{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003768 if( session == NULL )
3769 return;
3770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003772 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003773#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003774
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003775#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003776#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3777 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003778 mbedtls_free( session->hostname );
3779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003781#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003782
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003783 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003784}
3785
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003786#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003787
3788#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3789#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3790#else
3791#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3792#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3793
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003794#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003795
3796#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3797#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3798#else
3799#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3800#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3801
3802#if defined(MBEDTLS_SSL_ALPN)
3803#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3804#else
3805#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3806#endif /* MBEDTLS_SSL_ALPN */
3807
3808#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3809#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3810#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3811#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3812
3813#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3814 ( (uint32_t) ( \
3815 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3816 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3817 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3818 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3819 0u ) )
3820
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003821static unsigned char ssl_serialized_context_header[] = {
3822 MBEDTLS_VERSION_MAJOR,
3823 MBEDTLS_VERSION_MINOR,
3824 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003825 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3826 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3827 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3828 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3829 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003830};
3831
Paul Bakker5121ce52009-01-03 21:22:43 +00003832/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003833 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003834 *
3835 * The format of the serialized data is:
3836 * (in the presentation language of TLS, RFC 8446 section 3)
3837 *
3838 * // header
3839 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003840 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003841 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003842 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003843 * Note: When updating the format, remember to keep these
3844 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003845 *
3846 * // session sub-structure
3847 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3848 * // transform sub-structure
3849 * uint8 random[64]; // ServerHello.random+ClientHello.random
3850 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3851 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3852 * // fields from ssl_context
3853 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3854 * uint64 in_window_top; // DTLS: last validated record seq_num
3855 * uint64 in_window; // DTLS: bitmask for replay protection
3856 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3857 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3858 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3859 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3860 *
3861 * Note that many fields of the ssl_context or sub-structures are not
3862 * serialized, as they fall in one of the following categories:
3863 *
3864 * 1. forced value (eg in_left must be 0)
3865 * 2. pointer to dynamically-allocated memory (eg session, transform)
3866 * 3. value can be re-derived from other data (eg session keys from MS)
3867 * 4. value was temporary (eg content of input buffer)
3868 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003869 */
3870int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3871 unsigned char *buf,
3872 size_t buf_len,
3873 size_t *olen )
3874{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003875 unsigned char *p = buf;
3876 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003877 size_t session_len;
3878 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003879
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003880 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003881 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3882 * this function's documentation.
3883 *
3884 * These are due to assumptions/limitations in the implementation. Some of
3885 * them are likely to stay (no handshake in progress) some might go away
3886 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003887 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003889 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003890 {
3891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003893 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003894 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003895 {
3896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003898 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003899 /* Double-check that sub-structures are indeed ready */
3900 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003901 {
3902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003904 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003905 /* There must be no pending incoming or outgoing data */
3906 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003907 {
3908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003910 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003911 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003912 {
3913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003915 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003916 /* Protocol must be DLTS, not TLS */
3917 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003918 {
3919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003921 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003922 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003923 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003924 {
3925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003926 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003927 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003928 /* We must be using an AEAD ciphersuite */
3929 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003930 {
3931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003933 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003934 /* Renegotiation must not be enabled */
3935#if defined(MBEDTLS_SSL_RENEGOTIATION)
3936 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003937 {
3938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003940 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003941#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003942
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003943 /*
3944 * Version and format identifier
3945 */
3946 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003947
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003948 if( used <= buf_len )
3949 {
3950 memcpy( p, ssl_serialized_context_header,
3951 sizeof( ssl_serialized_context_header ) );
3952 p += sizeof( ssl_serialized_context_header );
3953 }
3954
3955 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003956 * Session (length + data)
3957 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003958 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003959 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3960 return( ret );
3961
3962 used += 4 + session_len;
3963 if( used <= buf_len )
3964 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003965 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3966 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003967
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003968 ret = ssl_session_save( ssl->session, 1,
3969 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003970 if( ret != 0 )
3971 return( ret );
3972
3973 p += session_len;
3974 }
3975
3976 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003977 * Transform
3978 */
3979 used += sizeof( ssl->transform->randbytes );
3980 if( used <= buf_len )
3981 {
3982 memcpy( p, ssl->transform->randbytes,
3983 sizeof( ssl->transform->randbytes ) );
3984 p += sizeof( ssl->transform->randbytes );
3985 }
3986
3987#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3988 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3989 if( used <= buf_len )
3990 {
3991 *p++ = ssl->transform->in_cid_len;
3992 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3993 p += ssl->transform->in_cid_len;
3994
3995 *p++ = ssl->transform->out_cid_len;
3996 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3997 p += ssl->transform->out_cid_len;
3998 }
3999#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4000
4001 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004002 * Saved fields from top-level ssl_context structure
4003 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004004 used += 4;
4005 if( used <= buf_len )
4006 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004007 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4008 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004009 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004010
4011#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4012 used += 16;
4013 if( used <= buf_len )
4014 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004015 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4016 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004017
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004018 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4019 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004020 }
4021#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4022
4023#if defined(MBEDTLS_SSL_PROTO_DTLS)
4024 used += 1;
4025 if( used <= buf_len )
4026 {
4027 *p++ = ssl->disable_datagram_packing;
4028 }
4029#endif /* MBEDTLS_SSL_PROTO_DTLS */
4030
Jerry Yuae0b2e22021-10-08 15:21:19 +08004031 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004032 if( used <= buf_len )
4033 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004034 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4035 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004036 }
4037
4038#if defined(MBEDTLS_SSL_PROTO_DTLS)
4039 used += 2;
4040 if( used <= buf_len )
4041 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004042 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4043 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004044 }
4045#endif /* MBEDTLS_SSL_PROTO_DTLS */
4046
4047#if defined(MBEDTLS_SSL_ALPN)
4048 {
4049 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004050 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004051 : 0;
4052
4053 used += 1 + alpn_len;
4054 if( used <= buf_len )
4055 {
4056 *p++ = alpn_len;
4057
4058 if( ssl->alpn_chosen != NULL )
4059 {
4060 memcpy( p, ssl->alpn_chosen, alpn_len );
4061 p += alpn_len;
4062 }
4063 }
4064 }
4065#endif /* MBEDTLS_SSL_ALPN */
4066
4067 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004068 * Done
4069 */
4070 *olen = used;
4071
4072 if( used > buf_len )
4073 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004074
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004075 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4076
Hanno Becker43aefe22020-02-05 10:44:56 +00004077 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004078}
4079
4080/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004081 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004082 *
4083 * This internal version is wrapped by a public function that cleans up in
4084 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004085 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004086MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004087static int ssl_context_load( mbedtls_ssl_context *ssl,
4088 const unsigned char *buf,
4089 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004090{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004091 const unsigned char *p = buf;
4092 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004093 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004095#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004096 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004097#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004098
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004099 /*
4100 * The context should have been freshly setup or reset.
4101 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004102 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004103 * renegotiating, or if the user mistakenly loaded a session first.)
4104 */
4105 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4106 ssl->session != NULL )
4107 {
4108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4109 }
4110
4111 /*
4112 * We can't check that the config matches the initial one, but we can at
4113 * least check it matches the requirements for serializing.
4114 */
4115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004116 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4117 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004118#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004119 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004120#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004121 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004122 {
4123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4124 }
4125
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004126 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4127
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004128 /*
4129 * Check version identifier
4130 */
4131 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4133
4134 if( memcmp( p, ssl_serialized_context_header,
4135 sizeof( ssl_serialized_context_header ) ) != 0 )
4136 {
4137 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4138 }
4139 p += sizeof( ssl_serialized_context_header );
4140
4141 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004142 * Session
4143 */
4144 if( (size_t)( end - p ) < 4 )
4145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4146
4147 session_len = ( (size_t) p[0] << 24 ) |
4148 ( (size_t) p[1] << 16 ) |
4149 ( (size_t) p[2] << 8 ) |
4150 ( (size_t) p[3] );
4151 p += 4;
4152
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004153 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004154 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004155 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004156 ssl->session_in = ssl->session;
4157 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004158 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004159
4160 if( (size_t)( end - p ) < session_len )
4161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4162
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004163 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004164 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004165 {
4166 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004167 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004168 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004169
4170 p += session_len;
4171
4172 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004173 * Transform
4174 */
4175
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004176 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004177 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004178 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004179 ssl->transform_in = ssl->transform;
4180 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004181 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004182
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004183#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004184 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4185 if( prf_func == NULL )
4186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4187
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004188 /* Read random bytes and populate structure */
4189 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004191
Hanno Beckerbd257552021-03-22 06:59:27 +00004192 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004193 ssl->session->ciphersuite,
4194 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004195#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004196 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004197#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004198 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004199 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004200 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004201 ssl->conf->endpoint,
4202 ssl );
4203 if( ret != 0 )
4204 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004205#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004206 p += sizeof( ssl->transform->randbytes );
4207
4208#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4209 /* Read connection IDs and store them */
4210 if( (size_t)( end - p ) < 1 )
4211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4212
4213 ssl->transform->in_cid_len = *p++;
4214
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004215 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4217
4218 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4219 p += ssl->transform->in_cid_len;
4220
4221 ssl->transform->out_cid_len = *p++;
4222
4223 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4225
4226 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4227 p += ssl->transform->out_cid_len;
4228#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4229
4230 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004231 * Saved fields from top-level ssl_context structure
4232 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004233 if( (size_t)( end - p ) < 4 )
4234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4235
4236 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4237 ( (uint32_t) p[1] << 16 ) |
4238 ( (uint32_t) p[2] << 8 ) |
4239 ( (uint32_t) p[3] );
4240 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004241
4242#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4243 if( (size_t)( end - p ) < 16 )
4244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4245
4246 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4247 ( (uint64_t) p[1] << 48 ) |
4248 ( (uint64_t) p[2] << 40 ) |
4249 ( (uint64_t) p[3] << 32 ) |
4250 ( (uint64_t) p[4] << 24 ) |
4251 ( (uint64_t) p[5] << 16 ) |
4252 ( (uint64_t) p[6] << 8 ) |
4253 ( (uint64_t) p[7] );
4254 p += 8;
4255
4256 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4257 ( (uint64_t) p[1] << 48 ) |
4258 ( (uint64_t) p[2] << 40 ) |
4259 ( (uint64_t) p[3] << 32 ) |
4260 ( (uint64_t) p[4] << 24 ) |
4261 ( (uint64_t) p[5] << 16 ) |
4262 ( (uint64_t) p[6] << 8 ) |
4263 ( (uint64_t) p[7] );
4264 p += 8;
4265#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4266
4267#if defined(MBEDTLS_SSL_PROTO_DTLS)
4268 if( (size_t)( end - p ) < 1 )
4269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4270
4271 ssl->disable_datagram_packing = *p++;
4272#endif /* MBEDTLS_SSL_PROTO_DTLS */
4273
Jerry Yud9a94fe2021-09-28 18:58:59 +08004274 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004276 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4277 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004278
4279#if defined(MBEDTLS_SSL_PROTO_DTLS)
4280 if( (size_t)( end - p ) < 2 )
4281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4282
4283 ssl->mtu = ( p[0] << 8 ) | p[1];
4284 p += 2;
4285#endif /* MBEDTLS_SSL_PROTO_DTLS */
4286
4287#if defined(MBEDTLS_SSL_ALPN)
4288 {
4289 uint8_t alpn_len;
4290 const char **cur;
4291
4292 if( (size_t)( end - p ) < 1 )
4293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4294
4295 alpn_len = *p++;
4296
4297 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4298 {
4299 /* alpn_chosen should point to an item in the configured list */
4300 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4301 {
4302 if( strlen( *cur ) == alpn_len &&
4303 memcmp( p, cur, alpn_len ) == 0 )
4304 {
4305 ssl->alpn_chosen = *cur;
4306 break;
4307 }
4308 }
4309 }
4310
4311 /* can only happen on conf mismatch */
4312 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4314
4315 p += alpn_len;
4316 }
4317#endif /* MBEDTLS_SSL_ALPN */
4318
4319 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004320 * Forced fields from top-level ssl_context structure
4321 *
4322 * Most of them already set to the correct value by mbedtls_ssl_init() and
4323 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4324 */
4325 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004326 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004327
Hanno Becker361b10d2019-08-30 10:42:49 +01004328 /* Adjust pointers for header fields of outgoing records to
4329 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004330 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004331
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004332#if defined(MBEDTLS_SSL_PROTO_DTLS)
4333 ssl->in_epoch = 1;
4334#endif
4335
4336 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4337 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004338 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4339 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004340 if( ssl->handshake != NULL )
4341 {
4342 mbedtls_ssl_handshake_free( ssl );
4343 mbedtls_free( ssl->handshake );
4344 ssl->handshake = NULL;
4345 }
4346
4347 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004348 * Done - should have consumed entire buffer
4349 */
4350 if( p != end )
4351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004352
4353 return( 0 );
4354}
4355
4356/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004357 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004358 */
4359int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4360 const unsigned char *buf,
4361 size_t len )
4362{
4363 int ret = ssl_context_load( context, buf, len );
4364
4365 if( ret != 0 )
4366 mbedtls_ssl_free( context );
4367
4368 return( ret );
4369}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004370#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004371
4372/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 * Free an SSL context
4374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004376{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004377 if( ssl == NULL )
4378 return;
4379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004381
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004382 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 {
sander-visserb8aa2072020-05-06 22:05:13 +02004384#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4385 size_t out_buf_len = ssl->out_buf_len;
4386#else
4387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4388#endif
4389
Darryl Greenb33cc762019-11-28 14:29:44 +00004390 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004392 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004393 }
4394
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004395 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004396 {
sander-visserb8aa2072020-05-06 22:05:13 +02004397#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4398 size_t in_buf_len = ssl->in_buf_len;
4399#else
4400 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4401#endif
4402
Darryl Greenb33cc762019-11-28 14:29:44 +00004403 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004405 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004406 }
4407
Paul Bakker48916f92012-09-16 19:57:18 +00004408 if( ssl->transform )
4409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410 mbedtls_ssl_transform_free( ssl->transform );
4411 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004412 }
4413
4414 if( ssl->handshake )
4415 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004416 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4418 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 mbedtls_free( ssl->handshake );
4421 mbedtls_free( ssl->transform_negotiate );
4422 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004423 }
4424
Ronald Cron6f135e12021-12-08 16:57:54 +01004425#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004426 mbedtls_ssl_transform_free( ssl->transform_application );
4427 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004428#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004429
Paul Bakkerc0463502013-02-14 11:19:38 +01004430 if( ssl->session )
4431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 mbedtls_ssl_session_free( ssl->session );
4433 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004434 }
4435
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004436#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004437 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004438 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004439 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004441 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004442#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004443
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004444#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004446#endif
4447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004449
Paul Bakker86f04f42013-02-14 11:20:09 +01004450 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004451 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004452}
4453
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004454/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004455 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004456 */
4457void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4458{
4459 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4460}
4461
Gilles Peskineae270bf2021-06-02 00:05:29 +02004462/* The selection should be the same as mbedtls_x509_crt_profile_default in
4463 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004464 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004465 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004466 * about this list.
4467 */
Brett Warrene0edc842021-08-17 09:53:13 +01004468static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004469#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004470 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004471#endif
4472#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004473 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004474#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004475#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004476 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004477#endif
4478#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004479 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004480#endif
4481#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004482 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004483#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004484#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004485 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004486#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004487#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004488 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004489#endif
4490#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004491 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004492#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004493 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004494};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004495
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004496static int ssl_preset_suiteb_ciphersuites[] = {
4497 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4498 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4499 0
4500};
4501
Ronald Crone68ab4f2022-10-05 12:46:29 +02004502#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004503
Jerry Yu909df7b2022-01-22 11:56:27 +08004504/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004505 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004506 * rules SHOULD be upheld.
4507 * - No duplicate entries.
4508 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004509 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004510 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004511 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004512static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004513
Andrzej Kurek25f27152022-08-17 16:09:31 -04004514#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004515 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4516 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004517#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004518 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004519
Andrzej Kurek25f27152022-08-17 16:09:31 -04004520#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004521 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4522 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004523#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004524 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4525
Andrzej Kurek25f27152022-08-17 16:09:31 -04004526#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004527 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4528 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004529#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004530 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004531
Andrzej Kurek25f27152022-08-17 16:09:31 -04004532#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004533 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004534#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004535
Andrzej Kurek25f27152022-08-17 16:09:31 -04004536#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004537 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004538#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004539
Andrzej Kurek25f27152022-08-17 16:09:31 -04004540#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004541 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004542#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004543
Andrzej Kurek25f27152022-08-17 16:09:31 -04004544#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004545 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4546#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4547
Andrzej Kurek25f27152022-08-17 16:09:31 -04004548#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004549 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4550#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4551
Andrzej Kurek25f27152022-08-17 16:09:31 -04004552#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004553 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4554#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4555
Gabor Mezei15b95a62022-05-09 16:37:58 +02004556 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004557};
4558
4559/* NOTICE: see above */
4560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4561static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004562#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004563#if defined(MBEDTLS_ECDSA_C)
4564 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004565#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004566#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4567 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4568#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004569#if defined(MBEDTLS_RSA_C)
4570 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4571#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004572#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004573#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004574#if defined(MBEDTLS_ECDSA_C)
4575 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004576#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004577#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4578 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4579#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004580#if defined(MBEDTLS_RSA_C)
4581 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4582#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004583#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004584#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004585#if defined(MBEDTLS_ECDSA_C)
4586 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004587#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004588#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4589 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4590#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004591#if defined(MBEDTLS_RSA_C)
4592 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4593#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004594#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004595 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004596};
4597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4598/* NOTICE: see above */
4599static uint16_t ssl_preset_suiteb_sig_algs[] = {
4600
Andrzej Kurek25f27152022-08-17 16:09:31 -04004601#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004602 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4603 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004604#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004605 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4606
Andrzej Kurek25f27152022-08-17 16:09:31 -04004607#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu53037892022-01-25 11:02:06 +08004608 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4609 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004610#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004611 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004612
Andrzej Kurek25f27152022-08-17 16:09:31 -04004613#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08004614 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004615#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08004616
Andrzej Kurek25f27152022-08-17 16:09:31 -04004617#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu53037892022-01-25 11:02:06 +08004618 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004619#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004620
Gabor Mezei15b95a62022-05-09 16:37:58 +02004621 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004622};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004623
Jerry Yu909df7b2022-01-22 11:56:27 +08004624/* NOTICE: see above */
4625#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4626static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004627#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004628#if defined(MBEDTLS_ECDSA_C)
4629 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004630#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004631#if defined(MBEDTLS_RSA_C)
4632 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4633#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004634#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004635#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004636#if defined(MBEDTLS_ECDSA_C)
4637 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004638#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004639#if defined(MBEDTLS_RSA_C)
4640 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4641#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004642#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004643 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004644};
Jerry Yu909df7b2022-01-22 11:56:27 +08004645#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4646
Ronald Crone68ab4f2022-10-05 12:46:29 +02004647#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004648
Brett Warrene0edc842021-08-17 09:53:13 +01004649static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004650#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004651 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004652#endif
4653#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004654 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004655#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004656 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004657};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004658
Ronald Crone68ab4f2022-10-05 12:46:29 +02004659#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004660/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004661 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004662MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004663static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004664{
4665 size_t i, j;
4666 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004667
Gabor Mezei15b95a62022-05-09 16:37:58 +02004668 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004669 {
Jerry Yuf377d642022-01-25 10:43:59 +08004670 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004671 {
Jerry Yuf377d642022-01-25 10:43:59 +08004672 if( sig_algs[i] != sig_algs[j] )
4673 continue;
4674 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4675 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4676 sig_algs[i], j, i );
4677 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004678 }
4679 }
4680 return( ret );
4681}
4682
Ronald Crone68ab4f2022-10-05 12:46:29 +02004683#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004684
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004685/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004686 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004687 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004688int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004689 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004690{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004691#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004693#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004694
Ronald Crone68ab4f2022-10-05 12:46:29 +02004695#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004696 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004697 {
4698 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4699 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4700 }
4701
Jerry Yuf377d642022-01-25 10:43:59 +08004702 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004703 {
4704 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4705 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4706 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004707
4708#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004709 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004710 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004711 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004712 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4713 }
4714
Jerry Yuf377d642022-01-25 10:43:59 +08004715 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004716 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004717 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004718 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4719 }
4720#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004721#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004722
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004723 /* Use the functions here so that they are covered in tests,
4724 * but otherwise access member directly for efficiency */
4725 mbedtls_ssl_conf_endpoint( conf, endpoint );
4726 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004727
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004728 /*
4729 * Things that are common to all presets
4730 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004731#if defined(MBEDTLS_SSL_CLI_C)
4732 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4733 {
4734 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4735#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4736 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4737#endif
4738 }
4739#endif
4740
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004741#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4742 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4743#endif
4744
4745#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4746 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4747#endif
4748
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004749#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004750 conf->f_cookie_write = ssl_cookie_write_dummy;
4751 conf->f_cookie_check = ssl_cookie_check_dummy;
4752#endif
4753
4754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4755 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4756#endif
4757
Janos Follath088ce432017-04-10 12:42:31 +01004758#if defined(MBEDTLS_SSL_SRV_C)
4759 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004760 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004761#endif
4762
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004763#if defined(MBEDTLS_SSL_PROTO_DTLS)
4764 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4765 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4766#endif
4767
4768#if defined(MBEDTLS_SSL_RENEGOTIATION)
4769 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004770 memset( conf->renego_period, 0x00, 2 );
4771 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004772#endif
4773
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004774#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004775 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4776 {
4777 const unsigned char dhm_p[] =
4778 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4779 const unsigned char dhm_g[] =
4780 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004781
Hanno Beckere2defad2021-07-24 05:59:17 +01004782 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4783 dhm_p, sizeof( dhm_p ),
4784 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4785 {
4786 return( ret );
4787 }
4788 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004789#endif
4790
Ronald Cron6f135e12021-12-08 16:57:54 +01004791#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004792#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004793 mbedtls_ssl_conf_new_session_tickets(
4794 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4795#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004796 /*
4797 * Allow all TLS 1.3 key exchange modes by default.
4798 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004799 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004800#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004801
XiaokangQian4d3a6042022-04-21 13:46:17 +00004802 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4803 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004804#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004805 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004806 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004807#else
XiaokangQian060d8672022-04-21 09:24:56 +00004808 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004809#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004810 }
4811 else
4812 {
4813#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4814 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4815 {
4816 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4817 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4818 }
4819 else
4820 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4821 {
4822 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4823 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4824 }
4825#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4826 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4827 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4828#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4829 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4830 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4831#else
4832 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4833#endif
4834 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004835
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004836 /*
4837 * Preset-specific defaults
4838 */
4839 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004840 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004841 /*
4842 * NSA Suite B
4843 */
4844 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004845
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004846 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004847
4848#if defined(MBEDTLS_X509_CRT_PARSE_C)
4849 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004850#endif
4851
Ronald Crone68ab4f2022-10-05 12:46:29 +02004852#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004853#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4854 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4855 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4856 else
4857#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4858 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004859#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004860
Brett Warrene0edc842021-08-17 09:53:13 +01004861#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4862 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004863#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004864 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004865 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004866
4867 /*
4868 * Default
4869 */
4870 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004871
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004872 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004873
4874#if defined(MBEDTLS_X509_CRT_PARSE_C)
4875 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4876#endif
4877
Ronald Crone68ab4f2022-10-05 12:46:29 +02004878#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004879#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4880 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4881 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4882 else
4883#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4884 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004885#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004886
Brett Warrene0edc842021-08-17 09:53:13 +01004887#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4888 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004889#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004890 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004891
4892#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4893 conf->dhm_min_bitlen = 1024;
4894#endif
4895 }
4896
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004897 return( 0 );
4898}
4899
4900/*
4901 * Free mbedtls_ssl_config
4902 */
4903void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4904{
4905#if defined(MBEDTLS_DHM_C)
4906 mbedtls_mpi_free( &conf->dhm_P );
4907 mbedtls_mpi_free( &conf->dhm_G );
4908#endif
4909
Ronald Cron73fe8df2022-10-05 14:31:43 +02004910#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004911#if defined(MBEDTLS_USE_PSA_CRYPTO)
4912 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4913 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004914 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4915 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004916#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004917 if( conf->psk != NULL )
4918 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004919 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004920 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004921 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004922 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004923 }
4924
4925 if( conf->psk_identity != NULL )
4926 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004927 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004928 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004929 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004930 conf->psk_identity_len = 0;
4931 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02004932#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004933
4934#if defined(MBEDTLS_X509_CRT_PARSE_C)
4935 ssl_key_cert_free( conf->key_cert );
4936#endif
4937
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004938 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004939}
4940
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004941#if defined(MBEDTLS_PK_C) && \
4942 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004943/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004947{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948#if defined(MBEDTLS_RSA_C)
4949 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4950 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004951#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952#if defined(MBEDTLS_ECDSA_C)
4953 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4954 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004957}
4958
Hanno Becker7e5437a2017-04-28 17:15:26 +01004959unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4960{
4961 switch( type ) {
4962 case MBEDTLS_PK_RSA:
4963 return( MBEDTLS_SSL_SIG_RSA );
4964 case MBEDTLS_PK_ECDSA:
4965 case MBEDTLS_PK_ECKEY:
4966 return( MBEDTLS_SSL_SIG_ECDSA );
4967 default:
4968 return( MBEDTLS_SSL_SIG_ANON );
4969 }
4970}
4971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004973{
4974 switch( sig )
4975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976#if defined(MBEDTLS_RSA_C)
4977 case MBEDTLS_SSL_SIG_RSA:
4978 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980#if defined(MBEDTLS_ECDSA_C)
4981 case MBEDTLS_SSL_SIG_ECDSA:
4982 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004983#endif
4984 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004986 }
4987}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004988#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004989
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004990/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004991 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004994{
4995 switch( hash )
4996 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998 case MBEDTLS_SSL_HASH_MD5:
4999 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005000#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 case MBEDTLS_SSL_HASH_SHA1:
5003 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005004#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005005#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 case MBEDTLS_SSL_HASH_SHA224:
5007 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005008#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 case MBEDTLS_SSL_HASH_SHA256:
5011 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005012#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005013#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 case MBEDTLS_SSL_HASH_SHA384:
5015 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005016#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005017#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 case MBEDTLS_SSL_HASH_SHA512:
5019 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005020#endif
5021 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005023 }
5024}
5025
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005026/*
5027 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5028 */
5029unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5030{
5031 switch( md )
5032 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005033#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005034 case MBEDTLS_MD_MD5:
5035 return( MBEDTLS_SSL_HASH_MD5 );
5036#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005037#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005038 case MBEDTLS_MD_SHA1:
5039 return( MBEDTLS_SSL_HASH_SHA1 );
5040#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005041#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005042 case MBEDTLS_MD_SHA224:
5043 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005044#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005045#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005046 case MBEDTLS_MD_SHA256:
5047 return( MBEDTLS_SSL_HASH_SHA256 );
5048#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005049#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005050 case MBEDTLS_MD_SHA384:
5051 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005052#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005053#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005054 case MBEDTLS_MD_SHA512:
5055 return( MBEDTLS_SSL_HASH_SHA512 );
5056#endif
5057 default:
5058 return( MBEDTLS_SSL_HASH_NONE );
5059 }
5060}
5061
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005062/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005063 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005064 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005065 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005066int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005067{
Brett Warrene0edc842021-08-17 09:53:13 +01005068 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005069
Brett Warrene0edc842021-08-17 09:53:13 +01005070 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005071 return( -1 );
5072
Brett Warrene0edc842021-08-17 09:53:13 +01005073 for( ; *group_list != 0; group_list++ )
5074 {
5075 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005076 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005077 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005078
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005079 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005080}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005081
5082#if defined(MBEDTLS_ECP_C)
5083/*
5084 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5085 */
5086int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5087{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005088 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005089 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005090
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005091 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005092 return -1;
5093
5094 uint16_t tls_id = grp_info->tls_id;
5095
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005096 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5097}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005098#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005100#if defined(MBEDTLS_X509_CRT_PARSE_C)
5101int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5102 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005103 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005104 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005105{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005106 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005107 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005108 const char *ext_oid;
5109 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005112 {
5113 /* Server part of the key exchange */
5114 switch( ciphersuite->key_exchange )
5115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 case MBEDTLS_KEY_EXCHANGE_RSA:
5117 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005118 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005119 break;
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5122 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5123 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5124 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005125 break;
5126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5128 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005129 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005130 break;
5131
5132 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 case MBEDTLS_KEY_EXCHANGE_NONE:
5134 case MBEDTLS_KEY_EXCHANGE_PSK:
5135 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5136 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005137 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005138 usage = 0;
5139 }
5140 }
5141 else
5142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5144 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005145 }
5146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005148 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005149 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005150 ret = -1;
5151 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005153 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5156 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005157 }
5158 else
5159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5161 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005162 }
5163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005165 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005166 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005167 ret = -1;
5168 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005169
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005170 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005173
Jerry Yu148165c2021-09-24 23:20:59 +08005174#if defined(MBEDTLS_USE_PSA_CRYPTO)
5175int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5176 const mbedtls_md_type_t md,
5177 unsigned char *dst,
5178 size_t dst_len,
5179 size_t *olen )
5180{
Ronald Cronf6893e12022-01-07 22:09:01 +01005181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5182 psa_hash_operation_t *hash_operation_to_clone;
5183 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5184
Jerry Yu148165c2021-09-24 23:20:59 +08005185 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005186
5187 switch( md )
5188 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005189#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005190 case MBEDTLS_MD_SHA384:
5191 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5192 break;
5193#endif
5194
Andrzej Kurek25f27152022-08-17 16:09:31 -04005195#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005196 case MBEDTLS_MD_SHA256:
5197 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5198 break;
5199#endif
5200
5201 default:
5202 goto exit;
5203 }
5204
5205 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5206 if( status != PSA_SUCCESS )
5207 goto exit;
5208
5209 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5210 if( status != PSA_SUCCESS )
5211 goto exit;
5212
5213exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005214#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5215 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5216 (void) ssl;
5217#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005218 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005219}
5220#else /* MBEDTLS_USE_PSA_CRYPTO */
5221
Andrzej Kurek25f27152022-08-17 16:09:31 -04005222#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005223MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005224static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5225 unsigned char *dst,
5226 size_t dst_len,
5227 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005228{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005229 int ret;
5230 mbedtls_sha512_context sha512;
5231
5232 if( dst_len < 48 )
5233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5234
5235 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005236 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005237
5238 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5239 {
5240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5241 goto exit;
5242 }
5243
5244 *olen = 48;
5245
5246exit:
5247
5248 mbedtls_sha512_free( &sha512 );
5249 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005250}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005251#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005252
Andrzej Kurek25f27152022-08-17 16:09:31 -04005253#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005254MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005255static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5256 unsigned char *dst,
5257 size_t dst_len,
5258 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005259{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005260 int ret;
5261 mbedtls_sha256_context sha256;
5262
5263 if( dst_len < 32 )
5264 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5265
5266 mbedtls_sha256_init( &sha256 );
5267 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005268
Jerry Yu24c0ec32021-09-09 14:21:07 +08005269 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5270 {
5271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5272 goto exit;
5273 }
5274
5275 *olen = 32;
5276
5277exit:
5278
5279 mbedtls_sha256_free( &sha256 );
5280 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005281}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005282#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005283
Jerry Yu000f9762021-09-14 11:12:51 +08005284int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5285 const mbedtls_md_type_t md,
5286 unsigned char *dst,
5287 size_t dst_len,
5288 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005289{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005290 switch( md )
5291 {
5292
Andrzej Kurek25f27152022-08-17 16:09:31 -04005293#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005294 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005295 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005296#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005297
Andrzej Kurek25f27152022-08-17 16:09:31 -04005298#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005299 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005300 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005301#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005302
5303 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005304#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5305 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5306 (void) ssl;
5307 (void) dst;
5308 (void) dst_len;
5309 (void) olen;
5310#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005311 break;
5312 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005314}
XiaokangQian647719a2021-12-07 09:16:29 +00005315
Jerry Yu148165c2021-09-24 23:20:59 +08005316#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005317
Ronald Crone68ab4f2022-10-05 12:46:29 +02005318#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005319/* mbedtls_ssl_parse_sig_alg_ext()
5320 *
5321 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5322 * value (TLS 1.3 RFC8446):
5323 * enum {
5324 * ....
5325 * ecdsa_secp256r1_sha256( 0x0403 ),
5326 * ecdsa_secp384r1_sha384( 0x0503 ),
5327 * ecdsa_secp521r1_sha512( 0x0603 ),
5328 * ....
5329 * } SignatureScheme;
5330 *
5331 * struct {
5332 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5333 * } SignatureSchemeList;
5334 *
5335 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5336 * value (TLS 1.2 RFC5246):
5337 * enum {
5338 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5339 * sha512(6), (255)
5340 * } HashAlgorithm;
5341 *
5342 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5343 * SignatureAlgorithm;
5344 *
5345 * struct {
5346 * HashAlgorithm hash;
5347 * SignatureAlgorithm signature;
5348 * } SignatureAndHashAlgorithm;
5349 *
5350 * SignatureAndHashAlgorithm
5351 * supported_signature_algorithms<2..2^16-2>;
5352 *
5353 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5354 * generalization of the TLS 1.2 signature algorithm extension.
5355 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5356 * `SignatureScheme` field of TLS 1.3
5357 *
5358 */
5359int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5360 const unsigned char *buf,
5361 const unsigned char *end )
5362{
5363 const unsigned char *p = buf;
5364 size_t supported_sig_algs_len = 0;
5365 const unsigned char *supported_sig_algs_end;
5366 uint16_t sig_alg;
5367 uint32_t common_idx = 0;
5368
5369 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5370 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5371 p += 2;
5372
5373 memset( ssl->handshake->received_sig_algs, 0,
5374 sizeof(ssl->handshake->received_sig_algs) );
5375
5376 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5377 supported_sig_algs_end = p + supported_sig_algs_len;
5378 while( p < supported_sig_algs_end )
5379 {
5380 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5381 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5382 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005383 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5384 sig_alg,
5385 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005386#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005387 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005388 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5389 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5390 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005391 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005392 }
5393#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005394
Jerry Yuf3b46b52022-06-19 16:52:27 +08005395 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5396 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005397
5398 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5399 {
5400 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5401 common_idx += 1;
5402 }
5403 }
5404 /* Check that we consumed all the message. */
5405 if( p != end )
5406 {
5407 MBEDTLS_SSL_DEBUG_MSG( 1,
5408 ( "Signature algorithms extension length misaligned" ) );
5409 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5410 MBEDTLS_ERR_SSL_DECODE_ERROR );
5411 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5412 }
5413
5414 if( common_idx == 0 )
5415 {
5416 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5417 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5418 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5419 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5420 }
5421
Gabor Mezei15b95a62022-05-09 16:37:58 +02005422 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005423 return( 0 );
5424}
5425
Ronald Crone68ab4f2022-10-05 12:46:29 +02005426#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005427
Jerry Yudc7bd172022-02-17 13:44:15 +08005428#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5429
5430#if defined(MBEDTLS_USE_PSA_CRYPTO)
5431
5432static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5433 mbedtls_svc_key_id_t key,
5434 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005435 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005436 const unsigned char* seed, size_t seed_length,
5437 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005438 const unsigned char* other_secret,
5439 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005440 size_t capacity )
5441{
5442 psa_status_t status;
5443
5444 status = psa_key_derivation_setup( derivation, alg );
5445 if( status != PSA_SUCCESS )
5446 return( status );
5447
5448 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5449 {
5450 status = psa_key_derivation_input_bytes( derivation,
5451 PSA_KEY_DERIVATION_INPUT_SEED,
5452 seed, seed_length );
5453 if( status != PSA_SUCCESS )
5454 return( status );
5455
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005456 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005457 {
5458 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005459 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5460 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005461 if( status != PSA_SUCCESS )
5462 return( status );
5463 }
5464
Jerry Yudc7bd172022-02-17 13:44:15 +08005465 if( mbedtls_svc_key_id_is_null( key ) )
5466 {
5467 status = psa_key_derivation_input_bytes(
5468 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005469 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005470 }
5471 else
5472 {
5473 status = psa_key_derivation_input_key(
5474 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5475 }
5476 if( status != PSA_SUCCESS )
5477 return( status );
5478
5479 status = psa_key_derivation_input_bytes( derivation,
5480 PSA_KEY_DERIVATION_INPUT_LABEL,
5481 label, label_length );
5482 if( status != PSA_SUCCESS )
5483 return( status );
5484 }
5485 else
5486 {
5487 return( PSA_ERROR_NOT_SUPPORTED );
5488 }
5489
5490 status = psa_key_derivation_set_capacity( derivation, capacity );
5491 if( status != PSA_SUCCESS )
5492 return( status );
5493
5494 return( PSA_SUCCESS );
5495}
5496
Andrzej Kurek57d10632022-10-24 10:32:01 -04005497#if defined(PSA_WANT_ALG_SHA_384) || \
5498 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005499MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005500static int tls_prf_generic( mbedtls_md_type_t md_type,
5501 const unsigned char *secret, size_t slen,
5502 const char *label,
5503 const unsigned char *random, size_t rlen,
5504 unsigned char *dstbuf, size_t dlen )
5505{
5506 psa_status_t status;
5507 psa_algorithm_t alg;
5508 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5509 psa_key_derivation_operation_t derivation =
5510 PSA_KEY_DERIVATION_OPERATION_INIT;
5511
5512 if( md_type == MBEDTLS_MD_SHA384 )
5513 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5514 else
5515 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5516
5517 /* Normally a "secret" should be long enough to be impossible to
5518 * find by brute force, and in particular should not be empty. But
5519 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5520 * and for this use case it makes sense to have a 0-length "secret".
5521 * Since the key API doesn't allow importing a key of length 0,
5522 * keep master_key=0, which setup_psa_key_derivation() understands
5523 * to mean a 0-length "secret" input. */
5524 if( slen != 0 )
5525 {
5526 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5527 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5528 psa_set_key_algorithm( &key_attributes, alg );
5529 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5530
5531 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5532 if( status != PSA_SUCCESS )
5533 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5534 }
5535
5536 status = setup_psa_key_derivation( &derivation,
5537 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005538 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005539 random, rlen,
5540 (unsigned char const *) label,
5541 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005542 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005543 dlen );
5544 if( status != PSA_SUCCESS )
5545 {
5546 psa_key_derivation_abort( &derivation );
5547 psa_destroy_key( master_key );
5548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5549 }
5550
5551 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5552 if( status != PSA_SUCCESS )
5553 {
5554 psa_key_derivation_abort( &derivation );
5555 psa_destroy_key( master_key );
5556 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5557 }
5558
5559 status = psa_key_derivation_abort( &derivation );
5560 if( status != PSA_SUCCESS )
5561 {
5562 psa_destroy_key( master_key );
5563 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5564 }
5565
5566 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5567 status = psa_destroy_key( master_key );
5568 if( status != PSA_SUCCESS )
5569 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5570
5571 return( 0 );
5572}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005573#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005574#else /* MBEDTLS_USE_PSA_CRYPTO */
5575
Andrzej Kurek57d10632022-10-24 10:32:01 -04005576#if defined(MBEDTLS_MD_C) && \
5577 ( defined(MBEDTLS_SHA256_C) || \
5578 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005579MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005580static int tls_prf_generic( mbedtls_md_type_t md_type,
5581 const unsigned char *secret, size_t slen,
5582 const char *label,
5583 const unsigned char *random, size_t rlen,
5584 unsigned char *dstbuf, size_t dlen )
5585{
5586 size_t nb;
5587 size_t i, j, k, md_len;
5588 unsigned char *tmp;
5589 size_t tmp_len = 0;
5590 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5591 const mbedtls_md_info_t *md_info;
5592 mbedtls_md_context_t md_ctx;
5593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5594
5595 mbedtls_md_init( &md_ctx );
5596
5597 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5598 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5599
5600 md_len = mbedtls_md_get_size( md_info );
5601
5602 tmp_len = md_len + strlen( label ) + rlen;
5603 tmp = mbedtls_calloc( 1, tmp_len );
5604 if( tmp == NULL )
5605 {
5606 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5607 goto exit;
5608 }
5609
5610 nb = strlen( label );
5611 memcpy( tmp + md_len, label, nb );
5612 memcpy( tmp + md_len + nb, random, rlen );
5613 nb += rlen;
5614
5615 /*
5616 * Compute P_<hash>(secret, label + random)[0..dlen]
5617 */
5618 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5619 goto exit;
5620
5621 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5622 if( ret != 0 )
5623 goto exit;
5624 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5625 if( ret != 0 )
5626 goto exit;
5627 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5628 if( ret != 0 )
5629 goto exit;
5630
5631 for( i = 0; i < dlen; i += md_len )
5632 {
5633 ret = mbedtls_md_hmac_reset ( &md_ctx );
5634 if( ret != 0 )
5635 goto exit;
5636 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5637 if( ret != 0 )
5638 goto exit;
5639 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5640 if( ret != 0 )
5641 goto exit;
5642
5643 ret = mbedtls_md_hmac_reset ( &md_ctx );
5644 if( ret != 0 )
5645 goto exit;
5646 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5647 if( ret != 0 )
5648 goto exit;
5649 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5650 if( ret != 0 )
5651 goto exit;
5652
5653 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5654
5655 for( j = 0; j < k; j++ )
5656 dstbuf[i + j] = h_i[j];
5657 }
5658
5659exit:
5660 mbedtls_md_free( &md_ctx );
5661
5662 mbedtls_platform_zeroize( tmp, tmp_len );
5663 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5664
5665 mbedtls_free( tmp );
5666
5667 return( ret );
5668}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005669#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005670#endif /* MBEDTLS_USE_PSA_CRYPTO */
5671
Andrzej Kurek25f27152022-08-17 16:09:31 -04005672#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005673MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005674static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5675 const char *label,
5676 const unsigned char *random, size_t rlen,
5677 unsigned char *dstbuf, size_t dlen )
5678{
5679 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5680 label, random, rlen, dstbuf, dlen ) );
5681}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005682#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005683
Andrzej Kurek25f27152022-08-17 16:09:31 -04005684#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005685MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005686static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5687 const char *label,
5688 const unsigned char *random, size_t rlen,
5689 unsigned char *dstbuf, size_t dlen )
5690{
5691 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5692 label, random, rlen, dstbuf, dlen ) );
5693}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005694#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005695
Jerry Yuf009d862022-02-17 14:01:37 +08005696/*
5697 * Set appropriate PRF function and other SSL / TLS1.2 functions
5698 *
5699 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005700 * - hash associated with the ciphersuite (only used by TLS 1.2)
5701 *
5702 * Outputs:
5703 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5704 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005705MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005706static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005707 mbedtls_md_type_t hash )
5708{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005709#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005710 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005711 {
5712 handshake->tls_prf = tls_prf_sha384;
5713 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5714 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5715 }
5716 else
5717#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005718#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005719 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005720 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005721 handshake->tls_prf = tls_prf_sha256;
5722 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5723 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5724 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005725#else
Jerry Yuf009d862022-02-17 14:01:37 +08005726 {
Jerry Yuf009d862022-02-17 14:01:37 +08005727 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005728 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5730 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005731#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005732
5733 return( 0 );
5734}
Jerry Yud6ab2352022-02-17 14:03:43 +08005735
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005736/*
5737 * Compute master secret if needed
5738 *
5739 * Parameters:
5740 * [in/out] handshake
5741 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5742 * (PSA-PSK) ciphersuite_info, psk_opaque
5743 * [out] premaster (cleared)
5744 * [out] master
5745 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5746 * debug: conf->f_dbg, conf->p_dbg
5747 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005748 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005749 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005750MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005751static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5752 unsigned char *master,
5753 const mbedtls_ssl_context *ssl )
5754{
5755 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5756
5757 /* cf. RFC 5246, Section 8.1:
5758 * "The master secret is always exactly 48 bytes in length." */
5759 size_t const master_secret_len = 48;
5760
5761#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5762 unsigned char session_hash[48];
5763#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5764
5765 /* The label for the KDF used for key expansion.
5766 * This is either "master secret" or "extended master secret"
5767 * depending on whether the Extended Master Secret extension
5768 * is used. */
5769 char const *lbl = "master secret";
5770
Przemek Stekielae4ed302022-04-05 17:15:55 +02005771 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005772 * - If the Extended Master Secret extension is not used,
5773 * this is ClientHello.Random + ServerHello.Random
5774 * (see Sect. 8.1 in RFC 5246).
5775 * - If the Extended Master Secret extension is used,
5776 * this is the transcript of the handshake so far.
5777 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005778 unsigned char const *seed = handshake->randbytes;
5779 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005780
5781#if !defined(MBEDTLS_DEBUG_C) && \
5782 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5783 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5784 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5785 ssl = NULL; /* make sure we don't use it except for those cases */
5786 (void) ssl;
5787#endif
5788
5789 if( handshake->resume != 0 )
5790 {
5791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5792 return( 0 );
5793 }
5794
5795#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5796 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5797 {
5798 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005799 seed = session_hash;
5800 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005801
5802 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005803 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005804 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005805#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005806
Przemek Stekiel99114f32022-04-22 11:20:09 +02005807#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005808 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005809 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005810 {
5811 /* Perform PSK-to-MS expansion in a single step. */
5812 psa_status_t status;
5813 psa_algorithm_t alg;
5814 mbedtls_svc_key_id_t psk;
5815 psa_key_derivation_operation_t derivation =
5816 PSA_KEY_DERIVATION_OPERATION_INIT;
5817 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5818
5819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5820
5821 psk = mbedtls_ssl_get_opaque_psk( ssl );
5822
5823 if( hash_alg == MBEDTLS_MD_SHA384 )
5824 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5825 else
5826 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5827
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005828 size_t other_secret_len = 0;
5829 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005830
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005831 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005832 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005833 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005834 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005835 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005836 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005837 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005838 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005839 other_secret_len = 48;
5840 other_secret = handshake->premaster + 2;
5841 break;
5842 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005843 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005844 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5845 other_secret = handshake->premaster + 2;
5846 break;
5847 default:
5848 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005849 }
5850
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005851 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005852 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005853 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005854 (unsigned char const *) lbl,
5855 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005856 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005857 master_secret_len );
5858 if( status != PSA_SUCCESS )
5859 {
5860 psa_key_derivation_abort( &derivation );
5861 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5862 }
5863
5864 status = psa_key_derivation_output_bytes( &derivation,
5865 master,
5866 master_secret_len );
5867 if( status != PSA_SUCCESS )
5868 {
5869 psa_key_derivation_abort( &derivation );
5870 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5871 }
5872
5873 status = psa_key_derivation_abort( &derivation );
5874 if( status != PSA_SUCCESS )
5875 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5876 }
5877 else
5878#endif
5879 {
5880 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005881 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005882 master,
5883 master_secret_len );
5884 if( ret != 0 )
5885 {
5886 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5887 return( ret );
5888 }
5889
5890 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5891 handshake->premaster,
5892 handshake->pmslen );
5893
5894 mbedtls_platform_zeroize( handshake->premaster,
5895 sizeof(handshake->premaster) );
5896 }
5897
5898 return( 0 );
5899}
5900
Jerry Yud62f87e2022-02-17 14:09:02 +08005901int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5902{
5903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5904 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5905 ssl->handshake->ciphersuite_info;
5906
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5908
5909 /* Set PRF, calc_verify and calc_finished function pointers */
5910 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005911 ciphersuite_info->mac );
5912 if( ret != 0 )
5913 {
5914 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5915 return( ret );
5916 }
5917
5918 /* Compute master secret if needed */
5919 ret = ssl_compute_master( ssl->handshake,
5920 ssl->session_negotiate->master,
5921 ssl );
5922 if( ret != 0 )
5923 {
5924 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5925 return( ret );
5926 }
5927
5928 /* Swap the client and server random values:
5929 * - MS derivation wanted client+server (RFC 5246 8.1)
5930 * - key derivation wants server+client (RFC 5246 6.3) */
5931 {
5932 unsigned char tmp[64];
5933 memcpy( tmp, ssl->handshake->randbytes, 64 );
5934 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5935 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5936 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5937 }
5938
5939 /* Populate transform structure */
5940 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5941 ssl->session_negotiate->ciphersuite,
5942 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005943#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005944 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005945#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005946 ssl->handshake->tls_prf,
5947 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005948 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005949 ssl->conf->endpoint,
5950 ssl );
5951 if( ret != 0 )
5952 {
5953 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5954 return( ret );
5955 }
5956
5957 /* We no longer need Server/ClientHello.random values */
5958 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5959 sizeof( ssl->handshake->randbytes ) );
5960
5961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5962
5963 return( 0 );
5964}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005965
Ronald Cron4dcbca92022-03-07 10:21:40 +01005966int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5967{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005968 switch( md )
5969 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005970#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005971 case MBEDTLS_SSL_HASH_SHA384:
5972 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5973 break;
5974#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005975#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005976 case MBEDTLS_SSL_HASH_SHA256:
5977 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5978 break;
5979#endif
5980 default:
5981 return( -1 );
5982 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005983#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5984 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5985 (void) ssl;
5986#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01005987 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005988}
5989
Andrzej Kurek25f27152022-08-17 16:09:31 -04005990#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005991void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5992 unsigned char *hash,
5993 size_t *hlen )
5994{
5995#if defined(MBEDTLS_USE_PSA_CRYPTO)
5996 size_t hash_size;
5997 psa_status_t status;
5998 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5999
6000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6001 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6002 if( status != PSA_SUCCESS )
6003 {
6004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6005 return;
6006 }
6007
6008 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6009 if( status != PSA_SUCCESS )
6010 {
6011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6012 return;
6013 }
6014
6015 *hlen = 32;
6016 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6018#else
6019 mbedtls_sha256_context sha256;
6020
6021 mbedtls_sha256_init( &sha256 );
6022
6023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6024
6025 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6026 mbedtls_sha256_finish( &sha256, hash );
6027
6028 *hlen = 32;
6029
6030 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6032
6033 mbedtls_sha256_free( &sha256 );
6034#endif /* MBEDTLS_USE_PSA_CRYPTO */
6035 return;
6036}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006037#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006038
Andrzej Kurek25f27152022-08-17 16:09:31 -04006039#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006040void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6041 unsigned char *hash,
6042 size_t *hlen )
6043{
6044#if defined(MBEDTLS_USE_PSA_CRYPTO)
6045 size_t hash_size;
6046 psa_status_t status;
6047 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6048
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6050 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6051 if( status != PSA_SUCCESS )
6052 {
6053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6054 return;
6055 }
6056
6057 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6058 if( status != PSA_SUCCESS )
6059 {
6060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6061 return;
6062 }
6063
6064 *hlen = 48;
6065 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6067#else
6068 mbedtls_sha512_context sha512;
6069
6070 mbedtls_sha512_init( &sha512 );
6071
6072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6073
Andrzej Kureka242e832022-08-11 10:03:14 -04006074 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006075 mbedtls_sha512_finish( &sha512, hash );
6076
6077 *hlen = 48;
6078
6079 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6081
6082 mbedtls_sha512_free( &sha512 );
6083#endif /* MBEDTLS_USE_PSA_CRYPTO */
6084 return;
6085}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006086#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006087
Neil Armstrong80f6f322022-05-03 17:56:38 +02006088#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6089 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006090int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6091{
6092 unsigned char *p = ssl->handshake->premaster;
6093 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6094 const unsigned char *psk = NULL;
6095 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006096 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006097
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006098 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006099 {
6100 /*
6101 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006102 * checked before calling this function.
6103 *
6104 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006105 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006106 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006107 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6108 {
6109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6111 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006112 }
6113
6114 /*
6115 * PMS = struct {
6116 * opaque other_secret<0..2^16-1>;
6117 * opaque psk<0..2^16-1>;
6118 * };
6119 * with "other_secret" depending on the particular key exchange
6120 */
6121#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6122 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6123 {
6124 if( end - p < 2 )
6125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6126
6127 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6128 p += 2;
6129
6130 if( end < p || (size_t)( end - p ) < psk_len )
6131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6132
6133 memset( p, 0, psk_len );
6134 p += psk_len;
6135 }
6136 else
6137#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6138#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6139 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6140 {
6141 /*
6142 * other_secret already set by the ClientKeyExchange message,
6143 * and is 48 bytes long
6144 */
6145 if( end - p < 2 )
6146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6147
6148 *p++ = 0;
6149 *p++ = 48;
6150 p += 48;
6151 }
6152 else
6153#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6154#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6155 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6156 {
6157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6158 size_t len;
6159
6160 /* Write length only when we know the actual value */
6161 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6162 p + 2, end - ( p + 2 ), &len,
6163 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6164 {
6165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6166 return( ret );
6167 }
6168 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6169 p += 2 + len;
6170
6171 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6172 }
6173 else
6174#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006175#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006176 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6177 {
6178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6179 size_t zlen;
6180
6181 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6182 p + 2, end - ( p + 2 ),
6183 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6184 {
6185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6186 return( ret );
6187 }
6188
6189 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6190 p += 2 + zlen;
6191
6192 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6193 MBEDTLS_DEBUG_ECDH_Z );
6194 }
6195 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006196#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006197 {
6198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6200 }
6201
6202 /* opaque psk<0..2^16-1>; */
6203 if( end - p < 2 )
6204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6205
6206 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6207 p += 2;
6208
6209 if( end < p || (size_t)( end - p ) < psk_len )
6210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6211
6212 memcpy( p, psk, psk_len );
6213 p += psk_len;
6214
6215 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6216
6217 return( 0 );
6218}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006219#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006220
6221#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006222MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006223static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6224
6225#if defined(MBEDTLS_SSL_PROTO_DTLS)
6226int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6227{
6228 /* If renegotiation is not enforced, retransmit until we would reach max
6229 * timeout if we were using the usual handshake doubling scheme */
6230 if( ssl->conf->renego_max_records < 0 )
6231 {
6232 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6233 unsigned char doublings = 1;
6234
6235 while( ratio != 0 )
6236 {
6237 ++doublings;
6238 ratio >>= 1;
6239 }
6240
6241 if( ++ssl->renego_records_seen > doublings )
6242 {
6243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6244 return( 0 );
6245 }
6246 }
6247
6248 return( ssl_write_hello_request( ssl ) );
6249}
6250#endif
6251#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006252
Jerry Yud9526692022-02-17 14:23:47 +08006253/*
6254 * Handshake functions
6255 */
6256#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6257/* No certificate support -> dummy functions */
6258int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6259{
6260 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6261 ssl->handshake->ciphersuite_info;
6262
6263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6264
6265 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6266 {
6267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6268 ssl->state++;
6269 return( 0 );
6270 }
6271
6272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6274}
6275
6276int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6277{
6278 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6279 ssl->handshake->ciphersuite_info;
6280
6281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6282
6283 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6284 {
6285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6286 ssl->state++;
6287 return( 0 );
6288 }
6289
6290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6291 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6292}
6293
6294#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6295/* Some certificate support -> implement write and parse */
6296
6297int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6298{
6299 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6300 size_t i, n;
6301 const mbedtls_x509_crt *crt;
6302 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6303 ssl->handshake->ciphersuite_info;
6304
6305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6306
6307 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6308 {
6309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6310 ssl->state++;
6311 return( 0 );
6312 }
6313
6314#if defined(MBEDTLS_SSL_CLI_C)
6315 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6316 {
6317 if( ssl->handshake->client_auth == 0 )
6318 {
6319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6320 ssl->state++;
6321 return( 0 );
6322 }
6323 }
6324#endif /* MBEDTLS_SSL_CLI_C */
6325#if defined(MBEDTLS_SSL_SRV_C)
6326 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6327 {
6328 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6329 {
6330 /* Should never happen because we shouldn't have picked the
6331 * ciphersuite if we don't have a certificate. */
6332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6333 }
6334 }
6335#endif
6336
6337 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6338
6339 /*
6340 * 0 . 0 handshake type
6341 * 1 . 3 handshake length
6342 * 4 . 6 length of all certs
6343 * 7 . 9 length of cert. 1
6344 * 10 . n-1 peer certificate
6345 * n . n+2 length of cert. 2
6346 * n+3 . ... upper level cert, etc.
6347 */
6348 i = 7;
6349 crt = mbedtls_ssl_own_cert( ssl );
6350
6351 while( crt != NULL )
6352 {
6353 n = crt->raw.len;
6354 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6355 {
6356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6357 " > %" MBEDTLS_PRINTF_SIZET,
6358 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6359 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6360 }
6361
6362 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6363 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6364 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6365
6366 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6367 i += n; crt = crt->next;
6368 }
6369
6370 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6371 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6372 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6373
6374 ssl->out_msglen = i;
6375 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6376 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6377
6378 ssl->state++;
6379
6380 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6381 {
6382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6383 return( ret );
6384 }
6385
6386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6387
6388 return( ret );
6389}
6390
6391#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6392
6393#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006394MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006395static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6396 unsigned char *crt_buf,
6397 size_t crt_buf_len )
6398{
6399 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6400
6401 if( peer_crt == NULL )
6402 return( -1 );
6403
6404 if( peer_crt->raw.len != crt_buf_len )
6405 return( -1 );
6406
6407 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6408}
6409#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006410MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006411static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6412 unsigned char *crt_buf,
6413 size_t crt_buf_len )
6414{
6415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6416 unsigned char const * const peer_cert_digest =
6417 ssl->session->peer_cert_digest;
6418 mbedtls_md_type_t const peer_cert_digest_type =
6419 ssl->session->peer_cert_digest_type;
6420 mbedtls_md_info_t const * const digest_info =
6421 mbedtls_md_info_from_type( peer_cert_digest_type );
6422 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6423 size_t digest_len;
6424
6425 if( peer_cert_digest == NULL || digest_info == NULL )
6426 return( -1 );
6427
6428 digest_len = mbedtls_md_get_size( digest_info );
6429 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6430 return( -1 );
6431
6432 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6433 if( ret != 0 )
6434 return( -1 );
6435
6436 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6437}
6438#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6439#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6440
6441/*
6442 * Once the certificate message is read, parse it into a cert chain and
6443 * perform basic checks, but leave actual verification to the caller
6444 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006445MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006446static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6447 mbedtls_x509_crt *chain )
6448{
6449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6450#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6451 int crt_cnt=0;
6452#endif
6453 size_t i, n;
6454 uint8_t alert;
6455
6456 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6457 {
6458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6459 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6460 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6461 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6462 }
6463
6464 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6465 {
6466 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6467 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6468 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6469 }
6470
6471 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6472 {
6473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6474 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6475 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6476 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6477 }
6478
6479 i = mbedtls_ssl_hs_hdr_len( ssl );
6480
6481 /*
6482 * Same message structure as in mbedtls_ssl_write_certificate()
6483 */
6484 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6485
6486 if( ssl->in_msg[i] != 0 ||
6487 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6488 {
6489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6490 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6491 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6492 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6493 }
6494
6495 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6496 i += 3;
6497
6498 /* Iterate through and parse the CRTs in the provided chain. */
6499 while( i < ssl->in_hslen )
6500 {
6501 /* Check that there's room for the next CRT's length fields. */
6502 if ( i + 3 > ssl->in_hslen ) {
6503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6504 mbedtls_ssl_send_alert_message( ssl,
6505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6506 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6507 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6508 }
6509 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6510 * anything beyond 2**16 ~ 64K. */
6511 if( ssl->in_msg[i] != 0 )
6512 {
6513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6514 mbedtls_ssl_send_alert_message( ssl,
6515 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6516 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6517 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6518 }
6519
6520 /* Read length of the next CRT in the chain. */
6521 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6522 | (unsigned int) ssl->in_msg[i + 2];
6523 i += 3;
6524
6525 if( n < 128 || i + n > ssl->in_hslen )
6526 {
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6528 mbedtls_ssl_send_alert_message( ssl,
6529 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6530 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6531 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6532 }
6533
6534 /* Check if we're handling the first CRT in the chain. */
6535#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6536 if( crt_cnt++ == 0 &&
6537 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6538 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6539 {
6540 /* During client-side renegotiation, check that the server's
6541 * end-CRTs hasn't changed compared to the initial handshake,
6542 * mitigating the triple handshake attack. On success, reuse
6543 * the original end-CRT instead of parsing it again. */
6544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6545 if( ssl_check_peer_crt_unchanged( ssl,
6546 &ssl->in_msg[i],
6547 n ) != 0 )
6548 {
6549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6550 mbedtls_ssl_send_alert_message( ssl,
6551 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6552 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6553 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6554 }
6555
6556 /* Now we can safely free the original chain. */
6557 ssl_clear_peer_cert( ssl->session );
6558 }
6559#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6560
6561 /* Parse the next certificate in the chain. */
6562#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6563 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6564#else
6565 /* If we don't need to store the CRT chain permanently, parse
6566 * it in-place from the input buffer instead of making a copy. */
6567 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6568#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6569 switch( ret )
6570 {
6571 case 0: /*ok*/
6572 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6573 /* Ignore certificate with an unknown algorithm: maybe a
6574 prior certificate was already trusted. */
6575 break;
6576
6577 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6578 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6579 goto crt_parse_der_failed;
6580
6581 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6582 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6583 goto crt_parse_der_failed;
6584
6585 default:
6586 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6587 crt_parse_der_failed:
6588 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6589 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6590 return( ret );
6591 }
6592
6593 i += n;
6594 }
6595
6596 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6597 return( 0 );
6598}
6599
6600#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006601MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006602static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6603{
6604 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6605 return( -1 );
6606
6607 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6608 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6609 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6610 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6611 {
Ronald Cron19385882022-06-15 16:26:13 +02006612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006613 return( 0 );
6614 }
6615 return( -1 );
6616}
6617#endif /* MBEDTLS_SSL_SRV_C */
6618
6619/* Check if a certificate message is expected.
6620 * Return either
6621 * - SSL_CERTIFICATE_EXPECTED, or
6622 * - SSL_CERTIFICATE_SKIP
6623 * indicating whether a Certificate message is expected or not.
6624 */
6625#define SSL_CERTIFICATE_EXPECTED 0
6626#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006627MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006628static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6629 int authmode )
6630{
6631 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6632 ssl->handshake->ciphersuite_info;
6633
6634 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6635 return( SSL_CERTIFICATE_SKIP );
6636
6637#if defined(MBEDTLS_SSL_SRV_C)
6638 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6639 {
6640 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6641 return( SSL_CERTIFICATE_SKIP );
6642
6643 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6644 {
6645 ssl->session_negotiate->verify_result =
6646 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6647 return( SSL_CERTIFICATE_SKIP );
6648 }
6649 }
6650#else
6651 ((void) authmode);
6652#endif /* MBEDTLS_SSL_SRV_C */
6653
6654 return( SSL_CERTIFICATE_EXPECTED );
6655}
6656
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006657MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006658static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6659 int authmode,
6660 mbedtls_x509_crt *chain,
6661 void *rs_ctx )
6662{
6663 int ret = 0;
6664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6665 ssl->handshake->ciphersuite_info;
6666 int have_ca_chain = 0;
6667
6668 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6669 void *p_vrfy;
6670
6671 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6672 return( 0 );
6673
6674 if( ssl->f_vrfy != NULL )
6675 {
6676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6677 f_vrfy = ssl->f_vrfy;
6678 p_vrfy = ssl->p_vrfy;
6679 }
6680 else
6681 {
6682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6683 f_vrfy = ssl->conf->f_vrfy;
6684 p_vrfy = ssl->conf->p_vrfy;
6685 }
6686
6687 /*
6688 * Main check: verify certificate
6689 */
6690#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6691 if( ssl->conf->f_ca_cb != NULL )
6692 {
6693 ((void) rs_ctx);
6694 have_ca_chain = 1;
6695
6696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6697 ret = mbedtls_x509_crt_verify_with_ca_cb(
6698 chain,
6699 ssl->conf->f_ca_cb,
6700 ssl->conf->p_ca_cb,
6701 ssl->conf->cert_profile,
6702 ssl->hostname,
6703 &ssl->session_negotiate->verify_result,
6704 f_vrfy, p_vrfy );
6705 }
6706 else
6707#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6708 {
6709 mbedtls_x509_crt *ca_chain;
6710 mbedtls_x509_crl *ca_crl;
6711
6712#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6713 if( ssl->handshake->sni_ca_chain != NULL )
6714 {
6715 ca_chain = ssl->handshake->sni_ca_chain;
6716 ca_crl = ssl->handshake->sni_ca_crl;
6717 }
6718 else
6719#endif
6720 {
6721 ca_chain = ssl->conf->ca_chain;
6722 ca_crl = ssl->conf->ca_crl;
6723 }
6724
6725 if( ca_chain != NULL )
6726 have_ca_chain = 1;
6727
6728 ret = mbedtls_x509_crt_verify_restartable(
6729 chain,
6730 ca_chain, ca_crl,
6731 ssl->conf->cert_profile,
6732 ssl->hostname,
6733 &ssl->session_negotiate->verify_result,
6734 f_vrfy, p_vrfy, rs_ctx );
6735 }
6736
6737 if( ret != 0 )
6738 {
6739 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6740 }
6741
6742#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6743 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6744 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6745#endif
6746
6747 /*
6748 * Secondary checks: always done, but change 'ret' only if it was 0
6749 */
6750
6751#if defined(MBEDTLS_ECP_C)
6752 {
6753 const mbedtls_pk_context *pk = &chain->pk;
6754
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006755 /* If certificate uses an EC key, make sure the curve is OK.
6756 * This is a public key, so it can't be opaque, so can_do() is a good
6757 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006758 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006759 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006760 /* and in the unlikely case the above assumption no longer holds
6761 * we are making sure that pk_ec() here does not return a NULL
6762 */
6763 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6764 if( ec == NULL )
6765 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006767 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6768 }
Jerry Yud9526692022-02-17 14:23:47 +08006769
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006770 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6771 {
6772 ssl->session_negotiate->verify_result |=
6773 MBEDTLS_X509_BADCERT_BAD_KEY;
6774
6775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6776 if( ret == 0 )
6777 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6778 }
Jerry Yud9526692022-02-17 14:23:47 +08006779 }
6780 }
6781#endif /* MBEDTLS_ECP_C */
6782
6783 if( mbedtls_ssl_check_cert_usage( chain,
6784 ciphersuite_info,
6785 ! ssl->conf->endpoint,
6786 &ssl->session_negotiate->verify_result ) != 0 )
6787 {
6788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6789 if( ret == 0 )
6790 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6791 }
6792
6793 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6794 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6795 * with details encoded in the verification flags. All other kinds
6796 * of error codes, including those from the user provided f_vrfy
6797 * functions, are treated as fatal and lead to a failure of
6798 * ssl_parse_certificate even if verification was optional. */
6799 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6800 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6801 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6802 {
6803 ret = 0;
6804 }
6805
6806 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6807 {
6808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6809 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6810 }
6811
6812 if( ret != 0 )
6813 {
6814 uint8_t alert;
6815
6816 /* The certificate may have been rejected for several reasons.
6817 Pick one and send the corresponding alert. Which alert to send
6818 may be a subject of debate in some cases. */
6819 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6820 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6821 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6822 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6823 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6824 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6825 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6826 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6827 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6828 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6829 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6830 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6831 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6832 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6833 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6834 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6835 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6836 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6837 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6838 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6839 else
6840 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6841 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6842 alert );
6843 }
6844
6845#if defined(MBEDTLS_DEBUG_C)
6846 if( ssl->session_negotiate->verify_result != 0 )
6847 {
6848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6849 (unsigned int) ssl->session_negotiate->verify_result ) );
6850 }
6851 else
6852 {
6853 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6854 }
6855#endif /* MBEDTLS_DEBUG_C */
6856
6857 return( ret );
6858}
6859
6860#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006861MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006862static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6863 unsigned char *start, size_t len )
6864{
6865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6866 /* Remember digest of the peer's end-CRT. */
6867 ssl->session_negotiate->peer_cert_digest =
6868 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6869 if( ssl->session_negotiate->peer_cert_digest == NULL )
6870 {
6871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6872 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6873 mbedtls_ssl_send_alert_message( ssl,
6874 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6875 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6876
6877 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6878 }
6879
6880 ret = mbedtls_md( mbedtls_md_info_from_type(
6881 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6882 start, len,
6883 ssl->session_negotiate->peer_cert_digest );
6884
6885 ssl->session_negotiate->peer_cert_digest_type =
6886 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6887 ssl->session_negotiate->peer_cert_digest_len =
6888 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6889
6890 return( ret );
6891}
6892
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006893MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006894static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6895 unsigned char *start, size_t len )
6896{
6897 unsigned char *end = start + len;
6898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6899
6900 /* Make a copy of the peer's raw public key. */
6901 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6902 ret = mbedtls_pk_parse_subpubkey( &start, end,
6903 &ssl->handshake->peer_pubkey );
6904 if( ret != 0 )
6905 {
6906 /* We should have parsed the public key before. */
6907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6908 }
6909
6910 return( 0 );
6911}
6912#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6913
6914int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6915{
6916 int ret = 0;
6917 int crt_expected;
6918#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6919 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6920 ? ssl->handshake->sni_authmode
6921 : ssl->conf->authmode;
6922#else
6923 const int authmode = ssl->conf->authmode;
6924#endif
6925 void *rs_ctx = NULL;
6926 mbedtls_x509_crt *chain = NULL;
6927
6928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6929
6930 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6931 if( crt_expected == SSL_CERTIFICATE_SKIP )
6932 {
6933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6934 goto exit;
6935 }
6936
6937#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6938 if( ssl->handshake->ecrs_enabled &&
6939 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6940 {
6941 chain = ssl->handshake->ecrs_peer_cert;
6942 ssl->handshake->ecrs_peer_cert = NULL;
6943 goto crt_verify;
6944 }
6945#endif
6946
6947 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6948 {
6949 /* mbedtls_ssl_read_record may have sent an alert already. We
6950 let it decide whether to alert. */
6951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6952 goto exit;
6953 }
6954
6955#if defined(MBEDTLS_SSL_SRV_C)
6956 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6957 {
6958 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6959
6960 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6961 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6962
6963 goto exit;
6964 }
6965#endif /* MBEDTLS_SSL_SRV_C */
6966
6967 /* Clear existing peer CRT structure in case we tried to
6968 * reuse a session but it failed, and allocate a new one. */
6969 ssl_clear_peer_cert( ssl->session_negotiate );
6970
6971 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6972 if( chain == NULL )
6973 {
6974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6975 sizeof( mbedtls_x509_crt ) ) );
6976 mbedtls_ssl_send_alert_message( ssl,
6977 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6978 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6979
6980 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6981 goto exit;
6982 }
6983 mbedtls_x509_crt_init( chain );
6984
6985 ret = ssl_parse_certificate_chain( ssl, chain );
6986 if( ret != 0 )
6987 goto exit;
6988
6989#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6990 if( ssl->handshake->ecrs_enabled)
6991 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6992
6993crt_verify:
6994 if( ssl->handshake->ecrs_enabled)
6995 rs_ctx = &ssl->handshake->ecrs_ctx;
6996#endif
6997
6998 ret = ssl_parse_certificate_verify( ssl, authmode,
6999 chain, rs_ctx );
7000 if( ret != 0 )
7001 goto exit;
7002
7003#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7004 {
7005 unsigned char *crt_start, *pk_start;
7006 size_t crt_len, pk_len;
7007
7008 /* We parse the CRT chain without copying, so
7009 * these pointers point into the input buffer,
7010 * and are hence still valid after freeing the
7011 * CRT chain. */
7012
7013 crt_start = chain->raw.p;
7014 crt_len = chain->raw.len;
7015
7016 pk_start = chain->pk_raw.p;
7017 pk_len = chain->pk_raw.len;
7018
7019 /* Free the CRT structures before computing
7020 * digest and copying the peer's public key. */
7021 mbedtls_x509_crt_free( chain );
7022 mbedtls_free( chain );
7023 chain = NULL;
7024
7025 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7026 if( ret != 0 )
7027 goto exit;
7028
7029 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7030 if( ret != 0 )
7031 goto exit;
7032 }
7033#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7034 /* Pass ownership to session structure. */
7035 ssl->session_negotiate->peer_cert = chain;
7036 chain = NULL;
7037#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7038
7039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7040
7041exit:
7042
7043 if( ret == 0 )
7044 ssl->state++;
7045
7046#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7047 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7048 {
7049 ssl->handshake->ecrs_peer_cert = chain;
7050 chain = NULL;
7051 }
7052#endif
7053
7054 if( chain != NULL )
7055 {
7056 mbedtls_x509_crt_free( chain );
7057 mbedtls_free( chain );
7058 }
7059
7060 return( ret );
7061}
7062#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7063
Andrzej Kurek25f27152022-08-17 16:09:31 -04007064#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007065static void ssl_calc_finished_tls_sha256(
7066 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7067{
7068 int len = 12;
7069 const char *sender;
7070 unsigned char padbuf[32];
7071#if defined(MBEDTLS_USE_PSA_CRYPTO)
7072 size_t hash_size;
7073 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7074 psa_status_t status;
7075#else
7076 mbedtls_sha256_context sha256;
7077#endif
7078
7079 mbedtls_ssl_session *session = ssl->session_negotiate;
7080 if( !session )
7081 session = ssl->session;
7082
7083 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7084 ? "client finished"
7085 : "server finished";
7086
7087#if defined(MBEDTLS_USE_PSA_CRYPTO)
7088 sha256_psa = psa_hash_operation_init();
7089
7090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7091
7092 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7093 if( status != PSA_SUCCESS )
7094 {
7095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7096 return;
7097 }
7098
7099 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7100 if( status != PSA_SUCCESS )
7101 {
7102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7103 return;
7104 }
7105 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7106#else
7107
7108 mbedtls_sha256_init( &sha256 );
7109
7110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7111
7112 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7113
7114 /*
7115 * TLSv1.2:
7116 * hash = PRF( master, finished_label,
7117 * Hash( handshake ) )[0.11]
7118 */
7119
7120#if !defined(MBEDTLS_SHA256_ALT)
7121 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7122 sha256.state, sizeof( sha256.state ) );
7123#endif
7124
7125 mbedtls_sha256_finish( &sha256, padbuf );
7126 mbedtls_sha256_free( &sha256 );
7127#endif /* MBEDTLS_USE_PSA_CRYPTO */
7128
7129 ssl->handshake->tls_prf( session->master, 48, sender,
7130 padbuf, 32, buf, len );
7131
7132 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7133
7134 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7135
7136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7137}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007138#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007139
Jerry Yub7ba49e2022-02-17 14:25:53 +08007140
Andrzej Kurek25f27152022-08-17 16:09:31 -04007141#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007142static void ssl_calc_finished_tls_sha384(
7143 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7144{
7145 int len = 12;
7146 const char *sender;
7147 unsigned char padbuf[48];
7148#if defined(MBEDTLS_USE_PSA_CRYPTO)
7149 size_t hash_size;
7150 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7151 psa_status_t status;
7152#else
7153 mbedtls_sha512_context sha512;
7154#endif
7155
7156 mbedtls_ssl_session *session = ssl->session_negotiate;
7157 if( !session )
7158 session = ssl->session;
7159
7160 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7161 ? "client finished"
7162 : "server finished";
7163
7164#if defined(MBEDTLS_USE_PSA_CRYPTO)
7165 sha384_psa = psa_hash_operation_init();
7166
7167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7168
7169 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7170 if( status != PSA_SUCCESS )
7171 {
7172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7173 return;
7174 }
7175
7176 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7177 if( status != PSA_SUCCESS )
7178 {
7179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7180 return;
7181 }
7182 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7183#else
7184 mbedtls_sha512_init( &sha512 );
7185
7186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7187
Andrzej Kureka242e832022-08-11 10:03:14 -04007188 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007189
7190 /*
7191 * TLSv1.2:
7192 * hash = PRF( master, finished_label,
7193 * Hash( handshake ) )[0.11]
7194 */
7195
7196#if !defined(MBEDTLS_SHA512_ALT)
7197 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7198 sha512.state, sizeof( sha512.state ) );
7199#endif
7200 mbedtls_sha512_finish( &sha512, padbuf );
7201
7202 mbedtls_sha512_free( &sha512 );
7203#endif
7204
7205 ssl->handshake->tls_prf( session->master, 48, sender,
7206 padbuf, 48, buf, len );
7207
7208 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7209
7210 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7211
7212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7213}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007214#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007215
Jerry Yuaef00152022-02-17 14:27:31 +08007216void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7217{
7218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7219
7220 /*
7221 * Free our handshake params
7222 */
7223 mbedtls_ssl_handshake_free( ssl );
7224 mbedtls_free( ssl->handshake );
7225 ssl->handshake = NULL;
7226
7227 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007228 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007229 */
7230 if( ssl->transform )
7231 {
7232 mbedtls_ssl_transform_free( ssl->transform );
7233 mbedtls_free( ssl->transform );
7234 }
7235 ssl->transform = ssl->transform_negotiate;
7236 ssl->transform_negotiate = NULL;
7237
7238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7239}
7240
Jerry Yu2a9fff52022-02-17 14:28:51 +08007241void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7242{
7243 int resume = ssl->handshake->resume;
7244
7245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7246
7247#if defined(MBEDTLS_SSL_RENEGOTIATION)
7248 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7249 {
7250 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7251 ssl->renego_records_seen = 0;
7252 }
7253#endif
7254
7255 /*
7256 * Free the previous session and switch in the current one
7257 */
7258 if( ssl->session )
7259 {
7260#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7261 /* RFC 7366 3.1: keep the EtM state */
7262 ssl->session_negotiate->encrypt_then_mac =
7263 ssl->session->encrypt_then_mac;
7264#endif
7265
7266 mbedtls_ssl_session_free( ssl->session );
7267 mbedtls_free( ssl->session );
7268 }
7269 ssl->session = ssl->session_negotiate;
7270 ssl->session_negotiate = NULL;
7271
7272 /*
7273 * Add cache entry
7274 */
7275 if( ssl->conf->f_set_cache != NULL &&
7276 ssl->session->id_len != 0 &&
7277 resume == 0 )
7278 {
7279 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7280 ssl->session->id,
7281 ssl->session->id_len,
7282 ssl->session ) != 0 )
7283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7284 }
7285
7286#if defined(MBEDTLS_SSL_PROTO_DTLS)
7287 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7288 ssl->handshake->flight != NULL )
7289 {
7290 /* Cancel handshake timer */
7291 mbedtls_ssl_set_timer( ssl, 0 );
7292
7293 /* Keep last flight around in case we need to resend it:
7294 * we need the handshake and transform structures for that */
7295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7296 }
7297 else
7298#endif
7299 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7300
7301 ssl->state++;
7302
7303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7304}
7305
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007306int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7307{
7308 int ret, hash_len;
7309
7310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7311
7312 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7313
7314 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7315
7316 /*
7317 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7318 * may define some other value. Currently (early 2016), no defined
7319 * ciphersuite does this (and this is unlikely to change as activity has
7320 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7321 */
7322 hash_len = 12;
7323
7324#if defined(MBEDTLS_SSL_RENEGOTIATION)
7325 ssl->verify_data_len = hash_len;
7326 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7327#endif
7328
7329 ssl->out_msglen = 4 + hash_len;
7330 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7331 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7332
7333 /*
7334 * In case of session resuming, invert the client and server
7335 * ChangeCipherSpec messages order.
7336 */
7337 if( ssl->handshake->resume != 0 )
7338 {
7339#if defined(MBEDTLS_SSL_CLI_C)
7340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7341 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7342#endif
7343#if defined(MBEDTLS_SSL_SRV_C)
7344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7345 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7346#endif
7347 }
7348 else
7349 ssl->state++;
7350
7351 /*
7352 * Switch to our negotiated transform and session parameters for outbound
7353 * data.
7354 */
7355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7356
7357#if defined(MBEDTLS_SSL_PROTO_DTLS)
7358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7359 {
7360 unsigned char i;
7361
7362 /* Remember current epoch settings for resending */
7363 ssl->handshake->alt_transform_out = ssl->transform_out;
7364 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7365 sizeof( ssl->handshake->alt_out_ctr ) );
7366
7367 /* Set sequence_number to zero */
7368 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7369
7370
7371 /* Increment epoch */
7372 for( i = 2; i > 0; i-- )
7373 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7374 break;
7375
7376 /* The loop goes to its end iff the counter is wrapping */
7377 if( i == 0 )
7378 {
7379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7380 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7381 }
7382 }
7383 else
7384#endif /* MBEDTLS_SSL_PROTO_DTLS */
7385 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7386
7387 ssl->transform_out = ssl->transform_negotiate;
7388 ssl->session_out = ssl->session_negotiate;
7389
7390#if defined(MBEDTLS_SSL_PROTO_DTLS)
7391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7392 mbedtls_ssl_send_flight_completed( ssl );
7393#endif
7394
7395 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7396 {
7397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7398 return( ret );
7399 }
7400
7401#if defined(MBEDTLS_SSL_PROTO_DTLS)
7402 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7403 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7404 {
7405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7406 return( ret );
7407 }
7408#endif
7409
7410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7411
7412 return( 0 );
7413}
7414
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007415#define SSL_MAX_HASH_LEN 12
7416
7417int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7418{
7419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7420 unsigned int hash_len = 12;
7421 unsigned char buf[SSL_MAX_HASH_LEN];
7422
7423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7424
7425 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7426
7427 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7428 {
7429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7430 goto exit;
7431 }
7432
7433 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7434 {
7435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7436 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7437 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7438 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7439 goto exit;
7440 }
7441
7442 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7443 {
7444 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7445 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7446 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7447 goto exit;
7448 }
7449
7450 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7451 {
7452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7453 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7454 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7455 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7456 goto exit;
7457 }
7458
7459 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7460 buf, hash_len ) != 0 )
7461 {
7462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7463 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7464 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7465 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7466 goto exit;
7467 }
7468
7469#if defined(MBEDTLS_SSL_RENEGOTIATION)
7470 ssl->verify_data_len = hash_len;
7471 memcpy( ssl->peer_verify_data, buf, hash_len );
7472#endif
7473
7474 if( ssl->handshake->resume != 0 )
7475 {
7476#if defined(MBEDTLS_SSL_CLI_C)
7477 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7478 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7479#endif
7480#if defined(MBEDTLS_SSL_SRV_C)
7481 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7482 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7483#endif
7484 }
7485 else
7486 ssl->state++;
7487
7488#if defined(MBEDTLS_SSL_PROTO_DTLS)
7489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7490 mbedtls_ssl_recv_flight_completed( ssl );
7491#endif
7492
7493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7494
7495exit:
7496 mbedtls_platform_zeroize( buf, hash_len );
7497 return( ret );
7498}
7499
Jerry Yu392112c2022-02-17 14:34:10 +08007500#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7501/*
7502 * Helper to get TLS 1.2 PRF from ciphersuite
7503 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7504 */
7505static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7506{
Jerry Yu392112c2022-02-17 14:34:10 +08007507 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007508 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7509#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007510 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007511 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007512 else
Jerry Yu392112c2022-02-17 14:34:10 +08007513#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007514#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7515 {
7516 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7517 return( tls_prf_sha256 );
7518 }
7519#endif
7520#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7521 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7522 (void) ciphersuite_info;
7523#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007524
Andrzej Kurek894edde2022-09-29 06:31:14 -04007525 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007526}
7527#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007528
7529static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7530{
7531 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007532#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007533 if( tls_prf == tls_prf_sha384 )
7534 {
7535 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7536 }
7537 else
7538#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007539#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007540 if( tls_prf == tls_prf_sha256 )
7541 {
7542 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7543 }
7544 else
7545#endif
7546 return( MBEDTLS_SSL_TLS_PRF_NONE );
7547}
7548
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007549/*
7550 * Populate a transform structure with session keys and all the other
7551 * necessary information.
7552 *
7553 * Parameters:
7554 * - [in/out]: transform: structure to populate
7555 * [in] must be just initialised with mbedtls_ssl_transform_init()
7556 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7557 * - [in] ciphersuite
7558 * - [in] master
7559 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007560 * - [in] tls_prf: pointer to PRF to use for key derivation
7561 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007562 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007563 * - [in] endpoint: client or server
7564 * - [in] ssl: used for:
7565 * - ssl->conf->{f,p}_export_keys
7566 * [in] optionally used for:
7567 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7568 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007569MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007570static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7571 int ciphersuite,
7572 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007573#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007574 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007575#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007576 ssl_tls_prf_t tls_prf,
7577 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007578 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007579 unsigned endpoint,
7580 const mbedtls_ssl_context *ssl )
7581{
7582 int ret = 0;
7583 unsigned char keyblk[256];
7584 unsigned char *key1;
7585 unsigned char *key2;
7586 unsigned char *mac_enc;
7587 unsigned char *mac_dec;
7588 size_t mac_key_len = 0;
7589 size_t iv_copy_len;
7590 size_t keylen;
7591 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007592 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007593#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007594 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007595 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007596#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007597
7598#if defined(MBEDTLS_USE_PSA_CRYPTO)
7599 psa_key_type_t key_type;
7600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7601 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007602 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007603 size_t key_bits;
7604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7605#endif
7606
7607#if !defined(MBEDTLS_DEBUG_C) && \
7608 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7609 if( ssl->f_export_keys == NULL )
7610 {
7611 ssl = NULL; /* make sure we don't use it except for these cases */
7612 (void) ssl;
7613 }
7614#endif
7615
7616 /*
7617 * Some data just needs copying into the structure
7618 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007619#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007620 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007621#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007622 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623
7624#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7625 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7626#endif
7627
7628#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007629 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007630 {
7631 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7632 * generation separate. This should never happen. */
7633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7634 }
7635#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7636
7637 /*
7638 * Get various info structures
7639 */
7640 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7641 if( ciphersuite_info == NULL )
7642 {
7643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7644 ciphersuite ) );
7645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7646 }
7647
Neil Armstrongab555e02022-04-04 11:07:59 +02007648 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007649#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007650 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007651#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007652 ciphersuite_info );
7653
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007654 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7655 transform->taglen =
7656 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7657
7658#if defined(MBEDTLS_USE_PSA_CRYPTO)
7659 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7660 transform->taglen,
7661 &alg,
7662 &key_type,
7663 &key_bits ) ) != PSA_SUCCESS )
7664 {
7665 ret = psa_ssl_status_to_mbedtls( status );
7666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7667 goto end;
7668 }
7669#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007670 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7671 if( cipher_info == NULL )
7672 {
7673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7674 ciphersuite_info->cipher ) );
7675 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7676 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007677#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007678
Neil Armstronge4512952022-03-08 09:08:22 +01007679#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007680 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007681 if( mac_alg == 0 )
7682 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007684 (unsigned) ciphersuite_info->mac ) );
7685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7686 }
7687#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007688 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7689 if( md_info == NULL )
7690 {
7691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7692 (unsigned) ciphersuite_info->mac ) );
7693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7694 }
Neil Armstronge4512952022-03-08 09:08:22 +01007695#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007696
7697#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7698 /* Copy own and peer's CID if the use of the CID
7699 * extension has been negotiated. */
7700 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7701 {
7702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7703
7704 transform->in_cid_len = ssl->own_cid_len;
7705 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7706 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7707 transform->in_cid_len );
7708
7709 transform->out_cid_len = ssl->handshake->peer_cid_len;
7710 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7711 ssl->handshake->peer_cid_len );
7712 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7713 transform->out_cid_len );
7714 }
7715#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7716
7717 /*
7718 * Compute key block using the PRF
7719 */
7720 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7721 if( ret != 0 )
7722 {
7723 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7724 return( ret );
7725 }
7726
7727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7728 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7729 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7730 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7731 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7732
7733 /*
7734 * Determine the appropriate key, IV and MAC length.
7735 */
7736
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007737#if defined(MBEDTLS_USE_PSA_CRYPTO)
7738 keylen = PSA_BITS_TO_BYTES(key_bits);
7739#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007740 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007741#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007742
7743#if defined(MBEDTLS_GCM_C) || \
7744 defined(MBEDTLS_CCM_C) || \
7745 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007746 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007747 {
7748 size_t explicit_ivlen;
7749
7750 transform->maclen = 0;
7751 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007752
7753 /* All modes haves 96-bit IVs, but the length of the static parts vary
7754 * with mode and version:
7755 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7756 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7757 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7758 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7759 * sequence number).
7760 */
7761 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007762
7763 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007764#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007765 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007766#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007767 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7768 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007769#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007770
7771 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007772 transform->fixed_ivlen = 12;
7773 else
7774 transform->fixed_ivlen = 4;
7775
7776 /* Minimum length of encrypted record */
7777 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7778 transform->minlen = explicit_ivlen + transform->taglen;
7779 }
7780 else
7781#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7782#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007783 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7784 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7785 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007786 {
Neil Armstronge4512952022-03-08 09:08:22 +01007787#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007788 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007789#else
7790 size_t block_size = cipher_info->block_size;
7791#endif /* MBEDTLS_USE_PSA_CRYPTO */
7792
7793#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007794 /* Get MAC length */
7795 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7796#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007797 /* Initialize HMAC contexts */
7798 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7799 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7800 {
7801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7802 goto end;
7803 }
7804
7805 /* Get MAC length */
7806 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007807#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007808 transform->maclen = mac_key_len;
7809
7810 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007811#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007812 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007813#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007814 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007815#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007816
7817 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007818 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007819 transform->minlen = transform->maclen;
7820 else
7821 {
7822 /*
7823 * GenericBlockCipher:
7824 * 1. if EtM is in use: one block plus MAC
7825 * otherwise: * first multiple of blocklen greater than maclen
7826 * 2. IV
7827 */
7828#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007829 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007830 {
7831 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007832 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007833 }
7834 else
7835#endif
7836 {
7837 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007838 + block_size
7839 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007840 }
7841
Glenn Strauss07c64162022-03-14 12:34:51 -04007842 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007843 {
7844 transform->minlen += transform->ivlen;
7845 }
7846 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007847 {
7848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7849 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7850 goto end;
7851 }
7852 }
7853 }
7854 else
7855#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7856 {
7857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7859 }
7860
7861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7862 (unsigned) keylen,
7863 (unsigned) transform->minlen,
7864 (unsigned) transform->ivlen,
7865 (unsigned) transform->maclen ) );
7866
7867 /*
7868 * Finally setup the cipher contexts, IVs and MAC secrets.
7869 */
7870#if defined(MBEDTLS_SSL_CLI_C)
7871 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7872 {
7873 key1 = keyblk + mac_key_len * 2;
7874 key2 = keyblk + mac_key_len * 2 + keylen;
7875
7876 mac_enc = keyblk;
7877 mac_dec = keyblk + mac_key_len;
7878
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007879 iv_copy_len = ( transform->fixed_ivlen ) ?
7880 transform->fixed_ivlen : transform->ivlen;
7881 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7882 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7883 iv_copy_len );
7884 }
7885 else
7886#endif /* MBEDTLS_SSL_CLI_C */
7887#if defined(MBEDTLS_SSL_SRV_C)
7888 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7889 {
7890 key1 = keyblk + mac_key_len * 2 + keylen;
7891 key2 = keyblk + mac_key_len * 2;
7892
7893 mac_enc = keyblk + mac_key_len;
7894 mac_dec = keyblk;
7895
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007896 iv_copy_len = ( transform->fixed_ivlen ) ?
7897 transform->fixed_ivlen : transform->ivlen;
7898 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7899 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7900 iv_copy_len );
7901 }
7902 else
7903#endif /* MBEDTLS_SSL_SRV_C */
7904 {
7905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7906 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7907 goto end;
7908 }
7909
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007910 if( ssl != NULL && ssl->f_export_keys != NULL )
7911 {
7912 ssl->f_export_keys( ssl->p_export_keys,
7913 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7914 master, 48,
7915 randbytes + 32,
7916 randbytes,
7917 tls_prf_get_type( tls_prf ) );
7918 }
7919
7920#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007921 transform->psa_alg = alg;
7922
7923 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7924 {
7925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7926 psa_set_key_algorithm( &attributes, alg );
7927 psa_set_key_type( &attributes, key_type );
7928
7929 if( ( status = psa_import_key( &attributes,
7930 key1,
7931 PSA_BITS_TO_BYTES( key_bits ),
7932 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7933 {
7934 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7935 ret = psa_ssl_status_to_mbedtls( status );
7936 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7937 goto end;
7938 }
7939
7940 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7941
7942 if( ( status = psa_import_key( &attributes,
7943 key2,
7944 PSA_BITS_TO_BYTES( key_bits ),
7945 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7946 {
7947 ret = psa_ssl_status_to_mbedtls( status );
7948 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7949 goto end;
7950 }
7951 }
7952#else
7953 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7954 cipher_info ) ) != 0 )
7955 {
7956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7957 goto end;
7958 }
7959
7960 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7961 cipher_info ) ) != 0 )
7962 {
7963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7964 goto end;
7965 }
7966
7967 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7968 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7969 MBEDTLS_ENCRYPT ) ) != 0 )
7970 {
7971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7972 goto end;
7973 }
7974
7975 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7976 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7977 MBEDTLS_DECRYPT ) ) != 0 )
7978 {
7979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7980 goto end;
7981 }
7982
7983#if defined(MBEDTLS_CIPHER_MODE_CBC)
7984 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7985 {
7986 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7987 MBEDTLS_PADDING_NONE ) ) != 0 )
7988 {
7989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7990 goto end;
7991 }
7992
7993 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7994 MBEDTLS_PADDING_NONE ) ) != 0 )
7995 {
7996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7997 goto end;
7998 }
7999 }
8000#endif /* MBEDTLS_CIPHER_MODE_CBC */
8001#endif /* MBEDTLS_USE_PSA_CRYPTO */
8002
Neil Armstrong29c0c042022-03-17 17:47:28 +01008003#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8004 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8005 For AEAD-based ciphersuites, there is nothing to do here. */
8006 if( mac_key_len != 0 )
8007 {
8008#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008009 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008010
8011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008012 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008013 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8014
8015 if( ( status = psa_import_key( &attributes,
8016 mac_enc, mac_key_len,
8017 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8018 {
8019 ret = psa_ssl_status_to_mbedtls( status );
8020 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8021 goto end;
8022 }
8023
Ronald Cronfb39f152022-03-25 14:36:28 +01008024 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008025 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8026#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8027 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8028#endif
8029 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008030 /* mbedtls_ct_hmac() requires the key to be exportable */
8031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8032 PSA_KEY_USAGE_VERIFY_HASH );
8033 else
8034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8035
8036 if( ( status = psa_import_key( &attributes,
8037 mac_dec, mac_key_len,
8038 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8039 {
8040 ret = psa_ssl_status_to_mbedtls( status );
8041 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8042 goto end;
8043 }
8044#else
8045 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8046 if( ret != 0 )
8047 goto end;
8048 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8049 if( ret != 0 )
8050 goto end;
8051#endif /* MBEDTLS_USE_PSA_CRYPTO */
8052 }
8053#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8054
8055 ((void) mac_dec);
8056 ((void) mac_enc);
8057
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008058end:
8059 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8060 return( ret );
8061}
8062
Jerry Yuee40f9d2022-02-17 14:55:16 +08008063#if defined(MBEDTLS_USE_PSA_CRYPTO)
8064int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8065 unsigned char *hash, size_t *hashlen,
8066 unsigned char *data, size_t data_len,
8067 mbedtls_md_type_t md_alg )
8068{
8069 psa_status_t status;
8070 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008071 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008072
8073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8074
8075 if( ( status = psa_hash_setup( &hash_operation,
8076 hash_alg ) ) != PSA_SUCCESS )
8077 {
8078 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8079 goto exit;
8080 }
8081
8082 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8083 64 ) ) != PSA_SUCCESS )
8084 {
8085 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8086 goto exit;
8087 }
8088
8089 if( ( status = psa_hash_update( &hash_operation,
8090 data, data_len ) ) != PSA_SUCCESS )
8091 {
8092 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8093 goto exit;
8094 }
8095
8096 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8097 hashlen ) ) != PSA_SUCCESS )
8098 {
8099 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8100 goto exit;
8101 }
8102
8103exit:
8104 if( status != PSA_SUCCESS )
8105 {
8106 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8107 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8108 switch( status )
8109 {
8110 case PSA_ERROR_NOT_SUPPORTED:
8111 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8112 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8113 case PSA_ERROR_BUFFER_TOO_SMALL:
8114 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8115 case PSA_ERROR_INSUFFICIENT_MEMORY:
8116 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8117 default:
8118 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8119 }
8120 }
8121 return( 0 );
8122}
8123
8124#else
8125
8126int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8127 unsigned char *hash, size_t *hashlen,
8128 unsigned char *data, size_t data_len,
8129 mbedtls_md_type_t md_alg )
8130{
8131 int ret = 0;
8132 mbedtls_md_context_t ctx;
8133 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8134 *hashlen = mbedtls_md_get_size( md_info );
8135
8136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8137
8138 mbedtls_md_init( &ctx );
8139
8140 /*
8141 * digitally-signed struct {
8142 * opaque client_random[32];
8143 * opaque server_random[32];
8144 * ServerDHParams params;
8145 * };
8146 */
8147 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8148 {
8149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8150 goto exit;
8151 }
8152 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8153 {
8154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8155 goto exit;
8156 }
8157 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8158 {
8159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8160 goto exit;
8161 }
8162 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8163 {
8164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8165 goto exit;
8166 }
8167 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8168 {
8169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8170 goto exit;
8171 }
8172
8173exit:
8174 mbedtls_md_free( &ctx );
8175
8176 if( ret != 0 )
8177 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8178 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8179
8180 return( ret );
8181}
8182#endif /* MBEDTLS_USE_PSA_CRYPTO */
8183
Jerry Yud9d91da2022-02-17 14:57:06 +08008184#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8185
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008186/* Find the preferred hash for a given signature algorithm. */
8187unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8188 mbedtls_ssl_context *ssl,
8189 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008190{
Gabor Mezei078e8032022-04-27 21:17:56 +02008191 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008192 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008193
8194 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008195 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008196
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008197 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008198 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008199 unsigned int hash_alg_received =
8200 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8201 received_sig_algs[i] );
8202 unsigned int sig_alg_received =
8203 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8204 received_sig_algs[i] );
8205
8206 if( sig_alg == sig_alg_received )
8207 {
8208#if defined(MBEDTLS_USE_PSA_CRYPTO)
8209 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8210 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008211 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008212 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008213
Neil Armstrong96eceb82022-06-30 18:05:05 +02008214 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8215 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8216 PSA_ALG_ECDSA( psa_hash_alg ),
8217 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008218 continue;
8219
Neil Armstrong96eceb82022-06-30 18:05:05 +02008220 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008221 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8222 PSA_ALG_RSA_PKCS1V15_SIGN(
8223 psa_hash_alg ),
8224 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008225 continue;
8226 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008227#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008228
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008229 return( hash_alg_received );
8230 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008231 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008232
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008233 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008234}
8235
8236#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8237
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008238/* Serialization of TLS 1.2 sessions:
8239 *
8240 * struct {
8241 * uint64 start_time;
8242 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008243 * uint8 session_id_len; // at most 32
8244 * opaque session_id[32];
8245 * opaque master[48]; // fixed length in the standard
8246 * uint32 verify_result;
8247 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8248 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8249 * uint32 ticket_lifetime;
8250 * uint8 mfl_code; // up to 255 according to standard
8251 * uint8 encrypt_then_mac; // 0 or 1
8252 * } serialized_session_tls12;
8253 *
8254 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008255static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008256 unsigned char *buf,
8257 size_t buf_len )
8258{
8259 unsigned char *p = buf;
8260 size_t used = 0;
8261
8262#if defined(MBEDTLS_HAVE_TIME)
8263 uint64_t start;
8264#endif
8265#if defined(MBEDTLS_X509_CRT_PARSE_C)
8266#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8267 size_t cert_len;
8268#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8269#endif /* MBEDTLS_X509_CRT_PARSE_C */
8270
8271 /*
8272 * Time
8273 */
8274#if defined(MBEDTLS_HAVE_TIME)
8275 used += 8;
8276
8277 if( used <= buf_len )
8278 {
8279 start = (uint64_t) session->start;
8280
8281 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8282 p += 8;
8283 }
8284#endif /* MBEDTLS_HAVE_TIME */
8285
8286 /*
8287 * Basic mandatory fields
8288 */
8289 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008290 + 1 /* id_len */
8291 + sizeof( session->id )
8292 + sizeof( session->master )
8293 + 4; /* verify_result */
8294
8295 if( used <= buf_len )
8296 {
8297 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8298 p += 2;
8299
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008300 *p++ = MBEDTLS_BYTE_0( session->id_len );
8301 memcpy( p, session->id, 32 );
8302 p += 32;
8303
8304 memcpy( p, session->master, 48 );
8305 p += 48;
8306
8307 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8308 p += 4;
8309 }
8310
8311 /*
8312 * Peer's end-entity certificate
8313 */
8314#if defined(MBEDTLS_X509_CRT_PARSE_C)
8315#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8316 if( session->peer_cert == NULL )
8317 cert_len = 0;
8318 else
8319 cert_len = session->peer_cert->raw.len;
8320
8321 used += 3 + cert_len;
8322
8323 if( used <= buf_len )
8324 {
8325 *p++ = MBEDTLS_BYTE_2( cert_len );
8326 *p++ = MBEDTLS_BYTE_1( cert_len );
8327 *p++ = MBEDTLS_BYTE_0( cert_len );
8328
8329 if( session->peer_cert != NULL )
8330 {
8331 memcpy( p, session->peer_cert->raw.p, cert_len );
8332 p += cert_len;
8333 }
8334 }
8335#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8336 if( session->peer_cert_digest != NULL )
8337 {
8338 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8339 if( used <= buf_len )
8340 {
8341 *p++ = (unsigned char) session->peer_cert_digest_type;
8342 *p++ = (unsigned char) session->peer_cert_digest_len;
8343 memcpy( p, session->peer_cert_digest,
8344 session->peer_cert_digest_len );
8345 p += session->peer_cert_digest_len;
8346 }
8347 }
8348 else
8349 {
8350 used += 2;
8351 if( used <= buf_len )
8352 {
8353 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8354 *p++ = 0;
8355 }
8356 }
8357#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8358#endif /* MBEDTLS_X509_CRT_PARSE_C */
8359
8360 /*
8361 * Session ticket if any, plus associated data
8362 */
8363#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8364 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8365
8366 if( used <= buf_len )
8367 {
8368 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8369 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8370 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8371
8372 if( session->ticket != NULL )
8373 {
8374 memcpy( p, session->ticket, session->ticket_len );
8375 p += session->ticket_len;
8376 }
8377
8378 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8379 p += 4;
8380 }
8381#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8382
8383 /*
8384 * Misc extension-related info
8385 */
8386#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8387 used += 1;
8388
8389 if( used <= buf_len )
8390 *p++ = session->mfl_code;
8391#endif
8392
8393#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8394 used += 1;
8395
8396 if( used <= buf_len )
8397 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8398#endif
8399
8400 return( used );
8401}
8402
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008403MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008404static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008405 const unsigned char *buf,
8406 size_t len )
8407{
8408#if defined(MBEDTLS_HAVE_TIME)
8409 uint64_t start;
8410#endif
8411#if defined(MBEDTLS_X509_CRT_PARSE_C)
8412#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8413 size_t cert_len;
8414#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8415#endif /* MBEDTLS_X509_CRT_PARSE_C */
8416
8417 const unsigned char *p = buf;
8418 const unsigned char * const end = buf + len;
8419
8420 /*
8421 * Time
8422 */
8423#if defined(MBEDTLS_HAVE_TIME)
8424 if( 8 > (size_t)( end - p ) )
8425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8426
8427 start = ( (uint64_t) p[0] << 56 ) |
8428 ( (uint64_t) p[1] << 48 ) |
8429 ( (uint64_t) p[2] << 40 ) |
8430 ( (uint64_t) p[3] << 32 ) |
8431 ( (uint64_t) p[4] << 24 ) |
8432 ( (uint64_t) p[5] << 16 ) |
8433 ( (uint64_t) p[6] << 8 ) |
8434 ( (uint64_t) p[7] );
8435 p += 8;
8436
8437 session->start = (time_t) start;
8438#endif /* MBEDTLS_HAVE_TIME */
8439
8440 /*
8441 * Basic mandatory fields
8442 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008443 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8445
8446 session->ciphersuite = ( p[0] << 8 ) | p[1];
8447 p += 2;
8448
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008449 session->id_len = *p++;
8450 memcpy( session->id, p, 32 );
8451 p += 32;
8452
8453 memcpy( session->master, p, 48 );
8454 p += 48;
8455
8456 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8457 ( (uint32_t) p[1] << 16 ) |
8458 ( (uint32_t) p[2] << 8 ) |
8459 ( (uint32_t) p[3] );
8460 p += 4;
8461
8462 /* Immediately clear invalid pointer values that have been read, in case
8463 * we exit early before we replaced them with valid ones. */
8464#if defined(MBEDTLS_X509_CRT_PARSE_C)
8465#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8466 session->peer_cert = NULL;
8467#else
8468 session->peer_cert_digest = NULL;
8469#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8470#endif /* MBEDTLS_X509_CRT_PARSE_C */
8471#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8472 session->ticket = NULL;
8473#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8474
8475 /*
8476 * Peer certificate
8477 */
8478#if defined(MBEDTLS_X509_CRT_PARSE_C)
8479#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8480 /* Deserialize CRT from the end of the ticket. */
8481 if( 3 > (size_t)( end - p ) )
8482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8483
8484 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8485 p += 3;
8486
8487 if( cert_len != 0 )
8488 {
8489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8490
8491 if( cert_len > (size_t)( end - p ) )
8492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8493
8494 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8495
8496 if( session->peer_cert == NULL )
8497 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8498
8499 mbedtls_x509_crt_init( session->peer_cert );
8500
8501 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8502 p, cert_len ) ) != 0 )
8503 {
8504 mbedtls_x509_crt_free( session->peer_cert );
8505 mbedtls_free( session->peer_cert );
8506 session->peer_cert = NULL;
8507 return( ret );
8508 }
8509
8510 p += cert_len;
8511 }
8512#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8513 /* Deserialize CRT digest from the end of the ticket. */
8514 if( 2 > (size_t)( end - p ) )
8515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8516
8517 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8518 session->peer_cert_digest_len = (size_t) *p++;
8519
8520 if( session->peer_cert_digest_len != 0 )
8521 {
8522 const mbedtls_md_info_t *md_info =
8523 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8524 if( md_info == NULL )
8525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8526 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8528
8529 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8531
8532 session->peer_cert_digest =
8533 mbedtls_calloc( 1, session->peer_cert_digest_len );
8534 if( session->peer_cert_digest == NULL )
8535 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8536
8537 memcpy( session->peer_cert_digest, p,
8538 session->peer_cert_digest_len );
8539 p += session->peer_cert_digest_len;
8540 }
8541#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8542#endif /* MBEDTLS_X509_CRT_PARSE_C */
8543
8544 /*
8545 * Session ticket and associated data
8546 */
8547#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8548 if( 3 > (size_t)( end - p ) )
8549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8550
8551 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8552 p += 3;
8553
8554 if( session->ticket_len != 0 )
8555 {
8556 if( session->ticket_len > (size_t)( end - p ) )
8557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8558
8559 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8560 if( session->ticket == NULL )
8561 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8562
8563 memcpy( session->ticket, p, session->ticket_len );
8564 p += session->ticket_len;
8565 }
8566
8567 if( 4 > (size_t)( end - p ) )
8568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8569
8570 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8571 ( (uint32_t) p[1] << 16 ) |
8572 ( (uint32_t) p[2] << 8 ) |
8573 ( (uint32_t) p[3] );
8574 p += 4;
8575#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8576
8577 /*
8578 * Misc extension-related info
8579 */
8580#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8581 if( 1 > (size_t)( end - p ) )
8582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8583
8584 session->mfl_code = *p++;
8585#endif
8586
8587#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8588 if( 1 > (size_t)( end - p ) )
8589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8590
8591 session->encrypt_then_mac = *p++;
8592#endif
8593
8594 /* Done, should have consumed entire buffer */
8595 if( p != end )
8596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8597
8598 return( 0 );
8599}
Jerry Yudc7bd172022-02-17 13:44:15 +08008600#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8601
XiaokangQian75d40ef2022-04-20 11:05:24 +00008602int mbedtls_ssl_validate_ciphersuite(
8603 const mbedtls_ssl_context *ssl,
8604 const mbedtls_ssl_ciphersuite_t *suite_info,
8605 mbedtls_ssl_protocol_version min_tls_version,
8606 mbedtls_ssl_protocol_version max_tls_version )
8607{
8608 (void) ssl;
8609
8610 if( suite_info == NULL )
8611 return( -1 );
8612
8613 if( ( suite_info->min_tls_version > max_tls_version ) ||
8614 ( suite_info->max_tls_version < min_tls_version ) )
8615 {
8616 return( -1 );
8617 }
8618
XiaokangQian060d8672022-04-21 09:24:56 +00008619#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008620#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8621 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8622 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8623 {
8624 return( -1 );
8625 }
8626#endif
8627
8628 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8629#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8630 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8631 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8632 {
8633 return( -1 );
8634 }
8635#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8636#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8637
8638 return( 0 );
8639}
8640
Ronald Crone68ab4f2022-10-05 12:46:29 +02008641#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008642/*
8643 * Function for writing a signature algorithm extension.
8644 *
8645 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8646 * value (TLS 1.3 RFC8446):
8647 * enum {
8648 * ....
8649 * ecdsa_secp256r1_sha256( 0x0403 ),
8650 * ecdsa_secp384r1_sha384( 0x0503 ),
8651 * ecdsa_secp521r1_sha512( 0x0603 ),
8652 * ....
8653 * } SignatureScheme;
8654 *
8655 * struct {
8656 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8657 * } SignatureSchemeList;
8658 *
8659 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8660 * value (TLS 1.2 RFC5246):
8661 * enum {
8662 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8663 * sha512(6), (255)
8664 * } HashAlgorithm;
8665 *
8666 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8667 * SignatureAlgorithm;
8668 *
8669 * struct {
8670 * HashAlgorithm hash;
8671 * SignatureAlgorithm signature;
8672 * } SignatureAndHashAlgorithm;
8673 *
8674 * SignatureAndHashAlgorithm
8675 * supported_signature_algorithms<2..2^16-2>;
8676 *
8677 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8678 * generalization of the TLS 1.2 signature algorithm extension.
8679 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8680 * `SignatureScheme` field of TLS 1.3
8681 *
8682 */
8683int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8684 const unsigned char *end, size_t *out_len )
8685{
8686 unsigned char *p = buf;
8687 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8688 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8689
8690 *out_len = 0;
8691
8692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8693
8694 /* Check if we have space for header and length field:
8695 * - extension_type (2 bytes)
8696 * - extension_data_length (2 bytes)
8697 * - supported_signature_algorithms_length (2 bytes)
8698 */
8699 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8700 p += 6;
8701
8702 /*
8703 * Write supported_signature_algorithms
8704 */
8705 supported_sig_alg = p;
8706 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8707 if( sig_alg == NULL )
8708 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8709
8710 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8711 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8713 *sig_alg,
8714 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008715 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8716 continue;
8717 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8718 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8719 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008721 *sig_alg,
8722 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008723 }
8724
8725 /* Length of supported_signature_algorithms */
8726 supported_sig_alg_len = p - supported_sig_alg;
8727 if( supported_sig_alg_len == 0 )
8728 {
8729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8731 }
8732
XiaokangQianeaf36512022-04-24 09:07:44 +00008733 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008734 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008735 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8736
XiaokangQianeaf36512022-04-24 09:07:44 +00008737 *out_len = p - buf;
8738
8739#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8740 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8741#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8742 return( 0 );
8743}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008744#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008745
XiaokangQian40a35232022-05-07 09:02:40 +00008746#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008747/*
8748 * mbedtls_ssl_parse_server_name_ext
8749 *
8750 * Structure of server_name extension:
8751 *
8752 * enum {
8753 * host_name(0), (255)
8754 * } NameType;
8755 * opaque HostName<1..2^16-1>;
8756 *
8757 * struct {
8758 * NameType name_type;
8759 * select (name_type) {
8760 * case host_name: HostName;
8761 * } name;
8762 * } ServerName;
8763 * struct {
8764 * ServerName server_name_list<1..2^16-1>
8765 * } ServerNameList;
8766 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008767MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008768int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8769 const unsigned char *buf,
8770 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008771{
8772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8773 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008774 size_t server_name_list_len, hostname_len;
8775 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008776
XiaokangQianf2a94202022-05-20 06:44:24 +00008777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008778
8779 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008780 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008781 p += 2;
8782
XiaokangQian9b2b7712022-05-17 02:57:00 +00008783 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8784 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008785 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008786 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008787 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008788 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008789 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8790 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008791
8792 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8793 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008794 /* sni_name is intended to be used only during the parsing of the
8795 * ClientHello message (it is reset to NULL before the end of
8796 * the message parsing). Thus it is ok to just point to the
8797 * reception buffer and not make a copy of it.
8798 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008799 ssl->handshake->sni_name = p + 3;
8800 ssl->handshake->sni_name_len = hostname_len;
8801 if( ssl->conf->f_sni == NULL )
8802 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008803 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008804 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008805 if( ret != 0 )
8806 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008807 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008808 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8809 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008810 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8811 }
8812 return( 0 );
8813 }
8814
8815 p += hostname_len + 3;
8816 }
8817
8818 return( 0 );
8819}
8820#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8821
XiaokangQianacb39922022-06-17 10:18:48 +00008822#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008823MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008824int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8825 const unsigned char *buf,
8826 const unsigned char *end )
8827{
8828 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008829 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008830 const unsigned char *protocol_name_list;
8831 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008832 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008833
8834 /* If ALPN not configured, just ignore the extension */
8835 if( ssl->conf->alpn_list == NULL )
8836 return( 0 );
8837
8838 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008839 * RFC7301, section 3.1
8840 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008841 *
XiaokangQianc7403452022-06-23 03:24:12 +00008842 * struct {
8843 * ProtocolName protocol_name_list<2..2^16-1>
8844 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008845 */
8846
XiaokangQianc7403452022-06-23 03:24:12 +00008847 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008848 * protocol_name_list_len 2 bytes
8849 * protocol_name_len 1 bytes
8850 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008851 */
XiaokangQianacb39922022-06-17 10:18:48 +00008852 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8853
XiaokangQianc7403452022-06-23 03:24:12 +00008854 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008855 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008856 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008857 protocol_name_list = p;
8858 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008859
8860 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008861 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008862 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008863 protocol_name_len = *p++;
8864 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8865 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008866 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008867 {
8868 MBEDTLS_SSL_PEND_FATAL_ALERT(
8869 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8870 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008871 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008872 }
8873
8874 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008875 }
8876
8877 /* Use our order of preference */
8878 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8879 {
8880 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008881 p = protocol_name_list;
8882 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008883 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008884 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008885 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008886 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008887 {
8888 ssl->alpn_chosen = *alpn;
8889 return( 0 );
8890 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008891
8892 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008893 }
8894 }
8895
XiaokangQian95d5f542022-06-24 02:29:26 +00008896 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008897 MBEDTLS_SSL_PEND_FATAL_ALERT(
8898 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8899 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8900 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8901}
8902
8903int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8904 unsigned char *buf,
8905 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008906 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008907{
8908 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008909 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008910 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008911
8912 if( ssl->alpn_chosen == NULL )
8913 {
8914 return( 0 );
8915 }
8916
XiaokangQian95d5f542022-06-24 02:29:26 +00008917 protocol_name_len = strlen( ssl->alpn_chosen );
8918 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008919
8920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8921 /*
8922 * 0 . 1 ext identifier
8923 * 2 . 3 ext length
8924 * 4 . 5 protocol list length
8925 * 6 . 6 protocol name length
8926 * 7 . 7+n protocol name
8927 */
8928 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8929
XiaokangQian95d5f542022-06-24 02:29:26 +00008930 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008931
XiaokangQian95d5f542022-06-24 02:29:26 +00008932 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8933 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008934 /* Note: the length of the chosen protocol has been checked to be less
8935 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8936 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008937 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008938
XiaokangQian95d5f542022-06-24 02:29:26 +00008939 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008940 return ( 0 );
8941}
8942#endif /* MBEDTLS_SSL_ALPN */
8943
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008944#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008945 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008946 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008947 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008948int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8949 const char *hostname )
8950{
8951 /* Initialize to suppress unnecessary compiler warning */
8952 size_t hostname_len = 0;
8953
8954 /* Check if new hostname is valid before
8955 * making any change to current one */
8956 if( hostname != NULL )
8957 {
8958 hostname_len = strlen( hostname );
8959
8960 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8962 }
8963
8964 /* Now it's clear that we will overwrite the old hostname,
8965 * so we can free it safely */
8966 if( session->hostname != NULL )
8967 {
8968 mbedtls_platform_zeroize( session->hostname,
8969 strlen( session->hostname ) );
8970 mbedtls_free( session->hostname );
8971 }
8972
8973 /* Passing NULL as hostname shall clear the old one */
8974 if( hostname == NULL )
8975 {
8976 session->hostname = NULL;
8977 }
8978 else
8979 {
8980 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8981 if( session->hostname == NULL )
8982 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8983
8984 memcpy( session->hostname, hostname, hostname_len );
8985 }
8986
8987 return( 0 );
8988}
Xiaokang Qian03409292022-10-12 02:49:52 +00008989#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8990 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008991 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008992 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994#endif /* MBEDTLS_SSL_TLS_C */