blob: 7723363dfe5024d4a3c4d7a4c6510393bc91a5d5 [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 */
Jerry Yu2e199812022-12-01 18:57:19 +0800968#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +0000969 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Jerry Yu2e199812022-12-01 18:57:19 +0800971#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200975 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200976
Jerry Yu2e199812022-12-01 18:57:19 +0800977#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200978 /*
979 * Either the pointers are now NULL or cleared properly and can be freed.
980 * Now allocate missing structures.
981 */
982 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200983 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200984 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 }
Jerry Yu2e199812022-12-01 18:57:19 +0800986#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +0000987
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200989 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200990 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200991 }
Paul Bakker48916f92012-09-16 19:57:18 +0000992
Paul Bakker82788fb2014-10-20 13:59:19 +0200993 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200994 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200995 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200996 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500997#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
998 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400999
Andrzej Kurek4a063792020-10-21 15:08:44 +02001000 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1001 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001002#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001003
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001004 /* All pointers should exist and can be directly freed without issue */
Jerry Yu2e199812022-12-01 18:57:19 +08001005 if( ssl->handshake == NULL ||
1006#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001007 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001008#endif
1009 ssl->session_negotiate == NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00001010 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001014 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001015
1016#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1017 mbedtls_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001019#endif
1020
1021 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001022 ssl->session_negotiate = NULL;
1023
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001024 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001025 }
1026
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001027 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001029 ssl_handshake_params_init( ssl->handshake );
1030
Jerry Yu2e199812022-12-01 18:57:19 +08001031#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1032 mbedtls_ssl_transform_init( ssl->transform_negotiate );
1033#endif
1034
Jerry Yud0766ec2022-09-22 10:46:57 +08001035#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1036 defined(MBEDTLS_SSL_SRV_C) && \
1037 defined(MBEDTLS_SSL_SESSION_TICKETS)
1038 ssl->handshake->new_session_tickets_count =
1039 ssl->conf->new_session_tickets_count ;
1040#endif
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001043 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1044 {
1045 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001046
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001047 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1048 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1049 else
1050 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001051
Hanno Becker0f57a652020-02-05 10:37:26 +00001052 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001053 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001054#endif
1055
Brett Warrene0edc842021-08-17 09:53:13 +01001056/*
1057 * curve_list is translated to IANA TLS group identifiers here because
1058 * mbedtls_ssl_conf_curves returns void and so can't return
1059 * any error codes.
1060 */
1061#if defined(MBEDTLS_ECP_C)
1062#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1063 /* Heap allocate and translate curve_list from internal to IANA group ids */
1064 if ( ssl->conf->curve_list != NULL )
1065 {
1066 size_t length;
1067 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1068
1069 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1070 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1071
1072 /* Leave room for zero termination */
1073 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1074 if ( group_list == NULL )
1075 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1076
1077 for( size_t i = 0; i < length; i++ )
1078 {
1079 const mbedtls_ecp_curve_info *info =
1080 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1081 if ( info == NULL )
1082 {
1083 mbedtls_free( group_list );
1084 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1085 }
1086 group_list[i] = info->tls_id;
1087 }
1088
1089 group_list[length] = 0;
1090
1091 ssl->handshake->group_list = group_list;
1092 ssl->handshake->group_list_heap_allocated = 1;
1093 }
1094 else
1095 {
1096 ssl->handshake->group_list = ssl->conf->group_list;
1097 ssl->handshake->group_list_heap_allocated = 0;
1098 }
1099#endif /* MBEDTLS_DEPRECATED_REMOVED */
1100#endif /* MBEDTLS_ECP_C */
1101
Ronald Crone68ab4f2022-10-05 12:46:29 +02001102#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001103#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001105 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1106 signature algorithms IANA identifiers. */
1107 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001108 ssl->conf->sig_hashes != NULL )
1109 {
1110 const int *md;
1111 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001112 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001113 uint16_t *p;
1114
Jerry Yub476a442022-01-21 18:14:45 +08001115#if defined(static_assert)
1116 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1117 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1118 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001119#endif
1120
Jerry Yuf017ee42022-01-12 15:49:48 +08001121 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1122 {
1123 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1124 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001125#if defined(MBEDTLS_ECDSA_C)
1126 sig_algs_len += sizeof( uint16_t );
1127#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001128
Jerry Yub476a442022-01-21 18:14:45 +08001129#if defined(MBEDTLS_RSA_C)
1130 sig_algs_len += sizeof( uint16_t );
1131#endif
1132 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1133 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001134 }
1135
Jerry Yub476a442022-01-21 18:14:45 +08001136 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001137 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1138
Jerry Yub476a442022-01-21 18:14:45 +08001139 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1140 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001141 if( ssl->handshake->sig_algs == NULL )
1142 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1143
1144 p = (uint16_t *)ssl->handshake->sig_algs;
1145 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1146 {
1147 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1148 if( hash == MBEDTLS_SSL_HASH_NONE )
1149 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001150#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001151 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1152 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001153#endif
1154#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001155 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1156 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001157#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001158 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001159 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001160 ssl->handshake->sig_algs_heap_allocated = 1;
1161 }
1162 else
Jerry Yua69269a2022-01-17 21:06:01 +08001163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001164 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001165 ssl->handshake->sig_algs_heap_allocated = 0;
1166 }
Jerry Yucc539102022-06-27 16:27:35 +08001167#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001168#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001169 return( 0 );
1170}
1171
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001172#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001173/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001174MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001175static int ssl_cookie_write_dummy( void *ctx,
1176 unsigned char **p, unsigned char *end,
1177 const unsigned char *cli_id, size_t cli_id_len )
1178{
1179 ((void) ctx);
1180 ((void) p);
1181 ((void) end);
1182 ((void) cli_id);
1183 ((void) cli_id_len);
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001186}
1187
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001188MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001189static int ssl_cookie_check_dummy( void *ctx,
1190 const unsigned char *cookie, size_t cookie_len,
1191 const unsigned char *cli_id, size_t cli_id_len )
1192{
1193 ((void) ctx);
1194 ((void) cookie);
1195 ((void) cookie_len);
1196 ((void) cli_id);
1197 ((void) cli_id_len);
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001200}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001201#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203/*
1204 * Initialize an SSL context
1205 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001206void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1207{
1208 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1209}
1210
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001211MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001212static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1213{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001214 const mbedtls_ssl_config *conf = ssl->conf;
1215
Ronald Cron6f135e12021-12-08 16:57:54 +01001216#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001217 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1218 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001219 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1222 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1223 }
1224
Jerry Yu60835a82021-08-04 10:13:52 +08001225 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1226 return( 0 );
1227 }
1228#endif
1229
1230#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001231 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001232 {
1233 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1234 return( 0 );
1235 }
1236#endif
1237
Ronald Cron6f135e12021-12-08 16:57:54 +01001238#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001239 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001240 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1242 {
1243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1244 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1245 }
1246
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001247 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1248 {
1249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1250 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1251 }
1252
1253
Ronald Crone1d3f062022-02-10 14:50:54 +01001254 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1255 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001256 }
1257#endif
1258
1259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1260 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1261}
1262
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001263MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001264static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1265{
1266 int ret;
1267 ret = ssl_conf_version_check( ssl );
1268 if( ret != 0 )
1269 return( ret );
1270
Jerry Yudef7ae42022-10-30 14:13:19 +08001271#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1272 /* RFC 8446 section 4.4.3
1273 *
1274 * If the verification fails, the receiver MUST terminate the handshake with
1275 * a "decrypt_error" alert.
1276 *
1277 * If the client is configured as TLS 1.3 only with optional verify, return
1278 * bad config.
1279 *
1280 */
1281 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1282 (mbedtls_ssl_context *)ssl ) &&
1283 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1284 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1285 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1286 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1287 {
1288 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001289 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001290 "is not available for TLS 1.3 client" ) );
1291 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1292 }
1293#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1294
Jerry Yu60835a82021-08-04 10:13:52 +08001295 /* Space for further checks */
1296
1297 return( 0 );
1298}
1299
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001300/*
1301 * Setup an SSL context
1302 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001303
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001304int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001305 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001306{
Janos Follath865b3eb2019-12-16 11:46:15 +00001307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001308 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1309 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001311 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001312
Jerry Yu60835a82021-08-04 10:13:52 +08001313 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1314 return( ret );
1315
Paul Bakker62f2dee2012-09-28 07:31:51 +00001316 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001317 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001318 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001319
1320 /* Set to NULL in case of an error condition */
1321 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001322
Darryl Greenb33cc762019-11-28 14:29:44 +00001323#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1324 ssl->in_buf_len = in_buf_len;
1325#endif
1326 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001327 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001330 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001331 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001332 }
1333
Darryl Greenb33cc762019-11-28 14:29:44 +00001334#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1335 ssl->out_buf_len = out_buf_len;
1336#endif
1337 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001338 if( ssl->out_buf == NULL )
1339 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001341 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001342 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 }
1344
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001345 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001346
Johan Pascalb62bb512015-12-03 21:56:45 +01001347#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001348 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001349#endif
1350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001352 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001355
1356error:
1357 mbedtls_free( ssl->in_buf );
1358 mbedtls_free( ssl->out_buf );
1359
1360 ssl->conf = NULL;
1361
Darryl Greenb33cc762019-11-28 14:29:44 +00001362#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1363 ssl->in_buf_len = 0;
1364 ssl->out_buf_len = 0;
1365#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001366 ssl->in_buf = NULL;
1367 ssl->out_buf = NULL;
1368
1369 ssl->in_hdr = NULL;
1370 ssl->in_ctr = NULL;
1371 ssl->in_len = NULL;
1372 ssl->in_iv = NULL;
1373 ssl->in_msg = NULL;
1374
1375 ssl->out_hdr = NULL;
1376 ssl->out_ctr = NULL;
1377 ssl->out_len = NULL;
1378 ssl->out_iv = NULL;
1379 ssl->out_msg = NULL;
1380
k-stachowiak9f7798e2018-07-31 16:52:32 +02001381 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001382}
1383
1384/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001385 * Reset an initialized and used SSL context for re-use while retaining
1386 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001387 *
1388 * If partial is non-zero, keep data in the input buffer and client ID.
1389 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001390 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001391void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1392 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001393{
Darryl Greenb33cc762019-11-28 14:29:44 +00001394#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1395 size_t in_buf_len = ssl->in_buf_len;
1396 size_t out_buf_len = ssl->out_buf_len;
1397#else
1398 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1399 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1400#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001401
Hanno Beckerb0302c42021-08-03 09:39:42 +01001402#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1403 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001404#endif
1405
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001406 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001407 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001408
Hanno Beckerb0302c42021-08-03 09:39:42 +01001409 mbedtls_ssl_reset_in_out_pointers( ssl );
1410
1411 /* Reset incoming message parsing */
1412 ssl->in_offt = NULL;
1413 ssl->nb_zero = 0;
1414 ssl->in_msgtype = 0;
1415 ssl->in_msglen = 0;
1416 ssl->in_hslen = 0;
1417 ssl->keep_current_message = 0;
1418 ssl->transform_in = NULL;
1419
1420#if defined(MBEDTLS_SSL_PROTO_DTLS)
1421 ssl->next_record_offset = 0;
1422 ssl->in_epoch = 0;
1423#endif
1424
1425 /* Keep current datagram if partial == 1 */
1426 if( partial == 0 )
1427 {
1428 ssl->in_left = 0;
1429 memset( ssl->in_buf, 0, in_buf_len );
1430 }
1431
Ronald Cronad8c17b2022-06-10 17:18:09 +02001432 ssl->send_alert = 0;
1433
Hanno Beckerb0302c42021-08-03 09:39:42 +01001434 /* Reset outgoing message writing */
1435 ssl->out_msgtype = 0;
1436 ssl->out_msglen = 0;
1437 ssl->out_left = 0;
1438 memset( ssl->out_buf, 0, out_buf_len );
1439 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1440 ssl->transform_out = NULL;
1441
1442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1443 mbedtls_ssl_dtls_replay_reset( ssl );
1444#endif
1445
XiaokangQian2b01dc32022-01-21 02:53:13 +00001446#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001447 if( ssl->transform )
1448 {
1449 mbedtls_ssl_transform_free( ssl->transform );
1450 mbedtls_free( ssl->transform );
1451 ssl->transform = NULL;
1452 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1454
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1456 mbedtls_ssl_transform_free( ssl->transform_application );
1457 mbedtls_free( ssl->transform_application );
1458 ssl->transform_application = NULL;
1459
1460 if( ssl->handshake != NULL )
1461 {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001462#if defined(MBEDTLS_SSL_EARLY_DATA)
XiaokangQian2b01dc32022-01-21 02:53:13 +00001463 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1464 mbedtls_free( ssl->handshake->transform_earlydata );
1465 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001466#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001467
1468 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1469 mbedtls_free( ssl->handshake->transform_handshake );
1470 ssl->handshake->transform_handshake = NULL;
1471 }
1472
XiaokangQian2b01dc32022-01-21 02:53:13 +00001473#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001474}
1475
1476int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1477{
1478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1479
1480 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1481
XiaokangQian78b1fa72022-01-19 06:56:30 +00001482 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483
1484 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SSL_RENEGOTIATION)
1486 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001487 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001488
1489 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1491 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001494
Hanno Beckerb0302c42021-08-03 09:39:42 +01001495 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001496 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001497 if( ssl->session )
1498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_ssl_session_free( ssl->session );
1500 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001501 ssl->session = NULL;
1502 }
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001505 ssl->alpn_chosen = NULL;
1506#endif
1507
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001508#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001509 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001510#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001511 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001512#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001513 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001514 {
1515 mbedtls_free( ssl->cli_id );
1516 ssl->cli_id = NULL;
1517 ssl->cli_id_len = 0;
1518 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001519#endif
1520
Paul Bakker48916f92012-09-16 19:57:18 +00001521 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1522 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001523
1524 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001525}
1526
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001527/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001528 * Reset an initialized and used SSL context for re-use while retaining
1529 * all application-set variables, function pointers and data.
1530 */
1531int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1532{
Hanno Becker43aefe22020-02-05 10:44:56 +00001533 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001534}
1535
1536/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 * SSL set accessors
1538 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001539void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001541 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001542}
1543
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001544void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001547}
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001550void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001551{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001552 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001553}
1554#endif
1555
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001556void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001558 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001559}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001562
Hanno Becker1841b0a2018-08-24 11:13:57 +01001563void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1564 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001565{
1566 ssl->disable_datagram_packing = !allow_packing;
1567}
1568
1569void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1570 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001572 conf->hs_timeout_min = min;
1573 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001574}
1575#endif
1576
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001577void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001578{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001579 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001580}
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001583void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001584 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001585 void *p_vrfy )
1586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001587 conf->f_vrfy = f_vrfy;
1588 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001591
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001592void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001593 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 void *p_rng )
1595{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 conf->f_rng = f_rng;
1597 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001598}
1599
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001600void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001601 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 void *p_dbg )
1603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001604 conf->f_dbg = f_dbg;
1605 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001606}
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001609 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001610 mbedtls_ssl_send_t *f_send,
1611 mbedtls_ssl_recv_t *f_recv,
1612 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001613{
1614 ssl->p_bio = p_bio;
1615 ssl->f_send = f_send;
1616 ssl->f_recv = f_recv;
1617 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001618}
1619
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001620#if defined(MBEDTLS_SSL_PROTO_DTLS)
1621void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1622{
1623 ssl->mtu = mtu;
1624}
1625#endif
1626
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001627void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001628{
1629 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001630}
1631
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001632void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1633 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001634 mbedtls_ssl_set_timer_t *f_set_timer,
1635 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001636{
1637 ssl->p_timer = p_timer;
1638 ssl->f_set_timer = f_set_timer;
1639 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001640
1641 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001642 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001643}
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001646void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001647 void *p_cache,
1648 mbedtls_ssl_cache_get_t *f_get_cache,
1649 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001651 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001652 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001653 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_SSL_CLI_C)
1658int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001659{
Janos Follath865b3eb2019-12-16 11:46:15 +00001660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001661
1662 if( ssl == NULL ||
1663 session == NULL ||
1664 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001665 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001668 }
1669
Hanno Beckere810bbc2021-05-14 16:01:05 +01001670 if( ssl->handshake->resume == 1 )
1671 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1672
Jerry Yu21092062022-10-10 21:21:31 +08001673#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1674 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001675 {
Jerry Yu21092062022-10-10 21:21:31 +08001676 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1677 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1678
1679 if( mbedtls_ssl_validate_ciphersuite(
1680 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1681 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1682 {
1683 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1684 session->ciphersuite ) );
1685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1686 }
Jerry Yu40afab62022-10-08 10:42:13 +08001687 }
Jerry Yu21092062022-10-10 21:21:31 +08001688#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001689
Hanno Becker52055ae2019-02-06 14:30:46 +00001690 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1691 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001692 return( ret );
1693
Paul Bakker0a597072012-09-25 21:55:46 +00001694 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001695
1696 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001700void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1701 const int *ciphersuites )
1702{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001703 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001704}
1705
Ronald Cron6f135e12021-12-08 16:57:54 +01001706#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001707void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001708 const int kex_modes )
1709{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001710 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001711}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001712
1713#if defined(MBEDTLS_SSL_EARLY_DATA)
1714void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001715 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001716{
1717 conf->early_data_enabled = early_data_enabled;
1718}
Jerry Yucc4e0072022-11-22 17:22:22 +08001719
1720#if defined(MBEDTLS_SSL_SRV_C)
1721void mbedtls_ssl_tls13_conf_max_early_data_size(
1722 mbedtls_ssl_config *conf, uint32_t max_early_data_size )
1723{
Jerry Yu39da9852022-12-06 16:58:36 +08001724 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001725}
1726#endif /* MBEDTLS_SSL_SRV_C */
1727
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001728#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001729#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001732void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001733 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001734{
1735 conf->cert_profile = profile;
1736}
1737
Glenn Strauss36872db2022-01-22 05:06:31 -05001738static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1739{
1740 mbedtls_ssl_key_cert *cur = key_cert, *next;
1741
1742 while( cur != NULL )
1743 {
1744 next = cur->next;
1745 mbedtls_free( cur );
1746 cur = next;
1747 }
1748}
1749
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001750/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001751MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001752static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1753 mbedtls_x509_crt *cert,
1754 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001755{
niisato8ee24222018-06-25 19:05:48 +09001756 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001757
Glenn Strauss36872db2022-01-22 05:06:31 -05001758 if( cert == NULL )
1759 {
1760 /* Free list if cert is null */
1761 ssl_key_cert_free( *head );
1762 *head = NULL;
1763 return( 0 );
1764 }
1765
niisato8ee24222018-06-25 19:05:48 +09001766 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1767 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001768 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001769
niisato8ee24222018-06-25 19:05:48 +09001770 new_cert->cert = cert;
1771 new_cert->key = key;
1772 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001773
Glenn Strauss36872db2022-01-22 05:06:31 -05001774 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001775 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001776 {
niisato8ee24222018-06-25 19:05:48 +09001777 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001778 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001779 else
1780 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001781 mbedtls_ssl_key_cert *cur = *head;
1782 while( cur->next != NULL )
1783 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001784 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001785 }
1786
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001790int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001791 mbedtls_x509_crt *own_cert,
1792 mbedtls_pk_context *pk_key )
1793{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001794 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001795}
1796
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001797void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001798 mbedtls_x509_crt *ca_chain,
1799 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001800{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001801 conf->ca_chain = ca_chain;
1802 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001803
1804#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1805 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1806 * cannot be used together. */
1807 conf->f_ca_cb = NULL;
1808 conf->p_ca_cb = NULL;
1809#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001810}
Hanno Becker5adaad92019-03-27 16:54:37 +00001811
1812#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1813void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001814 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001815 void *p_ca_cb )
1816{
1817 conf->f_ca_cb = f_ca_cb;
1818 conf->p_ca_cb = p_ca_cb;
1819
1820 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1821 * cannot be used together. */
1822 conf->ca_chain = NULL;
1823 conf->ca_crl = NULL;
1824}
1825#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001827
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001828#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001829const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1830 size_t *name_len )
1831{
1832 *name_len = ssl->handshake->sni_name_len;
1833 return( ssl->handshake->sni_name );
1834}
1835
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001836int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1837 mbedtls_x509_crt *own_cert,
1838 mbedtls_pk_context *pk_key )
1839{
1840 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1841 own_cert, pk_key ) );
1842}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001843
1844void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1845 mbedtls_x509_crt *ca_chain,
1846 mbedtls_x509_crl *ca_crl )
1847{
1848 ssl->handshake->sni_ca_chain = ca_chain;
1849 ssl->handshake->sni_ca_crl = ca_crl;
1850}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001851
Glenn Strauss999ef702022-03-11 01:37:23 -05001852#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1853void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1854 const mbedtls_x509_crt *crt)
1855{
1856 ssl->handshake->dn_hints = crt;
1857}
1858#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1859
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001860void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1861 int authmode )
1862{
1863 ssl->handshake->sni_authmode = authmode;
1864}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001865#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1866
Hanno Becker8927c832019-04-03 12:52:50 +01001867#if defined(MBEDTLS_X509_CRT_PARSE_C)
1868void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1869 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1870 void *p_vrfy )
1871{
1872 ssl->f_vrfy = f_vrfy;
1873 ssl->p_vrfy = p_vrfy;
1874}
1875#endif
1876
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001877#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001878/*
1879 * Set EC J-PAKE password for current handshake
1880 */
Valerio Setti02c25b52022-11-15 14:08:42 +01001881#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001882int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1883 const unsigned char *pw,
1884 size_t pw_len )
1885{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001886 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1888 psa_pake_role_t psa_role;
1889 psa_status_t status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001890
Janos Follath8eb64132016-06-03 15:40:57 +01001891 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1893
Neil Armstrongca7d5062022-05-31 14:43:23 +02001894 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1895 psa_role = PSA_PAKE_ROLE_SERVER;
1896 else
1897 psa_role = PSA_PAKE_ROLE_CLIENT;
1898
Valerio Settiaca21b72022-11-17 18:17:01 +01001899 /* Empty password is not valid */
1900 if( ( pw == NULL) || ( pw_len == 0 ) )
1901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001902
Valerio Settiaca21b72022-11-17 18:17:01 +01001903 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1904 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1905 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001906
Valerio Settiaca21b72022-11-17 18:17:01 +01001907 status = psa_import_key( &attributes, pw, pw_len,
1908 &ssl->handshake->psa_pake_password );
1909 if( status != PSA_SUCCESS )
1910 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001911
1912 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1913 psa_pake_cs_set_primitive( &cipher_suite,
1914 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1915 PSA_ECC_FAMILY_SECP_R1,
1916 256) );
1917 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1918
1919 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1920 if( status != PSA_SUCCESS )
1921 {
1922 psa_destroy_key( ssl->handshake->psa_pake_password );
1923 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1924 }
1925
1926 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1927 if( status != PSA_SUCCESS )
1928 {
1929 psa_destroy_key( ssl->handshake->psa_pake_password );
1930 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1931 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1932 }
1933
Valerio Settiaca21b72022-11-17 18:17:01 +01001934 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1935 ssl->handshake->psa_pake_password );
1936 if( status != PSA_SUCCESS )
Neil Armstrongca7d5062022-05-31 14:43:23 +02001937 {
Valerio Settiaca21b72022-11-17 18:17:01 +01001938 psa_destroy_key( ssl->handshake->psa_pake_password );
1939 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1940 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001941 }
1942
1943 ssl->handshake->psa_pake_ctx_is_ok = 1;
1944
1945 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001946}
1947#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001948int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1949 const unsigned char *pw,
1950 size_t pw_len )
1951{
1952 mbedtls_ecjpake_role role;
1953
1954 if( ssl->handshake == NULL || ssl->conf == NULL )
1955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1956
1957 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1958 role = MBEDTLS_ECJPAKE_SERVER;
1959 else
1960 role = MBEDTLS_ECJPAKE_CLIENT;
1961
1962 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1963 role,
1964 MBEDTLS_MD_SHA256,
1965 MBEDTLS_ECP_DP_SECP256R1,
1966 pw, pw_len ) );
1967}
Valerio Setti02c25b52022-11-15 14:08:42 +01001968#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001969#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001970
Ronald Cron73fe8df2022-10-05 14:31:43 +02001971#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001972int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001973{
Ronald Crond29e13e2022-10-19 10:33:48 +02001974 if( conf->psk_identity == NULL ||
1975 conf->psk_identity_len == 0 )
1976 {
1977 return( 0 );
1978 }
1979
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001980#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001981 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001982 return( 1 );
1983#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001984
1985 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001986 return( 1 );
1987
1988 return( 0 );
1989}
1990
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001991static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1992{
1993 /* Remove reference to existing PSK, if any. */
1994#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001995 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001996 {
1997 /* The maintenance of the PSK key slot is the
1998 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001999 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002000 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002001#endif /* MBEDTLS_USE_PSA_CRYPTO */
2002 if( conf->psk != NULL )
2003 {
2004 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
2005
2006 mbedtls_free( conf->psk );
2007 conf->psk = NULL;
2008 conf->psk_len = 0;
2009 }
2010
2011 /* Remove reference to PSK identity, if any. */
2012 if( conf->psk_identity != NULL )
2013 {
2014 mbedtls_free( conf->psk_identity );
2015 conf->psk_identity = NULL;
2016 conf->psk_identity_len = 0;
2017 }
2018}
2019
Hanno Becker7390c712018-11-15 13:33:04 +00002020/* This function assumes that PSK identity in the SSL config is unset.
2021 * It checks that the provided identity is well-formed and attempts
2022 * to make a copy of it in the SSL config.
2023 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002024MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002025static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2026 unsigned char const *psk_identity,
2027 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002028{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002029 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002030 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002031 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002032 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002033 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002034 {
2035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2036 }
2037
Hanno Becker7390c712018-11-15 13:33:04 +00002038 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2039 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002040 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002041
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002042 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002043 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002044
2045 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002046}
2047
Hanno Becker7390c712018-11-15 13:33:04 +00002048int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2049 const unsigned char *psk, size_t psk_len,
2050 const unsigned char *psk_identity, size_t psk_identity_len )
2051{
Janos Follath865b3eb2019-12-16 11:46:15 +00002052 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002053
2054 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002055 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002056 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002057
2058 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002059 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002061 if( psk_len == 0 )
2062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2063 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2065
Hanno Becker7390c712018-11-15 13:33:04 +00002066 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2067 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2068 conf->psk_len = psk_len;
2069 memcpy( conf->psk, psk, conf->psk_len );
2070
2071 /* Check and set PSK Identity */
2072 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2073 if( ret != 0 )
2074 ssl_conf_remove_psk( conf );
2075
2076 return( ret );
2077}
2078
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002079static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2080{
2081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002082 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002083 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002084 /* The maintenance of the external PSK key slot is the
2085 * user's responsibility. */
2086 if( ssl->handshake->psk_opaque_is_internal )
2087 {
2088 psa_destroy_key( ssl->handshake->psk_opaque );
2089 ssl->handshake->psk_opaque_is_internal = 0;
2090 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002091 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002092 }
Neil Armstronge952a302022-05-03 10:22:14 +02002093#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002094 if( ssl->handshake->psk != NULL )
2095 {
2096 mbedtls_platform_zeroize( ssl->handshake->psk,
2097 ssl->handshake->psk_len );
2098 mbedtls_free( ssl->handshake->psk );
2099 ssl->handshake->psk_len = 0;
2100 }
Neil Armstronge952a302022-05-03 10:22:14 +02002101#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002102}
2103
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002104int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2105 const unsigned char *psk, size_t psk_len )
2106{
Neil Armstrong501c9322022-05-03 09:35:09 +02002107#if defined(MBEDTLS_USE_PSA_CRYPTO)
2108 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002110 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002112#endif /* MBEDTLS_USE_PSA_CRYPTO */
2113
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002114 if( psk == NULL || ssl->handshake == NULL )
2115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2116
2117 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2119
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002120 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002121
Neil Armstrong501c9322022-05-03 09:35:09 +02002122#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002123#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2124 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2125 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002126 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002127 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2128 else
2129 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2130 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2131 }
2132#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002133
Jerry Yu568ec252022-07-22 21:27:34 +08002134#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002135 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2136 {
2137 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2138 psa_set_key_usage_flags( &key_attributes,
2139 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2140 }
2141#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2142
Neil Armstrong501c9322022-05-03 09:35:09 +02002143 psa_set_key_algorithm( &key_attributes, alg );
2144 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2145
2146 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2147 if( status != PSA_SUCCESS )
2148 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2149
2150 /* Allow calling psa_destroy_key() on psk remove */
2151 ssl->handshake->psk_opaque_is_internal = 1;
2152 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2153#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002154 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002155 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002156
2157 ssl->handshake->psk_len = psk_len;
2158 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2159
2160 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002161#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002162}
2163
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002164#if defined(MBEDTLS_USE_PSA_CRYPTO)
2165int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002166 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002167 const unsigned char *psk_identity,
2168 size_t psk_identity_len )
2169{
Janos Follath865b3eb2019-12-16 11:46:15 +00002170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002171
2172 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002173 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002174 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002175
Hanno Becker7390c712018-11-15 13:33:04 +00002176 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002177 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002179 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002180
2181 /* Check and set PSK Identity */
2182 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2183 psk_identity_len );
2184 if( ret != 0 )
2185 ssl_conf_remove_psk( conf );
2186
2187 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002188}
2189
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002190int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2191 mbedtls_svc_key_id_t psk )
2192{
2193 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2194 ( ssl->handshake == NULL ) )
2195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2196
2197 ssl_remove_psk( ssl );
2198 ssl->handshake->psk_opaque = psk;
2199 return( 0 );
2200}
2201#endif /* MBEDTLS_USE_PSA_CRYPTO */
2202
Jerry Yu8897c072022-08-12 13:56:53 +08002203#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002204void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2205 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2206 size_t),
2207 void *p_psk )
2208{
2209 conf->f_psk = f_psk;
2210 conf->p_psk = p_psk;
2211}
Jerry Yu8897c072022-08-12 13:56:53 +08002212#endif /* MBEDTLS_SSL_SRV_C */
2213
Ronald Cron73fe8df2022-10-05 14:31:43 +02002214#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002215
2216#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002217static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2218 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002219{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002220#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002221 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002222 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002223#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002224 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002225 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002226 return( MBEDTLS_SSL_MODE_STREAM );
2227}
2228
2229#else /* MBEDTLS_USE_PSA_CRYPTO */
2230
2231static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2232 mbedtls_cipher_mode_t mode )
2233{
2234#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2235 if( mode == MBEDTLS_MODE_CBC )
2236 return( MBEDTLS_SSL_MODE_CBC );
2237#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2238
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002239#if defined(MBEDTLS_GCM_C) || \
2240 defined(MBEDTLS_CCM_C) || \
2241 defined(MBEDTLS_CHACHAPOLY_C)
2242 if( mode == MBEDTLS_MODE_GCM ||
2243 mode == MBEDTLS_MODE_CCM ||
2244 mode == MBEDTLS_MODE_CHACHAPOLY )
2245 return( MBEDTLS_SSL_MODE_AEAD );
2246#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002247
2248 return( MBEDTLS_SSL_MODE_STREAM );
2249}
Gilles Peskine301711e2022-04-26 16:57:05 +02002250#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002251
Gilles Peskinee108d982022-04-26 16:50:40 +02002252static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2253 mbedtls_ssl_mode_t base_mode,
2254 int encrypt_then_mac )
2255{
2256#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2257 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2258 base_mode == MBEDTLS_SSL_MODE_CBC )
2259 {
2260 return( MBEDTLS_SSL_MODE_CBC_ETM );
2261 }
2262#else
2263 (void) encrypt_then_mac;
2264#endif
2265 return( base_mode );
2266}
2267
Neil Armstrongab555e02022-04-04 11:07:59 +02002268mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002269 const mbedtls_ssl_transform *transform )
2270{
Gilles Peskinee108d982022-04-26 16:50:40 +02002271 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002272#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002273 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002274#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002275 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2276#endif
2277 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002278
Gilles Peskinee108d982022-04-26 16:50:40 +02002279 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002280#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002281 encrypt_then_mac = transform->encrypt_then_mac;
2282#endif
2283 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002284}
2285
Neil Armstrongab555e02022-04-04 11:07:59 +02002286mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002287#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002288 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002289#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002290 const mbedtls_ssl_ciphersuite_t *suite )
2291{
Gilles Peskinee108d982022-04-26 16:50:40 +02002292 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2293
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002294#if defined(MBEDTLS_USE_PSA_CRYPTO)
2295 psa_status_t status;
2296 psa_algorithm_t alg;
2297 psa_key_type_t type;
2298 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002299 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2300 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002301 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002302#else
2303 const mbedtls_cipher_info_t *cipher =
2304 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002305 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002306 {
2307 base_mode =
2308 mbedtls_ssl_get_base_mode(
2309 mbedtls_cipher_info_get_mode( cipher ) );
2310 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002311#endif /* MBEDTLS_USE_PSA_CRYPTO */
2312
Gilles Peskinee108d982022-04-26 16:50:40 +02002313#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2314 int encrypt_then_mac = 0;
2315#endif
2316 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002317}
2318
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002319#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002320
2321#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002322/* Serialization of TLS 1.3 sessions:
2323 *
2324 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002325 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002326 * uint64 ticket_received;
2327 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002328 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002329 * } ClientOnlyData;
2330 *
2331 * struct {
2332 * uint8 endpoint;
2333 * uint8 ciphersuite[2];
2334 * uint32 ticket_age_add;
2335 * uint8 ticket_flags;
2336 * opaque resumption_key<0..255>;
2337 * select ( endpoint ) {
2338 * case client: ClientOnlyData;
2339 * case server: uint64 start_time;
2340 * };
2341 * } serialized_session_tls13;
2342 *
2343 */
2344#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002345MBEDTLS_CHECK_RETURN_CRITICAL
2346static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2347 unsigned char *buf,
2348 size_t buf_len,
2349 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002350{
2351 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002352#if defined(MBEDTLS_SSL_CLI_C) && \
2353 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2354 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002355 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002356#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002357 size_t needed = 1 /* endpoint */
2358 + 2 /* ciphersuite */
2359 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002360 + 1 /* ticket_flags */
2361 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002362 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002363
2364 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2366 needed += session->resumption_key_len; /* resumption_key */
2367
Jerry Yu438ddd82022-07-07 06:55:50 +00002368#if defined(MBEDTLS_HAVE_TIME)
2369 needed += 8; /* start_time or ticket_received */
2370#endif
2371
2372#if defined(MBEDTLS_SSL_CLI_C)
2373 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2374 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002375#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002376 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002377 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002378#endif
2379
Jerry Yu438ddd82022-07-07 06:55:50 +00002380 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002381 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002382
2383 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002384 if( session->ticket_len > SIZE_MAX - needed )
2385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002386
Jerry Yue28d9742022-08-18 15:44:03 +08002387 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002388 }
2389#endif /* MBEDTLS_SSL_CLI_C */
2390
Jerry Yue36fdd62022-08-17 21:31:36 +08002391 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002392 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002393 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002394
2395 p[0] = session->endpoint;
2396 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2397 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2398 p[7] = session->ticket_flags;
2399
2400 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002401 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002402 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002403 memcpy( p, session->resumption_key, session->resumption_key_len );
2404 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002405
2406#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2407 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2408 {
2409 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2410 p += 8;
2411 }
2412#endif /* MBEDTLS_HAVE_TIME */
2413
2414#if defined(MBEDTLS_SSL_CLI_C)
2415 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2416 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002417#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2418 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2419 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002420 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002421 {
2422 /* save host name */
2423 memcpy( p, session->hostname, hostname_len );
2424 p += hostname_len;
2425 }
2426#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2427
Jerry Yu438ddd82022-07-07 06:55:50 +00002428#if defined(MBEDTLS_HAVE_TIME)
2429 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2430 p += 8;
2431#endif
2432 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2433 p += 4;
2434
2435 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2436 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002437
2438 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002439 {
2440 memcpy( p, session->ticket, session->ticket_len );
2441 p += session->ticket_len;
2442 }
2443 }
2444#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002445 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002446}
2447
2448MBEDTLS_CHECK_RETURN_CRITICAL
2449static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2450 const unsigned char *buf,
2451 size_t len )
2452{
2453 const unsigned char *p = buf;
2454 const unsigned char *end = buf + len;
2455
2456 if( end - p < 9 )
2457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2458 session->endpoint = p[0];
2459 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2460 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2461 session->ticket_flags = p[7];
2462
2463 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002464 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002465 p += 9;
2466
Jerry Yubc7c1a42022-07-21 22:57:37 +08002467 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2469
Jerry Yubc7c1a42022-07-21 22:57:37 +08002470 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002472 memcpy( session->resumption_key, p, session->resumption_key_len );
2473 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002474
2475#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2476 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2477 {
2478 if( end - p < 8 )
2479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2480 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2481 p += 8;
2482 }
2483#endif /* MBEDTLS_HAVE_TIME */
2484
2485#if defined(MBEDTLS_SSL_CLI_C)
2486 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2487 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002488#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2489 defined(MBEDTLS_SSL_SESSION_TICKETS)
2490 size_t hostname_len;
2491 /* load host name */
2492 if( end - p < 2 )
2493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2494 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2495 p += 2;
2496
2497 if( end - p < ( long int )hostname_len )
2498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2499 if( hostname_len > 0 )
2500 {
2501 session->hostname = mbedtls_calloc( 1, hostname_len );
2502 if( session->hostname == NULL )
2503 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2504 memcpy( session->hostname, p, hostname_len );
2505 p += hostname_len;
2506 }
2507#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2508 MBEDTLS_SSL_SESSION_TICKETS */
2509
Jerry Yu438ddd82022-07-07 06:55:50 +00002510#if defined(MBEDTLS_HAVE_TIME)
2511 if( end - p < 8 )
2512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2513 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2514 p += 8;
2515#endif
2516 if( end - p < 4 )
2517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2518 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2519 p += 4;
2520
2521 if( end - p < 2 )
2522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2523 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2524 p += 2;
2525
2526 if( end - p < ( long int )session->ticket_len )
2527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2528 if( session->ticket_len > 0 )
2529 {
2530 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2531 if( session->ticket == NULL )
2532 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2533 memcpy( session->ticket, p, session->ticket_len );
2534 p += session->ticket_len;
2535 }
2536 }
2537#endif /* MBEDTLS_SSL_CLI_C */
2538
2539 return( 0 );
2540
2541}
2542#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002543MBEDTLS_CHECK_RETURN_CRITICAL
2544static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2545 unsigned char *buf,
2546 size_t buf_len,
2547 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002548{
2549 ((void) session);
2550 ((void) buf);
2551 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002552 *olen = 0;
2553 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002554}
Jerry Yu438ddd82022-07-07 06:55:50 +00002555
2556static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2557 unsigned char *buf,
2558 size_t buf_len )
2559{
2560 ((void) session);
2561 ((void) buf);
2562 ((void) buf_len);
2563 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2564}
2565#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002566#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2567
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002568psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002569 size_t taglen,
2570 psa_algorithm_t *alg,
2571 psa_key_type_t *key_type,
2572 size_t *key_size )
2573{
2574 switch ( mbedtls_cipher_type )
2575 {
2576 case MBEDTLS_CIPHER_AES_128_CBC:
2577 *alg = PSA_ALG_CBC_NO_PADDING;
2578 *key_type = PSA_KEY_TYPE_AES;
2579 *key_size = 128;
2580 break;
2581 case MBEDTLS_CIPHER_AES_128_CCM:
2582 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2583 *key_type = PSA_KEY_TYPE_AES;
2584 *key_size = 128;
2585 break;
2586 case MBEDTLS_CIPHER_AES_128_GCM:
2587 *alg = PSA_ALG_GCM;
2588 *key_type = PSA_KEY_TYPE_AES;
2589 *key_size = 128;
2590 break;
2591 case MBEDTLS_CIPHER_AES_192_CCM:
2592 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2593 *key_type = PSA_KEY_TYPE_AES;
2594 *key_size = 192;
2595 break;
2596 case MBEDTLS_CIPHER_AES_192_GCM:
2597 *alg = PSA_ALG_GCM;
2598 *key_type = PSA_KEY_TYPE_AES;
2599 *key_size = 192;
2600 break;
2601 case MBEDTLS_CIPHER_AES_256_CBC:
2602 *alg = PSA_ALG_CBC_NO_PADDING;
2603 *key_type = PSA_KEY_TYPE_AES;
2604 *key_size = 256;
2605 break;
2606 case MBEDTLS_CIPHER_AES_256_CCM:
2607 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2608 *key_type = PSA_KEY_TYPE_AES;
2609 *key_size = 256;
2610 break;
2611 case MBEDTLS_CIPHER_AES_256_GCM:
2612 *alg = PSA_ALG_GCM;
2613 *key_type = PSA_KEY_TYPE_AES;
2614 *key_size = 256;
2615 break;
2616 case MBEDTLS_CIPHER_ARIA_128_CBC:
2617 *alg = PSA_ALG_CBC_NO_PADDING;
2618 *key_type = PSA_KEY_TYPE_ARIA;
2619 *key_size = 128;
2620 break;
2621 case MBEDTLS_CIPHER_ARIA_128_CCM:
2622 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2623 *key_type = PSA_KEY_TYPE_ARIA;
2624 *key_size = 128;
2625 break;
2626 case MBEDTLS_CIPHER_ARIA_128_GCM:
2627 *alg = PSA_ALG_GCM;
2628 *key_type = PSA_KEY_TYPE_ARIA;
2629 *key_size = 128;
2630 break;
2631 case MBEDTLS_CIPHER_ARIA_192_CCM:
2632 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2633 *key_type = PSA_KEY_TYPE_ARIA;
2634 *key_size = 192;
2635 break;
2636 case MBEDTLS_CIPHER_ARIA_192_GCM:
2637 *alg = PSA_ALG_GCM;
2638 *key_type = PSA_KEY_TYPE_ARIA;
2639 *key_size = 192;
2640 break;
2641 case MBEDTLS_CIPHER_ARIA_256_CBC:
2642 *alg = PSA_ALG_CBC_NO_PADDING;
2643 *key_type = PSA_KEY_TYPE_ARIA;
2644 *key_size = 256;
2645 break;
2646 case MBEDTLS_CIPHER_ARIA_256_CCM:
2647 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2648 *key_type = PSA_KEY_TYPE_ARIA;
2649 *key_size = 256;
2650 break;
2651 case MBEDTLS_CIPHER_ARIA_256_GCM:
2652 *alg = PSA_ALG_GCM;
2653 *key_type = PSA_KEY_TYPE_ARIA;
2654 *key_size = 256;
2655 break;
2656 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2657 *alg = PSA_ALG_CBC_NO_PADDING;
2658 *key_type = PSA_KEY_TYPE_CAMELLIA;
2659 *key_size = 128;
2660 break;
2661 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2662 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2663 *key_type = PSA_KEY_TYPE_CAMELLIA;
2664 *key_size = 128;
2665 break;
2666 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2667 *alg = PSA_ALG_GCM;
2668 *key_type = PSA_KEY_TYPE_CAMELLIA;
2669 *key_size = 128;
2670 break;
2671 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2672 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2673 *key_type = PSA_KEY_TYPE_CAMELLIA;
2674 *key_size = 192;
2675 break;
2676 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2677 *alg = PSA_ALG_GCM;
2678 *key_type = PSA_KEY_TYPE_CAMELLIA;
2679 *key_size = 192;
2680 break;
2681 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2682 *alg = PSA_ALG_CBC_NO_PADDING;
2683 *key_type = PSA_KEY_TYPE_CAMELLIA;
2684 *key_size = 256;
2685 break;
2686 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2687 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2688 *key_type = PSA_KEY_TYPE_CAMELLIA;
2689 *key_size = 256;
2690 break;
2691 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2692 *alg = PSA_ALG_GCM;
2693 *key_type = PSA_KEY_TYPE_CAMELLIA;
2694 *key_size = 256;
2695 break;
2696 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2697 *alg = PSA_ALG_CHACHA20_POLY1305;
2698 *key_type = PSA_KEY_TYPE_CHACHA20;
2699 *key_size = 256;
2700 break;
2701 case MBEDTLS_CIPHER_NULL:
2702 *alg = MBEDTLS_SSL_NULL_CIPHER;
2703 *key_type = 0;
2704 *key_size = 0;
2705 break;
2706 default:
2707 return PSA_ERROR_NOT_SUPPORTED;
2708 }
2709
2710 return PSA_SUCCESS;
2711}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002712#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002713
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002714#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002715int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2716 const unsigned char *dhm_P, size_t P_len,
2717 const unsigned char *dhm_G, size_t G_len )
2718{
Janos Follath865b3eb2019-12-16 11:46:15 +00002719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002720
Glenn Strausscee11292021-12-20 01:43:17 -05002721 mbedtls_mpi_free( &conf->dhm_P );
2722 mbedtls_mpi_free( &conf->dhm_G );
2723
Hanno Beckera90658f2017-10-04 15:29:08 +01002724 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2725 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2726 {
2727 mbedtls_mpi_free( &conf->dhm_P );
2728 mbedtls_mpi_free( &conf->dhm_G );
2729 return( ret );
2730 }
2731
2732 return( 0 );
2733}
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002735int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002736{
Janos Follath865b3eb2019-12-16 11:46:15 +00002737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002738
Glenn Strausscee11292021-12-20 01:43:17 -05002739 mbedtls_mpi_free( &conf->dhm_P );
2740 mbedtls_mpi_free( &conf->dhm_G );
2741
Gilles Peskinee5702482021-06-11 21:59:08 +02002742 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2743 &conf->dhm_P ) ) != 0 ||
2744 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2745 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002746 {
2747 mbedtls_mpi_free( &conf->dhm_P );
2748 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002749 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002750 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002751
2752 return( 0 );
2753}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002754#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002755
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002756#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2757/*
2758 * Set the minimum length for Diffie-Hellman parameters
2759 */
2760void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2761 unsigned int bitlen )
2762{
2763 conf->dhm_min_bitlen = bitlen;
2764}
2765#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2766
Ronald Crone68ab4f2022-10-05 12:46:29 +02002767#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002768#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002769/*
2770 * Set allowed/preferred hashes for handshake signatures
2771 */
2772void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2773 const int *hashes )
2774{
2775 conf->sig_hashes = hashes;
2776}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002777#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002778
Jerry Yuf017ee42022-01-12 15:49:48 +08002779/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002780void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2781 const uint16_t* sig_algs )
2782{
Jerry Yuf017ee42022-01-12 15:49:48 +08002783#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2784 conf->sig_hashes = NULL;
2785#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2786 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002787}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002788#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002789
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002790#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002791#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002792/*
2793 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002794 *
2795 * mbedtls_ssl_setup() takes the provided list
2796 * and translates it to a list of IANA TLS group identifiers,
2797 * stored in ssl->handshake->group_list.
2798 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002799 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002800void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002801 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002802{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002803 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002804 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002805}
Brett Warrene0edc842021-08-17 09:53:13 +01002806#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002807#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002808
Brett Warrene0edc842021-08-17 09:53:13 +01002809/*
2810 * Set the allowed groups
2811 */
2812void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2813 const uint16_t *group_list )
2814{
2815#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2816 conf->curve_list = NULL;
2817#endif
2818 conf->group_list = group_list;
2819}
2820
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002821#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002823{
Hanno Becker947194e2017-04-07 13:25:49 +01002824 /* Initialize to suppress unnecessary compiler warning */
2825 size_t hostname_len = 0;
2826
2827 /* Check if new hostname is valid before
2828 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002829 if( hostname != NULL )
2830 {
2831 hostname_len = strlen( hostname );
2832
2833 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2835 }
2836
2837 /* Now it's clear that we will overwrite the old hostname,
2838 * so we can free it safely */
2839
2840 if( ssl->hostname != NULL )
2841 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002842 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002843 mbedtls_free( ssl->hostname );
2844 }
2845
2846 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002847
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002849 {
2850 ssl->hostname = NULL;
2851 }
2852 else
2853 {
2854 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002855 if( ssl->hostname == NULL )
2856 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002857
Hanno Becker947194e2017-04-07 13:25:49 +01002858 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002859
Hanno Becker947194e2017-04-07 13:25:49 +01002860 ssl->hostname[hostname_len] = '\0';
2861 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
2863 return( 0 );
2864}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002865#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002867#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002868void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002870 const unsigned char *, size_t),
2871 void *p_sni )
2872{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002873 conf->f_sni = f_sni;
2874 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002879int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002880{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002881 size_t cur_len, tot_len;
2882 const char **p;
2883
2884 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002885 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2886 * MUST NOT be truncated."
2887 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002888 */
2889 tot_len = 0;
2890 for( p = protos; *p != NULL; p++ )
2891 {
2892 cur_len = strlen( *p );
2893 tot_len += cur_len;
2894
Ronald Cron8216dd32020-04-23 16:41:44 +02002895 if( ( cur_len == 0 ) ||
2896 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2897 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002899 }
2900
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002901 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002902
2903 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002904}
2905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002907{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002908 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002911
Johan Pascalb62bb512015-12-03 21:56:45 +01002912#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002913void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2914 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002915{
2916 conf->dtls_srtp_mki_support = support_mki_value;
2917}
2918
Ron Eldoref72faf2018-07-12 11:54:20 +03002919int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2920 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002921 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002922{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002923 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002924 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002926 }
Ron Eldor591f1622018-01-22 12:30:04 +02002927
2928 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002929 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002930 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002931 }
Ron Eldor591f1622018-01-22 12:30:04 +02002932
2933 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2934 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002935 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002936}
2937
Ron Eldoref72faf2018-07-12 11:54:20 +03002938int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002939 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002940{
Johan Pascal253d0262020-09-22 13:04:45 +02002941 const mbedtls_ssl_srtp_profile *p;
2942 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002943
Johan Pascal253d0262020-09-22 13:04:45 +02002944 /* check the profiles list: all entry must be valid,
2945 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002946 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2947 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2948 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002949 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002950 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002951 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002952 list_size++;
2953 }
2954 else
2955 {
2956 /* unsupported value, stop parsing and set the size to an error value */
2957 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002958 }
2959 }
2960
Johan Pascal5ef72d22020-10-28 17:05:47 +01002961 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002962 {
Johan Pascal253d0262020-09-22 13:04:45 +02002963 conf->dtls_srtp_profile_list = NULL;
2964 conf->dtls_srtp_profile_list_len = 0;
2965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2966 }
2967
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002968 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002969 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002970
2971 return( 0 );
2972}
2973
Johan Pascal5ef72d22020-10-28 17:05:47 +01002974void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2975 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002976{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002977 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2978 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002979 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002980 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002981 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002982 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002983 else
2984 {
2985 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002986 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2987 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002988 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002989}
Johan Pascalb62bb512015-12-03 21:56:45 +01002990#endif /* MBEDTLS_SSL_DTLS_SRTP */
2991
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302992#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002993void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002994{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002995 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002996}
2997
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002998void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002999{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003000 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003001}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303002#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003003
Janos Follath088ce432017-04-10 12:42:31 +01003004#if defined(MBEDTLS_SSL_SRV_C)
3005void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
3006 char cert_req_ca_list )
3007{
3008 conf->cert_req_ca_list = cert_req_ca_list;
3009}
3010#endif
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003013void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003014{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003015 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003016}
3017#endif
3018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003020void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003021{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003022 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003023}
3024#endif
3025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003027int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003030 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003033 }
3034
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003035 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003036
3037 return( 0 );
3038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003040
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003041void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003042{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003043 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003044}
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003047void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003049 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003050}
3051
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003052void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003054 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003055}
3056
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003057void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003058 const unsigned char period[8] )
3059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003060 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003065#if defined(MBEDTLS_SSL_CLI_C)
3066void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003067{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003068 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003069}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003070#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003071
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003072#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003073
Jerry Yud0766ec2022-09-22 10:46:57 +08003074#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003075void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3076 uint16_t num_tickets )
3077{
Jerry Yud0766ec2022-09-22 10:46:57 +08003078 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003079}
3080#endif
3081
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003082void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3083 mbedtls_ssl_ticket_write_t *f_ticket_write,
3084 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3085 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003086{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003087 conf->f_ticket_write = f_ticket_write;
3088 conf->f_ticket_parse = f_ticket_parse;
3089 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003090}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003093
Hanno Becker7e6c1782021-06-08 09:24:55 +01003094void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3095 mbedtls_ssl_export_keys_t *f_export_keys,
3096 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003097{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003098 ssl->f_export_keys = f_export_keys;
3099 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003100}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003101
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003102#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003103void mbedtls_ssl_conf_async_private_cb(
3104 mbedtls_ssl_config *conf,
3105 mbedtls_ssl_async_sign_t *f_async_sign,
3106 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3107 mbedtls_ssl_async_resume_t *f_async_resume,
3108 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003109 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003110{
3111 conf->f_async_sign_start = f_async_sign;
3112 conf->f_async_decrypt_start = f_async_decrypt;
3113 conf->f_async_resume = f_async_resume;
3114 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003115 conf->p_async_config_data = async_config_data;
3116}
3117
Gilles Peskine8f97af72018-04-26 11:46:10 +02003118void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3119{
3120 return( conf->p_async_config_data );
3121}
3122
Gilles Peskine1febfef2018-04-30 11:54:39 +02003123void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003124{
3125 if( ssl->handshake == NULL )
3126 return( NULL );
3127 else
3128 return( ssl->handshake->user_async_ctx );
3129}
3130
Gilles Peskine1febfef2018-04-30 11:54:39 +02003131void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003132 void *ctx )
3133{
3134 if( ssl->handshake != NULL )
3135 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003136}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003137#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003138
Paul Bakker5121ce52009-01-03 21:22:43 +00003139/*
3140 * SSL get accessors
3141 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003142uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003143{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003144 if( ssl->session != NULL )
3145 return( ssl->session->verify_result );
3146
3147 if( ssl->session_negotiate != NULL )
3148 return( ssl->session_negotiate->verify_result );
3149
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003150 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151}
3152
Glenn Strauss8f526902022-01-13 00:04:49 -05003153int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3154{
3155 if( ssl == NULL || ssl->session == NULL )
3156 return( 0 );
3157
3158 return( ssl->session->ciphersuite );
3159}
3160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003162{
Paul Bakker926c8e42013-03-06 10:23:34 +01003163 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003164 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003167}
3168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003173 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003174 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003175 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003176 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003177 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003178 default:
3179 return( "unknown (DTLS)" );
3180 }
3181 }
3182#endif
3183
Glenn Strauss60bfe602022-03-14 19:04:24 -04003184 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003185 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003186 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003187 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003188 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003189 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003190 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003191 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003192 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003193}
3194
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003195#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003196size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3197{
David Horstmann95d516f2021-05-04 18:36:56 +01003198 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003199 size_t read_mfl;
3200
Jerry Yuddda0502022-12-01 19:43:12 +08003201#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003202 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3203 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3204 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3205 {
3206 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3207 }
Jerry Yuddda0502022-12-01 19:43:12 +08003208#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003209
3210 /* Check if a smaller max length was negotiated */
3211 if( ssl->session_out != NULL )
3212 {
3213 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3214 if( read_mfl < max_len )
3215 {
3216 max_len = read_mfl;
3217 }
3218 }
3219
Jerry Yuddda0502022-12-01 19:43:12 +08003220 /* During a handshake, use the value being negotiated */
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003221 if( ssl->session_negotiate != NULL )
3222 {
3223 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3224 if( read_mfl < max_len )
3225 {
3226 max_len = read_mfl;
3227 }
3228 }
3229
3230 return( max_len );
3231}
3232
3233size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003234{
3235 size_t max_len;
3236
3237 /*
3238 * Assume mfl_code is correct since it was checked when set
3239 */
Angus Grattond8213d02016-05-25 20:56:48 +10003240 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003241
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003242 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003243 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003244 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003245 {
Angus Grattond8213d02016-05-25 20:56:48 +10003246 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003247 }
3248
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003249 /* During a handshake, use the value being negotiated */
3250 if( ssl->session_negotiate != NULL &&
3251 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3252 {
3253 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3254 }
3255
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003256 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003257}
3258#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3259
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003260#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003261size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003262{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003263 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3264 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3265 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3266 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3267 return ( 0 );
3268
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003269 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3270 return( ssl->mtu );
3271
3272 if( ssl->mtu == 0 )
3273 return( ssl->handshake->mtu );
3274
3275 return( ssl->mtu < ssl->handshake->mtu ?
3276 ssl->mtu : ssl->handshake->mtu );
3277}
3278#endif /* MBEDTLS_SSL_PROTO_DTLS */
3279
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003280int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3281{
3282 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3283
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003284#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3285 !defined(MBEDTLS_SSL_PROTO_DTLS)
3286 (void) ssl;
3287#endif
3288
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003289#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003290 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003291
3292 if( max_len > mfl )
3293 max_len = mfl;
3294#endif
3295
3296#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003297 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003298 {
Hanno Becker89490712020-02-05 10:50:12 +00003299 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003300 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3301 const size_t overhead = (size_t) ret;
3302
3303 if( ret < 0 )
3304 return( ret );
3305
3306 if( mtu <= overhead )
3307 {
3308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3309 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3310 }
3311
3312 if( max_len > mtu - overhead )
3313 max_len = mtu - overhead;
3314 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003315#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003316
Hanno Becker0defedb2018-08-10 12:35:02 +01003317#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3318 !defined(MBEDTLS_SSL_PROTO_DTLS)
3319 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003320#endif
3321
3322 return( (int) max_len );
3323}
3324
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003325int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3326{
3327 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3328
3329#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3330 (void) ssl;
3331#endif
3332
3333#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3334 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3335
3336 if( max_len > mfl )
3337 max_len = mfl;
3338#endif
3339
3340 return( (int) max_len );
3341}
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#if defined(MBEDTLS_X509_CRT_PARSE_C)
3344const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003345{
3346 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003347 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003348
Hanno Beckere6824572019-02-07 13:18:46 +00003349#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003350 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003351#else
3352 return( NULL );
3353#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003358int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3359 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003360{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003361 int ret;
3362
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003363 if( ssl == NULL ||
3364 dst == NULL ||
3365 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003366 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003369 }
3370
Hanno Beckere810bbc2021-05-14 16:01:05 +01003371 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3372 * idempotent: Each session can only be exported once.
3373 *
3374 * (This is in preparation for TLS 1.3 support where we will
3375 * need the ability to export multiple sessions (aka tickets),
3376 * which will be achieved by calling mbedtls_ssl_get_session()
3377 * multiple times until it fails.)
3378 *
3379 * Check whether we have already exported the current session,
3380 * and fail if so.
3381 */
3382 if( ssl->session->exported == 1 )
3383 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3384
3385 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3386 if( ret != 0 )
3387 return( ret );
3388
3389 /* Remember that we've exported the session. */
3390 ssl->session->exported = 1;
3391 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003394
Paul Bakker5121ce52009-01-03 21:22:43 +00003395/*
Hanno Beckera835da52019-05-16 12:39:07 +01003396 * Define ticket header determining Mbed TLS version
3397 * and structure of the ticket.
3398 */
3399
Hanno Becker94ef3b32019-05-16 12:50:45 +01003400/*
Hanno Becker50b59662019-05-28 14:30:45 +01003401 * Define bitflag determining compile-time settings influencing
3402 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003403 */
3404
Hanno Becker50b59662019-05-28 14:30:45 +01003405#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003406#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003407#else
Hanno Becker3e088662019-05-29 11:10:18 +01003408#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003409#endif /* MBEDTLS_HAVE_TIME */
3410
3411#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003412#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003413#else
Hanno Becker3e088662019-05-29 11:10:18 +01003414#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003415#endif /* MBEDTLS_X509_CRT_PARSE_C */
3416
3417#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003418#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003419#else
Hanno Becker3e088662019-05-29 11:10:18 +01003420#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003421#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3422
3423#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003424#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003425#else
Hanno Becker3e088662019-05-29 11:10:18 +01003426#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003427#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3428
Hanno Becker94ef3b32019-05-16 12:50:45 +01003429#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003430#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003431#else
Hanno Becker3e088662019-05-29 11:10:18 +01003432#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003433#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3434
Hanno Becker94ef3b32019-05-16 12:50:45 +01003435#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3436#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3437#else
3438#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3439#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3440
Hanno Becker3e088662019-05-29 11:10:18 +01003441#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3442#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3443#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3444#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003445#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3446#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003447
Hanno Becker50b59662019-05-28 14:30:45 +01003448#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003449 ( (uint16_t) ( \
3450 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3451 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3452 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3453 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003454 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003455 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003456
Hanno Beckerf8787072019-05-16 12:41:07 +01003457static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003458 MBEDTLS_VERSION_MAJOR,
3459 MBEDTLS_VERSION_MINOR,
3460 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003461 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3462 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003463};
Hanno Beckera835da52019-05-16 12:39:07 +01003464
3465/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003466 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003467 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003468 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003469 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003470 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003471 * opaque mbedtls_version[3]; // library version: major, minor, patch
3472 * opaque session_format[2]; // library-version specific 16-bit field
3473 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003474 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003475 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003476 * Note: When updating the format, remember to keep
3477 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003478 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003479 * // In this version, `session_format` determines
3480 * // the setting of those compile-time
3481 * // configuration options which influence
3482 * // the structure of mbedtls_ssl_session.
3483 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003484 * uint8_t minor_ver; // Protocol minor version. Possible values:
3485 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003486 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003487 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003488 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003489 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003490 * serialized_session_tls12 data;
Jerry Yuddda0502022-12-01 19:43:12 +08003491 * case MBEDTLS_SSL_MINOR_VERSION_4:
3492 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003493 *
3494 * };
3495 *
3496 * } serialized_session;
3497 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003498 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003499
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003500MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003501static int ssl_session_save( const mbedtls_ssl_session *session,
3502 unsigned char omit_header,
3503 unsigned char *buf,
3504 size_t buf_len,
3505 size_t *olen )
3506{
3507 unsigned char *p = buf;
3508 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003509 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003510#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3511 size_t out_len;
3512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3513#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003514 if( session == NULL )
3515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3516
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003517 if( !omit_header )
3518 {
3519 /*
3520 * Add Mbed TLS version identifier
3521 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003522 used += sizeof( ssl_serialized_session_header );
3523
3524 if( used <= buf_len )
3525 {
3526 memcpy( p, ssl_serialized_session_header,
3527 sizeof( ssl_serialized_session_header ) );
3528 p += sizeof( ssl_serialized_session_header );
3529 }
3530 }
3531
3532 /*
3533 * TLS version identifier
3534 */
3535 used += 1;
3536 if( used <= buf_len )
3537 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003538 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003539 }
3540
3541 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003542 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003543 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003544 {
3545#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003546 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003547 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003548 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003549#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3550
Jerry Yu251a12e2022-07-13 15:15:48 +08003551#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3552 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003553 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3554 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003555 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003556 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003557 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003558#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3559
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003560 default:
3561 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3562 }
3563
3564 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003565 if( used > buf_len )
3566 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003567
3568 return( 0 );
3569}
3570
3571/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003572 * Public wrapper for ssl_session_save()
3573 */
3574int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3575 unsigned char *buf,
3576 size_t buf_len,
3577 size_t *olen )
3578{
3579 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3580}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003581
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003582/*
3583 * Deserialize session, see mbedtls_ssl_session_save() for format.
3584 *
3585 * This internal version is wrapped by a public function that cleans up in
3586 * case of error, and has an extra option omit_header.
3587 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003588MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003589static int ssl_session_load( mbedtls_ssl_session *session,
3590 unsigned char omit_header,
3591 const unsigned char *buf,
3592 size_t len )
3593{
3594 const unsigned char *p = buf;
3595 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003596 size_t remaining_len;
3597
3598
3599 if( session == NULL )
3600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003601
3602 if( !omit_header )
3603 {
3604 /*
3605 * Check Mbed TLS version identifier
3606 */
3607
3608 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3610
3611 if( memcmp( p, ssl_serialized_session_header,
3612 sizeof( ssl_serialized_session_header ) ) != 0 )
3613 {
3614 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3615 }
3616 p += sizeof( ssl_serialized_session_header );
3617 }
3618
3619 /*
3620 * TLS version identifier
3621 */
3622 if( 1 > (size_t)( end - p ) )
3623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003624 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003625
3626 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003627 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003628 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629 {
3630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003631 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003632 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003633#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3634
Jerry Yu438ddd82022-07-07 06:55:50 +00003635#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3636 case MBEDTLS_SSL_VERSION_TLS1_3:
3637 return( ssl_tls13_session_load( session, p, remaining_len ) );
3638#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3639
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003640 default:
3641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3642 }
3643}
3644
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003645/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003646 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003647 */
3648int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3649 const unsigned char *buf,
3650 size_t len )
3651{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003652 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003653
3654 if( ret != 0 )
3655 mbedtls_ssl_session_free( session );
3656
3657 return( ret );
3658}
3659
3660/*
Paul Bakker1961b702013-01-25 14:49:24 +01003661 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003663MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003664static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3665{
3666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3667
Ronald Cron66dbf912022-02-02 15:33:46 +01003668 /*
3669 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003670 * that were written into the output buffer by the previous handshake step,
3671 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003672 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3673 * We proceed to the next handshake step only when all data from the
3674 * previous one have been sent to the peer, thus we make sure that this is
3675 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3676 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3677 * we have to wait before to go ahead.
3678 * In the case of TLS 1.3, handshake step handlers do not send data to the
3679 * peer. Data are only sent here and through
3680 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003681 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003682 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003683 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3684 return( ret );
3685
3686#if defined(MBEDTLS_SSL_PROTO_DTLS)
3687 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3688 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3689 {
3690 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3691 return( ret );
3692 }
3693#endif /* MBEDTLS_SSL_PROTO_DTLS */
3694
3695 return( ret );
3696}
3697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003699{
Hanno Becker41934dd2021-08-07 19:13:43 +01003700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Hanno Becker41934dd2021-08-07 19:13:43 +01003702 if( ssl == NULL ||
3703 ssl->conf == NULL ||
3704 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003705 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003706 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003708 }
3709
3710 ret = ssl_prepare_handshake_step( ssl );
3711 if( ret != 0 )
3712 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003713
Jerry Yue7047812021-09-13 19:26:39 +08003714 ret = mbedtls_ssl_handle_pending_alert( ssl );
3715 if( ret != 0 )
3716 goto cleanup;
3717
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003718 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3719 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3720 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003723 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003724 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3726 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003727
Ronald Cron9f0fba32022-02-10 16:45:15 +01003728 switch( ssl->state )
3729 {
3730 case MBEDTLS_SSL_HELLO_REQUEST:
3731 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003732 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003733 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003734
Ronald Cron9f0fba32022-02-10 16:45:15 +01003735 case MBEDTLS_SSL_CLIENT_HELLO:
3736 ret = mbedtls_ssl_write_client_hello( ssl );
3737 break;
3738
3739 default:
3740#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003741 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003742 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3743 else
3744 ret = mbedtls_ssl_handshake_client_step( ssl );
3745#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3746 ret = mbedtls_ssl_handshake_client_step( ssl );
3747#else
3748 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3749#endif
3750 }
Jerry Yub9930e72021-08-06 17:11:51 +08003751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003754 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003755 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003756#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003757 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003758 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003759#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003760
3761#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3762 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3763 ret = mbedtls_ssl_handshake_server_step( ssl );
3764#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3765 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003766#endif
3767
Jerry Yue7047812021-09-13 19:26:39 +08003768 if( ret != 0 )
3769 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003770 /* handshake_step return error. And it is same
3771 * with alert_reason.
3772 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003773 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003774 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003775 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003776 goto cleanup;
3777 }
3778 }
3779
3780cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003781 return( ret );
3782}
3783
3784/*
3785 * Perform the SSL handshake
3786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003788{
3789 int ret = 0;
3790
Hanno Beckera817ea42020-10-20 15:20:23 +01003791 /* Sanity checks */
3792
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003793 if( ssl == NULL || ssl->conf == NULL )
3794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3795
Hanno Beckera817ea42020-10-20 15:20:23 +01003796#if defined(MBEDTLS_SSL_PROTO_DTLS)
3797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3798 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3799 {
3800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3801 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3803 }
3804#endif /* MBEDTLS_SSL_PROTO_DTLS */
3805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003807
Hanno Beckera817ea42020-10-20 15:20:23 +01003808 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003809 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003812
3813 if( ret != 0 )
3814 break;
3815 }
3816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003818
3819 return( ret );
3820}
3821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#if defined(MBEDTLS_SSL_RENEGOTIATION)
3823#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003824/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003825 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003826 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003827MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003829{
Janos Follath865b3eb2019-12-16 11:46:15 +00003830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003831
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 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3836 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003837
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003838 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003839 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003841 return( ret );
3842 }
3843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003845
3846 return( 0 );
3847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003849
3850/*
3851 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 * - any side: calling mbedtls_ssl_renegotiate(),
3853 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3854 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003855 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003856 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003858 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003859int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003860{
Janos Follath865b3eb2019-12-16 11:46:15 +00003861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003864
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003865 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3866 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003867
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003868 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3869 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003871 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003873 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003874 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003875 ssl->handshake->out_msg_seq = 1;
3876 else
3877 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003878 }
3879#endif
3880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3882 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003887 return( ret );
3888 }
3889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003891
3892 return( 0 );
3893}
3894
3895/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003896 * Renegotiate current connection on client,
3897 * or request renegotiation on server
3898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003902
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003903 if( ssl == NULL || ssl->conf == NULL )
3904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003907 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003908 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003909 {
Jerry Yu6848a612022-10-27 13:03:26 +08003910 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003914
3915 /* Did we already try/start sending HelloRequest? */
3916 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003918
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003919 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003920 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003924 /*
3925 * On client, either start the renegotiation process or,
3926 * if already in progress, continue the handshake
3927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003929 {
Jerry Yu6848a612022-10-27 13:03:26 +08003930 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003932
Hanno Becker40cdaa12020-02-05 10:48:27 +00003933 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003934 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003936 return( ret );
3937 }
3938 }
3939 else
3940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003944 return( ret );
3945 }
3946 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003948
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003949 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003950}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003952
Gilles Peskine9b562d52018-04-25 20:32:43 +02003953void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003954{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003955 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3956
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003957 if( handshake == NULL )
3958 return;
3959
Brett Warrene0edc842021-08-17 09:53:13 +01003960#if defined(MBEDTLS_ECP_C)
3961#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3962 if ( ssl->handshake->group_list_heap_allocated )
3963 mbedtls_free( (void*) handshake->group_list );
3964 handshake->group_list = NULL;
3965#endif /* MBEDTLS_DEPRECATED_REMOVED */
3966#endif /* MBEDTLS_ECP_C */
3967
Ronald Crone68ab4f2022-10-05 12:46:29 +02003968#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003969#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3970 if ( ssl->handshake->sig_algs_heap_allocated )
3971 mbedtls_free( (void*) handshake->sig_algs );
3972 handshake->sig_algs = NULL;
3973#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003974#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3975 if( ssl->handshake->certificate_request_context )
3976 {
3977 mbedtls_free( (void*) handshake->certificate_request_context );
3978 }
3979#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003980#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003981
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003982#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3983 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3984 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003985 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003986 handshake->async_in_progress = 0;
3987 }
3988#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3989
Andrzej Kurek25f27152022-08-17 16:09:31 -04003990#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003991#if defined(MBEDTLS_USE_PSA_CRYPTO)
3992 psa_hash_abort( &handshake->fin_sha256_psa );
3993#else
Jerry Yu2e199812022-12-01 18:57:19 +08003994 mbedtls_sha256_free( &handshake->fin_sha256 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003995#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003996#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003997#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003998#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003999 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05004000#else
Andrzej Kureka242e832022-08-11 10:03:14 -04004001 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004002#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004003#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005#if defined(MBEDTLS_DHM_C)
4006 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00004007#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004008#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02004010#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004011
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004012#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004013#if defined(MBEDTLS_USE_PSA_CRYPTO)
4014 psa_pake_abort( &handshake->psa_pake_ctx );
4015 psa_destroy_key( handshake->psa_pake_password );
4016 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4017#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004018 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004019#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004020#if defined(MBEDTLS_SSL_CLI_C)
4021 mbedtls_free( handshake->ecjpake_cache );
4022 handshake->ecjpake_cache = NULL;
4023 handshake->ecjpake_cache_len = 0;
4024#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004025#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004026
Janos Follath4ae5c292016-02-10 11:27:43 +00004027#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4028 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004029 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004031#endif
4032
Ronald Cron73fe8df2022-10-05 14:31:43 +02004033#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004034#if defined(MBEDTLS_USE_PSA_CRYPTO)
4035 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4036 {
4037 /* The maintenance of the external PSK key slot is the
4038 * user's responsibility. */
4039 if( ssl->handshake->psk_opaque_is_internal )
4040 {
4041 psa_destroy_key( ssl->handshake->psk_opaque );
4042 ssl->handshake->psk_opaque_is_internal = 0;
4043 }
4044 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4045 }
Neil Armstronge952a302022-05-03 10:22:14 +02004046#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004047 if( handshake->psk != NULL )
4048 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004049 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004050 mbedtls_free( handshake->psk );
4051 }
Neil Armstronge952a302022-05-03 10:22:14 +02004052#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004053#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4056 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004057 /*
4058 * Free only the linked list wrapper, not the keys themselves
4059 * since the belong to the SNI callback
4060 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004061 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004063
Gilles Peskineeccd8882020-03-10 12:19:08 +01004064#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004065 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004066 if( handshake->ecrs_peer_cert != NULL )
4067 {
4068 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4069 mbedtls_free( handshake->ecrs_peer_cert );
4070 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004071#endif
4072
Hanno Becker75173122019-02-06 16:18:31 +00004073#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4074 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4075 mbedtls_pk_free( &handshake->peer_pubkey );
4076#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4077
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004078#if defined(MBEDTLS_SSL_CLI_C) && \
4079 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4080 mbedtls_free( handshake->cookie );
4081#endif /* MBEDTLS_SSL_CLI_C &&
4082 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004083
4084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004085 mbedtls_ssl_flight_free( handshake->flight );
4086 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004087#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004088
Ronald Cronf12b81d2022-03-15 10:42:41 +01004089#if defined(MBEDTLS_ECDH_C) && \
4090 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004091 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004092 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004093#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4094
Ronald Cron6f135e12021-12-08 16:57:54 +01004095#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004096 mbedtls_ssl_transform_free( handshake->transform_handshake );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004097 mbedtls_free( handshake->transform_handshake );
4098#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yua1a568c2021-11-09 10:17:21 +08004099 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004100 mbedtls_free( handshake->transform_earlydata );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004101#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004102#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004103
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004104
4105#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4106 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4107 * processes datagrams and the fact that a datagram is allowed to have
4108 * several records in it, it is possible that the I/O buffers are not
4109 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004110 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4111 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004112#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004113
Jerry Yuba9c7272021-10-30 11:54:10 +08004114 /* mbedtls_platform_zeroize MUST be last one in this function */
4115 mbedtls_platform_zeroize( handshake,
4116 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004117}
4118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004120{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004121 if( session == NULL )
4122 return;
4123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004125 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004126#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004127
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004128#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004129#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4130 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004131 mbedtls_free( session->hostname );
4132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004134#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004135
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004136 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004137}
4138
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004139#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004140
4141#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4142#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4143#else
4144#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4145#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4146
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004147#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004148
4149#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4150#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4151#else
4152#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4153#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4154
4155#if defined(MBEDTLS_SSL_ALPN)
4156#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4157#else
4158#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4159#endif /* MBEDTLS_SSL_ALPN */
4160
4161#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4162#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4163#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4164#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4165
4166#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4167 ( (uint32_t) ( \
4168 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4169 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4170 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4171 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4172 0u ) )
4173
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004174static unsigned char ssl_serialized_context_header[] = {
4175 MBEDTLS_VERSION_MAJOR,
4176 MBEDTLS_VERSION_MINOR,
4177 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004178 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4179 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4180 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4181 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4182 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004183};
4184
Paul Bakker5121ce52009-01-03 21:22:43 +00004185/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004186 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004187 *
4188 * The format of the serialized data is:
4189 * (in the presentation language of TLS, RFC 8446 section 3)
4190 *
4191 * // header
4192 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004193 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004194 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004195 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004196 * Note: When updating the format, remember to keep these
4197 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004198 *
4199 * // session sub-structure
4200 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4201 * // transform sub-structure
4202 * uint8 random[64]; // ServerHello.random+ClientHello.random
4203 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4204 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4205 * // fields from ssl_context
4206 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4207 * uint64 in_window_top; // DTLS: last validated record seq_num
4208 * uint64 in_window; // DTLS: bitmask for replay protection
4209 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4210 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4211 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4212 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4213 *
4214 * Note that many fields of the ssl_context or sub-structures are not
4215 * serialized, as they fall in one of the following categories:
4216 *
4217 * 1. forced value (eg in_left must be 0)
4218 * 2. pointer to dynamically-allocated memory (eg session, transform)
4219 * 3. value can be re-derived from other data (eg session keys from MS)
4220 * 4. value was temporary (eg content of input buffer)
4221 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004222 */
4223int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4224 unsigned char *buf,
4225 size_t buf_len,
4226 size_t *olen )
4227{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004228 unsigned char *p = buf;
4229 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004230 size_t session_len;
4231 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004232
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004233 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004234 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4235 * this function's documentation.
4236 *
4237 * These are due to assumptions/limitations in the implementation. Some of
4238 * them are likely to stay (no handshake in progress) some might go away
4239 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004240 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004241 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004242 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004243 {
4244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004246 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004247 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004248 {
4249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
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 /* Double-check that sub-structures are indeed ready */
4253 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004254 {
4255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004257 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004258 /* There must be no pending incoming or outgoing data */
4259 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004260 {
4261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004263 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004264 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004265 {
4266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
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 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004269 /* Protocol must be DTLS, not TLS */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004270 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004271 {
4272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is 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 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004276 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004277 {
4278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004280 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004281 /* We must be using an AEAD ciphersuite */
4282 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004283 {
4284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004286 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004287 /* Renegotiation must not be enabled */
4288#if defined(MBEDTLS_SSL_RENEGOTIATION)
4289 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004290 {
4291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004293 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004294#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004295
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004296 /*
4297 * Version and format identifier
4298 */
4299 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004300
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004301 if( used <= buf_len )
4302 {
4303 memcpy( p, ssl_serialized_context_header,
4304 sizeof( ssl_serialized_context_header ) );
4305 p += sizeof( ssl_serialized_context_header );
4306 }
4307
4308 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004309 * Session (length + data)
4310 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004311 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004312 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4313 return( ret );
4314
4315 used += 4 + session_len;
4316 if( used <= buf_len )
4317 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004318 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4319 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004320
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004321 ret = ssl_session_save( ssl->session, 1,
4322 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004323 if( ret != 0 )
4324 return( ret );
4325
4326 p += session_len;
4327 }
4328
4329 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004330 * Transform
4331 */
4332 used += sizeof( ssl->transform->randbytes );
4333 if( used <= buf_len )
4334 {
4335 memcpy( p, ssl->transform->randbytes,
4336 sizeof( ssl->transform->randbytes ) );
4337 p += sizeof( ssl->transform->randbytes );
4338 }
4339
4340#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4341 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4342 if( used <= buf_len )
4343 {
4344 *p++ = ssl->transform->in_cid_len;
4345 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4346 p += ssl->transform->in_cid_len;
4347
4348 *p++ = ssl->transform->out_cid_len;
4349 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4350 p += ssl->transform->out_cid_len;
4351 }
4352#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4353
4354 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004355 * Saved fields from top-level ssl_context structure
4356 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004357 used += 4;
4358 if( used <= buf_len )
4359 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004360 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4361 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004362 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004363
4364#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4365 used += 16;
4366 if( used <= buf_len )
4367 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004368 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4369 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004370
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004371 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4372 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004373 }
4374#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4375
4376#if defined(MBEDTLS_SSL_PROTO_DTLS)
4377 used += 1;
4378 if( used <= buf_len )
4379 {
4380 *p++ = ssl->disable_datagram_packing;
4381 }
4382#endif /* MBEDTLS_SSL_PROTO_DTLS */
4383
Jerry Yuae0b2e22021-10-08 15:21:19 +08004384 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004385 if( used <= buf_len )
4386 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004387 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4388 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004389 }
4390
4391#if defined(MBEDTLS_SSL_PROTO_DTLS)
4392 used += 2;
4393 if( used <= buf_len )
4394 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004395 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4396 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004397 }
4398#endif /* MBEDTLS_SSL_PROTO_DTLS */
4399
4400#if defined(MBEDTLS_SSL_ALPN)
4401 {
4402 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004403 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004404 : 0;
4405
4406 used += 1 + alpn_len;
4407 if( used <= buf_len )
4408 {
4409 *p++ = alpn_len;
4410
4411 if( ssl->alpn_chosen != NULL )
4412 {
4413 memcpy( p, ssl->alpn_chosen, alpn_len );
4414 p += alpn_len;
4415 }
4416 }
4417 }
4418#endif /* MBEDTLS_SSL_ALPN */
4419
4420 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004421 * Done
4422 */
4423 *olen = used;
4424
4425 if( used > buf_len )
4426 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004427
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004428 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4429
Hanno Becker43aefe22020-02-05 10:44:56 +00004430 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004431}
4432
4433/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004434 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004435 *
4436 * This internal version is wrapped by a public function that cleans up in
4437 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004438 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004439MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004440static int ssl_context_load( mbedtls_ssl_context *ssl,
4441 const unsigned char *buf,
4442 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004443{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004444 const unsigned char *p = buf;
4445 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004446 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004448#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004449 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004450#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004451
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004452 /*
4453 * The context should have been freshly setup or reset.
4454 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004455 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004456 * renegotiating, or if the user mistakenly loaded a session first.)
4457 */
4458 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4459 ssl->session != NULL )
4460 {
4461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4462 }
4463
4464 /*
4465 * We can't check that the config matches the initial one, but we can at
4466 * least check it matches the requirements for serializing.
4467 */
4468 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004469 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4470 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004471#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004472 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004473#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004474 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004475 {
4476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4477 }
4478
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004479 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4480
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004481 /*
4482 * Check version identifier
4483 */
4484 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4486
4487 if( memcmp( p, ssl_serialized_context_header,
4488 sizeof( ssl_serialized_context_header ) ) != 0 )
4489 {
4490 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4491 }
4492 p += sizeof( ssl_serialized_context_header );
4493
4494 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004495 * Session
4496 */
4497 if( (size_t)( end - p ) < 4 )
4498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4499
4500 session_len = ( (size_t) p[0] << 24 ) |
4501 ( (size_t) p[1] << 16 ) |
4502 ( (size_t) p[2] << 8 ) |
4503 ( (size_t) p[3] );
4504 p += 4;
4505
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004506 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004507 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004508 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004509 ssl->session_in = ssl->session;
4510 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004511 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004512
4513 if( (size_t)( end - p ) < session_len )
4514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4515
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004516 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004517 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004518 {
4519 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004520 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004521 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004522
4523 p += session_len;
4524
4525 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004526 * Transform
4527 */
4528
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004529 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004530 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004531#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuddda0502022-12-01 19:43:12 +08004532 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004533 ssl->transform_in = ssl->transform;
4534 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004535 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004536#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004537
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004539 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4540 if( prf_func == NULL )
4541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4542
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004543 /* Read random bytes and populate structure */
4544 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004546
Hanno Beckerbd257552021-03-22 06:59:27 +00004547 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004548 ssl->session->ciphersuite,
4549 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004550#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004551 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004552#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004553 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004554 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004555 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004556 ssl->conf->endpoint,
4557 ssl );
4558 if( ret != 0 )
4559 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004560#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004561 p += sizeof( ssl->transform->randbytes );
4562
4563#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4564 /* Read connection IDs and store them */
4565 if( (size_t)( end - p ) < 1 )
4566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4567
4568 ssl->transform->in_cid_len = *p++;
4569
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004570 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4572
4573 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4574 p += ssl->transform->in_cid_len;
4575
4576 ssl->transform->out_cid_len = *p++;
4577
4578 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4580
4581 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4582 p += ssl->transform->out_cid_len;
4583#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4584
4585 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004586 * Saved fields from top-level ssl_context structure
4587 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004588 if( (size_t)( end - p ) < 4 )
4589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4590
4591 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4592 ( (uint32_t) p[1] << 16 ) |
4593 ( (uint32_t) p[2] << 8 ) |
4594 ( (uint32_t) p[3] );
4595 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004596
4597#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4598 if( (size_t)( end - p ) < 16 )
4599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4600
4601 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4602 ( (uint64_t) p[1] << 48 ) |
4603 ( (uint64_t) p[2] << 40 ) |
4604 ( (uint64_t) p[3] << 32 ) |
4605 ( (uint64_t) p[4] << 24 ) |
4606 ( (uint64_t) p[5] << 16 ) |
4607 ( (uint64_t) p[6] << 8 ) |
4608 ( (uint64_t) p[7] );
4609 p += 8;
4610
4611 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4612 ( (uint64_t) p[1] << 48 ) |
4613 ( (uint64_t) p[2] << 40 ) |
4614 ( (uint64_t) p[3] << 32 ) |
4615 ( (uint64_t) p[4] << 24 ) |
4616 ( (uint64_t) p[5] << 16 ) |
4617 ( (uint64_t) p[6] << 8 ) |
4618 ( (uint64_t) p[7] );
4619 p += 8;
4620#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4621
4622#if defined(MBEDTLS_SSL_PROTO_DTLS)
4623 if( (size_t)( end - p ) < 1 )
4624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4625
4626 ssl->disable_datagram_packing = *p++;
4627#endif /* MBEDTLS_SSL_PROTO_DTLS */
4628
Jerry Yud9a94fe2021-09-28 18:58:59 +08004629 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004631 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4632 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004633
4634#if defined(MBEDTLS_SSL_PROTO_DTLS)
4635 if( (size_t)( end - p ) < 2 )
4636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4637
4638 ssl->mtu = ( p[0] << 8 ) | p[1];
4639 p += 2;
4640#endif /* MBEDTLS_SSL_PROTO_DTLS */
4641
4642#if defined(MBEDTLS_SSL_ALPN)
4643 {
4644 uint8_t alpn_len;
4645 const char **cur;
4646
4647 if( (size_t)( end - p ) < 1 )
4648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4649
4650 alpn_len = *p++;
4651
4652 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4653 {
4654 /* alpn_chosen should point to an item in the configured list */
4655 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4656 {
4657 if( strlen( *cur ) == alpn_len &&
4658 memcmp( p, cur, alpn_len ) == 0 )
4659 {
4660 ssl->alpn_chosen = *cur;
4661 break;
4662 }
4663 }
4664 }
4665
4666 /* can only happen on conf mismatch */
4667 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4669
4670 p += alpn_len;
4671 }
4672#endif /* MBEDTLS_SSL_ALPN */
4673
4674 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004675 * Forced fields from top-level ssl_context structure
4676 *
4677 * Most of them already set to the correct value by mbedtls_ssl_init() and
4678 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4679 */
4680 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004681 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004682
Hanno Becker361b10d2019-08-30 10:42:49 +01004683 /* Adjust pointers for header fields of outgoing records to
4684 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004685 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004686
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004687#if defined(MBEDTLS_SSL_PROTO_DTLS)
4688 ssl->in_epoch = 1;
4689#endif
4690
4691 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4692 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004693 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4694 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004695 if( ssl->handshake != NULL )
4696 {
4697 mbedtls_ssl_handshake_free( ssl );
4698 mbedtls_free( ssl->handshake );
4699 ssl->handshake = NULL;
4700 }
4701
4702 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004703 * Done - should have consumed entire buffer
4704 */
4705 if( p != end )
4706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004707
4708 return( 0 );
4709}
4710
4711/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004712 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004713 */
4714int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4715 const unsigned char *buf,
4716 size_t len )
4717{
4718 int ret = ssl_context_load( context, buf, len );
4719
4720 if( ret != 0 )
4721 mbedtls_ssl_free( context );
4722
4723 return( ret );
4724}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004725#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004726
4727/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004728 * Free an SSL context
4729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004731{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004732 if( ssl == NULL )
4733 return;
4734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004736
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004737 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004738 {
sander-visserb8aa2072020-05-06 22:05:13 +02004739#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4740 size_t out_buf_len = ssl->out_buf_len;
4741#else
4742 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4743#endif
4744
Darryl Greenb33cc762019-11-28 14:29:44 +00004745 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004747 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004748 }
4749
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004750 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004751 {
sander-visserb8aa2072020-05-06 22:05:13 +02004752#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4753 size_t in_buf_len = ssl->in_buf_len;
4754#else
4755 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4756#endif
4757
Darryl Greenb33cc762019-11-28 14:29:44 +00004758 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004760 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 }
4762
Paul Bakker48916f92012-09-16 19:57:18 +00004763 if( ssl->transform )
4764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 mbedtls_ssl_transform_free( ssl->transform );
4766 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004767 }
4768
Jerry Yu2e199812022-12-01 18:57:19 +08004769
Paul Bakker48916f92012-09-16 19:57:18 +00004770 if( ssl->handshake )
4771 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004772 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 mbedtls_free( ssl->handshake );
Jerry Yu2e199812022-12-01 18:57:19 +08004774
4775#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4776 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 mbedtls_free( ssl->transform_negotiate );
Jerry Yu2e199812022-12-01 18:57:19 +08004778#endif
4779
4780 mbedtls_ssl_session_free( ssl->session_negotiate );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004782 }
4783
Ronald Cron6f135e12021-12-08 16:57:54 +01004784#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004785 mbedtls_ssl_transform_free( ssl->transform_application );
4786 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004787#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004788
Paul Bakkerc0463502013-02-14 11:19:38 +01004789 if( ssl->session )
4790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 mbedtls_ssl_session_free( ssl->session );
4792 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004793 }
4794
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004795#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004796 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004798 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004800 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004801#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004802
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004803#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004805#endif
4806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004808
Paul Bakker86f04f42013-02-14 11:20:09 +01004809 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004810 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811}
4812
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004813/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004814 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004815 */
4816void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4817{
4818 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4819}
4820
Gilles Peskineae270bf2021-06-02 00:05:29 +02004821/* The selection should be the same as mbedtls_x509_crt_profile_default in
4822 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004823 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004824 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004825 * about this list.
4826 */
Brett Warrene0edc842021-08-17 09:53:13 +01004827static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004828#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004829 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004830#endif
4831#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004832 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004833#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004834#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004835 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004836#endif
4837#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004838 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004839#endif
4840#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004841 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004842#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004843#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004844 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004845#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004846#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004847 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004848#endif
4849#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004850 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004851#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004852 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004853};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004854
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004855static int ssl_preset_suiteb_ciphersuites[] = {
4856 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4857 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4858 0
4859};
4860
Ronald Crone68ab4f2022-10-05 12:46:29 +02004861#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004862
Jerry Yu909df7b2022-01-22 11:56:27 +08004863/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004864 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004865 * rules SHOULD be upheld.
4866 * - No duplicate entries.
4867 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004868 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004869 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004870 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004871static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004872
Andrzej Kurek25f27152022-08-17 16:09:31 -04004873#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 +08004874 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4875 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004876#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004877 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004878
Andrzej Kurek25f27152022-08-17 16:09:31 -04004879#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 +08004880 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4881 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004882#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004883 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4884
Andrzej Kurek25f27152022-08-17 16:09:31 -04004885#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 +08004886 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4887 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004888#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004889 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004890
Andrzej Kurek25f27152022-08-17 16:09:31 -04004891#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 +08004892 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004893#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 +08004894
Andrzej Kurek25f27152022-08-17 16:09:31 -04004895#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 +08004896 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004897#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 +08004898
Andrzej Kurek25f27152022-08-17 16:09:31 -04004899#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 +08004900 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004901#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 +08004902
Andrzej Kurek25f27152022-08-17 16:09:31 -04004903#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 +08004904 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4905#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4906
Andrzej Kurek25f27152022-08-17 16:09:31 -04004907#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 +08004908 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4909#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4910
Andrzej Kurek25f27152022-08-17 16:09:31 -04004911#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 +08004912 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4913#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4914
Gabor Mezei15b95a62022-05-09 16:37:58 +02004915 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004916};
4917
4918/* NOTICE: see above */
4919#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4920static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004921#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004922#if defined(MBEDTLS_ECDSA_C)
4923 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004924#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004925#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4926 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4927#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004928#if defined(MBEDTLS_RSA_C)
4929 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4930#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004931#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004932#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004933#if defined(MBEDTLS_ECDSA_C)
4934 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004935#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004936#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4937 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4938#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004939#if defined(MBEDTLS_RSA_C)
4940 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4941#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004942#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004943#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004944#if defined(MBEDTLS_ECDSA_C)
4945 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004946#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004947#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4948 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4949#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004950#if defined(MBEDTLS_RSA_C)
4951 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4952#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004953#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004954 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004955};
4956#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4957/* NOTICE: see above */
4958static uint16_t ssl_preset_suiteb_sig_algs[] = {
4959
Andrzej Kurek25f27152022-08-17 16:09:31 -04004960#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 +08004961 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4962 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004963#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004964 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4965
Andrzej Kurek25f27152022-08-17 16:09:31 -04004966#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 +08004967 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4968 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004969#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004970 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004971
Andrzej Kurek25f27152022-08-17 16:09:31 -04004972#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 +08004973 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004974#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 +08004975
Andrzej Kurek25f27152022-08-17 16:09:31 -04004976#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 +08004977 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004978#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004979
Gabor Mezei15b95a62022-05-09 16:37:58 +02004980 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004981};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004982
Jerry Yu909df7b2022-01-22 11:56:27 +08004983/* NOTICE: see above */
4984#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4985static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004986#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004987#if defined(MBEDTLS_ECDSA_C)
4988 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004989#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004990#if defined(MBEDTLS_RSA_C)
4991 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4992#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004993#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004994#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004995#if defined(MBEDTLS_ECDSA_C)
4996 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004997#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004998#if defined(MBEDTLS_RSA_C)
4999 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
5000#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005001#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005002 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005003};
Jerry Yu909df7b2022-01-22 11:56:27 +08005004#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5005
Ronald Crone68ab4f2022-10-05 12:46:29 +02005006#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005007
Brett Warrene0edc842021-08-17 09:53:13 +01005008static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005009#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005010 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005011#endif
5012#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005013 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005014#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005015 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005016};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005017
Ronald Crone68ab4f2022-10-05 12:46:29 +02005018#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005019/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005020 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005021MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005022static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005023{
5024 size_t i, j;
5025 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005026
Gabor Mezei15b95a62022-05-09 16:37:58 +02005027 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005028 {
Jerry Yuf377d642022-01-25 10:43:59 +08005029 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005030 {
Jerry Yuf377d642022-01-25 10:43:59 +08005031 if( sig_algs[i] != sig_algs[j] )
5032 continue;
5033 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5034 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5035 sig_algs[i], j, i );
5036 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005037 }
5038 }
5039 return( ret );
5040}
5041
Ronald Crone68ab4f2022-10-05 12:46:29 +02005042#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005043
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005044/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005045 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005046 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005047int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005048 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005049{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005050#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005052#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005053
Ronald Crone68ab4f2022-10-05 12:46:29 +02005054#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005055 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005056 {
5057 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5058 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5059 }
5060
Jerry Yuf377d642022-01-25 10:43:59 +08005061 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005062 {
5063 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5064 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5065 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005066
5067#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005068 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005069 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005070 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005071 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5072 }
5073
Jerry Yuf377d642022-01-25 10:43:59 +08005074 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005075 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005076 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005077 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5078 }
5079#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005080#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005081
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005082 /* Use the functions here so that they are covered in tests,
5083 * but otherwise access member directly for efficiency */
5084 mbedtls_ssl_conf_endpoint( conf, endpoint );
5085 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005086
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005087 /*
5088 * Things that are common to all presets
5089 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005090#if defined(MBEDTLS_SSL_CLI_C)
5091 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5092 {
5093 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5094#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5095 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5096#endif
5097 }
5098#endif
5099
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005100#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5101 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5102#endif
5103
5104#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5105 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5106#endif
5107
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005108#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005109 conf->f_cookie_write = ssl_cookie_write_dummy;
5110 conf->f_cookie_check = ssl_cookie_check_dummy;
5111#endif
5112
5113#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5114 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5115#endif
5116
Janos Follath088ce432017-04-10 12:42:31 +01005117#if defined(MBEDTLS_SSL_SRV_C)
5118 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005119 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005120#endif
5121
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005122#if defined(MBEDTLS_SSL_PROTO_DTLS)
5123 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5124 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5125#endif
5126
5127#if defined(MBEDTLS_SSL_RENEGOTIATION)
5128 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005129 memset( conf->renego_period, 0x00, 2 );
5130 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005131#endif
5132
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005133#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005134 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5135 {
5136 const unsigned char dhm_p[] =
5137 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5138 const unsigned char dhm_g[] =
5139 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005140
Hanno Beckere2defad2021-07-24 05:59:17 +01005141 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5142 dhm_p, sizeof( dhm_p ),
5143 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5144 {
5145 return( ret );
5146 }
5147 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005148#endif
5149
Ronald Cron6f135e12021-12-08 16:57:54 +01005150#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005151
5152#if defined(MBEDTLS_SSL_EARLY_DATA)
5153 mbedtls_ssl_tls13_conf_early_data( conf, MBEDTLS_SSL_EARLY_DATA_DISABLED );
5154#if defined(MBEDTLS_SSL_SRV_C)
5155 mbedtls_ssl_tls13_conf_max_early_data_size(
5156 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE );
5157#endif
5158#endif /* MBEDTLS_SSL_EARLY_DATA */
5159
Jerry Yud0766ec2022-09-22 10:46:57 +08005160#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005161 mbedtls_ssl_conf_new_session_tickets(
5162 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5163#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005164 /*
5165 * Allow all TLS 1.3 key exchange modes by default.
5166 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005167 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005168#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005169
XiaokangQian4d3a6042022-04-21 13:46:17 +00005170 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5171 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005172#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005173 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005174 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005175#else
XiaokangQian060d8672022-04-21 09:24:56 +00005176 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005177#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005178 }
5179 else
5180 {
5181#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5182 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5183 {
5184 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5185 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5186 }
5187 else
5188 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5189 {
5190 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5191 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5192 }
5193#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5194 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5195 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5196#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5197 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5198 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5199#else
5200 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5201#endif
5202 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005203
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005204 /*
5205 * Preset-specific defaults
5206 */
5207 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005208 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005209 /*
5210 * NSA Suite B
5211 */
5212 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005213
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005214 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005215
5216#if defined(MBEDTLS_X509_CRT_PARSE_C)
5217 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005218#endif
5219
Ronald Crone68ab4f2022-10-05 12:46:29 +02005220#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005221#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5222 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5223 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5224 else
5225#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5226 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005227#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005228
Brett Warrene0edc842021-08-17 09:53:13 +01005229#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5230 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005231#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005232 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005233 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005234
5235 /*
5236 * Default
5237 */
5238 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005239
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005240 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005241
5242#if defined(MBEDTLS_X509_CRT_PARSE_C)
5243 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5244#endif
5245
Ronald Crone68ab4f2022-10-05 12:46:29 +02005246#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005247#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5248 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5249 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5250 else
5251#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5252 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005253#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005254
Brett Warrene0edc842021-08-17 09:53:13 +01005255#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5256 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005257#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005258 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005259
5260#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5261 conf->dhm_min_bitlen = 1024;
5262#endif
5263 }
5264
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005265 return( 0 );
5266}
5267
5268/*
5269 * Free mbedtls_ssl_config
5270 */
5271void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5272{
5273#if defined(MBEDTLS_DHM_C)
5274 mbedtls_mpi_free( &conf->dhm_P );
5275 mbedtls_mpi_free( &conf->dhm_G );
5276#endif
5277
Ronald Cron73fe8df2022-10-05 14:31:43 +02005278#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005279#if defined(MBEDTLS_USE_PSA_CRYPTO)
5280 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5281 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005282 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5283 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005284#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005285 if( conf->psk != NULL )
5286 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005287 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005288 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005289 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005290 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005291 }
5292
5293 if( conf->psk_identity != NULL )
5294 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005295 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005296 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005297 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005298 conf->psk_identity_len = 0;
5299 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005300#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005301
5302#if defined(MBEDTLS_X509_CRT_PARSE_C)
5303 ssl_key_cert_free( conf->key_cert );
5304#endif
5305
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005306 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005307}
5308
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005309#if defined(MBEDTLS_PK_C) && \
5310 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005311/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316#if defined(MBEDTLS_RSA_C)
5317 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5318 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005319#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320#if defined(MBEDTLS_ECDSA_C)
5321 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5322 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005323#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005325}
5326
Hanno Becker7e5437a2017-04-28 17:15:26 +01005327unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5328{
5329 switch( type ) {
5330 case MBEDTLS_PK_RSA:
5331 return( MBEDTLS_SSL_SIG_RSA );
5332 case MBEDTLS_PK_ECDSA:
5333 case MBEDTLS_PK_ECKEY:
5334 return( MBEDTLS_SSL_SIG_ECDSA );
5335 default:
5336 return( MBEDTLS_SSL_SIG_ANON );
5337 }
5338}
5339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005341{
5342 switch( sig )
5343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344#if defined(MBEDTLS_RSA_C)
5345 case MBEDTLS_SSL_SIG_RSA:
5346 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005347#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348#if defined(MBEDTLS_ECDSA_C)
5349 case MBEDTLS_SSL_SIG_ECDSA:
5350 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005351#endif
5352 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005354 }
5355}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005356#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005357
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005358/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005359 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005362{
5363 switch( hash )
5364 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005365#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 case MBEDTLS_SSL_HASH_MD5:
5367 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005368#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005369#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 case MBEDTLS_SSL_HASH_SHA1:
5371 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005372#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005373#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 case MBEDTLS_SSL_HASH_SHA224:
5375 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005376#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005377#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 case MBEDTLS_SSL_HASH_SHA256:
5379 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005380#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005381#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 case MBEDTLS_SSL_HASH_SHA384:
5383 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005384#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005385#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 case MBEDTLS_SSL_HASH_SHA512:
5387 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005388#endif
5389 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005391 }
5392}
5393
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005394/*
5395 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5396 */
5397unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5398{
5399 switch( md )
5400 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005401#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005402 case MBEDTLS_MD_MD5:
5403 return( MBEDTLS_SSL_HASH_MD5 );
5404#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005405#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005406 case MBEDTLS_MD_SHA1:
5407 return( MBEDTLS_SSL_HASH_SHA1 );
5408#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005409#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005410 case MBEDTLS_MD_SHA224:
5411 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005412#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005413#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005414 case MBEDTLS_MD_SHA256:
5415 return( MBEDTLS_SSL_HASH_SHA256 );
5416#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005417#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005418 case MBEDTLS_MD_SHA384:
5419 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005420#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005421#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005422 case MBEDTLS_MD_SHA512:
5423 return( MBEDTLS_SSL_HASH_SHA512 );
5424#endif
5425 default:
5426 return( MBEDTLS_SSL_HASH_NONE );
5427 }
5428}
5429
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005430/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005431 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005432 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005433 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005434int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005435{
Brett Warrene0edc842021-08-17 09:53:13 +01005436 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005437
Brett Warrene0edc842021-08-17 09:53:13 +01005438 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005439 return( -1 );
5440
Brett Warrene0edc842021-08-17 09:53:13 +01005441 for( ; *group_list != 0; group_list++ )
5442 {
5443 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005444 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005445 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005446
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005447 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005448}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005449
5450#if defined(MBEDTLS_ECP_C)
5451/*
5452 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5453 */
5454int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5455{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005456 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005457 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005458
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005459 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005460 return -1;
5461
5462 uint16_t tls_id = grp_info->tls_id;
5463
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005464 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5465}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005466#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#if defined(MBEDTLS_X509_CRT_PARSE_C)
5469int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5470 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005471 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005472 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005473{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005474 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005475 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005476 const char *ext_oid;
5477 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005480 {
5481 /* Server part of the key exchange */
5482 switch( ciphersuite->key_exchange )
5483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 case MBEDTLS_KEY_EXCHANGE_RSA:
5485 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005486 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005487 break;
5488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5490 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5491 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5492 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005493 break;
5494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5496 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005497 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005498 break;
5499
5500 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 case MBEDTLS_KEY_EXCHANGE_NONE:
5502 case MBEDTLS_KEY_EXCHANGE_PSK:
5503 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5504 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005505 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005506 usage = 0;
5507 }
5508 }
5509 else
5510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5512 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005513 }
5514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005516 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005517 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005518 ret = -1;
5519 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5524 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005525 }
5526 else
5527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5529 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005530 }
5531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005533 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005534 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005535 ret = -1;
5536 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005537
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005538 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005541
Jerry Yu148165c2021-09-24 23:20:59 +08005542#if defined(MBEDTLS_USE_PSA_CRYPTO)
5543int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5544 const mbedtls_md_type_t md,
5545 unsigned char *dst,
5546 size_t dst_len,
5547 size_t *olen )
5548{
Ronald Cronf6893e12022-01-07 22:09:01 +01005549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5550 psa_hash_operation_t *hash_operation_to_clone;
5551 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5552
Jerry Yu148165c2021-09-24 23:20:59 +08005553 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005554
5555 switch( md )
5556 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005557#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005558 case MBEDTLS_MD_SHA384:
5559 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5560 break;
5561#endif
5562
Andrzej Kurek25f27152022-08-17 16:09:31 -04005563#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005564 case MBEDTLS_MD_SHA256:
5565 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5566 break;
5567#endif
5568
5569 default:
5570 goto exit;
5571 }
5572
5573 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5574 if( status != PSA_SUCCESS )
5575 goto exit;
5576
5577 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5578 if( status != PSA_SUCCESS )
5579 goto exit;
5580
5581exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005582#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5583 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5584 (void) ssl;
5585#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005586 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005587}
5588#else /* MBEDTLS_USE_PSA_CRYPTO */
5589
Andrzej Kurek25f27152022-08-17 16:09:31 -04005590#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005591MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005592static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5593 unsigned char *dst,
5594 size_t dst_len,
5595 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005596{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005597 int ret;
5598 mbedtls_sha512_context sha512;
5599
5600 if( dst_len < 48 )
5601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5602
5603 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005604 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005605
5606 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5607 {
5608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5609 goto exit;
5610 }
5611
5612 *olen = 48;
5613
5614exit:
5615
5616 mbedtls_sha512_free( &sha512 );
5617 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005618}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005619#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005620
Andrzej Kurek25f27152022-08-17 16:09:31 -04005621#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005622MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005623static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5624 unsigned char *dst,
5625 size_t dst_len,
5626 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005627{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005628 int ret;
5629 mbedtls_sha256_context sha256;
5630
5631 if( dst_len < 32 )
5632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5633
5634 mbedtls_sha256_init( &sha256 );
5635 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005636
Jerry Yu24c0ec32021-09-09 14:21:07 +08005637 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5638 {
5639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5640 goto exit;
5641 }
5642
5643 *olen = 32;
5644
5645exit:
5646
5647 mbedtls_sha256_free( &sha256 );
5648 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005649}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005650#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005651
Jerry Yu000f9762021-09-14 11:12:51 +08005652int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5653 const mbedtls_md_type_t md,
5654 unsigned char *dst,
5655 size_t dst_len,
5656 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005657{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005658 switch( md )
5659 {
5660
Andrzej Kurek25f27152022-08-17 16:09:31 -04005661#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005662 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005663 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005664#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005665
Andrzej Kurek25f27152022-08-17 16:09:31 -04005666#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005667 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005668 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005669#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005670
5671 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005672#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5673 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5674 (void) ssl;
5675 (void) dst;
5676 (void) dst_len;
5677 (void) olen;
5678#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005679 break;
5680 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005682}
XiaokangQian647719a2021-12-07 09:16:29 +00005683
Jerry Yu148165c2021-09-24 23:20:59 +08005684#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005685
Ronald Crone68ab4f2022-10-05 12:46:29 +02005686#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005687/* mbedtls_ssl_parse_sig_alg_ext()
5688 *
5689 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5690 * value (TLS 1.3 RFC8446):
5691 * enum {
5692 * ....
5693 * ecdsa_secp256r1_sha256( 0x0403 ),
5694 * ecdsa_secp384r1_sha384( 0x0503 ),
5695 * ecdsa_secp521r1_sha512( 0x0603 ),
5696 * ....
5697 * } SignatureScheme;
5698 *
5699 * struct {
5700 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5701 * } SignatureSchemeList;
5702 *
5703 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5704 * value (TLS 1.2 RFC5246):
5705 * enum {
5706 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5707 * sha512(6), (255)
5708 * } HashAlgorithm;
5709 *
5710 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5711 * SignatureAlgorithm;
5712 *
5713 * struct {
5714 * HashAlgorithm hash;
5715 * SignatureAlgorithm signature;
5716 * } SignatureAndHashAlgorithm;
5717 *
5718 * SignatureAndHashAlgorithm
5719 * supported_signature_algorithms<2..2^16-2>;
5720 *
5721 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5722 * generalization of the TLS 1.2 signature algorithm extension.
5723 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5724 * `SignatureScheme` field of TLS 1.3
5725 *
5726 */
5727int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5728 const unsigned char *buf,
5729 const unsigned char *end )
5730{
5731 const unsigned char *p = buf;
5732 size_t supported_sig_algs_len = 0;
5733 const unsigned char *supported_sig_algs_end;
5734 uint16_t sig_alg;
5735 uint32_t common_idx = 0;
5736
5737 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5738 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5739 p += 2;
5740
5741 memset( ssl->handshake->received_sig_algs, 0,
5742 sizeof(ssl->handshake->received_sig_algs) );
5743
5744 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5745 supported_sig_algs_end = p + supported_sig_algs_len;
5746 while( p < supported_sig_algs_end )
5747 {
5748 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5749 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5750 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005751 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5752 sig_alg,
5753 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005754#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005755 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005756 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5757 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5758 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005759 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005760 }
5761#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005762
Jerry Yuf3b46b52022-06-19 16:52:27 +08005763 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5764 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005765
5766 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5767 {
5768 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5769 common_idx += 1;
5770 }
5771 }
5772 /* Check that we consumed all the message. */
5773 if( p != end )
5774 {
5775 MBEDTLS_SSL_DEBUG_MSG( 1,
5776 ( "Signature algorithms extension length misaligned" ) );
5777 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5778 MBEDTLS_ERR_SSL_DECODE_ERROR );
5779 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5780 }
5781
5782 if( common_idx == 0 )
5783 {
5784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5785 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5786 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5787 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5788 }
5789
Gabor Mezei15b95a62022-05-09 16:37:58 +02005790 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005791 return( 0 );
5792}
5793
Ronald Crone68ab4f2022-10-05 12:46:29 +02005794#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005795
Jerry Yudc7bd172022-02-17 13:44:15 +08005796#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5797
5798#if defined(MBEDTLS_USE_PSA_CRYPTO)
5799
5800static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5801 mbedtls_svc_key_id_t key,
5802 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005803 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005804 const unsigned char* seed, size_t seed_length,
5805 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005806 const unsigned char* other_secret,
5807 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005808 size_t capacity )
5809{
5810 psa_status_t status;
5811
5812 status = psa_key_derivation_setup( derivation, alg );
5813 if( status != PSA_SUCCESS )
5814 return( status );
5815
5816 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5817 {
5818 status = psa_key_derivation_input_bytes( derivation,
5819 PSA_KEY_DERIVATION_INPUT_SEED,
5820 seed, seed_length );
5821 if( status != PSA_SUCCESS )
5822 return( status );
5823
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005824 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005825 {
5826 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005827 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5828 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005829 if( status != PSA_SUCCESS )
5830 return( status );
5831 }
5832
Jerry Yudc7bd172022-02-17 13:44:15 +08005833 if( mbedtls_svc_key_id_is_null( key ) )
5834 {
5835 status = psa_key_derivation_input_bytes(
5836 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005837 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005838 }
5839 else
5840 {
5841 status = psa_key_derivation_input_key(
5842 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5843 }
5844 if( status != PSA_SUCCESS )
5845 return( status );
5846
5847 status = psa_key_derivation_input_bytes( derivation,
5848 PSA_KEY_DERIVATION_INPUT_LABEL,
5849 label, label_length );
5850 if( status != PSA_SUCCESS )
5851 return( status );
5852 }
5853 else
5854 {
5855 return( PSA_ERROR_NOT_SUPPORTED );
5856 }
5857
5858 status = psa_key_derivation_set_capacity( derivation, capacity );
5859 if( status != PSA_SUCCESS )
5860 return( status );
5861
5862 return( PSA_SUCCESS );
5863}
5864
Andrzej Kurek57d10632022-10-24 10:32:01 -04005865#if defined(PSA_WANT_ALG_SHA_384) || \
5866 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005867MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005868static int tls_prf_generic( mbedtls_md_type_t md_type,
5869 const unsigned char *secret, size_t slen,
5870 const char *label,
5871 const unsigned char *random, size_t rlen,
5872 unsigned char *dstbuf, size_t dlen )
5873{
5874 psa_status_t status;
5875 psa_algorithm_t alg;
5876 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5877 psa_key_derivation_operation_t derivation =
5878 PSA_KEY_DERIVATION_OPERATION_INIT;
5879
5880 if( md_type == MBEDTLS_MD_SHA384 )
5881 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5882 else
5883 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5884
5885 /* Normally a "secret" should be long enough to be impossible to
5886 * find by brute force, and in particular should not be empty. But
5887 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5888 * and for this use case it makes sense to have a 0-length "secret".
5889 * Since the key API doesn't allow importing a key of length 0,
5890 * keep master_key=0, which setup_psa_key_derivation() understands
5891 * to mean a 0-length "secret" input. */
5892 if( slen != 0 )
5893 {
5894 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5895 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5896 psa_set_key_algorithm( &key_attributes, alg );
5897 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5898
5899 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5900 if( status != PSA_SUCCESS )
5901 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5902 }
5903
5904 status = setup_psa_key_derivation( &derivation,
5905 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005906 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005907 random, rlen,
5908 (unsigned char const *) label,
5909 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005910 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005911 dlen );
5912 if( status != PSA_SUCCESS )
5913 {
5914 psa_key_derivation_abort( &derivation );
5915 psa_destroy_key( master_key );
5916 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5917 }
5918
5919 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5920 if( status != PSA_SUCCESS )
5921 {
5922 psa_key_derivation_abort( &derivation );
5923 psa_destroy_key( master_key );
5924 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5925 }
5926
5927 status = psa_key_derivation_abort( &derivation );
5928 if( status != PSA_SUCCESS )
5929 {
5930 psa_destroy_key( master_key );
5931 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5932 }
5933
5934 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5935 status = psa_destroy_key( master_key );
5936 if( status != PSA_SUCCESS )
5937 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5938
5939 return( 0 );
5940}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005941#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005942#else /* MBEDTLS_USE_PSA_CRYPTO */
5943
Andrzej Kurek57d10632022-10-24 10:32:01 -04005944#if defined(MBEDTLS_MD_C) && \
5945 ( defined(MBEDTLS_SHA256_C) || \
5946 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005947MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005948static int tls_prf_generic( mbedtls_md_type_t md_type,
5949 const unsigned char *secret, size_t slen,
5950 const char *label,
5951 const unsigned char *random, size_t rlen,
5952 unsigned char *dstbuf, size_t dlen )
5953{
5954 size_t nb;
5955 size_t i, j, k, md_len;
5956 unsigned char *tmp;
5957 size_t tmp_len = 0;
5958 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5959 const mbedtls_md_info_t *md_info;
5960 mbedtls_md_context_t md_ctx;
5961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5962
5963 mbedtls_md_init( &md_ctx );
5964
5965 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5966 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5967
5968 md_len = mbedtls_md_get_size( md_info );
5969
5970 tmp_len = md_len + strlen( label ) + rlen;
5971 tmp = mbedtls_calloc( 1, tmp_len );
5972 if( tmp == NULL )
5973 {
5974 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5975 goto exit;
5976 }
5977
5978 nb = strlen( label );
5979 memcpy( tmp + md_len, label, nb );
5980 memcpy( tmp + md_len + nb, random, rlen );
5981 nb += rlen;
5982
5983 /*
5984 * Compute P_<hash>(secret, label + random)[0..dlen]
5985 */
5986 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5987 goto exit;
5988
5989 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5990 if( ret != 0 )
5991 goto exit;
5992 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5993 if( ret != 0 )
5994 goto exit;
5995 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5996 if( ret != 0 )
5997 goto exit;
5998
5999 for( i = 0; i < dlen; i += md_len )
6000 {
6001 ret = mbedtls_md_hmac_reset ( &md_ctx );
6002 if( ret != 0 )
6003 goto exit;
6004 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
6005 if( ret != 0 )
6006 goto exit;
6007 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
6008 if( ret != 0 )
6009 goto exit;
6010
6011 ret = mbedtls_md_hmac_reset ( &md_ctx );
6012 if( ret != 0 )
6013 goto exit;
6014 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
6015 if( ret != 0 )
6016 goto exit;
6017 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6018 if( ret != 0 )
6019 goto exit;
6020
6021 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
6022
6023 for( j = 0; j < k; j++ )
6024 dstbuf[i + j] = h_i[j];
6025 }
6026
6027exit:
6028 mbedtls_md_free( &md_ctx );
6029
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006030 if ( tmp != NULL )
6031 mbedtls_platform_zeroize( tmp, tmp_len );
6032
Jerry Yudc7bd172022-02-17 13:44:15 +08006033 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6034
6035 mbedtls_free( tmp );
6036
6037 return( ret );
6038}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006039#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006040#endif /* MBEDTLS_USE_PSA_CRYPTO */
6041
Andrzej Kurek25f27152022-08-17 16:09:31 -04006042#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006043MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006044static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6045 const char *label,
6046 const unsigned char *random, size_t rlen,
6047 unsigned char *dstbuf, size_t dlen )
6048{
6049 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6050 label, random, rlen, dstbuf, dlen ) );
6051}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006052#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006053
Andrzej Kurek25f27152022-08-17 16:09:31 -04006054#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006055MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006056static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6057 const char *label,
6058 const unsigned char *random, size_t rlen,
6059 unsigned char *dstbuf, size_t dlen )
6060{
6061 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6062 label, random, rlen, dstbuf, dlen ) );
6063}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006064#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006065
Jerry Yuf009d862022-02-17 14:01:37 +08006066/*
6067 * Set appropriate PRF function and other SSL / TLS1.2 functions
6068 *
6069 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006070 * - hash associated with the ciphersuite (only used by TLS 1.2)
6071 *
6072 * Outputs:
6073 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6074 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006075MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006076static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006077 mbedtls_md_type_t hash )
6078{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006079#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006080 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006081 {
6082 handshake->tls_prf = tls_prf_sha384;
6083 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6084 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6085 }
6086 else
6087#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006088#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006089 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006090 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006091 handshake->tls_prf = tls_prf_sha256;
6092 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6093 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6094 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006095#else
Jerry Yuf009d862022-02-17 14:01:37 +08006096 {
Jerry Yuf009d862022-02-17 14:01:37 +08006097 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006098 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6100 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006101#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006102
6103 return( 0 );
6104}
Jerry Yud6ab2352022-02-17 14:03:43 +08006105
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006106/*
6107 * Compute master secret if needed
6108 *
6109 * Parameters:
6110 * [in/out] handshake
6111 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6112 * (PSA-PSK) ciphersuite_info, psk_opaque
6113 * [out] premaster (cleared)
6114 * [out] master
6115 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6116 * debug: conf->f_dbg, conf->p_dbg
6117 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006118 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006119 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006120MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006121static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6122 unsigned char *master,
6123 const mbedtls_ssl_context *ssl )
6124{
6125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6126
6127 /* cf. RFC 5246, Section 8.1:
6128 * "The master secret is always exactly 48 bytes in length." */
6129 size_t const master_secret_len = 48;
6130
6131#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6132 unsigned char session_hash[48];
6133#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6134
6135 /* The label for the KDF used for key expansion.
6136 * This is either "master secret" or "extended master secret"
6137 * depending on whether the Extended Master Secret extension
6138 * is used. */
6139 char const *lbl = "master secret";
6140
Przemek Stekielae4ed302022-04-05 17:15:55 +02006141 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006142 * - If the Extended Master Secret extension is not used,
6143 * this is ClientHello.Random + ServerHello.Random
6144 * (see Sect. 8.1 in RFC 5246).
6145 * - If the Extended Master Secret extension is used,
6146 * this is the transcript of the handshake so far.
6147 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006148 unsigned char const *seed = handshake->randbytes;
6149 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006150
6151#if !defined(MBEDTLS_DEBUG_C) && \
6152 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6153 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6154 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6155 ssl = NULL; /* make sure we don't use it except for those cases */
6156 (void) ssl;
6157#endif
6158
6159 if( handshake->resume != 0 )
6160 {
6161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6162 return( 0 );
6163 }
6164
6165#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6166 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6167 {
6168 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006169 seed = session_hash;
6170 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006171
6172 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006173 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006174 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006175#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006176
Przemek Stekiel99114f32022-04-22 11:20:09 +02006177#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006178 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006179 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006180 {
6181 /* Perform PSK-to-MS expansion in a single step. */
6182 psa_status_t status;
6183 psa_algorithm_t alg;
6184 mbedtls_svc_key_id_t psk;
6185 psa_key_derivation_operation_t derivation =
6186 PSA_KEY_DERIVATION_OPERATION_INIT;
6187 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6188
6189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6190
6191 psk = mbedtls_ssl_get_opaque_psk( ssl );
6192
6193 if( hash_alg == MBEDTLS_MD_SHA384 )
6194 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6195 else
6196 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6197
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006198 size_t other_secret_len = 0;
6199 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006200
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006201 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006202 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006203 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006204 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006205 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006206 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006207 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006208 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006209 other_secret_len = 48;
6210 other_secret = handshake->premaster + 2;
6211 break;
6212 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006213 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006214 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6215 other_secret = handshake->premaster + 2;
6216 break;
6217 default:
6218 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006219 }
6220
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006221 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006222 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006223 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006224 (unsigned char const *) lbl,
6225 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006226 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006227 master_secret_len );
6228 if( status != PSA_SUCCESS )
6229 {
6230 psa_key_derivation_abort( &derivation );
6231 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6232 }
6233
6234 status = psa_key_derivation_output_bytes( &derivation,
6235 master,
6236 master_secret_len );
6237 if( status != PSA_SUCCESS )
6238 {
6239 psa_key_derivation_abort( &derivation );
6240 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6241 }
6242
6243 status = psa_key_derivation_abort( &derivation );
6244 if( status != PSA_SUCCESS )
6245 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6246 }
6247 else
6248#endif
6249 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006250#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6251 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6252 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6253 {
6254 psa_status_t status;
6255 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6256 psa_key_derivation_operation_t derivation =
6257 PSA_KEY_DERIVATION_OPERATION_INIT;
6258
6259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6260
6261 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6262
6263 status = psa_key_derivation_setup( &derivation, alg );
6264 if( status != PSA_SUCCESS )
6265 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6266
6267 status = psa_key_derivation_set_capacity( &derivation,
6268 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6269 if( status != PSA_SUCCESS )
6270 {
6271 psa_key_derivation_abort( &derivation );
6272 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6273 }
6274
6275 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6276 &derivation );
6277 if( status != PSA_SUCCESS )
6278 {
6279 psa_key_derivation_abort( &derivation );
6280 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6281 }
6282
6283 status = psa_key_derivation_output_bytes( &derivation,
6284 handshake->premaster,
6285 handshake->pmslen );
6286 if( status != PSA_SUCCESS )
6287 {
6288 psa_key_derivation_abort( &derivation );
6289 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6290 }
6291
6292 status = psa_key_derivation_abort( &derivation );
6293 if( status != PSA_SUCCESS )
6294 {
6295 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6296 }
6297 }
6298#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006299 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006300 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006301 master,
6302 master_secret_len );
6303 if( ret != 0 )
6304 {
6305 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6306 return( ret );
6307 }
6308
6309 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6310 handshake->premaster,
6311 handshake->pmslen );
6312
6313 mbedtls_platform_zeroize( handshake->premaster,
6314 sizeof(handshake->premaster) );
6315 }
6316
6317 return( 0 );
6318}
6319
Jerry Yud62f87e2022-02-17 14:09:02 +08006320int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6321{
6322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6323 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6324 ssl->handshake->ciphersuite_info;
6325
6326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6327
6328 /* Set PRF, calc_verify and calc_finished function pointers */
6329 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006330 ciphersuite_info->mac );
6331 if( ret != 0 )
6332 {
6333 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6334 return( ret );
6335 }
6336
6337 /* Compute master secret if needed */
6338 ret = ssl_compute_master( ssl->handshake,
6339 ssl->session_negotiate->master,
6340 ssl );
6341 if( ret != 0 )
6342 {
6343 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6344 return( ret );
6345 }
6346
6347 /* Swap the client and server random values:
6348 * - MS derivation wanted client+server (RFC 5246 8.1)
6349 * - key derivation wants server+client (RFC 5246 6.3) */
6350 {
6351 unsigned char tmp[64];
6352 memcpy( tmp, ssl->handshake->randbytes, 64 );
6353 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6354 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6355 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6356 }
6357
6358 /* Populate transform structure */
6359 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6360 ssl->session_negotiate->ciphersuite,
6361 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006362#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006363 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006364#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006365 ssl->handshake->tls_prf,
6366 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006367 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006368 ssl->conf->endpoint,
6369 ssl );
6370 if( ret != 0 )
6371 {
6372 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6373 return( ret );
6374 }
6375
6376 /* We no longer need Server/ClientHello.random values */
6377 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6378 sizeof( ssl->handshake->randbytes ) );
6379
6380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6381
6382 return( 0 );
6383}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006384
Ronald Cron4dcbca92022-03-07 10:21:40 +01006385int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6386{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006387 switch( md )
6388 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006389#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006390 case MBEDTLS_SSL_HASH_SHA384:
6391 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6392 break;
6393#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006394#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006395 case MBEDTLS_SSL_HASH_SHA256:
6396 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6397 break;
6398#endif
6399 default:
6400 return( -1 );
6401 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006402#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6403 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6404 (void) ssl;
6405#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006406 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006407}
6408
Andrzej Kurek25f27152022-08-17 16:09:31 -04006409#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006410void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6411 unsigned char *hash,
6412 size_t *hlen )
6413{
6414#if defined(MBEDTLS_USE_PSA_CRYPTO)
6415 size_t hash_size;
6416 psa_status_t status;
6417 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6418
6419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6420 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6421 if( status != PSA_SUCCESS )
6422 {
6423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6424 return;
6425 }
6426
6427 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6428 if( status != PSA_SUCCESS )
6429 {
6430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6431 return;
6432 }
6433
6434 *hlen = 32;
6435 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6437#else
6438 mbedtls_sha256_context sha256;
6439
6440 mbedtls_sha256_init( &sha256 );
6441
6442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6443
6444 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6445 mbedtls_sha256_finish( &sha256, hash );
6446
6447 *hlen = 32;
6448
6449 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6451
6452 mbedtls_sha256_free( &sha256 );
6453#endif /* MBEDTLS_USE_PSA_CRYPTO */
6454 return;
6455}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006456#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006457
Andrzej Kurek25f27152022-08-17 16:09:31 -04006458#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006459void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6460 unsigned char *hash,
6461 size_t *hlen )
6462{
6463#if defined(MBEDTLS_USE_PSA_CRYPTO)
6464 size_t hash_size;
6465 psa_status_t status;
6466 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6467
6468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6469 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6470 if( status != PSA_SUCCESS )
6471 {
6472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6473 return;
6474 }
6475
6476 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6477 if( status != PSA_SUCCESS )
6478 {
6479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6480 return;
6481 }
6482
6483 *hlen = 48;
6484 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6486#else
6487 mbedtls_sha512_context sha512;
6488
6489 mbedtls_sha512_init( &sha512 );
6490
6491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6492
Andrzej Kureka242e832022-08-11 10:03:14 -04006493 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006494 mbedtls_sha512_finish( &sha512, hash );
6495
6496 *hlen = 48;
6497
6498 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6500
6501 mbedtls_sha512_free( &sha512 );
6502#endif /* MBEDTLS_USE_PSA_CRYPTO */
6503 return;
6504}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006505#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006506
Neil Armstrong80f6f322022-05-03 17:56:38 +02006507#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6508 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006509int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6510{
6511 unsigned char *p = ssl->handshake->premaster;
6512 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6513 const unsigned char *psk = NULL;
6514 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006515 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006516
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006517 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006518 {
6519 /*
6520 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006521 * checked before calling this function.
6522 *
6523 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006524 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006525 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006526 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6527 {
6528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6530 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006531 }
6532
6533 /*
6534 * PMS = struct {
6535 * opaque other_secret<0..2^16-1>;
6536 * opaque psk<0..2^16-1>;
6537 * };
6538 * with "other_secret" depending on the particular key exchange
6539 */
6540#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6541 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6542 {
6543 if( end - p < 2 )
6544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6545
6546 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6547 p += 2;
6548
6549 if( end < p || (size_t)( end - p ) < psk_len )
6550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6551
6552 memset( p, 0, psk_len );
6553 p += psk_len;
6554 }
6555 else
6556#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6557#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6558 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6559 {
6560 /*
6561 * other_secret already set by the ClientKeyExchange message,
6562 * and is 48 bytes long
6563 */
6564 if( end - p < 2 )
6565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6566
6567 *p++ = 0;
6568 *p++ = 48;
6569 p += 48;
6570 }
6571 else
6572#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6573#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6574 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6575 {
6576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6577 size_t len;
6578
6579 /* Write length only when we know the actual value */
6580 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6581 p + 2, end - ( p + 2 ), &len,
6582 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6583 {
6584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6585 return( ret );
6586 }
6587 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6588 p += 2 + len;
6589
6590 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6591 }
6592 else
6593#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006594#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006595 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6596 {
6597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6598 size_t zlen;
6599
6600 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6601 p + 2, end - ( p + 2 ),
6602 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6603 {
6604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6605 return( ret );
6606 }
6607
6608 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6609 p += 2 + zlen;
6610
6611 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6612 MBEDTLS_DEBUG_ECDH_Z );
6613 }
6614 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006615#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006616 {
6617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6619 }
6620
6621 /* opaque psk<0..2^16-1>; */
6622 if( end - p < 2 )
6623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6624
6625 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6626 p += 2;
6627
6628 if( end < p || (size_t)( end - p ) < psk_len )
6629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6630
6631 memcpy( p, psk, psk_len );
6632 p += psk_len;
6633
6634 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6635
6636 return( 0 );
6637}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006638#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006639
6640#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006641MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006642static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6643
6644#if defined(MBEDTLS_SSL_PROTO_DTLS)
6645int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6646{
6647 /* If renegotiation is not enforced, retransmit until we would reach max
6648 * timeout if we were using the usual handshake doubling scheme */
6649 if( ssl->conf->renego_max_records < 0 )
6650 {
6651 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6652 unsigned char doublings = 1;
6653
6654 while( ratio != 0 )
6655 {
6656 ++doublings;
6657 ratio >>= 1;
6658 }
6659
6660 if( ++ssl->renego_records_seen > doublings )
6661 {
6662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6663 return( 0 );
6664 }
6665 }
6666
6667 return( ssl_write_hello_request( ssl ) );
6668}
6669#endif
6670#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006671
Jerry Yud9526692022-02-17 14:23:47 +08006672/*
6673 * Handshake functions
6674 */
6675#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6676/* No certificate support -> dummy functions */
6677int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6678{
6679 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6680 ssl->handshake->ciphersuite_info;
6681
6682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6683
6684 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6685 {
6686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6687 ssl->state++;
6688 return( 0 );
6689 }
6690
6691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6693}
6694
6695int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6696{
6697 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6698 ssl->handshake->ciphersuite_info;
6699
6700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6701
6702 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6703 {
6704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6705 ssl->state++;
6706 return( 0 );
6707 }
6708
6709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6711}
6712
6713#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6714/* Some certificate support -> implement write and parse */
6715
6716int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6717{
6718 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6719 size_t i, n;
6720 const mbedtls_x509_crt *crt;
6721 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6722 ssl->handshake->ciphersuite_info;
6723
6724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6725
6726 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6727 {
6728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6729 ssl->state++;
6730 return( 0 );
6731 }
6732
6733#if defined(MBEDTLS_SSL_CLI_C)
6734 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6735 {
6736 if( ssl->handshake->client_auth == 0 )
6737 {
6738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6739 ssl->state++;
6740 return( 0 );
6741 }
6742 }
6743#endif /* MBEDTLS_SSL_CLI_C */
6744#if defined(MBEDTLS_SSL_SRV_C)
6745 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6746 {
6747 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6748 {
6749 /* Should never happen because we shouldn't have picked the
6750 * ciphersuite if we don't have a certificate. */
6751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6752 }
6753 }
6754#endif
6755
6756 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6757
6758 /*
6759 * 0 . 0 handshake type
6760 * 1 . 3 handshake length
6761 * 4 . 6 length of all certs
6762 * 7 . 9 length of cert. 1
6763 * 10 . n-1 peer certificate
6764 * n . n+2 length of cert. 2
6765 * n+3 . ... upper level cert, etc.
6766 */
6767 i = 7;
6768 crt = mbedtls_ssl_own_cert( ssl );
6769
6770 while( crt != NULL )
6771 {
6772 n = crt->raw.len;
6773 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6774 {
6775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6776 " > %" MBEDTLS_PRINTF_SIZET,
6777 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6778 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6779 }
6780
6781 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6782 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6783 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6784
6785 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6786 i += n; crt = crt->next;
6787 }
6788
6789 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6790 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6791 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6792
6793 ssl->out_msglen = i;
6794 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6795 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6796
6797 ssl->state++;
6798
6799 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6800 {
6801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6802 return( ret );
6803 }
6804
6805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6806
6807 return( ret );
6808}
6809
6810#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6811
6812#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006813MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006814static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6815 unsigned char *crt_buf,
6816 size_t crt_buf_len )
6817{
6818 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6819
6820 if( peer_crt == NULL )
6821 return( -1 );
6822
6823 if( peer_crt->raw.len != crt_buf_len )
6824 return( -1 );
6825
6826 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6827}
6828#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006829MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006830static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6831 unsigned char *crt_buf,
6832 size_t crt_buf_len )
6833{
6834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6835 unsigned char const * const peer_cert_digest =
6836 ssl->session->peer_cert_digest;
6837 mbedtls_md_type_t const peer_cert_digest_type =
6838 ssl->session->peer_cert_digest_type;
6839 mbedtls_md_info_t const * const digest_info =
6840 mbedtls_md_info_from_type( peer_cert_digest_type );
6841 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6842 size_t digest_len;
6843
6844 if( peer_cert_digest == NULL || digest_info == NULL )
6845 return( -1 );
6846
6847 digest_len = mbedtls_md_get_size( digest_info );
6848 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6849 return( -1 );
6850
6851 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6852 if( ret != 0 )
6853 return( -1 );
6854
6855 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6856}
6857#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6858#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6859
6860/*
6861 * Once the certificate message is read, parse it into a cert chain and
6862 * perform basic checks, but leave actual verification to the caller
6863 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006864MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006865static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6866 mbedtls_x509_crt *chain )
6867{
6868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6869#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6870 int crt_cnt=0;
6871#endif
6872 size_t i, n;
6873 uint8_t alert;
6874
6875 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6876 {
6877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6879 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6880 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6881 }
6882
6883 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6884 {
6885 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6886 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6887 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6888 }
6889
6890 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6891 {
6892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6894 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6895 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6896 }
6897
6898 i = mbedtls_ssl_hs_hdr_len( ssl );
6899
6900 /*
6901 * Same message structure as in mbedtls_ssl_write_certificate()
6902 */
6903 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6904
6905 if( ssl->in_msg[i] != 0 ||
6906 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6907 {
6908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6909 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6910 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6911 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6912 }
6913
6914 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6915 i += 3;
6916
6917 /* Iterate through and parse the CRTs in the provided chain. */
6918 while( i < ssl->in_hslen )
6919 {
6920 /* Check that there's room for the next CRT's length fields. */
6921 if ( i + 3 > ssl->in_hslen ) {
6922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6923 mbedtls_ssl_send_alert_message( ssl,
6924 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6925 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6926 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6927 }
6928 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6929 * anything beyond 2**16 ~ 64K. */
6930 if( ssl->in_msg[i] != 0 )
6931 {
6932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6933 mbedtls_ssl_send_alert_message( ssl,
6934 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6935 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6936 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6937 }
6938
6939 /* Read length of the next CRT in the chain. */
6940 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6941 | (unsigned int) ssl->in_msg[i + 2];
6942 i += 3;
6943
6944 if( n < 128 || i + n > ssl->in_hslen )
6945 {
6946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6947 mbedtls_ssl_send_alert_message( ssl,
6948 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6949 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6950 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6951 }
6952
6953 /* Check if we're handling the first CRT in the chain. */
6954#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6955 if( crt_cnt++ == 0 &&
6956 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6957 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6958 {
6959 /* During client-side renegotiation, check that the server's
6960 * end-CRTs hasn't changed compared to the initial handshake,
6961 * mitigating the triple handshake attack. On success, reuse
6962 * the original end-CRT instead of parsing it again. */
6963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6964 if( ssl_check_peer_crt_unchanged( ssl,
6965 &ssl->in_msg[i],
6966 n ) != 0 )
6967 {
6968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6969 mbedtls_ssl_send_alert_message( ssl,
6970 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6971 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6972 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6973 }
6974
6975 /* Now we can safely free the original chain. */
6976 ssl_clear_peer_cert( ssl->session );
6977 }
6978#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6979
6980 /* Parse the next certificate in the chain. */
6981#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6982 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6983#else
6984 /* If we don't need to store the CRT chain permanently, parse
6985 * it in-place from the input buffer instead of making a copy. */
6986 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6987#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6988 switch( ret )
6989 {
6990 case 0: /*ok*/
6991 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6992 /* Ignore certificate with an unknown algorithm: maybe a
6993 prior certificate was already trusted. */
6994 break;
6995
6996 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6997 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6998 goto crt_parse_der_failed;
6999
7000 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7001 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7002 goto crt_parse_der_failed;
7003
7004 default:
7005 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7006 crt_parse_der_failed:
7007 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
7008 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7009 return( ret );
7010 }
7011
7012 i += n;
7013 }
7014
7015 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
7016 return( 0 );
7017}
7018
7019#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007020MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007021static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7022{
7023 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7024 return( -1 );
7025
7026 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7027 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7028 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7029 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7030 {
Ronald Cron19385882022-06-15 16:26:13 +02007031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007032 return( 0 );
7033 }
7034 return( -1 );
7035}
7036#endif /* MBEDTLS_SSL_SRV_C */
7037
7038/* Check if a certificate message is expected.
7039 * Return either
7040 * - SSL_CERTIFICATE_EXPECTED, or
7041 * - SSL_CERTIFICATE_SKIP
7042 * indicating whether a Certificate message is expected or not.
7043 */
7044#define SSL_CERTIFICATE_EXPECTED 0
7045#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007046MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007047static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7048 int authmode )
7049{
7050 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7051 ssl->handshake->ciphersuite_info;
7052
7053 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7054 return( SSL_CERTIFICATE_SKIP );
7055
7056#if defined(MBEDTLS_SSL_SRV_C)
7057 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7058 {
7059 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7060 return( SSL_CERTIFICATE_SKIP );
7061
7062 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7063 {
7064 ssl->session_negotiate->verify_result =
7065 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7066 return( SSL_CERTIFICATE_SKIP );
7067 }
7068 }
7069#else
7070 ((void) authmode);
7071#endif /* MBEDTLS_SSL_SRV_C */
7072
7073 return( SSL_CERTIFICATE_EXPECTED );
7074}
7075
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007076MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007077static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7078 int authmode,
7079 mbedtls_x509_crt *chain,
7080 void *rs_ctx )
7081{
7082 int ret = 0;
7083 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7084 ssl->handshake->ciphersuite_info;
7085 int have_ca_chain = 0;
7086
7087 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7088 void *p_vrfy;
7089
7090 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7091 return( 0 );
7092
7093 if( ssl->f_vrfy != NULL )
7094 {
7095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7096 f_vrfy = ssl->f_vrfy;
7097 p_vrfy = ssl->p_vrfy;
7098 }
7099 else
7100 {
7101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7102 f_vrfy = ssl->conf->f_vrfy;
7103 p_vrfy = ssl->conf->p_vrfy;
7104 }
7105
7106 /*
7107 * Main check: verify certificate
7108 */
7109#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7110 if( ssl->conf->f_ca_cb != NULL )
7111 {
7112 ((void) rs_ctx);
7113 have_ca_chain = 1;
7114
7115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7116 ret = mbedtls_x509_crt_verify_with_ca_cb(
7117 chain,
7118 ssl->conf->f_ca_cb,
7119 ssl->conf->p_ca_cb,
7120 ssl->conf->cert_profile,
7121 ssl->hostname,
7122 &ssl->session_negotiate->verify_result,
7123 f_vrfy, p_vrfy );
7124 }
7125 else
7126#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7127 {
7128 mbedtls_x509_crt *ca_chain;
7129 mbedtls_x509_crl *ca_crl;
7130
7131#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7132 if( ssl->handshake->sni_ca_chain != NULL )
7133 {
7134 ca_chain = ssl->handshake->sni_ca_chain;
7135 ca_crl = ssl->handshake->sni_ca_crl;
7136 }
7137 else
7138#endif
7139 {
7140 ca_chain = ssl->conf->ca_chain;
7141 ca_crl = ssl->conf->ca_crl;
7142 }
7143
7144 if( ca_chain != NULL )
7145 have_ca_chain = 1;
7146
7147 ret = mbedtls_x509_crt_verify_restartable(
7148 chain,
7149 ca_chain, ca_crl,
7150 ssl->conf->cert_profile,
7151 ssl->hostname,
7152 &ssl->session_negotiate->verify_result,
7153 f_vrfy, p_vrfy, rs_ctx );
7154 }
7155
7156 if( ret != 0 )
7157 {
7158 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7159 }
7160
7161#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7162 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7163 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7164#endif
7165
7166 /*
7167 * Secondary checks: always done, but change 'ret' only if it was 0
7168 */
7169
7170#if defined(MBEDTLS_ECP_C)
7171 {
7172 const mbedtls_pk_context *pk = &chain->pk;
7173
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007174 /* If certificate uses an EC key, make sure the curve is OK.
7175 * This is a public key, so it can't be opaque, so can_do() is a good
7176 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007177 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007178 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007179 /* and in the unlikely case the above assumption no longer holds
7180 * we are making sure that pk_ec() here does not return a NULL
7181 */
7182 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7183 if( ec == NULL )
7184 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7187 }
Jerry Yud9526692022-02-17 14:23:47 +08007188
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007189 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7190 {
7191 ssl->session_negotiate->verify_result |=
7192 MBEDTLS_X509_BADCERT_BAD_KEY;
7193
7194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7195 if( ret == 0 )
7196 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7197 }
Jerry Yud9526692022-02-17 14:23:47 +08007198 }
7199 }
7200#endif /* MBEDTLS_ECP_C */
7201
7202 if( mbedtls_ssl_check_cert_usage( chain,
7203 ciphersuite_info,
7204 ! ssl->conf->endpoint,
7205 &ssl->session_negotiate->verify_result ) != 0 )
7206 {
7207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7208 if( ret == 0 )
7209 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7210 }
7211
7212 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7213 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7214 * with details encoded in the verification flags. All other kinds
7215 * of error codes, including those from the user provided f_vrfy
7216 * functions, are treated as fatal and lead to a failure of
7217 * ssl_parse_certificate even if verification was optional. */
7218 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7219 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7220 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7221 {
7222 ret = 0;
7223 }
7224
7225 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7226 {
7227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7228 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7229 }
7230
7231 if( ret != 0 )
7232 {
7233 uint8_t alert;
7234
7235 /* The certificate may have been rejected for several reasons.
7236 Pick one and send the corresponding alert. Which alert to send
7237 may be a subject of debate in some cases. */
7238 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7239 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7240 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7241 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7242 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7243 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7244 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7245 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7246 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7247 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7248 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7249 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7250 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7251 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7252 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7253 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7254 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7255 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7256 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7257 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7258 else
7259 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7260 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7261 alert );
7262 }
7263
7264#if defined(MBEDTLS_DEBUG_C)
7265 if( ssl->session_negotiate->verify_result != 0 )
7266 {
7267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7268 (unsigned int) ssl->session_negotiate->verify_result ) );
7269 }
7270 else
7271 {
7272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7273 }
7274#endif /* MBEDTLS_DEBUG_C */
7275
7276 return( ret );
7277}
7278
7279#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007280MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007281static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7282 unsigned char *start, size_t len )
7283{
7284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7285 /* Remember digest of the peer's end-CRT. */
7286 ssl->session_negotiate->peer_cert_digest =
7287 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7288 if( ssl->session_negotiate->peer_cert_digest == NULL )
7289 {
7290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7291 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7292 mbedtls_ssl_send_alert_message( ssl,
7293 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7294 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7295
7296 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7297 }
7298
7299 ret = mbedtls_md( mbedtls_md_info_from_type(
7300 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7301 start, len,
7302 ssl->session_negotiate->peer_cert_digest );
7303
7304 ssl->session_negotiate->peer_cert_digest_type =
7305 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7306 ssl->session_negotiate->peer_cert_digest_len =
7307 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7308
7309 return( ret );
7310}
7311
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007312MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007313static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7314 unsigned char *start, size_t len )
7315{
7316 unsigned char *end = start + len;
7317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7318
7319 /* Make a copy of the peer's raw public key. */
7320 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7321 ret = mbedtls_pk_parse_subpubkey( &start, end,
7322 &ssl->handshake->peer_pubkey );
7323 if( ret != 0 )
7324 {
7325 /* We should have parsed the public key before. */
7326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7327 }
7328
7329 return( 0 );
7330}
7331#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7332
7333int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7334{
7335 int ret = 0;
7336 int crt_expected;
7337#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7338 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7339 ? ssl->handshake->sni_authmode
7340 : ssl->conf->authmode;
7341#else
7342 const int authmode = ssl->conf->authmode;
7343#endif
7344 void *rs_ctx = NULL;
7345 mbedtls_x509_crt *chain = NULL;
7346
7347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7348
7349 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7350 if( crt_expected == SSL_CERTIFICATE_SKIP )
7351 {
7352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7353 goto exit;
7354 }
7355
7356#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7357 if( ssl->handshake->ecrs_enabled &&
7358 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7359 {
7360 chain = ssl->handshake->ecrs_peer_cert;
7361 ssl->handshake->ecrs_peer_cert = NULL;
7362 goto crt_verify;
7363 }
7364#endif
7365
7366 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7367 {
7368 /* mbedtls_ssl_read_record may have sent an alert already. We
7369 let it decide whether to alert. */
7370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7371 goto exit;
7372 }
7373
7374#if defined(MBEDTLS_SSL_SRV_C)
7375 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7376 {
7377 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7378
7379 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7380 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7381
7382 goto exit;
7383 }
7384#endif /* MBEDTLS_SSL_SRV_C */
7385
7386 /* Clear existing peer CRT structure in case we tried to
7387 * reuse a session but it failed, and allocate a new one. */
7388 ssl_clear_peer_cert( ssl->session_negotiate );
7389
7390 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7391 if( chain == NULL )
7392 {
7393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7394 sizeof( mbedtls_x509_crt ) ) );
7395 mbedtls_ssl_send_alert_message( ssl,
7396 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7397 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7398
7399 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7400 goto exit;
7401 }
7402 mbedtls_x509_crt_init( chain );
7403
7404 ret = ssl_parse_certificate_chain( ssl, chain );
7405 if( ret != 0 )
7406 goto exit;
7407
7408#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7409 if( ssl->handshake->ecrs_enabled)
7410 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7411
7412crt_verify:
7413 if( ssl->handshake->ecrs_enabled)
7414 rs_ctx = &ssl->handshake->ecrs_ctx;
7415#endif
7416
7417 ret = ssl_parse_certificate_verify( ssl, authmode,
7418 chain, rs_ctx );
7419 if( ret != 0 )
7420 goto exit;
7421
7422#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7423 {
7424 unsigned char *crt_start, *pk_start;
7425 size_t crt_len, pk_len;
7426
7427 /* We parse the CRT chain without copying, so
7428 * these pointers point into the input buffer,
7429 * and are hence still valid after freeing the
7430 * CRT chain. */
7431
7432 crt_start = chain->raw.p;
7433 crt_len = chain->raw.len;
7434
7435 pk_start = chain->pk_raw.p;
7436 pk_len = chain->pk_raw.len;
7437
7438 /* Free the CRT structures before computing
7439 * digest and copying the peer's public key. */
7440 mbedtls_x509_crt_free( chain );
7441 mbedtls_free( chain );
7442 chain = NULL;
7443
7444 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7445 if( ret != 0 )
7446 goto exit;
7447
7448 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7449 if( ret != 0 )
7450 goto exit;
7451 }
7452#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7453 /* Pass ownership to session structure. */
7454 ssl->session_negotiate->peer_cert = chain;
7455 chain = NULL;
7456#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7457
7458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7459
7460exit:
7461
7462 if( ret == 0 )
7463 ssl->state++;
7464
7465#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7466 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7467 {
7468 ssl->handshake->ecrs_peer_cert = chain;
7469 chain = NULL;
7470 }
7471#endif
7472
7473 if( chain != NULL )
7474 {
7475 mbedtls_x509_crt_free( chain );
7476 mbedtls_free( chain );
7477 }
7478
7479 return( ret );
7480}
7481#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7482
Andrzej Kurek25f27152022-08-17 16:09:31 -04007483#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007484static void ssl_calc_finished_tls_sha256(
7485 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7486{
7487 int len = 12;
7488 const char *sender;
7489 unsigned char padbuf[32];
7490#if defined(MBEDTLS_USE_PSA_CRYPTO)
7491 size_t hash_size;
7492 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7493 psa_status_t status;
7494#else
7495 mbedtls_sha256_context sha256;
7496#endif
7497
7498 mbedtls_ssl_session *session = ssl->session_negotiate;
7499 if( !session )
7500 session = ssl->session;
7501
7502 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7503 ? "client finished"
7504 : "server finished";
7505
7506#if defined(MBEDTLS_USE_PSA_CRYPTO)
7507 sha256_psa = psa_hash_operation_init();
7508
7509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7510
7511 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7512 if( status != PSA_SUCCESS )
7513 {
7514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7515 return;
7516 }
7517
7518 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7519 if( status != PSA_SUCCESS )
7520 {
7521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7522 return;
7523 }
7524 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7525#else
7526
7527 mbedtls_sha256_init( &sha256 );
7528
7529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7530
7531 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7532
7533 /*
7534 * TLSv1.2:
7535 * hash = PRF( master, finished_label,
7536 * Hash( handshake ) )[0.11]
7537 */
7538
7539#if !defined(MBEDTLS_SHA256_ALT)
7540 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7541 sha256.state, sizeof( sha256.state ) );
7542#endif
7543
7544 mbedtls_sha256_finish( &sha256, padbuf );
7545 mbedtls_sha256_free( &sha256 );
7546#endif /* MBEDTLS_USE_PSA_CRYPTO */
7547
7548 ssl->handshake->tls_prf( session->master, 48, sender,
7549 padbuf, 32, buf, len );
7550
7551 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7552
7553 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7554
7555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7556}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007557#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007558
Jerry Yub7ba49e2022-02-17 14:25:53 +08007559
Andrzej Kurek25f27152022-08-17 16:09:31 -04007560#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007561static void ssl_calc_finished_tls_sha384(
7562 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7563{
7564 int len = 12;
7565 const char *sender;
7566 unsigned char padbuf[48];
7567#if defined(MBEDTLS_USE_PSA_CRYPTO)
7568 size_t hash_size;
7569 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7570 psa_status_t status;
7571#else
7572 mbedtls_sha512_context sha512;
7573#endif
7574
7575 mbedtls_ssl_session *session = ssl->session_negotiate;
7576 if( !session )
7577 session = ssl->session;
7578
7579 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7580 ? "client finished"
7581 : "server finished";
7582
7583#if defined(MBEDTLS_USE_PSA_CRYPTO)
7584 sha384_psa = psa_hash_operation_init();
7585
7586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7587
7588 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7589 if( status != PSA_SUCCESS )
7590 {
7591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7592 return;
7593 }
7594
7595 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7596 if( status != PSA_SUCCESS )
7597 {
7598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7599 return;
7600 }
7601 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7602#else
7603 mbedtls_sha512_init( &sha512 );
7604
7605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7606
Andrzej Kureka242e832022-08-11 10:03:14 -04007607 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007608
7609 /*
7610 * TLSv1.2:
7611 * hash = PRF( master, finished_label,
7612 * Hash( handshake ) )[0.11]
7613 */
7614
7615#if !defined(MBEDTLS_SHA512_ALT)
7616 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7617 sha512.state, sizeof( sha512.state ) );
7618#endif
7619 mbedtls_sha512_finish( &sha512, padbuf );
7620
7621 mbedtls_sha512_free( &sha512 );
7622#endif
7623
7624 ssl->handshake->tls_prf( session->master, 48, sender,
7625 padbuf, 48, buf, len );
7626
7627 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7628
7629 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7630
7631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7632}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007633#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007634
Jerry Yuaef00152022-02-17 14:27:31 +08007635void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7636{
7637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7638
7639 /*
7640 * Free our handshake params
7641 */
7642 mbedtls_ssl_handshake_free( ssl );
7643 mbedtls_free( ssl->handshake );
7644 ssl->handshake = NULL;
7645
7646 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007647 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007648 */
7649 if( ssl->transform )
7650 {
7651 mbedtls_ssl_transform_free( ssl->transform );
7652 mbedtls_free( ssl->transform );
7653 }
7654 ssl->transform = ssl->transform_negotiate;
7655 ssl->transform_negotiate = NULL;
7656
7657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7658}
7659
Jerry Yu2a9fff52022-02-17 14:28:51 +08007660void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7661{
7662 int resume = ssl->handshake->resume;
7663
7664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7665
7666#if defined(MBEDTLS_SSL_RENEGOTIATION)
7667 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7668 {
7669 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7670 ssl->renego_records_seen = 0;
7671 }
7672#endif
7673
7674 /*
7675 * Free the previous session and switch in the current one
7676 */
7677 if( ssl->session )
7678 {
7679#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7680 /* RFC 7366 3.1: keep the EtM state */
7681 ssl->session_negotiate->encrypt_then_mac =
7682 ssl->session->encrypt_then_mac;
7683#endif
7684
7685 mbedtls_ssl_session_free( ssl->session );
7686 mbedtls_free( ssl->session );
7687 }
7688 ssl->session = ssl->session_negotiate;
7689 ssl->session_negotiate = NULL;
7690
7691 /*
7692 * Add cache entry
7693 */
7694 if( ssl->conf->f_set_cache != NULL &&
7695 ssl->session->id_len != 0 &&
7696 resume == 0 )
7697 {
7698 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7699 ssl->session->id,
7700 ssl->session->id_len,
7701 ssl->session ) != 0 )
7702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7703 }
7704
7705#if defined(MBEDTLS_SSL_PROTO_DTLS)
7706 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7707 ssl->handshake->flight != NULL )
7708 {
7709 /* Cancel handshake timer */
7710 mbedtls_ssl_set_timer( ssl, 0 );
7711
7712 /* Keep last flight around in case we need to resend it:
7713 * we need the handshake and transform structures for that */
7714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7715 }
7716 else
7717#endif
7718 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7719
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007720 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007721
7722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7723}
7724
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007725int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7726{
7727 int ret, hash_len;
7728
7729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7730
7731 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7732
7733 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7734
7735 /*
7736 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7737 * may define some other value. Currently (early 2016), no defined
7738 * ciphersuite does this (and this is unlikely to change as activity has
7739 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7740 */
7741 hash_len = 12;
7742
7743#if defined(MBEDTLS_SSL_RENEGOTIATION)
7744 ssl->verify_data_len = hash_len;
7745 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7746#endif
7747
7748 ssl->out_msglen = 4 + hash_len;
7749 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7750 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7751
7752 /*
7753 * In case of session resuming, invert the client and server
7754 * ChangeCipherSpec messages order.
7755 */
7756 if( ssl->handshake->resume != 0 )
7757 {
7758#if defined(MBEDTLS_SSL_CLI_C)
7759 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7760 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7761#endif
7762#if defined(MBEDTLS_SSL_SRV_C)
7763 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7764 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7765#endif
7766 }
7767 else
7768 ssl->state++;
7769
7770 /*
7771 * Switch to our negotiated transform and session parameters for outbound
7772 * data.
7773 */
7774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7775
7776#if defined(MBEDTLS_SSL_PROTO_DTLS)
7777 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7778 {
7779 unsigned char i;
7780
7781 /* Remember current epoch settings for resending */
7782 ssl->handshake->alt_transform_out = ssl->transform_out;
7783 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7784 sizeof( ssl->handshake->alt_out_ctr ) );
7785
7786 /* Set sequence_number to zero */
7787 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7788
7789
7790 /* Increment epoch */
7791 for( i = 2; i > 0; i-- )
7792 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7793 break;
7794
7795 /* The loop goes to its end iff the counter is wrapping */
7796 if( i == 0 )
7797 {
7798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7799 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7800 }
7801 }
7802 else
7803#endif /* MBEDTLS_SSL_PROTO_DTLS */
7804 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7805
7806 ssl->transform_out = ssl->transform_negotiate;
7807 ssl->session_out = ssl->session_negotiate;
7808
7809#if defined(MBEDTLS_SSL_PROTO_DTLS)
7810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7811 mbedtls_ssl_send_flight_completed( ssl );
7812#endif
7813
7814 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7815 {
7816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7817 return( ret );
7818 }
7819
7820#if defined(MBEDTLS_SSL_PROTO_DTLS)
7821 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7822 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7823 {
7824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7825 return( ret );
7826 }
7827#endif
7828
7829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7830
7831 return( 0 );
7832}
7833
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007834#define SSL_MAX_HASH_LEN 12
7835
7836int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7837{
7838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7839 unsigned int hash_len = 12;
7840 unsigned char buf[SSL_MAX_HASH_LEN];
7841
7842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7843
7844 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7845
7846 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7847 {
7848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7849 goto exit;
7850 }
7851
7852 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
7857 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7858 goto exit;
7859 }
7860
7861 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7862 {
7863 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7864 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7865 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7866 goto exit;
7867 }
7868
7869 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7870 {
7871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7872 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7873 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7874 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7875 goto exit;
7876 }
7877
7878 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7879 buf, hash_len ) != 0 )
7880 {
7881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7882 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7883 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7884 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7885 goto exit;
7886 }
7887
7888#if defined(MBEDTLS_SSL_RENEGOTIATION)
7889 ssl->verify_data_len = hash_len;
7890 memcpy( ssl->peer_verify_data, buf, hash_len );
7891#endif
7892
7893 if( ssl->handshake->resume != 0 )
7894 {
7895#if defined(MBEDTLS_SSL_CLI_C)
7896 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7897 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7898#endif
7899#if defined(MBEDTLS_SSL_SRV_C)
7900 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7901 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7902#endif
7903 }
7904 else
7905 ssl->state++;
7906
7907#if defined(MBEDTLS_SSL_PROTO_DTLS)
7908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7909 mbedtls_ssl_recv_flight_completed( ssl );
7910#endif
7911
7912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7913
7914exit:
7915 mbedtls_platform_zeroize( buf, hash_len );
7916 return( ret );
7917}
7918
Jerry Yu392112c2022-02-17 14:34:10 +08007919#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7920/*
7921 * Helper to get TLS 1.2 PRF from ciphersuite
7922 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7923 */
7924static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7925{
Jerry Yu392112c2022-02-17 14:34:10 +08007926 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007927 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7928#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007929 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007930 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007931 else
Jerry Yu392112c2022-02-17 14:34:10 +08007932#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007933#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7934 {
7935 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7936 return( tls_prf_sha256 );
7937 }
7938#endif
7939#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7940 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7941 (void) ciphersuite_info;
7942#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007943
Andrzej Kurek894edde2022-09-29 06:31:14 -04007944 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007945}
7946#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007947
7948static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7949{
7950 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007951#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007952 if( tls_prf == tls_prf_sha384 )
7953 {
7954 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7955 }
7956 else
7957#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007958#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007959 if( tls_prf == tls_prf_sha256 )
7960 {
7961 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7962 }
7963 else
7964#endif
7965 return( MBEDTLS_SSL_TLS_PRF_NONE );
7966}
7967
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007968/*
7969 * Populate a transform structure with session keys and all the other
7970 * necessary information.
7971 *
7972 * Parameters:
7973 * - [in/out]: transform: structure to populate
7974 * [in] must be just initialised with mbedtls_ssl_transform_init()
7975 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7976 * - [in] ciphersuite
7977 * - [in] master
7978 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007979 * - [in] tls_prf: pointer to PRF to use for key derivation
7980 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007981 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007982 * - [in] endpoint: client or server
7983 * - [in] ssl: used for:
7984 * - ssl->conf->{f,p}_export_keys
7985 * [in] optionally used for:
7986 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7987 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007988MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007989static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7990 int ciphersuite,
7991 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007992#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007993 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007994#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007995 ssl_tls_prf_t tls_prf,
7996 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007997 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007998 unsigned endpoint,
7999 const mbedtls_ssl_context *ssl )
8000{
8001 int ret = 0;
8002 unsigned char keyblk[256];
8003 unsigned char *key1;
8004 unsigned char *key2;
8005 unsigned char *mac_enc;
8006 unsigned char *mac_dec;
8007 size_t mac_key_len = 0;
8008 size_t iv_copy_len;
8009 size_t keylen;
8010 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008011 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008012#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008013 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008014 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008015#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008016
8017#if defined(MBEDTLS_USE_PSA_CRYPTO)
8018 psa_key_type_t key_type;
8019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8020 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008021 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008022 size_t key_bits;
8023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8024#endif
8025
8026#if !defined(MBEDTLS_DEBUG_C) && \
8027 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8028 if( ssl->f_export_keys == NULL )
8029 {
8030 ssl = NULL; /* make sure we don't use it except for these cases */
8031 (void) ssl;
8032 }
8033#endif
8034
8035 /*
8036 * Some data just needs copying into the structure
8037 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008038#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008039 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008040#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008041 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008042
8043#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8044 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8045#endif
8046
8047#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008048 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008049 {
8050 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8051 * generation separate. This should never happen. */
8052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8053 }
8054#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8055
8056 /*
8057 * Get various info structures
8058 */
8059 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8060 if( ciphersuite_info == NULL )
8061 {
8062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8063 ciphersuite ) );
8064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8065 }
8066
Neil Armstrongab555e02022-04-04 11:07:59 +02008067 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008068#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008069 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008070#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008071 ciphersuite_info );
8072
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008073 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8074 transform->taglen =
8075 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8076
8077#if defined(MBEDTLS_USE_PSA_CRYPTO)
8078 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8079 transform->taglen,
8080 &alg,
8081 &key_type,
8082 &key_bits ) ) != PSA_SUCCESS )
8083 {
8084 ret = psa_ssl_status_to_mbedtls( status );
8085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8086 goto end;
8087 }
8088#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008089 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8090 if( cipher_info == NULL )
8091 {
8092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8093 ciphersuite_info->cipher ) );
8094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8095 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008096#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008097
Neil Armstronge4512952022-03-08 09:08:22 +01008098#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008099 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008100 if( mac_alg == 0 )
8101 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008103 (unsigned) ciphersuite_info->mac ) );
8104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8105 }
8106#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008107 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8108 if( md_info == NULL )
8109 {
8110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8111 (unsigned) ciphersuite_info->mac ) );
8112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8113 }
Neil Armstronge4512952022-03-08 09:08:22 +01008114#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008115
8116#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8117 /* Copy own and peer's CID if the use of the CID
8118 * extension has been negotiated. */
8119 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8120 {
8121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8122
8123 transform->in_cid_len = ssl->own_cid_len;
8124 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8125 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8126 transform->in_cid_len );
8127
8128 transform->out_cid_len = ssl->handshake->peer_cid_len;
8129 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8130 ssl->handshake->peer_cid_len );
8131 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8132 transform->out_cid_len );
8133 }
8134#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8135
8136 /*
8137 * Compute key block using the PRF
8138 */
8139 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8140 if( ret != 0 )
8141 {
8142 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8143 return( ret );
8144 }
8145
8146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8147 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8148 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8149 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8150 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8151
8152 /*
8153 * Determine the appropriate key, IV and MAC length.
8154 */
8155
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008156#if defined(MBEDTLS_USE_PSA_CRYPTO)
8157 keylen = PSA_BITS_TO_BYTES(key_bits);
8158#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008159 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008160#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008161
8162#if defined(MBEDTLS_GCM_C) || \
8163 defined(MBEDTLS_CCM_C) || \
8164 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008165 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008166 {
8167 size_t explicit_ivlen;
8168
8169 transform->maclen = 0;
8170 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008171
8172 /* All modes haves 96-bit IVs, but the length of the static parts vary
8173 * with mode and version:
8174 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8175 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8176 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8177 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8178 * sequence number).
8179 */
8180 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008181
8182 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008183#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008184 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008185#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008186 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8187 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008188#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008189
8190 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008191 transform->fixed_ivlen = 12;
8192 else
8193 transform->fixed_ivlen = 4;
8194
8195 /* Minimum length of encrypted record */
8196 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8197 transform->minlen = explicit_ivlen + transform->taglen;
8198 }
8199 else
8200#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8201#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008202 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8203 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8204 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008205 {
Neil Armstronge4512952022-03-08 09:08:22 +01008206#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008207 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008208#else
8209 size_t block_size = cipher_info->block_size;
8210#endif /* MBEDTLS_USE_PSA_CRYPTO */
8211
8212#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008213 /* Get MAC length */
8214 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8215#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008216 /* Initialize HMAC contexts */
8217 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8218 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8219 {
8220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8221 goto end;
8222 }
8223
8224 /* Get MAC length */
8225 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008226#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008227 transform->maclen = mac_key_len;
8228
8229 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008231 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008232#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008233 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008234#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008235
8236 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008237 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238 transform->minlen = transform->maclen;
8239 else
8240 {
8241 /*
8242 * GenericBlockCipher:
8243 * 1. if EtM is in use: one block plus MAC
8244 * otherwise: * first multiple of blocklen greater than maclen
8245 * 2. IV
8246 */
8247#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008248 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008249 {
8250 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008251 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008252 }
8253 else
8254#endif
8255 {
8256 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008257 + block_size
8258 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008259 }
8260
Glenn Strauss07c64162022-03-14 12:34:51 -04008261 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008262 {
8263 transform->minlen += transform->ivlen;
8264 }
8265 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008266 {
8267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8268 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8269 goto end;
8270 }
8271 }
8272 }
8273 else
8274#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8275 {
8276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8278 }
8279
8280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8281 (unsigned) keylen,
8282 (unsigned) transform->minlen,
8283 (unsigned) transform->ivlen,
8284 (unsigned) transform->maclen ) );
8285
8286 /*
8287 * Finally setup the cipher contexts, IVs and MAC secrets.
8288 */
8289#if defined(MBEDTLS_SSL_CLI_C)
8290 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8291 {
8292 key1 = keyblk + mac_key_len * 2;
8293 key2 = keyblk + mac_key_len * 2 + keylen;
8294
8295 mac_enc = keyblk;
8296 mac_dec = keyblk + mac_key_len;
8297
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008298 iv_copy_len = ( transform->fixed_ivlen ) ?
8299 transform->fixed_ivlen : transform->ivlen;
8300 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8301 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8302 iv_copy_len );
8303 }
8304 else
8305#endif /* MBEDTLS_SSL_CLI_C */
8306#if defined(MBEDTLS_SSL_SRV_C)
8307 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8308 {
8309 key1 = keyblk + mac_key_len * 2 + keylen;
8310 key2 = keyblk + mac_key_len * 2;
8311
8312 mac_enc = keyblk + mac_key_len;
8313 mac_dec = keyblk;
8314
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008315 iv_copy_len = ( transform->fixed_ivlen ) ?
8316 transform->fixed_ivlen : transform->ivlen;
8317 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8318 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8319 iv_copy_len );
8320 }
8321 else
8322#endif /* MBEDTLS_SSL_SRV_C */
8323 {
8324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8325 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8326 goto end;
8327 }
8328
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008329 if( ssl != NULL && ssl->f_export_keys != NULL )
8330 {
8331 ssl->f_export_keys( ssl->p_export_keys,
8332 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8333 master, 48,
8334 randbytes + 32,
8335 randbytes,
8336 tls_prf_get_type( tls_prf ) );
8337 }
8338
8339#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340 transform->psa_alg = alg;
8341
8342 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8343 {
8344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8345 psa_set_key_algorithm( &attributes, alg );
8346 psa_set_key_type( &attributes, key_type );
8347
8348 if( ( status = psa_import_key( &attributes,
8349 key1,
8350 PSA_BITS_TO_BYTES( key_bits ),
8351 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8352 {
8353 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8354 ret = psa_ssl_status_to_mbedtls( status );
8355 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8356 goto end;
8357 }
8358
8359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8360
8361 if( ( status = psa_import_key( &attributes,
8362 key2,
8363 PSA_BITS_TO_BYTES( key_bits ),
8364 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8365 {
8366 ret = psa_ssl_status_to_mbedtls( status );
8367 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8368 goto end;
8369 }
8370 }
8371#else
8372 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8373 cipher_info ) ) != 0 )
8374 {
8375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8376 goto end;
8377 }
8378
8379 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8380 cipher_info ) ) != 0 )
8381 {
8382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8383 goto end;
8384 }
8385
8386 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8387 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8388 MBEDTLS_ENCRYPT ) ) != 0 )
8389 {
8390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8391 goto end;
8392 }
8393
8394 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8395 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8396 MBEDTLS_DECRYPT ) ) != 0 )
8397 {
8398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8399 goto end;
8400 }
8401
8402#if defined(MBEDTLS_CIPHER_MODE_CBC)
8403 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8404 {
8405 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8406 MBEDTLS_PADDING_NONE ) ) != 0 )
8407 {
8408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8409 goto end;
8410 }
8411
8412 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8413 MBEDTLS_PADDING_NONE ) ) != 0 )
8414 {
8415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8416 goto end;
8417 }
8418 }
8419#endif /* MBEDTLS_CIPHER_MODE_CBC */
8420#endif /* MBEDTLS_USE_PSA_CRYPTO */
8421
Neil Armstrong29c0c042022-03-17 17:47:28 +01008422#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8423 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8424 For AEAD-based ciphersuites, there is nothing to do here. */
8425 if( mac_key_len != 0 )
8426 {
8427#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008428 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008429
8430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008431 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008432 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8433
8434 if( ( status = psa_import_key( &attributes,
8435 mac_enc, mac_key_len,
8436 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8437 {
8438 ret = psa_ssl_status_to_mbedtls( status );
8439 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8440 goto end;
8441 }
8442
Ronald Cronfb39f152022-03-25 14:36:28 +01008443 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008444 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8445#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8446 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8447#endif
8448 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008449 /* mbedtls_ct_hmac() requires the key to be exportable */
8450 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8451 PSA_KEY_USAGE_VERIFY_HASH );
8452 else
8453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8454
8455 if( ( status = psa_import_key( &attributes,
8456 mac_dec, mac_key_len,
8457 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8458 {
8459 ret = psa_ssl_status_to_mbedtls( status );
8460 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8461 goto end;
8462 }
8463#else
8464 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8465 if( ret != 0 )
8466 goto end;
8467 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8468 if( ret != 0 )
8469 goto end;
8470#endif /* MBEDTLS_USE_PSA_CRYPTO */
8471 }
8472#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8473
8474 ((void) mac_dec);
8475 ((void) mac_enc);
8476
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008477end:
8478 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8479 return( ret );
8480}
8481
Valerio Settia08b1a42022-11-17 15:10:02 +01008482#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8483 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008484int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008485 psa_pake_operation_t *pake_ctx,
8486 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008487 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008488{
8489 psa_status_t status;
8490 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008491 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008492 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8493 * At round two perform a single cycle
8494 */
8495 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008496
Valerio Setti6b3dab02022-11-17 17:14:54 +01008497 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008498 {
8499 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8500 step <= PSA_PAKE_STEP_ZK_PROOF;
8501 ++step )
8502 {
8503 /* Length is stored at the first byte */
8504 size_t length = buf[input_offset];
8505 input_offset += 1;
8506
8507 if( input_offset + length > len )
8508 {
8509 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8510 }
8511
8512 status = psa_pake_input( pake_ctx, step,
8513 buf + input_offset, length );
8514 if( status != PSA_SUCCESS)
8515 {
8516 return psa_ssl_status_to_mbedtls( status );
8517 }
8518
8519 input_offset += length;
8520 }
8521 }
8522
Valerio Setti819de862022-11-17 18:05:19 +01008523 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008524 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008525
Valerio Settia08b1a42022-11-17 15:10:02 +01008526 return( 0 );
8527}
8528
Valerio Setti6b3dab02022-11-17 17:14:54 +01008529int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008530 psa_pake_operation_t *pake_ctx,
8531 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008532 size_t len, size_t *olen,
8533 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008534{
8535 psa_status_t status;
8536 size_t output_offset = 0;
8537 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008538 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008539 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8540 * At round two perform a single cycle
8541 */
8542 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008543
Valerio Setti6b3dab02022-11-17 17:14:54 +01008544 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008545 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008546 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8547 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008548 ++step )
8549 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008550 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008551 * For each step, prepend 1 byte with the length of the data as
8552 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008553 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008554 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008555 buf + output_offset + 1,
8556 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008557 &output_len );
8558 if( status != PSA_SUCCESS )
8559 {
8560 return( psa_ssl_status_to_mbedtls( status ) );
8561 }
8562
Valerio Setti99d88c12022-11-22 16:03:43 +01008563 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008564
8565 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008566 }
8567 }
8568
8569 *olen = output_offset;
8570
8571 return( 0 );
8572}
Valerio Settia08b1a42022-11-17 15:10:02 +01008573#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8574
Jerry Yuee40f9d2022-02-17 14:55:16 +08008575#if defined(MBEDTLS_USE_PSA_CRYPTO)
8576int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8577 unsigned char *hash, size_t *hashlen,
8578 unsigned char *data, size_t data_len,
8579 mbedtls_md_type_t md_alg )
8580{
8581 psa_status_t status;
8582 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008583 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008584
8585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8586
8587 if( ( status = psa_hash_setup( &hash_operation,
8588 hash_alg ) ) != PSA_SUCCESS )
8589 {
8590 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8591 goto exit;
8592 }
8593
8594 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8595 64 ) ) != PSA_SUCCESS )
8596 {
8597 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8598 goto exit;
8599 }
8600
8601 if( ( status = psa_hash_update( &hash_operation,
8602 data, data_len ) ) != PSA_SUCCESS )
8603 {
8604 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8605 goto exit;
8606 }
8607
8608 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8609 hashlen ) ) != PSA_SUCCESS )
8610 {
8611 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8612 goto exit;
8613 }
8614
8615exit:
8616 if( status != PSA_SUCCESS )
8617 {
8618 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8619 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8620 switch( status )
8621 {
8622 case PSA_ERROR_NOT_SUPPORTED:
8623 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8624 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8625 case PSA_ERROR_BUFFER_TOO_SMALL:
8626 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8627 case PSA_ERROR_INSUFFICIENT_MEMORY:
8628 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8629 default:
8630 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8631 }
8632 }
8633 return( 0 );
8634}
8635
8636#else
8637
8638int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8639 unsigned char *hash, size_t *hashlen,
8640 unsigned char *data, size_t data_len,
8641 mbedtls_md_type_t md_alg )
8642{
8643 int ret = 0;
8644 mbedtls_md_context_t ctx;
8645 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8646 *hashlen = mbedtls_md_get_size( md_info );
8647
8648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8649
8650 mbedtls_md_init( &ctx );
8651
8652 /*
8653 * digitally-signed struct {
8654 * opaque client_random[32];
8655 * opaque server_random[32];
8656 * ServerDHParams params;
8657 * };
8658 */
8659 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8660 {
8661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8662 goto exit;
8663 }
8664 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8665 {
8666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8667 goto exit;
8668 }
8669 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8670 {
8671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8672 goto exit;
8673 }
8674 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8675 {
8676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8677 goto exit;
8678 }
8679 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8680 {
8681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8682 goto exit;
8683 }
8684
8685exit:
8686 mbedtls_md_free( &ctx );
8687
8688 if( ret != 0 )
8689 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8690 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8691
8692 return( ret );
8693}
8694#endif /* MBEDTLS_USE_PSA_CRYPTO */
8695
Jerry Yud9d91da2022-02-17 14:57:06 +08008696#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8697
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008698/* Find the preferred hash for a given signature algorithm. */
8699unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8700 mbedtls_ssl_context *ssl,
8701 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008702{
Gabor Mezei078e8032022-04-27 21:17:56 +02008703 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008704 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008705
8706 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008707 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008708
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008709 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008710 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008711 unsigned int hash_alg_received =
8712 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8713 received_sig_algs[i] );
8714 unsigned int sig_alg_received =
8715 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8716 received_sig_algs[i] );
8717
8718 if( sig_alg == sig_alg_received )
8719 {
8720#if defined(MBEDTLS_USE_PSA_CRYPTO)
8721 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8722 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008723 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008724 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008725
Neil Armstrong96eceb82022-06-30 18:05:05 +02008726 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8727 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8728 PSA_ALG_ECDSA( psa_hash_alg ),
8729 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008730 continue;
8731
Neil Armstrong96eceb82022-06-30 18:05:05 +02008732 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008733 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8734 PSA_ALG_RSA_PKCS1V15_SIGN(
8735 psa_hash_alg ),
8736 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008737 continue;
8738 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008739#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008740
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008741 return( hash_alg_received );
8742 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008743 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008744
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008745 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008746}
8747
8748#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8749
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008750/* Serialization of TLS 1.2 sessions:
8751 *
8752 * struct {
8753 * uint64 start_time;
8754 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008755 * uint8 session_id_len; // at most 32
8756 * opaque session_id[32];
8757 * opaque master[48]; // fixed length in the standard
8758 * uint32 verify_result;
8759 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8760 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8761 * uint32 ticket_lifetime;
8762 * uint8 mfl_code; // up to 255 according to standard
8763 * uint8 encrypt_then_mac; // 0 or 1
8764 * } serialized_session_tls12;
8765 *
8766 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008767static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008768 unsigned char *buf,
8769 size_t buf_len )
8770{
8771 unsigned char *p = buf;
8772 size_t used = 0;
8773
8774#if defined(MBEDTLS_HAVE_TIME)
8775 uint64_t start;
8776#endif
8777#if defined(MBEDTLS_X509_CRT_PARSE_C)
8778#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8779 size_t cert_len;
8780#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8781#endif /* MBEDTLS_X509_CRT_PARSE_C */
8782
8783 /*
8784 * Time
8785 */
8786#if defined(MBEDTLS_HAVE_TIME)
8787 used += 8;
8788
8789 if( used <= buf_len )
8790 {
8791 start = (uint64_t) session->start;
8792
8793 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8794 p += 8;
8795 }
8796#endif /* MBEDTLS_HAVE_TIME */
8797
8798 /*
8799 * Basic mandatory fields
8800 */
8801 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008802 + 1 /* id_len */
8803 + sizeof( session->id )
8804 + sizeof( session->master )
8805 + 4; /* verify_result */
8806
8807 if( used <= buf_len )
8808 {
8809 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8810 p += 2;
8811
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008812 *p++ = MBEDTLS_BYTE_0( session->id_len );
8813 memcpy( p, session->id, 32 );
8814 p += 32;
8815
8816 memcpy( p, session->master, 48 );
8817 p += 48;
8818
8819 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8820 p += 4;
8821 }
8822
8823 /*
8824 * Peer's end-entity certificate
8825 */
8826#if defined(MBEDTLS_X509_CRT_PARSE_C)
8827#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8828 if( session->peer_cert == NULL )
8829 cert_len = 0;
8830 else
8831 cert_len = session->peer_cert->raw.len;
8832
8833 used += 3 + cert_len;
8834
8835 if( used <= buf_len )
8836 {
8837 *p++ = MBEDTLS_BYTE_2( cert_len );
8838 *p++ = MBEDTLS_BYTE_1( cert_len );
8839 *p++ = MBEDTLS_BYTE_0( cert_len );
8840
8841 if( session->peer_cert != NULL )
8842 {
8843 memcpy( p, session->peer_cert->raw.p, cert_len );
8844 p += cert_len;
8845 }
8846 }
8847#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8848 if( session->peer_cert_digest != NULL )
8849 {
8850 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8851 if( used <= buf_len )
8852 {
8853 *p++ = (unsigned char) session->peer_cert_digest_type;
8854 *p++ = (unsigned char) session->peer_cert_digest_len;
8855 memcpy( p, session->peer_cert_digest,
8856 session->peer_cert_digest_len );
8857 p += session->peer_cert_digest_len;
8858 }
8859 }
8860 else
8861 {
8862 used += 2;
8863 if( used <= buf_len )
8864 {
8865 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8866 *p++ = 0;
8867 }
8868 }
8869#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8870#endif /* MBEDTLS_X509_CRT_PARSE_C */
8871
8872 /*
8873 * Session ticket if any, plus associated data
8874 */
8875#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8876 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8877
8878 if( used <= buf_len )
8879 {
8880 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8881 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8882 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8883
8884 if( session->ticket != NULL )
8885 {
8886 memcpy( p, session->ticket, session->ticket_len );
8887 p += session->ticket_len;
8888 }
8889
8890 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8891 p += 4;
8892 }
8893#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8894
8895 /*
8896 * Misc extension-related info
8897 */
8898#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8899 used += 1;
8900
8901 if( used <= buf_len )
8902 *p++ = session->mfl_code;
8903#endif
8904
8905#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8906 used += 1;
8907
8908 if( used <= buf_len )
8909 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8910#endif
8911
8912 return( used );
8913}
8914
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008915MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008916static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008917 const unsigned char *buf,
8918 size_t len )
8919{
8920#if defined(MBEDTLS_HAVE_TIME)
8921 uint64_t start;
8922#endif
8923#if defined(MBEDTLS_X509_CRT_PARSE_C)
8924#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8925 size_t cert_len;
8926#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8927#endif /* MBEDTLS_X509_CRT_PARSE_C */
8928
8929 const unsigned char *p = buf;
8930 const unsigned char * const end = buf + len;
8931
8932 /*
8933 * Time
8934 */
8935#if defined(MBEDTLS_HAVE_TIME)
8936 if( 8 > (size_t)( end - p ) )
8937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8938
8939 start = ( (uint64_t) p[0] << 56 ) |
8940 ( (uint64_t) p[1] << 48 ) |
8941 ( (uint64_t) p[2] << 40 ) |
8942 ( (uint64_t) p[3] << 32 ) |
8943 ( (uint64_t) p[4] << 24 ) |
8944 ( (uint64_t) p[5] << 16 ) |
8945 ( (uint64_t) p[6] << 8 ) |
8946 ( (uint64_t) p[7] );
8947 p += 8;
8948
8949 session->start = (time_t) start;
8950#endif /* MBEDTLS_HAVE_TIME */
8951
8952 /*
8953 * Basic mandatory fields
8954 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008955 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8957
8958 session->ciphersuite = ( p[0] << 8 ) | p[1];
8959 p += 2;
8960
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008961 session->id_len = *p++;
8962 memcpy( session->id, p, 32 );
8963 p += 32;
8964
8965 memcpy( session->master, p, 48 );
8966 p += 48;
8967
8968 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8969 ( (uint32_t) p[1] << 16 ) |
8970 ( (uint32_t) p[2] << 8 ) |
8971 ( (uint32_t) p[3] );
8972 p += 4;
8973
8974 /* Immediately clear invalid pointer values that have been read, in case
8975 * we exit early before we replaced them with valid ones. */
8976#if defined(MBEDTLS_X509_CRT_PARSE_C)
8977#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8978 session->peer_cert = NULL;
8979#else
8980 session->peer_cert_digest = NULL;
8981#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8982#endif /* MBEDTLS_X509_CRT_PARSE_C */
8983#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8984 session->ticket = NULL;
8985#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8986
8987 /*
8988 * Peer certificate
8989 */
8990#if defined(MBEDTLS_X509_CRT_PARSE_C)
8991#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8992 /* Deserialize CRT from the end of the ticket. */
8993 if( 3 > (size_t)( end - p ) )
8994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8995
8996 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8997 p += 3;
8998
8999 if( cert_len != 0 )
9000 {
9001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9002
9003 if( cert_len > (size_t)( end - p ) )
9004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9005
9006 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9007
9008 if( session->peer_cert == NULL )
9009 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9010
9011 mbedtls_x509_crt_init( session->peer_cert );
9012
9013 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9014 p, cert_len ) ) != 0 )
9015 {
9016 mbedtls_x509_crt_free( session->peer_cert );
9017 mbedtls_free( session->peer_cert );
9018 session->peer_cert = NULL;
9019 return( ret );
9020 }
9021
9022 p += cert_len;
9023 }
9024#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9025 /* Deserialize CRT digest from the end of the ticket. */
9026 if( 2 > (size_t)( end - p ) )
9027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9028
9029 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9030 session->peer_cert_digest_len = (size_t) *p++;
9031
9032 if( session->peer_cert_digest_len != 0 )
9033 {
9034 const mbedtls_md_info_t *md_info =
9035 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9036 if( md_info == NULL )
9037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9038 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9040
9041 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9043
9044 session->peer_cert_digest =
9045 mbedtls_calloc( 1, session->peer_cert_digest_len );
9046 if( session->peer_cert_digest == NULL )
9047 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9048
9049 memcpy( session->peer_cert_digest, p,
9050 session->peer_cert_digest_len );
9051 p += session->peer_cert_digest_len;
9052 }
9053#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9054#endif /* MBEDTLS_X509_CRT_PARSE_C */
9055
9056 /*
9057 * Session ticket and associated data
9058 */
9059#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9060 if( 3 > (size_t)( end - p ) )
9061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9062
9063 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9064 p += 3;
9065
9066 if( session->ticket_len != 0 )
9067 {
9068 if( session->ticket_len > (size_t)( end - p ) )
9069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9070
9071 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9072 if( session->ticket == NULL )
9073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9074
9075 memcpy( session->ticket, p, session->ticket_len );
9076 p += session->ticket_len;
9077 }
9078
9079 if( 4 > (size_t)( end - p ) )
9080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9081
9082 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9083 ( (uint32_t) p[1] << 16 ) |
9084 ( (uint32_t) p[2] << 8 ) |
9085 ( (uint32_t) p[3] );
9086 p += 4;
9087#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9088
9089 /*
9090 * Misc extension-related info
9091 */
9092#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9093 if( 1 > (size_t)( end - p ) )
9094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9095
9096 session->mfl_code = *p++;
9097#endif
9098
9099#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9100 if( 1 > (size_t)( end - p ) )
9101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9102
9103 session->encrypt_then_mac = *p++;
9104#endif
9105
9106 /* Done, should have consumed entire buffer */
9107 if( p != end )
9108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9109
9110 return( 0 );
9111}
Jerry Yudc7bd172022-02-17 13:44:15 +08009112#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9113
XiaokangQian75d40ef2022-04-20 11:05:24 +00009114int mbedtls_ssl_validate_ciphersuite(
9115 const mbedtls_ssl_context *ssl,
9116 const mbedtls_ssl_ciphersuite_t *suite_info,
9117 mbedtls_ssl_protocol_version min_tls_version,
9118 mbedtls_ssl_protocol_version max_tls_version )
9119{
9120 (void) ssl;
9121
9122 if( suite_info == NULL )
9123 return( -1 );
9124
9125 if( ( suite_info->min_tls_version > max_tls_version ) ||
9126 ( suite_info->max_tls_version < min_tls_version ) )
9127 {
9128 return( -1 );
9129 }
9130
XiaokangQian060d8672022-04-21 09:24:56 +00009131#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009132#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009133#if defined(MBEDTLS_USE_PSA_CRYPTO)
9134 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9135 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9136#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009137 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9138 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009139#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009140 {
9141 return( -1 );
9142 }
9143#endif
9144
9145 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9146#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9147 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9148 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9149 {
9150 return( -1 );
9151 }
9152#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9154
9155 return( 0 );
9156}
9157
Ronald Crone68ab4f2022-10-05 12:46:29 +02009158#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009159/*
9160 * Function for writing a signature algorithm extension.
9161 *
9162 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9163 * value (TLS 1.3 RFC8446):
9164 * enum {
9165 * ....
9166 * ecdsa_secp256r1_sha256( 0x0403 ),
9167 * ecdsa_secp384r1_sha384( 0x0503 ),
9168 * ecdsa_secp521r1_sha512( 0x0603 ),
9169 * ....
9170 * } SignatureScheme;
9171 *
9172 * struct {
9173 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9174 * } SignatureSchemeList;
9175 *
9176 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9177 * value (TLS 1.2 RFC5246):
9178 * enum {
9179 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9180 * sha512(6), (255)
9181 * } HashAlgorithm;
9182 *
9183 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9184 * SignatureAlgorithm;
9185 *
9186 * struct {
9187 * HashAlgorithm hash;
9188 * SignatureAlgorithm signature;
9189 * } SignatureAndHashAlgorithm;
9190 *
9191 * SignatureAndHashAlgorithm
9192 * supported_signature_algorithms<2..2^16-2>;
9193 *
9194 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9195 * generalization of the TLS 1.2 signature algorithm extension.
9196 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9197 * `SignatureScheme` field of TLS 1.3
9198 *
9199 */
9200int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9201 const unsigned char *end, size_t *out_len )
9202{
9203 unsigned char *p = buf;
9204 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9205 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9206
9207 *out_len = 0;
9208
9209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9210
9211 /* Check if we have space for header and length field:
9212 * - extension_type (2 bytes)
9213 * - extension_data_length (2 bytes)
9214 * - supported_signature_algorithms_length (2 bytes)
9215 */
9216 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9217 p += 6;
9218
9219 /*
9220 * Write supported_signature_algorithms
9221 */
9222 supported_sig_alg = p;
9223 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9224 if( sig_alg == NULL )
9225 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9226
9227 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9228 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9230 *sig_alg,
9231 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009232 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9233 continue;
9234 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9235 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9236 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009238 *sig_alg,
9239 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009240 }
9241
9242 /* Length of supported_signature_algorithms */
9243 supported_sig_alg_len = p - supported_sig_alg;
9244 if( supported_sig_alg_len == 0 )
9245 {
9246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9248 }
9249
XiaokangQianeaf36512022-04-24 09:07:44 +00009250 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009251 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009252 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9253
XiaokangQianeaf36512022-04-24 09:07:44 +00009254 *out_len = p - buf;
9255
9256#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009257 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009258#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009259
XiaokangQianeaf36512022-04-24 09:07:44 +00009260 return( 0 );
9261}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009262#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009263
XiaokangQian40a35232022-05-07 09:02:40 +00009264#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009265/*
9266 * mbedtls_ssl_parse_server_name_ext
9267 *
9268 * Structure of server_name extension:
9269 *
9270 * enum {
9271 * host_name(0), (255)
9272 * } NameType;
9273 * opaque HostName<1..2^16-1>;
9274 *
9275 * struct {
9276 * NameType name_type;
9277 * select (name_type) {
9278 * case host_name: HostName;
9279 * } name;
9280 * } ServerName;
9281 * struct {
9282 * ServerName server_name_list<1..2^16-1>
9283 * } ServerNameList;
9284 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009285MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009286int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9287 const unsigned char *buf,
9288 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009289{
9290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9291 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009292 size_t server_name_list_len, hostname_len;
9293 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009294
XiaokangQianf2a94202022-05-20 06:44:24 +00009295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009296
9297 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009298 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009299 p += 2;
9300
XiaokangQian9b2b7712022-05-17 02:57:00 +00009301 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9302 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009303 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009304 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009305 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009306 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009307 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9308 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009309
9310 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9311 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009312 /* sni_name is intended to be used only during the parsing of the
9313 * ClientHello message (it is reset to NULL before the end of
9314 * the message parsing). Thus it is ok to just point to the
9315 * reception buffer and not make a copy of it.
9316 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009317 ssl->handshake->sni_name = p + 3;
9318 ssl->handshake->sni_name_len = hostname_len;
9319 if( ssl->conf->f_sni == NULL )
9320 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009321 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009322 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009323 if( ret != 0 )
9324 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009325 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009326 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9327 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009328 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9329 }
9330 return( 0 );
9331 }
9332
9333 p += hostname_len + 3;
9334 }
9335
9336 return( 0 );
9337}
9338#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9339
XiaokangQianacb39922022-06-17 10:18:48 +00009340#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009341MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009342int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9343 const unsigned char *buf,
9344 const unsigned char *end )
9345{
9346 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009347 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009348 const unsigned char *protocol_name_list;
9349 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009350 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009351
9352 /* If ALPN not configured, just ignore the extension */
9353 if( ssl->conf->alpn_list == NULL )
9354 return( 0 );
9355
9356 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009357 * RFC7301, section 3.1
9358 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009359 *
XiaokangQianc7403452022-06-23 03:24:12 +00009360 * struct {
9361 * ProtocolName protocol_name_list<2..2^16-1>
9362 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009363 */
9364
XiaokangQianc7403452022-06-23 03:24:12 +00009365 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009366 * protocol_name_list_len 2 bytes
9367 * protocol_name_len 1 bytes
9368 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009369 */
XiaokangQianacb39922022-06-17 10:18:48 +00009370 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9371
XiaokangQianc7403452022-06-23 03:24:12 +00009372 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009373 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009374 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009375 protocol_name_list = p;
9376 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009377
9378 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009379 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009380 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009381 protocol_name_len = *p++;
9382 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9383 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009384 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009385 {
9386 MBEDTLS_SSL_PEND_FATAL_ALERT(
9387 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9388 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009389 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009390 }
9391
9392 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009393 }
9394
9395 /* Use our order of preference */
9396 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9397 {
9398 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009399 p = protocol_name_list;
9400 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009401 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009402 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009403 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009404 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009405 {
9406 ssl->alpn_chosen = *alpn;
9407 return( 0 );
9408 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009409
9410 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009411 }
9412 }
9413
XiaokangQian95d5f542022-06-24 02:29:26 +00009414 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009415 MBEDTLS_SSL_PEND_FATAL_ALERT(
9416 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9417 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9418 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9419}
9420
9421int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9422 unsigned char *buf,
9423 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009424 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009425{
9426 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009427 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009428 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009429
9430 if( ssl->alpn_chosen == NULL )
9431 {
9432 return( 0 );
9433 }
9434
XiaokangQian95d5f542022-06-24 02:29:26 +00009435 protocol_name_len = strlen( ssl->alpn_chosen );
9436 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009437
9438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9439 /*
9440 * 0 . 1 ext identifier
9441 * 2 . 3 ext length
9442 * 4 . 5 protocol list length
9443 * 6 . 6 protocol name length
9444 * 7 . 7+n protocol name
9445 */
9446 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9447
XiaokangQian95d5f542022-06-24 02:29:26 +00009448 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009449
XiaokangQian95d5f542022-06-24 02:29:26 +00009450 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9451 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009452 /* Note: the length of the chosen protocol has been checked to be less
9453 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9454 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009455 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009456
XiaokangQian95d5f542022-06-24 02:29:26 +00009457 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009458
9459#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9460 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9461#endif
9462
XiaokangQianacb39922022-06-17 10:18:48 +00009463 return ( 0 );
9464}
9465#endif /* MBEDTLS_SSL_ALPN */
9466
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009467#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009468 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009469 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009470 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009471int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9472 const char *hostname )
9473{
9474 /* Initialize to suppress unnecessary compiler warning */
9475 size_t hostname_len = 0;
9476
9477 /* Check if new hostname is valid before
9478 * making any change to current one */
9479 if( hostname != NULL )
9480 {
9481 hostname_len = strlen( hostname );
9482
9483 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9485 }
9486
9487 /* Now it's clear that we will overwrite the old hostname,
9488 * so we can free it safely */
9489 if( session->hostname != NULL )
9490 {
9491 mbedtls_platform_zeroize( session->hostname,
9492 strlen( session->hostname ) );
9493 mbedtls_free( session->hostname );
9494 }
9495
9496 /* Passing NULL as hostname shall clear the old one */
9497 if( hostname == NULL )
9498 {
9499 session->hostname = NULL;
9500 }
9501 else
9502 {
9503 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9504 if( session->hostname == NULL )
9505 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9506
9507 memcpy( session->hostname, hostname, hostname_len );
9508 }
9509
9510 return( 0 );
9511}
Xiaokang Qian03409292022-10-12 02:49:52 +00009512#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9513 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009514 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009515 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517#endif /* MBEDTLS_SSL_TLS_C */