blob: 14ffef20b81f5e5f5f2b6a5d7f7179907b608f9c [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 {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001450#if defined(MBEDTLS_SSL_EARLY_DATA)
XiaokangQian2b01dc32022-01-21 02:53:13 +00001451 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1452 mbedtls_free( ssl->handshake->transform_earlydata );
1453 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001454#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455
1456 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1457 mbedtls_free( ssl->handshake->transform_handshake );
1458 ssl->handshake->transform_handshake = NULL;
1459 }
1460
XiaokangQian2b01dc32022-01-21 02:53:13 +00001461#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001462}
1463
1464int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1465{
1466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1467
1468 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1469
XiaokangQian78b1fa72022-01-19 06:56:30 +00001470 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001471
1472 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_RENEGOTIATION)
1474 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001475 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001476
1477 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1479 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001482
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001484 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001485 if( ssl->session )
1486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 mbedtls_ssl_session_free( ssl->session );
1488 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001489 ssl->session = NULL;
1490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001493 ssl->alpn_chosen = NULL;
1494#endif
1495
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001496#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001497 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001498#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001499 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001500#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001501 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001502 {
1503 mbedtls_free( ssl->cli_id );
1504 ssl->cli_id = NULL;
1505 ssl->cli_id_len = 0;
1506 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001507#endif
1508
Paul Bakker48916f92012-09-16 19:57:18 +00001509 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1510 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001511
1512 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001513}
1514
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001515/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001516 * Reset an initialized and used SSL context for re-use while retaining
1517 * all application-set variables, function pointers and data.
1518 */
1519int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1520{
Hanno Becker43aefe22020-02-05 10:44:56 +00001521 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001522}
1523
1524/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 * SSL set accessors
1526 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001527void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001529 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530}
1531
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001532void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001534 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001538void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001540 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001541}
1542#endif
1543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001544void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001547}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001550
Hanno Becker1841b0a2018-08-24 11:13:57 +01001551void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1552 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001553{
1554 ssl->disable_datagram_packing = !allow_packing;
1555}
1556
1557void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1558 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001559{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001560 conf->hs_timeout_min = min;
1561 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001562}
1563#endif
1564
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001565void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001567 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568}
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001571void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001572 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001573 void *p_vrfy )
1574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001575 conf->f_vrfy = f_vrfy;
1576 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001579
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001580void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001581 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 void *p_rng )
1583{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001584 conf->f_rng = f_rng;
1585 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001586}
1587
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001588void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001589 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 void *p_dbg )
1591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001592 conf->f_dbg = f_dbg;
1593 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001597 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001598 mbedtls_ssl_send_t *f_send,
1599 mbedtls_ssl_recv_t *f_recv,
1600 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001601{
1602 ssl->p_bio = p_bio;
1603 ssl->f_send = f_send;
1604 ssl->f_recv = f_recv;
1605 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001606}
1607
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001608#if defined(MBEDTLS_SSL_PROTO_DTLS)
1609void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1610{
1611 ssl->mtu = mtu;
1612}
1613#endif
1614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001615void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001616{
1617 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001618}
1619
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001620void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1621 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001622 mbedtls_ssl_set_timer_t *f_set_timer,
1623 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001624{
1625 ssl->p_timer = p_timer;
1626 ssl->f_set_timer = f_set_timer;
1627 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001628
1629 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001630 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001631}
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001634void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001635 void *p_cache,
1636 mbedtls_ssl_cache_get_t *f_get_cache,
1637 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001639 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001640 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001641 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_SSL_CLI_C)
1646int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001647{
Janos Follath865b3eb2019-12-16 11:46:15 +00001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001649
1650 if( ssl == NULL ||
1651 session == NULL ||
1652 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001653 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001656 }
1657
Hanno Beckere810bbc2021-05-14 16:01:05 +01001658 if( ssl->handshake->resume == 1 )
1659 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1660
Jerry Yu21092062022-10-10 21:21:31 +08001661#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1662 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001663 {
Jerry Yu21092062022-10-10 21:21:31 +08001664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1665 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1666
1667 if( mbedtls_ssl_validate_ciphersuite(
1668 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1669 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1670 {
1671 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1672 session->ciphersuite ) );
1673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1674 }
Jerry Yu40afab62022-10-08 10:42:13 +08001675 }
Jerry Yu21092062022-10-10 21:21:31 +08001676#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001677
Hanno Becker52055ae2019-02-06 14:30:46 +00001678 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1679 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001680 return( ret );
1681
Paul Bakker0a597072012-09-25 21:55:46 +00001682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001683
1684 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001688void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1689 const int *ciphersuites )
1690{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001691 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692}
1693
Ronald Cron6f135e12021-12-08 16:57:54 +01001694#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001695void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001696 const int kex_modes )
1697{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001698 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001699}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001700
1701#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yucc4e0072022-11-22 17:22:22 +08001702#if defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001703void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001704 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001705{
1706 conf->early_data_enabled = early_data_enabled;
1707}
Jerry Yucc4e0072022-11-22 17:22:22 +08001708#endif /* MBEDTLS_SSL_CLI_C */
1709
1710#if defined(MBEDTLS_SSL_SRV_C)
1711void mbedtls_ssl_tls13_conf_max_early_data_size(
1712 mbedtls_ssl_config *conf, uint32_t max_early_data_size )
1713{
1714 conf->max_early_data_size =
1715 max_early_data_size < MBEDTLS_SSL_MAX_EARLY_DATA_SIZE ?
1716 max_early_data_size : MBEDTLS_SSL_MAX_EARLY_DATA_SIZE;
1717}
1718#endif /* MBEDTLS_SSL_SRV_C */
1719
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001720#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001721#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001724void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001725 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001726{
1727 conf->cert_profile = profile;
1728}
1729
Glenn Strauss36872db2022-01-22 05:06:31 -05001730static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1731{
1732 mbedtls_ssl_key_cert *cur = key_cert, *next;
1733
1734 while( cur != NULL )
1735 {
1736 next = cur->next;
1737 mbedtls_free( cur );
1738 cur = next;
1739 }
1740}
1741
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001742/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001743MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001744static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1745 mbedtls_x509_crt *cert,
1746 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001747{
niisato8ee24222018-06-25 19:05:48 +09001748 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001749
Glenn Strauss36872db2022-01-22 05:06:31 -05001750 if( cert == NULL )
1751 {
1752 /* Free list if cert is null */
1753 ssl_key_cert_free( *head );
1754 *head = NULL;
1755 return( 0 );
1756 }
1757
niisato8ee24222018-06-25 19:05:48 +09001758 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1759 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001760 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001761
niisato8ee24222018-06-25 19:05:48 +09001762 new_cert->cert = cert;
1763 new_cert->key = key;
1764 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001765
Glenn Strauss36872db2022-01-22 05:06:31 -05001766 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001767 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001768 {
niisato8ee24222018-06-25 19:05:48 +09001769 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001770 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001771 else
1772 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001773 mbedtls_ssl_key_cert *cur = *head;
1774 while( cur->next != NULL )
1775 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001776 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001777 }
1778
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001779 return( 0 );
1780}
1781
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001782int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001783 mbedtls_x509_crt *own_cert,
1784 mbedtls_pk_context *pk_key )
1785{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001786 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001787}
1788
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001789void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001790 mbedtls_x509_crt *ca_chain,
1791 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001792{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001793 conf->ca_chain = ca_chain;
1794 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001795
1796#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1797 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1798 * cannot be used together. */
1799 conf->f_ca_cb = NULL;
1800 conf->p_ca_cb = NULL;
1801#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001802}
Hanno Becker5adaad92019-03-27 16:54:37 +00001803
1804#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1805void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001806 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001807 void *p_ca_cb )
1808{
1809 conf->f_ca_cb = f_ca_cb;
1810 conf->p_ca_cb = p_ca_cb;
1811
1812 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1813 * cannot be used together. */
1814 conf->ca_chain = NULL;
1815 conf->ca_crl = NULL;
1816}
1817#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001819
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001820#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001821const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1822 size_t *name_len )
1823{
1824 *name_len = ssl->handshake->sni_name_len;
1825 return( ssl->handshake->sni_name );
1826}
1827
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001828int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1829 mbedtls_x509_crt *own_cert,
1830 mbedtls_pk_context *pk_key )
1831{
1832 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1833 own_cert, pk_key ) );
1834}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001835
1836void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1837 mbedtls_x509_crt *ca_chain,
1838 mbedtls_x509_crl *ca_crl )
1839{
1840 ssl->handshake->sni_ca_chain = ca_chain;
1841 ssl->handshake->sni_ca_crl = ca_crl;
1842}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001843
Glenn Strauss999ef702022-03-11 01:37:23 -05001844#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1845void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1846 const mbedtls_x509_crt *crt)
1847{
1848 ssl->handshake->dn_hints = crt;
1849}
1850#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1851
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001852void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1853 int authmode )
1854{
1855 ssl->handshake->sni_authmode = authmode;
1856}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001857#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1858
Hanno Becker8927c832019-04-03 12:52:50 +01001859#if defined(MBEDTLS_X509_CRT_PARSE_C)
1860void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1861 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1862 void *p_vrfy )
1863{
1864 ssl->f_vrfy = f_vrfy;
1865 ssl->p_vrfy = p_vrfy;
1866}
1867#endif
1868
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001869#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001870/*
1871 * Set EC J-PAKE password for current handshake
1872 */
Valerio Setti02c25b52022-11-15 14:08:42 +01001873#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001874int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1875 const unsigned char *pw,
1876 size_t pw_len )
1877{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001878 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1880 psa_pake_role_t psa_role;
1881 psa_status_t status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001882
Janos Follath8eb64132016-06-03 15:40:57 +01001883 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001884 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1885
Neil Armstrongca7d5062022-05-31 14:43:23 +02001886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1887 psa_role = PSA_PAKE_ROLE_SERVER;
1888 else
1889 psa_role = PSA_PAKE_ROLE_CLIENT;
1890
Valerio Settiaca21b72022-11-17 18:17:01 +01001891 /* Empty password is not valid */
1892 if( ( pw == NULL) || ( pw_len == 0 ) )
1893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001894
Valerio Settiaca21b72022-11-17 18:17:01 +01001895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1896 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1897 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001898
Valerio Settiaca21b72022-11-17 18:17:01 +01001899 status = psa_import_key( &attributes, pw, pw_len,
1900 &ssl->handshake->psa_pake_password );
1901 if( status != PSA_SUCCESS )
1902 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001903
1904 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1905 psa_pake_cs_set_primitive( &cipher_suite,
1906 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1907 PSA_ECC_FAMILY_SECP_R1,
1908 256) );
1909 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1910
1911 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1912 if( status != PSA_SUCCESS )
1913 {
1914 psa_destroy_key( ssl->handshake->psa_pake_password );
1915 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1916 }
1917
1918 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1919 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
Valerio Settiaca21b72022-11-17 18:17:01 +01001926 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1927 ssl->handshake->psa_pake_password );
1928 if( status != PSA_SUCCESS )
Neil Armstrongca7d5062022-05-31 14:43:23 +02001929 {
Valerio Settiaca21b72022-11-17 18:17:01 +01001930 psa_destroy_key( ssl->handshake->psa_pake_password );
1931 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1932 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001933 }
1934
1935 ssl->handshake->psa_pake_ctx_is_ok = 1;
1936
1937 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001938}
1939#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001940int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1941 const unsigned char *pw,
1942 size_t pw_len )
1943{
1944 mbedtls_ecjpake_role role;
1945
1946 if( ssl->handshake == NULL || ssl->conf == NULL )
1947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1948
1949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1950 role = MBEDTLS_ECJPAKE_SERVER;
1951 else
1952 role = MBEDTLS_ECJPAKE_CLIENT;
1953
1954 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1955 role,
1956 MBEDTLS_MD_SHA256,
1957 MBEDTLS_ECP_DP_SECP256R1,
1958 pw, pw_len ) );
1959}
Valerio Setti02c25b52022-11-15 14:08:42 +01001960#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001961#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001962
Ronald Cron73fe8df2022-10-05 14:31:43 +02001963#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001964int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001965{
Ronald Crond29e13e2022-10-19 10:33:48 +02001966 if( conf->psk_identity == NULL ||
1967 conf->psk_identity_len == 0 )
1968 {
1969 return( 0 );
1970 }
1971
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001972#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001973 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001974 return( 1 );
1975#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001976
1977 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001978 return( 1 );
1979
1980 return( 0 );
1981}
1982
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001983static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1984{
1985 /* Remove reference to existing PSK, if any. */
1986#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001987 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001988 {
1989 /* The maintenance of the PSK key slot is the
1990 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001991 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001992 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001993#endif /* MBEDTLS_USE_PSA_CRYPTO */
1994 if( conf->psk != NULL )
1995 {
1996 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1997
1998 mbedtls_free( conf->psk );
1999 conf->psk = NULL;
2000 conf->psk_len = 0;
2001 }
2002
2003 /* Remove reference to PSK identity, if any. */
2004 if( conf->psk_identity != NULL )
2005 {
2006 mbedtls_free( conf->psk_identity );
2007 conf->psk_identity = NULL;
2008 conf->psk_identity_len = 0;
2009 }
2010}
2011
Hanno Becker7390c712018-11-15 13:33:04 +00002012/* This function assumes that PSK identity in the SSL config is unset.
2013 * It checks that the provided identity is well-formed and attempts
2014 * to make a copy of it in the SSL config.
2015 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002016MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002017static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2018 unsigned char const *psk_identity,
2019 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002020{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002021 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002022 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002023 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002024 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002025 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002026 {
2027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2028 }
2029
Hanno Becker7390c712018-11-15 13:33:04 +00002030 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2031 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002032 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002033
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002034 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002035 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002036
2037 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002038}
2039
Hanno Becker7390c712018-11-15 13:33:04 +00002040int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2041 const unsigned char *psk, size_t psk_len,
2042 const unsigned char *psk_identity, size_t psk_identity_len )
2043{
Janos Follath865b3eb2019-12-16 11:46:15 +00002044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002045
2046 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002047 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002048 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002049
2050 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002051 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002053 if( psk_len == 0 )
2054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2055 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2057
Hanno Becker7390c712018-11-15 13:33:04 +00002058 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2060 conf->psk_len = psk_len;
2061 memcpy( conf->psk, psk, conf->psk_len );
2062
2063 /* Check and set PSK Identity */
2064 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2065 if( ret != 0 )
2066 ssl_conf_remove_psk( conf );
2067
2068 return( ret );
2069}
2070
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002071static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2072{
2073#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002074 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002075 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002076 /* The maintenance of the external PSK key slot is the
2077 * user's responsibility. */
2078 if( ssl->handshake->psk_opaque_is_internal )
2079 {
2080 psa_destroy_key( ssl->handshake->psk_opaque );
2081 ssl->handshake->psk_opaque_is_internal = 0;
2082 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002083 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002084 }
Neil Armstronge952a302022-05-03 10:22:14 +02002085#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002086 if( ssl->handshake->psk != NULL )
2087 {
2088 mbedtls_platform_zeroize( ssl->handshake->psk,
2089 ssl->handshake->psk_len );
2090 mbedtls_free( ssl->handshake->psk );
2091 ssl->handshake->psk_len = 0;
2092 }
Neil Armstronge952a302022-05-03 10:22:14 +02002093#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002094}
2095
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002096int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2097 const unsigned char *psk, size_t psk_len )
2098{
Neil Armstrong501c9322022-05-03 09:35:09 +02002099#if defined(MBEDTLS_USE_PSA_CRYPTO)
2100 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002102 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002103 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002104#endif /* MBEDTLS_USE_PSA_CRYPTO */
2105
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002106 if( psk == NULL || ssl->handshake == NULL )
2107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2108
2109 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2110 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2111
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002112 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002113
Neil Armstrong501c9322022-05-03 09:35:09 +02002114#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002115#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2116 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2117 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002118 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002119 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2120 else
2121 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2122 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2123 }
2124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002125
Jerry Yu568ec252022-07-22 21:27:34 +08002126#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002127 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2128 {
2129 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2130 psa_set_key_usage_flags( &key_attributes,
2131 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2132 }
2133#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2134
Neil Armstrong501c9322022-05-03 09:35:09 +02002135 psa_set_key_algorithm( &key_attributes, alg );
2136 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2137
2138 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2139 if( status != PSA_SUCCESS )
2140 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2141
2142 /* Allow calling psa_destroy_key() on psk remove */
2143 ssl->handshake->psk_opaque_is_internal = 1;
2144 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2145#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002146 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002147 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002148
2149 ssl->handshake->psk_len = psk_len;
2150 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2151
2152 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002153#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002154}
2155
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002156#if defined(MBEDTLS_USE_PSA_CRYPTO)
2157int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002158 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002159 const unsigned char *psk_identity,
2160 size_t psk_identity_len )
2161{
Janos Follath865b3eb2019-12-16 11:46:15 +00002162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002163
2164 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002165 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002166 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002167
Hanno Becker7390c712018-11-15 13:33:04 +00002168 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002169 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002171 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002172
2173 /* Check and set PSK Identity */
2174 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2175 psk_identity_len );
2176 if( ret != 0 )
2177 ssl_conf_remove_psk( conf );
2178
2179 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002180}
2181
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002182int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2183 mbedtls_svc_key_id_t psk )
2184{
2185 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2186 ( ssl->handshake == NULL ) )
2187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2188
2189 ssl_remove_psk( ssl );
2190 ssl->handshake->psk_opaque = psk;
2191 return( 0 );
2192}
2193#endif /* MBEDTLS_USE_PSA_CRYPTO */
2194
Jerry Yu8897c072022-08-12 13:56:53 +08002195#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002196void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2197 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2198 size_t),
2199 void *p_psk )
2200{
2201 conf->f_psk = f_psk;
2202 conf->p_psk = p_psk;
2203}
Jerry Yu8897c072022-08-12 13:56:53 +08002204#endif /* MBEDTLS_SSL_SRV_C */
2205
Ronald Cron73fe8df2022-10-05 14:31:43 +02002206#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002207
2208#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002209static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2210 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002211{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002212#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002213 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002214 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002215#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002216 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002217 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002218 return( MBEDTLS_SSL_MODE_STREAM );
2219}
2220
2221#else /* MBEDTLS_USE_PSA_CRYPTO */
2222
2223static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2224 mbedtls_cipher_mode_t mode )
2225{
2226#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2227 if( mode == MBEDTLS_MODE_CBC )
2228 return( MBEDTLS_SSL_MODE_CBC );
2229#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2230
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002231#if defined(MBEDTLS_GCM_C) || \
2232 defined(MBEDTLS_CCM_C) || \
2233 defined(MBEDTLS_CHACHAPOLY_C)
2234 if( mode == MBEDTLS_MODE_GCM ||
2235 mode == MBEDTLS_MODE_CCM ||
2236 mode == MBEDTLS_MODE_CHACHAPOLY )
2237 return( MBEDTLS_SSL_MODE_AEAD );
2238#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002239
2240 return( MBEDTLS_SSL_MODE_STREAM );
2241}
Gilles Peskine301711e2022-04-26 16:57:05 +02002242#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002243
Gilles Peskinee108d982022-04-26 16:50:40 +02002244static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2245 mbedtls_ssl_mode_t base_mode,
2246 int encrypt_then_mac )
2247{
2248#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2249 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2250 base_mode == MBEDTLS_SSL_MODE_CBC )
2251 {
2252 return( MBEDTLS_SSL_MODE_CBC_ETM );
2253 }
2254#else
2255 (void) encrypt_then_mac;
2256#endif
2257 return( base_mode );
2258}
2259
Neil Armstrongab555e02022-04-04 11:07:59 +02002260mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002261 const mbedtls_ssl_transform *transform )
2262{
Gilles Peskinee108d982022-04-26 16:50:40 +02002263 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002264#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002265 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002266#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002267 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2268#endif
2269 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002270
Gilles Peskinee108d982022-04-26 16:50:40 +02002271 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002272#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002273 encrypt_then_mac = transform->encrypt_then_mac;
2274#endif
2275 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002276}
2277
Neil Armstrongab555e02022-04-04 11:07:59 +02002278mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002279#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002280 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002281#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002282 const mbedtls_ssl_ciphersuite_t *suite )
2283{
Gilles Peskinee108d982022-04-26 16:50:40 +02002284 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2285
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002286#if defined(MBEDTLS_USE_PSA_CRYPTO)
2287 psa_status_t status;
2288 psa_algorithm_t alg;
2289 psa_key_type_t type;
2290 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002291 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2292 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002293 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002294#else
2295 const mbedtls_cipher_info_t *cipher =
2296 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002297 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002298 {
2299 base_mode =
2300 mbedtls_ssl_get_base_mode(
2301 mbedtls_cipher_info_get_mode( cipher ) );
2302 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002303#endif /* MBEDTLS_USE_PSA_CRYPTO */
2304
Gilles Peskinee108d982022-04-26 16:50:40 +02002305#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2306 int encrypt_then_mac = 0;
2307#endif
2308 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002309}
2310
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002311#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002312
2313#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002314/* Serialization of TLS 1.3 sessions:
2315 *
2316 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002317 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002318 * uint64 ticket_received;
2319 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002320 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002321 * } ClientOnlyData;
2322 *
2323 * struct {
2324 * uint8 endpoint;
2325 * uint8 ciphersuite[2];
2326 * uint32 ticket_age_add;
2327 * uint8 ticket_flags;
2328 * opaque resumption_key<0..255>;
2329 * select ( endpoint ) {
2330 * case client: ClientOnlyData;
2331 * case server: uint64 start_time;
2332 * };
2333 * } serialized_session_tls13;
2334 *
2335 */
2336#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002337MBEDTLS_CHECK_RETURN_CRITICAL
2338static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2339 unsigned char *buf,
2340 size_t buf_len,
2341 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002342{
2343 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002344#if defined(MBEDTLS_SSL_CLI_C) && \
2345 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2346 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002347 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002348#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002349 size_t needed = 1 /* endpoint */
2350 + 2 /* ciphersuite */
2351 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002352 + 1 /* ticket_flags */
2353 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002354 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002355
2356 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2358 needed += session->resumption_key_len; /* resumption_key */
2359
Jerry Yu438ddd82022-07-07 06:55:50 +00002360#if defined(MBEDTLS_HAVE_TIME)
2361 needed += 8; /* start_time or ticket_received */
2362#endif
2363
2364#if defined(MBEDTLS_SSL_CLI_C)
2365 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2366 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002367#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002368 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002369 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002370#endif
2371
Jerry Yu438ddd82022-07-07 06:55:50 +00002372 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002373 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002374
2375 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002376 if( session->ticket_len > SIZE_MAX - needed )
2377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002378
Jerry Yue28d9742022-08-18 15:44:03 +08002379 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002380 }
2381#endif /* MBEDTLS_SSL_CLI_C */
2382
Jerry Yue36fdd62022-08-17 21:31:36 +08002383 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002384 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002385 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002386
2387 p[0] = session->endpoint;
2388 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2389 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2390 p[7] = session->ticket_flags;
2391
2392 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002393 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002394 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002395 memcpy( p, session->resumption_key, session->resumption_key_len );
2396 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002397
2398#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2399 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2400 {
2401 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2402 p += 8;
2403 }
2404#endif /* MBEDTLS_HAVE_TIME */
2405
2406#if defined(MBEDTLS_SSL_CLI_C)
2407 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2408 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002409#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2410 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2411 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002412 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002413 {
2414 /* save host name */
2415 memcpy( p, session->hostname, hostname_len );
2416 p += hostname_len;
2417 }
2418#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2419
Jerry Yu438ddd82022-07-07 06:55:50 +00002420#if defined(MBEDTLS_HAVE_TIME)
2421 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2422 p += 8;
2423#endif
2424 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2425 p += 4;
2426
2427 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2428 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002429
2430 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002431 {
2432 memcpy( p, session->ticket, session->ticket_len );
2433 p += session->ticket_len;
2434 }
2435 }
2436#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002437 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002438}
2439
2440MBEDTLS_CHECK_RETURN_CRITICAL
2441static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2442 const unsigned char *buf,
2443 size_t len )
2444{
2445 const unsigned char *p = buf;
2446 const unsigned char *end = buf + len;
2447
2448 if( end - p < 9 )
2449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2450 session->endpoint = p[0];
2451 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2452 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2453 session->ticket_flags = p[7];
2454
2455 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002456 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002457 p += 9;
2458
Jerry Yubc7c1a42022-07-21 22:57:37 +08002459 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2461
Jerry Yubc7c1a42022-07-21 22:57:37 +08002462 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002464 memcpy( session->resumption_key, p, session->resumption_key_len );
2465 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002466
2467#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2468 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2469 {
2470 if( end - p < 8 )
2471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2472 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2473 p += 8;
2474 }
2475#endif /* MBEDTLS_HAVE_TIME */
2476
2477#if defined(MBEDTLS_SSL_CLI_C)
2478 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2479 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002480#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2481 defined(MBEDTLS_SSL_SESSION_TICKETS)
2482 size_t hostname_len;
2483 /* load host name */
2484 if( end - p < 2 )
2485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2486 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2487 p += 2;
2488
2489 if( end - p < ( long int )hostname_len )
2490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2491 if( hostname_len > 0 )
2492 {
2493 session->hostname = mbedtls_calloc( 1, hostname_len );
2494 if( session->hostname == NULL )
2495 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2496 memcpy( session->hostname, p, hostname_len );
2497 p += hostname_len;
2498 }
2499#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2500 MBEDTLS_SSL_SESSION_TICKETS */
2501
Jerry Yu438ddd82022-07-07 06:55:50 +00002502#if defined(MBEDTLS_HAVE_TIME)
2503 if( end - p < 8 )
2504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2505 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2506 p += 8;
2507#endif
2508 if( end - p < 4 )
2509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2510 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2511 p += 4;
2512
2513 if( end - p < 2 )
2514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2515 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2516 p += 2;
2517
2518 if( end - p < ( long int )session->ticket_len )
2519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2520 if( session->ticket_len > 0 )
2521 {
2522 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2523 if( session->ticket == NULL )
2524 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2525 memcpy( session->ticket, p, session->ticket_len );
2526 p += session->ticket_len;
2527 }
2528 }
2529#endif /* MBEDTLS_SSL_CLI_C */
2530
2531 return( 0 );
2532
2533}
2534#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002535MBEDTLS_CHECK_RETURN_CRITICAL
2536static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2537 unsigned char *buf,
2538 size_t buf_len,
2539 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002540{
2541 ((void) session);
2542 ((void) buf);
2543 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002544 *olen = 0;
2545 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002546}
Jerry Yu438ddd82022-07-07 06:55:50 +00002547
2548static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2549 unsigned char *buf,
2550 size_t buf_len )
2551{
2552 ((void) session);
2553 ((void) buf);
2554 ((void) buf_len);
2555 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2556}
2557#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002558#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2559
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002560psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002561 size_t taglen,
2562 psa_algorithm_t *alg,
2563 psa_key_type_t *key_type,
2564 size_t *key_size )
2565{
2566 switch ( mbedtls_cipher_type )
2567 {
2568 case MBEDTLS_CIPHER_AES_128_CBC:
2569 *alg = PSA_ALG_CBC_NO_PADDING;
2570 *key_type = PSA_KEY_TYPE_AES;
2571 *key_size = 128;
2572 break;
2573 case MBEDTLS_CIPHER_AES_128_CCM:
2574 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2575 *key_type = PSA_KEY_TYPE_AES;
2576 *key_size = 128;
2577 break;
2578 case MBEDTLS_CIPHER_AES_128_GCM:
2579 *alg = PSA_ALG_GCM;
2580 *key_type = PSA_KEY_TYPE_AES;
2581 *key_size = 128;
2582 break;
2583 case MBEDTLS_CIPHER_AES_192_CCM:
2584 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2585 *key_type = PSA_KEY_TYPE_AES;
2586 *key_size = 192;
2587 break;
2588 case MBEDTLS_CIPHER_AES_192_GCM:
2589 *alg = PSA_ALG_GCM;
2590 *key_type = PSA_KEY_TYPE_AES;
2591 *key_size = 192;
2592 break;
2593 case MBEDTLS_CIPHER_AES_256_CBC:
2594 *alg = PSA_ALG_CBC_NO_PADDING;
2595 *key_type = PSA_KEY_TYPE_AES;
2596 *key_size = 256;
2597 break;
2598 case MBEDTLS_CIPHER_AES_256_CCM:
2599 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2600 *key_type = PSA_KEY_TYPE_AES;
2601 *key_size = 256;
2602 break;
2603 case MBEDTLS_CIPHER_AES_256_GCM:
2604 *alg = PSA_ALG_GCM;
2605 *key_type = PSA_KEY_TYPE_AES;
2606 *key_size = 256;
2607 break;
2608 case MBEDTLS_CIPHER_ARIA_128_CBC:
2609 *alg = PSA_ALG_CBC_NO_PADDING;
2610 *key_type = PSA_KEY_TYPE_ARIA;
2611 *key_size = 128;
2612 break;
2613 case MBEDTLS_CIPHER_ARIA_128_CCM:
2614 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2615 *key_type = PSA_KEY_TYPE_ARIA;
2616 *key_size = 128;
2617 break;
2618 case MBEDTLS_CIPHER_ARIA_128_GCM:
2619 *alg = PSA_ALG_GCM;
2620 *key_type = PSA_KEY_TYPE_ARIA;
2621 *key_size = 128;
2622 break;
2623 case MBEDTLS_CIPHER_ARIA_192_CCM:
2624 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2625 *key_type = PSA_KEY_TYPE_ARIA;
2626 *key_size = 192;
2627 break;
2628 case MBEDTLS_CIPHER_ARIA_192_GCM:
2629 *alg = PSA_ALG_GCM;
2630 *key_type = PSA_KEY_TYPE_ARIA;
2631 *key_size = 192;
2632 break;
2633 case MBEDTLS_CIPHER_ARIA_256_CBC:
2634 *alg = PSA_ALG_CBC_NO_PADDING;
2635 *key_type = PSA_KEY_TYPE_ARIA;
2636 *key_size = 256;
2637 break;
2638 case MBEDTLS_CIPHER_ARIA_256_CCM:
2639 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2640 *key_type = PSA_KEY_TYPE_ARIA;
2641 *key_size = 256;
2642 break;
2643 case MBEDTLS_CIPHER_ARIA_256_GCM:
2644 *alg = PSA_ALG_GCM;
2645 *key_type = PSA_KEY_TYPE_ARIA;
2646 *key_size = 256;
2647 break;
2648 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2649 *alg = PSA_ALG_CBC_NO_PADDING;
2650 *key_type = PSA_KEY_TYPE_CAMELLIA;
2651 *key_size = 128;
2652 break;
2653 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2654 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2655 *key_type = PSA_KEY_TYPE_CAMELLIA;
2656 *key_size = 128;
2657 break;
2658 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2659 *alg = PSA_ALG_GCM;
2660 *key_type = PSA_KEY_TYPE_CAMELLIA;
2661 *key_size = 128;
2662 break;
2663 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2664 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2665 *key_type = PSA_KEY_TYPE_CAMELLIA;
2666 *key_size = 192;
2667 break;
2668 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2669 *alg = PSA_ALG_GCM;
2670 *key_type = PSA_KEY_TYPE_CAMELLIA;
2671 *key_size = 192;
2672 break;
2673 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2674 *alg = PSA_ALG_CBC_NO_PADDING;
2675 *key_type = PSA_KEY_TYPE_CAMELLIA;
2676 *key_size = 256;
2677 break;
2678 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2679 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2680 *key_type = PSA_KEY_TYPE_CAMELLIA;
2681 *key_size = 256;
2682 break;
2683 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2684 *alg = PSA_ALG_GCM;
2685 *key_type = PSA_KEY_TYPE_CAMELLIA;
2686 *key_size = 256;
2687 break;
2688 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2689 *alg = PSA_ALG_CHACHA20_POLY1305;
2690 *key_type = PSA_KEY_TYPE_CHACHA20;
2691 *key_size = 256;
2692 break;
2693 case MBEDTLS_CIPHER_NULL:
2694 *alg = MBEDTLS_SSL_NULL_CIPHER;
2695 *key_type = 0;
2696 *key_size = 0;
2697 break;
2698 default:
2699 return PSA_ERROR_NOT_SUPPORTED;
2700 }
2701
2702 return PSA_SUCCESS;
2703}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002704#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002705
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002706#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002707int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2708 const unsigned char *dhm_P, size_t P_len,
2709 const unsigned char *dhm_G, size_t G_len )
2710{
Janos Follath865b3eb2019-12-16 11:46:15 +00002711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002712
Glenn Strausscee11292021-12-20 01:43:17 -05002713 mbedtls_mpi_free( &conf->dhm_P );
2714 mbedtls_mpi_free( &conf->dhm_G );
2715
Hanno Beckera90658f2017-10-04 15:29:08 +01002716 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2717 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2718 {
2719 mbedtls_mpi_free( &conf->dhm_P );
2720 mbedtls_mpi_free( &conf->dhm_G );
2721 return( ret );
2722 }
2723
2724 return( 0 );
2725}
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002727int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002728{
Janos Follath865b3eb2019-12-16 11:46:15 +00002729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002730
Glenn Strausscee11292021-12-20 01:43:17 -05002731 mbedtls_mpi_free( &conf->dhm_P );
2732 mbedtls_mpi_free( &conf->dhm_G );
2733
Gilles Peskinee5702482021-06-11 21:59:08 +02002734 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2735 &conf->dhm_P ) ) != 0 ||
2736 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2737 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002738 {
2739 mbedtls_mpi_free( &conf->dhm_P );
2740 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002741 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002742 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002743
2744 return( 0 );
2745}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002746#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002747
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002748#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2749/*
2750 * Set the minimum length for Diffie-Hellman parameters
2751 */
2752void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2753 unsigned int bitlen )
2754{
2755 conf->dhm_min_bitlen = bitlen;
2756}
2757#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2758
Ronald Crone68ab4f2022-10-05 12:46:29 +02002759#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002760#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002761/*
2762 * Set allowed/preferred hashes for handshake signatures
2763 */
2764void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2765 const int *hashes )
2766{
2767 conf->sig_hashes = hashes;
2768}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002769#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002770
Jerry Yuf017ee42022-01-12 15:49:48 +08002771/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002772void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2773 const uint16_t* sig_algs )
2774{
Jerry Yuf017ee42022-01-12 15:49:48 +08002775#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2776 conf->sig_hashes = NULL;
2777#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2778 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002779}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002780#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002781
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002782#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002783#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002784/*
2785 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002786 *
2787 * mbedtls_ssl_setup() takes the provided list
2788 * and translates it to a list of IANA TLS group identifiers,
2789 * stored in ssl->handshake->group_list.
2790 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002791 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002792void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002793 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002794{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002795 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002796 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002797}
Brett Warrene0edc842021-08-17 09:53:13 +01002798#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002799#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002800
Brett Warrene0edc842021-08-17 09:53:13 +01002801/*
2802 * Set the allowed groups
2803 */
2804void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2805 const uint16_t *group_list )
2806{
2807#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2808 conf->curve_list = NULL;
2809#endif
2810 conf->group_list = group_list;
2811}
2812
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002813#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002815{
Hanno Becker947194e2017-04-07 13:25:49 +01002816 /* Initialize to suppress unnecessary compiler warning */
2817 size_t hostname_len = 0;
2818
2819 /* Check if new hostname is valid before
2820 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002821 if( hostname != NULL )
2822 {
2823 hostname_len = strlen( hostname );
2824
2825 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2827 }
2828
2829 /* Now it's clear that we will overwrite the old hostname,
2830 * so we can free it safely */
2831
2832 if( ssl->hostname != NULL )
2833 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002834 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002835 mbedtls_free( ssl->hostname );
2836 }
2837
2838 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002839
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002841 {
2842 ssl->hostname = NULL;
2843 }
2844 else
2845 {
2846 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002847 if( ssl->hostname == NULL )
2848 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002849
Hanno Becker947194e2017-04-07 13:25:49 +01002850 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002851
Hanno Becker947194e2017-04-07 13:25:49 +01002852 ssl->hostname[hostname_len] = '\0';
2853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
2855 return( 0 );
2856}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002857#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002859#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002860void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002862 const unsigned char *, size_t),
2863 void *p_sni )
2864{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002865 conf->f_sni = f_sni;
2866 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002871int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002872{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002873 size_t cur_len, tot_len;
2874 const char **p;
2875
2876 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002877 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2878 * MUST NOT be truncated."
2879 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002880 */
2881 tot_len = 0;
2882 for( p = protos; *p != NULL; p++ )
2883 {
2884 cur_len = strlen( *p );
2885 tot_len += cur_len;
2886
Ronald Cron8216dd32020-04-23 16:41:44 +02002887 if( ( cur_len == 0 ) ||
2888 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2889 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002891 }
2892
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002893 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002894
2895 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002896}
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002899{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002900 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002903
Johan Pascalb62bb512015-12-03 21:56:45 +01002904#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002905void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2906 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002907{
2908 conf->dtls_srtp_mki_support = support_mki_value;
2909}
2910
Ron Eldoref72faf2018-07-12 11:54:20 +03002911int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2912 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002913 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002914{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002915 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002916 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002918 }
Ron Eldor591f1622018-01-22 12:30:04 +02002919
2920 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002921 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002922 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002923 }
Ron Eldor591f1622018-01-22 12:30:04 +02002924
2925 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2926 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002927 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002928}
2929
Ron Eldoref72faf2018-07-12 11:54:20 +03002930int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002931 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002932{
Johan Pascal253d0262020-09-22 13:04:45 +02002933 const mbedtls_ssl_srtp_profile *p;
2934 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002935
Johan Pascal253d0262020-09-22 13:04:45 +02002936 /* check the profiles list: all entry must be valid,
2937 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002938 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2939 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2940 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002941 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002942 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002943 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002944 list_size++;
2945 }
2946 else
2947 {
2948 /* unsupported value, stop parsing and set the size to an error value */
2949 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002950 }
2951 }
2952
Johan Pascal5ef72d22020-10-28 17:05:47 +01002953 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002954 {
Johan Pascal253d0262020-09-22 13:04:45 +02002955 conf->dtls_srtp_profile_list = NULL;
2956 conf->dtls_srtp_profile_list_len = 0;
2957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2958 }
2959
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002960 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002961 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002962
2963 return( 0 );
2964}
2965
Johan Pascal5ef72d22020-10-28 17:05:47 +01002966void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2967 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002968{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002969 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2970 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002971 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002972 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002973 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002974 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002975 else
2976 {
2977 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002978 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2979 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002980 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002981}
Johan Pascalb62bb512015-12-03 21:56:45 +01002982#endif /* MBEDTLS_SSL_DTLS_SRTP */
2983
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302984#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002985void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002986{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002987 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002988}
2989
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002990void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002991{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002992 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002993}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302994#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002995
Janos Follath088ce432017-04-10 12:42:31 +01002996#if defined(MBEDTLS_SSL_SRV_C)
2997void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2998 char cert_req_ca_list )
2999{
3000 conf->cert_req_ca_list = cert_req_ca_list;
3001}
3002#endif
3003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003005void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003006{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003007 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003008}
3009#endif
3010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003012void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003013{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003014 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003015}
3016#endif
3017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003019int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003022 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003025 }
3026
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003027 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003028
3029 return( 0 );
3030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003032
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003033void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003034{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003035 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003036}
3037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003039void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003040{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003041 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003042}
3043
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003044void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003045{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003046 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003047}
3048
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003049void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003050 const unsigned char period[8] )
3051{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003052 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003057#if defined(MBEDTLS_SSL_CLI_C)
3058void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003059{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003060 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003061}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003062#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003063
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003064#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003065
Jerry Yud0766ec2022-09-22 10:46:57 +08003066#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003067void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3068 uint16_t num_tickets )
3069{
Jerry Yud0766ec2022-09-22 10:46:57 +08003070 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003071}
3072#endif
3073
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003074void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3075 mbedtls_ssl_ticket_write_t *f_ticket_write,
3076 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3077 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003078{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003079 conf->f_ticket_write = f_ticket_write;
3080 conf->f_ticket_parse = f_ticket_parse;
3081 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003082}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003085
Hanno Becker7e6c1782021-06-08 09:24:55 +01003086void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3087 mbedtls_ssl_export_keys_t *f_export_keys,
3088 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003089{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003090 ssl->f_export_keys = f_export_keys;
3091 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003092}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003093
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003094#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003095void mbedtls_ssl_conf_async_private_cb(
3096 mbedtls_ssl_config *conf,
3097 mbedtls_ssl_async_sign_t *f_async_sign,
3098 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3099 mbedtls_ssl_async_resume_t *f_async_resume,
3100 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003101 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003102{
3103 conf->f_async_sign_start = f_async_sign;
3104 conf->f_async_decrypt_start = f_async_decrypt;
3105 conf->f_async_resume = f_async_resume;
3106 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003107 conf->p_async_config_data = async_config_data;
3108}
3109
Gilles Peskine8f97af72018-04-26 11:46:10 +02003110void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3111{
3112 return( conf->p_async_config_data );
3113}
3114
Gilles Peskine1febfef2018-04-30 11:54:39 +02003115void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003116{
3117 if( ssl->handshake == NULL )
3118 return( NULL );
3119 else
3120 return( ssl->handshake->user_async_ctx );
3121}
3122
Gilles Peskine1febfef2018-04-30 11:54:39 +02003123void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003124 void *ctx )
3125{
3126 if( ssl->handshake != NULL )
3127 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003128}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003129#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003130
Paul Bakker5121ce52009-01-03 21:22:43 +00003131/*
3132 * SSL get accessors
3133 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003134uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003135{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003136 if( ssl->session != NULL )
3137 return( ssl->session->verify_result );
3138
3139 if( ssl->session_negotiate != NULL )
3140 return( ssl->session_negotiate->verify_result );
3141
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003142 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003143}
3144
Glenn Strauss8f526902022-01-13 00:04:49 -05003145int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3146{
3147 if( ssl == NULL || ssl->session == NULL )
3148 return( 0 );
3149
3150 return( ssl->session->ciphersuite );
3151}
3152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003154{
Paul Bakker926c8e42013-03-06 10:23:34 +01003155 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003156 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003159}
3160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003164 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003165 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003166 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003167 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003168 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003169 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003170 default:
3171 return( "unknown (DTLS)" );
3172 }
3173 }
3174#endif
3175
Glenn Strauss60bfe602022-03-14 19:04:24 -04003176 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003177 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003178 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003180 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003181 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003182 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003183 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003184 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003185}
3186
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003187#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003188size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3189{
David Horstmann95d516f2021-05-04 18:36:56 +01003190 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003191 size_t read_mfl;
3192
3193 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3194 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3195 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3196 {
3197 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3198 }
3199
3200 /* Check if a smaller max length was negotiated */
3201 if( ssl->session_out != NULL )
3202 {
3203 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3204 if( read_mfl < max_len )
3205 {
3206 max_len = read_mfl;
3207 }
3208 }
3209
3210 // During a handshake, use the value being negotiated
3211 if( ssl->session_negotiate != NULL )
3212 {
3213 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3214 if( read_mfl < max_len )
3215 {
3216 max_len = read_mfl;
3217 }
3218 }
3219
3220 return( max_len );
3221}
3222
3223size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003224{
3225 size_t max_len;
3226
3227 /*
3228 * Assume mfl_code is correct since it was checked when set
3229 */
Angus Grattond8213d02016-05-25 20:56:48 +10003230 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003231
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003232 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003233 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003234 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003235 {
Angus Grattond8213d02016-05-25 20:56:48 +10003236 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003237 }
3238
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003239 /* During a handshake, use the value being negotiated */
3240 if( ssl->session_negotiate != NULL &&
3241 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3242 {
3243 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3244 }
3245
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003246 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003247}
3248#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3249
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003251size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003252{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003253 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3254 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3255 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3256 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3257 return ( 0 );
3258
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003259 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3260 return( ssl->mtu );
3261
3262 if( ssl->mtu == 0 )
3263 return( ssl->handshake->mtu );
3264
3265 return( ssl->mtu < ssl->handshake->mtu ?
3266 ssl->mtu : ssl->handshake->mtu );
3267}
3268#endif /* MBEDTLS_SSL_PROTO_DTLS */
3269
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003270int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3271{
3272 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3273
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003274#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3275 !defined(MBEDTLS_SSL_PROTO_DTLS)
3276 (void) ssl;
3277#endif
3278
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003279#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003280 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003281
3282 if( max_len > mfl )
3283 max_len = mfl;
3284#endif
3285
3286#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003287 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003288 {
Hanno Becker89490712020-02-05 10:50:12 +00003289 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003290 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3291 const size_t overhead = (size_t) ret;
3292
3293 if( ret < 0 )
3294 return( ret );
3295
3296 if( mtu <= overhead )
3297 {
3298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3299 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3300 }
3301
3302 if( max_len > mtu - overhead )
3303 max_len = mtu - overhead;
3304 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003305#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003306
Hanno Becker0defedb2018-08-10 12:35:02 +01003307#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3308 !defined(MBEDTLS_SSL_PROTO_DTLS)
3309 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003310#endif
3311
3312 return( (int) max_len );
3313}
3314
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003315int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3316{
3317 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3318
3319#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3320 (void) ssl;
3321#endif
3322
3323#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3324 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3325
3326 if( max_len > mfl )
3327 max_len = mfl;
3328#endif
3329
3330 return( (int) max_len );
3331}
3332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333#if defined(MBEDTLS_X509_CRT_PARSE_C)
3334const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003335{
3336 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003337 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003338
Hanno Beckere6824572019-02-07 13:18:46 +00003339#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003340 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003341#else
3342 return( NULL );
3343#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003348int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3349 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003350{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003351 int ret;
3352
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003353 if( ssl == NULL ||
3354 dst == NULL ||
3355 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003356 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003359 }
3360
Hanno Beckere810bbc2021-05-14 16:01:05 +01003361 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3362 * idempotent: Each session can only be exported once.
3363 *
3364 * (This is in preparation for TLS 1.3 support where we will
3365 * need the ability to export multiple sessions (aka tickets),
3366 * which will be achieved by calling mbedtls_ssl_get_session()
3367 * multiple times until it fails.)
3368 *
3369 * Check whether we have already exported the current session,
3370 * and fail if so.
3371 */
3372 if( ssl->session->exported == 1 )
3373 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3374
3375 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3376 if( ret != 0 )
3377 return( ret );
3378
3379 /* Remember that we've exported the session. */
3380 ssl->session->exported = 1;
3381 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003384
Paul Bakker5121ce52009-01-03 21:22:43 +00003385/*
Hanno Beckera835da52019-05-16 12:39:07 +01003386 * Define ticket header determining Mbed TLS version
3387 * and structure of the ticket.
3388 */
3389
Hanno Becker94ef3b32019-05-16 12:50:45 +01003390/*
Hanno Becker50b59662019-05-28 14:30:45 +01003391 * Define bitflag determining compile-time settings influencing
3392 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003393 */
3394
Hanno Becker50b59662019-05-28 14:30:45 +01003395#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003396#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003397#else
Hanno Becker3e088662019-05-29 11:10:18 +01003398#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003399#endif /* MBEDTLS_HAVE_TIME */
3400
3401#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003402#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003403#else
Hanno Becker3e088662019-05-29 11:10:18 +01003404#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003405#endif /* MBEDTLS_X509_CRT_PARSE_C */
3406
3407#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003408#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003409#else
Hanno Becker3e088662019-05-29 11:10:18 +01003410#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003411#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3412
3413#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003414#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003415#else
Hanno Becker3e088662019-05-29 11:10:18 +01003416#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003417#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3418
Hanno Becker94ef3b32019-05-16 12:50:45 +01003419#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003420#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003421#else
Hanno Becker3e088662019-05-29 11:10:18 +01003422#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003423#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3424
Hanno Becker94ef3b32019-05-16 12:50:45 +01003425#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3426#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3427#else
3428#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3429#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3430
Hanno Becker3e088662019-05-29 11:10:18 +01003431#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3432#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3433#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3434#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003435#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3436#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003437
Hanno Becker50b59662019-05-28 14:30:45 +01003438#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003439 ( (uint16_t) ( \
3440 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3441 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3442 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3443 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003444 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003445 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003446
Hanno Beckerf8787072019-05-16 12:41:07 +01003447static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003448 MBEDTLS_VERSION_MAJOR,
3449 MBEDTLS_VERSION_MINOR,
3450 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003451 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3452 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003453};
Hanno Beckera835da52019-05-16 12:39:07 +01003454
3455/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003456 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003457 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003458 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003459 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003460 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003461 * opaque mbedtls_version[3]; // library version: major, minor, patch
3462 * opaque session_format[2]; // library-version specific 16-bit field
3463 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003464 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003465 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003466 * Note: When updating the format, remember to keep
3467 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003468 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003469 * // In this version, `session_format` determines
3470 * // the setting of those compile-time
3471 * // configuration options which influence
3472 * // the structure of mbedtls_ssl_session.
3473 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003474 * uint8_t minor_ver; // Protocol minor version. Possible values:
3475 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003476 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003477 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003478 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003479 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003480 * serialized_session_tls12 data;
3481 *
3482 * };
3483 *
3484 * } serialized_session;
3485 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003486 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003487
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003488MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003489static int ssl_session_save( const mbedtls_ssl_session *session,
3490 unsigned char omit_header,
3491 unsigned char *buf,
3492 size_t buf_len,
3493 size_t *olen )
3494{
3495 unsigned char *p = buf;
3496 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003497 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003498#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3499 size_t out_len;
3500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3501#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003502 if( session == NULL )
3503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3504
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003505 if( !omit_header )
3506 {
3507 /*
3508 * Add Mbed TLS version identifier
3509 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003510 used += sizeof( ssl_serialized_session_header );
3511
3512 if( used <= buf_len )
3513 {
3514 memcpy( p, ssl_serialized_session_header,
3515 sizeof( ssl_serialized_session_header ) );
3516 p += sizeof( ssl_serialized_session_header );
3517 }
3518 }
3519
3520 /*
3521 * TLS version identifier
3522 */
3523 used += 1;
3524 if( used <= buf_len )
3525 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003526 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003527 }
3528
3529 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003530 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003531 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003532 {
3533#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003534 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003535 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003536 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003537#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3538
Jerry Yu251a12e2022-07-13 15:15:48 +08003539#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3540 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003541 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3542 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003543 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003544 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003545 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003546#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3547
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003548 default:
3549 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3550 }
3551
3552 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003553 if( used > buf_len )
3554 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003555
3556 return( 0 );
3557}
3558
3559/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003560 * Public wrapper for ssl_session_save()
3561 */
3562int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3563 unsigned char *buf,
3564 size_t buf_len,
3565 size_t *olen )
3566{
3567 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3568}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003569
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003570/*
3571 * Deserialize session, see mbedtls_ssl_session_save() for format.
3572 *
3573 * This internal version is wrapped by a public function that cleans up in
3574 * case of error, and has an extra option omit_header.
3575 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003576MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003577static int ssl_session_load( mbedtls_ssl_session *session,
3578 unsigned char omit_header,
3579 const unsigned char *buf,
3580 size_t len )
3581{
3582 const unsigned char *p = buf;
3583 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003584 size_t remaining_len;
3585
3586
3587 if( session == NULL )
3588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003589
3590 if( !omit_header )
3591 {
3592 /*
3593 * Check Mbed TLS version identifier
3594 */
3595
3596 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3598
3599 if( memcmp( p, ssl_serialized_session_header,
3600 sizeof( ssl_serialized_session_header ) ) != 0 )
3601 {
3602 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3603 }
3604 p += sizeof( ssl_serialized_session_header );
3605 }
3606
3607 /*
3608 * TLS version identifier
3609 */
3610 if( 1 > (size_t)( end - p ) )
3611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003612 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003613
3614 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003615 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003616 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003617 {
3618#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003619 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003620 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003621#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3622
Jerry Yu438ddd82022-07-07 06:55:50 +00003623#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3624 case MBEDTLS_SSL_VERSION_TLS1_3:
3625 return( ssl_tls13_session_load( session, p, remaining_len ) );
3626#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3627
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003628 default:
3629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3630 }
3631}
3632
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003633/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003634 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003635 */
3636int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3637 const unsigned char *buf,
3638 size_t len )
3639{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003640 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003641
3642 if( ret != 0 )
3643 mbedtls_ssl_session_free( session );
3644
3645 return( ret );
3646}
3647
3648/*
Paul Bakker1961b702013-01-25 14:49:24 +01003649 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003651MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003652static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3653{
3654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3655
Ronald Cron66dbf912022-02-02 15:33:46 +01003656 /*
3657 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003658 * that were written into the output buffer by the previous handshake step,
3659 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003660 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3661 * We proceed to the next handshake step only when all data from the
3662 * previous one have been sent to the peer, thus we make sure that this is
3663 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3664 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3665 * we have to wait before to go ahead.
3666 * In the case of TLS 1.3, handshake step handlers do not send data to the
3667 * peer. Data are only sent here and through
3668 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003669 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003670 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003671 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3672 return( ret );
3673
3674#if defined(MBEDTLS_SSL_PROTO_DTLS)
3675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3676 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3677 {
3678 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3679 return( ret );
3680 }
3681#endif /* MBEDTLS_SSL_PROTO_DTLS */
3682
3683 return( ret );
3684}
3685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003687{
Hanno Becker41934dd2021-08-07 19:13:43 +01003688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003689
Hanno Becker41934dd2021-08-07 19:13:43 +01003690 if( ssl == NULL ||
3691 ssl->conf == NULL ||
3692 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003693 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003694 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003696 }
3697
3698 ret = ssl_prepare_handshake_step( ssl );
3699 if( ret != 0 )
3700 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003701
Jerry Yue7047812021-09-13 19:26:39 +08003702 ret = mbedtls_ssl_handle_pending_alert( ssl );
3703 if( ret != 0 )
3704 goto cleanup;
3705
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003706 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3707 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3708 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003711 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003712 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3714 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003715
Ronald Cron9f0fba32022-02-10 16:45:15 +01003716 switch( ssl->state )
3717 {
3718 case MBEDTLS_SSL_HELLO_REQUEST:
3719 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003720 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003721 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003722
Ronald Cron9f0fba32022-02-10 16:45:15 +01003723 case MBEDTLS_SSL_CLIENT_HELLO:
3724 ret = mbedtls_ssl_write_client_hello( ssl );
3725 break;
3726
3727 default:
3728#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003729 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003730 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3731 else
3732 ret = mbedtls_ssl_handshake_client_step( ssl );
3733#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3734 ret = mbedtls_ssl_handshake_client_step( ssl );
3735#else
3736 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3737#endif
3738 }
Jerry Yub9930e72021-08-06 17:11:51 +08003739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003742 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003743 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003744#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003745 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003746 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003747#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003748
3749#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3750 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3751 ret = mbedtls_ssl_handshake_server_step( ssl );
3752#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003754#endif
3755
Jerry Yue7047812021-09-13 19:26:39 +08003756 if( ret != 0 )
3757 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003758 /* handshake_step return error. And it is same
3759 * with alert_reason.
3760 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003761 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003762 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003763 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003764 goto cleanup;
3765 }
3766 }
3767
3768cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003769 return( ret );
3770}
3771
3772/*
3773 * Perform the SSL handshake
3774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003776{
3777 int ret = 0;
3778
Hanno Beckera817ea42020-10-20 15:20:23 +01003779 /* Sanity checks */
3780
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003781 if( ssl == NULL || ssl->conf == NULL )
3782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3783
Hanno Beckera817ea42020-10-20 15:20:23 +01003784#if defined(MBEDTLS_SSL_PROTO_DTLS)
3785 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3786 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3787 {
3788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3789 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3791 }
3792#endif /* MBEDTLS_SSL_PROTO_DTLS */
3793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003795
Hanno Beckera817ea42020-10-20 15:20:23 +01003796 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003797 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003800
3801 if( ret != 0 )
3802 break;
3803 }
3804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
3807 return( ret );
3808}
3809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810#if defined(MBEDTLS_SSL_RENEGOTIATION)
3811#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003812/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003813 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003814 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003815MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003817{
Janos Follath865b3eb2019-12-16 11:46:15 +00003818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003821
3822 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3824 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003825
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003826 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003827 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003829 return( ret );
3830 }
3831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003833
3834 return( 0 );
3835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003837
3838/*
3839 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 * - any side: calling mbedtls_ssl_renegotiate(),
3841 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3842 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003843 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003844 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003846 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003847int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003848{
Janos Follath865b3eb2019-12-16 11:46:15 +00003849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003852
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003853 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3854 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003855
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003856 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3857 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003859 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003861 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003862 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003863 ssl->handshake->out_msg_seq = 1;
3864 else
3865 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003866 }
3867#endif
3868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3870 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003875 return( ret );
3876 }
3877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003879
3880 return( 0 );
3881}
3882
3883/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003884 * Renegotiate current connection on client,
3885 * or request renegotiation on server
3886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003890
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003891 if( ssl == NULL || ssl->conf == NULL )
3892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003895 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003896 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003897 {
Jerry Yu6848a612022-10-27 13:03:26 +08003898 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003902
3903 /* Did we already try/start sending HelloRequest? */
3904 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003906
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003907 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003908 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003912 /*
3913 * On client, either start the renegotiation process or,
3914 * if already in progress, continue the handshake
3915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003917 {
Jerry Yu6848a612022-10-27 13:03:26 +08003918 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003920
Hanno Becker40cdaa12020-02-05 10:48:27 +00003921 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003922 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003924 return( ret );
3925 }
3926 }
3927 else
3928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003932 return( ret );
3933 }
3934 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003936
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003937 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003940
Gilles Peskine9b562d52018-04-25 20:32:43 +02003941void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003942{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003943 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3944
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003945 if( handshake == NULL )
3946 return;
3947
Brett Warrene0edc842021-08-17 09:53:13 +01003948#if defined(MBEDTLS_ECP_C)
3949#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3950 if ( ssl->handshake->group_list_heap_allocated )
3951 mbedtls_free( (void*) handshake->group_list );
3952 handshake->group_list = NULL;
3953#endif /* MBEDTLS_DEPRECATED_REMOVED */
3954#endif /* MBEDTLS_ECP_C */
3955
Ronald Crone68ab4f2022-10-05 12:46:29 +02003956#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003957#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3958 if ( ssl->handshake->sig_algs_heap_allocated )
3959 mbedtls_free( (void*) handshake->sig_algs );
3960 handshake->sig_algs = NULL;
3961#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003962#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3963 if( ssl->handshake->certificate_request_context )
3964 {
3965 mbedtls_free( (void*) handshake->certificate_request_context );
3966 }
3967#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003968#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003969
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003970#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3971 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3972 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003973 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003974 handshake->async_in_progress = 0;
3975 }
3976#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3977
Andrzej Kurek25f27152022-08-17 16:09:31 -04003978#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003979#if defined(MBEDTLS_USE_PSA_CRYPTO)
3980 psa_hash_abort( &handshake->fin_sha256_psa );
3981#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003982 mbedtls_sha256_free( &handshake->fin_sha256 );
3983#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003984#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003985#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003986#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003987 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003988#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003989 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003990#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003991#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993#if defined(MBEDTLS_DHM_C)
3994 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003995#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003996#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003998#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01003999
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004000#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004001#if defined(MBEDTLS_USE_PSA_CRYPTO)
4002 psa_pake_abort( &handshake->psa_pake_ctx );
4003 psa_destroy_key( handshake->psa_pake_password );
4004 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4005#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004006 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004007#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004008#if defined(MBEDTLS_SSL_CLI_C)
4009 mbedtls_free( handshake->ecjpake_cache );
4010 handshake->ecjpake_cache = NULL;
4011 handshake->ecjpake_cache_len = 0;
4012#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004013#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004014
Janos Follath4ae5c292016-02-10 11:27:43 +00004015#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4016 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004017 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004019#endif
4020
Ronald Cron73fe8df2022-10-05 14:31:43 +02004021#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004022#if defined(MBEDTLS_USE_PSA_CRYPTO)
4023 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4024 {
4025 /* The maintenance of the external PSK key slot is the
4026 * user's responsibility. */
4027 if( ssl->handshake->psk_opaque_is_internal )
4028 {
4029 psa_destroy_key( ssl->handshake->psk_opaque );
4030 ssl->handshake->psk_opaque_is_internal = 0;
4031 }
4032 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4033 }
Neil Armstronge952a302022-05-03 10:22:14 +02004034#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004035 if( handshake->psk != NULL )
4036 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004037 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004038 mbedtls_free( handshake->psk );
4039 }
Neil Armstronge952a302022-05-03 10:22:14 +02004040#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004041#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4044 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004045 /*
4046 * Free only the linked list wrapper, not the keys themselves
4047 * since the belong to the SNI callback
4048 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004049 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004051
Gilles Peskineeccd8882020-03-10 12:19:08 +01004052#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004053 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004054 if( handshake->ecrs_peer_cert != NULL )
4055 {
4056 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4057 mbedtls_free( handshake->ecrs_peer_cert );
4058 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004059#endif
4060
Hanno Becker75173122019-02-06 16:18:31 +00004061#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4062 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4063 mbedtls_pk_free( &handshake->peer_pubkey );
4064#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4065
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004066#if defined(MBEDTLS_SSL_CLI_C) && \
4067 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4068 mbedtls_free( handshake->cookie );
4069#endif /* MBEDTLS_SSL_CLI_C &&
4070 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004071
4072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004073 mbedtls_ssl_flight_free( handshake->flight );
4074 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004075#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004076
Ronald Cronf12b81d2022-03-15 10:42:41 +01004077#if defined(MBEDTLS_ECDH_C) && \
4078 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004079 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004080 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004081#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4082
Ronald Cron6f135e12021-12-08 16:57:54 +01004083#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004084 mbedtls_ssl_transform_free( handshake->transform_handshake );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004085 mbedtls_free( handshake->transform_handshake );
4086#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yua1a568c2021-11-09 10:17:21 +08004087 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004088 mbedtls_free( handshake->transform_earlydata );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004089#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004090#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004091
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004092
4093#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4094 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4095 * processes datagrams and the fact that a datagram is allowed to have
4096 * several records in it, it is possible that the I/O buffers are not
4097 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004098 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4099 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004100#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004101
Jerry Yuba9c7272021-10-30 11:54:10 +08004102 /* mbedtls_platform_zeroize MUST be last one in this function */
4103 mbedtls_platform_zeroize( handshake,
4104 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004105}
4106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004108{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004109 if( session == NULL )
4110 return;
4111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004113 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004114#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004115
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004116#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004117#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4118 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004119 mbedtls_free( session->hostname );
4120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004122#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004123
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004124 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004125}
4126
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004127#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004128
4129#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4130#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4131#else
4132#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4133#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4134
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004135#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004136
4137#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4138#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4139#else
4140#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4141#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4142
4143#if defined(MBEDTLS_SSL_ALPN)
4144#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4145#else
4146#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4147#endif /* MBEDTLS_SSL_ALPN */
4148
4149#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4150#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4151#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4152#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4153
4154#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4155 ( (uint32_t) ( \
4156 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4157 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4158 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4159 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4160 0u ) )
4161
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004162static unsigned char ssl_serialized_context_header[] = {
4163 MBEDTLS_VERSION_MAJOR,
4164 MBEDTLS_VERSION_MINOR,
4165 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004166 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4167 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4168 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4169 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4170 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004171};
4172
Paul Bakker5121ce52009-01-03 21:22:43 +00004173/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004174 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004175 *
4176 * The format of the serialized data is:
4177 * (in the presentation language of TLS, RFC 8446 section 3)
4178 *
4179 * // header
4180 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004181 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004182 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004183 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004184 * Note: When updating the format, remember to keep these
4185 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004186 *
4187 * // session sub-structure
4188 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4189 * // transform sub-structure
4190 * uint8 random[64]; // ServerHello.random+ClientHello.random
4191 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4192 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4193 * // fields from ssl_context
4194 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4195 * uint64 in_window_top; // DTLS: last validated record seq_num
4196 * uint64 in_window; // DTLS: bitmask for replay protection
4197 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4198 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4199 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4200 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4201 *
4202 * Note that many fields of the ssl_context or sub-structures are not
4203 * serialized, as they fall in one of the following categories:
4204 *
4205 * 1. forced value (eg in_left must be 0)
4206 * 2. pointer to dynamically-allocated memory (eg session, transform)
4207 * 3. value can be re-derived from other data (eg session keys from MS)
4208 * 4. value was temporary (eg content of input buffer)
4209 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004210 */
4211int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4212 unsigned char *buf,
4213 size_t buf_len,
4214 size_t *olen )
4215{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004216 unsigned char *p = buf;
4217 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004218 size_t session_len;
4219 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004220
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004221 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004222 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4223 * this function's documentation.
4224 *
4225 * These are due to assumptions/limitations in the implementation. Some of
4226 * them are likely to stay (no handshake in progress) some might go away
4227 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004228 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004229 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004230 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004231 {
4232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004234 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004235 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004236 {
4237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004239 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004240 /* Double-check that sub-structures are indeed ready */
4241 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004242 {
4243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004245 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004246 /* There must be no pending incoming or outgoing data */
4247 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004248 {
4249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004251 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004252 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004253 {
4254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004256 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004257 /* Protocol must be DLTS, not TLS */
4258 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004259 {
4260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004262 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004263 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004264 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004265 {
4266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004268 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004269 /* We must be using an AEAD ciphersuite */
4270 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004271 {
4272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004274 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004275 /* Renegotiation must not be enabled */
4276#if defined(MBEDTLS_SSL_RENEGOTIATION)
4277 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004278 {
4279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004281 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004282#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004283
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004284 /*
4285 * Version and format identifier
4286 */
4287 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004288
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004289 if( used <= buf_len )
4290 {
4291 memcpy( p, ssl_serialized_context_header,
4292 sizeof( ssl_serialized_context_header ) );
4293 p += sizeof( ssl_serialized_context_header );
4294 }
4295
4296 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004297 * Session (length + data)
4298 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004299 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004300 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4301 return( ret );
4302
4303 used += 4 + session_len;
4304 if( used <= buf_len )
4305 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004306 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4307 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004308
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004309 ret = ssl_session_save( ssl->session, 1,
4310 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004311 if( ret != 0 )
4312 return( ret );
4313
4314 p += session_len;
4315 }
4316
4317 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004318 * Transform
4319 */
4320 used += sizeof( ssl->transform->randbytes );
4321 if( used <= buf_len )
4322 {
4323 memcpy( p, ssl->transform->randbytes,
4324 sizeof( ssl->transform->randbytes ) );
4325 p += sizeof( ssl->transform->randbytes );
4326 }
4327
4328#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4329 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4330 if( used <= buf_len )
4331 {
4332 *p++ = ssl->transform->in_cid_len;
4333 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4334 p += ssl->transform->in_cid_len;
4335
4336 *p++ = ssl->transform->out_cid_len;
4337 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4338 p += ssl->transform->out_cid_len;
4339 }
4340#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4341
4342 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004343 * Saved fields from top-level ssl_context structure
4344 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004345 used += 4;
4346 if( used <= buf_len )
4347 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004348 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4349 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004350 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004351
4352#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4353 used += 16;
4354 if( used <= buf_len )
4355 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004356 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4357 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004358
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004359 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4360 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004361 }
4362#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4363
4364#if defined(MBEDTLS_SSL_PROTO_DTLS)
4365 used += 1;
4366 if( used <= buf_len )
4367 {
4368 *p++ = ssl->disable_datagram_packing;
4369 }
4370#endif /* MBEDTLS_SSL_PROTO_DTLS */
4371
Jerry Yuae0b2e22021-10-08 15:21:19 +08004372 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004373 if( used <= buf_len )
4374 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004375 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4376 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004377 }
4378
4379#if defined(MBEDTLS_SSL_PROTO_DTLS)
4380 used += 2;
4381 if( used <= buf_len )
4382 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004383 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4384 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004385 }
4386#endif /* MBEDTLS_SSL_PROTO_DTLS */
4387
4388#if defined(MBEDTLS_SSL_ALPN)
4389 {
4390 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004391 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004392 : 0;
4393
4394 used += 1 + alpn_len;
4395 if( used <= buf_len )
4396 {
4397 *p++ = alpn_len;
4398
4399 if( ssl->alpn_chosen != NULL )
4400 {
4401 memcpy( p, ssl->alpn_chosen, alpn_len );
4402 p += alpn_len;
4403 }
4404 }
4405 }
4406#endif /* MBEDTLS_SSL_ALPN */
4407
4408 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004409 * Done
4410 */
4411 *olen = used;
4412
4413 if( used > buf_len )
4414 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004415
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004416 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4417
Hanno Becker43aefe22020-02-05 10:44:56 +00004418 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004419}
4420
4421/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004422 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004423 *
4424 * This internal version is wrapped by a public function that cleans up in
4425 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004426 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004427MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004428static int ssl_context_load( mbedtls_ssl_context *ssl,
4429 const unsigned char *buf,
4430 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004431{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004432 const unsigned char *p = buf;
4433 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004434 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004436#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004437 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004438#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004439
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004440 /*
4441 * The context should have been freshly setup or reset.
4442 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004443 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004444 * renegotiating, or if the user mistakenly loaded a session first.)
4445 */
4446 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4447 ssl->session != NULL )
4448 {
4449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4450 }
4451
4452 /*
4453 * We can't check that the config matches the initial one, but we can at
4454 * least check it matches the requirements for serializing.
4455 */
4456 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004457 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4458 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004459#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004460 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004461#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004462 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004463 {
4464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4465 }
4466
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004467 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4468
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004469 /*
4470 * Check version identifier
4471 */
4472 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4474
4475 if( memcmp( p, ssl_serialized_context_header,
4476 sizeof( ssl_serialized_context_header ) ) != 0 )
4477 {
4478 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4479 }
4480 p += sizeof( ssl_serialized_context_header );
4481
4482 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004483 * Session
4484 */
4485 if( (size_t)( end - p ) < 4 )
4486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4487
4488 session_len = ( (size_t) p[0] << 24 ) |
4489 ( (size_t) p[1] << 16 ) |
4490 ( (size_t) p[2] << 8 ) |
4491 ( (size_t) p[3] );
4492 p += 4;
4493
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004494 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004495 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004496 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004497 ssl->session_in = ssl->session;
4498 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004499 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004500
4501 if( (size_t)( end - p ) < session_len )
4502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4503
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004504 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004505 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004506 {
4507 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004508 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004509 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004510
4511 p += session_len;
4512
4513 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004514 * Transform
4515 */
4516
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004517 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004518 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004519 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004520 ssl->transform_in = ssl->transform;
4521 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004522 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004523
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004524#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004525 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4526 if( prf_func == NULL )
4527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4528
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004529 /* Read random bytes and populate structure */
4530 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004532
Hanno Beckerbd257552021-03-22 06:59:27 +00004533 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004534 ssl->session->ciphersuite,
4535 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004536#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004537 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004538#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004539 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004540 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004541 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004542 ssl->conf->endpoint,
4543 ssl );
4544 if( ret != 0 )
4545 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004547 p += sizeof( ssl->transform->randbytes );
4548
4549#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4550 /* Read connection IDs and store them */
4551 if( (size_t)( end - p ) < 1 )
4552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4553
4554 ssl->transform->in_cid_len = *p++;
4555
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004556 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4558
4559 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4560 p += ssl->transform->in_cid_len;
4561
4562 ssl->transform->out_cid_len = *p++;
4563
4564 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4566
4567 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4568 p += ssl->transform->out_cid_len;
4569#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4570
4571 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004572 * Saved fields from top-level ssl_context structure
4573 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004574 if( (size_t)( end - p ) < 4 )
4575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4576
4577 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4578 ( (uint32_t) p[1] << 16 ) |
4579 ( (uint32_t) p[2] << 8 ) |
4580 ( (uint32_t) p[3] );
4581 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004582
4583#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4584 if( (size_t)( end - p ) < 16 )
4585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4586
4587 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4588 ( (uint64_t) p[1] << 48 ) |
4589 ( (uint64_t) p[2] << 40 ) |
4590 ( (uint64_t) p[3] << 32 ) |
4591 ( (uint64_t) p[4] << 24 ) |
4592 ( (uint64_t) p[5] << 16 ) |
4593 ( (uint64_t) p[6] << 8 ) |
4594 ( (uint64_t) p[7] );
4595 p += 8;
4596
4597 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4598 ( (uint64_t) p[1] << 48 ) |
4599 ( (uint64_t) p[2] << 40 ) |
4600 ( (uint64_t) p[3] << 32 ) |
4601 ( (uint64_t) p[4] << 24 ) |
4602 ( (uint64_t) p[5] << 16 ) |
4603 ( (uint64_t) p[6] << 8 ) |
4604 ( (uint64_t) p[7] );
4605 p += 8;
4606#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4607
4608#if defined(MBEDTLS_SSL_PROTO_DTLS)
4609 if( (size_t)( end - p ) < 1 )
4610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4611
4612 ssl->disable_datagram_packing = *p++;
4613#endif /* MBEDTLS_SSL_PROTO_DTLS */
4614
Jerry Yud9a94fe2021-09-28 18:58:59 +08004615 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004617 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4618 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004619
4620#if defined(MBEDTLS_SSL_PROTO_DTLS)
4621 if( (size_t)( end - p ) < 2 )
4622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4623
4624 ssl->mtu = ( p[0] << 8 ) | p[1];
4625 p += 2;
4626#endif /* MBEDTLS_SSL_PROTO_DTLS */
4627
4628#if defined(MBEDTLS_SSL_ALPN)
4629 {
4630 uint8_t alpn_len;
4631 const char **cur;
4632
4633 if( (size_t)( end - p ) < 1 )
4634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4635
4636 alpn_len = *p++;
4637
4638 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4639 {
4640 /* alpn_chosen should point to an item in the configured list */
4641 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4642 {
4643 if( strlen( *cur ) == alpn_len &&
4644 memcmp( p, cur, alpn_len ) == 0 )
4645 {
4646 ssl->alpn_chosen = *cur;
4647 break;
4648 }
4649 }
4650 }
4651
4652 /* can only happen on conf mismatch */
4653 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4655
4656 p += alpn_len;
4657 }
4658#endif /* MBEDTLS_SSL_ALPN */
4659
4660 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004661 * Forced fields from top-level ssl_context structure
4662 *
4663 * Most of them already set to the correct value by mbedtls_ssl_init() and
4664 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4665 */
4666 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004667 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004668
Hanno Becker361b10d2019-08-30 10:42:49 +01004669 /* Adjust pointers for header fields of outgoing records to
4670 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004671 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004672
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004673#if defined(MBEDTLS_SSL_PROTO_DTLS)
4674 ssl->in_epoch = 1;
4675#endif
4676
4677 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4678 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004679 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4680 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004681 if( ssl->handshake != NULL )
4682 {
4683 mbedtls_ssl_handshake_free( ssl );
4684 mbedtls_free( ssl->handshake );
4685 ssl->handshake = NULL;
4686 }
4687
4688 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004689 * Done - should have consumed entire buffer
4690 */
4691 if( p != end )
4692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004693
4694 return( 0 );
4695}
4696
4697/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004698 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004699 */
4700int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4701 const unsigned char *buf,
4702 size_t len )
4703{
4704 int ret = ssl_context_load( context, buf, len );
4705
4706 if( ret != 0 )
4707 mbedtls_ssl_free( context );
4708
4709 return( ret );
4710}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004711#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004712
4713/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004714 * Free an SSL context
4715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004716void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004717{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004718 if( ssl == NULL )
4719 return;
4720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004722
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004723 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 {
sander-visserb8aa2072020-05-06 22:05:13 +02004725#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4726 size_t out_buf_len = ssl->out_buf_len;
4727#else
4728 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4729#endif
4730
Darryl Greenb33cc762019-11-28 14:29:44 +00004731 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004733 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 }
4735
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004736 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 {
sander-visserb8aa2072020-05-06 22:05:13 +02004738#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4739 size_t in_buf_len = ssl->in_buf_len;
4740#else
4741 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4742#endif
4743
Darryl Greenb33cc762019-11-28 14:29:44 +00004744 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004746 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004747 }
4748
Paul Bakker48916f92012-09-16 19:57:18 +00004749 if( ssl->transform )
4750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 mbedtls_ssl_transform_free( ssl->transform );
4752 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004753 }
4754
4755 if( ssl->handshake )
4756 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004757 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4759 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 mbedtls_free( ssl->handshake );
4762 mbedtls_free( ssl->transform_negotiate );
4763 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004764 }
4765
Ronald Cron6f135e12021-12-08 16:57:54 +01004766#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004767 mbedtls_ssl_transform_free( ssl->transform_application );
4768 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004769#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004770
Paul Bakkerc0463502013-02-14 11:19:38 +01004771 if( ssl->session )
4772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 mbedtls_ssl_session_free( ssl->session );
4774 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004775 }
4776
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004777#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004778 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004780 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004782 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004783#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004784
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004785#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004787#endif
4788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004790
Paul Bakker86f04f42013-02-14 11:20:09 +01004791 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004792 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004793}
4794
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004795/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004796 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004797 */
4798void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4799{
4800 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4801}
4802
Gilles Peskineae270bf2021-06-02 00:05:29 +02004803/* The selection should be the same as mbedtls_x509_crt_profile_default in
4804 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004805 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004806 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004807 * about this list.
4808 */
Brett Warrene0edc842021-08-17 09:53:13 +01004809static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004810#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004811 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004812#endif
4813#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004814 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004815#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004816#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004817 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004818#endif
4819#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004820 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004821#endif
4822#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004823 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004824#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004825#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004826 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004827#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004828#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004829 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004830#endif
4831#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004832 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004833#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004834 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004835};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004836
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004837static int ssl_preset_suiteb_ciphersuites[] = {
4838 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4839 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4840 0
4841};
4842
Ronald Crone68ab4f2022-10-05 12:46:29 +02004843#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004844
Jerry Yu909df7b2022-01-22 11:56:27 +08004845/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004846 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004847 * rules SHOULD be upheld.
4848 * - No duplicate entries.
4849 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004850 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004851 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004852 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004853static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004854
Andrzej Kurek25f27152022-08-17 16:09:31 -04004855#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 +08004856 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4857 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004858#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004859 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004860
Andrzej Kurek25f27152022-08-17 16:09:31 -04004861#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 +08004862 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4863 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004864#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004865 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4866
Andrzej Kurek25f27152022-08-17 16:09:31 -04004867#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 +08004868 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4869 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004870#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004871 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004872
Andrzej Kurek25f27152022-08-17 16:09:31 -04004873#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 +08004874 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004875#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 +08004876
Andrzej Kurek25f27152022-08-17 16:09:31 -04004877#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 +08004878 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004879#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 +08004880
Andrzej Kurek25f27152022-08-17 16:09:31 -04004881#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 +08004882 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004883#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 +08004884
Andrzej Kurek25f27152022-08-17 16:09:31 -04004885#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 +08004886 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4887#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4888
Andrzej Kurek25f27152022-08-17 16:09:31 -04004889#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 +08004890 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4891#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4892
Andrzej Kurek25f27152022-08-17 16:09:31 -04004893#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 +08004894 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4895#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4896
Gabor Mezei15b95a62022-05-09 16:37:58 +02004897 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004898};
4899
4900/* NOTICE: see above */
4901#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4902static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004903#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004904#if defined(MBEDTLS_ECDSA_C)
4905 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004906#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004907#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4908 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4909#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004910#if defined(MBEDTLS_RSA_C)
4911 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4912#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004913#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004914#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004915#if defined(MBEDTLS_ECDSA_C)
4916 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004917#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004918#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4919 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4920#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004921#if defined(MBEDTLS_RSA_C)
4922 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4923#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004924#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004925#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004926#if defined(MBEDTLS_ECDSA_C)
4927 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004928#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004929#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4930 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4931#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004932#if defined(MBEDTLS_RSA_C)
4933 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4934#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004935#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004936 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004937};
4938#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4939/* NOTICE: see above */
4940static uint16_t ssl_preset_suiteb_sig_algs[] = {
4941
Andrzej Kurek25f27152022-08-17 16:09:31 -04004942#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 +08004943 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4944 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004945#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004946 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4947
Andrzej Kurek25f27152022-08-17 16:09:31 -04004948#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 +08004949 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4950 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004951#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004952 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004953
Andrzej Kurek25f27152022-08-17 16:09:31 -04004954#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 +08004955 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004956#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 +08004957
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#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 +08004959 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004960#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004961
Gabor Mezei15b95a62022-05-09 16:37:58 +02004962 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004963};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004964
Jerry Yu909df7b2022-01-22 11:56:27 +08004965/* NOTICE: see above */
4966#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4967static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004968#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004969#if defined(MBEDTLS_ECDSA_C)
4970 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004971#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004972#if defined(MBEDTLS_RSA_C)
4973 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4974#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004975#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004976#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004977#if defined(MBEDTLS_ECDSA_C)
4978 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004979#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004980#if defined(MBEDTLS_RSA_C)
4981 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4982#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004983#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004984 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004985};
Jerry Yu909df7b2022-01-22 11:56:27 +08004986#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4987
Ronald Crone68ab4f2022-10-05 12:46:29 +02004988#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004989
Brett Warrene0edc842021-08-17 09:53:13 +01004990static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004991#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004992 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004993#endif
4994#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004995 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004996#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004997 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004998};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004999
Ronald Crone68ab4f2022-10-05 12:46:29 +02005000#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005001/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005002 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005003MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005004static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005005{
5006 size_t i, j;
5007 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005008
Gabor Mezei15b95a62022-05-09 16:37:58 +02005009 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005010 {
Jerry Yuf377d642022-01-25 10:43:59 +08005011 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005012 {
Jerry Yuf377d642022-01-25 10:43:59 +08005013 if( sig_algs[i] != sig_algs[j] )
5014 continue;
5015 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5016 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5017 sig_algs[i], j, i );
5018 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005019 }
5020 }
5021 return( ret );
5022}
5023
Ronald Crone68ab4f2022-10-05 12:46:29 +02005024#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005025
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005026/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005027 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005028 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005029int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005030 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005031{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005032#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005034#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005035
Ronald Crone68ab4f2022-10-05 12:46:29 +02005036#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005037 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005038 {
5039 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5040 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5041 }
5042
Jerry Yuf377d642022-01-25 10:43:59 +08005043 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005044 {
5045 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5046 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5047 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005048
5049#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005050 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005051 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005052 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005053 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5054 }
5055
Jerry Yuf377d642022-01-25 10:43:59 +08005056 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005057 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005058 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005059 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5060 }
5061#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005062#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005063
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005064 /* Use the functions here so that they are covered in tests,
5065 * but otherwise access member directly for efficiency */
5066 mbedtls_ssl_conf_endpoint( conf, endpoint );
5067 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005068
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005069 /*
5070 * Things that are common to all presets
5071 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005072#if defined(MBEDTLS_SSL_CLI_C)
5073 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5074 {
5075 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5076#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5077 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5078#endif
5079 }
5080#endif
5081
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005082#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5083 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5084#endif
5085
5086#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5087 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5088#endif
5089
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005090#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005091 conf->f_cookie_write = ssl_cookie_write_dummy;
5092 conf->f_cookie_check = ssl_cookie_check_dummy;
5093#endif
5094
5095#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5096 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5097#endif
5098
Janos Follath088ce432017-04-10 12:42:31 +01005099#if defined(MBEDTLS_SSL_SRV_C)
5100 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005101 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005102#endif
5103
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005104#if defined(MBEDTLS_SSL_PROTO_DTLS)
5105 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5106 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5107#endif
5108
5109#if defined(MBEDTLS_SSL_RENEGOTIATION)
5110 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005111 memset( conf->renego_period, 0x00, 2 );
5112 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005113#endif
5114
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005115#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005116 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5117 {
5118 const unsigned char dhm_p[] =
5119 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5120 const unsigned char dhm_g[] =
5121 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005122
Hanno Beckere2defad2021-07-24 05:59:17 +01005123 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5124 dhm_p, sizeof( dhm_p ),
5125 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5126 {
5127 return( ret );
5128 }
5129 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005130#endif
5131
Ronald Cron6f135e12021-12-08 16:57:54 +01005132#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005133#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005134 mbedtls_ssl_conf_new_session_tickets(
5135 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5136#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005137 /*
5138 * Allow all TLS 1.3 key exchange modes by default.
5139 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005140 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005141#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005142
XiaokangQian4d3a6042022-04-21 13:46:17 +00005143 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5144 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005146 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005147 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005148#else
XiaokangQian060d8672022-04-21 09:24:56 +00005149 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005150#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005151 }
5152 else
5153 {
5154#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5155 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5156 {
5157 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5158 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5159 }
5160 else
5161 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5162 {
5163 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5164 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5165 }
5166#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5167 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5168 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5169#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5170 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5171 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5172#else
5173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5174#endif
5175 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005176
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005177 /*
5178 * Preset-specific defaults
5179 */
5180 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005181 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005182 /*
5183 * NSA Suite B
5184 */
5185 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005186
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005187 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005188
5189#if defined(MBEDTLS_X509_CRT_PARSE_C)
5190 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005191#endif
5192
Ronald Crone68ab4f2022-10-05 12:46:29 +02005193#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005194#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5195 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5196 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5197 else
5198#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5199 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005200#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005201
Brett Warrene0edc842021-08-17 09:53:13 +01005202#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5203 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005204#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005205 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005206 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005207
5208 /*
5209 * Default
5210 */
5211 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005212
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005213 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005214
5215#if defined(MBEDTLS_X509_CRT_PARSE_C)
5216 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5217#endif
5218
Ronald Crone68ab4f2022-10-05 12:46:29 +02005219#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005220#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5221 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5222 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5223 else
5224#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5225 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005226#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005227
Brett Warrene0edc842021-08-17 09:53:13 +01005228#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5229 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005230#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005231 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005232
5233#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5234 conf->dhm_min_bitlen = 1024;
5235#endif
5236 }
5237
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005238 return( 0 );
5239}
5240
5241/*
5242 * Free mbedtls_ssl_config
5243 */
5244void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5245{
5246#if defined(MBEDTLS_DHM_C)
5247 mbedtls_mpi_free( &conf->dhm_P );
5248 mbedtls_mpi_free( &conf->dhm_G );
5249#endif
5250
Ronald Cron73fe8df2022-10-05 14:31:43 +02005251#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005252#if defined(MBEDTLS_USE_PSA_CRYPTO)
5253 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5254 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005255 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5256 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005257#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005258 if( conf->psk != NULL )
5259 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005260 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005261 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005262 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005263 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005264 }
5265
5266 if( conf->psk_identity != NULL )
5267 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005268 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005269 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005270 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005271 conf->psk_identity_len = 0;
5272 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005273#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005274
5275#if defined(MBEDTLS_X509_CRT_PARSE_C)
5276 ssl_key_cert_free( conf->key_cert );
5277#endif
5278
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005279 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005280}
5281
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005282#if defined(MBEDTLS_PK_C) && \
5283 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005284/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289#if defined(MBEDTLS_RSA_C)
5290 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5291 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293#if defined(MBEDTLS_ECDSA_C)
5294 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5295 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005296#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005298}
5299
Hanno Becker7e5437a2017-04-28 17:15:26 +01005300unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5301{
5302 switch( type ) {
5303 case MBEDTLS_PK_RSA:
5304 return( MBEDTLS_SSL_SIG_RSA );
5305 case MBEDTLS_PK_ECDSA:
5306 case MBEDTLS_PK_ECKEY:
5307 return( MBEDTLS_SSL_SIG_ECDSA );
5308 default:
5309 return( MBEDTLS_SSL_SIG_ANON );
5310 }
5311}
5312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005314{
5315 switch( sig )
5316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317#if defined(MBEDTLS_RSA_C)
5318 case MBEDTLS_SSL_SIG_RSA:
5319 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005320#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321#if defined(MBEDTLS_ECDSA_C)
5322 case MBEDTLS_SSL_SIG_ECDSA:
5323 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005324#endif
5325 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005327 }
5328}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005329#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005330
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005331/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005332 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005335{
5336 switch( hash )
5337 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005338#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 case MBEDTLS_SSL_HASH_MD5:
5340 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005341#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005342#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 case MBEDTLS_SSL_HASH_SHA1:
5344 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005345#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005346#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 case MBEDTLS_SSL_HASH_SHA224:
5348 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005349#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005350#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 case MBEDTLS_SSL_HASH_SHA256:
5352 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005353#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005354#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 case MBEDTLS_SSL_HASH_SHA384:
5356 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005357#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005358#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 case MBEDTLS_SSL_HASH_SHA512:
5360 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005361#endif
5362 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005364 }
5365}
5366
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005367/*
5368 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5369 */
5370unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5371{
5372 switch( md )
5373 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005374#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005375 case MBEDTLS_MD_MD5:
5376 return( MBEDTLS_SSL_HASH_MD5 );
5377#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005378#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005379 case MBEDTLS_MD_SHA1:
5380 return( MBEDTLS_SSL_HASH_SHA1 );
5381#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005382#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005383 case MBEDTLS_MD_SHA224:
5384 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005385#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005386#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005387 case MBEDTLS_MD_SHA256:
5388 return( MBEDTLS_SSL_HASH_SHA256 );
5389#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005390#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005391 case MBEDTLS_MD_SHA384:
5392 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005393#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005394#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005395 case MBEDTLS_MD_SHA512:
5396 return( MBEDTLS_SSL_HASH_SHA512 );
5397#endif
5398 default:
5399 return( MBEDTLS_SSL_HASH_NONE );
5400 }
5401}
5402
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005403/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005404 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005405 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005406 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005407int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005408{
Brett Warrene0edc842021-08-17 09:53:13 +01005409 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005410
Brett Warrene0edc842021-08-17 09:53:13 +01005411 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005412 return( -1 );
5413
Brett Warrene0edc842021-08-17 09:53:13 +01005414 for( ; *group_list != 0; group_list++ )
5415 {
5416 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005417 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005418 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005419
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005420 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005421}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005422
5423#if defined(MBEDTLS_ECP_C)
5424/*
5425 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5426 */
5427int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5428{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005429 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005430 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005431
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005432 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005433 return -1;
5434
5435 uint16_t tls_id = grp_info->tls_id;
5436
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005437 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5438}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005439#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441#if defined(MBEDTLS_X509_CRT_PARSE_C)
5442int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5443 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005444 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005445 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005446{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005447 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005448 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005449 const char *ext_oid;
5450 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005453 {
5454 /* Server part of the key exchange */
5455 switch( ciphersuite->key_exchange )
5456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 case MBEDTLS_KEY_EXCHANGE_RSA:
5458 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005459 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005460 break;
5461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5463 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5464 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5465 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005466 break;
5467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5469 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005470 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005471 break;
5472
5473 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 case MBEDTLS_KEY_EXCHANGE_NONE:
5475 case MBEDTLS_KEY_EXCHANGE_PSK:
5476 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5477 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005478 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005479 usage = 0;
5480 }
5481 }
5482 else
5483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5485 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005486 }
5487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005489 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005490 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005491 ret = -1;
5492 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5497 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005498 }
5499 else
5500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5502 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005503 }
5504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005506 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005507 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005508 ret = -1;
5509 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005510
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005511 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005514
Jerry Yu148165c2021-09-24 23:20:59 +08005515#if defined(MBEDTLS_USE_PSA_CRYPTO)
5516int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5517 const mbedtls_md_type_t md,
5518 unsigned char *dst,
5519 size_t dst_len,
5520 size_t *olen )
5521{
Ronald Cronf6893e12022-01-07 22:09:01 +01005522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5523 psa_hash_operation_t *hash_operation_to_clone;
5524 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5525
Jerry Yu148165c2021-09-24 23:20:59 +08005526 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005527
5528 switch( md )
5529 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005530#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005531 case MBEDTLS_MD_SHA384:
5532 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5533 break;
5534#endif
5535
Andrzej Kurek25f27152022-08-17 16:09:31 -04005536#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005537 case MBEDTLS_MD_SHA256:
5538 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5539 break;
5540#endif
5541
5542 default:
5543 goto exit;
5544 }
5545
5546 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5547 if( status != PSA_SUCCESS )
5548 goto exit;
5549
5550 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5551 if( status != PSA_SUCCESS )
5552 goto exit;
5553
5554exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005555#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5556 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5557 (void) ssl;
5558#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005559 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005560}
5561#else /* MBEDTLS_USE_PSA_CRYPTO */
5562
Andrzej Kurek25f27152022-08-17 16:09:31 -04005563#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005564MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005565static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5566 unsigned char *dst,
5567 size_t dst_len,
5568 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005569{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005570 int ret;
5571 mbedtls_sha512_context sha512;
5572
5573 if( dst_len < 48 )
5574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5575
5576 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005577 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005578
5579 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5580 {
5581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5582 goto exit;
5583 }
5584
5585 *olen = 48;
5586
5587exit:
5588
5589 mbedtls_sha512_free( &sha512 );
5590 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005591}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005592#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005593
Andrzej Kurek25f27152022-08-17 16:09:31 -04005594#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005595MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005596static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5597 unsigned char *dst,
5598 size_t dst_len,
5599 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005600{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005601 int ret;
5602 mbedtls_sha256_context sha256;
5603
5604 if( dst_len < 32 )
5605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5606
5607 mbedtls_sha256_init( &sha256 );
5608 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005609
Jerry Yu24c0ec32021-09-09 14:21:07 +08005610 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5611 {
5612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5613 goto exit;
5614 }
5615
5616 *olen = 32;
5617
5618exit:
5619
5620 mbedtls_sha256_free( &sha256 );
5621 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005622}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005623#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005624
Jerry Yu000f9762021-09-14 11:12:51 +08005625int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5626 const mbedtls_md_type_t md,
5627 unsigned char *dst,
5628 size_t dst_len,
5629 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005630{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005631 switch( md )
5632 {
5633
Andrzej Kurek25f27152022-08-17 16:09:31 -04005634#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005635 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005636 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005637#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005638
Andrzej Kurek25f27152022-08-17 16:09:31 -04005639#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005640 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005641 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005642#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005643
5644 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005645#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5646 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5647 (void) ssl;
5648 (void) dst;
5649 (void) dst_len;
5650 (void) olen;
5651#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005652 break;
5653 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005655}
XiaokangQian647719a2021-12-07 09:16:29 +00005656
Jerry Yu148165c2021-09-24 23:20:59 +08005657#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005658
Ronald Crone68ab4f2022-10-05 12:46:29 +02005659#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005660/* mbedtls_ssl_parse_sig_alg_ext()
5661 *
5662 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5663 * value (TLS 1.3 RFC8446):
5664 * enum {
5665 * ....
5666 * ecdsa_secp256r1_sha256( 0x0403 ),
5667 * ecdsa_secp384r1_sha384( 0x0503 ),
5668 * ecdsa_secp521r1_sha512( 0x0603 ),
5669 * ....
5670 * } SignatureScheme;
5671 *
5672 * struct {
5673 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5674 * } SignatureSchemeList;
5675 *
5676 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5677 * value (TLS 1.2 RFC5246):
5678 * enum {
5679 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5680 * sha512(6), (255)
5681 * } HashAlgorithm;
5682 *
5683 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5684 * SignatureAlgorithm;
5685 *
5686 * struct {
5687 * HashAlgorithm hash;
5688 * SignatureAlgorithm signature;
5689 * } SignatureAndHashAlgorithm;
5690 *
5691 * SignatureAndHashAlgorithm
5692 * supported_signature_algorithms<2..2^16-2>;
5693 *
5694 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5695 * generalization of the TLS 1.2 signature algorithm extension.
5696 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5697 * `SignatureScheme` field of TLS 1.3
5698 *
5699 */
5700int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5701 const unsigned char *buf,
5702 const unsigned char *end )
5703{
5704 const unsigned char *p = buf;
5705 size_t supported_sig_algs_len = 0;
5706 const unsigned char *supported_sig_algs_end;
5707 uint16_t sig_alg;
5708 uint32_t common_idx = 0;
5709
5710 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5711 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5712 p += 2;
5713
5714 memset( ssl->handshake->received_sig_algs, 0,
5715 sizeof(ssl->handshake->received_sig_algs) );
5716
5717 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5718 supported_sig_algs_end = p + supported_sig_algs_len;
5719 while( p < supported_sig_algs_end )
5720 {
5721 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5722 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5723 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005724 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5725 sig_alg,
5726 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005727#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005728 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005729 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5730 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5731 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005732 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005733 }
5734#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005735
Jerry Yuf3b46b52022-06-19 16:52:27 +08005736 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5737 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005738
5739 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5740 {
5741 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5742 common_idx += 1;
5743 }
5744 }
5745 /* Check that we consumed all the message. */
5746 if( p != end )
5747 {
5748 MBEDTLS_SSL_DEBUG_MSG( 1,
5749 ( "Signature algorithms extension length misaligned" ) );
5750 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5751 MBEDTLS_ERR_SSL_DECODE_ERROR );
5752 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5753 }
5754
5755 if( common_idx == 0 )
5756 {
5757 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5758 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5759 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5760 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5761 }
5762
Gabor Mezei15b95a62022-05-09 16:37:58 +02005763 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005764 return( 0 );
5765}
5766
Ronald Crone68ab4f2022-10-05 12:46:29 +02005767#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005768
Jerry Yudc7bd172022-02-17 13:44:15 +08005769#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5770
5771#if defined(MBEDTLS_USE_PSA_CRYPTO)
5772
5773static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5774 mbedtls_svc_key_id_t key,
5775 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005776 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005777 const unsigned char* seed, size_t seed_length,
5778 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005779 const unsigned char* other_secret,
5780 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005781 size_t capacity )
5782{
5783 psa_status_t status;
5784
5785 status = psa_key_derivation_setup( derivation, alg );
5786 if( status != PSA_SUCCESS )
5787 return( status );
5788
5789 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5790 {
5791 status = psa_key_derivation_input_bytes( derivation,
5792 PSA_KEY_DERIVATION_INPUT_SEED,
5793 seed, seed_length );
5794 if( status != PSA_SUCCESS )
5795 return( status );
5796
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005797 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005798 {
5799 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005800 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5801 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005802 if( status != PSA_SUCCESS )
5803 return( status );
5804 }
5805
Jerry Yudc7bd172022-02-17 13:44:15 +08005806 if( mbedtls_svc_key_id_is_null( key ) )
5807 {
5808 status = psa_key_derivation_input_bytes(
5809 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005810 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005811 }
5812 else
5813 {
5814 status = psa_key_derivation_input_key(
5815 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5816 }
5817 if( status != PSA_SUCCESS )
5818 return( status );
5819
5820 status = psa_key_derivation_input_bytes( derivation,
5821 PSA_KEY_DERIVATION_INPUT_LABEL,
5822 label, label_length );
5823 if( status != PSA_SUCCESS )
5824 return( status );
5825 }
5826 else
5827 {
5828 return( PSA_ERROR_NOT_SUPPORTED );
5829 }
5830
5831 status = psa_key_derivation_set_capacity( derivation, capacity );
5832 if( status != PSA_SUCCESS )
5833 return( status );
5834
5835 return( PSA_SUCCESS );
5836}
5837
Andrzej Kurek57d10632022-10-24 10:32:01 -04005838#if defined(PSA_WANT_ALG_SHA_384) || \
5839 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005840MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005841static int tls_prf_generic( mbedtls_md_type_t md_type,
5842 const unsigned char *secret, size_t slen,
5843 const char *label,
5844 const unsigned char *random, size_t rlen,
5845 unsigned char *dstbuf, size_t dlen )
5846{
5847 psa_status_t status;
5848 psa_algorithm_t alg;
5849 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5850 psa_key_derivation_operation_t derivation =
5851 PSA_KEY_DERIVATION_OPERATION_INIT;
5852
5853 if( md_type == MBEDTLS_MD_SHA384 )
5854 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5855 else
5856 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5857
5858 /* Normally a "secret" should be long enough to be impossible to
5859 * find by brute force, and in particular should not be empty. But
5860 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5861 * and for this use case it makes sense to have a 0-length "secret".
5862 * Since the key API doesn't allow importing a key of length 0,
5863 * keep master_key=0, which setup_psa_key_derivation() understands
5864 * to mean a 0-length "secret" input. */
5865 if( slen != 0 )
5866 {
5867 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5868 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5869 psa_set_key_algorithm( &key_attributes, alg );
5870 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5871
5872 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5873 if( status != PSA_SUCCESS )
5874 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5875 }
5876
5877 status = setup_psa_key_derivation( &derivation,
5878 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005879 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005880 random, rlen,
5881 (unsigned char const *) label,
5882 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005883 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005884 dlen );
5885 if( status != PSA_SUCCESS )
5886 {
5887 psa_key_derivation_abort( &derivation );
5888 psa_destroy_key( master_key );
5889 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5890 }
5891
5892 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5893 if( status != PSA_SUCCESS )
5894 {
5895 psa_key_derivation_abort( &derivation );
5896 psa_destroy_key( master_key );
5897 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5898 }
5899
5900 status = psa_key_derivation_abort( &derivation );
5901 if( status != PSA_SUCCESS )
5902 {
5903 psa_destroy_key( master_key );
5904 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5905 }
5906
5907 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5908 status = psa_destroy_key( master_key );
5909 if( status != PSA_SUCCESS )
5910 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5911
5912 return( 0 );
5913}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005914#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005915#else /* MBEDTLS_USE_PSA_CRYPTO */
5916
Andrzej Kurek57d10632022-10-24 10:32:01 -04005917#if defined(MBEDTLS_MD_C) && \
5918 ( defined(MBEDTLS_SHA256_C) || \
5919 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005920MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005921static int tls_prf_generic( mbedtls_md_type_t md_type,
5922 const unsigned char *secret, size_t slen,
5923 const char *label,
5924 const unsigned char *random, size_t rlen,
5925 unsigned char *dstbuf, size_t dlen )
5926{
5927 size_t nb;
5928 size_t i, j, k, md_len;
5929 unsigned char *tmp;
5930 size_t tmp_len = 0;
5931 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5932 const mbedtls_md_info_t *md_info;
5933 mbedtls_md_context_t md_ctx;
5934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5935
5936 mbedtls_md_init( &md_ctx );
5937
5938 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5940
5941 md_len = mbedtls_md_get_size( md_info );
5942
5943 tmp_len = md_len + strlen( label ) + rlen;
5944 tmp = mbedtls_calloc( 1, tmp_len );
5945 if( tmp == NULL )
5946 {
5947 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5948 goto exit;
5949 }
5950
5951 nb = strlen( label );
5952 memcpy( tmp + md_len, label, nb );
5953 memcpy( tmp + md_len + nb, random, rlen );
5954 nb += rlen;
5955
5956 /*
5957 * Compute P_<hash>(secret, label + random)[0..dlen]
5958 */
5959 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5960 goto exit;
5961
5962 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5963 if( ret != 0 )
5964 goto exit;
5965 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5966 if( ret != 0 )
5967 goto exit;
5968 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5969 if( ret != 0 )
5970 goto exit;
5971
5972 for( i = 0; i < dlen; i += md_len )
5973 {
5974 ret = mbedtls_md_hmac_reset ( &md_ctx );
5975 if( ret != 0 )
5976 goto exit;
5977 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5978 if( ret != 0 )
5979 goto exit;
5980 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5981 if( ret != 0 )
5982 goto exit;
5983
5984 ret = mbedtls_md_hmac_reset ( &md_ctx );
5985 if( ret != 0 )
5986 goto exit;
5987 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5988 if( ret != 0 )
5989 goto exit;
5990 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5991 if( ret != 0 )
5992 goto exit;
5993
5994 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5995
5996 for( j = 0; j < k; j++ )
5997 dstbuf[i + j] = h_i[j];
5998 }
5999
6000exit:
6001 mbedtls_md_free( &md_ctx );
6002
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006003 if ( tmp != NULL )
6004 mbedtls_platform_zeroize( tmp, tmp_len );
6005
Jerry Yudc7bd172022-02-17 13:44:15 +08006006 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6007
6008 mbedtls_free( tmp );
6009
6010 return( ret );
6011}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006012#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006013#endif /* MBEDTLS_USE_PSA_CRYPTO */
6014
Andrzej Kurek25f27152022-08-17 16:09:31 -04006015#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006016MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006017static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6018 const char *label,
6019 const unsigned char *random, size_t rlen,
6020 unsigned char *dstbuf, size_t dlen )
6021{
6022 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6023 label, random, rlen, dstbuf, dlen ) );
6024}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006025#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006026
Andrzej Kurek25f27152022-08-17 16:09:31 -04006027#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006028MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006029static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6030 const char *label,
6031 const unsigned char *random, size_t rlen,
6032 unsigned char *dstbuf, size_t dlen )
6033{
6034 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6035 label, random, rlen, dstbuf, dlen ) );
6036}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006037#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006038
Jerry Yuf009d862022-02-17 14:01:37 +08006039/*
6040 * Set appropriate PRF function and other SSL / TLS1.2 functions
6041 *
6042 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006043 * - hash associated with the ciphersuite (only used by TLS 1.2)
6044 *
6045 * Outputs:
6046 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6047 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006048MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006049static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006050 mbedtls_md_type_t hash )
6051{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006052#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006053 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006054 {
6055 handshake->tls_prf = tls_prf_sha384;
6056 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6057 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6058 }
6059 else
6060#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006061#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006062 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006063 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006064 handshake->tls_prf = tls_prf_sha256;
6065 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6066 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6067 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006068#else
Jerry Yuf009d862022-02-17 14:01:37 +08006069 {
Jerry Yuf009d862022-02-17 14:01:37 +08006070 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006071 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6073 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006074#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006075
6076 return( 0 );
6077}
Jerry Yud6ab2352022-02-17 14:03:43 +08006078
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006079/*
6080 * Compute master secret if needed
6081 *
6082 * Parameters:
6083 * [in/out] handshake
6084 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6085 * (PSA-PSK) ciphersuite_info, psk_opaque
6086 * [out] premaster (cleared)
6087 * [out] master
6088 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6089 * debug: conf->f_dbg, conf->p_dbg
6090 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006091 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006092 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006093MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006094static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6095 unsigned char *master,
6096 const mbedtls_ssl_context *ssl )
6097{
6098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6099
6100 /* cf. RFC 5246, Section 8.1:
6101 * "The master secret is always exactly 48 bytes in length." */
6102 size_t const master_secret_len = 48;
6103
6104#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6105 unsigned char session_hash[48];
6106#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6107
6108 /* The label for the KDF used for key expansion.
6109 * This is either "master secret" or "extended master secret"
6110 * depending on whether the Extended Master Secret extension
6111 * is used. */
6112 char const *lbl = "master secret";
6113
Przemek Stekielae4ed302022-04-05 17:15:55 +02006114 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006115 * - If the Extended Master Secret extension is not used,
6116 * this is ClientHello.Random + ServerHello.Random
6117 * (see Sect. 8.1 in RFC 5246).
6118 * - If the Extended Master Secret extension is used,
6119 * this is the transcript of the handshake so far.
6120 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006121 unsigned char const *seed = handshake->randbytes;
6122 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006123
6124#if !defined(MBEDTLS_DEBUG_C) && \
6125 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6126 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6127 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6128 ssl = NULL; /* make sure we don't use it except for those cases */
6129 (void) ssl;
6130#endif
6131
6132 if( handshake->resume != 0 )
6133 {
6134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6135 return( 0 );
6136 }
6137
6138#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6139 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6140 {
6141 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006142 seed = session_hash;
6143 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006144
6145 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006146 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006147 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006148#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006149
Przemek Stekiel99114f32022-04-22 11:20:09 +02006150#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006151 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006152 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006153 {
6154 /* Perform PSK-to-MS expansion in a single step. */
6155 psa_status_t status;
6156 psa_algorithm_t alg;
6157 mbedtls_svc_key_id_t psk;
6158 psa_key_derivation_operation_t derivation =
6159 PSA_KEY_DERIVATION_OPERATION_INIT;
6160 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6161
6162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6163
6164 psk = mbedtls_ssl_get_opaque_psk( ssl );
6165
6166 if( hash_alg == MBEDTLS_MD_SHA384 )
6167 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6168 else
6169 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6170
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006171 size_t other_secret_len = 0;
6172 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006173
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006174 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006175 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006176 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006177 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006178 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006179 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006180 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006181 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006182 other_secret_len = 48;
6183 other_secret = handshake->premaster + 2;
6184 break;
6185 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006186 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006187 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6188 other_secret = handshake->premaster + 2;
6189 break;
6190 default:
6191 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006192 }
6193
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006194 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006195 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006196 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006197 (unsigned char const *) lbl,
6198 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006199 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006200 master_secret_len );
6201 if( status != PSA_SUCCESS )
6202 {
6203 psa_key_derivation_abort( &derivation );
6204 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6205 }
6206
6207 status = psa_key_derivation_output_bytes( &derivation,
6208 master,
6209 master_secret_len );
6210 if( status != PSA_SUCCESS )
6211 {
6212 psa_key_derivation_abort( &derivation );
6213 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6214 }
6215
6216 status = psa_key_derivation_abort( &derivation );
6217 if( status != PSA_SUCCESS )
6218 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6219 }
6220 else
6221#endif
6222 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006223#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6224 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6225 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6226 {
6227 psa_status_t status;
6228 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6229 psa_key_derivation_operation_t derivation =
6230 PSA_KEY_DERIVATION_OPERATION_INIT;
6231
6232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6233
6234 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6235
6236 status = psa_key_derivation_setup( &derivation, alg );
6237 if( status != PSA_SUCCESS )
6238 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6239
6240 status = psa_key_derivation_set_capacity( &derivation,
6241 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6242 if( status != PSA_SUCCESS )
6243 {
6244 psa_key_derivation_abort( &derivation );
6245 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6246 }
6247
6248 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6249 &derivation );
6250 if( status != PSA_SUCCESS )
6251 {
6252 psa_key_derivation_abort( &derivation );
6253 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6254 }
6255
6256 status = psa_key_derivation_output_bytes( &derivation,
6257 handshake->premaster,
6258 handshake->pmslen );
6259 if( status != PSA_SUCCESS )
6260 {
6261 psa_key_derivation_abort( &derivation );
6262 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6263 }
6264
6265 status = psa_key_derivation_abort( &derivation );
6266 if( status != PSA_SUCCESS )
6267 {
6268 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6269 }
6270 }
6271#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006272 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006273 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006274 master,
6275 master_secret_len );
6276 if( ret != 0 )
6277 {
6278 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6279 return( ret );
6280 }
6281
6282 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6283 handshake->premaster,
6284 handshake->pmslen );
6285
6286 mbedtls_platform_zeroize( handshake->premaster,
6287 sizeof(handshake->premaster) );
6288 }
6289
6290 return( 0 );
6291}
6292
Jerry Yud62f87e2022-02-17 14:09:02 +08006293int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6294{
6295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6296 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6297 ssl->handshake->ciphersuite_info;
6298
6299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6300
6301 /* Set PRF, calc_verify and calc_finished function pointers */
6302 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006303 ciphersuite_info->mac );
6304 if( ret != 0 )
6305 {
6306 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6307 return( ret );
6308 }
6309
6310 /* Compute master secret if needed */
6311 ret = ssl_compute_master( ssl->handshake,
6312 ssl->session_negotiate->master,
6313 ssl );
6314 if( ret != 0 )
6315 {
6316 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6317 return( ret );
6318 }
6319
6320 /* Swap the client and server random values:
6321 * - MS derivation wanted client+server (RFC 5246 8.1)
6322 * - key derivation wants server+client (RFC 5246 6.3) */
6323 {
6324 unsigned char tmp[64];
6325 memcpy( tmp, ssl->handshake->randbytes, 64 );
6326 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6327 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6328 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6329 }
6330
6331 /* Populate transform structure */
6332 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6333 ssl->session_negotiate->ciphersuite,
6334 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006335#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006336 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006337#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006338 ssl->handshake->tls_prf,
6339 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006340 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006341 ssl->conf->endpoint,
6342 ssl );
6343 if( ret != 0 )
6344 {
6345 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6346 return( ret );
6347 }
6348
6349 /* We no longer need Server/ClientHello.random values */
6350 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6351 sizeof( ssl->handshake->randbytes ) );
6352
6353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6354
6355 return( 0 );
6356}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006357
Ronald Cron4dcbca92022-03-07 10:21:40 +01006358int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6359{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006360 switch( md )
6361 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006362#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006363 case MBEDTLS_SSL_HASH_SHA384:
6364 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6365 break;
6366#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006367#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006368 case MBEDTLS_SSL_HASH_SHA256:
6369 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6370 break;
6371#endif
6372 default:
6373 return( -1 );
6374 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006375#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6376 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6377 (void) ssl;
6378#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006379 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006380}
6381
Andrzej Kurek25f27152022-08-17 16:09:31 -04006382#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006383void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6384 unsigned char *hash,
6385 size_t *hlen )
6386{
6387#if defined(MBEDTLS_USE_PSA_CRYPTO)
6388 size_t hash_size;
6389 psa_status_t status;
6390 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6391
6392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6393 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6394 if( status != PSA_SUCCESS )
6395 {
6396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6397 return;
6398 }
6399
6400 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6401 if( status != PSA_SUCCESS )
6402 {
6403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6404 return;
6405 }
6406
6407 *hlen = 32;
6408 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6410#else
6411 mbedtls_sha256_context sha256;
6412
6413 mbedtls_sha256_init( &sha256 );
6414
6415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6416
6417 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6418 mbedtls_sha256_finish( &sha256, hash );
6419
6420 *hlen = 32;
6421
6422 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6424
6425 mbedtls_sha256_free( &sha256 );
6426#endif /* MBEDTLS_USE_PSA_CRYPTO */
6427 return;
6428}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006429#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006430
Andrzej Kurek25f27152022-08-17 16:09:31 -04006431#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006432void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6433 unsigned char *hash,
6434 size_t *hlen )
6435{
6436#if defined(MBEDTLS_USE_PSA_CRYPTO)
6437 size_t hash_size;
6438 psa_status_t status;
6439 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6440
6441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6442 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6443 if( status != PSA_SUCCESS )
6444 {
6445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6446 return;
6447 }
6448
6449 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6450 if( status != PSA_SUCCESS )
6451 {
6452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6453 return;
6454 }
6455
6456 *hlen = 48;
6457 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6459#else
6460 mbedtls_sha512_context sha512;
6461
6462 mbedtls_sha512_init( &sha512 );
6463
6464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6465
Andrzej Kureka242e832022-08-11 10:03:14 -04006466 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006467 mbedtls_sha512_finish( &sha512, hash );
6468
6469 *hlen = 48;
6470
6471 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6473
6474 mbedtls_sha512_free( &sha512 );
6475#endif /* MBEDTLS_USE_PSA_CRYPTO */
6476 return;
6477}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006478#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006479
Neil Armstrong80f6f322022-05-03 17:56:38 +02006480#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6481 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006482int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6483{
6484 unsigned char *p = ssl->handshake->premaster;
6485 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6486 const unsigned char *psk = NULL;
6487 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006488 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006489
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006490 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006491 {
6492 /*
6493 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006494 * checked before calling this function.
6495 *
6496 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006497 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006498 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006499 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6500 {
6501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6502 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6503 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006504 }
6505
6506 /*
6507 * PMS = struct {
6508 * opaque other_secret<0..2^16-1>;
6509 * opaque psk<0..2^16-1>;
6510 * };
6511 * with "other_secret" depending on the particular key exchange
6512 */
6513#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6514 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6515 {
6516 if( end - p < 2 )
6517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6518
6519 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6520 p += 2;
6521
6522 if( end < p || (size_t)( end - p ) < psk_len )
6523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6524
6525 memset( p, 0, psk_len );
6526 p += psk_len;
6527 }
6528 else
6529#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6530#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6531 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6532 {
6533 /*
6534 * other_secret already set by the ClientKeyExchange message,
6535 * and is 48 bytes long
6536 */
6537 if( end - p < 2 )
6538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6539
6540 *p++ = 0;
6541 *p++ = 48;
6542 p += 48;
6543 }
6544 else
6545#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6546#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6547 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6548 {
6549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6550 size_t len;
6551
6552 /* Write length only when we know the actual value */
6553 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6554 p + 2, end - ( p + 2 ), &len,
6555 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6556 {
6557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6558 return( ret );
6559 }
6560 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6561 p += 2 + len;
6562
6563 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6564 }
6565 else
6566#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006567#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006568 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6569 {
6570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6571 size_t zlen;
6572
6573 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6574 p + 2, end - ( p + 2 ),
6575 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6576 {
6577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6578 return( ret );
6579 }
6580
6581 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6582 p += 2 + zlen;
6583
6584 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6585 MBEDTLS_DEBUG_ECDH_Z );
6586 }
6587 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006588#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006589 {
6590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6592 }
6593
6594 /* opaque psk<0..2^16-1>; */
6595 if( end - p < 2 )
6596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6597
6598 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6599 p += 2;
6600
6601 if( end < p || (size_t)( end - p ) < psk_len )
6602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6603
6604 memcpy( p, psk, psk_len );
6605 p += psk_len;
6606
6607 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6608
6609 return( 0 );
6610}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006611#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006612
6613#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006614MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006615static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6616
6617#if defined(MBEDTLS_SSL_PROTO_DTLS)
6618int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6619{
6620 /* If renegotiation is not enforced, retransmit until we would reach max
6621 * timeout if we were using the usual handshake doubling scheme */
6622 if( ssl->conf->renego_max_records < 0 )
6623 {
6624 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6625 unsigned char doublings = 1;
6626
6627 while( ratio != 0 )
6628 {
6629 ++doublings;
6630 ratio >>= 1;
6631 }
6632
6633 if( ++ssl->renego_records_seen > doublings )
6634 {
6635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6636 return( 0 );
6637 }
6638 }
6639
6640 return( ssl_write_hello_request( ssl ) );
6641}
6642#endif
6643#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006644
Jerry Yud9526692022-02-17 14:23:47 +08006645/*
6646 * Handshake functions
6647 */
6648#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6649/* No certificate support -> dummy functions */
6650int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6651{
6652 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6653 ssl->handshake->ciphersuite_info;
6654
6655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6656
6657 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6658 {
6659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6660 ssl->state++;
6661 return( 0 );
6662 }
6663
6664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6665 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6666}
6667
6668int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6669{
6670 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6671 ssl->handshake->ciphersuite_info;
6672
6673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6674
6675 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6676 {
6677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6678 ssl->state++;
6679 return( 0 );
6680 }
6681
6682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6684}
6685
6686#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6687/* Some certificate support -> implement write and parse */
6688
6689int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6690{
6691 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6692 size_t i, n;
6693 const mbedtls_x509_crt *crt;
6694 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6695 ssl->handshake->ciphersuite_info;
6696
6697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6698
6699 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6700 {
6701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6702 ssl->state++;
6703 return( 0 );
6704 }
6705
6706#if defined(MBEDTLS_SSL_CLI_C)
6707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6708 {
6709 if( ssl->handshake->client_auth == 0 )
6710 {
6711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6712 ssl->state++;
6713 return( 0 );
6714 }
6715 }
6716#endif /* MBEDTLS_SSL_CLI_C */
6717#if defined(MBEDTLS_SSL_SRV_C)
6718 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6719 {
6720 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6721 {
6722 /* Should never happen because we shouldn't have picked the
6723 * ciphersuite if we don't have a certificate. */
6724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6725 }
6726 }
6727#endif
6728
6729 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6730
6731 /*
6732 * 0 . 0 handshake type
6733 * 1 . 3 handshake length
6734 * 4 . 6 length of all certs
6735 * 7 . 9 length of cert. 1
6736 * 10 . n-1 peer certificate
6737 * n . n+2 length of cert. 2
6738 * n+3 . ... upper level cert, etc.
6739 */
6740 i = 7;
6741 crt = mbedtls_ssl_own_cert( ssl );
6742
6743 while( crt != NULL )
6744 {
6745 n = crt->raw.len;
6746 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6747 {
6748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6749 " > %" MBEDTLS_PRINTF_SIZET,
6750 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6751 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6752 }
6753
6754 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6755 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6756 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6757
6758 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6759 i += n; crt = crt->next;
6760 }
6761
6762 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6763 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6764 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6765
6766 ssl->out_msglen = i;
6767 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6768 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6769
6770 ssl->state++;
6771
6772 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6773 {
6774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6775 return( ret );
6776 }
6777
6778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6779
6780 return( ret );
6781}
6782
6783#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6784
6785#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006786MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006787static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6788 unsigned char *crt_buf,
6789 size_t crt_buf_len )
6790{
6791 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6792
6793 if( peer_crt == NULL )
6794 return( -1 );
6795
6796 if( peer_crt->raw.len != crt_buf_len )
6797 return( -1 );
6798
6799 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6800}
6801#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006802MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006803static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6804 unsigned char *crt_buf,
6805 size_t crt_buf_len )
6806{
6807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6808 unsigned char const * const peer_cert_digest =
6809 ssl->session->peer_cert_digest;
6810 mbedtls_md_type_t const peer_cert_digest_type =
6811 ssl->session->peer_cert_digest_type;
6812 mbedtls_md_info_t const * const digest_info =
6813 mbedtls_md_info_from_type( peer_cert_digest_type );
6814 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6815 size_t digest_len;
6816
6817 if( peer_cert_digest == NULL || digest_info == NULL )
6818 return( -1 );
6819
6820 digest_len = mbedtls_md_get_size( digest_info );
6821 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6822 return( -1 );
6823
6824 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6825 if( ret != 0 )
6826 return( -1 );
6827
6828 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6829}
6830#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6831#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6832
6833/*
6834 * Once the certificate message is read, parse it into a cert chain and
6835 * perform basic checks, but leave actual verification to the caller
6836 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006837MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006838static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6839 mbedtls_x509_crt *chain )
6840{
6841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6842#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6843 int crt_cnt=0;
6844#endif
6845 size_t i, n;
6846 uint8_t alert;
6847
6848 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6849 {
6850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6852 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6853 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6854 }
6855
6856 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6857 {
6858 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6859 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6860 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6861 }
6862
6863 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6864 {
6865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6866 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6867 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6868 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6869 }
6870
6871 i = mbedtls_ssl_hs_hdr_len( ssl );
6872
6873 /*
6874 * Same message structure as in mbedtls_ssl_write_certificate()
6875 */
6876 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6877
6878 if( ssl->in_msg[i] != 0 ||
6879 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6880 {
6881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6882 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6883 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6884 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6885 }
6886
6887 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6888 i += 3;
6889
6890 /* Iterate through and parse the CRTs in the provided chain. */
6891 while( i < ssl->in_hslen )
6892 {
6893 /* Check that there's room for the next CRT's length fields. */
6894 if ( i + 3 > ssl->in_hslen ) {
6895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6896 mbedtls_ssl_send_alert_message( ssl,
6897 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6898 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6899 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6900 }
6901 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6902 * anything beyond 2**16 ~ 64K. */
6903 if( ssl->in_msg[i] != 0 )
6904 {
6905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6906 mbedtls_ssl_send_alert_message( ssl,
6907 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6908 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6909 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6910 }
6911
6912 /* Read length of the next CRT in the chain. */
6913 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6914 | (unsigned int) ssl->in_msg[i + 2];
6915 i += 3;
6916
6917 if( n < 128 || i + n > ssl->in_hslen )
6918 {
6919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6920 mbedtls_ssl_send_alert_message( ssl,
6921 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6922 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6923 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6924 }
6925
6926 /* Check if we're handling the first CRT in the chain. */
6927#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6928 if( crt_cnt++ == 0 &&
6929 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6930 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6931 {
6932 /* During client-side renegotiation, check that the server's
6933 * end-CRTs hasn't changed compared to the initial handshake,
6934 * mitigating the triple handshake attack. On success, reuse
6935 * the original end-CRT instead of parsing it again. */
6936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6937 if( ssl_check_peer_crt_unchanged( ssl,
6938 &ssl->in_msg[i],
6939 n ) != 0 )
6940 {
6941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6942 mbedtls_ssl_send_alert_message( ssl,
6943 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6944 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6945 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6946 }
6947
6948 /* Now we can safely free the original chain. */
6949 ssl_clear_peer_cert( ssl->session );
6950 }
6951#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6952
6953 /* Parse the next certificate in the chain. */
6954#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6955 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6956#else
6957 /* If we don't need to store the CRT chain permanently, parse
6958 * it in-place from the input buffer instead of making a copy. */
6959 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6960#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6961 switch( ret )
6962 {
6963 case 0: /*ok*/
6964 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6965 /* Ignore certificate with an unknown algorithm: maybe a
6966 prior certificate was already trusted. */
6967 break;
6968
6969 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6970 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6971 goto crt_parse_der_failed;
6972
6973 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6974 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6975 goto crt_parse_der_failed;
6976
6977 default:
6978 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6979 crt_parse_der_failed:
6980 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6981 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6982 return( ret );
6983 }
6984
6985 i += n;
6986 }
6987
6988 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6989 return( 0 );
6990}
6991
6992#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006993MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006994static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6995{
6996 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6997 return( -1 );
6998
6999 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7000 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7001 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7002 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7003 {
Ronald Cron19385882022-06-15 16:26:13 +02007004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007005 return( 0 );
7006 }
7007 return( -1 );
7008}
7009#endif /* MBEDTLS_SSL_SRV_C */
7010
7011/* Check if a certificate message is expected.
7012 * Return either
7013 * - SSL_CERTIFICATE_EXPECTED, or
7014 * - SSL_CERTIFICATE_SKIP
7015 * indicating whether a Certificate message is expected or not.
7016 */
7017#define SSL_CERTIFICATE_EXPECTED 0
7018#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007019MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007020static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7021 int authmode )
7022{
7023 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7024 ssl->handshake->ciphersuite_info;
7025
7026 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7027 return( SSL_CERTIFICATE_SKIP );
7028
7029#if defined(MBEDTLS_SSL_SRV_C)
7030 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7031 {
7032 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7033 return( SSL_CERTIFICATE_SKIP );
7034
7035 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7036 {
7037 ssl->session_negotiate->verify_result =
7038 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7039 return( SSL_CERTIFICATE_SKIP );
7040 }
7041 }
7042#else
7043 ((void) authmode);
7044#endif /* MBEDTLS_SSL_SRV_C */
7045
7046 return( SSL_CERTIFICATE_EXPECTED );
7047}
7048
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007049MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007050static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7051 int authmode,
7052 mbedtls_x509_crt *chain,
7053 void *rs_ctx )
7054{
7055 int ret = 0;
7056 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7057 ssl->handshake->ciphersuite_info;
7058 int have_ca_chain = 0;
7059
7060 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7061 void *p_vrfy;
7062
7063 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7064 return( 0 );
7065
7066 if( ssl->f_vrfy != NULL )
7067 {
7068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7069 f_vrfy = ssl->f_vrfy;
7070 p_vrfy = ssl->p_vrfy;
7071 }
7072 else
7073 {
7074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7075 f_vrfy = ssl->conf->f_vrfy;
7076 p_vrfy = ssl->conf->p_vrfy;
7077 }
7078
7079 /*
7080 * Main check: verify certificate
7081 */
7082#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7083 if( ssl->conf->f_ca_cb != NULL )
7084 {
7085 ((void) rs_ctx);
7086 have_ca_chain = 1;
7087
7088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7089 ret = mbedtls_x509_crt_verify_with_ca_cb(
7090 chain,
7091 ssl->conf->f_ca_cb,
7092 ssl->conf->p_ca_cb,
7093 ssl->conf->cert_profile,
7094 ssl->hostname,
7095 &ssl->session_negotiate->verify_result,
7096 f_vrfy, p_vrfy );
7097 }
7098 else
7099#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7100 {
7101 mbedtls_x509_crt *ca_chain;
7102 mbedtls_x509_crl *ca_crl;
7103
7104#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7105 if( ssl->handshake->sni_ca_chain != NULL )
7106 {
7107 ca_chain = ssl->handshake->sni_ca_chain;
7108 ca_crl = ssl->handshake->sni_ca_crl;
7109 }
7110 else
7111#endif
7112 {
7113 ca_chain = ssl->conf->ca_chain;
7114 ca_crl = ssl->conf->ca_crl;
7115 }
7116
7117 if( ca_chain != NULL )
7118 have_ca_chain = 1;
7119
7120 ret = mbedtls_x509_crt_verify_restartable(
7121 chain,
7122 ca_chain, ca_crl,
7123 ssl->conf->cert_profile,
7124 ssl->hostname,
7125 &ssl->session_negotiate->verify_result,
7126 f_vrfy, p_vrfy, rs_ctx );
7127 }
7128
7129 if( ret != 0 )
7130 {
7131 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7132 }
7133
7134#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7135 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7136 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7137#endif
7138
7139 /*
7140 * Secondary checks: always done, but change 'ret' only if it was 0
7141 */
7142
7143#if defined(MBEDTLS_ECP_C)
7144 {
7145 const mbedtls_pk_context *pk = &chain->pk;
7146
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007147 /* If certificate uses an EC key, make sure the curve is OK.
7148 * This is a public key, so it can't be opaque, so can_do() is a good
7149 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007150 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007151 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007152 /* and in the unlikely case the above assumption no longer holds
7153 * we are making sure that pk_ec() here does not return a NULL
7154 */
7155 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7156 if( ec == NULL )
7157 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7160 }
Jerry Yud9526692022-02-17 14:23:47 +08007161
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007162 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7163 {
7164 ssl->session_negotiate->verify_result |=
7165 MBEDTLS_X509_BADCERT_BAD_KEY;
7166
7167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7168 if( ret == 0 )
7169 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7170 }
Jerry Yud9526692022-02-17 14:23:47 +08007171 }
7172 }
7173#endif /* MBEDTLS_ECP_C */
7174
7175 if( mbedtls_ssl_check_cert_usage( chain,
7176 ciphersuite_info,
7177 ! ssl->conf->endpoint,
7178 &ssl->session_negotiate->verify_result ) != 0 )
7179 {
7180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7181 if( ret == 0 )
7182 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7183 }
7184
7185 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7186 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7187 * with details encoded in the verification flags. All other kinds
7188 * of error codes, including those from the user provided f_vrfy
7189 * functions, are treated as fatal and lead to a failure of
7190 * ssl_parse_certificate even if verification was optional. */
7191 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7192 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7193 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7194 {
7195 ret = 0;
7196 }
7197
7198 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7199 {
7200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7201 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7202 }
7203
7204 if( ret != 0 )
7205 {
7206 uint8_t alert;
7207
7208 /* The certificate may have been rejected for several reasons.
7209 Pick one and send the corresponding alert. Which alert to send
7210 may be a subject of debate in some cases. */
7211 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7212 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7213 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7214 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7215 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7216 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7217 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7218 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7219 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7220 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7221 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7222 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7223 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7224 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7225 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7226 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7227 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7228 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7229 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7230 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7231 else
7232 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7233 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7234 alert );
7235 }
7236
7237#if defined(MBEDTLS_DEBUG_C)
7238 if( ssl->session_negotiate->verify_result != 0 )
7239 {
7240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7241 (unsigned int) ssl->session_negotiate->verify_result ) );
7242 }
7243 else
7244 {
7245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7246 }
7247#endif /* MBEDTLS_DEBUG_C */
7248
7249 return( ret );
7250}
7251
7252#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007253MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007254static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7255 unsigned char *start, size_t len )
7256{
7257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7258 /* Remember digest of the peer's end-CRT. */
7259 ssl->session_negotiate->peer_cert_digest =
7260 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7261 if( ssl->session_negotiate->peer_cert_digest == NULL )
7262 {
7263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7264 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7265 mbedtls_ssl_send_alert_message( ssl,
7266 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7267 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7268
7269 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7270 }
7271
7272 ret = mbedtls_md( mbedtls_md_info_from_type(
7273 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7274 start, len,
7275 ssl->session_negotiate->peer_cert_digest );
7276
7277 ssl->session_negotiate->peer_cert_digest_type =
7278 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7279 ssl->session_negotiate->peer_cert_digest_len =
7280 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7281
7282 return( ret );
7283}
7284
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007285MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007286static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7287 unsigned char *start, size_t len )
7288{
7289 unsigned char *end = start + len;
7290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7291
7292 /* Make a copy of the peer's raw public key. */
7293 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7294 ret = mbedtls_pk_parse_subpubkey( &start, end,
7295 &ssl->handshake->peer_pubkey );
7296 if( ret != 0 )
7297 {
7298 /* We should have parsed the public key before. */
7299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7300 }
7301
7302 return( 0 );
7303}
7304#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7305
7306int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7307{
7308 int ret = 0;
7309 int crt_expected;
7310#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7311 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7312 ? ssl->handshake->sni_authmode
7313 : ssl->conf->authmode;
7314#else
7315 const int authmode = ssl->conf->authmode;
7316#endif
7317 void *rs_ctx = NULL;
7318 mbedtls_x509_crt *chain = NULL;
7319
7320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7321
7322 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7323 if( crt_expected == SSL_CERTIFICATE_SKIP )
7324 {
7325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7326 goto exit;
7327 }
7328
7329#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7330 if( ssl->handshake->ecrs_enabled &&
7331 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7332 {
7333 chain = ssl->handshake->ecrs_peer_cert;
7334 ssl->handshake->ecrs_peer_cert = NULL;
7335 goto crt_verify;
7336 }
7337#endif
7338
7339 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7340 {
7341 /* mbedtls_ssl_read_record may have sent an alert already. We
7342 let it decide whether to alert. */
7343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7344 goto exit;
7345 }
7346
7347#if defined(MBEDTLS_SSL_SRV_C)
7348 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7349 {
7350 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7351
7352 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7353 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7354
7355 goto exit;
7356 }
7357#endif /* MBEDTLS_SSL_SRV_C */
7358
7359 /* Clear existing peer CRT structure in case we tried to
7360 * reuse a session but it failed, and allocate a new one. */
7361 ssl_clear_peer_cert( ssl->session_negotiate );
7362
7363 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7364 if( chain == NULL )
7365 {
7366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7367 sizeof( mbedtls_x509_crt ) ) );
7368 mbedtls_ssl_send_alert_message( ssl,
7369 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7370 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7371
7372 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7373 goto exit;
7374 }
7375 mbedtls_x509_crt_init( chain );
7376
7377 ret = ssl_parse_certificate_chain( ssl, chain );
7378 if( ret != 0 )
7379 goto exit;
7380
7381#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7382 if( ssl->handshake->ecrs_enabled)
7383 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7384
7385crt_verify:
7386 if( ssl->handshake->ecrs_enabled)
7387 rs_ctx = &ssl->handshake->ecrs_ctx;
7388#endif
7389
7390 ret = ssl_parse_certificate_verify( ssl, authmode,
7391 chain, rs_ctx );
7392 if( ret != 0 )
7393 goto exit;
7394
7395#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7396 {
7397 unsigned char *crt_start, *pk_start;
7398 size_t crt_len, pk_len;
7399
7400 /* We parse the CRT chain without copying, so
7401 * these pointers point into the input buffer,
7402 * and are hence still valid after freeing the
7403 * CRT chain. */
7404
7405 crt_start = chain->raw.p;
7406 crt_len = chain->raw.len;
7407
7408 pk_start = chain->pk_raw.p;
7409 pk_len = chain->pk_raw.len;
7410
7411 /* Free the CRT structures before computing
7412 * digest and copying the peer's public key. */
7413 mbedtls_x509_crt_free( chain );
7414 mbedtls_free( chain );
7415 chain = NULL;
7416
7417 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7418 if( ret != 0 )
7419 goto exit;
7420
7421 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7422 if( ret != 0 )
7423 goto exit;
7424 }
7425#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7426 /* Pass ownership to session structure. */
7427 ssl->session_negotiate->peer_cert = chain;
7428 chain = NULL;
7429#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7430
7431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7432
7433exit:
7434
7435 if( ret == 0 )
7436 ssl->state++;
7437
7438#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7439 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7440 {
7441 ssl->handshake->ecrs_peer_cert = chain;
7442 chain = NULL;
7443 }
7444#endif
7445
7446 if( chain != NULL )
7447 {
7448 mbedtls_x509_crt_free( chain );
7449 mbedtls_free( chain );
7450 }
7451
7452 return( ret );
7453}
7454#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7455
Andrzej Kurek25f27152022-08-17 16:09:31 -04007456#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007457static void ssl_calc_finished_tls_sha256(
7458 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7459{
7460 int len = 12;
7461 const char *sender;
7462 unsigned char padbuf[32];
7463#if defined(MBEDTLS_USE_PSA_CRYPTO)
7464 size_t hash_size;
7465 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7466 psa_status_t status;
7467#else
7468 mbedtls_sha256_context sha256;
7469#endif
7470
7471 mbedtls_ssl_session *session = ssl->session_negotiate;
7472 if( !session )
7473 session = ssl->session;
7474
7475 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7476 ? "client finished"
7477 : "server finished";
7478
7479#if defined(MBEDTLS_USE_PSA_CRYPTO)
7480 sha256_psa = psa_hash_operation_init();
7481
7482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7483
7484 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7485 if( status != PSA_SUCCESS )
7486 {
7487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7488 return;
7489 }
7490
7491 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7492 if( status != PSA_SUCCESS )
7493 {
7494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7495 return;
7496 }
7497 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7498#else
7499
7500 mbedtls_sha256_init( &sha256 );
7501
7502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7503
7504 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7505
7506 /*
7507 * TLSv1.2:
7508 * hash = PRF( master, finished_label,
7509 * Hash( handshake ) )[0.11]
7510 */
7511
7512#if !defined(MBEDTLS_SHA256_ALT)
7513 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7514 sha256.state, sizeof( sha256.state ) );
7515#endif
7516
7517 mbedtls_sha256_finish( &sha256, padbuf );
7518 mbedtls_sha256_free( &sha256 );
7519#endif /* MBEDTLS_USE_PSA_CRYPTO */
7520
7521 ssl->handshake->tls_prf( session->master, 48, sender,
7522 padbuf, 32, buf, len );
7523
7524 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7525
7526 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7527
7528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7529}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007530#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007531
Jerry Yub7ba49e2022-02-17 14:25:53 +08007532
Andrzej Kurek25f27152022-08-17 16:09:31 -04007533#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007534static void ssl_calc_finished_tls_sha384(
7535 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7536{
7537 int len = 12;
7538 const char *sender;
7539 unsigned char padbuf[48];
7540#if defined(MBEDTLS_USE_PSA_CRYPTO)
7541 size_t hash_size;
7542 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7543 psa_status_t status;
7544#else
7545 mbedtls_sha512_context sha512;
7546#endif
7547
7548 mbedtls_ssl_session *session = ssl->session_negotiate;
7549 if( !session )
7550 session = ssl->session;
7551
7552 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7553 ? "client finished"
7554 : "server finished";
7555
7556#if defined(MBEDTLS_USE_PSA_CRYPTO)
7557 sha384_psa = psa_hash_operation_init();
7558
7559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7560
7561 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7562 if( status != PSA_SUCCESS )
7563 {
7564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7565 return;
7566 }
7567
7568 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7569 if( status != PSA_SUCCESS )
7570 {
7571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7572 return;
7573 }
7574 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7575#else
7576 mbedtls_sha512_init( &sha512 );
7577
7578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7579
Andrzej Kureka242e832022-08-11 10:03:14 -04007580 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007581
7582 /*
7583 * TLSv1.2:
7584 * hash = PRF( master, finished_label,
7585 * Hash( handshake ) )[0.11]
7586 */
7587
7588#if !defined(MBEDTLS_SHA512_ALT)
7589 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7590 sha512.state, sizeof( sha512.state ) );
7591#endif
7592 mbedtls_sha512_finish( &sha512, padbuf );
7593
7594 mbedtls_sha512_free( &sha512 );
7595#endif
7596
7597 ssl->handshake->tls_prf( session->master, 48, sender,
7598 padbuf, 48, buf, len );
7599
7600 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7601
7602 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7603
7604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7605}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007606#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007607
Jerry Yuaef00152022-02-17 14:27:31 +08007608void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7609{
7610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7611
7612 /*
7613 * Free our handshake params
7614 */
7615 mbedtls_ssl_handshake_free( ssl );
7616 mbedtls_free( ssl->handshake );
7617 ssl->handshake = NULL;
7618
7619 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007620 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007621 */
7622 if( ssl->transform )
7623 {
7624 mbedtls_ssl_transform_free( ssl->transform );
7625 mbedtls_free( ssl->transform );
7626 }
7627 ssl->transform = ssl->transform_negotiate;
7628 ssl->transform_negotiate = NULL;
7629
7630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7631}
7632
Jerry Yu2a9fff52022-02-17 14:28:51 +08007633void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7634{
7635 int resume = ssl->handshake->resume;
7636
7637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7638
7639#if defined(MBEDTLS_SSL_RENEGOTIATION)
7640 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7641 {
7642 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7643 ssl->renego_records_seen = 0;
7644 }
7645#endif
7646
7647 /*
7648 * Free the previous session and switch in the current one
7649 */
7650 if( ssl->session )
7651 {
7652#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7653 /* RFC 7366 3.1: keep the EtM state */
7654 ssl->session_negotiate->encrypt_then_mac =
7655 ssl->session->encrypt_then_mac;
7656#endif
7657
7658 mbedtls_ssl_session_free( ssl->session );
7659 mbedtls_free( ssl->session );
7660 }
7661 ssl->session = ssl->session_negotiate;
7662 ssl->session_negotiate = NULL;
7663
7664 /*
7665 * Add cache entry
7666 */
7667 if( ssl->conf->f_set_cache != NULL &&
7668 ssl->session->id_len != 0 &&
7669 resume == 0 )
7670 {
7671 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7672 ssl->session->id,
7673 ssl->session->id_len,
7674 ssl->session ) != 0 )
7675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7676 }
7677
7678#if defined(MBEDTLS_SSL_PROTO_DTLS)
7679 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7680 ssl->handshake->flight != NULL )
7681 {
7682 /* Cancel handshake timer */
7683 mbedtls_ssl_set_timer( ssl, 0 );
7684
7685 /* Keep last flight around in case we need to resend it:
7686 * we need the handshake and transform structures for that */
7687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7688 }
7689 else
7690#endif
7691 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7692
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007693 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007694
7695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7696}
7697
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007698int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7699{
7700 int ret, hash_len;
7701
7702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7703
7704 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7705
7706 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7707
7708 /*
7709 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7710 * may define some other value. Currently (early 2016), no defined
7711 * ciphersuite does this (and this is unlikely to change as activity has
7712 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7713 */
7714 hash_len = 12;
7715
7716#if defined(MBEDTLS_SSL_RENEGOTIATION)
7717 ssl->verify_data_len = hash_len;
7718 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7719#endif
7720
7721 ssl->out_msglen = 4 + hash_len;
7722 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7723 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7724
7725 /*
7726 * In case of session resuming, invert the client and server
7727 * ChangeCipherSpec messages order.
7728 */
7729 if( ssl->handshake->resume != 0 )
7730 {
7731#if defined(MBEDTLS_SSL_CLI_C)
7732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7733 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7734#endif
7735#if defined(MBEDTLS_SSL_SRV_C)
7736 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7737 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7738#endif
7739 }
7740 else
7741 ssl->state++;
7742
7743 /*
7744 * Switch to our negotiated transform and session parameters for outbound
7745 * data.
7746 */
7747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7748
7749#if defined(MBEDTLS_SSL_PROTO_DTLS)
7750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7751 {
7752 unsigned char i;
7753
7754 /* Remember current epoch settings for resending */
7755 ssl->handshake->alt_transform_out = ssl->transform_out;
7756 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7757 sizeof( ssl->handshake->alt_out_ctr ) );
7758
7759 /* Set sequence_number to zero */
7760 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7761
7762
7763 /* Increment epoch */
7764 for( i = 2; i > 0; i-- )
7765 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7766 break;
7767
7768 /* The loop goes to its end iff the counter is wrapping */
7769 if( i == 0 )
7770 {
7771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7772 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7773 }
7774 }
7775 else
7776#endif /* MBEDTLS_SSL_PROTO_DTLS */
7777 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7778
7779 ssl->transform_out = ssl->transform_negotiate;
7780 ssl->session_out = ssl->session_negotiate;
7781
7782#if defined(MBEDTLS_SSL_PROTO_DTLS)
7783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7784 mbedtls_ssl_send_flight_completed( ssl );
7785#endif
7786
7787 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7788 {
7789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7790 return( ret );
7791 }
7792
7793#if defined(MBEDTLS_SSL_PROTO_DTLS)
7794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7795 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7796 {
7797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7798 return( ret );
7799 }
7800#endif
7801
7802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7803
7804 return( 0 );
7805}
7806
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007807#define SSL_MAX_HASH_LEN 12
7808
7809int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7810{
7811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7812 unsigned int hash_len = 12;
7813 unsigned char buf[SSL_MAX_HASH_LEN];
7814
7815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7816
7817 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7818
7819 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7820 {
7821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7822 goto exit;
7823 }
7824
7825 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7826 {
7827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7828 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7829 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7830 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7831 goto exit;
7832 }
7833
7834 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7835 {
7836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7837 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7838 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7839 goto exit;
7840 }
7841
7842 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7843 {
7844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7845 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7846 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7847 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7848 goto exit;
7849 }
7850
7851 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7852 buf, hash_len ) != 0 )
7853 {
7854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7855 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7856 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7857 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7858 goto exit;
7859 }
7860
7861#if defined(MBEDTLS_SSL_RENEGOTIATION)
7862 ssl->verify_data_len = hash_len;
7863 memcpy( ssl->peer_verify_data, buf, hash_len );
7864#endif
7865
7866 if( ssl->handshake->resume != 0 )
7867 {
7868#if defined(MBEDTLS_SSL_CLI_C)
7869 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7870 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7871#endif
7872#if defined(MBEDTLS_SSL_SRV_C)
7873 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7874 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7875#endif
7876 }
7877 else
7878 ssl->state++;
7879
7880#if defined(MBEDTLS_SSL_PROTO_DTLS)
7881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7882 mbedtls_ssl_recv_flight_completed( ssl );
7883#endif
7884
7885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7886
7887exit:
7888 mbedtls_platform_zeroize( buf, hash_len );
7889 return( ret );
7890}
7891
Jerry Yu392112c2022-02-17 14:34:10 +08007892#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7893/*
7894 * Helper to get TLS 1.2 PRF from ciphersuite
7895 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7896 */
7897static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7898{
Jerry Yu392112c2022-02-17 14:34:10 +08007899 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007900 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7901#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007902 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007903 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007904 else
Jerry Yu392112c2022-02-17 14:34:10 +08007905#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007906#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7907 {
7908 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7909 return( tls_prf_sha256 );
7910 }
7911#endif
7912#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7913 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7914 (void) ciphersuite_info;
7915#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007916
Andrzej Kurek894edde2022-09-29 06:31:14 -04007917 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007918}
7919#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007920
7921static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7922{
7923 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007924#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007925 if( tls_prf == tls_prf_sha384 )
7926 {
7927 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7928 }
7929 else
7930#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007931#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007932 if( tls_prf == tls_prf_sha256 )
7933 {
7934 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7935 }
7936 else
7937#endif
7938 return( MBEDTLS_SSL_TLS_PRF_NONE );
7939}
7940
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007941/*
7942 * Populate a transform structure with session keys and all the other
7943 * necessary information.
7944 *
7945 * Parameters:
7946 * - [in/out]: transform: structure to populate
7947 * [in] must be just initialised with mbedtls_ssl_transform_init()
7948 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7949 * - [in] ciphersuite
7950 * - [in] master
7951 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007952 * - [in] tls_prf: pointer to PRF to use for key derivation
7953 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007954 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007955 * - [in] endpoint: client or server
7956 * - [in] ssl: used for:
7957 * - ssl->conf->{f,p}_export_keys
7958 * [in] optionally used for:
7959 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7960 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007961MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007962static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7963 int ciphersuite,
7964 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007965#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007966 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007967#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007968 ssl_tls_prf_t tls_prf,
7969 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007970 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007971 unsigned endpoint,
7972 const mbedtls_ssl_context *ssl )
7973{
7974 int ret = 0;
7975 unsigned char keyblk[256];
7976 unsigned char *key1;
7977 unsigned char *key2;
7978 unsigned char *mac_enc;
7979 unsigned char *mac_dec;
7980 size_t mac_key_len = 0;
7981 size_t iv_copy_len;
7982 size_t keylen;
7983 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007984 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007985#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007986 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007987 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007988#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007989
7990#if defined(MBEDTLS_USE_PSA_CRYPTO)
7991 psa_key_type_t key_type;
7992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7993 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007994 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007995 size_t key_bits;
7996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7997#endif
7998
7999#if !defined(MBEDTLS_DEBUG_C) && \
8000 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8001 if( ssl->f_export_keys == NULL )
8002 {
8003 ssl = NULL; /* make sure we don't use it except for these cases */
8004 (void) ssl;
8005 }
8006#endif
8007
8008 /*
8009 * Some data just needs copying into the structure
8010 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008011#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008012 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008013#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008014 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008015
8016#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8017 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8018#endif
8019
8020#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008021 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008022 {
8023 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8024 * generation separate. This should never happen. */
8025 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8026 }
8027#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8028
8029 /*
8030 * Get various info structures
8031 */
8032 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8033 if( ciphersuite_info == NULL )
8034 {
8035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8036 ciphersuite ) );
8037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8038 }
8039
Neil Armstrongab555e02022-04-04 11:07:59 +02008040 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008041#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008042 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008043#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008044 ciphersuite_info );
8045
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008046 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8047 transform->taglen =
8048 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8049
8050#if defined(MBEDTLS_USE_PSA_CRYPTO)
8051 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8052 transform->taglen,
8053 &alg,
8054 &key_type,
8055 &key_bits ) ) != PSA_SUCCESS )
8056 {
8057 ret = psa_ssl_status_to_mbedtls( status );
8058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8059 goto end;
8060 }
8061#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008062 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8063 if( cipher_info == NULL )
8064 {
8065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8066 ciphersuite_info->cipher ) );
8067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8068 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008069#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008070
Neil Armstronge4512952022-03-08 09:08:22 +01008071#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008072 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008073 if( mac_alg == 0 )
8074 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008076 (unsigned) ciphersuite_info->mac ) );
8077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8078 }
8079#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008080 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8081 if( md_info == NULL )
8082 {
8083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8084 (unsigned) ciphersuite_info->mac ) );
8085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8086 }
Neil Armstronge4512952022-03-08 09:08:22 +01008087#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008088
8089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8090 /* Copy own and peer's CID if the use of the CID
8091 * extension has been negotiated. */
8092 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8093 {
8094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8095
8096 transform->in_cid_len = ssl->own_cid_len;
8097 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8098 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8099 transform->in_cid_len );
8100
8101 transform->out_cid_len = ssl->handshake->peer_cid_len;
8102 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8103 ssl->handshake->peer_cid_len );
8104 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8105 transform->out_cid_len );
8106 }
8107#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8108
8109 /*
8110 * Compute key block using the PRF
8111 */
8112 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8113 if( ret != 0 )
8114 {
8115 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8116 return( ret );
8117 }
8118
8119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8120 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8121 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8122 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8123 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8124
8125 /*
8126 * Determine the appropriate key, IV and MAC length.
8127 */
8128
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008129#if defined(MBEDTLS_USE_PSA_CRYPTO)
8130 keylen = PSA_BITS_TO_BYTES(key_bits);
8131#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008132 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008133#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008134
8135#if defined(MBEDTLS_GCM_C) || \
8136 defined(MBEDTLS_CCM_C) || \
8137 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008138 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008139 {
8140 size_t explicit_ivlen;
8141
8142 transform->maclen = 0;
8143 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008144
8145 /* All modes haves 96-bit IVs, but the length of the static parts vary
8146 * with mode and version:
8147 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8148 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8149 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8150 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8151 * sequence number).
8152 */
8153 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008154
8155 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008156#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008157 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008158#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008159 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8160 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008161#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008162
8163 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008164 transform->fixed_ivlen = 12;
8165 else
8166 transform->fixed_ivlen = 4;
8167
8168 /* Minimum length of encrypted record */
8169 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8170 transform->minlen = explicit_ivlen + transform->taglen;
8171 }
8172 else
8173#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8174#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008175 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8176 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8177 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008178 {
Neil Armstronge4512952022-03-08 09:08:22 +01008179#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008180 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008181#else
8182 size_t block_size = cipher_info->block_size;
8183#endif /* MBEDTLS_USE_PSA_CRYPTO */
8184
8185#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008186 /* Get MAC length */
8187 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8188#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008189 /* Initialize HMAC contexts */
8190 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8191 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8192 {
8193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8194 goto end;
8195 }
8196
8197 /* Get MAC length */
8198 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008199#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008200 transform->maclen = mac_key_len;
8201
8202 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008203#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008204 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008205#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008206 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008207#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008208
8209 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008210 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008211 transform->minlen = transform->maclen;
8212 else
8213 {
8214 /*
8215 * GenericBlockCipher:
8216 * 1. if EtM is in use: one block plus MAC
8217 * otherwise: * first multiple of blocklen greater than maclen
8218 * 2. IV
8219 */
8220#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008221 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008222 {
8223 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008224 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008225 }
8226 else
8227#endif
8228 {
8229 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008230 + block_size
8231 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008232 }
8233
Glenn Strauss07c64162022-03-14 12:34:51 -04008234 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008235 {
8236 transform->minlen += transform->ivlen;
8237 }
8238 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008239 {
8240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8241 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8242 goto end;
8243 }
8244 }
8245 }
8246 else
8247#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8248 {
8249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8251 }
8252
8253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8254 (unsigned) keylen,
8255 (unsigned) transform->minlen,
8256 (unsigned) transform->ivlen,
8257 (unsigned) transform->maclen ) );
8258
8259 /*
8260 * Finally setup the cipher contexts, IVs and MAC secrets.
8261 */
8262#if defined(MBEDTLS_SSL_CLI_C)
8263 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8264 {
8265 key1 = keyblk + mac_key_len * 2;
8266 key2 = keyblk + mac_key_len * 2 + keylen;
8267
8268 mac_enc = keyblk;
8269 mac_dec = keyblk + mac_key_len;
8270
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008271 iv_copy_len = ( transform->fixed_ivlen ) ?
8272 transform->fixed_ivlen : transform->ivlen;
8273 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8274 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8275 iv_copy_len );
8276 }
8277 else
8278#endif /* MBEDTLS_SSL_CLI_C */
8279#if defined(MBEDTLS_SSL_SRV_C)
8280 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8281 {
8282 key1 = keyblk + mac_key_len * 2 + keylen;
8283 key2 = keyblk + mac_key_len * 2;
8284
8285 mac_enc = keyblk + mac_key_len;
8286 mac_dec = keyblk;
8287
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008288 iv_copy_len = ( transform->fixed_ivlen ) ?
8289 transform->fixed_ivlen : transform->ivlen;
8290 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8291 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8292 iv_copy_len );
8293 }
8294 else
8295#endif /* MBEDTLS_SSL_SRV_C */
8296 {
8297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8298 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8299 goto end;
8300 }
8301
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008302 if( ssl != NULL && ssl->f_export_keys != NULL )
8303 {
8304 ssl->f_export_keys( ssl->p_export_keys,
8305 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8306 master, 48,
8307 randbytes + 32,
8308 randbytes,
8309 tls_prf_get_type( tls_prf ) );
8310 }
8311
8312#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008313 transform->psa_alg = alg;
8314
8315 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8316 {
8317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8318 psa_set_key_algorithm( &attributes, alg );
8319 psa_set_key_type( &attributes, key_type );
8320
8321 if( ( status = psa_import_key( &attributes,
8322 key1,
8323 PSA_BITS_TO_BYTES( key_bits ),
8324 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8325 {
8326 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8327 ret = psa_ssl_status_to_mbedtls( status );
8328 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8329 goto end;
8330 }
8331
8332 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8333
8334 if( ( status = psa_import_key( &attributes,
8335 key2,
8336 PSA_BITS_TO_BYTES( key_bits ),
8337 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8338 {
8339 ret = psa_ssl_status_to_mbedtls( status );
8340 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8341 goto end;
8342 }
8343 }
8344#else
8345 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8346 cipher_info ) ) != 0 )
8347 {
8348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8349 goto end;
8350 }
8351
8352 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8353 cipher_info ) ) != 0 )
8354 {
8355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8356 goto end;
8357 }
8358
8359 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8360 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8361 MBEDTLS_ENCRYPT ) ) != 0 )
8362 {
8363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8364 goto end;
8365 }
8366
8367 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8368 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8369 MBEDTLS_DECRYPT ) ) != 0 )
8370 {
8371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8372 goto end;
8373 }
8374
8375#if defined(MBEDTLS_CIPHER_MODE_CBC)
8376 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8377 {
8378 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8379 MBEDTLS_PADDING_NONE ) ) != 0 )
8380 {
8381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8382 goto end;
8383 }
8384
8385 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8386 MBEDTLS_PADDING_NONE ) ) != 0 )
8387 {
8388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8389 goto end;
8390 }
8391 }
8392#endif /* MBEDTLS_CIPHER_MODE_CBC */
8393#endif /* MBEDTLS_USE_PSA_CRYPTO */
8394
Neil Armstrong29c0c042022-03-17 17:47:28 +01008395#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8396 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8397 For AEAD-based ciphersuites, there is nothing to do here. */
8398 if( mac_key_len != 0 )
8399 {
8400#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008401 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008402
8403 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008404 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008405 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8406
8407 if( ( status = psa_import_key( &attributes,
8408 mac_enc, mac_key_len,
8409 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8410 {
8411 ret = psa_ssl_status_to_mbedtls( status );
8412 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8413 goto end;
8414 }
8415
Ronald Cronfb39f152022-03-25 14:36:28 +01008416 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008417 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8418#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8419 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8420#endif
8421 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008422 /* mbedtls_ct_hmac() requires the key to be exportable */
8423 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8424 PSA_KEY_USAGE_VERIFY_HASH );
8425 else
8426 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8427
8428 if( ( status = psa_import_key( &attributes,
8429 mac_dec, mac_key_len,
8430 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8431 {
8432 ret = psa_ssl_status_to_mbedtls( status );
8433 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8434 goto end;
8435 }
8436#else
8437 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8438 if( ret != 0 )
8439 goto end;
8440 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8441 if( ret != 0 )
8442 goto end;
8443#endif /* MBEDTLS_USE_PSA_CRYPTO */
8444 }
8445#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8446
8447 ((void) mac_dec);
8448 ((void) mac_enc);
8449
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008450end:
8451 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8452 return( ret );
8453}
8454
Valerio Settia08b1a42022-11-17 15:10:02 +01008455#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8456 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008457int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008458 psa_pake_operation_t *pake_ctx,
8459 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008460 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008461{
8462 psa_status_t status;
8463 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008464 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008465 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8466 * At round two perform a single cycle
8467 */
8468 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008469
Valerio Setti6b3dab02022-11-17 17:14:54 +01008470 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008471 {
8472 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8473 step <= PSA_PAKE_STEP_ZK_PROOF;
8474 ++step )
8475 {
8476 /* Length is stored at the first byte */
8477 size_t length = buf[input_offset];
8478 input_offset += 1;
8479
8480 if( input_offset + length > len )
8481 {
8482 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8483 }
8484
8485 status = psa_pake_input( pake_ctx, step,
8486 buf + input_offset, length );
8487 if( status != PSA_SUCCESS)
8488 {
8489 return psa_ssl_status_to_mbedtls( status );
8490 }
8491
8492 input_offset += length;
8493 }
8494 }
8495
Valerio Setti819de862022-11-17 18:05:19 +01008496 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008497 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008498
Valerio Settia08b1a42022-11-17 15:10:02 +01008499 return( 0 );
8500}
8501
Valerio Setti6b3dab02022-11-17 17:14:54 +01008502int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008503 psa_pake_operation_t *pake_ctx,
8504 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008505 size_t len, size_t *olen,
8506 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008507{
8508 psa_status_t status;
8509 size_t output_offset = 0;
8510 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008511 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008512 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8513 * At round two perform a single cycle
8514 */
8515 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008516
Valerio Setti6b3dab02022-11-17 17:14:54 +01008517 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008518 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008519 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8520 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008521 ++step )
8522 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008523 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008524 * For each step, prepend 1 byte with the length of the data as
8525 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008526 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008527 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008528 buf + output_offset + 1,
8529 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008530 &output_len );
8531 if( status != PSA_SUCCESS )
8532 {
8533 return( psa_ssl_status_to_mbedtls( status ) );
8534 }
8535
Valerio Setti99d88c12022-11-22 16:03:43 +01008536 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008537
8538 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008539 }
8540 }
8541
8542 *olen = output_offset;
8543
8544 return( 0 );
8545}
Valerio Settia08b1a42022-11-17 15:10:02 +01008546#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8547
Jerry Yuee40f9d2022-02-17 14:55:16 +08008548#if defined(MBEDTLS_USE_PSA_CRYPTO)
8549int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8550 unsigned char *hash, size_t *hashlen,
8551 unsigned char *data, size_t data_len,
8552 mbedtls_md_type_t md_alg )
8553{
8554 psa_status_t status;
8555 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008556 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008557
8558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8559
8560 if( ( status = psa_hash_setup( &hash_operation,
8561 hash_alg ) ) != PSA_SUCCESS )
8562 {
8563 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8564 goto exit;
8565 }
8566
8567 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8568 64 ) ) != PSA_SUCCESS )
8569 {
8570 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8571 goto exit;
8572 }
8573
8574 if( ( status = psa_hash_update( &hash_operation,
8575 data, data_len ) ) != PSA_SUCCESS )
8576 {
8577 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8578 goto exit;
8579 }
8580
8581 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8582 hashlen ) ) != PSA_SUCCESS )
8583 {
8584 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8585 goto exit;
8586 }
8587
8588exit:
8589 if( status != PSA_SUCCESS )
8590 {
8591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8592 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8593 switch( status )
8594 {
8595 case PSA_ERROR_NOT_SUPPORTED:
8596 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8597 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8598 case PSA_ERROR_BUFFER_TOO_SMALL:
8599 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8600 case PSA_ERROR_INSUFFICIENT_MEMORY:
8601 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8602 default:
8603 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8604 }
8605 }
8606 return( 0 );
8607}
8608
8609#else
8610
8611int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8612 unsigned char *hash, size_t *hashlen,
8613 unsigned char *data, size_t data_len,
8614 mbedtls_md_type_t md_alg )
8615{
8616 int ret = 0;
8617 mbedtls_md_context_t ctx;
8618 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8619 *hashlen = mbedtls_md_get_size( md_info );
8620
8621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8622
8623 mbedtls_md_init( &ctx );
8624
8625 /*
8626 * digitally-signed struct {
8627 * opaque client_random[32];
8628 * opaque server_random[32];
8629 * ServerDHParams params;
8630 * };
8631 */
8632 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8633 {
8634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8635 goto exit;
8636 }
8637 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8638 {
8639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8640 goto exit;
8641 }
8642 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8643 {
8644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8645 goto exit;
8646 }
8647 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8648 {
8649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8650 goto exit;
8651 }
8652 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8653 {
8654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8655 goto exit;
8656 }
8657
8658exit:
8659 mbedtls_md_free( &ctx );
8660
8661 if( ret != 0 )
8662 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8663 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8664
8665 return( ret );
8666}
8667#endif /* MBEDTLS_USE_PSA_CRYPTO */
8668
Jerry Yud9d91da2022-02-17 14:57:06 +08008669#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8670
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008671/* Find the preferred hash for a given signature algorithm. */
8672unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8673 mbedtls_ssl_context *ssl,
8674 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008675{
Gabor Mezei078e8032022-04-27 21:17:56 +02008676 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008677 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008678
8679 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008680 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008681
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008682 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008683 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008684 unsigned int hash_alg_received =
8685 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8686 received_sig_algs[i] );
8687 unsigned int sig_alg_received =
8688 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8689 received_sig_algs[i] );
8690
8691 if( sig_alg == sig_alg_received )
8692 {
8693#if defined(MBEDTLS_USE_PSA_CRYPTO)
8694 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8695 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008696 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008697 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008698
Neil Armstrong96eceb82022-06-30 18:05:05 +02008699 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8700 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8701 PSA_ALG_ECDSA( psa_hash_alg ),
8702 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008703 continue;
8704
Neil Armstrong96eceb82022-06-30 18:05:05 +02008705 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008706 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8707 PSA_ALG_RSA_PKCS1V15_SIGN(
8708 psa_hash_alg ),
8709 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008710 continue;
8711 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008712#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008713
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008714 return( hash_alg_received );
8715 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008716 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008717
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008718 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008719}
8720
8721#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8722
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008723/* Serialization of TLS 1.2 sessions:
8724 *
8725 * struct {
8726 * uint64 start_time;
8727 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008728 * uint8 session_id_len; // at most 32
8729 * opaque session_id[32];
8730 * opaque master[48]; // fixed length in the standard
8731 * uint32 verify_result;
8732 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8733 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8734 * uint32 ticket_lifetime;
8735 * uint8 mfl_code; // up to 255 according to standard
8736 * uint8 encrypt_then_mac; // 0 or 1
8737 * } serialized_session_tls12;
8738 *
8739 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008740static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008741 unsigned char *buf,
8742 size_t buf_len )
8743{
8744 unsigned char *p = buf;
8745 size_t used = 0;
8746
8747#if defined(MBEDTLS_HAVE_TIME)
8748 uint64_t start;
8749#endif
8750#if defined(MBEDTLS_X509_CRT_PARSE_C)
8751#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8752 size_t cert_len;
8753#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8754#endif /* MBEDTLS_X509_CRT_PARSE_C */
8755
8756 /*
8757 * Time
8758 */
8759#if defined(MBEDTLS_HAVE_TIME)
8760 used += 8;
8761
8762 if( used <= buf_len )
8763 {
8764 start = (uint64_t) session->start;
8765
8766 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8767 p += 8;
8768 }
8769#endif /* MBEDTLS_HAVE_TIME */
8770
8771 /*
8772 * Basic mandatory fields
8773 */
8774 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008775 + 1 /* id_len */
8776 + sizeof( session->id )
8777 + sizeof( session->master )
8778 + 4; /* verify_result */
8779
8780 if( used <= buf_len )
8781 {
8782 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8783 p += 2;
8784
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008785 *p++ = MBEDTLS_BYTE_0( session->id_len );
8786 memcpy( p, session->id, 32 );
8787 p += 32;
8788
8789 memcpy( p, session->master, 48 );
8790 p += 48;
8791
8792 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8793 p += 4;
8794 }
8795
8796 /*
8797 * Peer's end-entity certificate
8798 */
8799#if defined(MBEDTLS_X509_CRT_PARSE_C)
8800#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8801 if( session->peer_cert == NULL )
8802 cert_len = 0;
8803 else
8804 cert_len = session->peer_cert->raw.len;
8805
8806 used += 3 + cert_len;
8807
8808 if( used <= buf_len )
8809 {
8810 *p++ = MBEDTLS_BYTE_2( cert_len );
8811 *p++ = MBEDTLS_BYTE_1( cert_len );
8812 *p++ = MBEDTLS_BYTE_0( cert_len );
8813
8814 if( session->peer_cert != NULL )
8815 {
8816 memcpy( p, session->peer_cert->raw.p, cert_len );
8817 p += cert_len;
8818 }
8819 }
8820#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8821 if( session->peer_cert_digest != NULL )
8822 {
8823 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8824 if( used <= buf_len )
8825 {
8826 *p++ = (unsigned char) session->peer_cert_digest_type;
8827 *p++ = (unsigned char) session->peer_cert_digest_len;
8828 memcpy( p, session->peer_cert_digest,
8829 session->peer_cert_digest_len );
8830 p += session->peer_cert_digest_len;
8831 }
8832 }
8833 else
8834 {
8835 used += 2;
8836 if( used <= buf_len )
8837 {
8838 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8839 *p++ = 0;
8840 }
8841 }
8842#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8843#endif /* MBEDTLS_X509_CRT_PARSE_C */
8844
8845 /*
8846 * Session ticket if any, plus associated data
8847 */
8848#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8849 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8850
8851 if( used <= buf_len )
8852 {
8853 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8854 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8855 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8856
8857 if( session->ticket != NULL )
8858 {
8859 memcpy( p, session->ticket, session->ticket_len );
8860 p += session->ticket_len;
8861 }
8862
8863 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8864 p += 4;
8865 }
8866#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8867
8868 /*
8869 * Misc extension-related info
8870 */
8871#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8872 used += 1;
8873
8874 if( used <= buf_len )
8875 *p++ = session->mfl_code;
8876#endif
8877
8878#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8879 used += 1;
8880
8881 if( used <= buf_len )
8882 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8883#endif
8884
8885 return( used );
8886}
8887
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008888MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008889static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008890 const unsigned char *buf,
8891 size_t len )
8892{
8893#if defined(MBEDTLS_HAVE_TIME)
8894 uint64_t start;
8895#endif
8896#if defined(MBEDTLS_X509_CRT_PARSE_C)
8897#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8898 size_t cert_len;
8899#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8900#endif /* MBEDTLS_X509_CRT_PARSE_C */
8901
8902 const unsigned char *p = buf;
8903 const unsigned char * const end = buf + len;
8904
8905 /*
8906 * Time
8907 */
8908#if defined(MBEDTLS_HAVE_TIME)
8909 if( 8 > (size_t)( end - p ) )
8910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8911
8912 start = ( (uint64_t) p[0] << 56 ) |
8913 ( (uint64_t) p[1] << 48 ) |
8914 ( (uint64_t) p[2] << 40 ) |
8915 ( (uint64_t) p[3] << 32 ) |
8916 ( (uint64_t) p[4] << 24 ) |
8917 ( (uint64_t) p[5] << 16 ) |
8918 ( (uint64_t) p[6] << 8 ) |
8919 ( (uint64_t) p[7] );
8920 p += 8;
8921
8922 session->start = (time_t) start;
8923#endif /* MBEDTLS_HAVE_TIME */
8924
8925 /*
8926 * Basic mandatory fields
8927 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008928 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008929 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8930
8931 session->ciphersuite = ( p[0] << 8 ) | p[1];
8932 p += 2;
8933
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008934 session->id_len = *p++;
8935 memcpy( session->id, p, 32 );
8936 p += 32;
8937
8938 memcpy( session->master, p, 48 );
8939 p += 48;
8940
8941 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8942 ( (uint32_t) p[1] << 16 ) |
8943 ( (uint32_t) p[2] << 8 ) |
8944 ( (uint32_t) p[3] );
8945 p += 4;
8946
8947 /* Immediately clear invalid pointer values that have been read, in case
8948 * we exit early before we replaced them with valid ones. */
8949#if defined(MBEDTLS_X509_CRT_PARSE_C)
8950#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8951 session->peer_cert = NULL;
8952#else
8953 session->peer_cert_digest = NULL;
8954#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8955#endif /* MBEDTLS_X509_CRT_PARSE_C */
8956#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8957 session->ticket = NULL;
8958#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8959
8960 /*
8961 * Peer certificate
8962 */
8963#if defined(MBEDTLS_X509_CRT_PARSE_C)
8964#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8965 /* Deserialize CRT from the end of the ticket. */
8966 if( 3 > (size_t)( end - p ) )
8967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8968
8969 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8970 p += 3;
8971
8972 if( cert_len != 0 )
8973 {
8974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8975
8976 if( cert_len > (size_t)( end - p ) )
8977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8978
8979 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8980
8981 if( session->peer_cert == NULL )
8982 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8983
8984 mbedtls_x509_crt_init( session->peer_cert );
8985
8986 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8987 p, cert_len ) ) != 0 )
8988 {
8989 mbedtls_x509_crt_free( session->peer_cert );
8990 mbedtls_free( session->peer_cert );
8991 session->peer_cert = NULL;
8992 return( ret );
8993 }
8994
8995 p += cert_len;
8996 }
8997#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8998 /* Deserialize CRT digest from the end of the ticket. */
8999 if( 2 > (size_t)( end - p ) )
9000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9001
9002 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9003 session->peer_cert_digest_len = (size_t) *p++;
9004
9005 if( session->peer_cert_digest_len != 0 )
9006 {
9007 const mbedtls_md_info_t *md_info =
9008 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9009 if( md_info == NULL )
9010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9011 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9013
9014 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9016
9017 session->peer_cert_digest =
9018 mbedtls_calloc( 1, session->peer_cert_digest_len );
9019 if( session->peer_cert_digest == NULL )
9020 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9021
9022 memcpy( session->peer_cert_digest, p,
9023 session->peer_cert_digest_len );
9024 p += session->peer_cert_digest_len;
9025 }
9026#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9027#endif /* MBEDTLS_X509_CRT_PARSE_C */
9028
9029 /*
9030 * Session ticket and associated data
9031 */
9032#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9033 if( 3 > (size_t)( end - p ) )
9034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9035
9036 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9037 p += 3;
9038
9039 if( session->ticket_len != 0 )
9040 {
9041 if( session->ticket_len > (size_t)( end - p ) )
9042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9043
9044 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9045 if( session->ticket == NULL )
9046 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9047
9048 memcpy( session->ticket, p, session->ticket_len );
9049 p += session->ticket_len;
9050 }
9051
9052 if( 4 > (size_t)( end - p ) )
9053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9054
9055 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9056 ( (uint32_t) p[1] << 16 ) |
9057 ( (uint32_t) p[2] << 8 ) |
9058 ( (uint32_t) p[3] );
9059 p += 4;
9060#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9061
9062 /*
9063 * Misc extension-related info
9064 */
9065#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9066 if( 1 > (size_t)( end - p ) )
9067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9068
9069 session->mfl_code = *p++;
9070#endif
9071
9072#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9073 if( 1 > (size_t)( end - p ) )
9074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9075
9076 session->encrypt_then_mac = *p++;
9077#endif
9078
9079 /* Done, should have consumed entire buffer */
9080 if( p != end )
9081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9082
9083 return( 0 );
9084}
Jerry Yudc7bd172022-02-17 13:44:15 +08009085#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9086
XiaokangQian75d40ef2022-04-20 11:05:24 +00009087int mbedtls_ssl_validate_ciphersuite(
9088 const mbedtls_ssl_context *ssl,
9089 const mbedtls_ssl_ciphersuite_t *suite_info,
9090 mbedtls_ssl_protocol_version min_tls_version,
9091 mbedtls_ssl_protocol_version max_tls_version )
9092{
9093 (void) ssl;
9094
9095 if( suite_info == NULL )
9096 return( -1 );
9097
9098 if( ( suite_info->min_tls_version > max_tls_version ) ||
9099 ( suite_info->max_tls_version < min_tls_version ) )
9100 {
9101 return( -1 );
9102 }
9103
XiaokangQian060d8672022-04-21 09:24:56 +00009104#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009105#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009106#if defined(MBEDTLS_USE_PSA_CRYPTO)
9107 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9108 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9109#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009110 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9111 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009112#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009113 {
9114 return( -1 );
9115 }
9116#endif
9117
9118 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9119#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9120 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9121 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9122 {
9123 return( -1 );
9124 }
9125#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9126#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9127
9128 return( 0 );
9129}
9130
Ronald Crone68ab4f2022-10-05 12:46:29 +02009131#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009132/*
9133 * Function for writing a signature algorithm extension.
9134 *
9135 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9136 * value (TLS 1.3 RFC8446):
9137 * enum {
9138 * ....
9139 * ecdsa_secp256r1_sha256( 0x0403 ),
9140 * ecdsa_secp384r1_sha384( 0x0503 ),
9141 * ecdsa_secp521r1_sha512( 0x0603 ),
9142 * ....
9143 * } SignatureScheme;
9144 *
9145 * struct {
9146 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9147 * } SignatureSchemeList;
9148 *
9149 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9150 * value (TLS 1.2 RFC5246):
9151 * enum {
9152 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9153 * sha512(6), (255)
9154 * } HashAlgorithm;
9155 *
9156 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9157 * SignatureAlgorithm;
9158 *
9159 * struct {
9160 * HashAlgorithm hash;
9161 * SignatureAlgorithm signature;
9162 * } SignatureAndHashAlgorithm;
9163 *
9164 * SignatureAndHashAlgorithm
9165 * supported_signature_algorithms<2..2^16-2>;
9166 *
9167 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9168 * generalization of the TLS 1.2 signature algorithm extension.
9169 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9170 * `SignatureScheme` field of TLS 1.3
9171 *
9172 */
9173int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9174 const unsigned char *end, size_t *out_len )
9175{
9176 unsigned char *p = buf;
9177 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9178 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9179
9180 *out_len = 0;
9181
9182 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9183
9184 /* Check if we have space for header and length field:
9185 * - extension_type (2 bytes)
9186 * - extension_data_length (2 bytes)
9187 * - supported_signature_algorithms_length (2 bytes)
9188 */
9189 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9190 p += 6;
9191
9192 /*
9193 * Write supported_signature_algorithms
9194 */
9195 supported_sig_alg = p;
9196 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9197 if( sig_alg == NULL )
9198 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9199
9200 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9201 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9203 *sig_alg,
9204 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009205 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9206 continue;
9207 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9208 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9209 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009211 *sig_alg,
9212 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009213 }
9214
9215 /* Length of supported_signature_algorithms */
9216 supported_sig_alg_len = p - supported_sig_alg;
9217 if( supported_sig_alg_len == 0 )
9218 {
9219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9221 }
9222
XiaokangQianeaf36512022-04-24 09:07:44 +00009223 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009224 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009225 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9226
XiaokangQianeaf36512022-04-24 09:07:44 +00009227 *out_len = p - buf;
9228
9229#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009230 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009231#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009232
XiaokangQianeaf36512022-04-24 09:07:44 +00009233 return( 0 );
9234}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009235#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009236
XiaokangQian40a35232022-05-07 09:02:40 +00009237#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009238/*
9239 * mbedtls_ssl_parse_server_name_ext
9240 *
9241 * Structure of server_name extension:
9242 *
9243 * enum {
9244 * host_name(0), (255)
9245 * } NameType;
9246 * opaque HostName<1..2^16-1>;
9247 *
9248 * struct {
9249 * NameType name_type;
9250 * select (name_type) {
9251 * case host_name: HostName;
9252 * } name;
9253 * } ServerName;
9254 * struct {
9255 * ServerName server_name_list<1..2^16-1>
9256 * } ServerNameList;
9257 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009258MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009259int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9260 const unsigned char *buf,
9261 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009262{
9263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9264 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009265 size_t server_name_list_len, hostname_len;
9266 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009267
XiaokangQianf2a94202022-05-20 06:44:24 +00009268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009269
9270 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009271 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009272 p += 2;
9273
XiaokangQian9b2b7712022-05-17 02:57:00 +00009274 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9275 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009276 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009277 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009278 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009279 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009280 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9281 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009282
9283 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9284 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009285 /* sni_name is intended to be used only during the parsing of the
9286 * ClientHello message (it is reset to NULL before the end of
9287 * the message parsing). Thus it is ok to just point to the
9288 * reception buffer and not make a copy of it.
9289 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009290 ssl->handshake->sni_name = p + 3;
9291 ssl->handshake->sni_name_len = hostname_len;
9292 if( ssl->conf->f_sni == NULL )
9293 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009294 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009295 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009296 if( ret != 0 )
9297 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009298 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009299 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9300 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009301 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9302 }
9303 return( 0 );
9304 }
9305
9306 p += hostname_len + 3;
9307 }
9308
9309 return( 0 );
9310}
9311#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9312
XiaokangQianacb39922022-06-17 10:18:48 +00009313#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009314MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009315int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9316 const unsigned char *buf,
9317 const unsigned char *end )
9318{
9319 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009320 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009321 const unsigned char *protocol_name_list;
9322 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009323 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009324
9325 /* If ALPN not configured, just ignore the extension */
9326 if( ssl->conf->alpn_list == NULL )
9327 return( 0 );
9328
9329 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009330 * RFC7301, section 3.1
9331 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009332 *
XiaokangQianc7403452022-06-23 03:24:12 +00009333 * struct {
9334 * ProtocolName protocol_name_list<2..2^16-1>
9335 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009336 */
9337
XiaokangQianc7403452022-06-23 03:24:12 +00009338 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009339 * protocol_name_list_len 2 bytes
9340 * protocol_name_len 1 bytes
9341 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009342 */
XiaokangQianacb39922022-06-17 10:18:48 +00009343 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9344
XiaokangQianc7403452022-06-23 03:24:12 +00009345 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009346 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009347 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009348 protocol_name_list = p;
9349 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009350
9351 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009352 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009353 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009354 protocol_name_len = *p++;
9355 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9356 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009357 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009358 {
9359 MBEDTLS_SSL_PEND_FATAL_ALERT(
9360 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9361 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009362 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009363 }
9364
9365 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009366 }
9367
9368 /* Use our order of preference */
9369 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9370 {
9371 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009372 p = protocol_name_list;
9373 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009374 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009375 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009376 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009377 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009378 {
9379 ssl->alpn_chosen = *alpn;
9380 return( 0 );
9381 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009382
9383 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009384 }
9385 }
9386
XiaokangQian95d5f542022-06-24 02:29:26 +00009387 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009388 MBEDTLS_SSL_PEND_FATAL_ALERT(
9389 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9390 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9391 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9392}
9393
9394int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9395 unsigned char *buf,
9396 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009397 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009398{
9399 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009400 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009401 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009402
9403 if( ssl->alpn_chosen == NULL )
9404 {
9405 return( 0 );
9406 }
9407
XiaokangQian95d5f542022-06-24 02:29:26 +00009408 protocol_name_len = strlen( ssl->alpn_chosen );
9409 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009410
9411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9412 /*
9413 * 0 . 1 ext identifier
9414 * 2 . 3 ext length
9415 * 4 . 5 protocol list length
9416 * 6 . 6 protocol name length
9417 * 7 . 7+n protocol name
9418 */
9419 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9420
XiaokangQian95d5f542022-06-24 02:29:26 +00009421 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009422
XiaokangQian95d5f542022-06-24 02:29:26 +00009423 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9424 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009425 /* Note: the length of the chosen protocol has been checked to be less
9426 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9427 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009428 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009429
XiaokangQian95d5f542022-06-24 02:29:26 +00009430 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009431
9432#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9433 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9434#endif
9435
XiaokangQianacb39922022-06-17 10:18:48 +00009436 return ( 0 );
9437}
9438#endif /* MBEDTLS_SSL_ALPN */
9439
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009440#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009441 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009442 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009443 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009444int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9445 const char *hostname )
9446{
9447 /* Initialize to suppress unnecessary compiler warning */
9448 size_t hostname_len = 0;
9449
9450 /* Check if new hostname is valid before
9451 * making any change to current one */
9452 if( hostname != NULL )
9453 {
9454 hostname_len = strlen( hostname );
9455
9456 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9458 }
9459
9460 /* Now it's clear that we will overwrite the old hostname,
9461 * so we can free it safely */
9462 if( session->hostname != NULL )
9463 {
9464 mbedtls_platform_zeroize( session->hostname,
9465 strlen( session->hostname ) );
9466 mbedtls_free( session->hostname );
9467 }
9468
9469 /* Passing NULL as hostname shall clear the old one */
9470 if( hostname == NULL )
9471 {
9472 session->hostname = NULL;
9473 }
9474 else
9475 {
9476 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9477 if( session->hostname == NULL )
9478 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9479
9480 memcpy( session->hostname, hostname, hostname_len );
9481 }
9482
9483 return( 0 );
9484}
Xiaokang Qian03409292022-10-12 02:49:52 +00009485#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9486 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009487 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009488 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#endif /* MBEDTLS_SSL_TLS_C */