blob: dda140ef70888107d872fb2bf016b471dac96fa1 [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 ||
Jerry Yu6848a612022-10-27 13:03:26 +0800171 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
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yud25cab02022-10-31 12:48:30 +0800619#if defined(MBEDTLS_DEBUG_C)
620static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800621 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800622 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
623 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
624 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
625 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
626 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
627 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
628 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
629 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
630 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
631 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
632 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
633 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
634 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
635 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
636 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
637 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
638 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
639 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
640 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
641 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
642 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
643 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
644 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
646 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
647 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
648 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
649};
650
Jerry Yuea52ed92022-11-08 21:01:17 +0800651static unsigned int extension_type_table[]={
Jerry Yud25cab02022-10-31 12:48:30 +0800652 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
653 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
654 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
655 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
657 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
658 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
659 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
660 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
661 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
662 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
664 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
665 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
666 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
668 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
669 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
670 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
671 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
672 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
673 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
674 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
675 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
676 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
677 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
678 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
679 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
680};
681
682const char *mbedtls_ssl_get_extension_name( unsigned int extension_type )
683{
684 return( extension_name_table[
685 mbedtls_ssl_get_extension_id( extension_type ) ] );
686}
687
688static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type )
689{
690 switch( hs_msg_type )
691 {
692 case MBEDTLS_SSL_HS_CLIENT_HELLO:
693 return( "ClientHello" );
694 case MBEDTLS_SSL_HS_SERVER_HELLO:
695 return( "ServerHello" );
696 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
697 return( "HelloRetryRequest" );
698 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
699 return( "NewSessionTicket" );
700 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
701 return( "EncryptedExtensions" );
702 case MBEDTLS_SSL_HS_CERTIFICATE:
703 return( "Certificate" );
704 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
705 return( "CertificateRequest" );
706 }
Jerry Yu79aa7212022-11-08 21:30:21 +0800707 return( "Unknown" );
Jerry Yud25cab02022-10-31 12:48:30 +0800708}
709
Jerry Yu79aa7212022-11-08 21:30:21 +0800710void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type, unsigned int extension_type,
713 const char *extra_msg0, const char *extra_msg1 )
Jerry Yud25cab02022-10-31 12:48:30 +0800714{
715 const char *extra_msg;
716 if( extra_msg0 && extra_msg1 )
717 {
718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
720 "%s: %s(%u) extension %s %s.",
721 ssl_tls13_get_hs_msg_name( hs_msg_type ),
722 mbedtls_ssl_get_extension_name( extension_type ),
723 extension_type,
724 extra_msg0, extra_msg1 );
725 return;
726 }
727
728 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
729 if( extra_msg )
730 {
731 mbedtls_debug_print_msg(
732 ssl, level, file, line,
733 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
734 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
735 extra_msg );
736 return;
737 }
738
739 mbedtls_debug_print_msg(
740 ssl, level, file, line,
741 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
742 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
743}
744
745void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
746 int level, const char *file, int line,
747 int hs_msg_type, uint32_t extensions_mask,
748 const char *extra )
749{
750
751 for( unsigned i = 0;
752 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
753 i++ )
754 {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yu97be6a92022-11-09 22:43:31 +0800757 extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra );
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
761#endif /* MBEDTLS_DEBUG_C */
762
Jerry Yuc73c6182022-02-08 20:29:25 +0800763void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
765{
766 ((void) ciphersuite_info);
767
Andrzej Kurek25f27152022-08-17 16:09:31 -0400768#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800769 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
770 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
771 else
772#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400773#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
775 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
776 else
777#endif
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
780 return;
781 }
782}
783
XiaokangQianadab9a62022-07-18 07:41:26 +0000784void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
785 unsigned hs_type,
786 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100787{
788 unsigned char hs_hdr[4];
789
790 /* Build HS header for checksum update. */
791 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
792 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
793 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
794 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
795
796 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
797}
798
799void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
800 unsigned hs_type,
801 unsigned char const *msg,
802 size_t msg_len )
803{
804 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
805 ssl->handshake->update_checksum( ssl, msg, msg_len );
806}
807
Jerry Yuc73c6182022-02-08 20:29:25 +0800808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
809{
810 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#if defined(MBEDTLS_USE_PSA_CRYPTO)
813 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
814 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
815#else
816 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
817#endif
818#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400819#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
822 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
823#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400824 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#endif
826#endif
827}
828
829static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
830 const unsigned char *buf, size_t len )
831{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
834 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
835#else
836 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
837#endif
838#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
842#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400843 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400846#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
847 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
848 (void) ssl;
849 (void) buf;
850 (void) len;
851#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800852}
853
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
856 const unsigned char *buf, size_t len )
857{
858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
860#else
861 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
862#endif
863}
864#endif
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
868 const unsigned char *buf, size_t len )
869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
871 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
872#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400873 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875}
876#endif
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881
Andrzej Kurek25f27152022-08-17 16:09:31 -0400882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884 handshake->fin_sha256_psa = psa_hash_operation_init();
885 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200888 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500893 handshake->fin_sha384_psa = psa_hash_operation_init();
894 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400896 mbedtls_sha512_init( &handshake->fin_sha384 );
897 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900
901 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_DHM_C)
904 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200906#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200910#if defined(MBEDTLS_USE_PSA_CRYPTO)
911 handshake->psa_pake_ctx = psa_pake_operation_init();
912 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
913#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200914 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200916#if defined(MBEDTLS_SSL_CLI_C)
917 handshake->ecjpake_cache = NULL;
918 handshake->ecjpake_cache_len = 0;
919#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200920#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200921
Gilles Peskineeccd8882020-03-10 12:19:08 +0100922#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200923 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200924#endif
925
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
927 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
928#endif
Hanno Becker75173122019-02-06 16:18:31 +0000929
930#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
931 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
932 mbedtls_pk_init( &handshake->peer_pubkey );
933#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934}
935
Hanno Beckera18d1322018-01-03 14:27:32 +0000936void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200939
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
942 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100943#else
944 mbedtls_cipher_init( &transform->cipher_ctx_enc );
945 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100946#endif
947
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
951 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100952#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_md_init( &transform->md_ctx_enc );
954 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000955#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100956#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957}
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962}
963
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200964MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000966{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000968 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200973 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974
975 /*
976 * Either the pointers are now NULL or cleared properly and can be freed.
977 * Now allocate missing structures.
978 */
979 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200981 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Paul Bakker48916f92012-09-16 19:57:18 +0000988
Paul Bakker82788fb2014-10-20 13:59:19 +0200989 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200991 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200992 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500993#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
994 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400995
Andrzej Kurek4a063792020-10-21 15:08:44 +0200996 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
997 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500998#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000999
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001000 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00001001 if( ssl->handshake == NULL ||
1002 ssl->transform_negotiate == NULL ||
1003 ssl->session_negotiate == NULL )
1004 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_free( ssl->handshake );
1008 mbedtls_free( ssl->transform_negotiate );
1009 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010
1011 ssl->handshake = NULL;
1012 ssl->transform_negotiate = NULL;
1013 ssl->session_negotiate = NULL;
1014
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001015 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001016 }
1017
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001020 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001021 ssl_handshake_params_init( ssl->handshake );
1022
Jerry Yud0766ec2022-09-22 10:46:57 +08001023#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1024 defined(MBEDTLS_SSL_SRV_C) && \
1025 defined(MBEDTLS_SSL_SESSION_TICKETS)
1026 ssl->handshake->new_session_tickets_count =
1027 ssl->conf->new_session_tickets_count ;
1028#endif
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1032 {
1033 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001034
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1036 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1037 else
1038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001039
Hanno Becker0f57a652020-02-05 10:37:26 +00001040 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001041 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001042#endif
1043
Brett Warrene0edc842021-08-17 09:53:13 +01001044/*
1045 * curve_list is translated to IANA TLS group identifiers here because
1046 * mbedtls_ssl_conf_curves returns void and so can't return
1047 * any error codes.
1048 */
1049#if defined(MBEDTLS_ECP_C)
1050#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1051 /* Heap allocate and translate curve_list from internal to IANA group ids */
1052 if ( ssl->conf->curve_list != NULL )
1053 {
1054 size_t length;
1055 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1056
1057 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1058 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1059
1060 /* Leave room for zero termination */
1061 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1062 if ( group_list == NULL )
1063 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1064
1065 for( size_t i = 0; i < length; i++ )
1066 {
1067 const mbedtls_ecp_curve_info *info =
1068 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1069 if ( info == NULL )
1070 {
1071 mbedtls_free( group_list );
1072 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1073 }
1074 group_list[i] = info->tls_id;
1075 }
1076
1077 group_list[length] = 0;
1078
1079 ssl->handshake->group_list = group_list;
1080 ssl->handshake->group_list_heap_allocated = 1;
1081 }
1082 else
1083 {
1084 ssl->handshake->group_list = ssl->conf->group_list;
1085 ssl->handshake->group_list_heap_allocated = 0;
1086 }
1087#endif /* MBEDTLS_DEPRECATED_REMOVED */
1088#endif /* MBEDTLS_ECP_C */
1089
Ronald Crone68ab4f2022-10-05 12:46:29 +02001090#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001091#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001093 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1094 signature algorithms IANA identifiers. */
1095 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001096 ssl->conf->sig_hashes != NULL )
1097 {
1098 const int *md;
1099 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001100 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001101 uint16_t *p;
1102
Jerry Yub476a442022-01-21 18:14:45 +08001103#if defined(static_assert)
1104 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1105 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1106 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001107#endif
1108
Jerry Yuf017ee42022-01-12 15:49:48 +08001109 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1110 {
1111 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1112 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001113#if defined(MBEDTLS_ECDSA_C)
1114 sig_algs_len += sizeof( uint16_t );
1115#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001116
Jerry Yub476a442022-01-21 18:14:45 +08001117#if defined(MBEDTLS_RSA_C)
1118 sig_algs_len += sizeof( uint16_t );
1119#endif
1120 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1121 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001122 }
1123
Jerry Yub476a442022-01-21 18:14:45 +08001124 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001125 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1126
Jerry Yub476a442022-01-21 18:14:45 +08001127 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1128 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001129 if( ssl->handshake->sig_algs == NULL )
1130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1131
1132 p = (uint16_t *)ssl->handshake->sig_algs;
1133 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1134 {
1135 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1136 if( hash == MBEDTLS_SSL_HASH_NONE )
1137 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001138#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001139 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1140 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001141#endif
1142#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1144 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001145#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001146 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001147 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001148 ssl->handshake->sig_algs_heap_allocated = 1;
1149 }
1150 else
Jerry Yua69269a2022-01-17 21:06:01 +08001151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001152 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001153 ssl->handshake->sig_algs_heap_allocated = 0;
1154 }
Jerry Yucc539102022-06-27 16:27:35 +08001155#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001156#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001161/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001162MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001163static int ssl_cookie_write_dummy( void *ctx,
1164 unsigned char **p, unsigned char *end,
1165 const unsigned char *cli_id, size_t cli_id_len )
1166{
1167 ((void) ctx);
1168 ((void) p);
1169 ((void) end);
1170 ((void) cli_id);
1171 ((void) cli_id_len);
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001174}
1175
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001176MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001177static int ssl_cookie_check_dummy( void *ctx,
1178 const unsigned char *cookie, size_t cookie_len,
1179 const unsigned char *cli_id, size_t cli_id_len )
1180{
1181 ((void) ctx);
1182 ((void) cookie);
1183 ((void) cookie_len);
1184 ((void) cli_id);
1185 ((void) cli_id_len);
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001188}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001189#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001190
Paul Bakker5121ce52009-01-03 21:22:43 +00001191/*
1192 * Initialize an SSL context
1193 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001194void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1195{
1196 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1197}
1198
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001199MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001200static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1201{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001202 const mbedtls_ssl_config *conf = ssl->conf;
1203
Ronald Cron6f135e12021-12-08 16:57:54 +01001204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001205 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1206 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1208 {
1209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1211 }
1212
Jerry Yu60835a82021-08-04 10:13:52 +08001213 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1214 return( 0 );
1215 }
1216#endif
1217
1218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001219 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1222 return( 0 );
1223 }
1224#endif
1225
Ronald Cron6f135e12021-12-08 16:57:54 +01001226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001227 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001228 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1230 {
1231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1232 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1233 }
1234
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001235 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1236 {
1237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1238 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1239 }
1240
1241
Ronald Crone1d3f062022-02-10 14:50:54 +01001242 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1243 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001244 }
1245#endif
1246
1247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1248 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1249}
1250
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001251MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001252static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1253{
1254 int ret;
1255 ret = ssl_conf_version_check( ssl );
1256 if( ret != 0 )
1257 return( ret );
1258
Jerry Yudef7ae42022-10-30 14:13:19 +08001259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1260 /* RFC 8446 section 4.4.3
1261 *
1262 * If the verification fails, the receiver MUST terminate the handshake with
1263 * a "decrypt_error" alert.
1264 *
1265 * If the client is configured as TLS 1.3 only with optional verify, return
1266 * bad config.
1267 *
1268 */
1269 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1270 (mbedtls_ssl_context *)ssl ) &&
1271 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1272 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1273 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1274 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1275 {
1276 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001277 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001278 "is not available for TLS 1.3 client" ) );
1279 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1280 }
1281#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1282
Jerry Yu60835a82021-08-04 10:13:52 +08001283 /* Space for further checks */
1284
1285 return( 0 );
1286}
1287
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001288/*
1289 * Setup an SSL context
1290 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001291
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001292int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001293 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294{
Janos Follath865b3eb2019-12-16 11:46:15 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001296 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1297 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001299 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001300
Jerry Yu60835a82021-08-04 10:13:52 +08001301 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1302 return( ret );
1303
Paul Bakker62f2dee2012-09-28 07:31:51 +00001304 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001305 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001306 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001307
1308 /* Set to NULL in case of an error condition */
1309 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001310
Darryl Greenb33cc762019-11-28 14:29:44 +00001311#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1312 ssl->in_buf_len = in_buf_len;
1313#endif
1314 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001315 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001318 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001319 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001320 }
1321
Darryl Greenb33cc762019-11-28 14:29:44 +00001322#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1323 ssl->out_buf_len = out_buf_len;
1324#endif
1325 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001326 if( ssl->out_buf == NULL )
1327 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001330 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
1332
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001333 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001334
Johan Pascalb62bb512015-12-03 21:56:45 +01001335#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001336 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001337#endif
1338
Paul Bakker48916f92012-09-16 19:57:18 +00001339 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001340 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001343
1344error:
1345 mbedtls_free( ssl->in_buf );
1346 mbedtls_free( ssl->out_buf );
1347
1348 ssl->conf = NULL;
1349
Darryl Greenb33cc762019-11-28 14:29:44 +00001350#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1351 ssl->in_buf_len = 0;
1352 ssl->out_buf_len = 0;
1353#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001354 ssl->in_buf = NULL;
1355 ssl->out_buf = NULL;
1356
1357 ssl->in_hdr = NULL;
1358 ssl->in_ctr = NULL;
1359 ssl->in_len = NULL;
1360 ssl->in_iv = NULL;
1361 ssl->in_msg = NULL;
1362
1363 ssl->out_hdr = NULL;
1364 ssl->out_ctr = NULL;
1365 ssl->out_len = NULL;
1366 ssl->out_iv = NULL;
1367 ssl->out_msg = NULL;
1368
k-stachowiak9f7798e2018-07-31 16:52:32 +02001369 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370}
1371
1372/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001373 * Reset an initialized and used SSL context for re-use while retaining
1374 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001375 *
1376 * If partial is non-zero, keep data in the input buffer and client ID.
1377 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001378 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001379void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1380 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001381{
Darryl Greenb33cc762019-11-28 14:29:44 +00001382#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1383 size_t in_buf_len = ssl->in_buf_len;
1384 size_t out_buf_len = ssl->out_buf_len;
1385#else
1386 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1388#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001389
Hanno Beckerb0302c42021-08-03 09:39:42 +01001390#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1391 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001392#endif
1393
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001394 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001395 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001396
Hanno Beckerb0302c42021-08-03 09:39:42 +01001397 mbedtls_ssl_reset_in_out_pointers( ssl );
1398
1399 /* Reset incoming message parsing */
1400 ssl->in_offt = NULL;
1401 ssl->nb_zero = 0;
1402 ssl->in_msgtype = 0;
1403 ssl->in_msglen = 0;
1404 ssl->in_hslen = 0;
1405 ssl->keep_current_message = 0;
1406 ssl->transform_in = NULL;
1407
1408#if defined(MBEDTLS_SSL_PROTO_DTLS)
1409 ssl->next_record_offset = 0;
1410 ssl->in_epoch = 0;
1411#endif
1412
1413 /* Keep current datagram if partial == 1 */
1414 if( partial == 0 )
1415 {
1416 ssl->in_left = 0;
1417 memset( ssl->in_buf, 0, in_buf_len );
1418 }
1419
Ronald Cronad8c17b2022-06-10 17:18:09 +02001420 ssl->send_alert = 0;
1421
Hanno Beckerb0302c42021-08-03 09:39:42 +01001422 /* Reset outgoing message writing */
1423 ssl->out_msgtype = 0;
1424 ssl->out_msglen = 0;
1425 ssl->out_left = 0;
1426 memset( ssl->out_buf, 0, out_buf_len );
1427 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1428 ssl->transform_out = NULL;
1429
1430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1431 mbedtls_ssl_dtls_replay_reset( ssl );
1432#endif
1433
XiaokangQian2b01dc32022-01-21 02:53:13 +00001434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435 if( ssl->transform )
1436 {
1437 mbedtls_ssl_transform_free( ssl->transform );
1438 mbedtls_free( ssl->transform );
1439 ssl->transform = NULL;
1440 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1442
XiaokangQian2b01dc32022-01-21 02:53:13 +00001443#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1444 mbedtls_ssl_transform_free( ssl->transform_application );
1445 mbedtls_free( ssl->transform_application );
1446 ssl->transform_application = NULL;
1447
1448 if( ssl->handshake != NULL )
1449 {
1450 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1451 mbedtls_free( ssl->handshake->transform_earlydata );
1452 ssl->handshake->transform_earlydata = NULL;
1453
1454 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1455 mbedtls_free( ssl->handshake->transform_handshake );
1456 ssl->handshake->transform_handshake = NULL;
1457 }
1458
XiaokangQian2b01dc32022-01-21 02:53:13 +00001459#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001460}
1461
1462int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1463{
1464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1465
1466 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1467
XiaokangQian78b1fa72022-01-19 06:56:30 +00001468 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001469
1470 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_RENEGOTIATION)
1472 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001473 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001474
1475 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1477 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001480
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001482 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001483 if( ssl->session )
1484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_ssl_session_free( ssl->session );
1486 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001487 ssl->session = NULL;
1488 }
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001491 ssl->alpn_chosen = NULL;
1492#endif
1493
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001494#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001495 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001496#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001497 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001498#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001499 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001500 {
1501 mbedtls_free( ssl->cli_id );
1502 ssl->cli_id = NULL;
1503 ssl->cli_id_len = 0;
1504 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001505#endif
1506
Paul Bakker48916f92012-09-16 19:57:18 +00001507 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1508 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001509
1510 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001511}
1512
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001513/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001514 * Reset an initialized and used SSL context for re-use while retaining
1515 * all application-set variables, function pointers and data.
1516 */
1517int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1518{
Hanno Becker43aefe22020-02-05 10:44:56 +00001519 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001520}
1521
1522/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 * SSL set accessors
1524 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001525void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001527 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001530void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001532 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001533}
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001536void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001538 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001539}
1540#endif
1541
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001542void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001544 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001545}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001548
Hanno Becker1841b0a2018-08-24 11:13:57 +01001549void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1550 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001551{
1552 ssl->disable_datagram_packing = !allow_packing;
1553}
1554
1555void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1556 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001558 conf->hs_timeout_min = min;
1559 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001560}
1561#endif
1562
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001563void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001565 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566}
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001569void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001570 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001571 void *p_vrfy )
1572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001573 conf->f_vrfy = f_vrfy;
1574 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001577
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001578void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001579 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 void *p_rng )
1581{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001582 conf->f_rng = f_rng;
1583 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001584}
1585
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001586void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001587 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 void *p_dbg )
1589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001590 conf->f_dbg = f_dbg;
1591 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001592}
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001595 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001596 mbedtls_ssl_send_t *f_send,
1597 mbedtls_ssl_recv_t *f_recv,
1598 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001599{
1600 ssl->p_bio = p_bio;
1601 ssl->f_send = f_send;
1602 ssl->f_recv = f_recv;
1603 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001604}
1605
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001606#if defined(MBEDTLS_SSL_PROTO_DTLS)
1607void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1608{
1609 ssl->mtu = mtu;
1610}
1611#endif
1612
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001613void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001614{
1615 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001616}
1617
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001618void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1619 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001620 mbedtls_ssl_set_timer_t *f_set_timer,
1621 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001622{
1623 ssl->p_timer = p_timer;
1624 ssl->f_set_timer = f_set_timer;
1625 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001626
1627 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001628 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001629}
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001632void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001633 void *p_cache,
1634 mbedtls_ssl_cache_get_t *f_get_cache,
1635 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001636{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001637 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001638 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001639 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_CLI_C)
1644int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001645{
Janos Follath865b3eb2019-12-16 11:46:15 +00001646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001647
1648 if( ssl == NULL ||
1649 session == NULL ||
1650 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001651 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654 }
1655
Hanno Beckere810bbc2021-05-14 16:01:05 +01001656 if( ssl->handshake->resume == 1 )
1657 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1658
Jerry Yu21092062022-10-10 21:21:31 +08001659#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1660 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001661 {
Jerry Yu21092062022-10-10 21:21:31 +08001662 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1663 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1664
1665 if( mbedtls_ssl_validate_ciphersuite(
1666 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1667 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1668 {
1669 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1670 session->ciphersuite ) );
1671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1672 }
Jerry Yu40afab62022-10-08 10:42:13 +08001673 }
Jerry Yu21092062022-10-10 21:21:31 +08001674#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001675
Hanno Becker52055ae2019-02-06 14:30:46 +00001676 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1677 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001678 return( ret );
1679
Paul Bakker0a597072012-09-25 21:55:46 +00001680 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001681
1682 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001686void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1687 const int *ciphersuites )
1688{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001689 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690}
1691
Ronald Cron6f135e12021-12-08 16:57:54 +01001692#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001693void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001694 const int kex_modes )
1695{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001696 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001697}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001698
1699#if defined(MBEDTLS_SSL_EARLY_DATA)
1700void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001701 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001702{
1703 conf->early_data_enabled = early_data_enabled;
1704}
1705#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001706#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001709void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001710 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001711{
1712 conf->cert_profile = profile;
1713}
1714
Glenn Strauss36872db2022-01-22 05:06:31 -05001715static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1716{
1717 mbedtls_ssl_key_cert *cur = key_cert, *next;
1718
1719 while( cur != NULL )
1720 {
1721 next = cur->next;
1722 mbedtls_free( cur );
1723 cur = next;
1724 }
1725}
1726
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001727/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001728MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001729static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1730 mbedtls_x509_crt *cert,
1731 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001732{
niisato8ee24222018-06-25 19:05:48 +09001733 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001734
Glenn Strauss36872db2022-01-22 05:06:31 -05001735 if( cert == NULL )
1736 {
1737 /* Free list if cert is null */
1738 ssl_key_cert_free( *head );
1739 *head = NULL;
1740 return( 0 );
1741 }
1742
niisato8ee24222018-06-25 19:05:48 +09001743 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1744 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001746
niisato8ee24222018-06-25 19:05:48 +09001747 new_cert->cert = cert;
1748 new_cert->key = key;
1749 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001750
Glenn Strauss36872db2022-01-22 05:06:31 -05001751 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001752 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001753 {
niisato8ee24222018-06-25 19:05:48 +09001754 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001755 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001756 else
1757 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001758 mbedtls_ssl_key_cert *cur = *head;
1759 while( cur->next != NULL )
1760 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001761 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001762 }
1763
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001764 return( 0 );
1765}
1766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001767int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001768 mbedtls_x509_crt *own_cert,
1769 mbedtls_pk_context *pk_key )
1770{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001771 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001772}
1773
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001774void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001775 mbedtls_x509_crt *ca_chain,
1776 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001778 conf->ca_chain = ca_chain;
1779 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001780
1781#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1782 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1783 * cannot be used together. */
1784 conf->f_ca_cb = NULL;
1785 conf->p_ca_cb = NULL;
1786#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001787}
Hanno Becker5adaad92019-03-27 16:54:37 +00001788
1789#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1790void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001791 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001792 void *p_ca_cb )
1793{
1794 conf->f_ca_cb = f_ca_cb;
1795 conf->p_ca_cb = p_ca_cb;
1796
1797 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1798 * cannot be used together. */
1799 conf->ca_chain = NULL;
1800 conf->ca_crl = NULL;
1801}
1802#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001804
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001805#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001806const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1807 size_t *name_len )
1808{
1809 *name_len = ssl->handshake->sni_name_len;
1810 return( ssl->handshake->sni_name );
1811}
1812
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001813int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1814 mbedtls_x509_crt *own_cert,
1815 mbedtls_pk_context *pk_key )
1816{
1817 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1818 own_cert, pk_key ) );
1819}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001820
1821void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1822 mbedtls_x509_crt *ca_chain,
1823 mbedtls_x509_crl *ca_crl )
1824{
1825 ssl->handshake->sni_ca_chain = ca_chain;
1826 ssl->handshake->sni_ca_crl = ca_crl;
1827}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001828
Glenn Strauss999ef702022-03-11 01:37:23 -05001829#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1830void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1831 const mbedtls_x509_crt *crt)
1832{
1833 ssl->handshake->dn_hints = crt;
1834}
1835#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1836
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001837void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1838 int authmode )
1839{
1840 ssl->handshake->sni_authmode = authmode;
1841}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001842#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1843
Hanno Becker8927c832019-04-03 12:52:50 +01001844#if defined(MBEDTLS_X509_CRT_PARSE_C)
1845void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1846 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1847 void *p_vrfy )
1848{
1849 ssl->f_vrfy = f_vrfy;
1850 ssl->p_vrfy = p_vrfy;
1851}
1852#endif
1853
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001854#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001855
Valerio Setti016f6822022-12-09 14:17:50 +01001856#if defined(MBEDTLS_USE_PSA_CRYPTO)
1857static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
1858 mbedtls_ssl_context *ssl,
1859 mbedtls_svc_key_id_t pwd )
1860{
1861 psa_status_t status;
1862 psa_pake_role_t psa_role;
1863 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1864
1865 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1866 psa_pake_cs_set_primitive( &cipher_suite,
1867 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1868 PSA_ECC_FAMILY_SECP_R1,
1869 256) );
1870 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1871
1872 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1873 if( status != PSA_SUCCESS )
1874 return status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001875
Neil Armstrongca7d5062022-05-31 14:43:23 +02001876 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1877 psa_role = PSA_PAKE_ROLE_SERVER;
1878 else
1879 psa_role = PSA_PAKE_ROLE_CLIENT;
1880
Valerio Setti016f6822022-12-09 14:17:50 +01001881 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1882 if( status != PSA_SUCCESS )
1883 return status;
1884
1885 status = psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx, pwd );
1886 if( status != PSA_SUCCESS )
1887 return status;
1888
1889 ssl->handshake->psa_pake_ctx_is_ok = 1;
1890
1891 return ( PSA_SUCCESS );
1892}
1893
1894int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1895 const unsigned char *pw,
1896 size_t pw_len )
1897{
1898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1899 psa_status_t status;
1900
1901 if( ssl->handshake == NULL || ssl->conf == NULL )
1902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1903
Valerio Settiaca21b72022-11-17 18:17:01 +01001904 /* Empty password is not valid */
1905 if( ( pw == NULL) || ( pw_len == 0 ) )
1906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001907
Valerio Settiaca21b72022-11-17 18:17:01 +01001908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1909 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1910 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001911
Valerio Settiaca21b72022-11-17 18:17:01 +01001912 status = psa_import_key( &attributes, pw, pw_len,
1913 &ssl->handshake->psa_pake_password );
1914 if( status != PSA_SUCCESS )
1915 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001916
Valerio Setti016f6822022-12-09 14:17:50 +01001917 status = mbedtls_ssl_set_hs_ecjpake_password_common( ssl,
1918 ssl->handshake->psa_pake_password );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001919 if( status != PSA_SUCCESS )
1920 {
1921 psa_destroy_key( ssl->handshake->psa_pake_password );
1922 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1923 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1924 }
1925
Neil Armstrongca7d5062022-05-31 14:43:23 +02001926 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001927}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001928
1929int mbedtls_ssl_set_hs_ecjpake_password_opaque( mbedtls_ssl_context *ssl,
1930 mbedtls_svc_key_id_t pwd )
1931{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001932 psa_status_t status;
1933
1934 if( ssl->handshake == NULL || ssl->conf == NULL )
1935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1936
Valerio Settia9a97dc2022-11-28 18:26:16 +01001937 if( mbedtls_svc_key_id_is_null( pwd ) )
1938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Valerio Settia9a97dc2022-11-28 18:26:16 +01001939
Valerio Setti016f6822022-12-09 14:17:50 +01001940 status = mbedtls_ssl_set_hs_ecjpake_password_common( ssl, pwd );
Valerio Settia9a97dc2022-11-28 18:26:16 +01001941 if( status != PSA_SUCCESS )
Valerio Setti016f6822022-12-09 14:17:50 +01001942 {
1943 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
Valerio Settiae7fe7e2022-12-07 17:36:59 +01001944 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Valerio Setti016f6822022-12-09 14:17:50 +01001945 }
Valerio Settia9a97dc2022-11-28 18:26:16 +01001946
1947 return( 0 );
1948}
Valerio Setti02c25b52022-11-15 14:08:42 +01001949#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001950int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1951 const unsigned char *pw,
1952 size_t pw_len )
1953{
1954 mbedtls_ecjpake_role role;
1955
1956 if( ssl->handshake == NULL || ssl->conf == NULL )
1957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1958
Valerio Settic689ed82022-12-07 14:40:38 +01001959 /* Empty password is not valid */
1960 if( ( pw == NULL) || ( pw_len == 0 ) )
1961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1962
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001963 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1964 role = MBEDTLS_ECJPAKE_SERVER;
1965 else
1966 role = MBEDTLS_ECJPAKE_CLIENT;
1967
1968 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1969 role,
1970 MBEDTLS_MD_SHA256,
1971 MBEDTLS_ECP_DP_SECP256R1,
1972 pw, pw_len ) );
1973}
Valerio Setti02c25b52022-11-15 14:08:42 +01001974#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001975#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001976
Ronald Cron73fe8df2022-10-05 14:31:43 +02001977#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001978int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001979{
Ronald Crond29e13e2022-10-19 10:33:48 +02001980 if( conf->psk_identity == NULL ||
1981 conf->psk_identity_len == 0 )
1982 {
1983 return( 0 );
1984 }
1985
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001986#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001987 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001988 return( 1 );
1989#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001990
1991 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001992 return( 1 );
1993
1994 return( 0 );
1995}
1996
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001997static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1998{
1999 /* Remove reference to existing PSK, if any. */
2000#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002001 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002002 {
2003 /* The maintenance of the PSK key slot is the
2004 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002005 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002006 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002007#endif /* MBEDTLS_USE_PSA_CRYPTO */
2008 if( conf->psk != NULL )
2009 {
2010 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
2011
2012 mbedtls_free( conf->psk );
2013 conf->psk = NULL;
2014 conf->psk_len = 0;
2015 }
2016
2017 /* Remove reference to PSK identity, if any. */
2018 if( conf->psk_identity != NULL )
2019 {
2020 mbedtls_free( conf->psk_identity );
2021 conf->psk_identity = NULL;
2022 conf->psk_identity_len = 0;
2023 }
2024}
2025
Hanno Becker7390c712018-11-15 13:33:04 +00002026/* This function assumes that PSK identity in the SSL config is unset.
2027 * It checks that the provided identity is well-formed and attempts
2028 * to make a copy of it in the SSL config.
2029 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002030MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002031static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2032 unsigned char const *psk_identity,
2033 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002034{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002035 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002036 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002037 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002038 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002039 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002040 {
2041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2042 }
2043
Hanno Becker7390c712018-11-15 13:33:04 +00002044 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2045 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002046 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002047
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002048 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002049 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002050
2051 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002052}
2053
Hanno Becker7390c712018-11-15 13:33:04 +00002054int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2055 const unsigned char *psk, size_t psk_len,
2056 const unsigned char *psk_identity, size_t psk_identity_len )
2057{
Janos Follath865b3eb2019-12-16 11:46:15 +00002058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002059
2060 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002061 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002062 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002063
2064 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002065 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002067 if( psk_len == 0 )
2068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2069 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2071
Hanno Becker7390c712018-11-15 13:33:04 +00002072 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2074 conf->psk_len = psk_len;
2075 memcpy( conf->psk, psk, conf->psk_len );
2076
2077 /* Check and set PSK Identity */
2078 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2079 if( ret != 0 )
2080 ssl_conf_remove_psk( conf );
2081
2082 return( ret );
2083}
2084
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002085static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2086{
2087#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002088 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002089 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002090 /* The maintenance of the external PSK key slot is the
2091 * user's responsibility. */
2092 if( ssl->handshake->psk_opaque_is_internal )
2093 {
2094 psa_destroy_key( ssl->handshake->psk_opaque );
2095 ssl->handshake->psk_opaque_is_internal = 0;
2096 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002097 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002098 }
Neil Armstronge952a302022-05-03 10:22:14 +02002099#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002100 if( ssl->handshake->psk != NULL )
2101 {
2102 mbedtls_platform_zeroize( ssl->handshake->psk,
2103 ssl->handshake->psk_len );
2104 mbedtls_free( ssl->handshake->psk );
2105 ssl->handshake->psk_len = 0;
2106 }
Neil Armstronge952a302022-05-03 10:22:14 +02002107#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002108}
2109
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002110int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2111 const unsigned char *psk, size_t psk_len )
2112{
Neil Armstrong501c9322022-05-03 09:35:09 +02002113#if defined(MBEDTLS_USE_PSA_CRYPTO)
2114 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002115 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002116 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002118#endif /* MBEDTLS_USE_PSA_CRYPTO */
2119
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002120 if( psk == NULL || ssl->handshake == NULL )
2121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2122
2123 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2125
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002126 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002127
Neil Armstrong501c9322022-05-03 09:35:09 +02002128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002129#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2130 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2131 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002132 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002133 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2134 else
2135 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2136 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2137 }
2138#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002139
Jerry Yu568ec252022-07-22 21:27:34 +08002140#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002141 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2142 {
2143 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2144 psa_set_key_usage_flags( &key_attributes,
2145 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2146 }
2147#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2148
Neil Armstrong501c9322022-05-03 09:35:09 +02002149 psa_set_key_algorithm( &key_attributes, alg );
2150 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2151
2152 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2153 if( status != PSA_SUCCESS )
2154 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2155
2156 /* Allow calling psa_destroy_key() on psk remove */
2157 ssl->handshake->psk_opaque_is_internal = 1;
2158 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2159#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002160 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002161 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002162
2163 ssl->handshake->psk_len = psk_len;
2164 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2165
2166 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002167#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002168}
2169
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002170#if defined(MBEDTLS_USE_PSA_CRYPTO)
2171int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002172 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002173 const unsigned char *psk_identity,
2174 size_t psk_identity_len )
2175{
Janos Follath865b3eb2019-12-16 11:46:15 +00002176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002177
2178 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002179 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002180 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002181
Hanno Becker7390c712018-11-15 13:33:04 +00002182 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002183 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002185 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002186
2187 /* Check and set PSK Identity */
2188 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2189 psk_identity_len );
2190 if( ret != 0 )
2191 ssl_conf_remove_psk( conf );
2192
2193 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002194}
2195
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002196int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2197 mbedtls_svc_key_id_t psk )
2198{
2199 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2200 ( ssl->handshake == NULL ) )
2201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2202
2203 ssl_remove_psk( ssl );
2204 ssl->handshake->psk_opaque = psk;
2205 return( 0 );
2206}
2207#endif /* MBEDTLS_USE_PSA_CRYPTO */
2208
Jerry Yu8897c072022-08-12 13:56:53 +08002209#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002210void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2211 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2212 size_t),
2213 void *p_psk )
2214{
2215 conf->f_psk = f_psk;
2216 conf->p_psk = p_psk;
2217}
Jerry Yu8897c072022-08-12 13:56:53 +08002218#endif /* MBEDTLS_SSL_SRV_C */
2219
Ronald Cron73fe8df2022-10-05 14:31:43 +02002220#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002221
2222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002223static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2224 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002225{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002226#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002227 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002228 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002229#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002230 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002231 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002232 return( MBEDTLS_SSL_MODE_STREAM );
2233}
2234
2235#else /* MBEDTLS_USE_PSA_CRYPTO */
2236
2237static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2238 mbedtls_cipher_mode_t mode )
2239{
2240#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2241 if( mode == MBEDTLS_MODE_CBC )
2242 return( MBEDTLS_SSL_MODE_CBC );
2243#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2244
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002245#if defined(MBEDTLS_GCM_C) || \
2246 defined(MBEDTLS_CCM_C) || \
2247 defined(MBEDTLS_CHACHAPOLY_C)
2248 if( mode == MBEDTLS_MODE_GCM ||
2249 mode == MBEDTLS_MODE_CCM ||
2250 mode == MBEDTLS_MODE_CHACHAPOLY )
2251 return( MBEDTLS_SSL_MODE_AEAD );
2252#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002253
2254 return( MBEDTLS_SSL_MODE_STREAM );
2255}
Gilles Peskine301711e2022-04-26 16:57:05 +02002256#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002257
Gilles Peskinee108d982022-04-26 16:50:40 +02002258static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2259 mbedtls_ssl_mode_t base_mode,
2260 int encrypt_then_mac )
2261{
2262#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2263 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2264 base_mode == MBEDTLS_SSL_MODE_CBC )
2265 {
2266 return( MBEDTLS_SSL_MODE_CBC_ETM );
2267 }
2268#else
2269 (void) encrypt_then_mac;
2270#endif
2271 return( base_mode );
2272}
2273
Neil Armstrongab555e02022-04-04 11:07:59 +02002274mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002275 const mbedtls_ssl_transform *transform )
2276{
Gilles Peskinee108d982022-04-26 16:50:40 +02002277 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002278#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002279 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002280#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002281 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2282#endif
2283 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002284
Gilles Peskinee108d982022-04-26 16:50:40 +02002285 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002286#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002287 encrypt_then_mac = transform->encrypt_then_mac;
2288#endif
2289 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002290}
2291
Neil Armstrongab555e02022-04-04 11:07:59 +02002292mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002293#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002294 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002295#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002296 const mbedtls_ssl_ciphersuite_t *suite )
2297{
Gilles Peskinee108d982022-04-26 16:50:40 +02002298 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2299
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002300#if defined(MBEDTLS_USE_PSA_CRYPTO)
2301 psa_status_t status;
2302 psa_algorithm_t alg;
2303 psa_key_type_t type;
2304 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002305 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2306 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002307 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002308#else
2309 const mbedtls_cipher_info_t *cipher =
2310 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002311 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002312 {
2313 base_mode =
2314 mbedtls_ssl_get_base_mode(
2315 mbedtls_cipher_info_get_mode( cipher ) );
2316 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002317#endif /* MBEDTLS_USE_PSA_CRYPTO */
2318
Gilles Peskinee108d982022-04-26 16:50:40 +02002319#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2320 int encrypt_then_mac = 0;
2321#endif
2322 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002323}
2324
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002325#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002326
2327#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002328/* Serialization of TLS 1.3 sessions:
2329 *
2330 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002331 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002332 * uint64 ticket_received;
2333 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002334 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002335 * } ClientOnlyData;
2336 *
2337 * struct {
2338 * uint8 endpoint;
2339 * uint8 ciphersuite[2];
2340 * uint32 ticket_age_add;
2341 * uint8 ticket_flags;
2342 * opaque resumption_key<0..255>;
2343 * select ( endpoint ) {
2344 * case client: ClientOnlyData;
2345 * case server: uint64 start_time;
2346 * };
2347 * } serialized_session_tls13;
2348 *
2349 */
2350#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002351MBEDTLS_CHECK_RETURN_CRITICAL
2352static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2353 unsigned char *buf,
2354 size_t buf_len,
2355 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002356{
2357 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002358#if defined(MBEDTLS_SSL_CLI_C) && \
2359 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2360 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002361 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002362#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002363 size_t needed = 1 /* endpoint */
2364 + 2 /* ciphersuite */
2365 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002366 + 1 /* ticket_flags */
2367 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002368 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002369
2370 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2372 needed += session->resumption_key_len; /* resumption_key */
2373
Jerry Yu438ddd82022-07-07 06:55:50 +00002374#if defined(MBEDTLS_HAVE_TIME)
2375 needed += 8; /* start_time or ticket_received */
2376#endif
2377
2378#if defined(MBEDTLS_SSL_CLI_C)
2379 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2380 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002381#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002382 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002383 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002384#endif
2385
Jerry Yu438ddd82022-07-07 06:55:50 +00002386 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002387 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002388
2389 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002390 if( session->ticket_len > SIZE_MAX - needed )
2391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002392
Jerry Yue28d9742022-08-18 15:44:03 +08002393 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002394 }
2395#endif /* MBEDTLS_SSL_CLI_C */
2396
Jerry Yue36fdd62022-08-17 21:31:36 +08002397 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002398 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002399 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002400
2401 p[0] = session->endpoint;
2402 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2403 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2404 p[7] = session->ticket_flags;
2405
2406 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002407 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002408 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002409 memcpy( p, session->resumption_key, session->resumption_key_len );
2410 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002411
2412#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2413 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2414 {
2415 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2416 p += 8;
2417 }
2418#endif /* MBEDTLS_HAVE_TIME */
2419
2420#if defined(MBEDTLS_SSL_CLI_C)
2421 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2422 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002423#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2424 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2425 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002426 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002427 {
2428 /* save host name */
2429 memcpy( p, session->hostname, hostname_len );
2430 p += hostname_len;
2431 }
2432#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2433
Jerry Yu438ddd82022-07-07 06:55:50 +00002434#if defined(MBEDTLS_HAVE_TIME)
2435 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2436 p += 8;
2437#endif
2438 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2439 p += 4;
2440
2441 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2442 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002443
2444 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002445 {
2446 memcpy( p, session->ticket, session->ticket_len );
2447 p += session->ticket_len;
2448 }
2449 }
2450#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002451 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002452}
2453
2454MBEDTLS_CHECK_RETURN_CRITICAL
2455static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2456 const unsigned char *buf,
2457 size_t len )
2458{
2459 const unsigned char *p = buf;
2460 const unsigned char *end = buf + len;
2461
2462 if( end - p < 9 )
2463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2464 session->endpoint = p[0];
2465 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2466 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2467 session->ticket_flags = p[7];
2468
2469 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002470 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002471 p += 9;
2472
Jerry Yubc7c1a42022-07-21 22:57:37 +08002473 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2475
Jerry Yubc7c1a42022-07-21 22:57:37 +08002476 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002478 memcpy( session->resumption_key, p, session->resumption_key_len );
2479 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002480
2481#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2482 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2483 {
2484 if( end - p < 8 )
2485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2486 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2487 p += 8;
2488 }
2489#endif /* MBEDTLS_HAVE_TIME */
2490
2491#if defined(MBEDTLS_SSL_CLI_C)
2492 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2493 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002494#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2495 defined(MBEDTLS_SSL_SESSION_TICKETS)
2496 size_t hostname_len;
2497 /* load host name */
2498 if( end - p < 2 )
2499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2500 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2501 p += 2;
2502
2503 if( end - p < ( long int )hostname_len )
2504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2505 if( hostname_len > 0 )
2506 {
2507 session->hostname = mbedtls_calloc( 1, hostname_len );
2508 if( session->hostname == NULL )
2509 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2510 memcpy( session->hostname, p, hostname_len );
2511 p += hostname_len;
2512 }
2513#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2514 MBEDTLS_SSL_SESSION_TICKETS */
2515
Jerry Yu438ddd82022-07-07 06:55:50 +00002516#if defined(MBEDTLS_HAVE_TIME)
2517 if( end - p < 8 )
2518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2519 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2520 p += 8;
2521#endif
2522 if( end - p < 4 )
2523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2524 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2525 p += 4;
2526
2527 if( end - p < 2 )
2528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2529 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2530 p += 2;
2531
2532 if( end - p < ( long int )session->ticket_len )
2533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2534 if( session->ticket_len > 0 )
2535 {
2536 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2537 if( session->ticket == NULL )
2538 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2539 memcpy( session->ticket, p, session->ticket_len );
2540 p += session->ticket_len;
2541 }
2542 }
2543#endif /* MBEDTLS_SSL_CLI_C */
2544
2545 return( 0 );
2546
2547}
2548#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002549MBEDTLS_CHECK_RETURN_CRITICAL
2550static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2551 unsigned char *buf,
2552 size_t buf_len,
2553 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002554{
2555 ((void) session);
2556 ((void) buf);
2557 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002558 *olen = 0;
2559 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002560}
Jerry Yu438ddd82022-07-07 06:55:50 +00002561
2562static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2563 unsigned char *buf,
2564 size_t buf_len )
2565{
2566 ((void) session);
2567 ((void) buf);
2568 ((void) buf_len);
2569 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2570}
2571#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002572#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2573
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002574psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002575 size_t taglen,
2576 psa_algorithm_t *alg,
2577 psa_key_type_t *key_type,
2578 size_t *key_size )
2579{
2580 switch ( mbedtls_cipher_type )
2581 {
2582 case MBEDTLS_CIPHER_AES_128_CBC:
2583 *alg = PSA_ALG_CBC_NO_PADDING;
2584 *key_type = PSA_KEY_TYPE_AES;
2585 *key_size = 128;
2586 break;
2587 case MBEDTLS_CIPHER_AES_128_CCM:
2588 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2589 *key_type = PSA_KEY_TYPE_AES;
2590 *key_size = 128;
2591 break;
2592 case MBEDTLS_CIPHER_AES_128_GCM:
2593 *alg = PSA_ALG_GCM;
2594 *key_type = PSA_KEY_TYPE_AES;
2595 *key_size = 128;
2596 break;
2597 case MBEDTLS_CIPHER_AES_192_CCM:
2598 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2599 *key_type = PSA_KEY_TYPE_AES;
2600 *key_size = 192;
2601 break;
2602 case MBEDTLS_CIPHER_AES_192_GCM:
2603 *alg = PSA_ALG_GCM;
2604 *key_type = PSA_KEY_TYPE_AES;
2605 *key_size = 192;
2606 break;
2607 case MBEDTLS_CIPHER_AES_256_CBC:
2608 *alg = PSA_ALG_CBC_NO_PADDING;
2609 *key_type = PSA_KEY_TYPE_AES;
2610 *key_size = 256;
2611 break;
2612 case MBEDTLS_CIPHER_AES_256_CCM:
2613 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2614 *key_type = PSA_KEY_TYPE_AES;
2615 *key_size = 256;
2616 break;
2617 case MBEDTLS_CIPHER_AES_256_GCM:
2618 *alg = PSA_ALG_GCM;
2619 *key_type = PSA_KEY_TYPE_AES;
2620 *key_size = 256;
2621 break;
2622 case MBEDTLS_CIPHER_ARIA_128_CBC:
2623 *alg = PSA_ALG_CBC_NO_PADDING;
2624 *key_type = PSA_KEY_TYPE_ARIA;
2625 *key_size = 128;
2626 break;
2627 case MBEDTLS_CIPHER_ARIA_128_CCM:
2628 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2629 *key_type = PSA_KEY_TYPE_ARIA;
2630 *key_size = 128;
2631 break;
2632 case MBEDTLS_CIPHER_ARIA_128_GCM:
2633 *alg = PSA_ALG_GCM;
2634 *key_type = PSA_KEY_TYPE_ARIA;
2635 *key_size = 128;
2636 break;
2637 case MBEDTLS_CIPHER_ARIA_192_CCM:
2638 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2639 *key_type = PSA_KEY_TYPE_ARIA;
2640 *key_size = 192;
2641 break;
2642 case MBEDTLS_CIPHER_ARIA_192_GCM:
2643 *alg = PSA_ALG_GCM;
2644 *key_type = PSA_KEY_TYPE_ARIA;
2645 *key_size = 192;
2646 break;
2647 case MBEDTLS_CIPHER_ARIA_256_CBC:
2648 *alg = PSA_ALG_CBC_NO_PADDING;
2649 *key_type = PSA_KEY_TYPE_ARIA;
2650 *key_size = 256;
2651 break;
2652 case MBEDTLS_CIPHER_ARIA_256_CCM:
2653 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2654 *key_type = PSA_KEY_TYPE_ARIA;
2655 *key_size = 256;
2656 break;
2657 case MBEDTLS_CIPHER_ARIA_256_GCM:
2658 *alg = PSA_ALG_GCM;
2659 *key_type = PSA_KEY_TYPE_ARIA;
2660 *key_size = 256;
2661 break;
2662 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2663 *alg = PSA_ALG_CBC_NO_PADDING;
2664 *key_type = PSA_KEY_TYPE_CAMELLIA;
2665 *key_size = 128;
2666 break;
2667 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2668 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2669 *key_type = PSA_KEY_TYPE_CAMELLIA;
2670 *key_size = 128;
2671 break;
2672 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2673 *alg = PSA_ALG_GCM;
2674 *key_type = PSA_KEY_TYPE_CAMELLIA;
2675 *key_size = 128;
2676 break;
2677 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2678 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2679 *key_type = PSA_KEY_TYPE_CAMELLIA;
2680 *key_size = 192;
2681 break;
2682 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2683 *alg = PSA_ALG_GCM;
2684 *key_type = PSA_KEY_TYPE_CAMELLIA;
2685 *key_size = 192;
2686 break;
2687 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2688 *alg = PSA_ALG_CBC_NO_PADDING;
2689 *key_type = PSA_KEY_TYPE_CAMELLIA;
2690 *key_size = 256;
2691 break;
2692 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2693 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2694 *key_type = PSA_KEY_TYPE_CAMELLIA;
2695 *key_size = 256;
2696 break;
2697 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2698 *alg = PSA_ALG_GCM;
2699 *key_type = PSA_KEY_TYPE_CAMELLIA;
2700 *key_size = 256;
2701 break;
2702 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2703 *alg = PSA_ALG_CHACHA20_POLY1305;
2704 *key_type = PSA_KEY_TYPE_CHACHA20;
2705 *key_size = 256;
2706 break;
2707 case MBEDTLS_CIPHER_NULL:
2708 *alg = MBEDTLS_SSL_NULL_CIPHER;
2709 *key_type = 0;
2710 *key_size = 0;
2711 break;
2712 default:
2713 return PSA_ERROR_NOT_SUPPORTED;
2714 }
2715
2716 return PSA_SUCCESS;
2717}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002718#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002719
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002720#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002721int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2722 const unsigned char *dhm_P, size_t P_len,
2723 const unsigned char *dhm_G, size_t G_len )
2724{
Janos Follath865b3eb2019-12-16 11:46:15 +00002725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002726
Glenn Strausscee11292021-12-20 01:43:17 -05002727 mbedtls_mpi_free( &conf->dhm_P );
2728 mbedtls_mpi_free( &conf->dhm_G );
2729
Hanno Beckera90658f2017-10-04 15:29:08 +01002730 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2731 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2732 {
2733 mbedtls_mpi_free( &conf->dhm_P );
2734 mbedtls_mpi_free( &conf->dhm_G );
2735 return( ret );
2736 }
2737
2738 return( 0 );
2739}
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002741int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002742{
Janos Follath865b3eb2019-12-16 11:46:15 +00002743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002744
Glenn Strausscee11292021-12-20 01:43:17 -05002745 mbedtls_mpi_free( &conf->dhm_P );
2746 mbedtls_mpi_free( &conf->dhm_G );
2747
Gilles Peskinee5702482021-06-11 21:59:08 +02002748 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2749 &conf->dhm_P ) ) != 0 ||
2750 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2751 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002752 {
2753 mbedtls_mpi_free( &conf->dhm_P );
2754 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002755 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002756 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002757
2758 return( 0 );
2759}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002760#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002761
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002762#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2763/*
2764 * Set the minimum length for Diffie-Hellman parameters
2765 */
2766void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2767 unsigned int bitlen )
2768{
2769 conf->dhm_min_bitlen = bitlen;
2770}
2771#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2772
Ronald Crone68ab4f2022-10-05 12:46:29 +02002773#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002774#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002775/*
2776 * Set allowed/preferred hashes for handshake signatures
2777 */
2778void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2779 const int *hashes )
2780{
2781 conf->sig_hashes = hashes;
2782}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002783#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002784
Jerry Yuf017ee42022-01-12 15:49:48 +08002785/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002786void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2787 const uint16_t* sig_algs )
2788{
Jerry Yuf017ee42022-01-12 15:49:48 +08002789#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2790 conf->sig_hashes = NULL;
2791#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2792 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002793}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002794#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002795
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002796#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002797#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002798/*
2799 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002800 *
2801 * mbedtls_ssl_setup() takes the provided list
2802 * and translates it to a list of IANA TLS group identifiers,
2803 * stored in ssl->handshake->group_list.
2804 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002805 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002806void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002807 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002808{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002809 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002810 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002811}
Brett Warrene0edc842021-08-17 09:53:13 +01002812#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002813#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002814
Brett Warrene0edc842021-08-17 09:53:13 +01002815/*
2816 * Set the allowed groups
2817 */
2818void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2819 const uint16_t *group_list )
2820{
2821#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2822 conf->curve_list = NULL;
2823#endif
2824 conf->group_list = group_list;
2825}
2826
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002827#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002829{
Hanno Becker947194e2017-04-07 13:25:49 +01002830 /* Initialize to suppress unnecessary compiler warning */
2831 size_t hostname_len = 0;
2832
2833 /* Check if new hostname is valid before
2834 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002835 if( hostname != NULL )
2836 {
2837 hostname_len = strlen( hostname );
2838
2839 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2841 }
2842
2843 /* Now it's clear that we will overwrite the old hostname,
2844 * so we can free it safely */
2845
2846 if( ssl->hostname != NULL )
2847 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002848 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002849 mbedtls_free( ssl->hostname );
2850 }
2851
2852 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002853
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002855 {
2856 ssl->hostname = NULL;
2857 }
2858 else
2859 {
2860 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002861 if( ssl->hostname == NULL )
2862 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002863
Hanno Becker947194e2017-04-07 13:25:49 +01002864 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002865
Hanno Becker947194e2017-04-07 13:25:49 +01002866 ssl->hostname[hostname_len] = '\0';
2867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
2869 return( 0 );
2870}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002871#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002873#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002874void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002876 const unsigned char *, size_t),
2877 void *p_sni )
2878{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002879 conf->f_sni = f_sni;
2880 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002885int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002886{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002887 size_t cur_len, tot_len;
2888 const char **p;
2889
2890 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002891 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2892 * MUST NOT be truncated."
2893 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002894 */
2895 tot_len = 0;
2896 for( p = protos; *p != NULL; p++ )
2897 {
2898 cur_len = strlen( *p );
2899 tot_len += cur_len;
2900
Ronald Cron8216dd32020-04-23 16:41:44 +02002901 if( ( cur_len == 0 ) ||
2902 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2903 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002905 }
2906
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002907 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002908
2909 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002910}
2911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002913{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002914 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002917
Johan Pascalb62bb512015-12-03 21:56:45 +01002918#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002919void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2920 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002921{
2922 conf->dtls_srtp_mki_support = support_mki_value;
2923}
2924
Ron Eldoref72faf2018-07-12 11:54:20 +03002925int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2926 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002927 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002928{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002929 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002930 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002932 }
Ron Eldor591f1622018-01-22 12:30:04 +02002933
2934 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002935 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002936 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002937 }
Ron Eldor591f1622018-01-22 12:30:04 +02002938
2939 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2940 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002941 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002942}
2943
Ron Eldoref72faf2018-07-12 11:54:20 +03002944int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002945 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002946{
Johan Pascal253d0262020-09-22 13:04:45 +02002947 const mbedtls_ssl_srtp_profile *p;
2948 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002949
Johan Pascal253d0262020-09-22 13:04:45 +02002950 /* check the profiles list: all entry must be valid,
2951 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002952 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2953 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2954 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002955 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002956 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002957 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002958 list_size++;
2959 }
2960 else
2961 {
2962 /* unsupported value, stop parsing and set the size to an error value */
2963 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002964 }
2965 }
2966
Johan Pascal5ef72d22020-10-28 17:05:47 +01002967 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002968 {
Johan Pascal253d0262020-09-22 13:04:45 +02002969 conf->dtls_srtp_profile_list = NULL;
2970 conf->dtls_srtp_profile_list_len = 0;
2971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2972 }
2973
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002974 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002975 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002976
2977 return( 0 );
2978}
2979
Johan Pascal5ef72d22020-10-28 17:05:47 +01002980void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2981 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002982{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002983 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2984 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002985 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002986 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002987 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002988 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002989 else
2990 {
2991 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002992 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2993 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002994 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002995}
Johan Pascalb62bb512015-12-03 21:56:45 +01002996#endif /* MBEDTLS_SSL_DTLS_SRTP */
2997
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302998#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002999void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00003000{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003001 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003002}
3003
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003004void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00003005{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003006 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003007}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303008#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003009
Janos Follath088ce432017-04-10 12:42:31 +01003010#if defined(MBEDTLS_SSL_SRV_C)
3011void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
3012 char cert_req_ca_list )
3013{
3014 conf->cert_req_ca_list = cert_req_ca_list;
3015}
3016#endif
3017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003019void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003020{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003021 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003022}
3023#endif
3024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003026void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003027{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003028 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003029}
3030#endif
3031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003033int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003036 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003039 }
3040
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003041 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003042
3043 return( 0 );
3044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003046
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003047void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003049 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003050}
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003053void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003055 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003056}
3057
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003058void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003060 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003061}
3062
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003063void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003064 const unsigned char period[8] )
3065{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003066 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003071#if defined(MBEDTLS_SSL_CLI_C)
3072void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003073{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003074 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003075}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003076#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003077
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003078#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003079
Jerry Yud0766ec2022-09-22 10:46:57 +08003080#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003081void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3082 uint16_t num_tickets )
3083{
Jerry Yud0766ec2022-09-22 10:46:57 +08003084 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003085}
3086#endif
3087
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003088void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3089 mbedtls_ssl_ticket_write_t *f_ticket_write,
3090 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3091 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003092{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003093 conf->f_ticket_write = f_ticket_write;
3094 conf->f_ticket_parse = f_ticket_parse;
3095 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003096}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003099
Hanno Becker7e6c1782021-06-08 09:24:55 +01003100void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3101 mbedtls_ssl_export_keys_t *f_export_keys,
3102 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003103{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003104 ssl->f_export_keys = f_export_keys;
3105 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003106}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003107
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003108#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003109void mbedtls_ssl_conf_async_private_cb(
3110 mbedtls_ssl_config *conf,
3111 mbedtls_ssl_async_sign_t *f_async_sign,
3112 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3113 mbedtls_ssl_async_resume_t *f_async_resume,
3114 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003115 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003116{
3117 conf->f_async_sign_start = f_async_sign;
3118 conf->f_async_decrypt_start = f_async_decrypt;
3119 conf->f_async_resume = f_async_resume;
3120 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003121 conf->p_async_config_data = async_config_data;
3122}
3123
Gilles Peskine8f97af72018-04-26 11:46:10 +02003124void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3125{
3126 return( conf->p_async_config_data );
3127}
3128
Gilles Peskine1febfef2018-04-30 11:54:39 +02003129void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003130{
3131 if( ssl->handshake == NULL )
3132 return( NULL );
3133 else
3134 return( ssl->handshake->user_async_ctx );
3135}
3136
Gilles Peskine1febfef2018-04-30 11:54:39 +02003137void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003138 void *ctx )
3139{
3140 if( ssl->handshake != NULL )
3141 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003142}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003143#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003144
Paul Bakker5121ce52009-01-03 21:22:43 +00003145/*
3146 * SSL get accessors
3147 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003148uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003149{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003150 if( ssl->session != NULL )
3151 return( ssl->session->verify_result );
3152
3153 if( ssl->session_negotiate != NULL )
3154 return( ssl->session_negotiate->verify_result );
3155
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003156 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003157}
3158
Glenn Strauss8f526902022-01-13 00:04:49 -05003159int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3160{
3161 if( ssl == NULL || ssl->session == NULL )
3162 return( 0 );
3163
3164 return( ssl->session->ciphersuite );
3165}
3166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003168{
Paul Bakker926c8e42013-03-06 10:23:34 +01003169 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003170 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003173}
3174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003179 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003180 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003181 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003182 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003183 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003184 default:
3185 return( "unknown (DTLS)" );
3186 }
3187 }
3188#endif
3189
Glenn Strauss60bfe602022-03-14 19:04:24 -04003190 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003191 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003192 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003194 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003195 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003196 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003197 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003198 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003199}
3200
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003202size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3203{
David Horstmann95d516f2021-05-04 18:36:56 +01003204 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003205 size_t read_mfl;
3206
3207 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3208 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3209 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3210 {
3211 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3212 }
3213
3214 /* Check if a smaller max length was negotiated */
3215 if( ssl->session_out != NULL )
3216 {
3217 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3218 if( read_mfl < max_len )
3219 {
3220 max_len = read_mfl;
3221 }
3222 }
3223
3224 // During a handshake, use the value being negotiated
3225 if( ssl->session_negotiate != NULL )
3226 {
3227 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3228 if( read_mfl < max_len )
3229 {
3230 max_len = read_mfl;
3231 }
3232 }
3233
3234 return( max_len );
3235}
3236
3237size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003238{
3239 size_t max_len;
3240
3241 /*
3242 * Assume mfl_code is correct since it was checked when set
3243 */
Angus Grattond8213d02016-05-25 20:56:48 +10003244 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003245
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003246 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003247 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003248 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003249 {
Angus Grattond8213d02016-05-25 20:56:48 +10003250 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003251 }
3252
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003253 /* During a handshake, use the value being negotiated */
3254 if( ssl->session_negotiate != NULL &&
3255 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3256 {
3257 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3258 }
3259
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003260 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003261}
3262#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3263
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003265size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003266{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003267 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3268 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3269 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3270 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3271 return ( 0 );
3272
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003273 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3274 return( ssl->mtu );
3275
3276 if( ssl->mtu == 0 )
3277 return( ssl->handshake->mtu );
3278
3279 return( ssl->mtu < ssl->handshake->mtu ?
3280 ssl->mtu : ssl->handshake->mtu );
3281}
3282#endif /* MBEDTLS_SSL_PROTO_DTLS */
3283
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003284int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3285{
3286 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3287
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003288#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3289 !defined(MBEDTLS_SSL_PROTO_DTLS)
3290 (void) ssl;
3291#endif
3292
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003293#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003294 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003295
3296 if( max_len > mfl )
3297 max_len = mfl;
3298#endif
3299
3300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003301 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003302 {
Hanno Becker89490712020-02-05 10:50:12 +00003303 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003304 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3305 const size_t overhead = (size_t) ret;
3306
3307 if( ret < 0 )
3308 return( ret );
3309
3310 if( mtu <= overhead )
3311 {
3312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3313 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3314 }
3315
3316 if( max_len > mtu - overhead )
3317 max_len = mtu - overhead;
3318 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003319#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003320
Hanno Becker0defedb2018-08-10 12:35:02 +01003321#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3322 !defined(MBEDTLS_SSL_PROTO_DTLS)
3323 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003324#endif
3325
3326 return( (int) max_len );
3327}
3328
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003329int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3330{
3331 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3332
3333#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3334 (void) ssl;
3335#endif
3336
3337#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3338 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3339
3340 if( max_len > mfl )
3341 max_len = mfl;
3342#endif
3343
3344 return( (int) max_len );
3345}
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#if defined(MBEDTLS_X509_CRT_PARSE_C)
3348const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003349{
3350 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003351 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003352
Hanno Beckere6824572019-02-07 13:18:46 +00003353#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003354 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003355#else
3356 return( NULL );
3357#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003362int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3363 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003364{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003365 int ret;
3366
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003367 if( ssl == NULL ||
3368 dst == NULL ||
3369 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003370 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003373 }
3374
Hanno Beckere810bbc2021-05-14 16:01:05 +01003375 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3376 * idempotent: Each session can only be exported once.
3377 *
3378 * (This is in preparation for TLS 1.3 support where we will
3379 * need the ability to export multiple sessions (aka tickets),
3380 * which will be achieved by calling mbedtls_ssl_get_session()
3381 * multiple times until it fails.)
3382 *
3383 * Check whether we have already exported the current session,
3384 * and fail if so.
3385 */
3386 if( ssl->session->exported == 1 )
3387 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3388
3389 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3390 if( ret != 0 )
3391 return( ret );
3392
3393 /* Remember that we've exported the session. */
3394 ssl->session->exported = 1;
3395 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003398
Paul Bakker5121ce52009-01-03 21:22:43 +00003399/*
Hanno Beckera835da52019-05-16 12:39:07 +01003400 * Define ticket header determining Mbed TLS version
3401 * and structure of the ticket.
3402 */
3403
Hanno Becker94ef3b32019-05-16 12:50:45 +01003404/*
Hanno Becker50b59662019-05-28 14:30:45 +01003405 * Define bitflag determining compile-time settings influencing
3406 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003407 */
3408
Hanno Becker50b59662019-05-28 14:30:45 +01003409#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003410#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003411#else
Hanno Becker3e088662019-05-29 11:10:18 +01003412#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003413#endif /* MBEDTLS_HAVE_TIME */
3414
3415#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003416#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003417#else
Hanno Becker3e088662019-05-29 11:10:18 +01003418#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003419#endif /* MBEDTLS_X509_CRT_PARSE_C */
3420
3421#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003422#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003423#else
Hanno Becker3e088662019-05-29 11:10:18 +01003424#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003425#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3426
3427#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003428#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003429#else
Hanno Becker3e088662019-05-29 11:10:18 +01003430#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003431#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3432
Hanno Becker94ef3b32019-05-16 12:50:45 +01003433#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003434#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003435#else
Hanno Becker3e088662019-05-29 11:10:18 +01003436#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003437#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3438
Hanno Becker94ef3b32019-05-16 12:50:45 +01003439#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3440#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3441#else
3442#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3443#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3444
Hanno Becker3e088662019-05-29 11:10:18 +01003445#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3446#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3447#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3448#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003449#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3450#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003451
Hanno Becker50b59662019-05-28 14:30:45 +01003452#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003453 ( (uint16_t) ( \
3454 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3455 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3456 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3457 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003458 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003459 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003460
Hanno Beckerf8787072019-05-16 12:41:07 +01003461static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003462 MBEDTLS_VERSION_MAJOR,
3463 MBEDTLS_VERSION_MINOR,
3464 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003465 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3466 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003467};
Hanno Beckera835da52019-05-16 12:39:07 +01003468
3469/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003470 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003471 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003472 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003473 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003474 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003475 * opaque mbedtls_version[3]; // library version: major, minor, patch
3476 * opaque session_format[2]; // library-version specific 16-bit field
3477 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003478 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003479 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003480 * Note: When updating the format, remember to keep
3481 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003482 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003483 * // In this version, `session_format` determines
3484 * // the setting of those compile-time
3485 * // configuration options which influence
3486 * // the structure of mbedtls_ssl_session.
3487 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003488 * uint8_t minor_ver; // Protocol minor version. Possible values:
3489 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003490 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003491 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003492 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003493 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003494 * serialized_session_tls12 data;
3495 *
3496 * };
3497 *
3498 * } serialized_session;
3499 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003500 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003501
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003502MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003503static int ssl_session_save( const mbedtls_ssl_session *session,
3504 unsigned char omit_header,
3505 unsigned char *buf,
3506 size_t buf_len,
3507 size_t *olen )
3508{
3509 unsigned char *p = buf;
3510 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003511 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003512#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3513 size_t out_len;
3514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3515#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003516 if( session == NULL )
3517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3518
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003519 if( !omit_header )
3520 {
3521 /*
3522 * Add Mbed TLS version identifier
3523 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003524 used += sizeof( ssl_serialized_session_header );
3525
3526 if( used <= buf_len )
3527 {
3528 memcpy( p, ssl_serialized_session_header,
3529 sizeof( ssl_serialized_session_header ) );
3530 p += sizeof( ssl_serialized_session_header );
3531 }
3532 }
3533
3534 /*
3535 * TLS version identifier
3536 */
3537 used += 1;
3538 if( used <= buf_len )
3539 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003540 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003541 }
3542
3543 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003544 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003545 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003546 {
3547#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003548 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003549 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003550 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003551#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3552
Jerry Yu251a12e2022-07-13 15:15:48 +08003553#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3554 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003555 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3556 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003557 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003558 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003559 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003560#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3561
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003562 default:
3563 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3564 }
3565
3566 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003567 if( used > buf_len )
3568 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003569
3570 return( 0 );
3571}
3572
3573/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003574 * Public wrapper for ssl_session_save()
3575 */
3576int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3577 unsigned char *buf,
3578 size_t buf_len,
3579 size_t *olen )
3580{
3581 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3582}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003583
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003584/*
3585 * Deserialize session, see mbedtls_ssl_session_save() for format.
3586 *
3587 * This internal version is wrapped by a public function that cleans up in
3588 * case of error, and has an extra option omit_header.
3589 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003590MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003591static int ssl_session_load( mbedtls_ssl_session *session,
3592 unsigned char omit_header,
3593 const unsigned char *buf,
3594 size_t len )
3595{
3596 const unsigned char *p = buf;
3597 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003598 size_t remaining_len;
3599
3600
3601 if( session == NULL )
3602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003603
3604 if( !omit_header )
3605 {
3606 /*
3607 * Check Mbed TLS version identifier
3608 */
3609
3610 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3612
3613 if( memcmp( p, ssl_serialized_session_header,
3614 sizeof( ssl_serialized_session_header ) ) != 0 )
3615 {
3616 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3617 }
3618 p += sizeof( ssl_serialized_session_header );
3619 }
3620
3621 /*
3622 * TLS version identifier
3623 */
3624 if( 1 > (size_t)( end - p ) )
3625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003626 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003627
3628 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003629 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003630 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003631 {
3632#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003633 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003634 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003635#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3636
Jerry Yu438ddd82022-07-07 06:55:50 +00003637#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3638 case MBEDTLS_SSL_VERSION_TLS1_3:
3639 return( ssl_tls13_session_load( session, p, remaining_len ) );
3640#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3641
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003642 default:
3643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3644 }
3645}
3646
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003647/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003648 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003649 */
3650int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3651 const unsigned char *buf,
3652 size_t len )
3653{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003654 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003655
3656 if( ret != 0 )
3657 mbedtls_ssl_session_free( session );
3658
3659 return( ret );
3660}
3661
3662/*
Paul Bakker1961b702013-01-25 14:49:24 +01003663 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003665MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003666static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3667{
3668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3669
Ronald Cron66dbf912022-02-02 15:33:46 +01003670 /*
3671 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003672 * that were written into the output buffer by the previous handshake step,
3673 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003674 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3675 * We proceed to the next handshake step only when all data from the
3676 * previous one have been sent to the peer, thus we make sure that this is
3677 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3678 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3679 * we have to wait before to go ahead.
3680 * In the case of TLS 1.3, handshake step handlers do not send data to the
3681 * peer. Data are only sent here and through
3682 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003683 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003684 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003685 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3686 return( ret );
3687
3688#if defined(MBEDTLS_SSL_PROTO_DTLS)
3689 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3690 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3691 {
3692 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3693 return( ret );
3694 }
3695#endif /* MBEDTLS_SSL_PROTO_DTLS */
3696
3697 return( ret );
3698}
3699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003701{
Hanno Becker41934dd2021-08-07 19:13:43 +01003702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Hanno Becker41934dd2021-08-07 19:13:43 +01003704 if( ssl == NULL ||
3705 ssl->conf == NULL ||
3706 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003707 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003708 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003709 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003710 }
3711
3712 ret = ssl_prepare_handshake_step( ssl );
3713 if( ret != 0 )
3714 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003715
Jerry Yue7047812021-09-13 19:26:39 +08003716 ret = mbedtls_ssl_handle_pending_alert( ssl );
3717 if( ret != 0 )
3718 goto cleanup;
3719
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003720 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3721 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3722 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003726 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3728 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003729
Ronald Cron9f0fba32022-02-10 16:45:15 +01003730 switch( ssl->state )
3731 {
3732 case MBEDTLS_SSL_HELLO_REQUEST:
3733 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003734 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003735 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003736
Ronald Cron9f0fba32022-02-10 16:45:15 +01003737 case MBEDTLS_SSL_CLIENT_HELLO:
3738 ret = mbedtls_ssl_write_client_hello( ssl );
3739 break;
3740
3741 default:
3742#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003743 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003744 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3745 else
3746 ret = mbedtls_ssl_handshake_client_step( ssl );
3747#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3748 ret = mbedtls_ssl_handshake_client_step( ssl );
3749#else
3750 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3751#endif
3752 }
Jerry Yub9930e72021-08-06 17:11:51 +08003753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003756 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003757 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003758#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003759 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003760 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003761#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003762
3763#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3764 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3765 ret = mbedtls_ssl_handshake_server_step( ssl );
3766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003768#endif
3769
Jerry Yue7047812021-09-13 19:26:39 +08003770 if( ret != 0 )
3771 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003772 /* handshake_step return error. And it is same
3773 * with alert_reason.
3774 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003775 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003776 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003777 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003778 goto cleanup;
3779 }
3780 }
3781
3782cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003783 return( ret );
3784}
3785
3786/*
3787 * Perform the SSL handshake
3788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003790{
3791 int ret = 0;
3792
Hanno Beckera817ea42020-10-20 15:20:23 +01003793 /* Sanity checks */
3794
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003795 if( ssl == NULL || ssl->conf == NULL )
3796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3797
Hanno Beckera817ea42020-10-20 15:20:23 +01003798#if defined(MBEDTLS_SSL_PROTO_DTLS)
3799 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3800 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3801 {
3802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3803 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3805 }
3806#endif /* MBEDTLS_SSL_PROTO_DTLS */
3807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003809
Hanno Beckera817ea42020-10-20 15:20:23 +01003810 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003811 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003814
3815 if( ret != 0 )
3816 break;
3817 }
3818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003820
3821 return( ret );
3822}
3823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824#if defined(MBEDTLS_SSL_RENEGOTIATION)
3825#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003826/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003827 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003828 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003829MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003831{
Janos Follath865b3eb2019-12-16 11:46:15 +00003832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003835
3836 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3838 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003839
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003840 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003841 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003843 return( ret );
3844 }
3845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003847
3848 return( 0 );
3849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003851
3852/*
3853 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 * - any side: calling mbedtls_ssl_renegotiate(),
3855 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3856 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003857 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003858 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003860 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003861int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003862{
Janos Follath865b3eb2019-12-16 11:46:15 +00003863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003866
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003867 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3868 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003869
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003870 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3871 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003873 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003875 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003876 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003877 ssl->handshake->out_msg_seq = 1;
3878 else
3879 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003880 }
3881#endif
3882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3884 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003889 return( ret );
3890 }
3891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003893
3894 return( 0 );
3895}
3896
3897/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003898 * Renegotiate current connection on client,
3899 * or request renegotiation on server
3900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003904
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003905 if( ssl == NULL || ssl->conf == NULL )
3906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003909 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003910 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003911 {
Jerry Yu6848a612022-10-27 13:03:26 +08003912 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003916
3917 /* Did we already try/start sending HelloRequest? */
3918 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003920
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003921 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003922 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003926 /*
3927 * On client, either start the renegotiation process or,
3928 * if already in progress, continue the handshake
3929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003931 {
Jerry Yu6848a612022-10-27 13:03:26 +08003932 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003934
Hanno Becker40cdaa12020-02-05 10:48:27 +00003935 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003936 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003938 return( ret );
3939 }
3940 }
3941 else
3942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003946 return( ret );
3947 }
3948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003950
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003951 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003954
Gilles Peskine9b562d52018-04-25 20:32:43 +02003955void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003956{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003957 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3958
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003959 if( handshake == NULL )
3960 return;
3961
Brett Warrene0edc842021-08-17 09:53:13 +01003962#if defined(MBEDTLS_ECP_C)
3963#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3964 if ( ssl->handshake->group_list_heap_allocated )
3965 mbedtls_free( (void*) handshake->group_list );
3966 handshake->group_list = NULL;
3967#endif /* MBEDTLS_DEPRECATED_REMOVED */
3968#endif /* MBEDTLS_ECP_C */
3969
Ronald Crone68ab4f2022-10-05 12:46:29 +02003970#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003971#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3972 if ( ssl->handshake->sig_algs_heap_allocated )
3973 mbedtls_free( (void*) handshake->sig_algs );
3974 handshake->sig_algs = NULL;
3975#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003976#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3977 if( ssl->handshake->certificate_request_context )
3978 {
3979 mbedtls_free( (void*) handshake->certificate_request_context );
3980 }
3981#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003982#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003983
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003984#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3985 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3986 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003987 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003988 handshake->async_in_progress = 0;
3989 }
3990#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3991
Andrzej Kurek25f27152022-08-17 16:09:31 -04003992#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003993#if defined(MBEDTLS_USE_PSA_CRYPTO)
3994 psa_hash_abort( &handshake->fin_sha256_psa );
3995#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003996 mbedtls_sha256_free( &handshake->fin_sha256 );
3997#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003998#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003999#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004000#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05004001 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05004002#else
Andrzej Kureka242e832022-08-11 10:03:14 -04004003 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004004#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004005#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#if defined(MBEDTLS_DHM_C)
4008 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00004009#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004010#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02004012#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004013
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004014#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004015#if defined(MBEDTLS_USE_PSA_CRYPTO)
4016 psa_pake_abort( &handshake->psa_pake_ctx );
Valerio Settieb3f7882022-12-08 18:42:58 +01004017 /*
4018 * Opaque keys are not stored in the handshake's data and it's the user
4019 * responsibility to destroy them. Clear ones, instead, are created by
4020 * the TLS library and should be destroyed at the same level
4021 */
4022 if( ! mbedtls_svc_key_id_is_null( handshake->psa_pake_password ) )
4023 {
4024 psa_destroy_key( handshake->psa_pake_password );
4025 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004026 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4027#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004028 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004029#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004030#if defined(MBEDTLS_SSL_CLI_C)
4031 mbedtls_free( handshake->ecjpake_cache );
4032 handshake->ecjpake_cache = NULL;
4033 handshake->ecjpake_cache_len = 0;
4034#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004035#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004036
Janos Follath4ae5c292016-02-10 11:27:43 +00004037#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4038 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004039 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004041#endif
4042
Ronald Cron73fe8df2022-10-05 14:31:43 +02004043#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004044#if defined(MBEDTLS_USE_PSA_CRYPTO)
4045 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4046 {
4047 /* The maintenance of the external PSK key slot is the
4048 * user's responsibility. */
4049 if( ssl->handshake->psk_opaque_is_internal )
4050 {
4051 psa_destroy_key( ssl->handshake->psk_opaque );
4052 ssl->handshake->psk_opaque_is_internal = 0;
4053 }
4054 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4055 }
Neil Armstronge952a302022-05-03 10:22:14 +02004056#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004057 if( handshake->psk != NULL )
4058 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004059 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004060 mbedtls_free( handshake->psk );
4061 }
Neil Armstronge952a302022-05-03 10:22:14 +02004062#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004063#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4066 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004067 /*
4068 * Free only the linked list wrapper, not the keys themselves
4069 * since the belong to the SNI callback
4070 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004071 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004073
Gilles Peskineeccd8882020-03-10 12:19:08 +01004074#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004075 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004076 if( handshake->ecrs_peer_cert != NULL )
4077 {
4078 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4079 mbedtls_free( handshake->ecrs_peer_cert );
4080 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004081#endif
4082
Hanno Becker75173122019-02-06 16:18:31 +00004083#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4084 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4085 mbedtls_pk_free( &handshake->peer_pubkey );
4086#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4087
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004088#if defined(MBEDTLS_SSL_CLI_C) && \
4089 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4090 mbedtls_free( handshake->cookie );
4091#endif /* MBEDTLS_SSL_CLI_C &&
4092 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004093
4094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004095 mbedtls_ssl_flight_free( handshake->flight );
4096 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004097#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004098
Ronald Cronf12b81d2022-03-15 10:42:41 +01004099#if defined(MBEDTLS_ECDH_C) && \
4100 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004101 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004102 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004103#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4104
Ronald Cron6f135e12021-12-08 16:57:54 +01004105#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004106 mbedtls_ssl_transform_free( handshake->transform_handshake );
4107 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004108 mbedtls_free( handshake->transform_earlydata );
4109 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01004110#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004111
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004112
4113#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4114 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4115 * processes datagrams and the fact that a datagram is allowed to have
4116 * several records in it, it is possible that the I/O buffers are not
4117 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004118 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4119 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004120#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004121
Jerry Yuba9c7272021-10-30 11:54:10 +08004122 /* mbedtls_platform_zeroize MUST be last one in this function */
4123 mbedtls_platform_zeroize( handshake,
4124 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004125}
4126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004128{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004129 if( session == NULL )
4130 return;
4131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004133 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004134#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004135
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004136#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004137#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4138 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004139 mbedtls_free( session->hostname );
4140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004142#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004143
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004144 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004145}
4146
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004147#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004148
4149#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4150#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4151#else
4152#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4154
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004155#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004156
4157#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4158#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4159#else
4160#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4161#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4162
4163#if defined(MBEDTLS_SSL_ALPN)
4164#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4165#else
4166#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4167#endif /* MBEDTLS_SSL_ALPN */
4168
4169#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4170#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4171#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4172#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4173
4174#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4175 ( (uint32_t) ( \
4176 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4177 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4178 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4179 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4180 0u ) )
4181
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004182static unsigned char ssl_serialized_context_header[] = {
4183 MBEDTLS_VERSION_MAJOR,
4184 MBEDTLS_VERSION_MINOR,
4185 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004186 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4187 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4188 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4189 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4190 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004191};
4192
Paul Bakker5121ce52009-01-03 21:22:43 +00004193/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004194 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004195 *
4196 * The format of the serialized data is:
4197 * (in the presentation language of TLS, RFC 8446 section 3)
4198 *
4199 * // header
4200 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004201 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004202 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004203 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004204 * Note: When updating the format, remember to keep these
4205 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004206 *
4207 * // session sub-structure
4208 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4209 * // transform sub-structure
4210 * uint8 random[64]; // ServerHello.random+ClientHello.random
4211 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4212 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4213 * // fields from ssl_context
4214 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4215 * uint64 in_window_top; // DTLS: last validated record seq_num
4216 * uint64 in_window; // DTLS: bitmask for replay protection
4217 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4218 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4219 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4220 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4221 *
4222 * Note that many fields of the ssl_context or sub-structures are not
4223 * serialized, as they fall in one of the following categories:
4224 *
4225 * 1. forced value (eg in_left must be 0)
4226 * 2. pointer to dynamically-allocated memory (eg session, transform)
4227 * 3. value can be re-derived from other data (eg session keys from MS)
4228 * 4. value was temporary (eg content of input buffer)
4229 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004230 */
4231int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4232 unsigned char *buf,
4233 size_t buf_len,
4234 size_t *olen )
4235{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004236 unsigned char *p = buf;
4237 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004238 size_t session_len;
4239 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004240
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004241 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004242 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4243 * this function's documentation.
4244 *
4245 * These are due to assumptions/limitations in the implementation. Some of
4246 * them are likely to stay (no handshake in progress) some might go away
4247 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004248 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004249 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004250 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004251 {
4252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004254 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004255 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004256 {
4257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004259 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004260 /* Double-check that sub-structures are indeed ready */
4261 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004262 {
4263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004265 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004266 /* There must be no pending incoming or outgoing data */
4267 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004268 {
4269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004271 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004272 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004273 {
4274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004276 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004277 /* Protocol must be DLTS, not TLS */
4278 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004279 {
4280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004282 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004283 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004284 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004285 {
4286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004288 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004289 /* We must be using an AEAD ciphersuite */
4290 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004291 {
4292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004294 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004295 /* Renegotiation must not be enabled */
4296#if defined(MBEDTLS_SSL_RENEGOTIATION)
4297 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004298 {
4299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004301 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004302#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004303
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004304 /*
4305 * Version and format identifier
4306 */
4307 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004308
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004309 if( used <= buf_len )
4310 {
4311 memcpy( p, ssl_serialized_context_header,
4312 sizeof( ssl_serialized_context_header ) );
4313 p += sizeof( ssl_serialized_context_header );
4314 }
4315
4316 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004317 * Session (length + data)
4318 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004319 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004320 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4321 return( ret );
4322
4323 used += 4 + session_len;
4324 if( used <= buf_len )
4325 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004326 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4327 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004328
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004329 ret = ssl_session_save( ssl->session, 1,
4330 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004331 if( ret != 0 )
4332 return( ret );
4333
4334 p += session_len;
4335 }
4336
4337 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004338 * Transform
4339 */
4340 used += sizeof( ssl->transform->randbytes );
4341 if( used <= buf_len )
4342 {
4343 memcpy( p, ssl->transform->randbytes,
4344 sizeof( ssl->transform->randbytes ) );
4345 p += sizeof( ssl->transform->randbytes );
4346 }
4347
4348#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4349 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4350 if( used <= buf_len )
4351 {
4352 *p++ = ssl->transform->in_cid_len;
4353 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4354 p += ssl->transform->in_cid_len;
4355
4356 *p++ = ssl->transform->out_cid_len;
4357 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4358 p += ssl->transform->out_cid_len;
4359 }
4360#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4361
4362 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004363 * Saved fields from top-level ssl_context structure
4364 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004365 used += 4;
4366 if( used <= buf_len )
4367 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004368 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4369 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004370 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004371
4372#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4373 used += 16;
4374 if( used <= buf_len )
4375 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004376 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4377 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004378
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004379 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4380 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004381 }
4382#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4383
4384#if defined(MBEDTLS_SSL_PROTO_DTLS)
4385 used += 1;
4386 if( used <= buf_len )
4387 {
4388 *p++ = ssl->disable_datagram_packing;
4389 }
4390#endif /* MBEDTLS_SSL_PROTO_DTLS */
4391
Jerry Yuae0b2e22021-10-08 15:21:19 +08004392 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004393 if( used <= buf_len )
4394 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004395 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4396 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004397 }
4398
4399#if defined(MBEDTLS_SSL_PROTO_DTLS)
4400 used += 2;
4401 if( used <= buf_len )
4402 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004403 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4404 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004405 }
4406#endif /* MBEDTLS_SSL_PROTO_DTLS */
4407
4408#if defined(MBEDTLS_SSL_ALPN)
4409 {
4410 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004411 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004412 : 0;
4413
4414 used += 1 + alpn_len;
4415 if( used <= buf_len )
4416 {
4417 *p++ = alpn_len;
4418
4419 if( ssl->alpn_chosen != NULL )
4420 {
4421 memcpy( p, ssl->alpn_chosen, alpn_len );
4422 p += alpn_len;
4423 }
4424 }
4425 }
4426#endif /* MBEDTLS_SSL_ALPN */
4427
4428 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004429 * Done
4430 */
4431 *olen = used;
4432
4433 if( used > buf_len )
4434 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004435
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004436 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4437
Hanno Becker43aefe22020-02-05 10:44:56 +00004438 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004439}
4440
4441/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004442 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004443 *
4444 * This internal version is wrapped by a public function that cleans up in
4445 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004446 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004447MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004448static int ssl_context_load( mbedtls_ssl_context *ssl,
4449 const unsigned char *buf,
4450 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004451{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004452 const unsigned char *p = buf;
4453 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004454 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004456#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004457 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004458#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004459
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004460 /*
4461 * The context should have been freshly setup or reset.
4462 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004463 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004464 * renegotiating, or if the user mistakenly loaded a session first.)
4465 */
4466 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4467 ssl->session != NULL )
4468 {
4469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4470 }
4471
4472 /*
4473 * We can't check that the config matches the initial one, but we can at
4474 * least check it matches the requirements for serializing.
4475 */
4476 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004477 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4478 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004479#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004480 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004481#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004482 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004483 {
4484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4485 }
4486
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004487 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4488
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004489 /*
4490 * Check version identifier
4491 */
4492 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4494
4495 if( memcmp( p, ssl_serialized_context_header,
4496 sizeof( ssl_serialized_context_header ) ) != 0 )
4497 {
4498 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4499 }
4500 p += sizeof( ssl_serialized_context_header );
4501
4502 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004503 * Session
4504 */
4505 if( (size_t)( end - p ) < 4 )
4506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4507
4508 session_len = ( (size_t) p[0] << 24 ) |
4509 ( (size_t) p[1] << 16 ) |
4510 ( (size_t) p[2] << 8 ) |
4511 ( (size_t) p[3] );
4512 p += 4;
4513
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004514 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004515 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004516 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004517 ssl->session_in = ssl->session;
4518 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004519 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004520
4521 if( (size_t)( end - p ) < session_len )
4522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4523
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004524 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004525 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004526 {
4527 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004528 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004529 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004530
4531 p += session_len;
4532
4533 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004534 * Transform
4535 */
4536
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004537 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004538 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004539 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004540 ssl->transform_in = ssl->transform;
4541 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004542 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004543
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004544#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004545 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4546 if( prf_func == NULL )
4547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4548
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004549 /* Read random bytes and populate structure */
4550 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004552
Hanno Beckerbd257552021-03-22 06:59:27 +00004553 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004554 ssl->session->ciphersuite,
4555 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004556#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004557 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004558#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004559 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004560 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004561 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004562 ssl->conf->endpoint,
4563 ssl );
4564 if( ret != 0 )
4565 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004566#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004567 p += sizeof( ssl->transform->randbytes );
4568
4569#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4570 /* Read connection IDs and store them */
4571 if( (size_t)( end - p ) < 1 )
4572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4573
4574 ssl->transform->in_cid_len = *p++;
4575
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004576 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4578
4579 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4580 p += ssl->transform->in_cid_len;
4581
4582 ssl->transform->out_cid_len = *p++;
4583
4584 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4586
4587 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4588 p += ssl->transform->out_cid_len;
4589#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4590
4591 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004592 * Saved fields from top-level ssl_context structure
4593 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004594 if( (size_t)( end - p ) < 4 )
4595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4596
4597 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4598 ( (uint32_t) p[1] << 16 ) |
4599 ( (uint32_t) p[2] << 8 ) |
4600 ( (uint32_t) p[3] );
4601 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004602
4603#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4604 if( (size_t)( end - p ) < 16 )
4605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4606
4607 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4608 ( (uint64_t) p[1] << 48 ) |
4609 ( (uint64_t) p[2] << 40 ) |
4610 ( (uint64_t) p[3] << 32 ) |
4611 ( (uint64_t) p[4] << 24 ) |
4612 ( (uint64_t) p[5] << 16 ) |
4613 ( (uint64_t) p[6] << 8 ) |
4614 ( (uint64_t) p[7] );
4615 p += 8;
4616
4617 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4618 ( (uint64_t) p[1] << 48 ) |
4619 ( (uint64_t) p[2] << 40 ) |
4620 ( (uint64_t) p[3] << 32 ) |
4621 ( (uint64_t) p[4] << 24 ) |
4622 ( (uint64_t) p[5] << 16 ) |
4623 ( (uint64_t) p[6] << 8 ) |
4624 ( (uint64_t) p[7] );
4625 p += 8;
4626#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4627
4628#if defined(MBEDTLS_SSL_PROTO_DTLS)
4629 if( (size_t)( end - p ) < 1 )
4630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4631
4632 ssl->disable_datagram_packing = *p++;
4633#endif /* MBEDTLS_SSL_PROTO_DTLS */
4634
Jerry Yud9a94fe2021-09-28 18:58:59 +08004635 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004637 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4638 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004639
4640#if defined(MBEDTLS_SSL_PROTO_DTLS)
4641 if( (size_t)( end - p ) < 2 )
4642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4643
4644 ssl->mtu = ( p[0] << 8 ) | p[1];
4645 p += 2;
4646#endif /* MBEDTLS_SSL_PROTO_DTLS */
4647
4648#if defined(MBEDTLS_SSL_ALPN)
4649 {
4650 uint8_t alpn_len;
4651 const char **cur;
4652
4653 if( (size_t)( end - p ) < 1 )
4654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4655
4656 alpn_len = *p++;
4657
4658 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4659 {
4660 /* alpn_chosen should point to an item in the configured list */
4661 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4662 {
4663 if( strlen( *cur ) == alpn_len &&
4664 memcmp( p, cur, alpn_len ) == 0 )
4665 {
4666 ssl->alpn_chosen = *cur;
4667 break;
4668 }
4669 }
4670 }
4671
4672 /* can only happen on conf mismatch */
4673 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4675
4676 p += alpn_len;
4677 }
4678#endif /* MBEDTLS_SSL_ALPN */
4679
4680 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004681 * Forced fields from top-level ssl_context structure
4682 *
4683 * Most of them already set to the correct value by mbedtls_ssl_init() and
4684 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4685 */
4686 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004687 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004688
Hanno Becker361b10d2019-08-30 10:42:49 +01004689 /* Adjust pointers for header fields of outgoing records to
4690 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004691 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004692
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004693#if defined(MBEDTLS_SSL_PROTO_DTLS)
4694 ssl->in_epoch = 1;
4695#endif
4696
4697 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4698 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004699 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4700 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004701 if( ssl->handshake != NULL )
4702 {
4703 mbedtls_ssl_handshake_free( ssl );
4704 mbedtls_free( ssl->handshake );
4705 ssl->handshake = NULL;
4706 }
4707
4708 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004709 * Done - should have consumed entire buffer
4710 */
4711 if( p != end )
4712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004713
4714 return( 0 );
4715}
4716
4717/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004718 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004719 */
4720int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4721 const unsigned char *buf,
4722 size_t len )
4723{
4724 int ret = ssl_context_load( context, buf, len );
4725
4726 if( ret != 0 )
4727 mbedtls_ssl_free( context );
4728
4729 return( ret );
4730}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004731#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004732
4733/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 * Free an SSL context
4735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004737{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004738 if( ssl == NULL )
4739 return;
4740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004742
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004743 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004744 {
sander-visserb8aa2072020-05-06 22:05:13 +02004745#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4746 size_t out_buf_len = ssl->out_buf_len;
4747#else
4748 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4749#endif
4750
Darryl Greenb33cc762019-11-28 14:29:44 +00004751 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004753 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 }
4755
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004756 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004757 {
sander-visserb8aa2072020-05-06 22:05:13 +02004758#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4759 size_t in_buf_len = ssl->in_buf_len;
4760#else
4761 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4762#endif
4763
Darryl Greenb33cc762019-11-28 14:29:44 +00004764 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004766 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 }
4768
Paul Bakker48916f92012-09-16 19:57:18 +00004769 if( ssl->transform )
4770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 mbedtls_ssl_transform_free( ssl->transform );
4772 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004773 }
4774
4775 if( ssl->handshake )
4776 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004777 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4779 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 mbedtls_free( ssl->handshake );
4782 mbedtls_free( ssl->transform_negotiate );
4783 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004784 }
4785
Ronald Cron6f135e12021-12-08 16:57:54 +01004786#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004787 mbedtls_ssl_transform_free( ssl->transform_application );
4788 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004789#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004790
Paul Bakkerc0463502013-02-14 11:19:38 +01004791 if( ssl->session )
4792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 mbedtls_ssl_session_free( ssl->session );
4794 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004795 }
4796
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004797#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004798 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004799 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004800 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004803#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004804
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004805#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004807#endif
4808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004810
Paul Bakker86f04f42013-02-14 11:20:09 +01004811 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004812 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004813}
4814
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004815/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004816 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004817 */
4818void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4819{
4820 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4821}
4822
Gilles Peskineae270bf2021-06-02 00:05:29 +02004823/* The selection should be the same as mbedtls_x509_crt_profile_default in
4824 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004825 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004826 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004827 * about this list.
4828 */
Brett Warrene0edc842021-08-17 09:53:13 +01004829static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004830#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004831 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004832#endif
4833#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004834 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004835#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004836#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004837 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004838#endif
4839#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004840 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004841#endif
4842#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004843 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004844#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004845#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004846 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004847#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004848#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004849 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004850#endif
4851#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004852 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004853#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004854 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004855};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004856
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004857static int ssl_preset_suiteb_ciphersuites[] = {
4858 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4859 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4860 0
4861};
4862
Ronald Crone68ab4f2022-10-05 12:46:29 +02004863#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004864
Jerry Yu909df7b2022-01-22 11:56:27 +08004865/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004866 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004867 * rules SHOULD be upheld.
4868 * - No duplicate entries.
4869 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004870 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004871 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004872 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004873static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004874
Andrzej Kurek25f27152022-08-17 16:09:31 -04004875#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 +08004876 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4877 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004878#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004879 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004880
Andrzej Kurek25f27152022-08-17 16:09:31 -04004881#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 +08004882 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4883 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004884#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004885 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4886
Andrzej Kurek25f27152022-08-17 16:09:31 -04004887#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 +08004888 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4889 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004890#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004891 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004892
Andrzej Kurek25f27152022-08-17 16:09:31 -04004893#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 +08004894 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004895#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 +08004896
Andrzej Kurek25f27152022-08-17 16:09:31 -04004897#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 +08004898 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004899#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 +08004900
Andrzej Kurek25f27152022-08-17 16:09:31 -04004901#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 +08004902 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004903#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 +08004904
Andrzej Kurek25f27152022-08-17 16:09:31 -04004905#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 +08004906 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4907#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4908
Andrzej Kurek25f27152022-08-17 16:09:31 -04004909#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 +08004910 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4911#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4912
Andrzej Kurek25f27152022-08-17 16:09:31 -04004913#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 +08004914 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4915#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4916
Gabor Mezei15b95a62022-05-09 16:37:58 +02004917 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004918};
4919
4920/* NOTICE: see above */
4921#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4922static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004923#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004924#if defined(MBEDTLS_ECDSA_C)
4925 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004926#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004927#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4928 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4929#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004930#if defined(MBEDTLS_RSA_C)
4931 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4932#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004933#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004934#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004935#if defined(MBEDTLS_ECDSA_C)
4936 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004937#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004938#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4939 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4940#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004941#if defined(MBEDTLS_RSA_C)
4942 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4943#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004944#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004945#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004946#if defined(MBEDTLS_ECDSA_C)
4947 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004948#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004949#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4950 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4951#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004952#if defined(MBEDTLS_RSA_C)
4953 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4954#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004955#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004956 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004957};
4958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4959/* NOTICE: see above */
4960static uint16_t ssl_preset_suiteb_sig_algs[] = {
4961
Andrzej Kurek25f27152022-08-17 16:09:31 -04004962#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 +08004963 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4964 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004965#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004966 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4967
Andrzej Kurek25f27152022-08-17 16:09:31 -04004968#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 +08004969 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4970 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004971#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004972 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004973
Andrzej Kurek25f27152022-08-17 16:09:31 -04004974#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 +08004975 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004976#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 +08004977
Andrzej Kurek25f27152022-08-17 16:09:31 -04004978#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 +08004979 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004980#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004981
Gabor Mezei15b95a62022-05-09 16:37:58 +02004982 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004983};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004984
Jerry Yu909df7b2022-01-22 11:56:27 +08004985/* NOTICE: see above */
4986#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4987static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004988#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004989#if defined(MBEDTLS_ECDSA_C)
4990 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004991#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004992#if defined(MBEDTLS_RSA_C)
4993 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4994#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004995#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004996#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004997#if defined(MBEDTLS_ECDSA_C)
4998 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004999#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005000#if defined(MBEDTLS_RSA_C)
5001 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
5002#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005003#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005004 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005005};
Jerry Yu909df7b2022-01-22 11:56:27 +08005006#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5007
Ronald Crone68ab4f2022-10-05 12:46:29 +02005008#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005009
Brett Warrene0edc842021-08-17 09:53:13 +01005010static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005011#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005012 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005013#endif
5014#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005015 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005016#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005017 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005018};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005019
Ronald Crone68ab4f2022-10-05 12:46:29 +02005020#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005021/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005022 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005023MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005024static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005025{
5026 size_t i, j;
5027 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005028
Gabor Mezei15b95a62022-05-09 16:37:58 +02005029 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005030 {
Jerry Yuf377d642022-01-25 10:43:59 +08005031 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005032 {
Jerry Yuf377d642022-01-25 10:43:59 +08005033 if( sig_algs[i] != sig_algs[j] )
5034 continue;
5035 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5036 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5037 sig_algs[i], j, i );
5038 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005039 }
5040 }
5041 return( ret );
5042}
5043
Ronald Crone68ab4f2022-10-05 12:46:29 +02005044#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005045
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005046/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005047 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005048 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005049int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005050 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005051{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005052#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005054#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005055
Ronald Crone68ab4f2022-10-05 12:46:29 +02005056#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005057 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005058 {
5059 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5060 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5061 }
5062
Jerry Yuf377d642022-01-25 10:43:59 +08005063 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005064 {
5065 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5066 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5067 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005068
5069#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005070 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005071 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005072 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005073 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5074 }
5075
Jerry Yuf377d642022-01-25 10:43:59 +08005076 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005077 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005078 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005079 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5080 }
5081#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005082#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005083
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005084 /* Use the functions here so that they are covered in tests,
5085 * but otherwise access member directly for efficiency */
5086 mbedtls_ssl_conf_endpoint( conf, endpoint );
5087 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005088
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005089 /*
5090 * Things that are common to all presets
5091 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005092#if defined(MBEDTLS_SSL_CLI_C)
5093 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5094 {
5095 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5096#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5097 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5098#endif
5099 }
5100#endif
5101
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005102#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5103 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5104#endif
5105
5106#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5107 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5108#endif
5109
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005110#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005111 conf->f_cookie_write = ssl_cookie_write_dummy;
5112 conf->f_cookie_check = ssl_cookie_check_dummy;
5113#endif
5114
5115#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5116 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5117#endif
5118
Janos Follath088ce432017-04-10 12:42:31 +01005119#if defined(MBEDTLS_SSL_SRV_C)
5120 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005121 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005122#endif
5123
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005124#if defined(MBEDTLS_SSL_PROTO_DTLS)
5125 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5126 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5127#endif
5128
5129#if defined(MBEDTLS_SSL_RENEGOTIATION)
5130 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005131 memset( conf->renego_period, 0x00, 2 );
5132 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005133#endif
5134
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005135#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005136 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5137 {
5138 const unsigned char dhm_p[] =
5139 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5140 const unsigned char dhm_g[] =
5141 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005142
Hanno Beckere2defad2021-07-24 05:59:17 +01005143 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5144 dhm_p, sizeof( dhm_p ),
5145 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5146 {
5147 return( ret );
5148 }
5149 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005150#endif
5151
Ronald Cron6f135e12021-12-08 16:57:54 +01005152#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005153#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005154 mbedtls_ssl_conf_new_session_tickets(
5155 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5156#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005157 /*
5158 * Allow all TLS 1.3 key exchange modes by default.
5159 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005160 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005161#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005162
XiaokangQian4d3a6042022-04-21 13:46:17 +00005163 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5164 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005165#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005166 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005167 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005168#else
XiaokangQian060d8672022-04-21 09:24:56 +00005169 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005170#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005171 }
5172 else
5173 {
5174#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5175 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5176 {
5177 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5178 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5179 }
5180 else
5181 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5182 {
5183 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5184 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5185 }
5186#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5187 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5188 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5189#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5190 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5191 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5192#else
5193 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5194#endif
5195 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005196
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005197 /*
5198 * Preset-specific defaults
5199 */
5200 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005201 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005202 /*
5203 * NSA Suite B
5204 */
5205 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005206
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005207 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005208
5209#if defined(MBEDTLS_X509_CRT_PARSE_C)
5210 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005211#endif
5212
Ronald Crone68ab4f2022-10-05 12:46:29 +02005213#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005214#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5215 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5216 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5217 else
5218#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5219 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005220#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005221
Brett Warrene0edc842021-08-17 09:53:13 +01005222#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5223 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005224#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005225 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005226 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005227
5228 /*
5229 * Default
5230 */
5231 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005232
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005233 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005234
5235#if defined(MBEDTLS_X509_CRT_PARSE_C)
5236 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5237#endif
5238
Ronald Crone68ab4f2022-10-05 12:46:29 +02005239#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005240#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5241 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5242 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5243 else
5244#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5245 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005246#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005247
Brett Warrene0edc842021-08-17 09:53:13 +01005248#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5249 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005250#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005251 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005252
5253#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5254 conf->dhm_min_bitlen = 1024;
5255#endif
5256 }
5257
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005258 return( 0 );
5259}
5260
5261/*
5262 * Free mbedtls_ssl_config
5263 */
5264void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5265{
5266#if defined(MBEDTLS_DHM_C)
5267 mbedtls_mpi_free( &conf->dhm_P );
5268 mbedtls_mpi_free( &conf->dhm_G );
5269#endif
5270
Ronald Cron73fe8df2022-10-05 14:31:43 +02005271#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005272#if defined(MBEDTLS_USE_PSA_CRYPTO)
5273 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5274 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005275 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5276 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005277#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005278 if( conf->psk != NULL )
5279 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005280 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005281 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005282 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005283 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005284 }
5285
5286 if( conf->psk_identity != NULL )
5287 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005288 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005289 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005290 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005291 conf->psk_identity_len = 0;
5292 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005293#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005294
5295#if defined(MBEDTLS_X509_CRT_PARSE_C)
5296 ssl_key_cert_free( conf->key_cert );
5297#endif
5298
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005299 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005300}
5301
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005302#if defined(MBEDTLS_PK_C) && \
5303 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005304/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309#if defined(MBEDTLS_RSA_C)
5310 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5311 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313#if defined(MBEDTLS_ECDSA_C)
5314 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5315 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005316#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005318}
5319
Hanno Becker7e5437a2017-04-28 17:15:26 +01005320unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5321{
5322 switch( type ) {
5323 case MBEDTLS_PK_RSA:
5324 return( MBEDTLS_SSL_SIG_RSA );
5325 case MBEDTLS_PK_ECDSA:
5326 case MBEDTLS_PK_ECKEY:
5327 return( MBEDTLS_SSL_SIG_ECDSA );
5328 default:
5329 return( MBEDTLS_SSL_SIG_ANON );
5330 }
5331}
5332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005334{
5335 switch( sig )
5336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#if defined(MBEDTLS_RSA_C)
5338 case MBEDTLS_SSL_SIG_RSA:
5339 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005340#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341#if defined(MBEDTLS_ECDSA_C)
5342 case MBEDTLS_SSL_SIG_ECDSA:
5343 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005344#endif
5345 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005347 }
5348}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005349#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005350
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005351/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005352 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005355{
5356 switch( hash )
5357 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005358#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 case MBEDTLS_SSL_HASH_MD5:
5360 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005361#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005362#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363 case MBEDTLS_SSL_HASH_SHA1:
5364 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005365#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005366#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 case MBEDTLS_SSL_HASH_SHA224:
5368 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005369#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005370#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 case MBEDTLS_SSL_HASH_SHA256:
5372 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005373#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005374#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 case MBEDTLS_SSL_HASH_SHA384:
5376 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005377#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005378#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 case MBEDTLS_SSL_HASH_SHA512:
5380 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005381#endif
5382 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005384 }
5385}
5386
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005387/*
5388 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5389 */
5390unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5391{
5392 switch( md )
5393 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005394#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005395 case MBEDTLS_MD_MD5:
5396 return( MBEDTLS_SSL_HASH_MD5 );
5397#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005398#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005399 case MBEDTLS_MD_SHA1:
5400 return( MBEDTLS_SSL_HASH_SHA1 );
5401#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005402#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005403 case MBEDTLS_MD_SHA224:
5404 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005405#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005406#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005407 case MBEDTLS_MD_SHA256:
5408 return( MBEDTLS_SSL_HASH_SHA256 );
5409#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005410#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005411 case MBEDTLS_MD_SHA384:
5412 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005413#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005414#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005415 case MBEDTLS_MD_SHA512:
5416 return( MBEDTLS_SSL_HASH_SHA512 );
5417#endif
5418 default:
5419 return( MBEDTLS_SSL_HASH_NONE );
5420 }
5421}
5422
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005423/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005424 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005425 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005426 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005427int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005428{
Brett Warrene0edc842021-08-17 09:53:13 +01005429 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005430
Brett Warrene0edc842021-08-17 09:53:13 +01005431 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005432 return( -1 );
5433
Brett Warrene0edc842021-08-17 09:53:13 +01005434 for( ; *group_list != 0; group_list++ )
5435 {
5436 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005437 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005438 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005439
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005440 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005441}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005442
5443#if defined(MBEDTLS_ECP_C)
5444/*
5445 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5446 */
5447int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5448{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005449 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005450 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005451
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005452 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005453 return -1;
5454
5455 uint16_t tls_id = grp_info->tls_id;
5456
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005457 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5458}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005459#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461#if defined(MBEDTLS_X509_CRT_PARSE_C)
5462int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5463 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005464 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005465 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005466{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005467 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005468 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005469 const char *ext_oid;
5470 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005473 {
5474 /* Server part of the key exchange */
5475 switch( ciphersuite->key_exchange )
5476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 case MBEDTLS_KEY_EXCHANGE_RSA:
5478 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005479 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005480 break;
5481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5483 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5484 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5485 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005486 break;
5487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5489 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005490 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005491 break;
5492
5493 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 case MBEDTLS_KEY_EXCHANGE_NONE:
5495 case MBEDTLS_KEY_EXCHANGE_PSK:
5496 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5497 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005498 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005499 usage = 0;
5500 }
5501 }
5502 else
5503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5505 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005506 }
5507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005509 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005510 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005511 ret = -1;
5512 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5517 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005518 }
5519 else
5520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5522 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005523 }
5524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005526 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005527 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005528 ret = -1;
5529 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005530
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005531 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005534
Jerry Yu148165c2021-09-24 23:20:59 +08005535#if defined(MBEDTLS_USE_PSA_CRYPTO)
5536int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5537 const mbedtls_md_type_t md,
5538 unsigned char *dst,
5539 size_t dst_len,
5540 size_t *olen )
5541{
Ronald Cronf6893e12022-01-07 22:09:01 +01005542 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5543 psa_hash_operation_t *hash_operation_to_clone;
5544 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5545
Jerry Yu148165c2021-09-24 23:20:59 +08005546 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005547
5548 switch( md )
5549 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005550#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005551 case MBEDTLS_MD_SHA384:
5552 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5553 break;
5554#endif
5555
Andrzej Kurek25f27152022-08-17 16:09:31 -04005556#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005557 case MBEDTLS_MD_SHA256:
5558 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5559 break;
5560#endif
5561
5562 default:
5563 goto exit;
5564 }
5565
5566 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5567 if( status != PSA_SUCCESS )
5568 goto exit;
5569
5570 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5571 if( status != PSA_SUCCESS )
5572 goto exit;
5573
5574exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005575#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5576 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5577 (void) ssl;
5578#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005579 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005580}
5581#else /* MBEDTLS_USE_PSA_CRYPTO */
5582
Andrzej Kurek25f27152022-08-17 16:09:31 -04005583#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005584MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005585static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5586 unsigned char *dst,
5587 size_t dst_len,
5588 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005589{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005590 int ret;
5591 mbedtls_sha512_context sha512;
5592
5593 if( dst_len < 48 )
5594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5595
5596 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005597 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005598
5599 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5600 {
5601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5602 goto exit;
5603 }
5604
5605 *olen = 48;
5606
5607exit:
5608
5609 mbedtls_sha512_free( &sha512 );
5610 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005611}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005612#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005613
Andrzej Kurek25f27152022-08-17 16:09:31 -04005614#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005615MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005616static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5617 unsigned char *dst,
5618 size_t dst_len,
5619 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005620{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005621 int ret;
5622 mbedtls_sha256_context sha256;
5623
5624 if( dst_len < 32 )
5625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5626
5627 mbedtls_sha256_init( &sha256 );
5628 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005629
Jerry Yu24c0ec32021-09-09 14:21:07 +08005630 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5631 {
5632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5633 goto exit;
5634 }
5635
5636 *olen = 32;
5637
5638exit:
5639
5640 mbedtls_sha256_free( &sha256 );
5641 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005642}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005643#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005644
Jerry Yu000f9762021-09-14 11:12:51 +08005645int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5646 const mbedtls_md_type_t md,
5647 unsigned char *dst,
5648 size_t dst_len,
5649 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005650{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005651 switch( md )
5652 {
5653
Andrzej Kurek25f27152022-08-17 16:09:31 -04005654#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005655 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005656 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005657#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005658
Andrzej Kurek25f27152022-08-17 16:09:31 -04005659#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005660 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005661 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005662#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005663
5664 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005665#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5666 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5667 (void) ssl;
5668 (void) dst;
5669 (void) dst_len;
5670 (void) olen;
5671#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005672 break;
5673 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005675}
XiaokangQian647719a2021-12-07 09:16:29 +00005676
Jerry Yu148165c2021-09-24 23:20:59 +08005677#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005678
Ronald Crone68ab4f2022-10-05 12:46:29 +02005679#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005680/* mbedtls_ssl_parse_sig_alg_ext()
5681 *
5682 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5683 * value (TLS 1.3 RFC8446):
5684 * enum {
5685 * ....
5686 * ecdsa_secp256r1_sha256( 0x0403 ),
5687 * ecdsa_secp384r1_sha384( 0x0503 ),
5688 * ecdsa_secp521r1_sha512( 0x0603 ),
5689 * ....
5690 * } SignatureScheme;
5691 *
5692 * struct {
5693 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5694 * } SignatureSchemeList;
5695 *
5696 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5697 * value (TLS 1.2 RFC5246):
5698 * enum {
5699 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5700 * sha512(6), (255)
5701 * } HashAlgorithm;
5702 *
5703 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5704 * SignatureAlgorithm;
5705 *
5706 * struct {
5707 * HashAlgorithm hash;
5708 * SignatureAlgorithm signature;
5709 * } SignatureAndHashAlgorithm;
5710 *
5711 * SignatureAndHashAlgorithm
5712 * supported_signature_algorithms<2..2^16-2>;
5713 *
5714 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5715 * generalization of the TLS 1.2 signature algorithm extension.
5716 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5717 * `SignatureScheme` field of TLS 1.3
5718 *
5719 */
5720int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5721 const unsigned char *buf,
5722 const unsigned char *end )
5723{
5724 const unsigned char *p = buf;
5725 size_t supported_sig_algs_len = 0;
5726 const unsigned char *supported_sig_algs_end;
5727 uint16_t sig_alg;
5728 uint32_t common_idx = 0;
5729
5730 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5731 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5732 p += 2;
5733
5734 memset( ssl->handshake->received_sig_algs, 0,
5735 sizeof(ssl->handshake->received_sig_algs) );
5736
5737 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5738 supported_sig_algs_end = p + supported_sig_algs_len;
5739 while( p < supported_sig_algs_end )
5740 {
5741 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5742 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5743 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005744 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5745 sig_alg,
5746 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005747#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005748 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005749 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5750 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5751 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005752 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005753 }
5754#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005755
Jerry Yuf3b46b52022-06-19 16:52:27 +08005756 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5757 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005758
5759 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5760 {
5761 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5762 common_idx += 1;
5763 }
5764 }
5765 /* Check that we consumed all the message. */
5766 if( p != end )
5767 {
5768 MBEDTLS_SSL_DEBUG_MSG( 1,
5769 ( "Signature algorithms extension length misaligned" ) );
5770 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5771 MBEDTLS_ERR_SSL_DECODE_ERROR );
5772 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5773 }
5774
5775 if( common_idx == 0 )
5776 {
5777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5778 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5779 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5780 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5781 }
5782
Gabor Mezei15b95a62022-05-09 16:37:58 +02005783 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005784 return( 0 );
5785}
5786
Ronald Crone68ab4f2022-10-05 12:46:29 +02005787#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005788
Jerry Yudc7bd172022-02-17 13:44:15 +08005789#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5790
5791#if defined(MBEDTLS_USE_PSA_CRYPTO)
5792
5793static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5794 mbedtls_svc_key_id_t key,
5795 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005796 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005797 const unsigned char* seed, size_t seed_length,
5798 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005799 const unsigned char* other_secret,
5800 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005801 size_t capacity )
5802{
5803 psa_status_t status;
5804
5805 status = psa_key_derivation_setup( derivation, alg );
5806 if( status != PSA_SUCCESS )
5807 return( status );
5808
5809 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5810 {
5811 status = psa_key_derivation_input_bytes( derivation,
5812 PSA_KEY_DERIVATION_INPUT_SEED,
5813 seed, seed_length );
5814 if( status != PSA_SUCCESS )
5815 return( status );
5816
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005817 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005818 {
5819 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005820 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5821 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005822 if( status != PSA_SUCCESS )
5823 return( status );
5824 }
5825
Jerry Yudc7bd172022-02-17 13:44:15 +08005826 if( mbedtls_svc_key_id_is_null( key ) )
5827 {
5828 status = psa_key_derivation_input_bytes(
5829 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005830 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005831 }
5832 else
5833 {
5834 status = psa_key_derivation_input_key(
5835 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5836 }
5837 if( status != PSA_SUCCESS )
5838 return( status );
5839
5840 status = psa_key_derivation_input_bytes( derivation,
5841 PSA_KEY_DERIVATION_INPUT_LABEL,
5842 label, label_length );
5843 if( status != PSA_SUCCESS )
5844 return( status );
5845 }
5846 else
5847 {
5848 return( PSA_ERROR_NOT_SUPPORTED );
5849 }
5850
5851 status = psa_key_derivation_set_capacity( derivation, capacity );
5852 if( status != PSA_SUCCESS )
5853 return( status );
5854
5855 return( PSA_SUCCESS );
5856}
5857
Andrzej Kurek57d10632022-10-24 10:32:01 -04005858#if defined(PSA_WANT_ALG_SHA_384) || \
5859 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005860MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005861static int tls_prf_generic( mbedtls_md_type_t md_type,
5862 const unsigned char *secret, size_t slen,
5863 const char *label,
5864 const unsigned char *random, size_t rlen,
5865 unsigned char *dstbuf, size_t dlen )
5866{
5867 psa_status_t status;
5868 psa_algorithm_t alg;
5869 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5870 psa_key_derivation_operation_t derivation =
5871 PSA_KEY_DERIVATION_OPERATION_INIT;
5872
5873 if( md_type == MBEDTLS_MD_SHA384 )
5874 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5875 else
5876 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5877
5878 /* Normally a "secret" should be long enough to be impossible to
5879 * find by brute force, and in particular should not be empty. But
5880 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5881 * and for this use case it makes sense to have a 0-length "secret".
5882 * Since the key API doesn't allow importing a key of length 0,
5883 * keep master_key=0, which setup_psa_key_derivation() understands
5884 * to mean a 0-length "secret" input. */
5885 if( slen != 0 )
5886 {
5887 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5888 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5889 psa_set_key_algorithm( &key_attributes, alg );
5890 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5891
5892 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5893 if( status != PSA_SUCCESS )
5894 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5895 }
5896
5897 status = setup_psa_key_derivation( &derivation,
5898 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005899 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005900 random, rlen,
5901 (unsigned char const *) label,
5902 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005903 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005904 dlen );
5905 if( status != PSA_SUCCESS )
5906 {
5907 psa_key_derivation_abort( &derivation );
5908 psa_destroy_key( master_key );
5909 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5910 }
5911
5912 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5913 if( status != PSA_SUCCESS )
5914 {
5915 psa_key_derivation_abort( &derivation );
5916 psa_destroy_key( master_key );
5917 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5918 }
5919
5920 status = psa_key_derivation_abort( &derivation );
5921 if( status != PSA_SUCCESS )
5922 {
5923 psa_destroy_key( master_key );
5924 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5925 }
5926
5927 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5928 status = psa_destroy_key( master_key );
5929 if( status != PSA_SUCCESS )
5930 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5931
5932 return( 0 );
5933}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005934#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005935#else /* MBEDTLS_USE_PSA_CRYPTO */
5936
Andrzej Kurek57d10632022-10-24 10:32:01 -04005937#if defined(MBEDTLS_MD_C) && \
5938 ( defined(MBEDTLS_SHA256_C) || \
5939 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005940MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005941static int tls_prf_generic( mbedtls_md_type_t md_type,
5942 const unsigned char *secret, size_t slen,
5943 const char *label,
5944 const unsigned char *random, size_t rlen,
5945 unsigned char *dstbuf, size_t dlen )
5946{
5947 size_t nb;
5948 size_t i, j, k, md_len;
5949 unsigned char *tmp;
5950 size_t tmp_len = 0;
5951 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5952 const mbedtls_md_info_t *md_info;
5953 mbedtls_md_context_t md_ctx;
5954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5955
5956 mbedtls_md_init( &md_ctx );
5957
5958 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5960
5961 md_len = mbedtls_md_get_size( md_info );
5962
5963 tmp_len = md_len + strlen( label ) + rlen;
5964 tmp = mbedtls_calloc( 1, tmp_len );
5965 if( tmp == NULL )
5966 {
5967 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5968 goto exit;
5969 }
5970
5971 nb = strlen( label );
5972 memcpy( tmp + md_len, label, nb );
5973 memcpy( tmp + md_len + nb, random, rlen );
5974 nb += rlen;
5975
5976 /*
5977 * Compute P_<hash>(secret, label + random)[0..dlen]
5978 */
5979 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5980 goto exit;
5981
5982 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5983 if( ret != 0 )
5984 goto exit;
5985 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5986 if( ret != 0 )
5987 goto exit;
5988 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5989 if( ret != 0 )
5990 goto exit;
5991
5992 for( i = 0; i < dlen; i += md_len )
5993 {
5994 ret = mbedtls_md_hmac_reset ( &md_ctx );
5995 if( ret != 0 )
5996 goto exit;
5997 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5998 if( ret != 0 )
5999 goto exit;
6000 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
6001 if( ret != 0 )
6002 goto exit;
6003
6004 ret = mbedtls_md_hmac_reset ( &md_ctx );
6005 if( ret != 0 )
6006 goto exit;
6007 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
6008 if( ret != 0 )
6009 goto exit;
6010 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6011 if( ret != 0 )
6012 goto exit;
6013
6014 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
6015
6016 for( j = 0; j < k; j++ )
6017 dstbuf[i + j] = h_i[j];
6018 }
6019
6020exit:
6021 mbedtls_md_free( &md_ctx );
6022
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006023 if ( tmp != NULL )
6024 mbedtls_platform_zeroize( tmp, tmp_len );
6025
Jerry Yudc7bd172022-02-17 13:44:15 +08006026 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6027
6028 mbedtls_free( tmp );
6029
6030 return( ret );
6031}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006032#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006033#endif /* MBEDTLS_USE_PSA_CRYPTO */
6034
Andrzej Kurek25f27152022-08-17 16:09:31 -04006035#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006036MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006037static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6038 const char *label,
6039 const unsigned char *random, size_t rlen,
6040 unsigned char *dstbuf, size_t dlen )
6041{
6042 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6043 label, random, rlen, dstbuf, dlen ) );
6044}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006045#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006046
Andrzej Kurek25f27152022-08-17 16:09:31 -04006047#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006048MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006049static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6050 const char *label,
6051 const unsigned char *random, size_t rlen,
6052 unsigned char *dstbuf, size_t dlen )
6053{
6054 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6055 label, random, rlen, dstbuf, dlen ) );
6056}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006057#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006058
Jerry Yuf009d862022-02-17 14:01:37 +08006059/*
6060 * Set appropriate PRF function and other SSL / TLS1.2 functions
6061 *
6062 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006063 * - hash associated with the ciphersuite (only used by TLS 1.2)
6064 *
6065 * Outputs:
6066 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6067 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006068MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006069static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006070 mbedtls_md_type_t hash )
6071{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006072#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006073 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006074 {
6075 handshake->tls_prf = tls_prf_sha384;
6076 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6077 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6078 }
6079 else
6080#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006081#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006082 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006083 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006084 handshake->tls_prf = tls_prf_sha256;
6085 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6086 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6087 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006088#else
Jerry Yuf009d862022-02-17 14:01:37 +08006089 {
Jerry Yuf009d862022-02-17 14:01:37 +08006090 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006091 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6093 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006094#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006095
6096 return( 0 );
6097}
Jerry Yud6ab2352022-02-17 14:03:43 +08006098
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006099/*
6100 * Compute master secret if needed
6101 *
6102 * Parameters:
6103 * [in/out] handshake
6104 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6105 * (PSA-PSK) ciphersuite_info, psk_opaque
6106 * [out] premaster (cleared)
6107 * [out] master
6108 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6109 * debug: conf->f_dbg, conf->p_dbg
6110 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006111 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006112 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006113MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006114static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6115 unsigned char *master,
6116 const mbedtls_ssl_context *ssl )
6117{
6118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6119
6120 /* cf. RFC 5246, Section 8.1:
6121 * "The master secret is always exactly 48 bytes in length." */
6122 size_t const master_secret_len = 48;
6123
6124#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6125 unsigned char session_hash[48];
6126#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6127
6128 /* The label for the KDF used for key expansion.
6129 * This is either "master secret" or "extended master secret"
6130 * depending on whether the Extended Master Secret extension
6131 * is used. */
6132 char const *lbl = "master secret";
6133
Przemek Stekielae4ed302022-04-05 17:15:55 +02006134 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006135 * - If the Extended Master Secret extension is not used,
6136 * this is ClientHello.Random + ServerHello.Random
6137 * (see Sect. 8.1 in RFC 5246).
6138 * - If the Extended Master Secret extension is used,
6139 * this is the transcript of the handshake so far.
6140 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006141 unsigned char const *seed = handshake->randbytes;
6142 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006143
6144#if !defined(MBEDTLS_DEBUG_C) && \
6145 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6146 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6147 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6148 ssl = NULL; /* make sure we don't use it except for those cases */
6149 (void) ssl;
6150#endif
6151
6152 if( handshake->resume != 0 )
6153 {
6154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6155 return( 0 );
6156 }
6157
6158#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6159 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6160 {
6161 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006162 seed = session_hash;
6163 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006164
6165 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006166 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006167 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006168#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006169
Przemek Stekiel99114f32022-04-22 11:20:09 +02006170#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006171 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006172 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006173 {
6174 /* Perform PSK-to-MS expansion in a single step. */
6175 psa_status_t status;
6176 psa_algorithm_t alg;
6177 mbedtls_svc_key_id_t psk;
6178 psa_key_derivation_operation_t derivation =
6179 PSA_KEY_DERIVATION_OPERATION_INIT;
6180 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6181
6182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6183
6184 psk = mbedtls_ssl_get_opaque_psk( ssl );
6185
6186 if( hash_alg == MBEDTLS_MD_SHA384 )
6187 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6188 else
6189 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6190
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006191 size_t other_secret_len = 0;
6192 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006193
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006194 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006195 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006196 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006197 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006198 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006199 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006200 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006201 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006202 other_secret_len = 48;
6203 other_secret = handshake->premaster + 2;
6204 break;
6205 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006206 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006207 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6208 other_secret = handshake->premaster + 2;
6209 break;
6210 default:
6211 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006212 }
6213
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006214 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006215 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006216 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006217 (unsigned char const *) lbl,
6218 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006219 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006220 master_secret_len );
6221 if( status != PSA_SUCCESS )
6222 {
6223 psa_key_derivation_abort( &derivation );
6224 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6225 }
6226
6227 status = psa_key_derivation_output_bytes( &derivation,
6228 master,
6229 master_secret_len );
6230 if( status != PSA_SUCCESS )
6231 {
6232 psa_key_derivation_abort( &derivation );
6233 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6234 }
6235
6236 status = psa_key_derivation_abort( &derivation );
6237 if( status != PSA_SUCCESS )
6238 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6239 }
6240 else
6241#endif
6242 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006243#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6244 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6245 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6246 {
6247 psa_status_t status;
6248 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6249 psa_key_derivation_operation_t derivation =
6250 PSA_KEY_DERIVATION_OPERATION_INIT;
6251
6252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6253
6254 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6255
6256 status = psa_key_derivation_setup( &derivation, alg );
6257 if( status != PSA_SUCCESS )
6258 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6259
6260 status = psa_key_derivation_set_capacity( &derivation,
6261 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6262 if( status != PSA_SUCCESS )
6263 {
6264 psa_key_derivation_abort( &derivation );
6265 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6266 }
6267
6268 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6269 &derivation );
6270 if( status != PSA_SUCCESS )
6271 {
6272 psa_key_derivation_abort( &derivation );
6273 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6274 }
6275
6276 status = psa_key_derivation_output_bytes( &derivation,
6277 handshake->premaster,
6278 handshake->pmslen );
6279 if( status != PSA_SUCCESS )
6280 {
6281 psa_key_derivation_abort( &derivation );
6282 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6283 }
6284
6285 status = psa_key_derivation_abort( &derivation );
6286 if( status != PSA_SUCCESS )
6287 {
6288 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6289 }
6290 }
6291#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006292 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006293 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006294 master,
6295 master_secret_len );
6296 if( ret != 0 )
6297 {
6298 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6299 return( ret );
6300 }
6301
6302 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6303 handshake->premaster,
6304 handshake->pmslen );
6305
6306 mbedtls_platform_zeroize( handshake->premaster,
6307 sizeof(handshake->premaster) );
6308 }
6309
6310 return( 0 );
6311}
6312
Jerry Yud62f87e2022-02-17 14:09:02 +08006313int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6314{
6315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6316 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6317 ssl->handshake->ciphersuite_info;
6318
6319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6320
6321 /* Set PRF, calc_verify and calc_finished function pointers */
6322 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006323 ciphersuite_info->mac );
6324 if( ret != 0 )
6325 {
6326 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6327 return( ret );
6328 }
6329
6330 /* Compute master secret if needed */
6331 ret = ssl_compute_master( ssl->handshake,
6332 ssl->session_negotiate->master,
6333 ssl );
6334 if( ret != 0 )
6335 {
6336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6337 return( ret );
6338 }
6339
6340 /* Swap the client and server random values:
6341 * - MS derivation wanted client+server (RFC 5246 8.1)
6342 * - key derivation wants server+client (RFC 5246 6.3) */
6343 {
6344 unsigned char tmp[64];
6345 memcpy( tmp, ssl->handshake->randbytes, 64 );
6346 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6347 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6348 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6349 }
6350
6351 /* Populate transform structure */
6352 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6353 ssl->session_negotiate->ciphersuite,
6354 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006355#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006356 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006357#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006358 ssl->handshake->tls_prf,
6359 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006360 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006361 ssl->conf->endpoint,
6362 ssl );
6363 if( ret != 0 )
6364 {
6365 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6366 return( ret );
6367 }
6368
6369 /* We no longer need Server/ClientHello.random values */
6370 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6371 sizeof( ssl->handshake->randbytes ) );
6372
6373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6374
6375 return( 0 );
6376}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006377
Ronald Cron4dcbca92022-03-07 10:21:40 +01006378int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6379{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006380 switch( md )
6381 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006382#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006383 case MBEDTLS_SSL_HASH_SHA384:
6384 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6385 break;
6386#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006387#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006388 case MBEDTLS_SSL_HASH_SHA256:
6389 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6390 break;
6391#endif
6392 default:
6393 return( -1 );
6394 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006395#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6396 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6397 (void) ssl;
6398#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006399 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006400}
6401
Andrzej Kurek25f27152022-08-17 16:09:31 -04006402#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006403void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6404 unsigned char *hash,
6405 size_t *hlen )
6406{
6407#if defined(MBEDTLS_USE_PSA_CRYPTO)
6408 size_t hash_size;
6409 psa_status_t status;
6410 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6411
6412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6413 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6414 if( status != PSA_SUCCESS )
6415 {
6416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6417 return;
6418 }
6419
6420 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6421 if( status != PSA_SUCCESS )
6422 {
6423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6424 return;
6425 }
6426
6427 *hlen = 32;
6428 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6430#else
6431 mbedtls_sha256_context sha256;
6432
6433 mbedtls_sha256_init( &sha256 );
6434
6435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6436
6437 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6438 mbedtls_sha256_finish( &sha256, hash );
6439
6440 *hlen = 32;
6441
6442 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6444
6445 mbedtls_sha256_free( &sha256 );
6446#endif /* MBEDTLS_USE_PSA_CRYPTO */
6447 return;
6448}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006449#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006450
Andrzej Kurek25f27152022-08-17 16:09:31 -04006451#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006452void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6453 unsigned char *hash,
6454 size_t *hlen )
6455{
6456#if defined(MBEDTLS_USE_PSA_CRYPTO)
6457 size_t hash_size;
6458 psa_status_t status;
6459 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6460
6461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6462 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6463 if( status != PSA_SUCCESS )
6464 {
6465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6466 return;
6467 }
6468
6469 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6470 if( status != PSA_SUCCESS )
6471 {
6472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6473 return;
6474 }
6475
6476 *hlen = 48;
6477 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6479#else
6480 mbedtls_sha512_context sha512;
6481
6482 mbedtls_sha512_init( &sha512 );
6483
6484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6485
Andrzej Kureka242e832022-08-11 10:03:14 -04006486 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006487 mbedtls_sha512_finish( &sha512, hash );
6488
6489 *hlen = 48;
6490
6491 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6493
6494 mbedtls_sha512_free( &sha512 );
6495#endif /* MBEDTLS_USE_PSA_CRYPTO */
6496 return;
6497}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006498#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006499
Neil Armstrong80f6f322022-05-03 17:56:38 +02006500#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6501 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006502int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6503{
6504 unsigned char *p = ssl->handshake->premaster;
6505 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6506 const unsigned char *psk = NULL;
6507 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006508 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006509
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006510 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006511 {
6512 /*
6513 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006514 * checked before calling this function.
6515 *
6516 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006517 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006518 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006519 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6520 {
6521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6523 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006524 }
6525
6526 /*
6527 * PMS = struct {
6528 * opaque other_secret<0..2^16-1>;
6529 * opaque psk<0..2^16-1>;
6530 * };
6531 * with "other_secret" depending on the particular key exchange
6532 */
6533#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6534 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6535 {
6536 if( end - p < 2 )
6537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6538
6539 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6540 p += 2;
6541
6542 if( end < p || (size_t)( end - p ) < psk_len )
6543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6544
6545 memset( p, 0, psk_len );
6546 p += psk_len;
6547 }
6548 else
6549#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6550#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6551 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6552 {
6553 /*
6554 * other_secret already set by the ClientKeyExchange message,
6555 * and is 48 bytes long
6556 */
6557 if( end - p < 2 )
6558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6559
6560 *p++ = 0;
6561 *p++ = 48;
6562 p += 48;
6563 }
6564 else
6565#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6566#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6567 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6568 {
6569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6570 size_t len;
6571
6572 /* Write length only when we know the actual value */
6573 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6574 p + 2, end - ( p + 2 ), &len,
6575 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6576 {
6577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6578 return( ret );
6579 }
6580 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6581 p += 2 + len;
6582
6583 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6584 }
6585 else
6586#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006587#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006588 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6589 {
6590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6591 size_t zlen;
6592
6593 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6594 p + 2, end - ( p + 2 ),
6595 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6596 {
6597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6598 return( ret );
6599 }
6600
6601 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6602 p += 2 + zlen;
6603
6604 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6605 MBEDTLS_DEBUG_ECDH_Z );
6606 }
6607 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006608#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006609 {
6610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6612 }
6613
6614 /* opaque psk<0..2^16-1>; */
6615 if( end - p < 2 )
6616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6617
6618 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6619 p += 2;
6620
6621 if( end < p || (size_t)( end - p ) < psk_len )
6622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6623
6624 memcpy( p, psk, psk_len );
6625 p += psk_len;
6626
6627 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6628
6629 return( 0 );
6630}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006631#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006632
6633#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006634MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006635static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6636
6637#if defined(MBEDTLS_SSL_PROTO_DTLS)
6638int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6639{
6640 /* If renegotiation is not enforced, retransmit until we would reach max
6641 * timeout if we were using the usual handshake doubling scheme */
6642 if( ssl->conf->renego_max_records < 0 )
6643 {
6644 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6645 unsigned char doublings = 1;
6646
6647 while( ratio != 0 )
6648 {
6649 ++doublings;
6650 ratio >>= 1;
6651 }
6652
6653 if( ++ssl->renego_records_seen > doublings )
6654 {
6655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6656 return( 0 );
6657 }
6658 }
6659
6660 return( ssl_write_hello_request( ssl ) );
6661}
6662#endif
6663#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006664
Jerry Yud9526692022-02-17 14:23:47 +08006665/*
6666 * Handshake functions
6667 */
6668#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6669/* No certificate support -> dummy functions */
6670int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6671{
6672 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6673 ssl->handshake->ciphersuite_info;
6674
6675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6676
6677 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6680 ssl->state++;
6681 return( 0 );
6682 }
6683
6684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6686}
6687
6688int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6689{
6690 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6691 ssl->handshake->ciphersuite_info;
6692
6693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6694
6695 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6696 {
6697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6698 ssl->state++;
6699 return( 0 );
6700 }
6701
6702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6704}
6705
6706#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6707/* Some certificate support -> implement write and parse */
6708
6709int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6710{
6711 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6712 size_t i, n;
6713 const mbedtls_x509_crt *crt;
6714 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6715 ssl->handshake->ciphersuite_info;
6716
6717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6718
6719 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6720 {
6721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6722 ssl->state++;
6723 return( 0 );
6724 }
6725
6726#if defined(MBEDTLS_SSL_CLI_C)
6727 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6728 {
6729 if( ssl->handshake->client_auth == 0 )
6730 {
6731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6732 ssl->state++;
6733 return( 0 );
6734 }
6735 }
6736#endif /* MBEDTLS_SSL_CLI_C */
6737#if defined(MBEDTLS_SSL_SRV_C)
6738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6739 {
6740 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6741 {
6742 /* Should never happen because we shouldn't have picked the
6743 * ciphersuite if we don't have a certificate. */
6744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6745 }
6746 }
6747#endif
6748
6749 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6750
6751 /*
6752 * 0 . 0 handshake type
6753 * 1 . 3 handshake length
6754 * 4 . 6 length of all certs
6755 * 7 . 9 length of cert. 1
6756 * 10 . n-1 peer certificate
6757 * n . n+2 length of cert. 2
6758 * n+3 . ... upper level cert, etc.
6759 */
6760 i = 7;
6761 crt = mbedtls_ssl_own_cert( ssl );
6762
6763 while( crt != NULL )
6764 {
6765 n = crt->raw.len;
6766 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6767 {
6768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6769 " > %" MBEDTLS_PRINTF_SIZET,
6770 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6771 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6772 }
6773
6774 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6775 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6776 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6777
6778 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6779 i += n; crt = crt->next;
6780 }
6781
6782 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6783 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6784 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6785
6786 ssl->out_msglen = i;
6787 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6788 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6789
6790 ssl->state++;
6791
6792 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6793 {
6794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6795 return( ret );
6796 }
6797
6798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6799
6800 return( ret );
6801}
6802
6803#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6804
6805#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006806MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006807static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6808 unsigned char *crt_buf,
6809 size_t crt_buf_len )
6810{
6811 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6812
6813 if( peer_crt == NULL )
6814 return( -1 );
6815
6816 if( peer_crt->raw.len != crt_buf_len )
6817 return( -1 );
6818
6819 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6820}
6821#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006822MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006823static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6824 unsigned char *crt_buf,
6825 size_t crt_buf_len )
6826{
6827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6828 unsigned char const * const peer_cert_digest =
6829 ssl->session->peer_cert_digest;
6830 mbedtls_md_type_t const peer_cert_digest_type =
6831 ssl->session->peer_cert_digest_type;
6832 mbedtls_md_info_t const * const digest_info =
6833 mbedtls_md_info_from_type( peer_cert_digest_type );
6834 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6835 size_t digest_len;
6836
6837 if( peer_cert_digest == NULL || digest_info == NULL )
6838 return( -1 );
6839
6840 digest_len = mbedtls_md_get_size( digest_info );
6841 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6842 return( -1 );
6843
6844 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6845 if( ret != 0 )
6846 return( -1 );
6847
6848 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6849}
6850#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6851#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6852
6853/*
6854 * Once the certificate message is read, parse it into a cert chain and
6855 * perform basic checks, but leave actual verification to the caller
6856 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006857MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006858static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6859 mbedtls_x509_crt *chain )
6860{
6861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6862#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6863 int crt_cnt=0;
6864#endif
6865 size_t i, n;
6866 uint8_t alert;
6867
6868 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6869 {
6870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6871 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6872 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6873 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6874 }
6875
6876 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6877 {
6878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6879 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6880 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6881 }
6882
6883 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6884 {
6885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6886 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6887 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6888 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6889 }
6890
6891 i = mbedtls_ssl_hs_hdr_len( ssl );
6892
6893 /*
6894 * Same message structure as in mbedtls_ssl_write_certificate()
6895 */
6896 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6897
6898 if( ssl->in_msg[i] != 0 ||
6899 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6900 {
6901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6902 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6903 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6904 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6905 }
6906
6907 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6908 i += 3;
6909
6910 /* Iterate through and parse the CRTs in the provided chain. */
6911 while( i < ssl->in_hslen )
6912 {
6913 /* Check that there's room for the next CRT's length fields. */
6914 if ( i + 3 > ssl->in_hslen ) {
6915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6916 mbedtls_ssl_send_alert_message( ssl,
6917 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6918 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6919 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6920 }
6921 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6922 * anything beyond 2**16 ~ 64K. */
6923 if( ssl->in_msg[i] != 0 )
6924 {
6925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6926 mbedtls_ssl_send_alert_message( ssl,
6927 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6928 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6929 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6930 }
6931
6932 /* Read length of the next CRT in the chain. */
6933 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6934 | (unsigned int) ssl->in_msg[i + 2];
6935 i += 3;
6936
6937 if( n < 128 || i + n > ssl->in_hslen )
6938 {
6939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6940 mbedtls_ssl_send_alert_message( ssl,
6941 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6942 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6943 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6944 }
6945
6946 /* Check if we're handling the first CRT in the chain. */
6947#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6948 if( crt_cnt++ == 0 &&
6949 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6950 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6951 {
6952 /* During client-side renegotiation, check that the server's
6953 * end-CRTs hasn't changed compared to the initial handshake,
6954 * mitigating the triple handshake attack. On success, reuse
6955 * the original end-CRT instead of parsing it again. */
6956 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6957 if( ssl_check_peer_crt_unchanged( ssl,
6958 &ssl->in_msg[i],
6959 n ) != 0 )
6960 {
6961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6962 mbedtls_ssl_send_alert_message( ssl,
6963 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6964 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6965 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6966 }
6967
6968 /* Now we can safely free the original chain. */
6969 ssl_clear_peer_cert( ssl->session );
6970 }
6971#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6972
6973 /* Parse the next certificate in the chain. */
6974#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6975 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6976#else
6977 /* If we don't need to store the CRT chain permanently, parse
6978 * it in-place from the input buffer instead of making a copy. */
6979 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6980#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6981 switch( ret )
6982 {
6983 case 0: /*ok*/
6984 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6985 /* Ignore certificate with an unknown algorithm: maybe a
6986 prior certificate was already trusted. */
6987 break;
6988
6989 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6990 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6991 goto crt_parse_der_failed;
6992
6993 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6994 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6995 goto crt_parse_der_failed;
6996
6997 default:
6998 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6999 crt_parse_der_failed:
7000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
7001 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7002 return( ret );
7003 }
7004
7005 i += n;
7006 }
7007
7008 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
7009 return( 0 );
7010}
7011
7012#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007013MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007014static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7015{
7016 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7017 return( -1 );
7018
7019 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7020 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7021 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7022 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7023 {
Ronald Cron19385882022-06-15 16:26:13 +02007024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007025 return( 0 );
7026 }
7027 return( -1 );
7028}
7029#endif /* MBEDTLS_SSL_SRV_C */
7030
7031/* Check if a certificate message is expected.
7032 * Return either
7033 * - SSL_CERTIFICATE_EXPECTED, or
7034 * - SSL_CERTIFICATE_SKIP
7035 * indicating whether a Certificate message is expected or not.
7036 */
7037#define SSL_CERTIFICATE_EXPECTED 0
7038#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007039MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007040static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7041 int authmode )
7042{
7043 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7044 ssl->handshake->ciphersuite_info;
7045
7046 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7047 return( SSL_CERTIFICATE_SKIP );
7048
7049#if defined(MBEDTLS_SSL_SRV_C)
7050 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7051 {
7052 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7053 return( SSL_CERTIFICATE_SKIP );
7054
7055 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7056 {
7057 ssl->session_negotiate->verify_result =
7058 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7059 return( SSL_CERTIFICATE_SKIP );
7060 }
7061 }
7062#else
7063 ((void) authmode);
7064#endif /* MBEDTLS_SSL_SRV_C */
7065
7066 return( SSL_CERTIFICATE_EXPECTED );
7067}
7068
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007069MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007070static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7071 int authmode,
7072 mbedtls_x509_crt *chain,
7073 void *rs_ctx )
7074{
7075 int ret = 0;
7076 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7077 ssl->handshake->ciphersuite_info;
7078 int have_ca_chain = 0;
7079
7080 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7081 void *p_vrfy;
7082
7083 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7084 return( 0 );
7085
7086 if( ssl->f_vrfy != NULL )
7087 {
7088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7089 f_vrfy = ssl->f_vrfy;
7090 p_vrfy = ssl->p_vrfy;
7091 }
7092 else
7093 {
7094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7095 f_vrfy = ssl->conf->f_vrfy;
7096 p_vrfy = ssl->conf->p_vrfy;
7097 }
7098
7099 /*
7100 * Main check: verify certificate
7101 */
7102#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7103 if( ssl->conf->f_ca_cb != NULL )
7104 {
7105 ((void) rs_ctx);
7106 have_ca_chain = 1;
7107
7108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7109 ret = mbedtls_x509_crt_verify_with_ca_cb(
7110 chain,
7111 ssl->conf->f_ca_cb,
7112 ssl->conf->p_ca_cb,
7113 ssl->conf->cert_profile,
7114 ssl->hostname,
7115 &ssl->session_negotiate->verify_result,
7116 f_vrfy, p_vrfy );
7117 }
7118 else
7119#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7120 {
7121 mbedtls_x509_crt *ca_chain;
7122 mbedtls_x509_crl *ca_crl;
7123
7124#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7125 if( ssl->handshake->sni_ca_chain != NULL )
7126 {
7127 ca_chain = ssl->handshake->sni_ca_chain;
7128 ca_crl = ssl->handshake->sni_ca_crl;
7129 }
7130 else
7131#endif
7132 {
7133 ca_chain = ssl->conf->ca_chain;
7134 ca_crl = ssl->conf->ca_crl;
7135 }
7136
7137 if( ca_chain != NULL )
7138 have_ca_chain = 1;
7139
7140 ret = mbedtls_x509_crt_verify_restartable(
7141 chain,
7142 ca_chain, ca_crl,
7143 ssl->conf->cert_profile,
7144 ssl->hostname,
7145 &ssl->session_negotiate->verify_result,
7146 f_vrfy, p_vrfy, rs_ctx );
7147 }
7148
7149 if( ret != 0 )
7150 {
7151 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7152 }
7153
7154#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7155 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7156 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7157#endif
7158
7159 /*
7160 * Secondary checks: always done, but change 'ret' only if it was 0
7161 */
7162
7163#if defined(MBEDTLS_ECP_C)
7164 {
7165 const mbedtls_pk_context *pk = &chain->pk;
7166
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007167 /* If certificate uses an EC key, make sure the curve is OK.
7168 * This is a public key, so it can't be opaque, so can_do() is a good
7169 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007170 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007171 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007172 /* and in the unlikely case the above assumption no longer holds
7173 * we are making sure that pk_ec() here does not return a NULL
7174 */
7175 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7176 if( ec == NULL )
7177 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7180 }
Jerry Yud9526692022-02-17 14:23:47 +08007181
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007182 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7183 {
7184 ssl->session_negotiate->verify_result |=
7185 MBEDTLS_X509_BADCERT_BAD_KEY;
7186
7187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7188 if( ret == 0 )
7189 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7190 }
Jerry Yud9526692022-02-17 14:23:47 +08007191 }
7192 }
7193#endif /* MBEDTLS_ECP_C */
7194
7195 if( mbedtls_ssl_check_cert_usage( chain,
7196 ciphersuite_info,
7197 ! ssl->conf->endpoint,
7198 &ssl->session_negotiate->verify_result ) != 0 )
7199 {
7200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7201 if( ret == 0 )
7202 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7203 }
7204
7205 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7206 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7207 * with details encoded in the verification flags. All other kinds
7208 * of error codes, including those from the user provided f_vrfy
7209 * functions, are treated as fatal and lead to a failure of
7210 * ssl_parse_certificate even if verification was optional. */
7211 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7212 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7213 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7214 {
7215 ret = 0;
7216 }
7217
7218 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7219 {
7220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7221 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7222 }
7223
7224 if( ret != 0 )
7225 {
7226 uint8_t alert;
7227
7228 /* The certificate may have been rejected for several reasons.
7229 Pick one and send the corresponding alert. Which alert to send
7230 may be a subject of debate in some cases. */
7231 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7232 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7233 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7234 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7235 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7236 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7237 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7238 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7239 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7240 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7241 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7242 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7243 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7244 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7245 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7246 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7247 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7248 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7249 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7250 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7251 else
7252 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7253 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7254 alert );
7255 }
7256
7257#if defined(MBEDTLS_DEBUG_C)
7258 if( ssl->session_negotiate->verify_result != 0 )
7259 {
7260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7261 (unsigned int) ssl->session_negotiate->verify_result ) );
7262 }
7263 else
7264 {
7265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7266 }
7267#endif /* MBEDTLS_DEBUG_C */
7268
7269 return( ret );
7270}
7271
7272#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007273MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007274static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7275 unsigned char *start, size_t len )
7276{
7277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7278 /* Remember digest of the peer's end-CRT. */
7279 ssl->session_negotiate->peer_cert_digest =
7280 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7281 if( ssl->session_negotiate->peer_cert_digest == NULL )
7282 {
7283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7284 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7285 mbedtls_ssl_send_alert_message( ssl,
7286 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7287 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7288
7289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7290 }
7291
7292 ret = mbedtls_md( mbedtls_md_info_from_type(
7293 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7294 start, len,
7295 ssl->session_negotiate->peer_cert_digest );
7296
7297 ssl->session_negotiate->peer_cert_digest_type =
7298 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7299 ssl->session_negotiate->peer_cert_digest_len =
7300 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7301
7302 return( ret );
7303}
7304
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007305MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007306static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7307 unsigned char *start, size_t len )
7308{
7309 unsigned char *end = start + len;
7310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7311
7312 /* Make a copy of the peer's raw public key. */
7313 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7314 ret = mbedtls_pk_parse_subpubkey( &start, end,
7315 &ssl->handshake->peer_pubkey );
7316 if( ret != 0 )
7317 {
7318 /* We should have parsed the public key before. */
7319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7320 }
7321
7322 return( 0 );
7323}
7324#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7325
7326int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7327{
7328 int ret = 0;
7329 int crt_expected;
7330#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7331 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7332 ? ssl->handshake->sni_authmode
7333 : ssl->conf->authmode;
7334#else
7335 const int authmode = ssl->conf->authmode;
7336#endif
7337 void *rs_ctx = NULL;
7338 mbedtls_x509_crt *chain = NULL;
7339
7340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7341
7342 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7343 if( crt_expected == SSL_CERTIFICATE_SKIP )
7344 {
7345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7346 goto exit;
7347 }
7348
7349#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7350 if( ssl->handshake->ecrs_enabled &&
7351 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7352 {
7353 chain = ssl->handshake->ecrs_peer_cert;
7354 ssl->handshake->ecrs_peer_cert = NULL;
7355 goto crt_verify;
7356 }
7357#endif
7358
7359 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7360 {
7361 /* mbedtls_ssl_read_record may have sent an alert already. We
7362 let it decide whether to alert. */
7363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7364 goto exit;
7365 }
7366
7367#if defined(MBEDTLS_SSL_SRV_C)
7368 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7369 {
7370 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7371
7372 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7373 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7374
7375 goto exit;
7376 }
7377#endif /* MBEDTLS_SSL_SRV_C */
7378
7379 /* Clear existing peer CRT structure in case we tried to
7380 * reuse a session but it failed, and allocate a new one. */
7381 ssl_clear_peer_cert( ssl->session_negotiate );
7382
7383 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7384 if( chain == NULL )
7385 {
7386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7387 sizeof( mbedtls_x509_crt ) ) );
7388 mbedtls_ssl_send_alert_message( ssl,
7389 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7390 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7391
7392 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7393 goto exit;
7394 }
7395 mbedtls_x509_crt_init( chain );
7396
7397 ret = ssl_parse_certificate_chain( ssl, chain );
7398 if( ret != 0 )
7399 goto exit;
7400
7401#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7402 if( ssl->handshake->ecrs_enabled)
7403 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7404
7405crt_verify:
7406 if( ssl->handshake->ecrs_enabled)
7407 rs_ctx = &ssl->handshake->ecrs_ctx;
7408#endif
7409
7410 ret = ssl_parse_certificate_verify( ssl, authmode,
7411 chain, rs_ctx );
7412 if( ret != 0 )
7413 goto exit;
7414
7415#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7416 {
7417 unsigned char *crt_start, *pk_start;
7418 size_t crt_len, pk_len;
7419
7420 /* We parse the CRT chain without copying, so
7421 * these pointers point into the input buffer,
7422 * and are hence still valid after freeing the
7423 * CRT chain. */
7424
7425 crt_start = chain->raw.p;
7426 crt_len = chain->raw.len;
7427
7428 pk_start = chain->pk_raw.p;
7429 pk_len = chain->pk_raw.len;
7430
7431 /* Free the CRT structures before computing
7432 * digest and copying the peer's public key. */
7433 mbedtls_x509_crt_free( chain );
7434 mbedtls_free( chain );
7435 chain = NULL;
7436
7437 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7438 if( ret != 0 )
7439 goto exit;
7440
7441 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7442 if( ret != 0 )
7443 goto exit;
7444 }
7445#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7446 /* Pass ownership to session structure. */
7447 ssl->session_negotiate->peer_cert = chain;
7448 chain = NULL;
7449#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7450
7451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7452
7453exit:
7454
7455 if( ret == 0 )
7456 ssl->state++;
7457
7458#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7459 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7460 {
7461 ssl->handshake->ecrs_peer_cert = chain;
7462 chain = NULL;
7463 }
7464#endif
7465
7466 if( chain != NULL )
7467 {
7468 mbedtls_x509_crt_free( chain );
7469 mbedtls_free( chain );
7470 }
7471
7472 return( ret );
7473}
7474#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7475
Andrzej Kurek25f27152022-08-17 16:09:31 -04007476#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007477static void ssl_calc_finished_tls_sha256(
7478 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7479{
7480 int len = 12;
7481 const char *sender;
7482 unsigned char padbuf[32];
7483#if defined(MBEDTLS_USE_PSA_CRYPTO)
7484 size_t hash_size;
7485 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7486 psa_status_t status;
7487#else
7488 mbedtls_sha256_context sha256;
7489#endif
7490
7491 mbedtls_ssl_session *session = ssl->session_negotiate;
7492 if( !session )
7493 session = ssl->session;
7494
7495 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7496 ? "client finished"
7497 : "server finished";
7498
7499#if defined(MBEDTLS_USE_PSA_CRYPTO)
7500 sha256_psa = psa_hash_operation_init();
7501
7502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7503
7504 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7505 if( status != PSA_SUCCESS )
7506 {
7507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7508 return;
7509 }
7510
7511 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7512 if( status != PSA_SUCCESS )
7513 {
7514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7515 return;
7516 }
7517 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7518#else
7519
7520 mbedtls_sha256_init( &sha256 );
7521
7522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7523
7524 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7525
7526 /*
7527 * TLSv1.2:
7528 * hash = PRF( master, finished_label,
7529 * Hash( handshake ) )[0.11]
7530 */
7531
7532#if !defined(MBEDTLS_SHA256_ALT)
7533 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7534 sha256.state, sizeof( sha256.state ) );
7535#endif
7536
7537 mbedtls_sha256_finish( &sha256, padbuf );
7538 mbedtls_sha256_free( &sha256 );
7539#endif /* MBEDTLS_USE_PSA_CRYPTO */
7540
7541 ssl->handshake->tls_prf( session->master, 48, sender,
7542 padbuf, 32, buf, len );
7543
7544 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7545
7546 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7547
7548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7549}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007550#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007551
Jerry Yub7ba49e2022-02-17 14:25:53 +08007552
Andrzej Kurek25f27152022-08-17 16:09:31 -04007553#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007554static void ssl_calc_finished_tls_sha384(
7555 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7556{
7557 int len = 12;
7558 const char *sender;
7559 unsigned char padbuf[48];
7560#if defined(MBEDTLS_USE_PSA_CRYPTO)
7561 size_t hash_size;
7562 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7563 psa_status_t status;
7564#else
7565 mbedtls_sha512_context sha512;
7566#endif
7567
7568 mbedtls_ssl_session *session = ssl->session_negotiate;
7569 if( !session )
7570 session = ssl->session;
7571
7572 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7573 ? "client finished"
7574 : "server finished";
7575
7576#if defined(MBEDTLS_USE_PSA_CRYPTO)
7577 sha384_psa = psa_hash_operation_init();
7578
7579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7580
7581 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7582 if( status != PSA_SUCCESS )
7583 {
7584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7585 return;
7586 }
7587
7588 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7589 if( status != PSA_SUCCESS )
7590 {
7591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7592 return;
7593 }
7594 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7595#else
7596 mbedtls_sha512_init( &sha512 );
7597
7598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7599
Andrzej Kureka242e832022-08-11 10:03:14 -04007600 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007601
7602 /*
7603 * TLSv1.2:
7604 * hash = PRF( master, finished_label,
7605 * Hash( handshake ) )[0.11]
7606 */
7607
7608#if !defined(MBEDTLS_SHA512_ALT)
7609 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7610 sha512.state, sizeof( sha512.state ) );
7611#endif
7612 mbedtls_sha512_finish( &sha512, padbuf );
7613
7614 mbedtls_sha512_free( &sha512 );
7615#endif
7616
7617 ssl->handshake->tls_prf( session->master, 48, sender,
7618 padbuf, 48, buf, len );
7619
7620 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7621
7622 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7623
7624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7625}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007626#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007627
Jerry Yuaef00152022-02-17 14:27:31 +08007628void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7629{
7630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7631
7632 /*
7633 * Free our handshake params
7634 */
7635 mbedtls_ssl_handshake_free( ssl );
7636 mbedtls_free( ssl->handshake );
7637 ssl->handshake = NULL;
7638
7639 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007640 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007641 */
7642 if( ssl->transform )
7643 {
7644 mbedtls_ssl_transform_free( ssl->transform );
7645 mbedtls_free( ssl->transform );
7646 }
7647 ssl->transform = ssl->transform_negotiate;
7648 ssl->transform_negotiate = NULL;
7649
7650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7651}
7652
Jerry Yu2a9fff52022-02-17 14:28:51 +08007653void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7654{
7655 int resume = ssl->handshake->resume;
7656
7657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7658
7659#if defined(MBEDTLS_SSL_RENEGOTIATION)
7660 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7661 {
7662 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7663 ssl->renego_records_seen = 0;
7664 }
7665#endif
7666
7667 /*
7668 * Free the previous session and switch in the current one
7669 */
7670 if( ssl->session )
7671 {
7672#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7673 /* RFC 7366 3.1: keep the EtM state */
7674 ssl->session_negotiate->encrypt_then_mac =
7675 ssl->session->encrypt_then_mac;
7676#endif
7677
7678 mbedtls_ssl_session_free( ssl->session );
7679 mbedtls_free( ssl->session );
7680 }
7681 ssl->session = ssl->session_negotiate;
7682 ssl->session_negotiate = NULL;
7683
7684 /*
7685 * Add cache entry
7686 */
7687 if( ssl->conf->f_set_cache != NULL &&
7688 ssl->session->id_len != 0 &&
7689 resume == 0 )
7690 {
7691 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7692 ssl->session->id,
7693 ssl->session->id_len,
7694 ssl->session ) != 0 )
7695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7696 }
7697
7698#if defined(MBEDTLS_SSL_PROTO_DTLS)
7699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7700 ssl->handshake->flight != NULL )
7701 {
7702 /* Cancel handshake timer */
7703 mbedtls_ssl_set_timer( ssl, 0 );
7704
7705 /* Keep last flight around in case we need to resend it:
7706 * we need the handshake and transform structures for that */
7707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7708 }
7709 else
7710#endif
7711 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7712
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007713 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007714
7715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7716}
7717
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007718int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7719{
7720 int ret, hash_len;
7721
7722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7723
7724 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7725
7726 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7727
7728 /*
7729 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7730 * may define some other value. Currently (early 2016), no defined
7731 * ciphersuite does this (and this is unlikely to change as activity has
7732 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7733 */
7734 hash_len = 12;
7735
7736#if defined(MBEDTLS_SSL_RENEGOTIATION)
7737 ssl->verify_data_len = hash_len;
7738 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7739#endif
7740
7741 ssl->out_msglen = 4 + hash_len;
7742 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7743 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7744
7745 /*
7746 * In case of session resuming, invert the client and server
7747 * ChangeCipherSpec messages order.
7748 */
7749 if( ssl->handshake->resume != 0 )
7750 {
7751#if defined(MBEDTLS_SSL_CLI_C)
7752 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7753 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7754#endif
7755#if defined(MBEDTLS_SSL_SRV_C)
7756 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7757 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7758#endif
7759 }
7760 else
7761 ssl->state++;
7762
7763 /*
7764 * Switch to our negotiated transform and session parameters for outbound
7765 * data.
7766 */
7767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7768
7769#if defined(MBEDTLS_SSL_PROTO_DTLS)
7770 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7771 {
7772 unsigned char i;
7773
7774 /* Remember current epoch settings for resending */
7775 ssl->handshake->alt_transform_out = ssl->transform_out;
7776 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7777 sizeof( ssl->handshake->alt_out_ctr ) );
7778
7779 /* Set sequence_number to zero */
7780 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7781
7782
7783 /* Increment epoch */
7784 for( i = 2; i > 0; i-- )
7785 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7786 break;
7787
7788 /* The loop goes to its end iff the counter is wrapping */
7789 if( i == 0 )
7790 {
7791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7792 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7793 }
7794 }
7795 else
7796#endif /* MBEDTLS_SSL_PROTO_DTLS */
7797 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7798
7799 ssl->transform_out = ssl->transform_negotiate;
7800 ssl->session_out = ssl->session_negotiate;
7801
7802#if defined(MBEDTLS_SSL_PROTO_DTLS)
7803 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7804 mbedtls_ssl_send_flight_completed( ssl );
7805#endif
7806
7807 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7808 {
7809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7810 return( ret );
7811 }
7812
7813#if defined(MBEDTLS_SSL_PROTO_DTLS)
7814 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7815 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7816 {
7817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7818 return( ret );
7819 }
7820#endif
7821
7822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7823
7824 return( 0 );
7825}
7826
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007827#define SSL_MAX_HASH_LEN 12
7828
7829int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7830{
7831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7832 unsigned int hash_len = 12;
7833 unsigned char buf[SSL_MAX_HASH_LEN];
7834
7835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7836
7837 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7838
7839 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7840 {
7841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7842 goto exit;
7843 }
7844
7845 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7846 {
7847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7849 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7850 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7851 goto exit;
7852 }
7853
7854 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7855 {
7856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7857 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7858 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7859 goto exit;
7860 }
7861
7862 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7863 {
7864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7866 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7867 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7868 goto exit;
7869 }
7870
7871 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7872 buf, hash_len ) != 0 )
7873 {
7874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7875 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7876 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7877 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7878 goto exit;
7879 }
7880
7881#if defined(MBEDTLS_SSL_RENEGOTIATION)
7882 ssl->verify_data_len = hash_len;
7883 memcpy( ssl->peer_verify_data, buf, hash_len );
7884#endif
7885
7886 if( ssl->handshake->resume != 0 )
7887 {
7888#if defined(MBEDTLS_SSL_CLI_C)
7889 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7890 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7891#endif
7892#if defined(MBEDTLS_SSL_SRV_C)
7893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7894 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7895#endif
7896 }
7897 else
7898 ssl->state++;
7899
7900#if defined(MBEDTLS_SSL_PROTO_DTLS)
7901 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7902 mbedtls_ssl_recv_flight_completed( ssl );
7903#endif
7904
7905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7906
7907exit:
7908 mbedtls_platform_zeroize( buf, hash_len );
7909 return( ret );
7910}
7911
Jerry Yu392112c2022-02-17 14:34:10 +08007912#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7913/*
7914 * Helper to get TLS 1.2 PRF from ciphersuite
7915 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7916 */
7917static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7918{
Jerry Yu392112c2022-02-17 14:34:10 +08007919 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007920 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7921#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007922 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007923 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007924 else
Jerry Yu392112c2022-02-17 14:34:10 +08007925#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007926#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7927 {
7928 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7929 return( tls_prf_sha256 );
7930 }
7931#endif
7932#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7933 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7934 (void) ciphersuite_info;
7935#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007936
Andrzej Kurek894edde2022-09-29 06:31:14 -04007937 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007938}
7939#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007940
7941static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7942{
7943 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007944#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007945 if( tls_prf == tls_prf_sha384 )
7946 {
7947 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7948 }
7949 else
7950#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007951#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007952 if( tls_prf == tls_prf_sha256 )
7953 {
7954 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7955 }
7956 else
7957#endif
7958 return( MBEDTLS_SSL_TLS_PRF_NONE );
7959}
7960
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007961/*
7962 * Populate a transform structure with session keys and all the other
7963 * necessary information.
7964 *
7965 * Parameters:
7966 * - [in/out]: transform: structure to populate
7967 * [in] must be just initialised with mbedtls_ssl_transform_init()
7968 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7969 * - [in] ciphersuite
7970 * - [in] master
7971 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007972 * - [in] tls_prf: pointer to PRF to use for key derivation
7973 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007974 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007975 * - [in] endpoint: client or server
7976 * - [in] ssl: used for:
7977 * - ssl->conf->{f,p}_export_keys
7978 * [in] optionally used for:
7979 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7980 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007981MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007982static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7983 int ciphersuite,
7984 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007985#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007986 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007987#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007988 ssl_tls_prf_t tls_prf,
7989 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007990 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007991 unsigned endpoint,
7992 const mbedtls_ssl_context *ssl )
7993{
7994 int ret = 0;
7995 unsigned char keyblk[256];
7996 unsigned char *key1;
7997 unsigned char *key2;
7998 unsigned char *mac_enc;
7999 unsigned char *mac_dec;
8000 size_t mac_key_len = 0;
8001 size_t iv_copy_len;
8002 size_t keylen;
8003 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008004 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008005#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008006 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008007 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008008#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008009
8010#if defined(MBEDTLS_USE_PSA_CRYPTO)
8011 psa_key_type_t key_type;
8012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8013 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008014 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008015 size_t key_bits;
8016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8017#endif
8018
8019#if !defined(MBEDTLS_DEBUG_C) && \
8020 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8021 if( ssl->f_export_keys == NULL )
8022 {
8023 ssl = NULL; /* make sure we don't use it except for these cases */
8024 (void) ssl;
8025 }
8026#endif
8027
8028 /*
8029 * Some data just needs copying into the structure
8030 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008031#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008032 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008033#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008034 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008035
8036#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8037 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8038#endif
8039
8040#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008041 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008042 {
8043 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8044 * generation separate. This should never happen. */
8045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8046 }
8047#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8048
8049 /*
8050 * Get various info structures
8051 */
8052 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8053 if( ciphersuite_info == NULL )
8054 {
8055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8056 ciphersuite ) );
8057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8058 }
8059
Neil Armstrongab555e02022-04-04 11:07:59 +02008060 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008061#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008062 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008063#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008064 ciphersuite_info );
8065
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008066 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8067 transform->taglen =
8068 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8069
8070#if defined(MBEDTLS_USE_PSA_CRYPTO)
8071 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8072 transform->taglen,
8073 &alg,
8074 &key_type,
8075 &key_bits ) ) != PSA_SUCCESS )
8076 {
8077 ret = psa_ssl_status_to_mbedtls( status );
8078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8079 goto end;
8080 }
8081#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008082 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8083 if( cipher_info == NULL )
8084 {
8085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8086 ciphersuite_info->cipher ) );
8087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8088 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008089#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008090
Neil Armstronge4512952022-03-08 09:08:22 +01008091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008092 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008093 if( mac_alg == 0 )
8094 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008096 (unsigned) ciphersuite_info->mac ) );
8097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8098 }
8099#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008100 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8101 if( md_info == NULL )
8102 {
8103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8104 (unsigned) ciphersuite_info->mac ) );
8105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8106 }
Neil Armstronge4512952022-03-08 09:08:22 +01008107#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008108
8109#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8110 /* Copy own and peer's CID if the use of the CID
8111 * extension has been negotiated. */
8112 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8113 {
8114 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8115
8116 transform->in_cid_len = ssl->own_cid_len;
8117 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8118 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8119 transform->in_cid_len );
8120
8121 transform->out_cid_len = ssl->handshake->peer_cid_len;
8122 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8123 ssl->handshake->peer_cid_len );
8124 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8125 transform->out_cid_len );
8126 }
8127#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8128
8129 /*
8130 * Compute key block using the PRF
8131 */
8132 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8133 if( ret != 0 )
8134 {
8135 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8136 return( ret );
8137 }
8138
8139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8140 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8141 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8142 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8143 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8144
8145 /*
8146 * Determine the appropriate key, IV and MAC length.
8147 */
8148
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008149#if defined(MBEDTLS_USE_PSA_CRYPTO)
8150 keylen = PSA_BITS_TO_BYTES(key_bits);
8151#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008152 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008153#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008154
8155#if defined(MBEDTLS_GCM_C) || \
8156 defined(MBEDTLS_CCM_C) || \
8157 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008158 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008159 {
8160 size_t explicit_ivlen;
8161
8162 transform->maclen = 0;
8163 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008164
8165 /* All modes haves 96-bit IVs, but the length of the static parts vary
8166 * with mode and version:
8167 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8168 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8169 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8170 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8171 * sequence number).
8172 */
8173 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008174
8175 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008176#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008177 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008178#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008179 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8180 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008181#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008182
8183 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008184 transform->fixed_ivlen = 12;
8185 else
8186 transform->fixed_ivlen = 4;
8187
8188 /* Minimum length of encrypted record */
8189 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8190 transform->minlen = explicit_ivlen + transform->taglen;
8191 }
8192 else
8193#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8194#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008195 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8196 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8197 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008198 {
Neil Armstronge4512952022-03-08 09:08:22 +01008199#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008200 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008201#else
8202 size_t block_size = cipher_info->block_size;
8203#endif /* MBEDTLS_USE_PSA_CRYPTO */
8204
8205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008206 /* Get MAC length */
8207 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8208#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209 /* Initialize HMAC contexts */
8210 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8211 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8212 {
8213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8214 goto end;
8215 }
8216
8217 /* Get MAC length */
8218 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008219#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008220 transform->maclen = mac_key_len;
8221
8222 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008224 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008225#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008226 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008227#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008228
8229 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008230 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008231 transform->minlen = transform->maclen;
8232 else
8233 {
8234 /*
8235 * GenericBlockCipher:
8236 * 1. if EtM is in use: one block plus MAC
8237 * otherwise: * first multiple of blocklen greater than maclen
8238 * 2. IV
8239 */
8240#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008241 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008242 {
8243 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008244 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008245 }
8246 else
8247#endif
8248 {
8249 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008250 + block_size
8251 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008252 }
8253
Glenn Strauss07c64162022-03-14 12:34:51 -04008254 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008255 {
8256 transform->minlen += transform->ivlen;
8257 }
8258 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008259 {
8260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8261 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8262 goto end;
8263 }
8264 }
8265 }
8266 else
8267#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8268 {
8269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8271 }
8272
8273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8274 (unsigned) keylen,
8275 (unsigned) transform->minlen,
8276 (unsigned) transform->ivlen,
8277 (unsigned) transform->maclen ) );
8278
8279 /*
8280 * Finally setup the cipher contexts, IVs and MAC secrets.
8281 */
8282#if defined(MBEDTLS_SSL_CLI_C)
8283 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8284 {
8285 key1 = keyblk + mac_key_len * 2;
8286 key2 = keyblk + mac_key_len * 2 + keylen;
8287
8288 mac_enc = keyblk;
8289 mac_dec = keyblk + mac_key_len;
8290
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008291 iv_copy_len = ( transform->fixed_ivlen ) ?
8292 transform->fixed_ivlen : transform->ivlen;
8293 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8294 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8295 iv_copy_len );
8296 }
8297 else
8298#endif /* MBEDTLS_SSL_CLI_C */
8299#if defined(MBEDTLS_SSL_SRV_C)
8300 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8301 {
8302 key1 = keyblk + mac_key_len * 2 + keylen;
8303 key2 = keyblk + mac_key_len * 2;
8304
8305 mac_enc = keyblk + mac_key_len;
8306 mac_dec = keyblk;
8307
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008308 iv_copy_len = ( transform->fixed_ivlen ) ?
8309 transform->fixed_ivlen : transform->ivlen;
8310 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8311 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8312 iv_copy_len );
8313 }
8314 else
8315#endif /* MBEDTLS_SSL_SRV_C */
8316 {
8317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8318 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8319 goto end;
8320 }
8321
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008322 if( ssl != NULL && ssl->f_export_keys != NULL )
8323 {
8324 ssl->f_export_keys( ssl->p_export_keys,
8325 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8326 master, 48,
8327 randbytes + 32,
8328 randbytes,
8329 tls_prf_get_type( tls_prf ) );
8330 }
8331
8332#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008333 transform->psa_alg = alg;
8334
8335 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8336 {
8337 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8338 psa_set_key_algorithm( &attributes, alg );
8339 psa_set_key_type( &attributes, key_type );
8340
8341 if( ( status = psa_import_key( &attributes,
8342 key1,
8343 PSA_BITS_TO_BYTES( key_bits ),
8344 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8345 {
8346 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8347 ret = psa_ssl_status_to_mbedtls( status );
8348 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8349 goto end;
8350 }
8351
8352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8353
8354 if( ( status = psa_import_key( &attributes,
8355 key2,
8356 PSA_BITS_TO_BYTES( key_bits ),
8357 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8358 {
8359 ret = psa_ssl_status_to_mbedtls( status );
8360 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8361 goto end;
8362 }
8363 }
8364#else
8365 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8366 cipher_info ) ) != 0 )
8367 {
8368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8369 goto end;
8370 }
8371
8372 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8373 cipher_info ) ) != 0 )
8374 {
8375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8376 goto end;
8377 }
8378
8379 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8380 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8381 MBEDTLS_ENCRYPT ) ) != 0 )
8382 {
8383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8384 goto end;
8385 }
8386
8387 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8388 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8389 MBEDTLS_DECRYPT ) ) != 0 )
8390 {
8391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8392 goto end;
8393 }
8394
8395#if defined(MBEDTLS_CIPHER_MODE_CBC)
8396 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8397 {
8398 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8399 MBEDTLS_PADDING_NONE ) ) != 0 )
8400 {
8401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8402 goto end;
8403 }
8404
8405 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8406 MBEDTLS_PADDING_NONE ) ) != 0 )
8407 {
8408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8409 goto end;
8410 }
8411 }
8412#endif /* MBEDTLS_CIPHER_MODE_CBC */
8413#endif /* MBEDTLS_USE_PSA_CRYPTO */
8414
Neil Armstrong29c0c042022-03-17 17:47:28 +01008415#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8416 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8417 For AEAD-based ciphersuites, there is nothing to do here. */
8418 if( mac_key_len != 0 )
8419 {
8420#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008421 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008422
8423 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008424 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008425 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8426
8427 if( ( status = psa_import_key( &attributes,
8428 mac_enc, mac_key_len,
8429 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8430 {
8431 ret = psa_ssl_status_to_mbedtls( status );
8432 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8433 goto end;
8434 }
8435
Ronald Cronfb39f152022-03-25 14:36:28 +01008436 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008437 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8438#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8439 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8440#endif
8441 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008442 /* mbedtls_ct_hmac() requires the key to be exportable */
8443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8444 PSA_KEY_USAGE_VERIFY_HASH );
8445 else
8446 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8447
8448 if( ( status = psa_import_key( &attributes,
8449 mac_dec, mac_key_len,
8450 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8451 {
8452 ret = psa_ssl_status_to_mbedtls( status );
8453 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8454 goto end;
8455 }
8456#else
8457 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8458 if( ret != 0 )
8459 goto end;
8460 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8461 if( ret != 0 )
8462 goto end;
8463#endif /* MBEDTLS_USE_PSA_CRYPTO */
8464 }
8465#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8466
8467 ((void) mac_dec);
8468 ((void) mac_enc);
8469
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008470end:
8471 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8472 return( ret );
8473}
8474
Valerio Settia08b1a42022-11-17 15:10:02 +01008475#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8476 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008477int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008478 psa_pake_operation_t *pake_ctx,
8479 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008480 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008481{
8482 psa_status_t status;
8483 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008484 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008485 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8486 * At round two perform a single cycle
8487 */
8488 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008489
Valerio Setti6b3dab02022-11-17 17:14:54 +01008490 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008491 {
8492 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8493 step <= PSA_PAKE_STEP_ZK_PROOF;
8494 ++step )
8495 {
8496 /* Length is stored at the first byte */
8497 size_t length = buf[input_offset];
8498 input_offset += 1;
8499
8500 if( input_offset + length > len )
8501 {
8502 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8503 }
8504
8505 status = psa_pake_input( pake_ctx, step,
8506 buf + input_offset, length );
8507 if( status != PSA_SUCCESS)
8508 {
8509 return psa_ssl_status_to_mbedtls( status );
8510 }
8511
8512 input_offset += length;
8513 }
8514 }
8515
Valerio Setti819de862022-11-17 18:05:19 +01008516 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008517 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008518
Valerio Settia08b1a42022-11-17 15:10:02 +01008519 return( 0 );
8520}
8521
Valerio Setti6b3dab02022-11-17 17:14:54 +01008522int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008523 psa_pake_operation_t *pake_ctx,
8524 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008525 size_t len, size_t *olen,
8526 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008527{
8528 psa_status_t status;
8529 size_t output_offset = 0;
8530 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008531 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008532 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8533 * At round two perform a single cycle
8534 */
8535 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008536
Valerio Setti6b3dab02022-11-17 17:14:54 +01008537 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008538 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008539 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8540 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008541 ++step )
8542 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008543 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008544 * For each step, prepend 1 byte with the length of the data as
8545 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008546 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008547 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008548 buf + output_offset + 1,
8549 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008550 &output_len );
8551 if( status != PSA_SUCCESS )
8552 {
8553 return( psa_ssl_status_to_mbedtls( status ) );
8554 }
8555
Valerio Setti99d88c12022-11-22 16:03:43 +01008556 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008557
8558 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008559 }
8560 }
8561
8562 *olen = output_offset;
8563
8564 return( 0 );
8565}
Valerio Settia08b1a42022-11-17 15:10:02 +01008566#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8567
Jerry Yuee40f9d2022-02-17 14:55:16 +08008568#if defined(MBEDTLS_USE_PSA_CRYPTO)
8569int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8570 unsigned char *hash, size_t *hashlen,
8571 unsigned char *data, size_t data_len,
8572 mbedtls_md_type_t md_alg )
8573{
8574 psa_status_t status;
8575 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008576 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008577
8578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8579
8580 if( ( status = psa_hash_setup( &hash_operation,
8581 hash_alg ) ) != PSA_SUCCESS )
8582 {
8583 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8584 goto exit;
8585 }
8586
8587 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8588 64 ) ) != PSA_SUCCESS )
8589 {
8590 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8591 goto exit;
8592 }
8593
8594 if( ( status = psa_hash_update( &hash_operation,
8595 data, data_len ) ) != PSA_SUCCESS )
8596 {
8597 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8598 goto exit;
8599 }
8600
8601 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8602 hashlen ) ) != PSA_SUCCESS )
8603 {
8604 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8605 goto exit;
8606 }
8607
8608exit:
8609 if( status != PSA_SUCCESS )
8610 {
8611 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8612 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8613 switch( status )
8614 {
8615 case PSA_ERROR_NOT_SUPPORTED:
8616 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8617 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8618 case PSA_ERROR_BUFFER_TOO_SMALL:
8619 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8620 case PSA_ERROR_INSUFFICIENT_MEMORY:
8621 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8622 default:
8623 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8624 }
8625 }
8626 return( 0 );
8627}
8628
8629#else
8630
8631int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8632 unsigned char *hash, size_t *hashlen,
8633 unsigned char *data, size_t data_len,
8634 mbedtls_md_type_t md_alg )
8635{
8636 int ret = 0;
8637 mbedtls_md_context_t ctx;
8638 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8639 *hashlen = mbedtls_md_get_size( md_info );
8640
8641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8642
8643 mbedtls_md_init( &ctx );
8644
8645 /*
8646 * digitally-signed struct {
8647 * opaque client_random[32];
8648 * opaque server_random[32];
8649 * ServerDHParams params;
8650 * };
8651 */
8652 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8653 {
8654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8655 goto exit;
8656 }
8657 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8658 {
8659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8660 goto exit;
8661 }
8662 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8663 {
8664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8665 goto exit;
8666 }
8667 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8668 {
8669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8670 goto exit;
8671 }
8672 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8673 {
8674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8675 goto exit;
8676 }
8677
8678exit:
8679 mbedtls_md_free( &ctx );
8680
8681 if( ret != 0 )
8682 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8683 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8684
8685 return( ret );
8686}
8687#endif /* MBEDTLS_USE_PSA_CRYPTO */
8688
Jerry Yud9d91da2022-02-17 14:57:06 +08008689#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8690
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008691/* Find the preferred hash for a given signature algorithm. */
8692unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8693 mbedtls_ssl_context *ssl,
8694 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008695{
Gabor Mezei078e8032022-04-27 21:17:56 +02008696 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008697 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008698
8699 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008700 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008701
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008702 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008703 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008704 unsigned int hash_alg_received =
8705 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8706 received_sig_algs[i] );
8707 unsigned int sig_alg_received =
8708 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8709 received_sig_algs[i] );
8710
8711 if( sig_alg == sig_alg_received )
8712 {
8713#if defined(MBEDTLS_USE_PSA_CRYPTO)
8714 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8715 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008716 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008717 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008718
Neil Armstrong96eceb82022-06-30 18:05:05 +02008719 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8720 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8721 PSA_ALG_ECDSA( psa_hash_alg ),
8722 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008723 continue;
8724
Neil Armstrong96eceb82022-06-30 18:05:05 +02008725 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008726 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8727 PSA_ALG_RSA_PKCS1V15_SIGN(
8728 psa_hash_alg ),
8729 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008730 continue;
8731 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008732#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008733
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008734 return( hash_alg_received );
8735 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008736 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008737
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008738 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008739}
8740
8741#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8742
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008743/* Serialization of TLS 1.2 sessions:
8744 *
8745 * struct {
8746 * uint64 start_time;
8747 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008748 * uint8 session_id_len; // at most 32
8749 * opaque session_id[32];
8750 * opaque master[48]; // fixed length in the standard
8751 * uint32 verify_result;
8752 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8753 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8754 * uint32 ticket_lifetime;
8755 * uint8 mfl_code; // up to 255 according to standard
8756 * uint8 encrypt_then_mac; // 0 or 1
8757 * } serialized_session_tls12;
8758 *
8759 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008760static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008761 unsigned char *buf,
8762 size_t buf_len )
8763{
8764 unsigned char *p = buf;
8765 size_t used = 0;
8766
8767#if defined(MBEDTLS_HAVE_TIME)
8768 uint64_t start;
8769#endif
8770#if defined(MBEDTLS_X509_CRT_PARSE_C)
8771#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8772 size_t cert_len;
8773#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8774#endif /* MBEDTLS_X509_CRT_PARSE_C */
8775
8776 /*
8777 * Time
8778 */
8779#if defined(MBEDTLS_HAVE_TIME)
8780 used += 8;
8781
8782 if( used <= buf_len )
8783 {
8784 start = (uint64_t) session->start;
8785
8786 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8787 p += 8;
8788 }
8789#endif /* MBEDTLS_HAVE_TIME */
8790
8791 /*
8792 * Basic mandatory fields
8793 */
8794 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008795 + 1 /* id_len */
8796 + sizeof( session->id )
8797 + sizeof( session->master )
8798 + 4; /* verify_result */
8799
8800 if( used <= buf_len )
8801 {
8802 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8803 p += 2;
8804
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008805 *p++ = MBEDTLS_BYTE_0( session->id_len );
8806 memcpy( p, session->id, 32 );
8807 p += 32;
8808
8809 memcpy( p, session->master, 48 );
8810 p += 48;
8811
8812 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8813 p += 4;
8814 }
8815
8816 /*
8817 * Peer's end-entity certificate
8818 */
8819#if defined(MBEDTLS_X509_CRT_PARSE_C)
8820#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8821 if( session->peer_cert == NULL )
8822 cert_len = 0;
8823 else
8824 cert_len = session->peer_cert->raw.len;
8825
8826 used += 3 + cert_len;
8827
8828 if( used <= buf_len )
8829 {
8830 *p++ = MBEDTLS_BYTE_2( cert_len );
8831 *p++ = MBEDTLS_BYTE_1( cert_len );
8832 *p++ = MBEDTLS_BYTE_0( cert_len );
8833
8834 if( session->peer_cert != NULL )
8835 {
8836 memcpy( p, session->peer_cert->raw.p, cert_len );
8837 p += cert_len;
8838 }
8839 }
8840#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8841 if( session->peer_cert_digest != NULL )
8842 {
8843 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8844 if( used <= buf_len )
8845 {
8846 *p++ = (unsigned char) session->peer_cert_digest_type;
8847 *p++ = (unsigned char) session->peer_cert_digest_len;
8848 memcpy( p, session->peer_cert_digest,
8849 session->peer_cert_digest_len );
8850 p += session->peer_cert_digest_len;
8851 }
8852 }
8853 else
8854 {
8855 used += 2;
8856 if( used <= buf_len )
8857 {
8858 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8859 *p++ = 0;
8860 }
8861 }
8862#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8863#endif /* MBEDTLS_X509_CRT_PARSE_C */
8864
8865 /*
8866 * Session ticket if any, plus associated data
8867 */
8868#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8869 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8870
8871 if( used <= buf_len )
8872 {
8873 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8874 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8875 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8876
8877 if( session->ticket != NULL )
8878 {
8879 memcpy( p, session->ticket, session->ticket_len );
8880 p += session->ticket_len;
8881 }
8882
8883 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8884 p += 4;
8885 }
8886#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8887
8888 /*
8889 * Misc extension-related info
8890 */
8891#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8892 used += 1;
8893
8894 if( used <= buf_len )
8895 *p++ = session->mfl_code;
8896#endif
8897
8898#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8899 used += 1;
8900
8901 if( used <= buf_len )
8902 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8903#endif
8904
8905 return( used );
8906}
8907
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008908MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008909static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008910 const unsigned char *buf,
8911 size_t len )
8912{
8913#if defined(MBEDTLS_HAVE_TIME)
8914 uint64_t start;
8915#endif
8916#if defined(MBEDTLS_X509_CRT_PARSE_C)
8917#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8918 size_t cert_len;
8919#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8920#endif /* MBEDTLS_X509_CRT_PARSE_C */
8921
8922 const unsigned char *p = buf;
8923 const unsigned char * const end = buf + len;
8924
8925 /*
8926 * Time
8927 */
8928#if defined(MBEDTLS_HAVE_TIME)
8929 if( 8 > (size_t)( end - p ) )
8930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8931
8932 start = ( (uint64_t) p[0] << 56 ) |
8933 ( (uint64_t) p[1] << 48 ) |
8934 ( (uint64_t) p[2] << 40 ) |
8935 ( (uint64_t) p[3] << 32 ) |
8936 ( (uint64_t) p[4] << 24 ) |
8937 ( (uint64_t) p[5] << 16 ) |
8938 ( (uint64_t) p[6] << 8 ) |
8939 ( (uint64_t) p[7] );
8940 p += 8;
8941
8942 session->start = (time_t) start;
8943#endif /* MBEDTLS_HAVE_TIME */
8944
8945 /*
8946 * Basic mandatory fields
8947 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008948 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8950
8951 session->ciphersuite = ( p[0] << 8 ) | p[1];
8952 p += 2;
8953
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008954 session->id_len = *p++;
8955 memcpy( session->id, p, 32 );
8956 p += 32;
8957
8958 memcpy( session->master, p, 48 );
8959 p += 48;
8960
8961 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8962 ( (uint32_t) p[1] << 16 ) |
8963 ( (uint32_t) p[2] << 8 ) |
8964 ( (uint32_t) p[3] );
8965 p += 4;
8966
8967 /* Immediately clear invalid pointer values that have been read, in case
8968 * we exit early before we replaced them with valid ones. */
8969#if defined(MBEDTLS_X509_CRT_PARSE_C)
8970#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8971 session->peer_cert = NULL;
8972#else
8973 session->peer_cert_digest = NULL;
8974#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8975#endif /* MBEDTLS_X509_CRT_PARSE_C */
8976#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8977 session->ticket = NULL;
8978#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8979
8980 /*
8981 * Peer certificate
8982 */
8983#if defined(MBEDTLS_X509_CRT_PARSE_C)
8984#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8985 /* Deserialize CRT from the end of the ticket. */
8986 if( 3 > (size_t)( end - p ) )
8987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8988
8989 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8990 p += 3;
8991
8992 if( cert_len != 0 )
8993 {
8994 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8995
8996 if( cert_len > (size_t)( end - p ) )
8997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8998
8999 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9000
9001 if( session->peer_cert == NULL )
9002 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9003
9004 mbedtls_x509_crt_init( session->peer_cert );
9005
9006 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9007 p, cert_len ) ) != 0 )
9008 {
9009 mbedtls_x509_crt_free( session->peer_cert );
9010 mbedtls_free( session->peer_cert );
9011 session->peer_cert = NULL;
9012 return( ret );
9013 }
9014
9015 p += cert_len;
9016 }
9017#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9018 /* Deserialize CRT digest from the end of the ticket. */
9019 if( 2 > (size_t)( end - p ) )
9020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9021
9022 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9023 session->peer_cert_digest_len = (size_t) *p++;
9024
9025 if( session->peer_cert_digest_len != 0 )
9026 {
9027 const mbedtls_md_info_t *md_info =
9028 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9029 if( md_info == NULL )
9030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9031 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9033
9034 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9036
9037 session->peer_cert_digest =
9038 mbedtls_calloc( 1, session->peer_cert_digest_len );
9039 if( session->peer_cert_digest == NULL )
9040 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9041
9042 memcpy( session->peer_cert_digest, p,
9043 session->peer_cert_digest_len );
9044 p += session->peer_cert_digest_len;
9045 }
9046#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9047#endif /* MBEDTLS_X509_CRT_PARSE_C */
9048
9049 /*
9050 * Session ticket and associated data
9051 */
9052#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9053 if( 3 > (size_t)( end - p ) )
9054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9055
9056 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9057 p += 3;
9058
9059 if( session->ticket_len != 0 )
9060 {
9061 if( session->ticket_len > (size_t)( end - p ) )
9062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9063
9064 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9065 if( session->ticket == NULL )
9066 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9067
9068 memcpy( session->ticket, p, session->ticket_len );
9069 p += session->ticket_len;
9070 }
9071
9072 if( 4 > (size_t)( end - p ) )
9073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9074
9075 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9076 ( (uint32_t) p[1] << 16 ) |
9077 ( (uint32_t) p[2] << 8 ) |
9078 ( (uint32_t) p[3] );
9079 p += 4;
9080#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9081
9082 /*
9083 * Misc extension-related info
9084 */
9085#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9086 if( 1 > (size_t)( end - p ) )
9087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9088
9089 session->mfl_code = *p++;
9090#endif
9091
9092#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9093 if( 1 > (size_t)( end - p ) )
9094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9095
9096 session->encrypt_then_mac = *p++;
9097#endif
9098
9099 /* Done, should have consumed entire buffer */
9100 if( p != end )
9101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9102
9103 return( 0 );
9104}
Jerry Yudc7bd172022-02-17 13:44:15 +08009105#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9106
XiaokangQian75d40ef2022-04-20 11:05:24 +00009107int mbedtls_ssl_validate_ciphersuite(
9108 const mbedtls_ssl_context *ssl,
9109 const mbedtls_ssl_ciphersuite_t *suite_info,
9110 mbedtls_ssl_protocol_version min_tls_version,
9111 mbedtls_ssl_protocol_version max_tls_version )
9112{
9113 (void) ssl;
9114
9115 if( suite_info == NULL )
9116 return( -1 );
9117
9118 if( ( suite_info->min_tls_version > max_tls_version ) ||
9119 ( suite_info->max_tls_version < min_tls_version ) )
9120 {
9121 return( -1 );
9122 }
9123
XiaokangQian060d8672022-04-21 09:24:56 +00009124#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009125#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009126#if defined(MBEDTLS_USE_PSA_CRYPTO)
9127 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9128 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9129#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009130 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9131 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009132#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009133 {
9134 return( -1 );
9135 }
9136#endif
9137
9138 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9139#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9140 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9141 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9142 {
9143 return( -1 );
9144 }
9145#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9146#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9147
9148 return( 0 );
9149}
9150
Ronald Crone68ab4f2022-10-05 12:46:29 +02009151#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009152/*
9153 * Function for writing a signature algorithm extension.
9154 *
9155 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9156 * value (TLS 1.3 RFC8446):
9157 * enum {
9158 * ....
9159 * ecdsa_secp256r1_sha256( 0x0403 ),
9160 * ecdsa_secp384r1_sha384( 0x0503 ),
9161 * ecdsa_secp521r1_sha512( 0x0603 ),
9162 * ....
9163 * } SignatureScheme;
9164 *
9165 * struct {
9166 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9167 * } SignatureSchemeList;
9168 *
9169 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9170 * value (TLS 1.2 RFC5246):
9171 * enum {
9172 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9173 * sha512(6), (255)
9174 * } HashAlgorithm;
9175 *
9176 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9177 * SignatureAlgorithm;
9178 *
9179 * struct {
9180 * HashAlgorithm hash;
9181 * SignatureAlgorithm signature;
9182 * } SignatureAndHashAlgorithm;
9183 *
9184 * SignatureAndHashAlgorithm
9185 * supported_signature_algorithms<2..2^16-2>;
9186 *
9187 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9188 * generalization of the TLS 1.2 signature algorithm extension.
9189 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9190 * `SignatureScheme` field of TLS 1.3
9191 *
9192 */
9193int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9194 const unsigned char *end, size_t *out_len )
9195{
9196 unsigned char *p = buf;
9197 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9198 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9199
9200 *out_len = 0;
9201
9202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9203
9204 /* Check if we have space for header and length field:
9205 * - extension_type (2 bytes)
9206 * - extension_data_length (2 bytes)
9207 * - supported_signature_algorithms_length (2 bytes)
9208 */
9209 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9210 p += 6;
9211
9212 /*
9213 * Write supported_signature_algorithms
9214 */
9215 supported_sig_alg = p;
9216 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9217 if( sig_alg == NULL )
9218 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9219
9220 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9221 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9223 *sig_alg,
9224 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009225 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9226 continue;
9227 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9228 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9229 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009231 *sig_alg,
9232 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009233 }
9234
9235 /* Length of supported_signature_algorithms */
9236 supported_sig_alg_len = p - supported_sig_alg;
9237 if( supported_sig_alg_len == 0 )
9238 {
9239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9241 }
9242
XiaokangQianeaf36512022-04-24 09:07:44 +00009243 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009244 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009245 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9246
XiaokangQianeaf36512022-04-24 09:07:44 +00009247 *out_len = p - buf;
9248
9249#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009250 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009251#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009252
XiaokangQianeaf36512022-04-24 09:07:44 +00009253 return( 0 );
9254}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009255#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009256
XiaokangQian40a35232022-05-07 09:02:40 +00009257#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009258/*
9259 * mbedtls_ssl_parse_server_name_ext
9260 *
9261 * Structure of server_name extension:
9262 *
9263 * enum {
9264 * host_name(0), (255)
9265 * } NameType;
9266 * opaque HostName<1..2^16-1>;
9267 *
9268 * struct {
9269 * NameType name_type;
9270 * select (name_type) {
9271 * case host_name: HostName;
9272 * } name;
9273 * } ServerName;
9274 * struct {
9275 * ServerName server_name_list<1..2^16-1>
9276 * } ServerNameList;
9277 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009278MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009279int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9280 const unsigned char *buf,
9281 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009282{
9283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9284 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009285 size_t server_name_list_len, hostname_len;
9286 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009287
XiaokangQianf2a94202022-05-20 06:44:24 +00009288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009289
9290 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009291 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009292 p += 2;
9293
XiaokangQian9b2b7712022-05-17 02:57:00 +00009294 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9295 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009296 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009297 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009298 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009299 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009300 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9301 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009302
9303 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9304 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009305 /* sni_name is intended to be used only during the parsing of the
9306 * ClientHello message (it is reset to NULL before the end of
9307 * the message parsing). Thus it is ok to just point to the
9308 * reception buffer and not make a copy of it.
9309 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009310 ssl->handshake->sni_name = p + 3;
9311 ssl->handshake->sni_name_len = hostname_len;
9312 if( ssl->conf->f_sni == NULL )
9313 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009314 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009315 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009316 if( ret != 0 )
9317 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009318 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009319 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9320 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009321 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9322 }
9323 return( 0 );
9324 }
9325
9326 p += hostname_len + 3;
9327 }
9328
9329 return( 0 );
9330}
9331#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9332
XiaokangQianacb39922022-06-17 10:18:48 +00009333#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009334MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009335int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9336 const unsigned char *buf,
9337 const unsigned char *end )
9338{
9339 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009340 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009341 const unsigned char *protocol_name_list;
9342 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009343 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009344
9345 /* If ALPN not configured, just ignore the extension */
9346 if( ssl->conf->alpn_list == NULL )
9347 return( 0 );
9348
9349 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009350 * RFC7301, section 3.1
9351 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009352 *
XiaokangQianc7403452022-06-23 03:24:12 +00009353 * struct {
9354 * ProtocolName protocol_name_list<2..2^16-1>
9355 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009356 */
9357
XiaokangQianc7403452022-06-23 03:24:12 +00009358 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009359 * protocol_name_list_len 2 bytes
9360 * protocol_name_len 1 bytes
9361 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009362 */
XiaokangQianacb39922022-06-17 10:18:48 +00009363 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9364
XiaokangQianc7403452022-06-23 03:24:12 +00009365 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009366 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009367 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009368 protocol_name_list = p;
9369 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009370
9371 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009372 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009373 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009374 protocol_name_len = *p++;
9375 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9376 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009377 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009378 {
9379 MBEDTLS_SSL_PEND_FATAL_ALERT(
9380 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9381 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009382 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009383 }
9384
9385 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009386 }
9387
9388 /* Use our order of preference */
9389 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9390 {
9391 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009392 p = protocol_name_list;
9393 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009394 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009395 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009396 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009397 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009398 {
9399 ssl->alpn_chosen = *alpn;
9400 return( 0 );
9401 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009402
9403 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009404 }
9405 }
9406
XiaokangQian95d5f542022-06-24 02:29:26 +00009407 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009408 MBEDTLS_SSL_PEND_FATAL_ALERT(
9409 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9410 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9411 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9412}
9413
9414int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9415 unsigned char *buf,
9416 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009417 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009418{
9419 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009420 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009421 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009422
9423 if( ssl->alpn_chosen == NULL )
9424 {
9425 return( 0 );
9426 }
9427
XiaokangQian95d5f542022-06-24 02:29:26 +00009428 protocol_name_len = strlen( ssl->alpn_chosen );
9429 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009430
9431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9432 /*
9433 * 0 . 1 ext identifier
9434 * 2 . 3 ext length
9435 * 4 . 5 protocol list length
9436 * 6 . 6 protocol name length
9437 * 7 . 7+n protocol name
9438 */
9439 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9440
XiaokangQian95d5f542022-06-24 02:29:26 +00009441 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009442
XiaokangQian95d5f542022-06-24 02:29:26 +00009443 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9444 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009445 /* Note: the length of the chosen protocol has been checked to be less
9446 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9447 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009448 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009449
XiaokangQian95d5f542022-06-24 02:29:26 +00009450 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009451
9452#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9453 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9454#endif
9455
XiaokangQianacb39922022-06-17 10:18:48 +00009456 return ( 0 );
9457}
9458#endif /* MBEDTLS_SSL_ALPN */
9459
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009460#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009461 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009462 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009463 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009464int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9465 const char *hostname )
9466{
9467 /* Initialize to suppress unnecessary compiler warning */
9468 size_t hostname_len = 0;
9469
9470 /* Check if new hostname is valid before
9471 * making any change to current one */
9472 if( hostname != NULL )
9473 {
9474 hostname_len = strlen( hostname );
9475
9476 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9478 }
9479
9480 /* Now it's clear that we will overwrite the old hostname,
9481 * so we can free it safely */
9482 if( session->hostname != NULL )
9483 {
9484 mbedtls_platform_zeroize( session->hostname,
9485 strlen( session->hostname ) );
9486 mbedtls_free( session->hostname );
9487 }
9488
9489 /* Passing NULL as hostname shall clear the old one */
9490 if( hostname == NULL )
9491 {
9492 session->hostname = NULL;
9493 }
9494 else
9495 {
9496 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9497 if( session->hostname == NULL )
9498 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9499
9500 memcpy( session->hostname, hostname, hostname_len );
9501 }
9502
9503 return( 0 );
9504}
Xiaokang Qian03409292022-10-12 02:49:52 +00009505#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9506 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009507 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009508 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#endif /* MBEDTLS_SSL_TLS_C */