blob: dbbd6f237d04cb43a9bae1ca76787e8c3737b6c0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
524void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
525 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
526{
527 ((void) ciphersuite_info);
528
Andrzej Kurek25f27152022-08-17 16:09:31 -0400529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800530 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
531 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
532 else
533#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400534#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800535 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
536 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
537 else
538#endif
539 {
540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
541 return;
542 }
543}
544
XiaokangQianadab9a62022-07-18 07:41:26 +0000545void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
546 unsigned hs_type,
547 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100548{
549 unsigned char hs_hdr[4];
550
551 /* Build HS header for checksum update. */
552 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
553 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
554 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
555 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
556
557 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
558}
559
560void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
561 unsigned hs_type,
562 unsigned char const *msg,
563 size_t msg_len )
564{
565 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
566 ssl->handshake->update_checksum( ssl, msg, msg_len );
567}
568
Jerry Yuc73c6182022-02-08 20:29:25 +0800569void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
570{
571 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400572#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800573#if defined(MBEDTLS_USE_PSA_CRYPTO)
574 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
575 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
576#else
577 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
578#endif
579#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400580#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800581#if defined(MBEDTLS_USE_PSA_CRYPTO)
582 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
583 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
584#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400585 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800586#endif
587#endif
588}
589
590static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
591 const unsigned char *buf, size_t len )
592{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
596#else
597 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
598#endif
599#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400600#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800601#if defined(MBEDTLS_USE_PSA_CRYPTO)
602 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
603#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400604 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800605#endif
606#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400607#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
608 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
609 (void) ssl;
610 (void) buf;
611 (void) len;
612#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800613}
614
Andrzej Kurek25f27152022-08-17 16:09:31 -0400615#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800616static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
617 const unsigned char *buf, size_t len )
618{
619#if defined(MBEDTLS_USE_PSA_CRYPTO)
620 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
621#else
622 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
623#endif
624}
625#endif
626
Andrzej Kurek25f27152022-08-17 16:09:31 -0400627#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800628static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
629 const unsigned char *buf, size_t len )
630{
631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
633#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400634 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800635#endif
636}
637#endif
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200642
Andrzej Kurek25f27152022-08-17 16:09:31 -0400643#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500644#if defined(MBEDTLS_USE_PSA_CRYPTO)
645 handshake->fin_sha256_psa = psa_hash_operation_init();
646 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
647#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200649 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200650#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500651#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400652#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500653#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500654 handshake->fin_sha384_psa = psa_hash_operation_init();
655 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500656#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400657 mbedtls_sha512_init( &handshake->fin_sha384 );
658 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500660#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200661
662 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_DHM_C)
665 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200666#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200667#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200669#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200670#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200671 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200672#if defined(MBEDTLS_SSL_CLI_C)
673 handshake->ecjpake_cache = NULL;
674 handshake->ecjpake_cache_len = 0;
675#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200676#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200677
Gilles Peskineeccd8882020-03-10 12:19:08 +0100678#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200679 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200680#endif
681
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200682#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
683 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
684#endif
Hanno Becker75173122019-02-06 16:18:31 +0000685
686#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
687 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
688 mbedtls_pk_init( &handshake->peer_pubkey );
689#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200690}
691
Hanno Beckera18d1322018-01-03 14:27:32 +0000692void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200695
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100696#if defined(MBEDTLS_USE_PSA_CRYPTO)
697 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
698 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100699#else
700 mbedtls_cipher_init( &transform->cipher_ctx_enc );
701 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100702#endif
703
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000704#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100705#if defined(MBEDTLS_USE_PSA_CRYPTO)
706 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
707 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100708#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_md_init( &transform->md_ctx_enc );
710 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000711#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100712#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713}
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200718}
719
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200720MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000722{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000724 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200726 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200729 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200730
731 /*
732 * Either the pointers are now NULL or cleared properly and can be freed.
733 * Now allocate missing structures.
734 */
735 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200737 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200738 }
Paul Bakker48916f92012-09-16 19:57:18 +0000739
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200740 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200742 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200743 }
Paul Bakker48916f92012-09-16 19:57:18 +0000744
Paul Bakker82788fb2014-10-20 13:59:19 +0200745 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200747 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200748 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500749#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
750 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400751
Andrzej Kurek4a063792020-10-21 15:08:44 +0200752 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
753 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500754#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000755
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200756 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000757 if( ssl->handshake == NULL ||
758 ssl->transform_negotiate == NULL ||
759 ssl->session_negotiate == NULL )
760 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_free( ssl->handshake );
764 mbedtls_free( ssl->transform_negotiate );
765 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200766
767 ssl->handshake = NULL;
768 ssl->transform_negotiate = NULL;
769 ssl->session_negotiate = NULL;
770
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200771 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000772 }
773
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200774 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000776 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200777 ssl_handshake_params_init( ssl->handshake );
778
Jerry Yud0766ec2022-09-22 10:46:57 +0800779#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
780 defined(MBEDTLS_SSL_SRV_C) && \
781 defined(MBEDTLS_SSL_SESSION_TICKETS)
782 ssl->handshake->new_session_tickets_count =
783 ssl->conf->new_session_tickets_count ;
784#endif
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
788 {
789 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200790
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200791 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
792 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
793 else
794 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200795
Hanno Becker0f57a652020-02-05 10:37:26 +0000796 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200797 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200798#endif
799
Brett Warrene0edc842021-08-17 09:53:13 +0100800/*
801 * curve_list is translated to IANA TLS group identifiers here because
802 * mbedtls_ssl_conf_curves returns void and so can't return
803 * any error codes.
804 */
805#if defined(MBEDTLS_ECP_C)
806#if !defined(MBEDTLS_DEPRECATED_REMOVED)
807 /* Heap allocate and translate curve_list from internal to IANA group ids */
808 if ( ssl->conf->curve_list != NULL )
809 {
810 size_t length;
811 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
812
813 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
814 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
815
816 /* Leave room for zero termination */
817 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
818 if ( group_list == NULL )
819 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
820
821 for( size_t i = 0; i < length; i++ )
822 {
823 const mbedtls_ecp_curve_info *info =
824 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
825 if ( info == NULL )
826 {
827 mbedtls_free( group_list );
828 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
829 }
830 group_list[i] = info->tls_id;
831 }
832
833 group_list[length] = 0;
834
835 ssl->handshake->group_list = group_list;
836 ssl->handshake->group_list_heap_allocated = 1;
837 }
838 else
839 {
840 ssl->handshake->group_list = ssl->conf->group_list;
841 ssl->handshake->group_list_heap_allocated = 0;
842 }
843#endif /* MBEDTLS_DEPRECATED_REMOVED */
844#endif /* MBEDTLS_ECP_C */
845
Ronald Crone68ab4f2022-10-05 12:46:29 +0200846#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +0800847#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800849 /* Heap allocate and translate sig_hashes from internal hash identifiers to
850 signature algorithms IANA identifiers. */
851 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800852 ssl->conf->sig_hashes != NULL )
853 {
854 const int *md;
855 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800856 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800857 uint16_t *p;
858
Jerry Yub476a442022-01-21 18:14:45 +0800859#if defined(static_assert)
860 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
861 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
862 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800863#endif
864
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
866 {
867 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
868 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800869#if defined(MBEDTLS_ECDSA_C)
870 sig_algs_len += sizeof( uint16_t );
871#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800872
Jerry Yub476a442022-01-21 18:14:45 +0800873#if defined(MBEDTLS_RSA_C)
874 sig_algs_len += sizeof( uint16_t );
875#endif
876 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
877 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800878 }
879
Jerry Yub476a442022-01-21 18:14:45 +0800880 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800881 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
882
Jerry Yub476a442022-01-21 18:14:45 +0800883 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
884 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800885 if( ssl->handshake->sig_algs == NULL )
886 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
887
888 p = (uint16_t *)ssl->handshake->sig_algs;
889 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
890 {
891 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
892 if( hash == MBEDTLS_SSL_HASH_NONE )
893 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800894#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
896 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800897#endif
898#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800899 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
900 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800901#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200903 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800904 ssl->handshake->sig_algs_heap_allocated = 1;
905 }
906 else
Jerry Yua69269a2022-01-17 21:06:01 +0800907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800908 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800909 ssl->handshake->sig_algs_heap_allocated = 0;
910 }
Jerry Yucc539102022-06-27 16:27:35 +0800911#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +0200912#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000913 return( 0 );
914}
915
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200916#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200918MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200919static int ssl_cookie_write_dummy( void *ctx,
920 unsigned char **p, unsigned char *end,
921 const unsigned char *cli_id, size_t cli_id_len )
922{
923 ((void) ctx);
924 ((void) p);
925 ((void) end);
926 ((void) cli_id);
927 ((void) cli_id_len);
928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200930}
931
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200933static int ssl_cookie_check_dummy( void *ctx,
934 const unsigned char *cookie, size_t cookie_len,
935 const unsigned char *cli_id, size_t cli_id_len )
936{
937 ((void) ctx);
938 ((void) cookie);
939 ((void) cookie_len);
940 ((void) cli_id);
941 ((void) cli_id_len);
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200944}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200945#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200946
Paul Bakker5121ce52009-01-03 21:22:43 +0000947/*
948 * Initialize an SSL context
949 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200950void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
951{
952 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
953}
954
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200955MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800956static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
957{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100958 const mbedtls_ssl_config *conf = ssl->conf;
959
Ronald Cron6f135e12021-12-08 16:57:54 +0100960#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100961 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
962 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000963 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
967 }
968
Jerry Yu60835a82021-08-04 10:13:52 +0800969 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
970 return( 0 );
971 }
972#endif
973
974#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100975 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800976 {
977 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
978 return( 0 );
979 }
980#endif
981
Ronald Cron6f135e12021-12-08 16:57:54 +0100982#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100983 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800984 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
988 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
989 }
990
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000991 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
992 {
993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
994 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
995 }
996
997
Ronald Crone1d3f062022-02-10 14:50:54 +0100998 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
999 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001000 }
1001#endif
1002
1003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1004 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1005}
1006
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001007MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001008static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1009{
1010 int ret;
1011 ret = ssl_conf_version_check( ssl );
1012 if( ret != 0 )
1013 return( ret );
1014
Jerry Yudef7ae42022-10-30 14:13:19 +08001015#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1016 /* RFC 8446 section 4.4.3
1017 *
1018 * If the verification fails, the receiver MUST terminate the handshake with
1019 * a "decrypt_error" alert.
1020 *
1021 * If the client is configured as TLS 1.3 only with optional verify, return
1022 * bad config.
1023 *
1024 */
1025 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1026 (mbedtls_ssl_context *)ssl ) &&
1027 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1028 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1029 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1030 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1031 {
1032 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001033 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001034 "is not available for TLS 1.3 client" ) );
1035 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1036 }
1037#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1038
Jerry Yu60835a82021-08-04 10:13:52 +08001039 /* Space for further checks */
1040
1041 return( 0 );
1042}
1043
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001044/*
1045 * Setup an SSL context
1046 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001047
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001048int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001049 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050{
Janos Follath865b3eb2019-12-16 11:46:15 +00001051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001052 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1053 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001055 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001056
Jerry Yu60835a82021-08-04 10:13:52 +08001057 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1058 return( ret );
1059
Paul Bakker62f2dee2012-09-28 07:31:51 +00001060 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001061 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001062 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001063
1064 /* Set to NULL in case of an error condition */
1065 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001066
Darryl Greenb33cc762019-11-28 14:29:44 +00001067#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1068 ssl->in_buf_len = in_buf_len;
1069#endif
1070 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001071 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001074 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001075 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001076 }
1077
Darryl Greenb33cc762019-11-28 14:29:44 +00001078#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1079 ssl->out_buf_len = out_buf_len;
1080#endif
1081 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001082 if( ssl->out_buf == NULL )
1083 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001085 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001086 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 }
1088
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001089 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001090
Johan Pascalb62bb512015-12-03 21:56:45 +01001091#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001092 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001093#endif
1094
Paul Bakker48916f92012-09-16 19:57:18 +00001095 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001096 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
1098 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001099
1100error:
1101 mbedtls_free( ssl->in_buf );
1102 mbedtls_free( ssl->out_buf );
1103
1104 ssl->conf = NULL;
1105
Darryl Greenb33cc762019-11-28 14:29:44 +00001106#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1107 ssl->in_buf_len = 0;
1108 ssl->out_buf_len = 0;
1109#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001110 ssl->in_buf = NULL;
1111 ssl->out_buf = NULL;
1112
1113 ssl->in_hdr = NULL;
1114 ssl->in_ctr = NULL;
1115 ssl->in_len = NULL;
1116 ssl->in_iv = NULL;
1117 ssl->in_msg = NULL;
1118
1119 ssl->out_hdr = NULL;
1120 ssl->out_ctr = NULL;
1121 ssl->out_len = NULL;
1122 ssl->out_iv = NULL;
1123 ssl->out_msg = NULL;
1124
k-stachowiak9f7798e2018-07-31 16:52:32 +02001125 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001126}
1127
1128/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001129 * Reset an initialized and used SSL context for re-use while retaining
1130 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001131 *
1132 * If partial is non-zero, keep data in the input buffer and client ID.
1133 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001134 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001135void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1136 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001137{
Darryl Greenb33cc762019-11-28 14:29:44 +00001138#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1139 size_t in_buf_len = ssl->in_buf_len;
1140 size_t out_buf_len = ssl->out_buf_len;
1141#else
1142 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1143 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1144#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001145
Hanno Beckerb0302c42021-08-03 09:39:42 +01001146#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1147 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001148#endif
1149
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001150 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001151 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001152
Hanno Beckerb0302c42021-08-03 09:39:42 +01001153 mbedtls_ssl_reset_in_out_pointers( ssl );
1154
1155 /* Reset incoming message parsing */
1156 ssl->in_offt = NULL;
1157 ssl->nb_zero = 0;
1158 ssl->in_msgtype = 0;
1159 ssl->in_msglen = 0;
1160 ssl->in_hslen = 0;
1161 ssl->keep_current_message = 0;
1162 ssl->transform_in = NULL;
1163
1164#if defined(MBEDTLS_SSL_PROTO_DTLS)
1165 ssl->next_record_offset = 0;
1166 ssl->in_epoch = 0;
1167#endif
1168
1169 /* Keep current datagram if partial == 1 */
1170 if( partial == 0 )
1171 {
1172 ssl->in_left = 0;
1173 memset( ssl->in_buf, 0, in_buf_len );
1174 }
1175
Ronald Cronad8c17b2022-06-10 17:18:09 +02001176 ssl->send_alert = 0;
1177
Hanno Beckerb0302c42021-08-03 09:39:42 +01001178 /* Reset outgoing message writing */
1179 ssl->out_msgtype = 0;
1180 ssl->out_msglen = 0;
1181 ssl->out_left = 0;
1182 memset( ssl->out_buf, 0, out_buf_len );
1183 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1184 ssl->transform_out = NULL;
1185
1186#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1187 mbedtls_ssl_dtls_replay_reset( ssl );
1188#endif
1189
XiaokangQian2b01dc32022-01-21 02:53:13 +00001190#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001191 if( ssl->transform )
1192 {
1193 mbedtls_ssl_transform_free( ssl->transform );
1194 mbedtls_free( ssl->transform );
1195 ssl->transform = NULL;
1196 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001197#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1198
XiaokangQian2b01dc32022-01-21 02:53:13 +00001199#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1200 mbedtls_ssl_transform_free( ssl->transform_application );
1201 mbedtls_free( ssl->transform_application );
1202 ssl->transform_application = NULL;
1203
1204 if( ssl->handshake != NULL )
1205 {
1206 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1207 mbedtls_free( ssl->handshake->transform_earlydata );
1208 ssl->handshake->transform_earlydata = NULL;
1209
1210 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1211 mbedtls_free( ssl->handshake->transform_handshake );
1212 ssl->handshake->transform_handshake = NULL;
1213 }
1214
XiaokangQian2b01dc32022-01-21 02:53:13 +00001215#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001216}
1217
1218int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1219{
1220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1221
1222 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1223
XiaokangQian78b1fa72022-01-19 06:56:30 +00001224 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001225
1226 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_SSL_RENEGOTIATION)
1228 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001229 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001230
1231 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1233 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001236
Hanno Beckerb0302c42021-08-03 09:39:42 +01001237 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001238 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001239 if( ssl->session )
1240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_ssl_session_free( ssl->session );
1242 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001243 ssl->session = NULL;
1244 }
1245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001247 ssl->alpn_chosen = NULL;
1248#endif
1249
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001250#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001251 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001252#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001253 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001254#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001255 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001256 {
1257 mbedtls_free( ssl->cli_id );
1258 ssl->cli_id = NULL;
1259 ssl->cli_id_len = 0;
1260 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001261#endif
1262
Paul Bakker48916f92012-09-16 19:57:18 +00001263 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1264 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001265
1266 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001267}
1268
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001269/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001270 * Reset an initialized and used SSL context for re-use while retaining
1271 * all application-set variables, function pointers and data.
1272 */
1273int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1274{
Hanno Becker43aefe22020-02-05 10:44:56 +00001275 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001276}
1277
1278/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 * SSL set accessors
1280 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001281void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001283 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001284}
1285
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001286void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001287{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001288 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001289}
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001292void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001293{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001294 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001295}
1296#endif
1297
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001298void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001300 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001301}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001304
Hanno Becker1841b0a2018-08-24 11:13:57 +01001305void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1306 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001307{
1308 ssl->disable_datagram_packing = !allow_packing;
1309}
1310
1311void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1312 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001314 conf->hs_timeout_min = min;
1315 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001316}
1317#endif
1318
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001319void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001321 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322}
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001325void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001326 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001327 void *p_vrfy )
1328{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001329 conf->f_vrfy = f_vrfy;
1330 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001331}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001333
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001334void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001335 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 void *p_rng )
1337{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001338 conf->f_rng = f_rng;
1339 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340}
1341
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001342void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001343 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 void *p_dbg )
1345{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001346 conf->f_dbg = f_dbg;
1347 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348}
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001351 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001352 mbedtls_ssl_send_t *f_send,
1353 mbedtls_ssl_recv_t *f_recv,
1354 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001355{
1356 ssl->p_bio = p_bio;
1357 ssl->f_send = f_send;
1358 ssl->f_recv = f_recv;
1359 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001360}
1361
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001362#if defined(MBEDTLS_SSL_PROTO_DTLS)
1363void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1364{
1365 ssl->mtu = mtu;
1366}
1367#endif
1368
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001369void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001370{
1371 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001372}
1373
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001374void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1375 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001376 mbedtls_ssl_set_timer_t *f_set_timer,
1377 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001378{
1379 ssl->p_timer = p_timer;
1380 ssl->f_set_timer = f_set_timer;
1381 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001382
1383 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001384 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001385}
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001388void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001389 void *p_cache,
1390 mbedtls_ssl_cache_get_t *f_get_cache,
1391 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001392{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001393 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001394 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001395 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_SSL_CLI_C)
1400int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401{
Janos Follath865b3eb2019-12-16 11:46:15 +00001402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001403
1404 if( ssl == NULL ||
1405 session == NULL ||
1406 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001407 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001410 }
1411
Hanno Beckere810bbc2021-05-14 16:01:05 +01001412 if( ssl->handshake->resume == 1 )
1413 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1414
Jerry Yu21092062022-10-10 21:21:31 +08001415#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1416 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001417 {
Jerry Yu21092062022-10-10 21:21:31 +08001418 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1419 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1420
1421 if( mbedtls_ssl_validate_ciphersuite(
1422 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1423 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1424 {
1425 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1426 session->ciphersuite ) );
1427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1428 }
Jerry Yu40afab62022-10-08 10:42:13 +08001429 }
Jerry Yu21092062022-10-10 21:21:31 +08001430#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001431
Hanno Becker52055ae2019-02-06 14:30:46 +00001432 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1433 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001434 return( ret );
1435
Paul Bakker0a597072012-09-25 21:55:46 +00001436 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001437
1438 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001442void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1443 const int *ciphersuites )
1444{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001445 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001446}
1447
Ronald Cron6f135e12021-12-08 16:57:54 +01001448#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001449void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001450 const int kex_modes )
1451{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001452 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001453}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001454
1455#if defined(MBEDTLS_SSL_EARLY_DATA)
1456void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001457 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001458{
1459 conf->early_data_enabled = early_data_enabled;
1460}
1461#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001462#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001465void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001466 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001467{
1468 conf->cert_profile = profile;
1469}
1470
Glenn Strauss36872db2022-01-22 05:06:31 -05001471static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1472{
1473 mbedtls_ssl_key_cert *cur = key_cert, *next;
1474
1475 while( cur != NULL )
1476 {
1477 next = cur->next;
1478 mbedtls_free( cur );
1479 cur = next;
1480 }
1481}
1482
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001483/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001484MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001485static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1486 mbedtls_x509_crt *cert,
1487 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001488{
niisato8ee24222018-06-25 19:05:48 +09001489 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001490
Glenn Strauss36872db2022-01-22 05:06:31 -05001491 if( cert == NULL )
1492 {
1493 /* Free list if cert is null */
1494 ssl_key_cert_free( *head );
1495 *head = NULL;
1496 return( 0 );
1497 }
1498
niisato8ee24222018-06-25 19:05:48 +09001499 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1500 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001501 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001502
niisato8ee24222018-06-25 19:05:48 +09001503 new_cert->cert = cert;
1504 new_cert->key = key;
1505 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001506
Glenn Strauss36872db2022-01-22 05:06:31 -05001507 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001508 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001509 {
niisato8ee24222018-06-25 19:05:48 +09001510 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001511 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001512 else
1513 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001514 mbedtls_ssl_key_cert *cur = *head;
1515 while( cur->next != NULL )
1516 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001517 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001518 }
1519
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001520 return( 0 );
1521}
1522
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001523int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001524 mbedtls_x509_crt *own_cert,
1525 mbedtls_pk_context *pk_key )
1526{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001527 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001528}
1529
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001530void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001531 mbedtls_x509_crt *ca_chain,
1532 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001533{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001534 conf->ca_chain = ca_chain;
1535 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001536
1537#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1538 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1539 * cannot be used together. */
1540 conf->f_ca_cb = NULL;
1541 conf->p_ca_cb = NULL;
1542#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001543}
Hanno Becker5adaad92019-03-27 16:54:37 +00001544
1545#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1546void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001547 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001548 void *p_ca_cb )
1549{
1550 conf->f_ca_cb = f_ca_cb;
1551 conf->p_ca_cb = p_ca_cb;
1552
1553 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1554 * cannot be used together. */
1555 conf->ca_chain = NULL;
1556 conf->ca_crl = NULL;
1557}
1558#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001560
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001561#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001562const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1563 size_t *name_len )
1564{
1565 *name_len = ssl->handshake->sni_name_len;
1566 return( ssl->handshake->sni_name );
1567}
1568
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001569int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1570 mbedtls_x509_crt *own_cert,
1571 mbedtls_pk_context *pk_key )
1572{
1573 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1574 own_cert, pk_key ) );
1575}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001576
1577void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1578 mbedtls_x509_crt *ca_chain,
1579 mbedtls_x509_crl *ca_crl )
1580{
1581 ssl->handshake->sni_ca_chain = ca_chain;
1582 ssl->handshake->sni_ca_crl = ca_crl;
1583}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001584
Glenn Strauss999ef702022-03-11 01:37:23 -05001585#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1586void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1587 const mbedtls_x509_crt *crt)
1588{
1589 ssl->handshake->dn_hints = crt;
1590}
1591#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1592
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001593void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1594 int authmode )
1595{
1596 ssl->handshake->sni_authmode = authmode;
1597}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001598#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1599
Hanno Becker8927c832019-04-03 12:52:50 +01001600#if defined(MBEDTLS_X509_CRT_PARSE_C)
1601void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1602 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1603 void *p_vrfy )
1604{
1605 ssl->f_vrfy = f_vrfy;
1606 ssl->p_vrfy = p_vrfy;
1607}
1608#endif
1609
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001610#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001611/*
1612 * Set EC J-PAKE password for current handshake
1613 */
1614int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1615 const unsigned char *pw,
1616 size_t pw_len )
1617{
1618 mbedtls_ecjpake_role role;
1619
Janos Follath8eb64132016-06-03 15:40:57 +01001620 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1622
1623 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1624 role = MBEDTLS_ECJPAKE_SERVER;
1625 else
1626 role = MBEDTLS_ECJPAKE_CLIENT;
1627
1628 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1629 role,
1630 MBEDTLS_MD_SHA256,
1631 MBEDTLS_ECP_DP_SECP256R1,
1632 pw, pw_len ) );
1633}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001634#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001635
Ronald Cron73fe8df2022-10-05 14:31:43 +02001636#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001637int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001638{
Ronald Crond29e13e2022-10-19 10:33:48 +02001639 if( conf->psk_identity == NULL ||
1640 conf->psk_identity_len == 0 )
1641 {
1642 return( 0 );
1643 }
1644
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001645#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001646 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001647 return( 1 );
1648#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001649
1650 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001651 return( 1 );
1652
1653 return( 0 );
1654}
1655
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001656static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1657{
1658 /* Remove reference to existing PSK, if any. */
1659#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001660 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001661 {
1662 /* The maintenance of the PSK key slot is the
1663 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001664 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001665 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001666#endif /* MBEDTLS_USE_PSA_CRYPTO */
1667 if( conf->psk != NULL )
1668 {
1669 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1670
1671 mbedtls_free( conf->psk );
1672 conf->psk = NULL;
1673 conf->psk_len = 0;
1674 }
1675
1676 /* Remove reference to PSK identity, if any. */
1677 if( conf->psk_identity != NULL )
1678 {
1679 mbedtls_free( conf->psk_identity );
1680 conf->psk_identity = NULL;
1681 conf->psk_identity_len = 0;
1682 }
1683}
1684
Hanno Becker7390c712018-11-15 13:33:04 +00001685/* This function assumes that PSK identity in the SSL config is unset.
1686 * It checks that the provided identity is well-formed and attempts
1687 * to make a copy of it in the SSL config.
1688 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001689MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001690static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1691 unsigned char const *psk_identity,
1692 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001693{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001694 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001695 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001696 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001697 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001698 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001699 {
1700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1701 }
1702
Hanno Becker7390c712018-11-15 13:33:04 +00001703 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1704 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001705 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001706
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001707 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001708 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001709
1710 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001711}
1712
Hanno Becker7390c712018-11-15 13:33:04 +00001713int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1714 const unsigned char *psk, size_t psk_len,
1715 const unsigned char *psk_identity, size_t psk_identity_len )
1716{
Janos Follath865b3eb2019-12-16 11:46:15 +00001717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001718
1719 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001720 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001721 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001722
1723 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001724 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001726 if( psk_len == 0 )
1727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1728 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1729 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1730
Hanno Becker7390c712018-11-15 13:33:04 +00001731 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1732 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1733 conf->psk_len = psk_len;
1734 memcpy( conf->psk, psk, conf->psk_len );
1735
1736 /* Check and set PSK Identity */
1737 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1738 if( ret != 0 )
1739 ssl_conf_remove_psk( conf );
1740
1741 return( ret );
1742}
1743
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001744static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1745{
1746#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001747 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001748 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001749 /* The maintenance of the external PSK key slot is the
1750 * user's responsibility. */
1751 if( ssl->handshake->psk_opaque_is_internal )
1752 {
1753 psa_destroy_key( ssl->handshake->psk_opaque );
1754 ssl->handshake->psk_opaque_is_internal = 0;
1755 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001756 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001757 }
Neil Armstronge952a302022-05-03 10:22:14 +02001758#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001759 if( ssl->handshake->psk != NULL )
1760 {
1761 mbedtls_platform_zeroize( ssl->handshake->psk,
1762 ssl->handshake->psk_len );
1763 mbedtls_free( ssl->handshake->psk );
1764 ssl->handshake->psk_len = 0;
1765 }
Neil Armstronge952a302022-05-03 10:22:14 +02001766#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001767}
1768
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001769int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1770 const unsigned char *psk, size_t psk_len )
1771{
Neil Armstrong501c9322022-05-03 09:35:09 +02001772#if defined(MBEDTLS_USE_PSA_CRYPTO)
1773 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001775 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001777#endif /* MBEDTLS_USE_PSA_CRYPTO */
1778
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001779 if( psk == NULL || ssl->handshake == NULL )
1780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1781
1782 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1784
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001785 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001786
Neil Armstrong501c9322022-05-03 09:35:09 +02001787#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001788#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1789 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1790 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001791 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001792 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1793 else
1794 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1795 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1796 }
1797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001798
Jerry Yu568ec252022-07-22 21:27:34 +08001799#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001800 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1801 {
1802 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1803 psa_set_key_usage_flags( &key_attributes,
1804 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1805 }
1806#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1807
Neil Armstrong501c9322022-05-03 09:35:09 +02001808 psa_set_key_algorithm( &key_attributes, alg );
1809 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1810
1811 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1812 if( status != PSA_SUCCESS )
1813 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1814
1815 /* Allow calling psa_destroy_key() on psk remove */
1816 ssl->handshake->psk_opaque_is_internal = 1;
1817 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1818#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001819 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001820 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001821
1822 ssl->handshake->psk_len = psk_len;
1823 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1824
1825 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001827}
1828
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001829#if defined(MBEDTLS_USE_PSA_CRYPTO)
1830int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001831 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001832 const unsigned char *psk_identity,
1833 size_t psk_identity_len )
1834{
Janos Follath865b3eb2019-12-16 11:46:15 +00001835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001836
1837 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001838 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001839 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001840
Hanno Becker7390c712018-11-15 13:33:04 +00001841 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001842 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001844 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001845
1846 /* Check and set PSK Identity */
1847 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1848 psk_identity_len );
1849 if( ret != 0 )
1850 ssl_conf_remove_psk( conf );
1851
1852 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001853}
1854
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001855int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1856 mbedtls_svc_key_id_t psk )
1857{
1858 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1859 ( ssl->handshake == NULL ) )
1860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1861
1862 ssl_remove_psk( ssl );
1863 ssl->handshake->psk_opaque = psk;
1864 return( 0 );
1865}
1866#endif /* MBEDTLS_USE_PSA_CRYPTO */
1867
Jerry Yu8897c072022-08-12 13:56:53 +08001868#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001869void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1870 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1871 size_t),
1872 void *p_psk )
1873{
1874 conf->f_psk = f_psk;
1875 conf->p_psk = p_psk;
1876}
Jerry Yu8897c072022-08-12 13:56:53 +08001877#endif /* MBEDTLS_SSL_SRV_C */
1878
Ronald Cron73fe8df2022-10-05 14:31:43 +02001879#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001880
1881#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001882static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1883 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001884{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001885#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001886 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001887 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001888#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001889 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001890 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001891 return( MBEDTLS_SSL_MODE_STREAM );
1892}
1893
1894#else /* MBEDTLS_USE_PSA_CRYPTO */
1895
1896static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1897 mbedtls_cipher_mode_t mode )
1898{
1899#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1900 if( mode == MBEDTLS_MODE_CBC )
1901 return( MBEDTLS_SSL_MODE_CBC );
1902#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1903
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001904#if defined(MBEDTLS_GCM_C) || \
1905 defined(MBEDTLS_CCM_C) || \
1906 defined(MBEDTLS_CHACHAPOLY_C)
1907 if( mode == MBEDTLS_MODE_GCM ||
1908 mode == MBEDTLS_MODE_CCM ||
1909 mode == MBEDTLS_MODE_CHACHAPOLY )
1910 return( MBEDTLS_SSL_MODE_AEAD );
1911#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001912
1913 return( MBEDTLS_SSL_MODE_STREAM );
1914}
Gilles Peskine301711e2022-04-26 16:57:05 +02001915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001916
Gilles Peskinee108d982022-04-26 16:50:40 +02001917static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1918 mbedtls_ssl_mode_t base_mode,
1919 int encrypt_then_mac )
1920{
1921#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1922 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1923 base_mode == MBEDTLS_SSL_MODE_CBC )
1924 {
1925 return( MBEDTLS_SSL_MODE_CBC_ETM );
1926 }
1927#else
1928 (void) encrypt_then_mac;
1929#endif
1930 return( base_mode );
1931}
1932
Neil Armstrongab555e02022-04-04 11:07:59 +02001933mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001934 const mbedtls_ssl_transform *transform )
1935{
Gilles Peskinee108d982022-04-26 16:50:40 +02001936 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001937#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001938 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001939#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001940 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1941#endif
1942 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001943
Gilles Peskinee108d982022-04-26 16:50:40 +02001944 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001945#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001946 encrypt_then_mac = transform->encrypt_then_mac;
1947#endif
1948 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001949}
1950
Neil Armstrongab555e02022-04-04 11:07:59 +02001951mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001952#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001953 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001954#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001955 const mbedtls_ssl_ciphersuite_t *suite )
1956{
Gilles Peskinee108d982022-04-26 16:50:40 +02001957 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1958
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001959#if defined(MBEDTLS_USE_PSA_CRYPTO)
1960 psa_status_t status;
1961 psa_algorithm_t alg;
1962 psa_key_type_t type;
1963 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001964 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1965 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001966 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001967#else
1968 const mbedtls_cipher_info_t *cipher =
1969 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001970 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001971 {
1972 base_mode =
1973 mbedtls_ssl_get_base_mode(
1974 mbedtls_cipher_info_get_mode( cipher ) );
1975 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001976#endif /* MBEDTLS_USE_PSA_CRYPTO */
1977
Gilles Peskinee108d982022-04-26 16:50:40 +02001978#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1979 int encrypt_then_mac = 0;
1980#endif
1981 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001982}
1983
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001984#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001985
1986#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001987/* Serialization of TLS 1.3 sessions:
1988 *
1989 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001990 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001991 * uint64 ticket_received;
1992 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001993 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001994 * } ClientOnlyData;
1995 *
1996 * struct {
1997 * uint8 endpoint;
1998 * uint8 ciphersuite[2];
1999 * uint32 ticket_age_add;
2000 * uint8 ticket_flags;
2001 * opaque resumption_key<0..255>;
2002 * select ( endpoint ) {
2003 * case client: ClientOnlyData;
2004 * case server: uint64 start_time;
2005 * };
2006 * } serialized_session_tls13;
2007 *
2008 */
2009#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002010MBEDTLS_CHECK_RETURN_CRITICAL
2011static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2012 unsigned char *buf,
2013 size_t buf_len,
2014 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002015{
2016 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002017#if defined(MBEDTLS_SSL_CLI_C) && \
2018 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2019 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002020 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002021#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002022 size_t needed = 1 /* endpoint */
2023 + 2 /* ciphersuite */
2024 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002025 + 1 /* ticket_flags */
2026 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002027 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002028
2029 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2031 needed += session->resumption_key_len; /* resumption_key */
2032
Jerry Yu438ddd82022-07-07 06:55:50 +00002033#if defined(MBEDTLS_HAVE_TIME)
2034 needed += 8; /* start_time or ticket_received */
2035#endif
2036
2037#if defined(MBEDTLS_SSL_CLI_C)
2038 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2039 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002040#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002041 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002042 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002043#endif
2044
Jerry Yu438ddd82022-07-07 06:55:50 +00002045 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002046 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002047
2048 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002049 if( session->ticket_len > SIZE_MAX - needed )
2050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002051
Jerry Yue28d9742022-08-18 15:44:03 +08002052 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002053 }
2054#endif /* MBEDTLS_SSL_CLI_C */
2055
Jerry Yue36fdd62022-08-17 21:31:36 +08002056 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002057 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002058 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002059
2060 p[0] = session->endpoint;
2061 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2062 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2063 p[7] = session->ticket_flags;
2064
2065 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002066 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002067 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002068 memcpy( p, session->resumption_key, session->resumption_key_len );
2069 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002070
2071#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2072 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2073 {
2074 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2075 p += 8;
2076 }
2077#endif /* MBEDTLS_HAVE_TIME */
2078
2079#if defined(MBEDTLS_SSL_CLI_C)
2080 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2081 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002082#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2083 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2084 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002085 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002086 {
2087 /* save host name */
2088 memcpy( p, session->hostname, hostname_len );
2089 p += hostname_len;
2090 }
2091#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2092
Jerry Yu438ddd82022-07-07 06:55:50 +00002093#if defined(MBEDTLS_HAVE_TIME)
2094 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2095 p += 8;
2096#endif
2097 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2098 p += 4;
2099
2100 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2101 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002102
2103 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002104 {
2105 memcpy( p, session->ticket, session->ticket_len );
2106 p += session->ticket_len;
2107 }
2108 }
2109#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002110 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002111}
2112
2113MBEDTLS_CHECK_RETURN_CRITICAL
2114static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2115 const unsigned char *buf,
2116 size_t len )
2117{
2118 const unsigned char *p = buf;
2119 const unsigned char *end = buf + len;
2120
2121 if( end - p < 9 )
2122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2123 session->endpoint = p[0];
2124 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2125 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2126 session->ticket_flags = p[7];
2127
2128 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002129 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002130 p += 9;
2131
Jerry Yubc7c1a42022-07-21 22:57:37 +08002132 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2134
Jerry Yubc7c1a42022-07-21 22:57:37 +08002135 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002137 memcpy( session->resumption_key, p, session->resumption_key_len );
2138 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002139
2140#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2141 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2142 {
2143 if( end - p < 8 )
2144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2145 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2146 p += 8;
2147 }
2148#endif /* MBEDTLS_HAVE_TIME */
2149
2150#if defined(MBEDTLS_SSL_CLI_C)
2151 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2152 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002153#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2154 defined(MBEDTLS_SSL_SESSION_TICKETS)
2155 size_t hostname_len;
2156 /* load host name */
2157 if( end - p < 2 )
2158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2159 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2160 p += 2;
2161
2162 if( end - p < ( long int )hostname_len )
2163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2164 if( hostname_len > 0 )
2165 {
2166 session->hostname = mbedtls_calloc( 1, hostname_len );
2167 if( session->hostname == NULL )
2168 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2169 memcpy( session->hostname, p, hostname_len );
2170 p += hostname_len;
2171 }
2172#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2173 MBEDTLS_SSL_SESSION_TICKETS */
2174
Jerry Yu438ddd82022-07-07 06:55:50 +00002175#if defined(MBEDTLS_HAVE_TIME)
2176 if( end - p < 8 )
2177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2178 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2179 p += 8;
2180#endif
2181 if( end - p < 4 )
2182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2183 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2184 p += 4;
2185
2186 if( end - p < 2 )
2187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2188 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2189 p += 2;
2190
2191 if( end - p < ( long int )session->ticket_len )
2192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2193 if( session->ticket_len > 0 )
2194 {
2195 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2196 if( session->ticket == NULL )
2197 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2198 memcpy( session->ticket, p, session->ticket_len );
2199 p += session->ticket_len;
2200 }
2201 }
2202#endif /* MBEDTLS_SSL_CLI_C */
2203
2204 return( 0 );
2205
2206}
2207#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002208MBEDTLS_CHECK_RETURN_CRITICAL
2209static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2210 unsigned char *buf,
2211 size_t buf_len,
2212 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002213{
2214 ((void) session);
2215 ((void) buf);
2216 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002217 *olen = 0;
2218 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002219}
Jerry Yu438ddd82022-07-07 06:55:50 +00002220
2221static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2222 unsigned char *buf,
2223 size_t buf_len )
2224{
2225 ((void) session);
2226 ((void) buf);
2227 ((void) buf_len);
2228 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2229}
2230#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002231#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2232
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002233psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002234 size_t taglen,
2235 psa_algorithm_t *alg,
2236 psa_key_type_t *key_type,
2237 size_t *key_size )
2238{
2239 switch ( mbedtls_cipher_type )
2240 {
2241 case MBEDTLS_CIPHER_AES_128_CBC:
2242 *alg = PSA_ALG_CBC_NO_PADDING;
2243 *key_type = PSA_KEY_TYPE_AES;
2244 *key_size = 128;
2245 break;
2246 case MBEDTLS_CIPHER_AES_128_CCM:
2247 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2248 *key_type = PSA_KEY_TYPE_AES;
2249 *key_size = 128;
2250 break;
2251 case MBEDTLS_CIPHER_AES_128_GCM:
2252 *alg = PSA_ALG_GCM;
2253 *key_type = PSA_KEY_TYPE_AES;
2254 *key_size = 128;
2255 break;
2256 case MBEDTLS_CIPHER_AES_192_CCM:
2257 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2258 *key_type = PSA_KEY_TYPE_AES;
2259 *key_size = 192;
2260 break;
2261 case MBEDTLS_CIPHER_AES_192_GCM:
2262 *alg = PSA_ALG_GCM;
2263 *key_type = PSA_KEY_TYPE_AES;
2264 *key_size = 192;
2265 break;
2266 case MBEDTLS_CIPHER_AES_256_CBC:
2267 *alg = PSA_ALG_CBC_NO_PADDING;
2268 *key_type = PSA_KEY_TYPE_AES;
2269 *key_size = 256;
2270 break;
2271 case MBEDTLS_CIPHER_AES_256_CCM:
2272 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2273 *key_type = PSA_KEY_TYPE_AES;
2274 *key_size = 256;
2275 break;
2276 case MBEDTLS_CIPHER_AES_256_GCM:
2277 *alg = PSA_ALG_GCM;
2278 *key_type = PSA_KEY_TYPE_AES;
2279 *key_size = 256;
2280 break;
2281 case MBEDTLS_CIPHER_ARIA_128_CBC:
2282 *alg = PSA_ALG_CBC_NO_PADDING;
2283 *key_type = PSA_KEY_TYPE_ARIA;
2284 *key_size = 128;
2285 break;
2286 case MBEDTLS_CIPHER_ARIA_128_CCM:
2287 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2288 *key_type = PSA_KEY_TYPE_ARIA;
2289 *key_size = 128;
2290 break;
2291 case MBEDTLS_CIPHER_ARIA_128_GCM:
2292 *alg = PSA_ALG_GCM;
2293 *key_type = PSA_KEY_TYPE_ARIA;
2294 *key_size = 128;
2295 break;
2296 case MBEDTLS_CIPHER_ARIA_192_CCM:
2297 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2298 *key_type = PSA_KEY_TYPE_ARIA;
2299 *key_size = 192;
2300 break;
2301 case MBEDTLS_CIPHER_ARIA_192_GCM:
2302 *alg = PSA_ALG_GCM;
2303 *key_type = PSA_KEY_TYPE_ARIA;
2304 *key_size = 192;
2305 break;
2306 case MBEDTLS_CIPHER_ARIA_256_CBC:
2307 *alg = PSA_ALG_CBC_NO_PADDING;
2308 *key_type = PSA_KEY_TYPE_ARIA;
2309 *key_size = 256;
2310 break;
2311 case MBEDTLS_CIPHER_ARIA_256_CCM:
2312 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2313 *key_type = PSA_KEY_TYPE_ARIA;
2314 *key_size = 256;
2315 break;
2316 case MBEDTLS_CIPHER_ARIA_256_GCM:
2317 *alg = PSA_ALG_GCM;
2318 *key_type = PSA_KEY_TYPE_ARIA;
2319 *key_size = 256;
2320 break;
2321 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2322 *alg = PSA_ALG_CBC_NO_PADDING;
2323 *key_type = PSA_KEY_TYPE_CAMELLIA;
2324 *key_size = 128;
2325 break;
2326 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2327 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2328 *key_type = PSA_KEY_TYPE_CAMELLIA;
2329 *key_size = 128;
2330 break;
2331 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2332 *alg = PSA_ALG_GCM;
2333 *key_type = PSA_KEY_TYPE_CAMELLIA;
2334 *key_size = 128;
2335 break;
2336 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2337 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2338 *key_type = PSA_KEY_TYPE_CAMELLIA;
2339 *key_size = 192;
2340 break;
2341 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2342 *alg = PSA_ALG_GCM;
2343 *key_type = PSA_KEY_TYPE_CAMELLIA;
2344 *key_size = 192;
2345 break;
2346 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2347 *alg = PSA_ALG_CBC_NO_PADDING;
2348 *key_type = PSA_KEY_TYPE_CAMELLIA;
2349 *key_size = 256;
2350 break;
2351 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2352 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2353 *key_type = PSA_KEY_TYPE_CAMELLIA;
2354 *key_size = 256;
2355 break;
2356 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2357 *alg = PSA_ALG_GCM;
2358 *key_type = PSA_KEY_TYPE_CAMELLIA;
2359 *key_size = 256;
2360 break;
2361 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2362 *alg = PSA_ALG_CHACHA20_POLY1305;
2363 *key_type = PSA_KEY_TYPE_CHACHA20;
2364 *key_size = 256;
2365 break;
2366 case MBEDTLS_CIPHER_NULL:
2367 *alg = MBEDTLS_SSL_NULL_CIPHER;
2368 *key_type = 0;
2369 *key_size = 0;
2370 break;
2371 default:
2372 return PSA_ERROR_NOT_SUPPORTED;
2373 }
2374
2375 return PSA_SUCCESS;
2376}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002377#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002378
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002379#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002380int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2381 const unsigned char *dhm_P, size_t P_len,
2382 const unsigned char *dhm_G, size_t G_len )
2383{
Janos Follath865b3eb2019-12-16 11:46:15 +00002384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002385
Glenn Strausscee11292021-12-20 01:43:17 -05002386 mbedtls_mpi_free( &conf->dhm_P );
2387 mbedtls_mpi_free( &conf->dhm_G );
2388
Hanno Beckera90658f2017-10-04 15:29:08 +01002389 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2390 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2391 {
2392 mbedtls_mpi_free( &conf->dhm_P );
2393 mbedtls_mpi_free( &conf->dhm_G );
2394 return( ret );
2395 }
2396
2397 return( 0 );
2398}
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002400int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002401{
Janos Follath865b3eb2019-12-16 11:46:15 +00002402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002403
Glenn Strausscee11292021-12-20 01:43:17 -05002404 mbedtls_mpi_free( &conf->dhm_P );
2405 mbedtls_mpi_free( &conf->dhm_G );
2406
Gilles Peskinee5702482021-06-11 21:59:08 +02002407 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2408 &conf->dhm_P ) ) != 0 ||
2409 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2410 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002411 {
2412 mbedtls_mpi_free( &conf->dhm_P );
2413 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002414 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002415 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002416
2417 return( 0 );
2418}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002419#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002420
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002421#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2422/*
2423 * Set the minimum length for Diffie-Hellman parameters
2424 */
2425void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2426 unsigned int bitlen )
2427{
2428 conf->dhm_min_bitlen = bitlen;
2429}
2430#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2431
Ronald Crone68ab4f2022-10-05 12:46:29 +02002432#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002433#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002434/*
2435 * Set allowed/preferred hashes for handshake signatures
2436 */
2437void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2438 const int *hashes )
2439{
2440 conf->sig_hashes = hashes;
2441}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002442#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002443
Jerry Yuf017ee42022-01-12 15:49:48 +08002444/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002445void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2446 const uint16_t* sig_algs )
2447{
Jerry Yuf017ee42022-01-12 15:49:48 +08002448#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2449 conf->sig_hashes = NULL;
2450#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2451 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002452}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002453#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002454
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002455#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002456#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002457/*
2458 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002459 *
2460 * mbedtls_ssl_setup() takes the provided list
2461 * and translates it to a list of IANA TLS group identifiers,
2462 * stored in ssl->handshake->group_list.
2463 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002464 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002465void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002466 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002468 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002469 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002470}
Brett Warrene0edc842021-08-17 09:53:13 +01002471#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002472#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002473
Brett Warrene0edc842021-08-17 09:53:13 +01002474/*
2475 * Set the allowed groups
2476 */
2477void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2478 const uint16_t *group_list )
2479{
2480#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2481 conf->curve_list = NULL;
2482#endif
2483 conf->group_list = group_list;
2484}
2485
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002486#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488{
Hanno Becker947194e2017-04-07 13:25:49 +01002489 /* Initialize to suppress unnecessary compiler warning */
2490 size_t hostname_len = 0;
2491
2492 /* Check if new hostname is valid before
2493 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002494 if( hostname != NULL )
2495 {
2496 hostname_len = strlen( hostname );
2497
2498 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2500 }
2501
2502 /* Now it's clear that we will overwrite the old hostname,
2503 * so we can free it safely */
2504
2505 if( ssl->hostname != NULL )
2506 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002507 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002508 mbedtls_free( ssl->hostname );
2509 }
2510
2511 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002512
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002514 {
2515 ssl->hostname = NULL;
2516 }
2517 else
2518 {
2519 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002520 if( ssl->hostname == NULL )
2521 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002522
Hanno Becker947194e2017-04-07 13:25:49 +01002523 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002524
Hanno Becker947194e2017-04-07 13:25:49 +01002525 ssl->hostname[hostname_len] = '\0';
2526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 return( 0 );
2529}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002530#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002532#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002533void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002535 const unsigned char *, size_t),
2536 void *p_sni )
2537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002538 conf->f_sni = f_sni;
2539 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002544int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002545{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002546 size_t cur_len, tot_len;
2547 const char **p;
2548
2549 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002550 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2551 * MUST NOT be truncated."
2552 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002553 */
2554 tot_len = 0;
2555 for( p = protos; *p != NULL; p++ )
2556 {
2557 cur_len = strlen( *p );
2558 tot_len += cur_len;
2559
Ronald Cron8216dd32020-04-23 16:41:44 +02002560 if( ( cur_len == 0 ) ||
2561 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2562 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002564 }
2565
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002566 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002567
2568 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002569}
2570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002572{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002573 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002576
Johan Pascalb62bb512015-12-03 21:56:45 +01002577#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002578void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2579 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002580{
2581 conf->dtls_srtp_mki_support = support_mki_value;
2582}
2583
Ron Eldoref72faf2018-07-12 11:54:20 +03002584int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2585 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002586 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002587{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002588 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002589 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002591 }
Ron Eldor591f1622018-01-22 12:30:04 +02002592
2593 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002594 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002595 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002596 }
Ron Eldor591f1622018-01-22 12:30:04 +02002597
2598 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2599 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002600 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002601}
2602
Ron Eldoref72faf2018-07-12 11:54:20 +03002603int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002604 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002605{
Johan Pascal253d0262020-09-22 13:04:45 +02002606 const mbedtls_ssl_srtp_profile *p;
2607 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002608
Johan Pascal253d0262020-09-22 13:04:45 +02002609 /* check the profiles list: all entry must be valid,
2610 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002611 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2612 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2613 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002614 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002615 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002616 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002617 list_size++;
2618 }
2619 else
2620 {
2621 /* unsupported value, stop parsing and set the size to an error value */
2622 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002623 }
2624 }
2625
Johan Pascal5ef72d22020-10-28 17:05:47 +01002626 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002627 {
Johan Pascal253d0262020-09-22 13:04:45 +02002628 conf->dtls_srtp_profile_list = NULL;
2629 conf->dtls_srtp_profile_list_len = 0;
2630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2631 }
2632
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002633 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002634 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002635
2636 return( 0 );
2637}
2638
Johan Pascal5ef72d22020-10-28 17:05:47 +01002639void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2640 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002641{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002642 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2643 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002644 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002645 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002646 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002647 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002648 else
2649 {
2650 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002651 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2652 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002653 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002654}
Johan Pascalb62bb512015-12-03 21:56:45 +01002655#endif /* MBEDTLS_SSL_DTLS_SRTP */
2656
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302657#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002658void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002659{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002660 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002661}
2662
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002663void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002664{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002665 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002666}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302667#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002668
Janos Follath088ce432017-04-10 12:42:31 +01002669#if defined(MBEDTLS_SSL_SRV_C)
2670void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2671 char cert_req_ca_list )
2672{
2673 conf->cert_req_ca_list = cert_req_ca_list;
2674}
2675#endif
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002678void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002680 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002681}
2682#endif
2683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002685void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002687 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002688}
2689#endif
2690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002692int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002695 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002698 }
2699
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002700 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002701
2702 return( 0 );
2703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002705
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002706void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002707{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002708 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002709}
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002712void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002713{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002714 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002715}
2716
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002717void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002718{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002719 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002720}
2721
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002722void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002723 const unsigned char period[8] )
2724{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002725 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002730#if defined(MBEDTLS_SSL_CLI_C)
2731void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002732{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002733 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002734}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002735#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002736
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002737#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002738
Jerry Yud0766ec2022-09-22 10:46:57 +08002739#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002740void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2741 uint16_t num_tickets )
2742{
Jerry Yud0766ec2022-09-22 10:46:57 +08002743 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002744}
2745#endif
2746
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002747void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2748 mbedtls_ssl_ticket_write_t *f_ticket_write,
2749 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2750 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002751{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002752 conf->f_ticket_write = f_ticket_write;
2753 conf->f_ticket_parse = f_ticket_parse;
2754 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002755}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002758
Hanno Becker7e6c1782021-06-08 09:24:55 +01002759void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2760 mbedtls_ssl_export_keys_t *f_export_keys,
2761 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002762{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002763 ssl->f_export_keys = f_export_keys;
2764 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002765}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002766
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002767#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002768void mbedtls_ssl_conf_async_private_cb(
2769 mbedtls_ssl_config *conf,
2770 mbedtls_ssl_async_sign_t *f_async_sign,
2771 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2772 mbedtls_ssl_async_resume_t *f_async_resume,
2773 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002774 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002775{
2776 conf->f_async_sign_start = f_async_sign;
2777 conf->f_async_decrypt_start = f_async_decrypt;
2778 conf->f_async_resume = f_async_resume;
2779 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002780 conf->p_async_config_data = async_config_data;
2781}
2782
Gilles Peskine8f97af72018-04-26 11:46:10 +02002783void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2784{
2785 return( conf->p_async_config_data );
2786}
2787
Gilles Peskine1febfef2018-04-30 11:54:39 +02002788void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002789{
2790 if( ssl->handshake == NULL )
2791 return( NULL );
2792 else
2793 return( ssl->handshake->user_async_ctx );
2794}
2795
Gilles Peskine1febfef2018-04-30 11:54:39 +02002796void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002797 void *ctx )
2798{
2799 if( ssl->handshake != NULL )
2800 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002801}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002802#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002803
Paul Bakker5121ce52009-01-03 21:22:43 +00002804/*
2805 * SSL get accessors
2806 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002807uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002808{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002809 if( ssl->session != NULL )
2810 return( ssl->session->verify_result );
2811
2812 if( ssl->session_negotiate != NULL )
2813 return( ssl->session_negotiate->verify_result );
2814
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002815 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816}
2817
Glenn Strauss8f526902022-01-13 00:04:49 -05002818int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2819{
2820 if( ssl == NULL || ssl->session == NULL )
2821 return( 0 );
2822
2823 return( ssl->session->ciphersuite );
2824}
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002827{
Paul Bakker926c8e42013-03-06 10:23:34 +01002828 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002829 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002832}
2833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002837 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002838 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002839 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002840 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002841 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002842 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002843 default:
2844 return( "unknown (DTLS)" );
2845 }
2846 }
2847#endif
2848
Glenn Strauss60bfe602022-03-14 19:04:24 -04002849 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002850 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002851 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002853 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002854 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002855 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002856 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002857 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002858}
2859
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002860#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002861size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2862{
David Horstmann95d516f2021-05-04 18:36:56 +01002863 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002864 size_t read_mfl;
2865
2866 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2867 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2868 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2869 {
2870 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2871 }
2872
2873 /* Check if a smaller max length was negotiated */
2874 if( ssl->session_out != NULL )
2875 {
2876 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2877 if( read_mfl < max_len )
2878 {
2879 max_len = read_mfl;
2880 }
2881 }
2882
2883 // During a handshake, use the value being negotiated
2884 if( ssl->session_negotiate != NULL )
2885 {
2886 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2887 if( read_mfl < max_len )
2888 {
2889 max_len = read_mfl;
2890 }
2891 }
2892
2893 return( max_len );
2894}
2895
2896size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002897{
2898 size_t max_len;
2899
2900 /*
2901 * Assume mfl_code is correct since it was checked when set
2902 */
Angus Grattond8213d02016-05-25 20:56:48 +10002903 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002904
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002905 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002906 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002907 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002908 {
Angus Grattond8213d02016-05-25 20:56:48 +10002909 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002910 }
2911
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002912 /* During a handshake, use the value being negotiated */
2913 if( ssl->session_negotiate != NULL &&
2914 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2915 {
2916 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2917 }
2918
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002919 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002920}
2921#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2922
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002924size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002925{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002926 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2927 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2928 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2929 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2930 return ( 0 );
2931
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002932 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2933 return( ssl->mtu );
2934
2935 if( ssl->mtu == 0 )
2936 return( ssl->handshake->mtu );
2937
2938 return( ssl->mtu < ssl->handshake->mtu ?
2939 ssl->mtu : ssl->handshake->mtu );
2940}
2941#endif /* MBEDTLS_SSL_PROTO_DTLS */
2942
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002943int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2944{
2945 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2946
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002947#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2948 !defined(MBEDTLS_SSL_PROTO_DTLS)
2949 (void) ssl;
2950#endif
2951
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002952#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002953 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002954
2955 if( max_len > mfl )
2956 max_len = mfl;
2957#endif
2958
2959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002960 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002961 {
Hanno Becker89490712020-02-05 10:50:12 +00002962 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002963 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2964 const size_t overhead = (size_t) ret;
2965
2966 if( ret < 0 )
2967 return( ret );
2968
2969 if( mtu <= overhead )
2970 {
2971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2972 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2973 }
2974
2975 if( max_len > mtu - overhead )
2976 max_len = mtu - overhead;
2977 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002978#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002979
Hanno Becker0defedb2018-08-10 12:35:02 +01002980#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2981 !defined(MBEDTLS_SSL_PROTO_DTLS)
2982 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002983#endif
2984
2985 return( (int) max_len );
2986}
2987
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002988int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2989{
2990 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2991
2992#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2993 (void) ssl;
2994#endif
2995
2996#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2997 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2998
2999 if( max_len > mfl )
3000 max_len = mfl;
3001#endif
3002
3003 return( (int) max_len );
3004}
3005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#if defined(MBEDTLS_X509_CRT_PARSE_C)
3007const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003008{
3009 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003010 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003011
Hanno Beckere6824572019-02-07 13:18:46 +00003012#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003013 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003014#else
3015 return( NULL );
3016#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003017}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003021int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3022 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003023{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003024 int ret;
3025
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003026 if( ssl == NULL ||
3027 dst == NULL ||
3028 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003029 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003032 }
3033
Hanno Beckere810bbc2021-05-14 16:01:05 +01003034 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3035 * idempotent: Each session can only be exported once.
3036 *
3037 * (This is in preparation for TLS 1.3 support where we will
3038 * need the ability to export multiple sessions (aka tickets),
3039 * which will be achieved by calling mbedtls_ssl_get_session()
3040 * multiple times until it fails.)
3041 *
3042 * Check whether we have already exported the current session,
3043 * and fail if so.
3044 */
3045 if( ssl->session->exported == 1 )
3046 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3047
3048 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3049 if( ret != 0 )
3050 return( ret );
3051
3052 /* Remember that we've exported the session. */
3053 ssl->session->exported = 1;
3054 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003057
Paul Bakker5121ce52009-01-03 21:22:43 +00003058/*
Hanno Beckera835da52019-05-16 12:39:07 +01003059 * Define ticket header determining Mbed TLS version
3060 * and structure of the ticket.
3061 */
3062
Hanno Becker94ef3b32019-05-16 12:50:45 +01003063/*
Hanno Becker50b59662019-05-28 14:30:45 +01003064 * Define bitflag determining compile-time settings influencing
3065 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003066 */
3067
Hanno Becker50b59662019-05-28 14:30:45 +01003068#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003069#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003070#else
Hanno Becker3e088662019-05-29 11:10:18 +01003071#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003072#endif /* MBEDTLS_HAVE_TIME */
3073
3074#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003075#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003076#else
Hanno Becker3e088662019-05-29 11:10:18 +01003077#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003078#endif /* MBEDTLS_X509_CRT_PARSE_C */
3079
3080#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003081#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003082#else
Hanno Becker3e088662019-05-29 11:10:18 +01003083#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003084#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3085
3086#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003087#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003088#else
Hanno Becker3e088662019-05-29 11:10:18 +01003089#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003090#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3091
Hanno Becker94ef3b32019-05-16 12:50:45 +01003092#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003093#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003094#else
Hanno Becker3e088662019-05-29 11:10:18 +01003095#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003096#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3097
Hanno Becker94ef3b32019-05-16 12:50:45 +01003098#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3099#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3100#else
3101#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3102#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3103
Hanno Becker3e088662019-05-29 11:10:18 +01003104#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3105#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3106#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3107#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003108#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3109#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003110
Hanno Becker50b59662019-05-28 14:30:45 +01003111#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003112 ( (uint16_t) ( \
3113 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3114 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3115 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3116 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003117 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003118 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003119
Hanno Beckerf8787072019-05-16 12:41:07 +01003120static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003121 MBEDTLS_VERSION_MAJOR,
3122 MBEDTLS_VERSION_MINOR,
3123 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003124 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3125 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003126};
Hanno Beckera835da52019-05-16 12:39:07 +01003127
3128/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003129 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003130 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003131 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003132 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003133 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003134 * opaque mbedtls_version[3]; // library version: major, minor, patch
3135 * opaque session_format[2]; // library-version specific 16-bit field
3136 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003137 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003138 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003139 * Note: When updating the format, remember to keep
3140 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003141 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003142 * // In this version, `session_format` determines
3143 * // the setting of those compile-time
3144 * // configuration options which influence
3145 * // the structure of mbedtls_ssl_session.
3146 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003147 * uint8_t minor_ver; // Protocol minor version. Possible values:
3148 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003149 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003150 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003151 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003152 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003153 * serialized_session_tls12 data;
3154 *
3155 * };
3156 *
3157 * } serialized_session;
3158 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003159 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003160
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003161MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003162static int ssl_session_save( const mbedtls_ssl_session *session,
3163 unsigned char omit_header,
3164 unsigned char *buf,
3165 size_t buf_len,
3166 size_t *olen )
3167{
3168 unsigned char *p = buf;
3169 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003170 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003171#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3172 size_t out_len;
3173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3174#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003175 if( session == NULL )
3176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3177
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003178 if( !omit_header )
3179 {
3180 /*
3181 * Add Mbed TLS version identifier
3182 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003183 used += sizeof( ssl_serialized_session_header );
3184
3185 if( used <= buf_len )
3186 {
3187 memcpy( p, ssl_serialized_session_header,
3188 sizeof( ssl_serialized_session_header ) );
3189 p += sizeof( ssl_serialized_session_header );
3190 }
3191 }
3192
3193 /*
3194 * TLS version identifier
3195 */
3196 used += 1;
3197 if( used <= buf_len )
3198 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003199 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003200 }
3201
3202 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003203 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003204 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003205 {
3206#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003207 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003208 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003209 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003210#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3211
Jerry Yu251a12e2022-07-13 15:15:48 +08003212#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3213 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003214 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3215 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003216 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003217 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003218 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003219#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3220
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003221 default:
3222 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3223 }
3224
3225 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003226 if( used > buf_len )
3227 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003228
3229 return( 0 );
3230}
3231
3232/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003233 * Public wrapper for ssl_session_save()
3234 */
3235int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3236 unsigned char *buf,
3237 size_t buf_len,
3238 size_t *olen )
3239{
3240 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3241}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003242
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003243/*
3244 * Deserialize session, see mbedtls_ssl_session_save() for format.
3245 *
3246 * This internal version is wrapped by a public function that cleans up in
3247 * case of error, and has an extra option omit_header.
3248 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003249MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003250static int ssl_session_load( mbedtls_ssl_session *session,
3251 unsigned char omit_header,
3252 const unsigned char *buf,
3253 size_t len )
3254{
3255 const unsigned char *p = buf;
3256 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003257 size_t remaining_len;
3258
3259
3260 if( session == NULL )
3261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003262
3263 if( !omit_header )
3264 {
3265 /*
3266 * Check Mbed TLS version identifier
3267 */
3268
3269 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3271
3272 if( memcmp( p, ssl_serialized_session_header,
3273 sizeof( ssl_serialized_session_header ) ) != 0 )
3274 {
3275 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3276 }
3277 p += sizeof( ssl_serialized_session_header );
3278 }
3279
3280 /*
3281 * TLS version identifier
3282 */
3283 if( 1 > (size_t)( end - p ) )
3284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003285 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003286
3287 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003288 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003289 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003290 {
3291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003292 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003293 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003294#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3295
Jerry Yu438ddd82022-07-07 06:55:50 +00003296#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3297 case MBEDTLS_SSL_VERSION_TLS1_3:
3298 return( ssl_tls13_session_load( session, p, remaining_len ) );
3299#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3300
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003301 default:
3302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3303 }
3304}
3305
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003306/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003307 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003308 */
3309int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3310 const unsigned char *buf,
3311 size_t len )
3312{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003313 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003314
3315 if( ret != 0 )
3316 mbedtls_ssl_session_free( session );
3317
3318 return( ret );
3319}
3320
3321/*
Paul Bakker1961b702013-01-25 14:49:24 +01003322 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003324MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003325static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3326{
3327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3328
Ronald Cron66dbf912022-02-02 15:33:46 +01003329 /*
3330 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003331 * that were written into the output buffer by the previous handshake step,
3332 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003333 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3334 * We proceed to the next handshake step only when all data from the
3335 * previous one have been sent to the peer, thus we make sure that this is
3336 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3337 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3338 * we have to wait before to go ahead.
3339 * In the case of TLS 1.3, handshake step handlers do not send data to the
3340 * peer. Data are only sent here and through
3341 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003342 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003343 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003344 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3345 return( ret );
3346
3347#if defined(MBEDTLS_SSL_PROTO_DTLS)
3348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3349 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3350 {
3351 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3352 return( ret );
3353 }
3354#endif /* MBEDTLS_SSL_PROTO_DTLS */
3355
3356 return( ret );
3357}
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003360{
Hanno Becker41934dd2021-08-07 19:13:43 +01003361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
Hanno Becker41934dd2021-08-07 19:13:43 +01003363 if( ssl == NULL ||
3364 ssl->conf == NULL ||
3365 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003366 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003367 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003369 }
3370
3371 ret = ssl_prepare_handshake_step( ssl );
3372 if( ret != 0 )
3373 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003374
Jerry Yue7047812021-09-13 19:26:39 +08003375 ret = mbedtls_ssl_handle_pending_alert( ssl );
3376 if( ret != 0 )
3377 goto cleanup;
3378
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003379 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3380 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3381 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003384 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003385 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3387 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003388
Ronald Cron9f0fba32022-02-10 16:45:15 +01003389 switch( ssl->state )
3390 {
3391 case MBEDTLS_SSL_HELLO_REQUEST:
3392 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003393 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003394 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003395
Ronald Cron9f0fba32022-02-10 16:45:15 +01003396 case MBEDTLS_SSL_CLIENT_HELLO:
3397 ret = mbedtls_ssl_write_client_hello( ssl );
3398 break;
3399
3400 default:
3401#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003402 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003403 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3404 else
3405 ret = mbedtls_ssl_handshake_client_step( ssl );
3406#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3407 ret = mbedtls_ssl_handshake_client_step( ssl );
3408#else
3409 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3410#endif
3411 }
Jerry Yub9930e72021-08-06 17:11:51 +08003412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003415 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003416 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003417#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003418 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003419 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003420#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003421
3422#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3423 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3424 ret = mbedtls_ssl_handshake_server_step( ssl );
3425#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003427#endif
3428
Jerry Yue7047812021-09-13 19:26:39 +08003429 if( ret != 0 )
3430 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003431 /* handshake_step return error. And it is same
3432 * with alert_reason.
3433 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003434 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003435 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003436 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003437 goto cleanup;
3438 }
3439 }
3440
3441cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003442 return( ret );
3443}
3444
3445/*
3446 * Perform the SSL handshake
3447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003449{
3450 int ret = 0;
3451
Hanno Beckera817ea42020-10-20 15:20:23 +01003452 /* Sanity checks */
3453
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003454 if( ssl == NULL || ssl->conf == NULL )
3455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3456
Hanno Beckera817ea42020-10-20 15:20:23 +01003457#if defined(MBEDTLS_SSL_PROTO_DTLS)
3458 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3459 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3460 {
3461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3462 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3464 }
3465#endif /* MBEDTLS_SSL_PROTO_DTLS */
3466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003468
Hanno Beckera817ea42020-10-20 15:20:23 +01003469 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003470 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003473
3474 if( ret != 0 )
3475 break;
3476 }
3477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003479
3480 return( ret );
3481}
3482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#if defined(MBEDTLS_SSL_RENEGOTIATION)
3484#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003485/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003486 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003487 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003488MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003490{
Janos Follath865b3eb2019-12-16 11:46:15 +00003491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003494
3495 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3497 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003498
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003499 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003500 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003502 return( ret );
3503 }
3504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003506
3507 return( 0 );
3508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003510
3511/*
3512 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 * - any side: calling mbedtls_ssl_renegotiate(),
3514 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3515 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003516 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003517 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003519 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003520int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003521{
Janos Follath865b3eb2019-12-16 11:46:15 +00003522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003525
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003526 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3527 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003528
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003529 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3530 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003532 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003534 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003536 ssl->handshake->out_msg_seq = 1;
3537 else
3538 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003539 }
3540#endif
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3543 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003548 return( ret );
3549 }
3550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003552
3553 return( 0 );
3554}
3555
3556/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003557 * Renegotiate current connection on client,
3558 * or request renegotiation on server
3559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003563
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003564 if( ssl == NULL || ssl->conf == NULL )
3565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003568 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003569 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003570 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003571 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003575
3576 /* Did we already try/start sending HelloRequest? */
3577 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003579
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003580 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003581 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003585 /*
3586 * On client, either start the renegotiation process or,
3587 * if already in progress, continue the handshake
3588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003590 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003591 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003593
Hanno Becker40cdaa12020-02-05 10:48:27 +00003594 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003595 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003597 return( ret );
3598 }
3599 }
3600 else
3601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003605 return( ret );
3606 }
3607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003609
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003610 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003613
Gilles Peskine9b562d52018-04-25 20:32:43 +02003614void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003615{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003616 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3617
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003618 if( handshake == NULL )
3619 return;
3620
Brett Warrene0edc842021-08-17 09:53:13 +01003621#if defined(MBEDTLS_ECP_C)
3622#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3623 if ( ssl->handshake->group_list_heap_allocated )
3624 mbedtls_free( (void*) handshake->group_list );
3625 handshake->group_list = NULL;
3626#endif /* MBEDTLS_DEPRECATED_REMOVED */
3627#endif /* MBEDTLS_ECP_C */
3628
Ronald Crone68ab4f2022-10-05 12:46:29 +02003629#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003630#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3631 if ( ssl->handshake->sig_algs_heap_allocated )
3632 mbedtls_free( (void*) handshake->sig_algs );
3633 handshake->sig_algs = NULL;
3634#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003635#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3636 if( ssl->handshake->certificate_request_context )
3637 {
3638 mbedtls_free( (void*) handshake->certificate_request_context );
3639 }
3640#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003641#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003642
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003643#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3644 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3645 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003646 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003647 handshake->async_in_progress = 0;
3648 }
3649#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3650
Andrzej Kurek25f27152022-08-17 16:09:31 -04003651#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003652#if defined(MBEDTLS_USE_PSA_CRYPTO)
3653 psa_hash_abort( &handshake->fin_sha256_psa );
3654#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003655 mbedtls_sha256_free( &handshake->fin_sha256 );
3656#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003657#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003658#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003659#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003660 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003661#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003662 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003663#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003664#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#if defined(MBEDTLS_DHM_C)
3667 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003668#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003669#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003671#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003672#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003673 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003674#if defined(MBEDTLS_SSL_CLI_C)
3675 mbedtls_free( handshake->ecjpake_cache );
3676 handshake->ecjpake_cache = NULL;
3677 handshake->ecjpake_cache_len = 0;
3678#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003679#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003680
Janos Follath4ae5c292016-02-10 11:27:43 +00003681#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3682 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003683 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003685#endif
3686
Ronald Cron73fe8df2022-10-05 14:31:43 +02003687#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003688#if defined(MBEDTLS_USE_PSA_CRYPTO)
3689 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3690 {
3691 /* The maintenance of the external PSK key slot is the
3692 * user's responsibility. */
3693 if( ssl->handshake->psk_opaque_is_internal )
3694 {
3695 psa_destroy_key( ssl->handshake->psk_opaque );
3696 ssl->handshake->psk_opaque_is_internal = 0;
3697 }
3698 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3699 }
Neil Armstronge952a302022-05-03 10:22:14 +02003700#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003701 if( handshake->psk != NULL )
3702 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003703 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003704 mbedtls_free( handshake->psk );
3705 }
Neil Armstronge952a302022-05-03 10:22:14 +02003706#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003707#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3710 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003711 /*
3712 * Free only the linked list wrapper, not the keys themselves
3713 * since the belong to the SNI callback
3714 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003715 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003717
Gilles Peskineeccd8882020-03-10 12:19:08 +01003718#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003719 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003720 if( handshake->ecrs_peer_cert != NULL )
3721 {
3722 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3723 mbedtls_free( handshake->ecrs_peer_cert );
3724 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003725#endif
3726
Hanno Becker75173122019-02-06 16:18:31 +00003727#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3728 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3729 mbedtls_pk_free( &handshake->peer_pubkey );
3730#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3731
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003732#if defined(MBEDTLS_SSL_CLI_C) && \
3733 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3734 mbedtls_free( handshake->cookie );
3735#endif /* MBEDTLS_SSL_CLI_C &&
3736 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003737
3738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003739 mbedtls_ssl_flight_free( handshake->flight );
3740 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003741#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003742
Ronald Cronf12b81d2022-03-15 10:42:41 +01003743#if defined(MBEDTLS_ECDH_C) && \
3744 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003745 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003746 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003747#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3748
Ronald Cron6f135e12021-12-08 16:57:54 +01003749#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003750 mbedtls_ssl_transform_free( handshake->transform_handshake );
3751 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003752 mbedtls_free( handshake->transform_earlydata );
3753 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003754#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003755
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003756
3757#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3758 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3759 * processes datagrams and the fact that a datagram is allowed to have
3760 * several records in it, it is possible that the I/O buffers are not
3761 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003762 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3763 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003764#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003765
Jerry Yuba9c7272021-10-30 11:54:10 +08003766 /* mbedtls_platform_zeroize MUST be last one in this function */
3767 mbedtls_platform_zeroize( handshake,
3768 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003769}
3770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003772{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003773 if( session == NULL )
3774 return;
3775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003777 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003778#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003779
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003780#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003781#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3782 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003783 mbedtls_free( session->hostname );
3784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003786#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003787
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003788 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003789}
3790
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003791#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003792
3793#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3794#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3795#else
3796#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3797#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3798
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003799#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003800
3801#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3802#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3803#else
3804#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3805#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3806
3807#if defined(MBEDTLS_SSL_ALPN)
3808#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3809#else
3810#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3811#endif /* MBEDTLS_SSL_ALPN */
3812
3813#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3814#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3815#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3816#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3817
3818#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3819 ( (uint32_t) ( \
3820 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3821 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3822 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3823 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3824 0u ) )
3825
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003826static unsigned char ssl_serialized_context_header[] = {
3827 MBEDTLS_VERSION_MAJOR,
3828 MBEDTLS_VERSION_MINOR,
3829 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003830 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3831 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3832 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3833 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3834 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003835};
3836
Paul Bakker5121ce52009-01-03 21:22:43 +00003837/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003838 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003839 *
3840 * The format of the serialized data is:
3841 * (in the presentation language of TLS, RFC 8446 section 3)
3842 *
3843 * // header
3844 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003845 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003846 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003847 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003848 * Note: When updating the format, remember to keep these
3849 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003850 *
3851 * // session sub-structure
3852 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3853 * // transform sub-structure
3854 * uint8 random[64]; // ServerHello.random+ClientHello.random
3855 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3856 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3857 * // fields from ssl_context
3858 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3859 * uint64 in_window_top; // DTLS: last validated record seq_num
3860 * uint64 in_window; // DTLS: bitmask for replay protection
3861 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3862 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3863 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3864 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3865 *
3866 * Note that many fields of the ssl_context or sub-structures are not
3867 * serialized, as they fall in one of the following categories:
3868 *
3869 * 1. forced value (eg in_left must be 0)
3870 * 2. pointer to dynamically-allocated memory (eg session, transform)
3871 * 3. value can be re-derived from other data (eg session keys from MS)
3872 * 4. value was temporary (eg content of input buffer)
3873 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003874 */
3875int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3876 unsigned char *buf,
3877 size_t buf_len,
3878 size_t *olen )
3879{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003880 unsigned char *p = buf;
3881 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003882 size_t session_len;
3883 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003884
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003885 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003886 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3887 * this function's documentation.
3888 *
3889 * These are due to assumptions/limitations in the implementation. Some of
3890 * them are likely to stay (no handshake in progress) some might go away
3891 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003892 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003893 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003894 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003895 {
3896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003898 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003899 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003900 {
3901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003903 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003904 /* Double-check that sub-structures are indeed ready */
3905 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003906 {
3907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003909 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003910 /* There must be no pending incoming or outgoing data */
3911 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003912 {
3913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003915 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003916 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003917 {
3918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003920 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003921 /* Protocol must be DLTS, not TLS */
3922 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003923 {
3924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003926 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003927 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003928 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003929 {
3930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003932 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003933 /* We must be using an AEAD ciphersuite */
3934 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003935 {
3936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003938 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003939 /* Renegotiation must not be enabled */
3940#if defined(MBEDTLS_SSL_RENEGOTIATION)
3941 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003942 {
3943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003945 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003946#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003947
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003948 /*
3949 * Version and format identifier
3950 */
3951 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003952
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003953 if( used <= buf_len )
3954 {
3955 memcpy( p, ssl_serialized_context_header,
3956 sizeof( ssl_serialized_context_header ) );
3957 p += sizeof( ssl_serialized_context_header );
3958 }
3959
3960 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003961 * Session (length + data)
3962 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003963 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003964 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3965 return( ret );
3966
3967 used += 4 + session_len;
3968 if( used <= buf_len )
3969 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003970 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3971 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003972
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003973 ret = ssl_session_save( ssl->session, 1,
3974 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003975 if( ret != 0 )
3976 return( ret );
3977
3978 p += session_len;
3979 }
3980
3981 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003982 * Transform
3983 */
3984 used += sizeof( ssl->transform->randbytes );
3985 if( used <= buf_len )
3986 {
3987 memcpy( p, ssl->transform->randbytes,
3988 sizeof( ssl->transform->randbytes ) );
3989 p += sizeof( ssl->transform->randbytes );
3990 }
3991
3992#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3993 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3994 if( used <= buf_len )
3995 {
3996 *p++ = ssl->transform->in_cid_len;
3997 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3998 p += ssl->transform->in_cid_len;
3999
4000 *p++ = ssl->transform->out_cid_len;
4001 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4002 p += ssl->transform->out_cid_len;
4003 }
4004#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4005
4006 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004007 * Saved fields from top-level ssl_context structure
4008 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004009 used += 4;
4010 if( used <= buf_len )
4011 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004012 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4013 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004014 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004015
4016#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4017 used += 16;
4018 if( used <= buf_len )
4019 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004020 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4021 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004022
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004023 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4024 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004025 }
4026#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4027
4028#if defined(MBEDTLS_SSL_PROTO_DTLS)
4029 used += 1;
4030 if( used <= buf_len )
4031 {
4032 *p++ = ssl->disable_datagram_packing;
4033 }
4034#endif /* MBEDTLS_SSL_PROTO_DTLS */
4035
Jerry Yuae0b2e22021-10-08 15:21:19 +08004036 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004037 if( used <= buf_len )
4038 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004039 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4040 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004041 }
4042
4043#if defined(MBEDTLS_SSL_PROTO_DTLS)
4044 used += 2;
4045 if( used <= buf_len )
4046 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004047 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4048 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004049 }
4050#endif /* MBEDTLS_SSL_PROTO_DTLS */
4051
4052#if defined(MBEDTLS_SSL_ALPN)
4053 {
4054 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004055 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004056 : 0;
4057
4058 used += 1 + alpn_len;
4059 if( used <= buf_len )
4060 {
4061 *p++ = alpn_len;
4062
4063 if( ssl->alpn_chosen != NULL )
4064 {
4065 memcpy( p, ssl->alpn_chosen, alpn_len );
4066 p += alpn_len;
4067 }
4068 }
4069 }
4070#endif /* MBEDTLS_SSL_ALPN */
4071
4072 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004073 * Done
4074 */
4075 *olen = used;
4076
4077 if( used > buf_len )
4078 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004079
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004080 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4081
Hanno Becker43aefe22020-02-05 10:44:56 +00004082 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004083}
4084
4085/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004086 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004087 *
4088 * This internal version is wrapped by a public function that cleans up in
4089 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004090 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004091MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004092static int ssl_context_load( mbedtls_ssl_context *ssl,
4093 const unsigned char *buf,
4094 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004095{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004096 const unsigned char *p = buf;
4097 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004098 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004100#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004101 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004102#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004103
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004104 /*
4105 * The context should have been freshly setup or reset.
4106 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004107 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004108 * renegotiating, or if the user mistakenly loaded a session first.)
4109 */
4110 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4111 ssl->session != NULL )
4112 {
4113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4114 }
4115
4116 /*
4117 * We can't check that the config matches the initial one, but we can at
4118 * least check it matches the requirements for serializing.
4119 */
4120 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004121 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4122 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004123#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004124 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004125#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004126 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004127 {
4128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4129 }
4130
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004131 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4132
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004133 /*
4134 * Check version identifier
4135 */
4136 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4138
4139 if( memcmp( p, ssl_serialized_context_header,
4140 sizeof( ssl_serialized_context_header ) ) != 0 )
4141 {
4142 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4143 }
4144 p += sizeof( ssl_serialized_context_header );
4145
4146 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004147 * Session
4148 */
4149 if( (size_t)( end - p ) < 4 )
4150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4151
4152 session_len = ( (size_t) p[0] << 24 ) |
4153 ( (size_t) p[1] << 16 ) |
4154 ( (size_t) p[2] << 8 ) |
4155 ( (size_t) p[3] );
4156 p += 4;
4157
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004158 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004159 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004160 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004161 ssl->session_in = ssl->session;
4162 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004163 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004164
4165 if( (size_t)( end - p ) < session_len )
4166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4167
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004168 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004169 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004170 {
4171 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004172 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004173 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004174
4175 p += session_len;
4176
4177 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004178 * Transform
4179 */
4180
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004181 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004182 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004183 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004184 ssl->transform_in = ssl->transform;
4185 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004186 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004187
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004188#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004189 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4190 if( prf_func == NULL )
4191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4192
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004193 /* Read random bytes and populate structure */
4194 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004196
Hanno Beckerbd257552021-03-22 06:59:27 +00004197 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004198 ssl->session->ciphersuite,
4199 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004200#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004201 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004202#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004203 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004204 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004205 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004206 ssl->conf->endpoint,
4207 ssl );
4208 if( ret != 0 )
4209 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004210#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004211 p += sizeof( ssl->transform->randbytes );
4212
4213#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4214 /* Read connection IDs and store them */
4215 if( (size_t)( end - p ) < 1 )
4216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4217
4218 ssl->transform->in_cid_len = *p++;
4219
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004220 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4222
4223 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4224 p += ssl->transform->in_cid_len;
4225
4226 ssl->transform->out_cid_len = *p++;
4227
4228 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4230
4231 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4232 p += ssl->transform->out_cid_len;
4233#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4234
4235 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004236 * Saved fields from top-level ssl_context structure
4237 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004238 if( (size_t)( end - p ) < 4 )
4239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4240
4241 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4242 ( (uint32_t) p[1] << 16 ) |
4243 ( (uint32_t) p[2] << 8 ) |
4244 ( (uint32_t) p[3] );
4245 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004246
4247#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4248 if( (size_t)( end - p ) < 16 )
4249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4250
4251 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4252 ( (uint64_t) p[1] << 48 ) |
4253 ( (uint64_t) p[2] << 40 ) |
4254 ( (uint64_t) p[3] << 32 ) |
4255 ( (uint64_t) p[4] << 24 ) |
4256 ( (uint64_t) p[5] << 16 ) |
4257 ( (uint64_t) p[6] << 8 ) |
4258 ( (uint64_t) p[7] );
4259 p += 8;
4260
4261 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4262 ( (uint64_t) p[1] << 48 ) |
4263 ( (uint64_t) p[2] << 40 ) |
4264 ( (uint64_t) p[3] << 32 ) |
4265 ( (uint64_t) p[4] << 24 ) |
4266 ( (uint64_t) p[5] << 16 ) |
4267 ( (uint64_t) p[6] << 8 ) |
4268 ( (uint64_t) p[7] );
4269 p += 8;
4270#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4271
4272#if defined(MBEDTLS_SSL_PROTO_DTLS)
4273 if( (size_t)( end - p ) < 1 )
4274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4275
4276 ssl->disable_datagram_packing = *p++;
4277#endif /* MBEDTLS_SSL_PROTO_DTLS */
4278
Jerry Yud9a94fe2021-09-28 18:58:59 +08004279 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004281 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4282 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004283
4284#if defined(MBEDTLS_SSL_PROTO_DTLS)
4285 if( (size_t)( end - p ) < 2 )
4286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4287
4288 ssl->mtu = ( p[0] << 8 ) | p[1];
4289 p += 2;
4290#endif /* MBEDTLS_SSL_PROTO_DTLS */
4291
4292#if defined(MBEDTLS_SSL_ALPN)
4293 {
4294 uint8_t alpn_len;
4295 const char **cur;
4296
4297 if( (size_t)( end - p ) < 1 )
4298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4299
4300 alpn_len = *p++;
4301
4302 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4303 {
4304 /* alpn_chosen should point to an item in the configured list */
4305 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4306 {
4307 if( strlen( *cur ) == alpn_len &&
4308 memcmp( p, cur, alpn_len ) == 0 )
4309 {
4310 ssl->alpn_chosen = *cur;
4311 break;
4312 }
4313 }
4314 }
4315
4316 /* can only happen on conf mismatch */
4317 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4319
4320 p += alpn_len;
4321 }
4322#endif /* MBEDTLS_SSL_ALPN */
4323
4324 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004325 * Forced fields from top-level ssl_context structure
4326 *
4327 * Most of them already set to the correct value by mbedtls_ssl_init() and
4328 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4329 */
4330 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004331 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004332
Hanno Becker361b10d2019-08-30 10:42:49 +01004333 /* Adjust pointers for header fields of outgoing records to
4334 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004335 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004336
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004337#if defined(MBEDTLS_SSL_PROTO_DTLS)
4338 ssl->in_epoch = 1;
4339#endif
4340
4341 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4342 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004343 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4344 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004345 if( ssl->handshake != NULL )
4346 {
4347 mbedtls_ssl_handshake_free( ssl );
4348 mbedtls_free( ssl->handshake );
4349 ssl->handshake = NULL;
4350 }
4351
4352 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004353 * Done - should have consumed entire buffer
4354 */
4355 if( p != end )
4356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004357
4358 return( 0 );
4359}
4360
4361/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004362 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004363 */
4364int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4365 const unsigned char *buf,
4366 size_t len )
4367{
4368 int ret = ssl_context_load( context, buf, len );
4369
4370 if( ret != 0 )
4371 mbedtls_ssl_free( context );
4372
4373 return( ret );
4374}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004375#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004376
4377/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004378 * Free an SSL context
4379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004381{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004382 if( ssl == NULL )
4383 return;
4384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004387 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004388 {
sander-visserb8aa2072020-05-06 22:05:13 +02004389#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4390 size_t out_buf_len = ssl->out_buf_len;
4391#else
4392 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4393#endif
4394
Darryl Greenb33cc762019-11-28 14:29:44 +00004395 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004397 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004398 }
4399
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004400 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 {
sander-visserb8aa2072020-05-06 22:05:13 +02004402#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4403 size_t in_buf_len = ssl->in_buf_len;
4404#else
4405 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4406#endif
4407
Darryl Greenb33cc762019-11-28 14:29:44 +00004408 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004410 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 }
4412
Paul Bakker48916f92012-09-16 19:57:18 +00004413 if( ssl->transform )
4414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415 mbedtls_ssl_transform_free( ssl->transform );
4416 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004417 }
4418
4419 if( ssl->handshake )
4420 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004421 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4423 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 mbedtls_free( ssl->handshake );
4426 mbedtls_free( ssl->transform_negotiate );
4427 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004428 }
4429
Ronald Cron6f135e12021-12-08 16:57:54 +01004430#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004431 mbedtls_ssl_transform_free( ssl->transform_application );
4432 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004433#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004434
Paul Bakkerc0463502013-02-14 11:19:38 +01004435 if( ssl->session )
4436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 mbedtls_ssl_session_free( ssl->session );
4438 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004439 }
4440
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004441#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004442 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004443 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004444 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004447#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004448
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004449#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004451#endif
4452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004454
Paul Bakker86f04f42013-02-14 11:20:09 +01004455 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004456 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004457}
4458
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004459/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004460 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004461 */
4462void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4463{
4464 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4465}
4466
Gilles Peskineae270bf2021-06-02 00:05:29 +02004467/* The selection should be the same as mbedtls_x509_crt_profile_default in
4468 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004469 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004470 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004471 * about this list.
4472 */
Brett Warrene0edc842021-08-17 09:53:13 +01004473static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004474#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004475 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004476#endif
4477#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004478 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004479#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004480#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004481 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004482#endif
4483#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004484 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004485#endif
4486#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004487 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004488#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004489#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004490 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004491#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004492#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004493 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004494#endif
4495#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004496 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004497#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004498 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004499};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004500
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004501static int ssl_preset_suiteb_ciphersuites[] = {
4502 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4503 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4504 0
4505};
4506
Ronald Crone68ab4f2022-10-05 12:46:29 +02004507#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004508
Jerry Yu909df7b2022-01-22 11:56:27 +08004509/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004510 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004511 * rules SHOULD be upheld.
4512 * - No duplicate entries.
4513 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004514 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004515 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004516 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004517static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004518
Andrzej Kurek25f27152022-08-17 16:09:31 -04004519#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 +08004520 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4521 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004522#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004523 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004524
Andrzej Kurek25f27152022-08-17 16:09:31 -04004525#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 +08004526 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4527 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004528#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004529 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4530
Andrzej Kurek25f27152022-08-17 16:09:31 -04004531#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 +08004532 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4533 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004534#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004535 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004536
Andrzej Kurek25f27152022-08-17 16:09:31 -04004537#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 +08004538 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004539#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 +08004540
Andrzej Kurek25f27152022-08-17 16:09:31 -04004541#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 +08004542 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004543#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 +08004544
Andrzej Kurek25f27152022-08-17 16:09:31 -04004545#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 +08004546 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004547#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 +08004548
Andrzej Kurek25f27152022-08-17 16:09:31 -04004549#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 +08004550 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4551#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4552
Andrzej Kurek25f27152022-08-17 16:09:31 -04004553#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 +08004554 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4555#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4556
Andrzej Kurek25f27152022-08-17 16:09:31 -04004557#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 +08004558 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4559#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4560
Gabor Mezei15b95a62022-05-09 16:37:58 +02004561 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004562};
4563
4564/* NOTICE: see above */
4565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4566static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004567#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004568#if defined(MBEDTLS_ECDSA_C)
4569 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004570#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004571#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4572 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4573#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004574#if defined(MBEDTLS_RSA_C)
4575 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4576#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004577#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004578#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004579#if defined(MBEDTLS_ECDSA_C)
4580 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004581#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004582#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4583 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4584#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004585#if defined(MBEDTLS_RSA_C)
4586 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4587#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004588#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004589#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004590#if defined(MBEDTLS_ECDSA_C)
4591 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004592#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004593#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4594 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4595#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004596#if defined(MBEDTLS_RSA_C)
4597 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4598#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004599#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004600 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004601};
4602#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4603/* NOTICE: see above */
4604static uint16_t ssl_preset_suiteb_sig_algs[] = {
4605
Andrzej Kurek25f27152022-08-17 16:09:31 -04004606#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 +08004607 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4608 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004609#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004610 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4611
Andrzej Kurek25f27152022-08-17 16:09:31 -04004612#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 +08004613 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4614 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004615#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004616 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004617
Andrzej Kurek25f27152022-08-17 16:09:31 -04004618#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 +08004619 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004620#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 +08004621
Andrzej Kurek25f27152022-08-17 16:09:31 -04004622#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 +08004623 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004624#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004625
Gabor Mezei15b95a62022-05-09 16:37:58 +02004626 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004627};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004628
Jerry Yu909df7b2022-01-22 11:56:27 +08004629/* NOTICE: see above */
4630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4631static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004632#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004633#if defined(MBEDTLS_ECDSA_C)
4634 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004635#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004636#if defined(MBEDTLS_RSA_C)
4637 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4638#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004639#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004640#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004641#if defined(MBEDTLS_ECDSA_C)
4642 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004643#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004644#if defined(MBEDTLS_RSA_C)
4645 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4646#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004647#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004648 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004649};
Jerry Yu909df7b2022-01-22 11:56:27 +08004650#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4651
Ronald Crone68ab4f2022-10-05 12:46:29 +02004652#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004653
Brett Warrene0edc842021-08-17 09:53:13 +01004654static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004655#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004656 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004657#endif
4658#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004659 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004660#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004661 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004662};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004663
Ronald Crone68ab4f2022-10-05 12:46:29 +02004664#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004665/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004666 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004667MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004668static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004669{
4670 size_t i, j;
4671 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004672
Gabor Mezei15b95a62022-05-09 16:37:58 +02004673 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004674 {
Jerry Yuf377d642022-01-25 10:43:59 +08004675 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004676 {
Jerry Yuf377d642022-01-25 10:43:59 +08004677 if( sig_algs[i] != sig_algs[j] )
4678 continue;
4679 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4680 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4681 sig_algs[i], j, i );
4682 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004683 }
4684 }
4685 return( ret );
4686}
4687
Ronald Crone68ab4f2022-10-05 12:46:29 +02004688#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004689
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004690/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004691 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004692 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004693int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004694 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004695{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004696#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004698#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004699
Ronald Crone68ab4f2022-10-05 12:46:29 +02004700#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004701 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004702 {
4703 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4704 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4705 }
4706
Jerry Yuf377d642022-01-25 10:43:59 +08004707 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004708 {
4709 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4710 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4711 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004712
4713#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004714 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004715 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004716 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004717 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4718 }
4719
Jerry Yuf377d642022-01-25 10:43:59 +08004720 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004721 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004722 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004723 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4724 }
4725#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004726#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004727
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004728 /* Use the functions here so that they are covered in tests,
4729 * but otherwise access member directly for efficiency */
4730 mbedtls_ssl_conf_endpoint( conf, endpoint );
4731 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004732
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004733 /*
4734 * Things that are common to all presets
4735 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004736#if defined(MBEDTLS_SSL_CLI_C)
4737 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4738 {
4739 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4740#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4741 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4742#endif
4743 }
4744#endif
4745
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004746#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4747 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4748#endif
4749
4750#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4751 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4752#endif
4753
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004754#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004755 conf->f_cookie_write = ssl_cookie_write_dummy;
4756 conf->f_cookie_check = ssl_cookie_check_dummy;
4757#endif
4758
4759#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4760 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4761#endif
4762
Janos Follath088ce432017-04-10 12:42:31 +01004763#if defined(MBEDTLS_SSL_SRV_C)
4764 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004765 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004766#endif
4767
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004768#if defined(MBEDTLS_SSL_PROTO_DTLS)
4769 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4770 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4771#endif
4772
4773#if defined(MBEDTLS_SSL_RENEGOTIATION)
4774 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004775 memset( conf->renego_period, 0x00, 2 );
4776 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004777#endif
4778
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004779#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004780 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4781 {
4782 const unsigned char dhm_p[] =
4783 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4784 const unsigned char dhm_g[] =
4785 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004786
Hanno Beckere2defad2021-07-24 05:59:17 +01004787 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4788 dhm_p, sizeof( dhm_p ),
4789 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4790 {
4791 return( ret );
4792 }
4793 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004794#endif
4795
Ronald Cron6f135e12021-12-08 16:57:54 +01004796#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004797#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004798 mbedtls_ssl_conf_new_session_tickets(
4799 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4800#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004801 /*
4802 * Allow all TLS 1.3 key exchange modes by default.
4803 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004804 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004805#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004806
XiaokangQian4d3a6042022-04-21 13:46:17 +00004807 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4808 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004809#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004810 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004811 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004812#else
XiaokangQian060d8672022-04-21 09:24:56 +00004813 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004814#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004815 }
4816 else
4817 {
4818#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4819 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4820 {
4821 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4822 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4823 }
4824 else
4825 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4826 {
4827 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4828 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4829 }
4830#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4831 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4832 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4833#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4834 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4835 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4836#else
4837 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4838#endif
4839 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004840
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004841 /*
4842 * Preset-specific defaults
4843 */
4844 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004845 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004846 /*
4847 * NSA Suite B
4848 */
4849 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004850
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004851 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004852
4853#if defined(MBEDTLS_X509_CRT_PARSE_C)
4854 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004855#endif
4856
Ronald Crone68ab4f2022-10-05 12:46:29 +02004857#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004858#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4859 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4860 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4861 else
4862#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4863 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004864#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004865
Brett Warrene0edc842021-08-17 09:53:13 +01004866#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4867 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004868#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004869 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004870 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004871
4872 /*
4873 * Default
4874 */
4875 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004876
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004877 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004878
4879#if defined(MBEDTLS_X509_CRT_PARSE_C)
4880 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4881#endif
4882
Ronald Crone68ab4f2022-10-05 12:46:29 +02004883#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004884#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4885 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4886 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4887 else
4888#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4889 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004890#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004891
Brett Warrene0edc842021-08-17 09:53:13 +01004892#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4893 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004894#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004895 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004896
4897#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4898 conf->dhm_min_bitlen = 1024;
4899#endif
4900 }
4901
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004902 return( 0 );
4903}
4904
4905/*
4906 * Free mbedtls_ssl_config
4907 */
4908void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4909{
4910#if defined(MBEDTLS_DHM_C)
4911 mbedtls_mpi_free( &conf->dhm_P );
4912 mbedtls_mpi_free( &conf->dhm_G );
4913#endif
4914
Ronald Cron73fe8df2022-10-05 14:31:43 +02004915#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004916#if defined(MBEDTLS_USE_PSA_CRYPTO)
4917 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4918 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004919 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4920 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004921#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004922 if( conf->psk != NULL )
4923 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004924 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004925 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004926 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004927 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004928 }
4929
4930 if( conf->psk_identity != NULL )
4931 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004932 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004933 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004934 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004935 conf->psk_identity_len = 0;
4936 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02004937#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004938
4939#if defined(MBEDTLS_X509_CRT_PARSE_C)
4940 ssl_key_cert_free( conf->key_cert );
4941#endif
4942
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004943 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004944}
4945
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004946#if defined(MBEDTLS_PK_C) && \
4947 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004948/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004952{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953#if defined(MBEDTLS_RSA_C)
4954 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4955 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957#if defined(MBEDTLS_ECDSA_C)
4958 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4959 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004962}
4963
Hanno Becker7e5437a2017-04-28 17:15:26 +01004964unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4965{
4966 switch( type ) {
4967 case MBEDTLS_PK_RSA:
4968 return( MBEDTLS_SSL_SIG_RSA );
4969 case MBEDTLS_PK_ECDSA:
4970 case MBEDTLS_PK_ECKEY:
4971 return( MBEDTLS_SSL_SIG_ECDSA );
4972 default:
4973 return( MBEDTLS_SSL_SIG_ANON );
4974 }
4975}
4976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004977mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004978{
4979 switch( sig )
4980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981#if defined(MBEDTLS_RSA_C)
4982 case MBEDTLS_SSL_SIG_RSA:
4983 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985#if defined(MBEDTLS_ECDSA_C)
4986 case MBEDTLS_SSL_SIG_ECDSA:
4987 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004988#endif
4989 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004991 }
4992}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004993#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004994
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004995/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004996 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004999{
5000 switch( hash )
5001 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005002#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 case MBEDTLS_SSL_HASH_MD5:
5004 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005005#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005006#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 case MBEDTLS_SSL_HASH_SHA1:
5008 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005009#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005010#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 case MBEDTLS_SSL_HASH_SHA224:
5012 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005013#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005014#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 case MBEDTLS_SSL_HASH_SHA256:
5016 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005017#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005018#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 case MBEDTLS_SSL_HASH_SHA384:
5020 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005021#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005022#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 case MBEDTLS_SSL_HASH_SHA512:
5024 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005025#endif
5026 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005028 }
5029}
5030
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005031/*
5032 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5033 */
5034unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5035{
5036 switch( md )
5037 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005038#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005039 case MBEDTLS_MD_MD5:
5040 return( MBEDTLS_SSL_HASH_MD5 );
5041#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005042#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005043 case MBEDTLS_MD_SHA1:
5044 return( MBEDTLS_SSL_HASH_SHA1 );
5045#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005046#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005047 case MBEDTLS_MD_SHA224:
5048 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005049#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005050#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005051 case MBEDTLS_MD_SHA256:
5052 return( MBEDTLS_SSL_HASH_SHA256 );
5053#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005054#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005055 case MBEDTLS_MD_SHA384:
5056 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005057#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005058#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005059 case MBEDTLS_MD_SHA512:
5060 return( MBEDTLS_SSL_HASH_SHA512 );
5061#endif
5062 default:
5063 return( MBEDTLS_SSL_HASH_NONE );
5064 }
5065}
5066
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005067/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005068 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005069 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005070 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005071int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005072{
Brett Warrene0edc842021-08-17 09:53:13 +01005073 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005074
Brett Warrene0edc842021-08-17 09:53:13 +01005075 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005076 return( -1 );
5077
Brett Warrene0edc842021-08-17 09:53:13 +01005078 for( ; *group_list != 0; group_list++ )
5079 {
5080 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005081 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005082 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005083
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005084 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005085}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005086
5087#if defined(MBEDTLS_ECP_C)
5088/*
5089 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5090 */
5091int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5092{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005093 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005094 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005095
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005096 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005097 return -1;
5098
5099 uint16_t tls_id = grp_info->tls_id;
5100
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005101 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5102}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005103#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105#if defined(MBEDTLS_X509_CRT_PARSE_C)
5106int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5107 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005108 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005109 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005110{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005111 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005112 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005113 const char *ext_oid;
5114 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005117 {
5118 /* Server part of the key exchange */
5119 switch( ciphersuite->key_exchange )
5120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 case MBEDTLS_KEY_EXCHANGE_RSA:
5122 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005123 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005124 break;
5125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5127 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5128 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5129 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005130 break;
5131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5133 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005134 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005135 break;
5136
5137 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 case MBEDTLS_KEY_EXCHANGE_NONE:
5139 case MBEDTLS_KEY_EXCHANGE_PSK:
5140 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5141 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005142 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005143 usage = 0;
5144 }
5145 }
5146 else
5147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5149 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005150 }
5151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005153 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005154 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005155 ret = -1;
5156 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5161 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005162 }
5163 else
5164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5166 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005167 }
5168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005170 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005171 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005172 ret = -1;
5173 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005174
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005175 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005178
Jerry Yu148165c2021-09-24 23:20:59 +08005179#if defined(MBEDTLS_USE_PSA_CRYPTO)
5180int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5181 const mbedtls_md_type_t md,
5182 unsigned char *dst,
5183 size_t dst_len,
5184 size_t *olen )
5185{
Ronald Cronf6893e12022-01-07 22:09:01 +01005186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5187 psa_hash_operation_t *hash_operation_to_clone;
5188 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5189
Jerry Yu148165c2021-09-24 23:20:59 +08005190 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005191
5192 switch( md )
5193 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005194#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005195 case MBEDTLS_MD_SHA384:
5196 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5197 break;
5198#endif
5199
Andrzej Kurek25f27152022-08-17 16:09:31 -04005200#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005201 case MBEDTLS_MD_SHA256:
5202 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5203 break;
5204#endif
5205
5206 default:
5207 goto exit;
5208 }
5209
5210 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5211 if( status != PSA_SUCCESS )
5212 goto exit;
5213
5214 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5215 if( status != PSA_SUCCESS )
5216 goto exit;
5217
5218exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005219#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5220 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5221 (void) ssl;
5222#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005223 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005224}
5225#else /* MBEDTLS_USE_PSA_CRYPTO */
5226
Andrzej Kurek25f27152022-08-17 16:09:31 -04005227#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005228MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005229static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5230 unsigned char *dst,
5231 size_t dst_len,
5232 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005233{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005234 int ret;
5235 mbedtls_sha512_context sha512;
5236
5237 if( dst_len < 48 )
5238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5239
5240 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005241 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005242
5243 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5244 {
5245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5246 goto exit;
5247 }
5248
5249 *olen = 48;
5250
5251exit:
5252
5253 mbedtls_sha512_free( &sha512 );
5254 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005255}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005256#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005257
Andrzej Kurek25f27152022-08-17 16:09:31 -04005258#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005259MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005260static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5261 unsigned char *dst,
5262 size_t dst_len,
5263 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005264{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005265 int ret;
5266 mbedtls_sha256_context sha256;
5267
5268 if( dst_len < 32 )
5269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5270
5271 mbedtls_sha256_init( &sha256 );
5272 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005273
Jerry Yu24c0ec32021-09-09 14:21:07 +08005274 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5275 {
5276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5277 goto exit;
5278 }
5279
5280 *olen = 32;
5281
5282exit:
5283
5284 mbedtls_sha256_free( &sha256 );
5285 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005286}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005287#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005288
Jerry Yu000f9762021-09-14 11:12:51 +08005289int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5290 const mbedtls_md_type_t md,
5291 unsigned char *dst,
5292 size_t dst_len,
5293 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005294{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005295 switch( md )
5296 {
5297
Andrzej Kurek25f27152022-08-17 16:09:31 -04005298#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005299 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005300 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005301#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005302
Andrzej Kurek25f27152022-08-17 16:09:31 -04005303#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005304 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005305 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005306#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005307
5308 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005309#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5310 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5311 (void) ssl;
5312 (void) dst;
5313 (void) dst_len;
5314 (void) olen;
5315#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005316 break;
5317 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005319}
XiaokangQian647719a2021-12-07 09:16:29 +00005320
Jerry Yu148165c2021-09-24 23:20:59 +08005321#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005322
Ronald Crone68ab4f2022-10-05 12:46:29 +02005323#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005324/* mbedtls_ssl_parse_sig_alg_ext()
5325 *
5326 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5327 * value (TLS 1.3 RFC8446):
5328 * enum {
5329 * ....
5330 * ecdsa_secp256r1_sha256( 0x0403 ),
5331 * ecdsa_secp384r1_sha384( 0x0503 ),
5332 * ecdsa_secp521r1_sha512( 0x0603 ),
5333 * ....
5334 * } SignatureScheme;
5335 *
5336 * struct {
5337 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5338 * } SignatureSchemeList;
5339 *
5340 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5341 * value (TLS 1.2 RFC5246):
5342 * enum {
5343 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5344 * sha512(6), (255)
5345 * } HashAlgorithm;
5346 *
5347 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5348 * SignatureAlgorithm;
5349 *
5350 * struct {
5351 * HashAlgorithm hash;
5352 * SignatureAlgorithm signature;
5353 * } SignatureAndHashAlgorithm;
5354 *
5355 * SignatureAndHashAlgorithm
5356 * supported_signature_algorithms<2..2^16-2>;
5357 *
5358 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5359 * generalization of the TLS 1.2 signature algorithm extension.
5360 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5361 * `SignatureScheme` field of TLS 1.3
5362 *
5363 */
5364int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5365 const unsigned char *buf,
5366 const unsigned char *end )
5367{
5368 const unsigned char *p = buf;
5369 size_t supported_sig_algs_len = 0;
5370 const unsigned char *supported_sig_algs_end;
5371 uint16_t sig_alg;
5372 uint32_t common_idx = 0;
5373
5374 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5375 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5376 p += 2;
5377
5378 memset( ssl->handshake->received_sig_algs, 0,
5379 sizeof(ssl->handshake->received_sig_algs) );
5380
5381 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5382 supported_sig_algs_end = p + supported_sig_algs_len;
5383 while( p < supported_sig_algs_end )
5384 {
5385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5386 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5387 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005388 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5389 sig_alg,
5390 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005392 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005393 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5394 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5395 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005396 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005397 }
5398#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005399
Jerry Yuf3b46b52022-06-19 16:52:27 +08005400 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5401 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005402
5403 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5404 {
5405 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5406 common_idx += 1;
5407 }
5408 }
5409 /* Check that we consumed all the message. */
5410 if( p != end )
5411 {
5412 MBEDTLS_SSL_DEBUG_MSG( 1,
5413 ( "Signature algorithms extension length misaligned" ) );
5414 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5415 MBEDTLS_ERR_SSL_DECODE_ERROR );
5416 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5417 }
5418
5419 if( common_idx == 0 )
5420 {
5421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5422 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5423 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5424 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5425 }
5426
Gabor Mezei15b95a62022-05-09 16:37:58 +02005427 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005428 return( 0 );
5429}
5430
Ronald Crone68ab4f2022-10-05 12:46:29 +02005431#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005432
Jerry Yudc7bd172022-02-17 13:44:15 +08005433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5434
5435#if defined(MBEDTLS_USE_PSA_CRYPTO)
5436
5437static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5438 mbedtls_svc_key_id_t key,
5439 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005440 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005441 const unsigned char* seed, size_t seed_length,
5442 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005443 const unsigned char* other_secret,
5444 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005445 size_t capacity )
5446{
5447 psa_status_t status;
5448
5449 status = psa_key_derivation_setup( derivation, alg );
5450 if( status != PSA_SUCCESS )
5451 return( status );
5452
5453 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5454 {
5455 status = psa_key_derivation_input_bytes( derivation,
5456 PSA_KEY_DERIVATION_INPUT_SEED,
5457 seed, seed_length );
5458 if( status != PSA_SUCCESS )
5459 return( status );
5460
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005461 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005462 {
5463 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005464 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5465 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005466 if( status != PSA_SUCCESS )
5467 return( status );
5468 }
5469
Jerry Yudc7bd172022-02-17 13:44:15 +08005470 if( mbedtls_svc_key_id_is_null( key ) )
5471 {
5472 status = psa_key_derivation_input_bytes(
5473 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005474 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005475 }
5476 else
5477 {
5478 status = psa_key_derivation_input_key(
5479 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5480 }
5481 if( status != PSA_SUCCESS )
5482 return( status );
5483
5484 status = psa_key_derivation_input_bytes( derivation,
5485 PSA_KEY_DERIVATION_INPUT_LABEL,
5486 label, label_length );
5487 if( status != PSA_SUCCESS )
5488 return( status );
5489 }
5490 else
5491 {
5492 return( PSA_ERROR_NOT_SUPPORTED );
5493 }
5494
5495 status = psa_key_derivation_set_capacity( derivation, capacity );
5496 if( status != PSA_SUCCESS )
5497 return( status );
5498
5499 return( PSA_SUCCESS );
5500}
5501
Andrzej Kurek57d10632022-10-24 10:32:01 -04005502#if defined(PSA_WANT_ALG_SHA_384) || \
5503 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005504MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005505static int tls_prf_generic( mbedtls_md_type_t md_type,
5506 const unsigned char *secret, size_t slen,
5507 const char *label,
5508 const unsigned char *random, size_t rlen,
5509 unsigned char *dstbuf, size_t dlen )
5510{
5511 psa_status_t status;
5512 psa_algorithm_t alg;
5513 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5514 psa_key_derivation_operation_t derivation =
5515 PSA_KEY_DERIVATION_OPERATION_INIT;
5516
5517 if( md_type == MBEDTLS_MD_SHA384 )
5518 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5519 else
5520 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5521
5522 /* Normally a "secret" should be long enough to be impossible to
5523 * find by brute force, and in particular should not be empty. But
5524 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5525 * and for this use case it makes sense to have a 0-length "secret".
5526 * Since the key API doesn't allow importing a key of length 0,
5527 * keep master_key=0, which setup_psa_key_derivation() understands
5528 * to mean a 0-length "secret" input. */
5529 if( slen != 0 )
5530 {
5531 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5532 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5533 psa_set_key_algorithm( &key_attributes, alg );
5534 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5535
5536 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5537 if( status != PSA_SUCCESS )
5538 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5539 }
5540
5541 status = setup_psa_key_derivation( &derivation,
5542 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005543 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005544 random, rlen,
5545 (unsigned char const *) label,
5546 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005547 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005548 dlen );
5549 if( status != PSA_SUCCESS )
5550 {
5551 psa_key_derivation_abort( &derivation );
5552 psa_destroy_key( master_key );
5553 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5554 }
5555
5556 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5557 if( status != PSA_SUCCESS )
5558 {
5559 psa_key_derivation_abort( &derivation );
5560 psa_destroy_key( master_key );
5561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5562 }
5563
5564 status = psa_key_derivation_abort( &derivation );
5565 if( status != PSA_SUCCESS )
5566 {
5567 psa_destroy_key( master_key );
5568 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5569 }
5570
5571 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5572 status = psa_destroy_key( master_key );
5573 if( status != PSA_SUCCESS )
5574 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5575
5576 return( 0 );
5577}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005578#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005579#else /* MBEDTLS_USE_PSA_CRYPTO */
5580
Andrzej Kurek57d10632022-10-24 10:32:01 -04005581#if defined(MBEDTLS_MD_C) && \
5582 ( defined(MBEDTLS_SHA256_C) || \
5583 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005584MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005585static int tls_prf_generic( mbedtls_md_type_t md_type,
5586 const unsigned char *secret, size_t slen,
5587 const char *label,
5588 const unsigned char *random, size_t rlen,
5589 unsigned char *dstbuf, size_t dlen )
5590{
5591 size_t nb;
5592 size_t i, j, k, md_len;
5593 unsigned char *tmp;
5594 size_t tmp_len = 0;
5595 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5596 const mbedtls_md_info_t *md_info;
5597 mbedtls_md_context_t md_ctx;
5598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5599
5600 mbedtls_md_init( &md_ctx );
5601
5602 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5604
5605 md_len = mbedtls_md_get_size( md_info );
5606
5607 tmp_len = md_len + strlen( label ) + rlen;
5608 tmp = mbedtls_calloc( 1, tmp_len );
5609 if( tmp == NULL )
5610 {
5611 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5612 goto exit;
5613 }
5614
5615 nb = strlen( label );
5616 memcpy( tmp + md_len, label, nb );
5617 memcpy( tmp + md_len + nb, random, rlen );
5618 nb += rlen;
5619
5620 /*
5621 * Compute P_<hash>(secret, label + random)[0..dlen]
5622 */
5623 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5624 goto exit;
5625
5626 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5627 if( ret != 0 )
5628 goto exit;
5629 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5630 if( ret != 0 )
5631 goto exit;
5632 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5633 if( ret != 0 )
5634 goto exit;
5635
5636 for( i = 0; i < dlen; i += md_len )
5637 {
5638 ret = mbedtls_md_hmac_reset ( &md_ctx );
5639 if( ret != 0 )
5640 goto exit;
5641 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5642 if( ret != 0 )
5643 goto exit;
5644 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5645 if( ret != 0 )
5646 goto exit;
5647
5648 ret = mbedtls_md_hmac_reset ( &md_ctx );
5649 if( ret != 0 )
5650 goto exit;
5651 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5652 if( ret != 0 )
5653 goto exit;
5654 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5655 if( ret != 0 )
5656 goto exit;
5657
5658 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5659
5660 for( j = 0; j < k; j++ )
5661 dstbuf[i + j] = h_i[j];
5662 }
5663
5664exit:
5665 mbedtls_md_free( &md_ctx );
5666
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00005667 if ( tmp != NULL )
5668 mbedtls_platform_zeroize( tmp, tmp_len );
5669
Jerry Yudc7bd172022-02-17 13:44:15 +08005670 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5671
5672 mbedtls_free( tmp );
5673
5674 return( ret );
5675}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005676#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005677#endif /* MBEDTLS_USE_PSA_CRYPTO */
5678
Andrzej Kurek25f27152022-08-17 16:09:31 -04005679#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005680MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005681static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5682 const char *label,
5683 const unsigned char *random, size_t rlen,
5684 unsigned char *dstbuf, size_t dlen )
5685{
5686 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5687 label, random, rlen, dstbuf, dlen ) );
5688}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005689#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005690
Andrzej Kurek25f27152022-08-17 16:09:31 -04005691#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005692MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005693static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5694 const char *label,
5695 const unsigned char *random, size_t rlen,
5696 unsigned char *dstbuf, size_t dlen )
5697{
5698 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5699 label, random, rlen, dstbuf, dlen ) );
5700}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005701#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005702
Jerry Yuf009d862022-02-17 14:01:37 +08005703/*
5704 * Set appropriate PRF function and other SSL / TLS1.2 functions
5705 *
5706 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005707 * - hash associated with the ciphersuite (only used by TLS 1.2)
5708 *
5709 * Outputs:
5710 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5711 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005712MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005713static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005714 mbedtls_md_type_t hash )
5715{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005716#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005717 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005718 {
5719 handshake->tls_prf = tls_prf_sha384;
5720 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5721 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5722 }
5723 else
5724#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005725#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005726 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005727 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005728 handshake->tls_prf = tls_prf_sha256;
5729 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5730 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5731 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005732#else
Jerry Yuf009d862022-02-17 14:01:37 +08005733 {
Jerry Yuf009d862022-02-17 14:01:37 +08005734 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005735 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5737 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005738#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005739
5740 return( 0 );
5741}
Jerry Yud6ab2352022-02-17 14:03:43 +08005742
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005743/*
5744 * Compute master secret if needed
5745 *
5746 * Parameters:
5747 * [in/out] handshake
5748 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5749 * (PSA-PSK) ciphersuite_info, psk_opaque
5750 * [out] premaster (cleared)
5751 * [out] master
5752 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5753 * debug: conf->f_dbg, conf->p_dbg
5754 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005755 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005756 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005757MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005758static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5759 unsigned char *master,
5760 const mbedtls_ssl_context *ssl )
5761{
5762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5763
5764 /* cf. RFC 5246, Section 8.1:
5765 * "The master secret is always exactly 48 bytes in length." */
5766 size_t const master_secret_len = 48;
5767
5768#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5769 unsigned char session_hash[48];
5770#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5771
5772 /* The label for the KDF used for key expansion.
5773 * This is either "master secret" or "extended master secret"
5774 * depending on whether the Extended Master Secret extension
5775 * is used. */
5776 char const *lbl = "master secret";
5777
Przemek Stekielae4ed302022-04-05 17:15:55 +02005778 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005779 * - If the Extended Master Secret extension is not used,
5780 * this is ClientHello.Random + ServerHello.Random
5781 * (see Sect. 8.1 in RFC 5246).
5782 * - If the Extended Master Secret extension is used,
5783 * this is the transcript of the handshake so far.
5784 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005785 unsigned char const *seed = handshake->randbytes;
5786 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005787
5788#if !defined(MBEDTLS_DEBUG_C) && \
5789 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5790 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5791 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5792 ssl = NULL; /* make sure we don't use it except for those cases */
5793 (void) ssl;
5794#endif
5795
5796 if( handshake->resume != 0 )
5797 {
5798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5799 return( 0 );
5800 }
5801
5802#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5803 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5804 {
5805 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005806 seed = session_hash;
5807 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005808
5809 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005810 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005811 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005812#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005813
Przemek Stekiel99114f32022-04-22 11:20:09 +02005814#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005815 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005816 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005817 {
5818 /* Perform PSK-to-MS expansion in a single step. */
5819 psa_status_t status;
5820 psa_algorithm_t alg;
5821 mbedtls_svc_key_id_t psk;
5822 psa_key_derivation_operation_t derivation =
5823 PSA_KEY_DERIVATION_OPERATION_INIT;
5824 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5825
5826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5827
5828 psk = mbedtls_ssl_get_opaque_psk( ssl );
5829
5830 if( hash_alg == MBEDTLS_MD_SHA384 )
5831 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5832 else
5833 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5834
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005835 size_t other_secret_len = 0;
5836 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005837
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005838 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005839 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005840 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005841 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005842 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005843 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005844 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005845 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005846 other_secret_len = 48;
5847 other_secret = handshake->premaster + 2;
5848 break;
5849 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005850 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005851 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5852 other_secret = handshake->premaster + 2;
5853 break;
5854 default:
5855 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005856 }
5857
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005858 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005859 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005860 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005861 (unsigned char const *) lbl,
5862 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005863 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005864 master_secret_len );
5865 if( status != PSA_SUCCESS )
5866 {
5867 psa_key_derivation_abort( &derivation );
5868 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5869 }
5870
5871 status = psa_key_derivation_output_bytes( &derivation,
5872 master,
5873 master_secret_len );
5874 if( status != PSA_SUCCESS )
5875 {
5876 psa_key_derivation_abort( &derivation );
5877 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5878 }
5879
5880 status = psa_key_derivation_abort( &derivation );
5881 if( status != PSA_SUCCESS )
5882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5883 }
5884 else
5885#endif
5886 {
5887 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005888 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005889 master,
5890 master_secret_len );
5891 if( ret != 0 )
5892 {
5893 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5894 return( ret );
5895 }
5896
5897 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5898 handshake->premaster,
5899 handshake->pmslen );
5900
5901 mbedtls_platform_zeroize( handshake->premaster,
5902 sizeof(handshake->premaster) );
5903 }
5904
5905 return( 0 );
5906}
5907
Jerry Yud62f87e2022-02-17 14:09:02 +08005908int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5909{
5910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5911 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5912 ssl->handshake->ciphersuite_info;
5913
5914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5915
5916 /* Set PRF, calc_verify and calc_finished function pointers */
5917 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005918 ciphersuite_info->mac );
5919 if( ret != 0 )
5920 {
5921 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5922 return( ret );
5923 }
5924
5925 /* Compute master secret if needed */
5926 ret = ssl_compute_master( ssl->handshake,
5927 ssl->session_negotiate->master,
5928 ssl );
5929 if( ret != 0 )
5930 {
5931 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5932 return( ret );
5933 }
5934
5935 /* Swap the client and server random values:
5936 * - MS derivation wanted client+server (RFC 5246 8.1)
5937 * - key derivation wants server+client (RFC 5246 6.3) */
5938 {
5939 unsigned char tmp[64];
5940 memcpy( tmp, ssl->handshake->randbytes, 64 );
5941 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5942 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5943 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5944 }
5945
5946 /* Populate transform structure */
5947 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5948 ssl->session_negotiate->ciphersuite,
5949 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005950#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005951 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005952#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005953 ssl->handshake->tls_prf,
5954 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005955 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005956 ssl->conf->endpoint,
5957 ssl );
5958 if( ret != 0 )
5959 {
5960 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5961 return( ret );
5962 }
5963
5964 /* We no longer need Server/ClientHello.random values */
5965 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5966 sizeof( ssl->handshake->randbytes ) );
5967
5968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5969
5970 return( 0 );
5971}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005972
Ronald Cron4dcbca92022-03-07 10:21:40 +01005973int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5974{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005975 switch( md )
5976 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005977#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005978 case MBEDTLS_SSL_HASH_SHA384:
5979 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5980 break;
5981#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005982#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005983 case MBEDTLS_SSL_HASH_SHA256:
5984 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5985 break;
5986#endif
5987 default:
5988 return( -1 );
5989 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005990#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5991 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5992 (void) ssl;
5993#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01005994 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005995}
5996
Andrzej Kurek25f27152022-08-17 16:09:31 -04005997#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005998void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5999 unsigned char *hash,
6000 size_t *hlen )
6001{
6002#if defined(MBEDTLS_USE_PSA_CRYPTO)
6003 size_t hash_size;
6004 psa_status_t status;
6005 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6006
6007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6008 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6009 if( status != PSA_SUCCESS )
6010 {
6011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6012 return;
6013 }
6014
6015 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6016 if( status != PSA_SUCCESS )
6017 {
6018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6019 return;
6020 }
6021
6022 *hlen = 32;
6023 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6025#else
6026 mbedtls_sha256_context sha256;
6027
6028 mbedtls_sha256_init( &sha256 );
6029
6030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6031
6032 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6033 mbedtls_sha256_finish( &sha256, hash );
6034
6035 *hlen = 32;
6036
6037 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6039
6040 mbedtls_sha256_free( &sha256 );
6041#endif /* MBEDTLS_USE_PSA_CRYPTO */
6042 return;
6043}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006044#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006045
Andrzej Kurek25f27152022-08-17 16:09:31 -04006046#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006047void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6048 unsigned char *hash,
6049 size_t *hlen )
6050{
6051#if defined(MBEDTLS_USE_PSA_CRYPTO)
6052 size_t hash_size;
6053 psa_status_t status;
6054 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6055
6056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6057 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6058 if( status != PSA_SUCCESS )
6059 {
6060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6061 return;
6062 }
6063
6064 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6065 if( status != PSA_SUCCESS )
6066 {
6067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6068 return;
6069 }
6070
6071 *hlen = 48;
6072 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6074#else
6075 mbedtls_sha512_context sha512;
6076
6077 mbedtls_sha512_init( &sha512 );
6078
6079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6080
Andrzej Kureka242e832022-08-11 10:03:14 -04006081 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006082 mbedtls_sha512_finish( &sha512, hash );
6083
6084 *hlen = 48;
6085
6086 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6088
6089 mbedtls_sha512_free( &sha512 );
6090#endif /* MBEDTLS_USE_PSA_CRYPTO */
6091 return;
6092}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006093#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006094
Neil Armstrong80f6f322022-05-03 17:56:38 +02006095#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6096 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006097int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6098{
6099 unsigned char *p = ssl->handshake->premaster;
6100 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6101 const unsigned char *psk = NULL;
6102 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006103 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006104
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006105 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006106 {
6107 /*
6108 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006109 * checked before calling this function.
6110 *
6111 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006112 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006113 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006114 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6115 {
6116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6118 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006119 }
6120
6121 /*
6122 * PMS = struct {
6123 * opaque other_secret<0..2^16-1>;
6124 * opaque psk<0..2^16-1>;
6125 * };
6126 * with "other_secret" depending on the particular key exchange
6127 */
6128#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6129 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6130 {
6131 if( end - p < 2 )
6132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6133
6134 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6135 p += 2;
6136
6137 if( end < p || (size_t)( end - p ) < psk_len )
6138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6139
6140 memset( p, 0, psk_len );
6141 p += psk_len;
6142 }
6143 else
6144#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6145#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6146 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6147 {
6148 /*
6149 * other_secret already set by the ClientKeyExchange message,
6150 * and is 48 bytes long
6151 */
6152 if( end - p < 2 )
6153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6154
6155 *p++ = 0;
6156 *p++ = 48;
6157 p += 48;
6158 }
6159 else
6160#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6161#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6162 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6163 {
6164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6165 size_t len;
6166
6167 /* Write length only when we know the actual value */
6168 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6169 p + 2, end - ( p + 2 ), &len,
6170 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6171 {
6172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6173 return( ret );
6174 }
6175 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6176 p += 2 + len;
6177
6178 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6179 }
6180 else
6181#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006182#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006183 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6184 {
6185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6186 size_t zlen;
6187
6188 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6189 p + 2, end - ( p + 2 ),
6190 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6191 {
6192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6193 return( ret );
6194 }
6195
6196 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6197 p += 2 + zlen;
6198
6199 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6200 MBEDTLS_DEBUG_ECDH_Z );
6201 }
6202 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006203#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006204 {
6205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6207 }
6208
6209 /* opaque psk<0..2^16-1>; */
6210 if( end - p < 2 )
6211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6212
6213 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6214 p += 2;
6215
6216 if( end < p || (size_t)( end - p ) < psk_len )
6217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6218
6219 memcpy( p, psk, psk_len );
6220 p += psk_len;
6221
6222 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6223
6224 return( 0 );
6225}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006226#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006227
6228#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006229MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006230static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6231
6232#if defined(MBEDTLS_SSL_PROTO_DTLS)
6233int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6234{
6235 /* If renegotiation is not enforced, retransmit until we would reach max
6236 * timeout if we were using the usual handshake doubling scheme */
6237 if( ssl->conf->renego_max_records < 0 )
6238 {
6239 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6240 unsigned char doublings = 1;
6241
6242 while( ratio != 0 )
6243 {
6244 ++doublings;
6245 ratio >>= 1;
6246 }
6247
6248 if( ++ssl->renego_records_seen > doublings )
6249 {
6250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6251 return( 0 );
6252 }
6253 }
6254
6255 return( ssl_write_hello_request( ssl ) );
6256}
6257#endif
6258#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006259
Jerry Yud9526692022-02-17 14:23:47 +08006260/*
6261 * Handshake functions
6262 */
6263#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6264/* No certificate support -> dummy functions */
6265int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6266{
6267 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6268 ssl->handshake->ciphersuite_info;
6269
6270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6271
6272 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6273 {
6274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6275 ssl->state++;
6276 return( 0 );
6277 }
6278
6279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6281}
6282
6283int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6284{
6285 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6286 ssl->handshake->ciphersuite_info;
6287
6288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6289
6290 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6291 {
6292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6293 ssl->state++;
6294 return( 0 );
6295 }
6296
6297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6299}
6300
6301#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6302/* Some certificate support -> implement write and parse */
6303
6304int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6305{
6306 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6307 size_t i, n;
6308 const mbedtls_x509_crt *crt;
6309 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6310 ssl->handshake->ciphersuite_info;
6311
6312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6313
6314 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6315 {
6316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6317 ssl->state++;
6318 return( 0 );
6319 }
6320
6321#if defined(MBEDTLS_SSL_CLI_C)
6322 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6323 {
6324 if( ssl->handshake->client_auth == 0 )
6325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6327 ssl->state++;
6328 return( 0 );
6329 }
6330 }
6331#endif /* MBEDTLS_SSL_CLI_C */
6332#if defined(MBEDTLS_SSL_SRV_C)
6333 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6334 {
6335 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6336 {
6337 /* Should never happen because we shouldn't have picked the
6338 * ciphersuite if we don't have a certificate. */
6339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6340 }
6341 }
6342#endif
6343
6344 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6345
6346 /*
6347 * 0 . 0 handshake type
6348 * 1 . 3 handshake length
6349 * 4 . 6 length of all certs
6350 * 7 . 9 length of cert. 1
6351 * 10 . n-1 peer certificate
6352 * n . n+2 length of cert. 2
6353 * n+3 . ... upper level cert, etc.
6354 */
6355 i = 7;
6356 crt = mbedtls_ssl_own_cert( ssl );
6357
6358 while( crt != NULL )
6359 {
6360 n = crt->raw.len;
6361 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6362 {
6363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6364 " > %" MBEDTLS_PRINTF_SIZET,
6365 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6366 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6367 }
6368
6369 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6370 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6371 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6372
6373 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6374 i += n; crt = crt->next;
6375 }
6376
6377 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6378 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6379 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6380
6381 ssl->out_msglen = i;
6382 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6383 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6384
6385 ssl->state++;
6386
6387 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6388 {
6389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6390 return( ret );
6391 }
6392
6393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6394
6395 return( ret );
6396}
6397
6398#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6399
6400#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006401MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006402static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6403 unsigned char *crt_buf,
6404 size_t crt_buf_len )
6405{
6406 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6407
6408 if( peer_crt == NULL )
6409 return( -1 );
6410
6411 if( peer_crt->raw.len != crt_buf_len )
6412 return( -1 );
6413
6414 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6415}
6416#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006418static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6419 unsigned char *crt_buf,
6420 size_t crt_buf_len )
6421{
6422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6423 unsigned char const * const peer_cert_digest =
6424 ssl->session->peer_cert_digest;
6425 mbedtls_md_type_t const peer_cert_digest_type =
6426 ssl->session->peer_cert_digest_type;
6427 mbedtls_md_info_t const * const digest_info =
6428 mbedtls_md_info_from_type( peer_cert_digest_type );
6429 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6430 size_t digest_len;
6431
6432 if( peer_cert_digest == NULL || digest_info == NULL )
6433 return( -1 );
6434
6435 digest_len = mbedtls_md_get_size( digest_info );
6436 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6437 return( -1 );
6438
6439 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6440 if( ret != 0 )
6441 return( -1 );
6442
6443 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6444}
6445#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6446#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6447
6448/*
6449 * Once the certificate message is read, parse it into a cert chain and
6450 * perform basic checks, but leave actual verification to the caller
6451 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006452MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006453static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6454 mbedtls_x509_crt *chain )
6455{
6456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6457#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6458 int crt_cnt=0;
6459#endif
6460 size_t i, n;
6461 uint8_t alert;
6462
6463 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6464 {
6465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6466 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6467 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6468 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6469 }
6470
6471 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6472 {
6473 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6474 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6475 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6476 }
6477
6478 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6479 {
6480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6481 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6482 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6483 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6484 }
6485
6486 i = mbedtls_ssl_hs_hdr_len( ssl );
6487
6488 /*
6489 * Same message structure as in mbedtls_ssl_write_certificate()
6490 */
6491 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6492
6493 if( ssl->in_msg[i] != 0 ||
6494 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6495 {
6496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6497 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6498 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6499 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6500 }
6501
6502 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6503 i += 3;
6504
6505 /* Iterate through and parse the CRTs in the provided chain. */
6506 while( i < ssl->in_hslen )
6507 {
6508 /* Check that there's room for the next CRT's length fields. */
6509 if ( i + 3 > ssl->in_hslen ) {
6510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6511 mbedtls_ssl_send_alert_message( ssl,
6512 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6513 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6514 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6515 }
6516 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6517 * anything beyond 2**16 ~ 64K. */
6518 if( ssl->in_msg[i] != 0 )
6519 {
6520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6521 mbedtls_ssl_send_alert_message( ssl,
6522 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6523 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6524 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6525 }
6526
6527 /* Read length of the next CRT in the chain. */
6528 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6529 | (unsigned int) ssl->in_msg[i + 2];
6530 i += 3;
6531
6532 if( n < 128 || i + n > ssl->in_hslen )
6533 {
6534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6535 mbedtls_ssl_send_alert_message( ssl,
6536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6537 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6538 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6539 }
6540
6541 /* Check if we're handling the first CRT in the chain. */
6542#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6543 if( crt_cnt++ == 0 &&
6544 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6545 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6546 {
6547 /* During client-side renegotiation, check that the server's
6548 * end-CRTs hasn't changed compared to the initial handshake,
6549 * mitigating the triple handshake attack. On success, reuse
6550 * the original end-CRT instead of parsing it again. */
6551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6552 if( ssl_check_peer_crt_unchanged( ssl,
6553 &ssl->in_msg[i],
6554 n ) != 0 )
6555 {
6556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6557 mbedtls_ssl_send_alert_message( ssl,
6558 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6559 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6560 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6561 }
6562
6563 /* Now we can safely free the original chain. */
6564 ssl_clear_peer_cert( ssl->session );
6565 }
6566#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6567
6568 /* Parse the next certificate in the chain. */
6569#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6570 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6571#else
6572 /* If we don't need to store the CRT chain permanently, parse
6573 * it in-place from the input buffer instead of making a copy. */
6574 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6575#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6576 switch( ret )
6577 {
6578 case 0: /*ok*/
6579 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6580 /* Ignore certificate with an unknown algorithm: maybe a
6581 prior certificate was already trusted. */
6582 break;
6583
6584 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6585 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6586 goto crt_parse_der_failed;
6587
6588 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6589 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6590 goto crt_parse_der_failed;
6591
6592 default:
6593 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6594 crt_parse_der_failed:
6595 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6596 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6597 return( ret );
6598 }
6599
6600 i += n;
6601 }
6602
6603 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6604 return( 0 );
6605}
6606
6607#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006608MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006609static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6610{
6611 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6612 return( -1 );
6613
6614 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6615 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6616 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6617 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6618 {
Ronald Cron19385882022-06-15 16:26:13 +02006619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006620 return( 0 );
6621 }
6622 return( -1 );
6623}
6624#endif /* MBEDTLS_SSL_SRV_C */
6625
6626/* Check if a certificate message is expected.
6627 * Return either
6628 * - SSL_CERTIFICATE_EXPECTED, or
6629 * - SSL_CERTIFICATE_SKIP
6630 * indicating whether a Certificate message is expected or not.
6631 */
6632#define SSL_CERTIFICATE_EXPECTED 0
6633#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006634MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006635static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6636 int authmode )
6637{
6638 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6639 ssl->handshake->ciphersuite_info;
6640
6641 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6642 return( SSL_CERTIFICATE_SKIP );
6643
6644#if defined(MBEDTLS_SSL_SRV_C)
6645 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6646 {
6647 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6648 return( SSL_CERTIFICATE_SKIP );
6649
6650 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6651 {
6652 ssl->session_negotiate->verify_result =
6653 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6654 return( SSL_CERTIFICATE_SKIP );
6655 }
6656 }
6657#else
6658 ((void) authmode);
6659#endif /* MBEDTLS_SSL_SRV_C */
6660
6661 return( SSL_CERTIFICATE_EXPECTED );
6662}
6663
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006664MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006665static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6666 int authmode,
6667 mbedtls_x509_crt *chain,
6668 void *rs_ctx )
6669{
6670 int ret = 0;
6671 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6672 ssl->handshake->ciphersuite_info;
6673 int have_ca_chain = 0;
6674
6675 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6676 void *p_vrfy;
6677
6678 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6679 return( 0 );
6680
6681 if( ssl->f_vrfy != NULL )
6682 {
6683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6684 f_vrfy = ssl->f_vrfy;
6685 p_vrfy = ssl->p_vrfy;
6686 }
6687 else
6688 {
6689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6690 f_vrfy = ssl->conf->f_vrfy;
6691 p_vrfy = ssl->conf->p_vrfy;
6692 }
6693
6694 /*
6695 * Main check: verify certificate
6696 */
6697#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6698 if( ssl->conf->f_ca_cb != NULL )
6699 {
6700 ((void) rs_ctx);
6701 have_ca_chain = 1;
6702
6703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6704 ret = mbedtls_x509_crt_verify_with_ca_cb(
6705 chain,
6706 ssl->conf->f_ca_cb,
6707 ssl->conf->p_ca_cb,
6708 ssl->conf->cert_profile,
6709 ssl->hostname,
6710 &ssl->session_negotiate->verify_result,
6711 f_vrfy, p_vrfy );
6712 }
6713 else
6714#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6715 {
6716 mbedtls_x509_crt *ca_chain;
6717 mbedtls_x509_crl *ca_crl;
6718
6719#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6720 if( ssl->handshake->sni_ca_chain != NULL )
6721 {
6722 ca_chain = ssl->handshake->sni_ca_chain;
6723 ca_crl = ssl->handshake->sni_ca_crl;
6724 }
6725 else
6726#endif
6727 {
6728 ca_chain = ssl->conf->ca_chain;
6729 ca_crl = ssl->conf->ca_crl;
6730 }
6731
6732 if( ca_chain != NULL )
6733 have_ca_chain = 1;
6734
6735 ret = mbedtls_x509_crt_verify_restartable(
6736 chain,
6737 ca_chain, ca_crl,
6738 ssl->conf->cert_profile,
6739 ssl->hostname,
6740 &ssl->session_negotiate->verify_result,
6741 f_vrfy, p_vrfy, rs_ctx );
6742 }
6743
6744 if( ret != 0 )
6745 {
6746 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6747 }
6748
6749#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6750 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6751 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6752#endif
6753
6754 /*
6755 * Secondary checks: always done, but change 'ret' only if it was 0
6756 */
6757
6758#if defined(MBEDTLS_ECP_C)
6759 {
6760 const mbedtls_pk_context *pk = &chain->pk;
6761
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006762 /* If certificate uses an EC key, make sure the curve is OK.
6763 * This is a public key, so it can't be opaque, so can_do() is a good
6764 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006765 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006766 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006767 /* and in the unlikely case the above assumption no longer holds
6768 * we are making sure that pk_ec() here does not return a NULL
6769 */
6770 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6771 if( ec == NULL )
6772 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6775 }
Jerry Yud9526692022-02-17 14:23:47 +08006776
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006777 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6778 {
6779 ssl->session_negotiate->verify_result |=
6780 MBEDTLS_X509_BADCERT_BAD_KEY;
6781
6782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6783 if( ret == 0 )
6784 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6785 }
Jerry Yud9526692022-02-17 14:23:47 +08006786 }
6787 }
6788#endif /* MBEDTLS_ECP_C */
6789
6790 if( mbedtls_ssl_check_cert_usage( chain,
6791 ciphersuite_info,
6792 ! ssl->conf->endpoint,
6793 &ssl->session_negotiate->verify_result ) != 0 )
6794 {
6795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6796 if( ret == 0 )
6797 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6798 }
6799
6800 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6801 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6802 * with details encoded in the verification flags. All other kinds
6803 * of error codes, including those from the user provided f_vrfy
6804 * functions, are treated as fatal and lead to a failure of
6805 * ssl_parse_certificate even if verification was optional. */
6806 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6807 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6808 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6809 {
6810 ret = 0;
6811 }
6812
6813 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6814 {
6815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6816 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6817 }
6818
6819 if( ret != 0 )
6820 {
6821 uint8_t alert;
6822
6823 /* The certificate may have been rejected for several reasons.
6824 Pick one and send the corresponding alert. Which alert to send
6825 may be a subject of debate in some cases. */
6826 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6827 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6828 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6829 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6830 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6831 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6832 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6833 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6834 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6835 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6836 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6837 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6838 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6839 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6840 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6841 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6842 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6843 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6844 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6845 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6846 else
6847 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6849 alert );
6850 }
6851
6852#if defined(MBEDTLS_DEBUG_C)
6853 if( ssl->session_negotiate->verify_result != 0 )
6854 {
6855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6856 (unsigned int) ssl->session_negotiate->verify_result ) );
6857 }
6858 else
6859 {
6860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6861 }
6862#endif /* MBEDTLS_DEBUG_C */
6863
6864 return( ret );
6865}
6866
6867#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006868MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006869static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6870 unsigned char *start, size_t len )
6871{
6872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6873 /* Remember digest of the peer's end-CRT. */
6874 ssl->session_negotiate->peer_cert_digest =
6875 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6876 if( ssl->session_negotiate->peer_cert_digest == NULL )
6877 {
6878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6879 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6880 mbedtls_ssl_send_alert_message( ssl,
6881 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6882 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6883
6884 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6885 }
6886
6887 ret = mbedtls_md( mbedtls_md_info_from_type(
6888 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6889 start, len,
6890 ssl->session_negotiate->peer_cert_digest );
6891
6892 ssl->session_negotiate->peer_cert_digest_type =
6893 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6894 ssl->session_negotiate->peer_cert_digest_len =
6895 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6896
6897 return( ret );
6898}
6899
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006900MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006901static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6902 unsigned char *start, size_t len )
6903{
6904 unsigned char *end = start + len;
6905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6906
6907 /* Make a copy of the peer's raw public key. */
6908 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6909 ret = mbedtls_pk_parse_subpubkey( &start, end,
6910 &ssl->handshake->peer_pubkey );
6911 if( ret != 0 )
6912 {
6913 /* We should have parsed the public key before. */
6914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6915 }
6916
6917 return( 0 );
6918}
6919#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6920
6921int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6922{
6923 int ret = 0;
6924 int crt_expected;
6925#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6926 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6927 ? ssl->handshake->sni_authmode
6928 : ssl->conf->authmode;
6929#else
6930 const int authmode = ssl->conf->authmode;
6931#endif
6932 void *rs_ctx = NULL;
6933 mbedtls_x509_crt *chain = NULL;
6934
6935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6936
6937 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6938 if( crt_expected == SSL_CERTIFICATE_SKIP )
6939 {
6940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6941 goto exit;
6942 }
6943
6944#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6945 if( ssl->handshake->ecrs_enabled &&
6946 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6947 {
6948 chain = ssl->handshake->ecrs_peer_cert;
6949 ssl->handshake->ecrs_peer_cert = NULL;
6950 goto crt_verify;
6951 }
6952#endif
6953
6954 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6955 {
6956 /* mbedtls_ssl_read_record may have sent an alert already. We
6957 let it decide whether to alert. */
6958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6959 goto exit;
6960 }
6961
6962#if defined(MBEDTLS_SSL_SRV_C)
6963 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6964 {
6965 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6966
6967 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6968 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6969
6970 goto exit;
6971 }
6972#endif /* MBEDTLS_SSL_SRV_C */
6973
6974 /* Clear existing peer CRT structure in case we tried to
6975 * reuse a session but it failed, and allocate a new one. */
6976 ssl_clear_peer_cert( ssl->session_negotiate );
6977
6978 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6979 if( chain == NULL )
6980 {
6981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6982 sizeof( mbedtls_x509_crt ) ) );
6983 mbedtls_ssl_send_alert_message( ssl,
6984 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6985 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6986
6987 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6988 goto exit;
6989 }
6990 mbedtls_x509_crt_init( chain );
6991
6992 ret = ssl_parse_certificate_chain( ssl, chain );
6993 if( ret != 0 )
6994 goto exit;
6995
6996#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6997 if( ssl->handshake->ecrs_enabled)
6998 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6999
7000crt_verify:
7001 if( ssl->handshake->ecrs_enabled)
7002 rs_ctx = &ssl->handshake->ecrs_ctx;
7003#endif
7004
7005 ret = ssl_parse_certificate_verify( ssl, authmode,
7006 chain, rs_ctx );
7007 if( ret != 0 )
7008 goto exit;
7009
7010#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7011 {
7012 unsigned char *crt_start, *pk_start;
7013 size_t crt_len, pk_len;
7014
7015 /* We parse the CRT chain without copying, so
7016 * these pointers point into the input buffer,
7017 * and are hence still valid after freeing the
7018 * CRT chain. */
7019
7020 crt_start = chain->raw.p;
7021 crt_len = chain->raw.len;
7022
7023 pk_start = chain->pk_raw.p;
7024 pk_len = chain->pk_raw.len;
7025
7026 /* Free the CRT structures before computing
7027 * digest and copying the peer's public key. */
7028 mbedtls_x509_crt_free( chain );
7029 mbedtls_free( chain );
7030 chain = NULL;
7031
7032 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7033 if( ret != 0 )
7034 goto exit;
7035
7036 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7037 if( ret != 0 )
7038 goto exit;
7039 }
7040#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7041 /* Pass ownership to session structure. */
7042 ssl->session_negotiate->peer_cert = chain;
7043 chain = NULL;
7044#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7045
7046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7047
7048exit:
7049
7050 if( ret == 0 )
7051 ssl->state++;
7052
7053#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7054 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7055 {
7056 ssl->handshake->ecrs_peer_cert = chain;
7057 chain = NULL;
7058 }
7059#endif
7060
7061 if( chain != NULL )
7062 {
7063 mbedtls_x509_crt_free( chain );
7064 mbedtls_free( chain );
7065 }
7066
7067 return( ret );
7068}
7069#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7070
Andrzej Kurek25f27152022-08-17 16:09:31 -04007071#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007072static void ssl_calc_finished_tls_sha256(
7073 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7074{
7075 int len = 12;
7076 const char *sender;
7077 unsigned char padbuf[32];
7078#if defined(MBEDTLS_USE_PSA_CRYPTO)
7079 size_t hash_size;
7080 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7081 psa_status_t status;
7082#else
7083 mbedtls_sha256_context sha256;
7084#endif
7085
7086 mbedtls_ssl_session *session = ssl->session_negotiate;
7087 if( !session )
7088 session = ssl->session;
7089
7090 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7091 ? "client finished"
7092 : "server finished";
7093
7094#if defined(MBEDTLS_USE_PSA_CRYPTO)
7095 sha256_psa = psa_hash_operation_init();
7096
7097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7098
7099 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7100 if( status != PSA_SUCCESS )
7101 {
7102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7103 return;
7104 }
7105
7106 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7107 if( status != PSA_SUCCESS )
7108 {
7109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7110 return;
7111 }
7112 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7113#else
7114
7115 mbedtls_sha256_init( &sha256 );
7116
7117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7118
7119 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7120
7121 /*
7122 * TLSv1.2:
7123 * hash = PRF( master, finished_label,
7124 * Hash( handshake ) )[0.11]
7125 */
7126
7127#if !defined(MBEDTLS_SHA256_ALT)
7128 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7129 sha256.state, sizeof( sha256.state ) );
7130#endif
7131
7132 mbedtls_sha256_finish( &sha256, padbuf );
7133 mbedtls_sha256_free( &sha256 );
7134#endif /* MBEDTLS_USE_PSA_CRYPTO */
7135
7136 ssl->handshake->tls_prf( session->master, 48, sender,
7137 padbuf, 32, buf, len );
7138
7139 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7140
7141 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7142
7143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7144}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007145#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007146
Jerry Yub7ba49e2022-02-17 14:25:53 +08007147
Andrzej Kurek25f27152022-08-17 16:09:31 -04007148#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007149static void ssl_calc_finished_tls_sha384(
7150 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7151{
7152 int len = 12;
7153 const char *sender;
7154 unsigned char padbuf[48];
7155#if defined(MBEDTLS_USE_PSA_CRYPTO)
7156 size_t hash_size;
7157 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7158 psa_status_t status;
7159#else
7160 mbedtls_sha512_context sha512;
7161#endif
7162
7163 mbedtls_ssl_session *session = ssl->session_negotiate;
7164 if( !session )
7165 session = ssl->session;
7166
7167 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7168 ? "client finished"
7169 : "server finished";
7170
7171#if defined(MBEDTLS_USE_PSA_CRYPTO)
7172 sha384_psa = psa_hash_operation_init();
7173
7174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7175
7176 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7177 if( status != PSA_SUCCESS )
7178 {
7179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7180 return;
7181 }
7182
7183 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7184 if( status != PSA_SUCCESS )
7185 {
7186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7187 return;
7188 }
7189 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7190#else
7191 mbedtls_sha512_init( &sha512 );
7192
7193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7194
Andrzej Kureka242e832022-08-11 10:03:14 -04007195 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007196
7197 /*
7198 * TLSv1.2:
7199 * hash = PRF( master, finished_label,
7200 * Hash( handshake ) )[0.11]
7201 */
7202
7203#if !defined(MBEDTLS_SHA512_ALT)
7204 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7205 sha512.state, sizeof( sha512.state ) );
7206#endif
7207 mbedtls_sha512_finish( &sha512, padbuf );
7208
7209 mbedtls_sha512_free( &sha512 );
7210#endif
7211
7212 ssl->handshake->tls_prf( session->master, 48, sender,
7213 padbuf, 48, buf, len );
7214
7215 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7216
7217 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7218
7219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7220}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007221#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007222
Jerry Yuaef00152022-02-17 14:27:31 +08007223void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7224{
7225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7226
7227 /*
7228 * Free our handshake params
7229 */
7230 mbedtls_ssl_handshake_free( ssl );
7231 mbedtls_free( ssl->handshake );
7232 ssl->handshake = NULL;
7233
7234 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007235 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007236 */
7237 if( ssl->transform )
7238 {
7239 mbedtls_ssl_transform_free( ssl->transform );
7240 mbedtls_free( ssl->transform );
7241 }
7242 ssl->transform = ssl->transform_negotiate;
7243 ssl->transform_negotiate = NULL;
7244
7245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7246}
7247
Jerry Yu2a9fff52022-02-17 14:28:51 +08007248void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7249{
7250 int resume = ssl->handshake->resume;
7251
7252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7253
7254#if defined(MBEDTLS_SSL_RENEGOTIATION)
7255 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7256 {
7257 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7258 ssl->renego_records_seen = 0;
7259 }
7260#endif
7261
7262 /*
7263 * Free the previous session and switch in the current one
7264 */
7265 if( ssl->session )
7266 {
7267#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7268 /* RFC 7366 3.1: keep the EtM state */
7269 ssl->session_negotiate->encrypt_then_mac =
7270 ssl->session->encrypt_then_mac;
7271#endif
7272
7273 mbedtls_ssl_session_free( ssl->session );
7274 mbedtls_free( ssl->session );
7275 }
7276 ssl->session = ssl->session_negotiate;
7277 ssl->session_negotiate = NULL;
7278
7279 /*
7280 * Add cache entry
7281 */
7282 if( ssl->conf->f_set_cache != NULL &&
7283 ssl->session->id_len != 0 &&
7284 resume == 0 )
7285 {
7286 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7287 ssl->session->id,
7288 ssl->session->id_len,
7289 ssl->session ) != 0 )
7290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7291 }
7292
7293#if defined(MBEDTLS_SSL_PROTO_DTLS)
7294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7295 ssl->handshake->flight != NULL )
7296 {
7297 /* Cancel handshake timer */
7298 mbedtls_ssl_set_timer( ssl, 0 );
7299
7300 /* Keep last flight around in case we need to resend it:
7301 * we need the handshake and transform structures for that */
7302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7303 }
7304 else
7305#endif
7306 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7307
7308 ssl->state++;
7309
7310 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7311}
7312
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007313int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7314{
7315 int ret, hash_len;
7316
7317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7318
7319 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7320
7321 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7322
7323 /*
7324 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7325 * may define some other value. Currently (early 2016), no defined
7326 * ciphersuite does this (and this is unlikely to change as activity has
7327 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7328 */
7329 hash_len = 12;
7330
7331#if defined(MBEDTLS_SSL_RENEGOTIATION)
7332 ssl->verify_data_len = hash_len;
7333 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7334#endif
7335
7336 ssl->out_msglen = 4 + hash_len;
7337 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7338 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7339
7340 /*
7341 * In case of session resuming, invert the client and server
7342 * ChangeCipherSpec messages order.
7343 */
7344 if( ssl->handshake->resume != 0 )
7345 {
7346#if defined(MBEDTLS_SSL_CLI_C)
7347 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7348 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7349#endif
7350#if defined(MBEDTLS_SSL_SRV_C)
7351 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7352 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7353#endif
7354 }
7355 else
7356 ssl->state++;
7357
7358 /*
7359 * Switch to our negotiated transform and session parameters for outbound
7360 * data.
7361 */
7362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7363
7364#if defined(MBEDTLS_SSL_PROTO_DTLS)
7365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7366 {
7367 unsigned char i;
7368
7369 /* Remember current epoch settings for resending */
7370 ssl->handshake->alt_transform_out = ssl->transform_out;
7371 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7372 sizeof( ssl->handshake->alt_out_ctr ) );
7373
7374 /* Set sequence_number to zero */
7375 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7376
7377
7378 /* Increment epoch */
7379 for( i = 2; i > 0; i-- )
7380 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7381 break;
7382
7383 /* The loop goes to its end iff the counter is wrapping */
7384 if( i == 0 )
7385 {
7386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7387 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7388 }
7389 }
7390 else
7391#endif /* MBEDTLS_SSL_PROTO_DTLS */
7392 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7393
7394 ssl->transform_out = ssl->transform_negotiate;
7395 ssl->session_out = ssl->session_negotiate;
7396
7397#if defined(MBEDTLS_SSL_PROTO_DTLS)
7398 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7399 mbedtls_ssl_send_flight_completed( ssl );
7400#endif
7401
7402 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7403 {
7404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7405 return( ret );
7406 }
7407
7408#if defined(MBEDTLS_SSL_PROTO_DTLS)
7409 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7410 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7411 {
7412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7413 return( ret );
7414 }
7415#endif
7416
7417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7418
7419 return( 0 );
7420}
7421
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007422#define SSL_MAX_HASH_LEN 12
7423
7424int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7425{
7426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7427 unsigned int hash_len = 12;
7428 unsigned char buf[SSL_MAX_HASH_LEN];
7429
7430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7431
7432 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7433
7434 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7435 {
7436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7437 goto exit;
7438 }
7439
7440 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7441 {
7442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7444 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7445 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7446 goto exit;
7447 }
7448
7449 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7450 {
7451 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7452 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7453 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7454 goto exit;
7455 }
7456
7457 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7458 {
7459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7460 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7461 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7462 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7463 goto exit;
7464 }
7465
7466 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7467 buf, hash_len ) != 0 )
7468 {
7469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7470 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7471 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7472 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7473 goto exit;
7474 }
7475
7476#if defined(MBEDTLS_SSL_RENEGOTIATION)
7477 ssl->verify_data_len = hash_len;
7478 memcpy( ssl->peer_verify_data, buf, hash_len );
7479#endif
7480
7481 if( ssl->handshake->resume != 0 )
7482 {
7483#if defined(MBEDTLS_SSL_CLI_C)
7484 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7485 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7486#endif
7487#if defined(MBEDTLS_SSL_SRV_C)
7488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7489 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7490#endif
7491 }
7492 else
7493 ssl->state++;
7494
7495#if defined(MBEDTLS_SSL_PROTO_DTLS)
7496 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7497 mbedtls_ssl_recv_flight_completed( ssl );
7498#endif
7499
7500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7501
7502exit:
7503 mbedtls_platform_zeroize( buf, hash_len );
7504 return( ret );
7505}
7506
Jerry Yu392112c2022-02-17 14:34:10 +08007507#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7508/*
7509 * Helper to get TLS 1.2 PRF from ciphersuite
7510 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7511 */
7512static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7513{
Jerry Yu392112c2022-02-17 14:34:10 +08007514 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007515 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7516#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007517 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007518 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007519 else
Jerry Yu392112c2022-02-17 14:34:10 +08007520#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007521#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7522 {
7523 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7524 return( tls_prf_sha256 );
7525 }
7526#endif
7527#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7528 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7529 (void) ciphersuite_info;
7530#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007531
Andrzej Kurek894edde2022-09-29 06:31:14 -04007532 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007533}
7534#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007535
7536static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7537{
7538 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007539#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007540 if( tls_prf == tls_prf_sha384 )
7541 {
7542 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7543 }
7544 else
7545#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007546#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007547 if( tls_prf == tls_prf_sha256 )
7548 {
7549 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7550 }
7551 else
7552#endif
7553 return( MBEDTLS_SSL_TLS_PRF_NONE );
7554}
7555
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007556/*
7557 * Populate a transform structure with session keys and all the other
7558 * necessary information.
7559 *
7560 * Parameters:
7561 * - [in/out]: transform: structure to populate
7562 * [in] must be just initialised with mbedtls_ssl_transform_init()
7563 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7564 * - [in] ciphersuite
7565 * - [in] master
7566 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007567 * - [in] tls_prf: pointer to PRF to use for key derivation
7568 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007569 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007570 * - [in] endpoint: client or server
7571 * - [in] ssl: used for:
7572 * - ssl->conf->{f,p}_export_keys
7573 * [in] optionally used for:
7574 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7575 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007576MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007577static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7578 int ciphersuite,
7579 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007580#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007581 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007582#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007583 ssl_tls_prf_t tls_prf,
7584 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007585 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007586 unsigned endpoint,
7587 const mbedtls_ssl_context *ssl )
7588{
7589 int ret = 0;
7590 unsigned char keyblk[256];
7591 unsigned char *key1;
7592 unsigned char *key2;
7593 unsigned char *mac_enc;
7594 unsigned char *mac_dec;
7595 size_t mac_key_len = 0;
7596 size_t iv_copy_len;
7597 size_t keylen;
7598 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007599 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007600#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007601 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007602 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007603#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007604
7605#if defined(MBEDTLS_USE_PSA_CRYPTO)
7606 psa_key_type_t key_type;
7607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7608 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007609 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007610 size_t key_bits;
7611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7612#endif
7613
7614#if !defined(MBEDTLS_DEBUG_C) && \
7615 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7616 if( ssl->f_export_keys == NULL )
7617 {
7618 ssl = NULL; /* make sure we don't use it except for these cases */
7619 (void) ssl;
7620 }
7621#endif
7622
7623 /*
7624 * Some data just needs copying into the structure
7625 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007626#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007627 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007628#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007629 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007630
7631#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7632 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7633#endif
7634
7635#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007636 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007637 {
7638 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7639 * generation separate. This should never happen. */
7640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7641 }
7642#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7643
7644 /*
7645 * Get various info structures
7646 */
7647 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7648 if( ciphersuite_info == NULL )
7649 {
7650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7651 ciphersuite ) );
7652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7653 }
7654
Neil Armstrongab555e02022-04-04 11:07:59 +02007655 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007656#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007657 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007658#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007659 ciphersuite_info );
7660
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007661 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7662 transform->taglen =
7663 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7664
7665#if defined(MBEDTLS_USE_PSA_CRYPTO)
7666 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7667 transform->taglen,
7668 &alg,
7669 &key_type,
7670 &key_bits ) ) != PSA_SUCCESS )
7671 {
7672 ret = psa_ssl_status_to_mbedtls( status );
7673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7674 goto end;
7675 }
7676#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007677 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7678 if( cipher_info == NULL )
7679 {
7680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7681 ciphersuite_info->cipher ) );
7682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7683 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007684#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007685
Neil Armstronge4512952022-03-08 09:08:22 +01007686#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007687 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007688 if( mac_alg == 0 )
7689 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007691 (unsigned) ciphersuite_info->mac ) );
7692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7693 }
7694#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007695 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7696 if( md_info == NULL )
7697 {
7698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7699 (unsigned) ciphersuite_info->mac ) );
7700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7701 }
Neil Armstronge4512952022-03-08 09:08:22 +01007702#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007703
7704#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7705 /* Copy own and peer's CID if the use of the CID
7706 * extension has been negotiated. */
7707 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7708 {
7709 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7710
7711 transform->in_cid_len = ssl->own_cid_len;
7712 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7713 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7714 transform->in_cid_len );
7715
7716 transform->out_cid_len = ssl->handshake->peer_cid_len;
7717 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7718 ssl->handshake->peer_cid_len );
7719 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7720 transform->out_cid_len );
7721 }
7722#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7723
7724 /*
7725 * Compute key block using the PRF
7726 */
7727 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7728 if( ret != 0 )
7729 {
7730 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7731 return( ret );
7732 }
7733
7734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7735 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7736 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7737 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7738 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7739
7740 /*
7741 * Determine the appropriate key, IV and MAC length.
7742 */
7743
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007744#if defined(MBEDTLS_USE_PSA_CRYPTO)
7745 keylen = PSA_BITS_TO_BYTES(key_bits);
7746#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007747 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007748#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007749
7750#if defined(MBEDTLS_GCM_C) || \
7751 defined(MBEDTLS_CCM_C) || \
7752 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007753 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007754 {
7755 size_t explicit_ivlen;
7756
7757 transform->maclen = 0;
7758 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007759
7760 /* All modes haves 96-bit IVs, but the length of the static parts vary
7761 * with mode and version:
7762 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7763 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7764 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7765 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7766 * sequence number).
7767 */
7768 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007769
7770 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007771#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007772 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007773#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007774 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7775 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007776#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007777
7778 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007779 transform->fixed_ivlen = 12;
7780 else
7781 transform->fixed_ivlen = 4;
7782
7783 /* Minimum length of encrypted record */
7784 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7785 transform->minlen = explicit_ivlen + transform->taglen;
7786 }
7787 else
7788#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7789#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007790 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7791 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7792 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007793 {
Neil Armstronge4512952022-03-08 09:08:22 +01007794#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007795 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007796#else
7797 size_t block_size = cipher_info->block_size;
7798#endif /* MBEDTLS_USE_PSA_CRYPTO */
7799
7800#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007801 /* Get MAC length */
7802 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7803#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007804 /* Initialize HMAC contexts */
7805 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7806 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7807 {
7808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7809 goto end;
7810 }
7811
7812 /* Get MAC length */
7813 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007814#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007815 transform->maclen = mac_key_len;
7816
7817 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007818#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007819 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007820#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007821 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007822#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007823
7824 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007825 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007826 transform->minlen = transform->maclen;
7827 else
7828 {
7829 /*
7830 * GenericBlockCipher:
7831 * 1. if EtM is in use: one block plus MAC
7832 * otherwise: * first multiple of blocklen greater than maclen
7833 * 2. IV
7834 */
7835#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007836 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007837 {
7838 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007839 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007840 }
7841 else
7842#endif
7843 {
7844 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007845 + block_size
7846 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007847 }
7848
Glenn Strauss07c64162022-03-14 12:34:51 -04007849 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007850 {
7851 transform->minlen += transform->ivlen;
7852 }
7853 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007854 {
7855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7856 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7857 goto end;
7858 }
7859 }
7860 }
7861 else
7862#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7863 {
7864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7866 }
7867
7868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7869 (unsigned) keylen,
7870 (unsigned) transform->minlen,
7871 (unsigned) transform->ivlen,
7872 (unsigned) transform->maclen ) );
7873
7874 /*
7875 * Finally setup the cipher contexts, IVs and MAC secrets.
7876 */
7877#if defined(MBEDTLS_SSL_CLI_C)
7878 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7879 {
7880 key1 = keyblk + mac_key_len * 2;
7881 key2 = keyblk + mac_key_len * 2 + keylen;
7882
7883 mac_enc = keyblk;
7884 mac_dec = keyblk + mac_key_len;
7885
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007886 iv_copy_len = ( transform->fixed_ivlen ) ?
7887 transform->fixed_ivlen : transform->ivlen;
7888 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7889 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7890 iv_copy_len );
7891 }
7892 else
7893#endif /* MBEDTLS_SSL_CLI_C */
7894#if defined(MBEDTLS_SSL_SRV_C)
7895 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7896 {
7897 key1 = keyblk + mac_key_len * 2 + keylen;
7898 key2 = keyblk + mac_key_len * 2;
7899
7900 mac_enc = keyblk + mac_key_len;
7901 mac_dec = keyblk;
7902
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007903 iv_copy_len = ( transform->fixed_ivlen ) ?
7904 transform->fixed_ivlen : transform->ivlen;
7905 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7906 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7907 iv_copy_len );
7908 }
7909 else
7910#endif /* MBEDTLS_SSL_SRV_C */
7911 {
7912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7913 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7914 goto end;
7915 }
7916
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007917 if( ssl != NULL && ssl->f_export_keys != NULL )
7918 {
7919 ssl->f_export_keys( ssl->p_export_keys,
7920 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7921 master, 48,
7922 randbytes + 32,
7923 randbytes,
7924 tls_prf_get_type( tls_prf ) );
7925 }
7926
7927#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007928 transform->psa_alg = alg;
7929
7930 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7931 {
7932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7933 psa_set_key_algorithm( &attributes, alg );
7934 psa_set_key_type( &attributes, key_type );
7935
7936 if( ( status = psa_import_key( &attributes,
7937 key1,
7938 PSA_BITS_TO_BYTES( key_bits ),
7939 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7940 {
7941 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7942 ret = psa_ssl_status_to_mbedtls( status );
7943 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7944 goto end;
7945 }
7946
7947 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7948
7949 if( ( status = psa_import_key( &attributes,
7950 key2,
7951 PSA_BITS_TO_BYTES( key_bits ),
7952 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7953 {
7954 ret = psa_ssl_status_to_mbedtls( status );
7955 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7956 goto end;
7957 }
7958 }
7959#else
7960 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7961 cipher_info ) ) != 0 )
7962 {
7963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7964 goto end;
7965 }
7966
7967 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7968 cipher_info ) ) != 0 )
7969 {
7970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7971 goto end;
7972 }
7973
7974 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7975 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7976 MBEDTLS_ENCRYPT ) ) != 0 )
7977 {
7978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7979 goto end;
7980 }
7981
7982 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7983 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7984 MBEDTLS_DECRYPT ) ) != 0 )
7985 {
7986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7987 goto end;
7988 }
7989
7990#if defined(MBEDTLS_CIPHER_MODE_CBC)
7991 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7992 {
7993 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7994 MBEDTLS_PADDING_NONE ) ) != 0 )
7995 {
7996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7997 goto end;
7998 }
7999
8000 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8001 MBEDTLS_PADDING_NONE ) ) != 0 )
8002 {
8003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8004 goto end;
8005 }
8006 }
8007#endif /* MBEDTLS_CIPHER_MODE_CBC */
8008#endif /* MBEDTLS_USE_PSA_CRYPTO */
8009
Neil Armstrong29c0c042022-03-17 17:47:28 +01008010#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8011 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8012 For AEAD-based ciphersuites, there is nothing to do here. */
8013 if( mac_key_len != 0 )
8014 {
8015#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008016 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008017
8018 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008019 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008020 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8021
8022 if( ( status = psa_import_key( &attributes,
8023 mac_enc, mac_key_len,
8024 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8025 {
8026 ret = psa_ssl_status_to_mbedtls( status );
8027 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8028 goto end;
8029 }
8030
Ronald Cronfb39f152022-03-25 14:36:28 +01008031 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008032 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8033#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8034 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8035#endif
8036 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008037 /* mbedtls_ct_hmac() requires the key to be exportable */
8038 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8039 PSA_KEY_USAGE_VERIFY_HASH );
8040 else
8041 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8042
8043 if( ( status = psa_import_key( &attributes,
8044 mac_dec, mac_key_len,
8045 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8046 {
8047 ret = psa_ssl_status_to_mbedtls( status );
8048 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8049 goto end;
8050 }
8051#else
8052 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8053 if( ret != 0 )
8054 goto end;
8055 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8056 if( ret != 0 )
8057 goto end;
8058#endif /* MBEDTLS_USE_PSA_CRYPTO */
8059 }
8060#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8061
8062 ((void) mac_dec);
8063 ((void) mac_enc);
8064
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008065end:
8066 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8067 return( ret );
8068}
8069
Jerry Yuee40f9d2022-02-17 14:55:16 +08008070#if defined(MBEDTLS_USE_PSA_CRYPTO)
8071int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8072 unsigned char *hash, size_t *hashlen,
8073 unsigned char *data, size_t data_len,
8074 mbedtls_md_type_t md_alg )
8075{
8076 psa_status_t status;
8077 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008078 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008079
8080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8081
8082 if( ( status = psa_hash_setup( &hash_operation,
8083 hash_alg ) ) != PSA_SUCCESS )
8084 {
8085 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8086 goto exit;
8087 }
8088
8089 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8090 64 ) ) != PSA_SUCCESS )
8091 {
8092 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8093 goto exit;
8094 }
8095
8096 if( ( status = psa_hash_update( &hash_operation,
8097 data, data_len ) ) != PSA_SUCCESS )
8098 {
8099 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8100 goto exit;
8101 }
8102
8103 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8104 hashlen ) ) != PSA_SUCCESS )
8105 {
8106 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8107 goto exit;
8108 }
8109
8110exit:
8111 if( status != PSA_SUCCESS )
8112 {
8113 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8114 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8115 switch( status )
8116 {
8117 case PSA_ERROR_NOT_SUPPORTED:
8118 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8119 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8120 case PSA_ERROR_BUFFER_TOO_SMALL:
8121 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8122 case PSA_ERROR_INSUFFICIENT_MEMORY:
8123 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8124 default:
8125 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8126 }
8127 }
8128 return( 0 );
8129}
8130
8131#else
8132
8133int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8134 unsigned char *hash, size_t *hashlen,
8135 unsigned char *data, size_t data_len,
8136 mbedtls_md_type_t md_alg )
8137{
8138 int ret = 0;
8139 mbedtls_md_context_t ctx;
8140 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8141 *hashlen = mbedtls_md_get_size( md_info );
8142
8143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8144
8145 mbedtls_md_init( &ctx );
8146
8147 /*
8148 * digitally-signed struct {
8149 * opaque client_random[32];
8150 * opaque server_random[32];
8151 * ServerDHParams params;
8152 * };
8153 */
8154 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8155 {
8156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8157 goto exit;
8158 }
8159 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8160 {
8161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8162 goto exit;
8163 }
8164 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8165 {
8166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8167 goto exit;
8168 }
8169 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8170 {
8171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8172 goto exit;
8173 }
8174 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8175 {
8176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8177 goto exit;
8178 }
8179
8180exit:
8181 mbedtls_md_free( &ctx );
8182
8183 if( ret != 0 )
8184 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8185 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8186
8187 return( ret );
8188}
8189#endif /* MBEDTLS_USE_PSA_CRYPTO */
8190
Jerry Yud9d91da2022-02-17 14:57:06 +08008191#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8192
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008193/* Find the preferred hash for a given signature algorithm. */
8194unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8195 mbedtls_ssl_context *ssl,
8196 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008197{
Gabor Mezei078e8032022-04-27 21:17:56 +02008198 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008199 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008200
8201 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008202 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008203
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008204 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008205 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008206 unsigned int hash_alg_received =
8207 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8208 received_sig_algs[i] );
8209 unsigned int sig_alg_received =
8210 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8211 received_sig_algs[i] );
8212
8213 if( sig_alg == sig_alg_received )
8214 {
8215#if defined(MBEDTLS_USE_PSA_CRYPTO)
8216 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8217 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008218 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008219 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008220
Neil Armstrong96eceb82022-06-30 18:05:05 +02008221 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8222 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8223 PSA_ALG_ECDSA( psa_hash_alg ),
8224 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008225 continue;
8226
Neil Armstrong96eceb82022-06-30 18:05:05 +02008227 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008228 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8229 PSA_ALG_RSA_PKCS1V15_SIGN(
8230 psa_hash_alg ),
8231 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008232 continue;
8233 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008234#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008235
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008236 return( hash_alg_received );
8237 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008238 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008239
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008240 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008241}
8242
8243#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8244
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008245/* Serialization of TLS 1.2 sessions:
8246 *
8247 * struct {
8248 * uint64 start_time;
8249 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008250 * uint8 session_id_len; // at most 32
8251 * opaque session_id[32];
8252 * opaque master[48]; // fixed length in the standard
8253 * uint32 verify_result;
8254 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8255 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8256 * uint32 ticket_lifetime;
8257 * uint8 mfl_code; // up to 255 according to standard
8258 * uint8 encrypt_then_mac; // 0 or 1
8259 * } serialized_session_tls12;
8260 *
8261 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008262static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008263 unsigned char *buf,
8264 size_t buf_len )
8265{
8266 unsigned char *p = buf;
8267 size_t used = 0;
8268
8269#if defined(MBEDTLS_HAVE_TIME)
8270 uint64_t start;
8271#endif
8272#if defined(MBEDTLS_X509_CRT_PARSE_C)
8273#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8274 size_t cert_len;
8275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8276#endif /* MBEDTLS_X509_CRT_PARSE_C */
8277
8278 /*
8279 * Time
8280 */
8281#if defined(MBEDTLS_HAVE_TIME)
8282 used += 8;
8283
8284 if( used <= buf_len )
8285 {
8286 start = (uint64_t) session->start;
8287
8288 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8289 p += 8;
8290 }
8291#endif /* MBEDTLS_HAVE_TIME */
8292
8293 /*
8294 * Basic mandatory fields
8295 */
8296 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008297 + 1 /* id_len */
8298 + sizeof( session->id )
8299 + sizeof( session->master )
8300 + 4; /* verify_result */
8301
8302 if( used <= buf_len )
8303 {
8304 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8305 p += 2;
8306
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008307 *p++ = MBEDTLS_BYTE_0( session->id_len );
8308 memcpy( p, session->id, 32 );
8309 p += 32;
8310
8311 memcpy( p, session->master, 48 );
8312 p += 48;
8313
8314 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8315 p += 4;
8316 }
8317
8318 /*
8319 * Peer's end-entity certificate
8320 */
8321#if defined(MBEDTLS_X509_CRT_PARSE_C)
8322#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8323 if( session->peer_cert == NULL )
8324 cert_len = 0;
8325 else
8326 cert_len = session->peer_cert->raw.len;
8327
8328 used += 3 + cert_len;
8329
8330 if( used <= buf_len )
8331 {
8332 *p++ = MBEDTLS_BYTE_2( cert_len );
8333 *p++ = MBEDTLS_BYTE_1( cert_len );
8334 *p++ = MBEDTLS_BYTE_0( cert_len );
8335
8336 if( session->peer_cert != NULL )
8337 {
8338 memcpy( p, session->peer_cert->raw.p, cert_len );
8339 p += cert_len;
8340 }
8341 }
8342#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8343 if( session->peer_cert_digest != NULL )
8344 {
8345 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8346 if( used <= buf_len )
8347 {
8348 *p++ = (unsigned char) session->peer_cert_digest_type;
8349 *p++ = (unsigned char) session->peer_cert_digest_len;
8350 memcpy( p, session->peer_cert_digest,
8351 session->peer_cert_digest_len );
8352 p += session->peer_cert_digest_len;
8353 }
8354 }
8355 else
8356 {
8357 used += 2;
8358 if( used <= buf_len )
8359 {
8360 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8361 *p++ = 0;
8362 }
8363 }
8364#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8365#endif /* MBEDTLS_X509_CRT_PARSE_C */
8366
8367 /*
8368 * Session ticket if any, plus associated data
8369 */
8370#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8371 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8372
8373 if( used <= buf_len )
8374 {
8375 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8376 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8377 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8378
8379 if( session->ticket != NULL )
8380 {
8381 memcpy( p, session->ticket, session->ticket_len );
8382 p += session->ticket_len;
8383 }
8384
8385 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8386 p += 4;
8387 }
8388#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8389
8390 /*
8391 * Misc extension-related info
8392 */
8393#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8394 used += 1;
8395
8396 if( used <= buf_len )
8397 *p++ = session->mfl_code;
8398#endif
8399
8400#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8401 used += 1;
8402
8403 if( used <= buf_len )
8404 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8405#endif
8406
8407 return( used );
8408}
8409
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008410MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008411static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008412 const unsigned char *buf,
8413 size_t len )
8414{
8415#if defined(MBEDTLS_HAVE_TIME)
8416 uint64_t start;
8417#endif
8418#if defined(MBEDTLS_X509_CRT_PARSE_C)
8419#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8420 size_t cert_len;
8421#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8422#endif /* MBEDTLS_X509_CRT_PARSE_C */
8423
8424 const unsigned char *p = buf;
8425 const unsigned char * const end = buf + len;
8426
8427 /*
8428 * Time
8429 */
8430#if defined(MBEDTLS_HAVE_TIME)
8431 if( 8 > (size_t)( end - p ) )
8432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8433
8434 start = ( (uint64_t) p[0] << 56 ) |
8435 ( (uint64_t) p[1] << 48 ) |
8436 ( (uint64_t) p[2] << 40 ) |
8437 ( (uint64_t) p[3] << 32 ) |
8438 ( (uint64_t) p[4] << 24 ) |
8439 ( (uint64_t) p[5] << 16 ) |
8440 ( (uint64_t) p[6] << 8 ) |
8441 ( (uint64_t) p[7] );
8442 p += 8;
8443
8444 session->start = (time_t) start;
8445#endif /* MBEDTLS_HAVE_TIME */
8446
8447 /*
8448 * Basic mandatory fields
8449 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008450 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8452
8453 session->ciphersuite = ( p[0] << 8 ) | p[1];
8454 p += 2;
8455
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008456 session->id_len = *p++;
8457 memcpy( session->id, p, 32 );
8458 p += 32;
8459
8460 memcpy( session->master, p, 48 );
8461 p += 48;
8462
8463 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8464 ( (uint32_t) p[1] << 16 ) |
8465 ( (uint32_t) p[2] << 8 ) |
8466 ( (uint32_t) p[3] );
8467 p += 4;
8468
8469 /* Immediately clear invalid pointer values that have been read, in case
8470 * we exit early before we replaced them with valid ones. */
8471#if defined(MBEDTLS_X509_CRT_PARSE_C)
8472#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8473 session->peer_cert = NULL;
8474#else
8475 session->peer_cert_digest = NULL;
8476#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8477#endif /* MBEDTLS_X509_CRT_PARSE_C */
8478#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8479 session->ticket = NULL;
8480#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8481
8482 /*
8483 * Peer certificate
8484 */
8485#if defined(MBEDTLS_X509_CRT_PARSE_C)
8486#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8487 /* Deserialize CRT from the end of the ticket. */
8488 if( 3 > (size_t)( end - p ) )
8489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8490
8491 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8492 p += 3;
8493
8494 if( cert_len != 0 )
8495 {
8496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8497
8498 if( cert_len > (size_t)( end - p ) )
8499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8500
8501 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8502
8503 if( session->peer_cert == NULL )
8504 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8505
8506 mbedtls_x509_crt_init( session->peer_cert );
8507
8508 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8509 p, cert_len ) ) != 0 )
8510 {
8511 mbedtls_x509_crt_free( session->peer_cert );
8512 mbedtls_free( session->peer_cert );
8513 session->peer_cert = NULL;
8514 return( ret );
8515 }
8516
8517 p += cert_len;
8518 }
8519#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8520 /* Deserialize CRT digest from the end of the ticket. */
8521 if( 2 > (size_t)( end - p ) )
8522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8523
8524 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8525 session->peer_cert_digest_len = (size_t) *p++;
8526
8527 if( session->peer_cert_digest_len != 0 )
8528 {
8529 const mbedtls_md_info_t *md_info =
8530 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8531 if( md_info == NULL )
8532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8533 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8535
8536 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8538
8539 session->peer_cert_digest =
8540 mbedtls_calloc( 1, session->peer_cert_digest_len );
8541 if( session->peer_cert_digest == NULL )
8542 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8543
8544 memcpy( session->peer_cert_digest, p,
8545 session->peer_cert_digest_len );
8546 p += session->peer_cert_digest_len;
8547 }
8548#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8549#endif /* MBEDTLS_X509_CRT_PARSE_C */
8550
8551 /*
8552 * Session ticket and associated data
8553 */
8554#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8555 if( 3 > (size_t)( end - p ) )
8556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8557
8558 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8559 p += 3;
8560
8561 if( session->ticket_len != 0 )
8562 {
8563 if( session->ticket_len > (size_t)( end - p ) )
8564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8565
8566 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8567 if( session->ticket == NULL )
8568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8569
8570 memcpy( session->ticket, p, session->ticket_len );
8571 p += session->ticket_len;
8572 }
8573
8574 if( 4 > (size_t)( end - p ) )
8575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8576
8577 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8578 ( (uint32_t) p[1] << 16 ) |
8579 ( (uint32_t) p[2] << 8 ) |
8580 ( (uint32_t) p[3] );
8581 p += 4;
8582#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8583
8584 /*
8585 * Misc extension-related info
8586 */
8587#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8588 if( 1 > (size_t)( end - p ) )
8589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8590
8591 session->mfl_code = *p++;
8592#endif
8593
8594#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8595 if( 1 > (size_t)( end - p ) )
8596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8597
8598 session->encrypt_then_mac = *p++;
8599#endif
8600
8601 /* Done, should have consumed entire buffer */
8602 if( p != end )
8603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8604
8605 return( 0 );
8606}
Jerry Yudc7bd172022-02-17 13:44:15 +08008607#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8608
XiaokangQian75d40ef2022-04-20 11:05:24 +00008609int mbedtls_ssl_validate_ciphersuite(
8610 const mbedtls_ssl_context *ssl,
8611 const mbedtls_ssl_ciphersuite_t *suite_info,
8612 mbedtls_ssl_protocol_version min_tls_version,
8613 mbedtls_ssl_protocol_version max_tls_version )
8614{
8615 (void) ssl;
8616
8617 if( suite_info == NULL )
8618 return( -1 );
8619
8620 if( ( suite_info->min_tls_version > max_tls_version ) ||
8621 ( suite_info->max_tls_version < min_tls_version ) )
8622 {
8623 return( -1 );
8624 }
8625
XiaokangQian060d8672022-04-21 09:24:56 +00008626#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008627#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8628 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8629 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8630 {
8631 return( -1 );
8632 }
8633#endif
8634
8635 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8636#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8637 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8638 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8639 {
8640 return( -1 );
8641 }
8642#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8643#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8644
8645 return( 0 );
8646}
8647
Ronald Crone68ab4f2022-10-05 12:46:29 +02008648#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008649/*
8650 * Function for writing a signature algorithm extension.
8651 *
8652 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8653 * value (TLS 1.3 RFC8446):
8654 * enum {
8655 * ....
8656 * ecdsa_secp256r1_sha256( 0x0403 ),
8657 * ecdsa_secp384r1_sha384( 0x0503 ),
8658 * ecdsa_secp521r1_sha512( 0x0603 ),
8659 * ....
8660 * } SignatureScheme;
8661 *
8662 * struct {
8663 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8664 * } SignatureSchemeList;
8665 *
8666 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8667 * value (TLS 1.2 RFC5246):
8668 * enum {
8669 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8670 * sha512(6), (255)
8671 * } HashAlgorithm;
8672 *
8673 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8674 * SignatureAlgorithm;
8675 *
8676 * struct {
8677 * HashAlgorithm hash;
8678 * SignatureAlgorithm signature;
8679 * } SignatureAndHashAlgorithm;
8680 *
8681 * SignatureAndHashAlgorithm
8682 * supported_signature_algorithms<2..2^16-2>;
8683 *
8684 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8685 * generalization of the TLS 1.2 signature algorithm extension.
8686 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8687 * `SignatureScheme` field of TLS 1.3
8688 *
8689 */
8690int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8691 const unsigned char *end, size_t *out_len )
8692{
8693 unsigned char *p = buf;
8694 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8695 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8696
8697 *out_len = 0;
8698
8699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8700
8701 /* Check if we have space for header and length field:
8702 * - extension_type (2 bytes)
8703 * - extension_data_length (2 bytes)
8704 * - supported_signature_algorithms_length (2 bytes)
8705 */
8706 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8707 p += 6;
8708
8709 /*
8710 * Write supported_signature_algorithms
8711 */
8712 supported_sig_alg = p;
8713 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8714 if( sig_alg == NULL )
8715 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8716
8717 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8718 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8720 *sig_alg,
8721 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008722 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8723 continue;
8724 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8725 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8726 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008728 *sig_alg,
8729 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008730 }
8731
8732 /* Length of supported_signature_algorithms */
8733 supported_sig_alg_len = p - supported_sig_alg;
8734 if( supported_sig_alg_len == 0 )
8735 {
8736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8738 }
8739
XiaokangQianeaf36512022-04-24 09:07:44 +00008740 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008741 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008742 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8743
XiaokangQianeaf36512022-04-24 09:07:44 +00008744 *out_len = p - buf;
8745
8746#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8747 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8748#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8749 return( 0 );
8750}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008751#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008752
XiaokangQian40a35232022-05-07 09:02:40 +00008753#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008754/*
8755 * mbedtls_ssl_parse_server_name_ext
8756 *
8757 * Structure of server_name extension:
8758 *
8759 * enum {
8760 * host_name(0), (255)
8761 * } NameType;
8762 * opaque HostName<1..2^16-1>;
8763 *
8764 * struct {
8765 * NameType name_type;
8766 * select (name_type) {
8767 * case host_name: HostName;
8768 * } name;
8769 * } ServerName;
8770 * struct {
8771 * ServerName server_name_list<1..2^16-1>
8772 * } ServerNameList;
8773 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008774MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008775int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8776 const unsigned char *buf,
8777 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008778{
8779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8780 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008781 size_t server_name_list_len, hostname_len;
8782 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008783
XiaokangQianf2a94202022-05-20 06:44:24 +00008784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008785
8786 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008787 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008788 p += 2;
8789
XiaokangQian9b2b7712022-05-17 02:57:00 +00008790 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8791 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008792 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008793 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008794 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008795 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008796 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8797 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008798
8799 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8800 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008801 /* sni_name is intended to be used only during the parsing of the
8802 * ClientHello message (it is reset to NULL before the end of
8803 * the message parsing). Thus it is ok to just point to the
8804 * reception buffer and not make a copy of it.
8805 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008806 ssl->handshake->sni_name = p + 3;
8807 ssl->handshake->sni_name_len = hostname_len;
8808 if( ssl->conf->f_sni == NULL )
8809 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008810 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008811 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008812 if( ret != 0 )
8813 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008814 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008815 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8816 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008817 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8818 }
8819 return( 0 );
8820 }
8821
8822 p += hostname_len + 3;
8823 }
8824
8825 return( 0 );
8826}
8827#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8828
XiaokangQianacb39922022-06-17 10:18:48 +00008829#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008830MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008831int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8832 const unsigned char *buf,
8833 const unsigned char *end )
8834{
8835 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008836 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008837 const unsigned char *protocol_name_list;
8838 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008839 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008840
8841 /* If ALPN not configured, just ignore the extension */
8842 if( ssl->conf->alpn_list == NULL )
8843 return( 0 );
8844
8845 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008846 * RFC7301, section 3.1
8847 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008848 *
XiaokangQianc7403452022-06-23 03:24:12 +00008849 * struct {
8850 * ProtocolName protocol_name_list<2..2^16-1>
8851 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008852 */
8853
XiaokangQianc7403452022-06-23 03:24:12 +00008854 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008855 * protocol_name_list_len 2 bytes
8856 * protocol_name_len 1 bytes
8857 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008858 */
XiaokangQianacb39922022-06-17 10:18:48 +00008859 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8860
XiaokangQianc7403452022-06-23 03:24:12 +00008861 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008862 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008863 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008864 protocol_name_list = p;
8865 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008866
8867 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008868 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008869 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008870 protocol_name_len = *p++;
8871 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8872 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008873 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008874 {
8875 MBEDTLS_SSL_PEND_FATAL_ALERT(
8876 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8877 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008878 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008879 }
8880
8881 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008882 }
8883
8884 /* Use our order of preference */
8885 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8886 {
8887 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008888 p = protocol_name_list;
8889 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008890 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008891 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008892 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008893 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008894 {
8895 ssl->alpn_chosen = *alpn;
8896 return( 0 );
8897 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008898
8899 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008900 }
8901 }
8902
XiaokangQian95d5f542022-06-24 02:29:26 +00008903 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008904 MBEDTLS_SSL_PEND_FATAL_ALERT(
8905 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8906 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8907 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8908}
8909
8910int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8911 unsigned char *buf,
8912 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008913 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008914{
8915 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008916 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008917 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008918
8919 if( ssl->alpn_chosen == NULL )
8920 {
8921 return( 0 );
8922 }
8923
XiaokangQian95d5f542022-06-24 02:29:26 +00008924 protocol_name_len = strlen( ssl->alpn_chosen );
8925 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008926
8927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8928 /*
8929 * 0 . 1 ext identifier
8930 * 2 . 3 ext length
8931 * 4 . 5 protocol list length
8932 * 6 . 6 protocol name length
8933 * 7 . 7+n protocol name
8934 */
8935 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8936
XiaokangQian95d5f542022-06-24 02:29:26 +00008937 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008938
XiaokangQian95d5f542022-06-24 02:29:26 +00008939 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8940 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008941 /* Note: the length of the chosen protocol has been checked to be less
8942 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8943 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008944 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008945
XiaokangQian95d5f542022-06-24 02:29:26 +00008946 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008947 return ( 0 );
8948}
8949#endif /* MBEDTLS_SSL_ALPN */
8950
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008951#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008952 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008953 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008954 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008955int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8956 const char *hostname )
8957{
8958 /* Initialize to suppress unnecessary compiler warning */
8959 size_t hostname_len = 0;
8960
8961 /* Check if new hostname is valid before
8962 * making any change to current one */
8963 if( hostname != NULL )
8964 {
8965 hostname_len = strlen( hostname );
8966
8967 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8969 }
8970
8971 /* Now it's clear that we will overwrite the old hostname,
8972 * so we can free it safely */
8973 if( session->hostname != NULL )
8974 {
8975 mbedtls_platform_zeroize( session->hostname,
8976 strlen( session->hostname ) );
8977 mbedtls_free( session->hostname );
8978 }
8979
8980 /* Passing NULL as hostname shall clear the old one */
8981 if( hostname == NULL )
8982 {
8983 session->hostname = NULL;
8984 }
8985 else
8986 {
8987 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8988 if( session->hostname == NULL )
8989 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8990
8991 memcpy( session->hostname, hostname, hostname_len );
8992 }
8993
8994 return( 0 );
8995}
Xiaokang Qian03409292022-10-12 02:49:52 +00008996#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8997 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008998 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008999 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001#endif /* MBEDTLS_SSL_TLS_C */