blob: b3210c41554275467450784c04647916ccab0a87 [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
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yuc73c6182022-02-08 20:29:25 +0800619void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
620 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
621{
622 ((void) ciphersuite_info);
623
Andrzej Kurek25f27152022-08-17 16:09:31 -0400624#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800625 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
626 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
627 else
628#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400629#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800630 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
631 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
632 else
633#endif
634 {
635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
636 return;
637 }
638}
639
XiaokangQianadab9a62022-07-18 07:41:26 +0000640void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
641 unsigned hs_type,
642 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100643{
644 unsigned char hs_hdr[4];
645
646 /* Build HS header for checksum update. */
647 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
648 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
649 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
650 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
651
652 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
653}
654
655void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
656 unsigned hs_type,
657 unsigned char const *msg,
658 size_t msg_len )
659{
660 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
661 ssl->handshake->update_checksum( ssl, msg, msg_len );
662}
663
Jerry Yuc73c6182022-02-08 20:29:25 +0800664void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
665{
666 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400667#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800668#if defined(MBEDTLS_USE_PSA_CRYPTO)
669 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
670 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
671#else
672 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
673#endif
674#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400675#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800676#if defined(MBEDTLS_USE_PSA_CRYPTO)
677 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
678 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
679#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400680 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800681#endif
682#endif
683}
684
685static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
686 const unsigned char *buf, size_t len )
687{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400688#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800689#if defined(MBEDTLS_USE_PSA_CRYPTO)
690 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
691#else
692 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
693#endif
694#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400695#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800696#if defined(MBEDTLS_USE_PSA_CRYPTO)
697 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
698#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400699 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800700#endif
701#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400702#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
703 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
704 (void) ssl;
705 (void) buf;
706 (void) len;
707#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800708}
709
Andrzej Kurek25f27152022-08-17 16:09:31 -0400710#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800711static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
712 const unsigned char *buf, size_t len )
713{
714#if defined(MBEDTLS_USE_PSA_CRYPTO)
715 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
716#else
717 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
718#endif
719}
720#endif
721
Andrzej Kurek25f27152022-08-17 16:09:31 -0400722#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800723static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
724 const unsigned char *buf, size_t len )
725{
726#if defined(MBEDTLS_USE_PSA_CRYPTO)
727 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
728#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400729 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800730#endif
731}
732#endif
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200737
Andrzej Kurek25f27152022-08-17 16:09:31 -0400738#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500739#if defined(MBEDTLS_USE_PSA_CRYPTO)
740 handshake->fin_sha256_psa = psa_hash_operation_init();
741 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
742#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200744 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200745#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500746#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400747#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500749 handshake->fin_sha384_psa = psa_hash_operation_init();
750 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500751#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400752 mbedtls_sha512_init( &handshake->fin_sha384 );
753 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200754#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500755#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200756
757 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_DHM_C)
760 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200762#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200764#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200765#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200766 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200767#if defined(MBEDTLS_SSL_CLI_C)
768 handshake->ecjpake_cache = NULL;
769 handshake->ecjpake_cache_len = 0;
770#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200771#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200772
Gilles Peskineeccd8882020-03-10 12:19:08 +0100773#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200774 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200775#endif
776
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200777#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
778 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
779#endif
Hanno Becker75173122019-02-06 16:18:31 +0000780
781#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
782 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
783 mbedtls_pk_init( &handshake->peer_pubkey );
784#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200785}
786
Hanno Beckera18d1322018-01-03 14:27:32 +0000787void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200790
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100791#if defined(MBEDTLS_USE_PSA_CRYPTO)
792 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
793 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100794#else
795 mbedtls_cipher_init( &transform->cipher_ctx_enc );
796 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100797#endif
798
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000799#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100800#if defined(MBEDTLS_USE_PSA_CRYPTO)
801 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
802 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100803#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_md_init( &transform->md_ctx_enc );
805 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000806#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100807#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200808}
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200813}
814
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200815MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000817{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200818 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000819 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200821 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200823 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200824 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200825
826 /*
827 * Either the pointers are now NULL or cleared properly and can be freed.
828 * Now allocate missing structures.
829 */
830 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200831 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200832 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200833 }
Paul Bakker48916f92012-09-16 19:57:18 +0000834
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200835 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200836 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200837 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200838 }
Paul Bakker48916f92012-09-16 19:57:18 +0000839
Paul Bakker82788fb2014-10-20 13:59:19 +0200840 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200841 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200842 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200843 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500844#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
845 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400846
Andrzej Kurek4a063792020-10-21 15:08:44 +0200847 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
848 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500849#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000850
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200851 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000852 if( ssl->handshake == NULL ||
853 ssl->transform_negotiate == NULL ||
854 ssl->session_negotiate == NULL )
855 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_free( ssl->handshake );
859 mbedtls_free( ssl->transform_negotiate );
860 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200861
862 ssl->handshake = NULL;
863 ssl->transform_negotiate = NULL;
864 ssl->session_negotiate = NULL;
865
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200866 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000867 }
868
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200869 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000871 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200872 ssl_handshake_params_init( ssl->handshake );
873
Jerry Yud0766ec2022-09-22 10:46:57 +0800874#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
875 defined(MBEDTLS_SSL_SRV_C) && \
876 defined(MBEDTLS_SSL_SESSION_TICKETS)
877 ssl->handshake->new_session_tickets_count =
878 ssl->conf->new_session_tickets_count ;
879#endif
880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
883 {
884 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200885
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
887 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
888 else
889 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200890
Hanno Becker0f57a652020-02-05 10:37:26 +0000891 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200892 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200893#endif
894
Brett Warrene0edc842021-08-17 09:53:13 +0100895/*
896 * curve_list is translated to IANA TLS group identifiers here because
897 * mbedtls_ssl_conf_curves returns void and so can't return
898 * any error codes.
899 */
900#if defined(MBEDTLS_ECP_C)
901#if !defined(MBEDTLS_DEPRECATED_REMOVED)
902 /* Heap allocate and translate curve_list from internal to IANA group ids */
903 if ( ssl->conf->curve_list != NULL )
904 {
905 size_t length;
906 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
907
908 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
909 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
910
911 /* Leave room for zero termination */
912 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
913 if ( group_list == NULL )
914 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
915
916 for( size_t i = 0; i < length; i++ )
917 {
918 const mbedtls_ecp_curve_info *info =
919 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
920 if ( info == NULL )
921 {
922 mbedtls_free( group_list );
923 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
924 }
925 group_list[i] = info->tls_id;
926 }
927
928 group_list[length] = 0;
929
930 ssl->handshake->group_list = group_list;
931 ssl->handshake->group_list_heap_allocated = 1;
932 }
933 else
934 {
935 ssl->handshake->group_list = ssl->conf->group_list;
936 ssl->handshake->group_list_heap_allocated = 0;
937 }
938#endif /* MBEDTLS_DEPRECATED_REMOVED */
939#endif /* MBEDTLS_ECP_C */
940
Ronald Crone68ab4f2022-10-05 12:46:29 +0200941#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +0800942#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800943#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800944 /* Heap allocate and translate sig_hashes from internal hash identifiers to
945 signature algorithms IANA identifiers. */
946 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800947 ssl->conf->sig_hashes != NULL )
948 {
949 const int *md;
950 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800951 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800952 uint16_t *p;
953
Jerry Yub476a442022-01-21 18:14:45 +0800954#if defined(static_assert)
955 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
956 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
957 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800958#endif
959
Jerry Yuf017ee42022-01-12 15:49:48 +0800960 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
961 {
962 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
963 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800964#if defined(MBEDTLS_ECDSA_C)
965 sig_algs_len += sizeof( uint16_t );
966#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800967
Jerry Yub476a442022-01-21 18:14:45 +0800968#if defined(MBEDTLS_RSA_C)
969 sig_algs_len += sizeof( uint16_t );
970#endif
971 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
972 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800973 }
974
Jerry Yub476a442022-01-21 18:14:45 +0800975 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800976 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
977
Jerry Yub476a442022-01-21 18:14:45 +0800978 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
979 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800980 if( ssl->handshake->sig_algs == NULL )
981 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
982
983 p = (uint16_t *)ssl->handshake->sig_algs;
984 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
985 {
986 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
987 if( hash == MBEDTLS_SSL_HASH_NONE )
988 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800989#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800990 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
991 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800992#endif
993#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800994 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
995 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800996#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800997 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200998 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800999 ssl->handshake->sig_algs_heap_allocated = 1;
1000 }
1001 else
Jerry Yua69269a2022-01-17 21:06:01 +08001002#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001003 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001004 ssl->handshake->sig_algs_heap_allocated = 0;
1005 }
Jerry Yucc539102022-06-27 16:27:35 +08001006#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001007#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 return( 0 );
1009}
1010
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001011#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001012/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001013MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001014static int ssl_cookie_write_dummy( void *ctx,
1015 unsigned char **p, unsigned char *end,
1016 const unsigned char *cli_id, size_t cli_id_len )
1017{
1018 ((void) ctx);
1019 ((void) p);
1020 ((void) end);
1021 ((void) cli_id);
1022 ((void) cli_id_len);
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001025}
1026
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001027MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001028static int ssl_cookie_check_dummy( void *ctx,
1029 const unsigned char *cookie, size_t cookie_len,
1030 const unsigned char *cli_id, size_t cli_id_len )
1031{
1032 ((void) ctx);
1033 ((void) cookie);
1034 ((void) cookie_len);
1035 ((void) cli_id);
1036 ((void) cli_id_len);
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001039}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001040#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042/*
1043 * Initialize an SSL context
1044 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001045void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1046{
1047 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1048}
1049
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001050MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001051static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1052{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001053 const mbedtls_ssl_config *conf = ssl->conf;
1054
Ronald Cron6f135e12021-12-08 16:57:54 +01001055#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001056 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1057 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001058 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1059 {
1060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1061 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1062 }
1063
Jerry Yu60835a82021-08-04 10:13:52 +08001064 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1065 return( 0 );
1066 }
1067#endif
1068
1069#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001070 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001071 {
1072 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1073 return( 0 );
1074 }
1075#endif
1076
Ronald Cron6f135e12021-12-08 16:57:54 +01001077#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001078 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001079 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001080 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1081 {
1082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1083 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1084 }
1085
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001086 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1087 {
1088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1089 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1090 }
1091
1092
Ronald Crone1d3f062022-02-10 14:50:54 +01001093 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1094 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001095 }
1096#endif
1097
1098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1099 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1100}
1101
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001102MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001103static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1104{
1105 int ret;
1106 ret = ssl_conf_version_check( ssl );
1107 if( ret != 0 )
1108 return( ret );
1109
1110 /* Space for further checks */
1111
1112 return( 0 );
1113}
1114
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001115/*
1116 * Setup an SSL context
1117 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001118
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001119int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001120 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121{
Janos Follath865b3eb2019-12-16 11:46:15 +00001122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001123 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1124 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001126 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001127
Jerry Yu60835a82021-08-04 10:13:52 +08001128 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1129 return( ret );
1130
Paul Bakker62f2dee2012-09-28 07:31:51 +00001131 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001132 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001133 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001134
1135 /* Set to NULL in case of an error condition */
1136 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001137
Darryl Greenb33cc762019-11-28 14:29:44 +00001138#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1139 ssl->in_buf_len = in_buf_len;
1140#endif
1141 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001142 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001145 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001146 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001147 }
1148
Darryl Greenb33cc762019-11-28 14:29:44 +00001149#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1150 ssl->out_buf_len = out_buf_len;
1151#endif
1152 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001153 if( ssl->out_buf == NULL )
1154 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001156 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001157 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 }
1159
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001160 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001161
Johan Pascalb62bb512015-12-03 21:56:45 +01001162#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001163 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001164#endif
1165
Paul Bakker48916f92012-09-16 19:57:18 +00001166 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001167 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
1169 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001170
1171error:
1172 mbedtls_free( ssl->in_buf );
1173 mbedtls_free( ssl->out_buf );
1174
1175 ssl->conf = NULL;
1176
Darryl Greenb33cc762019-11-28 14:29:44 +00001177#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1178 ssl->in_buf_len = 0;
1179 ssl->out_buf_len = 0;
1180#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001181 ssl->in_buf = NULL;
1182 ssl->out_buf = NULL;
1183
1184 ssl->in_hdr = NULL;
1185 ssl->in_ctr = NULL;
1186 ssl->in_len = NULL;
1187 ssl->in_iv = NULL;
1188 ssl->in_msg = NULL;
1189
1190 ssl->out_hdr = NULL;
1191 ssl->out_ctr = NULL;
1192 ssl->out_len = NULL;
1193 ssl->out_iv = NULL;
1194 ssl->out_msg = NULL;
1195
k-stachowiak9f7798e2018-07-31 16:52:32 +02001196 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001197}
1198
1199/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001200 * Reset an initialized and used SSL context for re-use while retaining
1201 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001202 *
1203 * If partial is non-zero, keep data in the input buffer and client ID.
1204 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001205 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001206void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1207 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001208{
Darryl Greenb33cc762019-11-28 14:29:44 +00001209#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1210 size_t in_buf_len = ssl->in_buf_len;
1211 size_t out_buf_len = ssl->out_buf_len;
1212#else
1213 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1214 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1215#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001216
Hanno Beckerb0302c42021-08-03 09:39:42 +01001217#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1218 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001219#endif
1220
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001221 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001222 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001223
Hanno Beckerb0302c42021-08-03 09:39:42 +01001224 mbedtls_ssl_reset_in_out_pointers( ssl );
1225
1226 /* Reset incoming message parsing */
1227 ssl->in_offt = NULL;
1228 ssl->nb_zero = 0;
1229 ssl->in_msgtype = 0;
1230 ssl->in_msglen = 0;
1231 ssl->in_hslen = 0;
1232 ssl->keep_current_message = 0;
1233 ssl->transform_in = NULL;
1234
1235#if defined(MBEDTLS_SSL_PROTO_DTLS)
1236 ssl->next_record_offset = 0;
1237 ssl->in_epoch = 0;
1238#endif
1239
1240 /* Keep current datagram if partial == 1 */
1241 if( partial == 0 )
1242 {
1243 ssl->in_left = 0;
1244 memset( ssl->in_buf, 0, in_buf_len );
1245 }
1246
Ronald Cronad8c17b2022-06-10 17:18:09 +02001247 ssl->send_alert = 0;
1248
Hanno Beckerb0302c42021-08-03 09:39:42 +01001249 /* Reset outgoing message writing */
1250 ssl->out_msgtype = 0;
1251 ssl->out_msglen = 0;
1252 ssl->out_left = 0;
1253 memset( ssl->out_buf, 0, out_buf_len );
1254 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1255 ssl->transform_out = NULL;
1256
1257#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1258 mbedtls_ssl_dtls_replay_reset( ssl );
1259#endif
1260
XiaokangQian2b01dc32022-01-21 02:53:13 +00001261#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001262 if( ssl->transform )
1263 {
1264 mbedtls_ssl_transform_free( ssl->transform );
1265 mbedtls_free( ssl->transform );
1266 ssl->transform = NULL;
1267 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001268#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1269
XiaokangQian2b01dc32022-01-21 02:53:13 +00001270#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1271 mbedtls_ssl_transform_free( ssl->transform_application );
1272 mbedtls_free( ssl->transform_application );
1273 ssl->transform_application = NULL;
1274
1275 if( ssl->handshake != NULL )
1276 {
1277 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1278 mbedtls_free( ssl->handshake->transform_earlydata );
1279 ssl->handshake->transform_earlydata = NULL;
1280
1281 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1282 mbedtls_free( ssl->handshake->transform_handshake );
1283 ssl->handshake->transform_handshake = NULL;
1284 }
1285
XiaokangQian2b01dc32022-01-21 02:53:13 +00001286#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001287}
1288
1289int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1290{
1291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1292
1293 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1294
XiaokangQian78b1fa72022-01-19 06:56:30 +00001295 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001296
1297 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_SSL_RENEGOTIATION)
1299 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001300 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001301
1302 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1304 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001307
Hanno Beckerb0302c42021-08-03 09:39:42 +01001308 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001309 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001310 if( ssl->session )
1311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_ssl_session_free( ssl->session );
1313 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001314 ssl->session = NULL;
1315 }
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001318 ssl->alpn_chosen = NULL;
1319#endif
1320
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001321#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001322 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001323#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001324 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001325#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001326 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001327 {
1328 mbedtls_free( ssl->cli_id );
1329 ssl->cli_id = NULL;
1330 ssl->cli_id_len = 0;
1331 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001332#endif
1333
Paul Bakker48916f92012-09-16 19:57:18 +00001334 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1335 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001336
1337 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001338}
1339
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001340/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001341 * Reset an initialized and used SSL context for re-use while retaining
1342 * all application-set variables, function pointers and data.
1343 */
1344int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1345{
Hanno Becker43aefe22020-02-05 10:44:56 +00001346 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001347}
1348
1349/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 * SSL set accessors
1351 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001352void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001353{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001354 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001355}
1356
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001357void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001358{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001359 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001360}
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001363void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001364{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001365 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001366}
1367#endif
1368
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001369void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001371 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001372}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001375
Hanno Becker1841b0a2018-08-24 11:13:57 +01001376void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1377 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001378{
1379 ssl->disable_datagram_packing = !allow_packing;
1380}
1381
1382void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1383 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001384{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001385 conf->hs_timeout_min = min;
1386 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001387}
1388#endif
1389
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001390void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001392 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001393}
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001396void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001397 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001398 void *p_vrfy )
1399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001400 conf->f_vrfy = f_vrfy;
1401 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001404
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001405void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001406 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 void *p_rng )
1408{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001409 conf->f_rng = f_rng;
1410 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001411}
1412
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001413void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001414 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 void *p_dbg )
1416{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001417 conf->f_dbg = f_dbg;
1418 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001419}
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001422 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001423 mbedtls_ssl_send_t *f_send,
1424 mbedtls_ssl_recv_t *f_recv,
1425 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001426{
1427 ssl->p_bio = p_bio;
1428 ssl->f_send = f_send;
1429 ssl->f_recv = f_recv;
1430 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001431}
1432
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001433#if defined(MBEDTLS_SSL_PROTO_DTLS)
1434void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1435{
1436 ssl->mtu = mtu;
1437}
1438#endif
1439
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001440void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001441{
1442 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001443}
1444
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001445void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1446 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001447 mbedtls_ssl_set_timer_t *f_set_timer,
1448 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001449{
1450 ssl->p_timer = p_timer;
1451 ssl->f_set_timer = f_set_timer;
1452 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001453
1454 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001455 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001456}
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001459void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001460 void *p_cache,
1461 mbedtls_ssl_cache_get_t *f_get_cache,
1462 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001463{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001464 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001465 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001466 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_SSL_CLI_C)
1471int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001472{
Janos Follath865b3eb2019-12-16 11:46:15 +00001473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001474
1475 if( ssl == NULL ||
1476 session == NULL ||
1477 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001478 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001481 }
1482
Hanno Beckere810bbc2021-05-14 16:01:05 +01001483 if( ssl->handshake->resume == 1 )
1484 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1485
Jerry Yu21092062022-10-10 21:21:31 +08001486#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1487 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001488 {
Jerry Yu21092062022-10-10 21:21:31 +08001489 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1490 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1491
1492 if( mbedtls_ssl_validate_ciphersuite(
1493 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1494 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1495 {
1496 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1497 session->ciphersuite ) );
1498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1499 }
Jerry Yu40afab62022-10-08 10:42:13 +08001500 }
Jerry Yu21092062022-10-10 21:21:31 +08001501#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001502
Hanno Becker52055ae2019-02-06 14:30:46 +00001503 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1504 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001505 return( ret );
1506
Paul Bakker0a597072012-09-25 21:55:46 +00001507 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001508
1509 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001513void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1514 const int *ciphersuites )
1515{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001516 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001517}
1518
Ronald Cron6f135e12021-12-08 16:57:54 +01001519#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001520void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001521 const int kex_modes )
1522{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001523 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001524}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001525
1526#if defined(MBEDTLS_SSL_EARLY_DATA)
1527void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001528 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001529{
1530 conf->early_data_enabled = early_data_enabled;
1531}
1532#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001533#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001536void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001537 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001538{
1539 conf->cert_profile = profile;
1540}
1541
Glenn Strauss36872db2022-01-22 05:06:31 -05001542static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1543{
1544 mbedtls_ssl_key_cert *cur = key_cert, *next;
1545
1546 while( cur != NULL )
1547 {
1548 next = cur->next;
1549 mbedtls_free( cur );
1550 cur = next;
1551 }
1552}
1553
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001554/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001555MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001556static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1557 mbedtls_x509_crt *cert,
1558 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001559{
niisato8ee24222018-06-25 19:05:48 +09001560 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001561
Glenn Strauss36872db2022-01-22 05:06:31 -05001562 if( cert == NULL )
1563 {
1564 /* Free list if cert is null */
1565 ssl_key_cert_free( *head );
1566 *head = NULL;
1567 return( 0 );
1568 }
1569
niisato8ee24222018-06-25 19:05:48 +09001570 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1571 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001572 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001573
niisato8ee24222018-06-25 19:05:48 +09001574 new_cert->cert = cert;
1575 new_cert->key = key;
1576 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001577
Glenn Strauss36872db2022-01-22 05:06:31 -05001578 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001579 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001580 {
niisato8ee24222018-06-25 19:05:48 +09001581 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001582 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001583 else
1584 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001585 mbedtls_ssl_key_cert *cur = *head;
1586 while( cur->next != NULL )
1587 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001588 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001589 }
1590
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001591 return( 0 );
1592}
1593
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001594int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001595 mbedtls_x509_crt *own_cert,
1596 mbedtls_pk_context *pk_key )
1597{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001598 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001599}
1600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001601void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001602 mbedtls_x509_crt *ca_chain,
1603 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001604{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001605 conf->ca_chain = ca_chain;
1606 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001607
1608#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1609 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1610 * cannot be used together. */
1611 conf->f_ca_cb = NULL;
1612 conf->p_ca_cb = NULL;
1613#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001614}
Hanno Becker5adaad92019-03-27 16:54:37 +00001615
1616#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1617void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001618 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001619 void *p_ca_cb )
1620{
1621 conf->f_ca_cb = f_ca_cb;
1622 conf->p_ca_cb = p_ca_cb;
1623
1624 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1625 * cannot be used together. */
1626 conf->ca_chain = NULL;
1627 conf->ca_crl = NULL;
1628}
1629#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001631
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001632#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001633const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1634 size_t *name_len )
1635{
1636 *name_len = ssl->handshake->sni_name_len;
1637 return( ssl->handshake->sni_name );
1638}
1639
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001640int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1641 mbedtls_x509_crt *own_cert,
1642 mbedtls_pk_context *pk_key )
1643{
1644 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1645 own_cert, pk_key ) );
1646}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001647
1648void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1649 mbedtls_x509_crt *ca_chain,
1650 mbedtls_x509_crl *ca_crl )
1651{
1652 ssl->handshake->sni_ca_chain = ca_chain;
1653 ssl->handshake->sni_ca_crl = ca_crl;
1654}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001655
Glenn Strauss999ef702022-03-11 01:37:23 -05001656#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1657void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1658 const mbedtls_x509_crt *crt)
1659{
1660 ssl->handshake->dn_hints = crt;
1661}
1662#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1663
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001664void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1665 int authmode )
1666{
1667 ssl->handshake->sni_authmode = authmode;
1668}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001669#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1670
Hanno Becker8927c832019-04-03 12:52:50 +01001671#if defined(MBEDTLS_X509_CRT_PARSE_C)
1672void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1673 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1674 void *p_vrfy )
1675{
1676 ssl->f_vrfy = f_vrfy;
1677 ssl->p_vrfy = p_vrfy;
1678}
1679#endif
1680
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001681#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001682/*
1683 * Set EC J-PAKE password for current handshake
1684 */
1685int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1686 const unsigned char *pw,
1687 size_t pw_len )
1688{
1689 mbedtls_ecjpake_role role;
1690
Janos Follath8eb64132016-06-03 15:40:57 +01001691 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1693
1694 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1695 role = MBEDTLS_ECJPAKE_SERVER;
1696 else
1697 role = MBEDTLS_ECJPAKE_CLIENT;
1698
1699 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1700 role,
1701 MBEDTLS_MD_SHA256,
1702 MBEDTLS_ECP_DP_SECP256R1,
1703 pw, pw_len ) );
1704}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001705#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001706
Ronald Cron73fe8df2022-10-05 14:31:43 +02001707#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001708int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001709{
Ronald Crond29e13e2022-10-19 10:33:48 +02001710 if( conf->psk_identity == NULL ||
1711 conf->psk_identity_len == 0 )
1712 {
1713 return( 0 );
1714 }
1715
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001716#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001717 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001718 return( 1 );
1719#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001720
1721 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001722 return( 1 );
1723
1724 return( 0 );
1725}
1726
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001727static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1728{
1729 /* Remove reference to existing PSK, if any. */
1730#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001731 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001732 {
1733 /* The maintenance of the PSK key slot is the
1734 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001735 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001736 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001737#endif /* MBEDTLS_USE_PSA_CRYPTO */
1738 if( conf->psk != NULL )
1739 {
1740 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1741
1742 mbedtls_free( conf->psk );
1743 conf->psk = NULL;
1744 conf->psk_len = 0;
1745 }
1746
1747 /* Remove reference to PSK identity, if any. */
1748 if( conf->psk_identity != NULL )
1749 {
1750 mbedtls_free( conf->psk_identity );
1751 conf->psk_identity = NULL;
1752 conf->psk_identity_len = 0;
1753 }
1754}
1755
Hanno Becker7390c712018-11-15 13:33:04 +00001756/* This function assumes that PSK identity in the SSL config is unset.
1757 * It checks that the provided identity is well-formed and attempts
1758 * to make a copy of it in the SSL config.
1759 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001760MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001761static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1762 unsigned char const *psk_identity,
1763 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001764{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001765 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001766 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001767 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001768 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001769 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001770 {
1771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1772 }
1773
Hanno Becker7390c712018-11-15 13:33:04 +00001774 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1775 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001777
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001778 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001779 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001780
1781 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001782}
1783
Hanno Becker7390c712018-11-15 13:33:04 +00001784int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1785 const unsigned char *psk, size_t psk_len,
1786 const unsigned char *psk_identity, size_t psk_identity_len )
1787{
Janos Follath865b3eb2019-12-16 11:46:15 +00001788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001789
1790 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001791 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001792 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001793
1794 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001795 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001797 if( psk_len == 0 )
1798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1799 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1801
Hanno Becker7390c712018-11-15 13:33:04 +00001802 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1803 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1804 conf->psk_len = psk_len;
1805 memcpy( conf->psk, psk, conf->psk_len );
1806
1807 /* Check and set PSK Identity */
1808 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1809 if( ret != 0 )
1810 ssl_conf_remove_psk( conf );
1811
1812 return( ret );
1813}
1814
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001815static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1816{
1817#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001818 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001819 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001820 /* The maintenance of the external PSK key slot is the
1821 * user's responsibility. */
1822 if( ssl->handshake->psk_opaque_is_internal )
1823 {
1824 psa_destroy_key( ssl->handshake->psk_opaque );
1825 ssl->handshake->psk_opaque_is_internal = 0;
1826 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001827 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001828 }
Neil Armstronge952a302022-05-03 10:22:14 +02001829#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001830 if( ssl->handshake->psk != NULL )
1831 {
1832 mbedtls_platform_zeroize( ssl->handshake->psk,
1833 ssl->handshake->psk_len );
1834 mbedtls_free( ssl->handshake->psk );
1835 ssl->handshake->psk_len = 0;
1836 }
Neil Armstronge952a302022-05-03 10:22:14 +02001837#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001838}
1839
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001840int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1841 const unsigned char *psk, size_t psk_len )
1842{
Neil Armstrong501c9322022-05-03 09:35:09 +02001843#if defined(MBEDTLS_USE_PSA_CRYPTO)
1844 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001845 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001846 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001848#endif /* MBEDTLS_USE_PSA_CRYPTO */
1849
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001850 if( psk == NULL || ssl->handshake == NULL )
1851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1852
1853 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1854 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1855
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001856 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001857
Neil Armstrong501c9322022-05-03 09:35:09 +02001858#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1860 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1861 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001862 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001863 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1864 else
1865 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1866 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1867 }
1868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001869
Jerry Yu568ec252022-07-22 21:27:34 +08001870#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001871 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1872 {
1873 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1874 psa_set_key_usage_flags( &key_attributes,
1875 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1876 }
1877#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1878
Neil Armstrong501c9322022-05-03 09:35:09 +02001879 psa_set_key_algorithm( &key_attributes, alg );
1880 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1881
1882 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1883 if( status != PSA_SUCCESS )
1884 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1885
1886 /* Allow calling psa_destroy_key() on psk remove */
1887 ssl->handshake->psk_opaque_is_internal = 1;
1888 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1889#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001890 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001891 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001892
1893 ssl->handshake->psk_len = psk_len;
1894 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1895
1896 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001897#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001898}
1899
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001900#if defined(MBEDTLS_USE_PSA_CRYPTO)
1901int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001902 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001903 const unsigned char *psk_identity,
1904 size_t psk_identity_len )
1905{
Janos Follath865b3eb2019-12-16 11:46:15 +00001906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001907
1908 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001909 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001910 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001911
Hanno Becker7390c712018-11-15 13:33:04 +00001912 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001913 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001915 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001916
1917 /* Check and set PSK Identity */
1918 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1919 psk_identity_len );
1920 if( ret != 0 )
1921 ssl_conf_remove_psk( conf );
1922
1923 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001924}
1925
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001926int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1927 mbedtls_svc_key_id_t psk )
1928{
1929 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1930 ( ssl->handshake == NULL ) )
1931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1932
1933 ssl_remove_psk( ssl );
1934 ssl->handshake->psk_opaque = psk;
1935 return( 0 );
1936}
1937#endif /* MBEDTLS_USE_PSA_CRYPTO */
1938
Jerry Yu8897c072022-08-12 13:56:53 +08001939#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001940void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1941 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1942 size_t),
1943 void *p_psk )
1944{
1945 conf->f_psk = f_psk;
1946 conf->p_psk = p_psk;
1947}
Jerry Yu8897c072022-08-12 13:56:53 +08001948#endif /* MBEDTLS_SSL_SRV_C */
1949
Ronald Cron73fe8df2022-10-05 14:31:43 +02001950#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001951
1952#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001953static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1954 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001955{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001956#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001957 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001958 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001959#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001960 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001961 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001962 return( MBEDTLS_SSL_MODE_STREAM );
1963}
1964
1965#else /* MBEDTLS_USE_PSA_CRYPTO */
1966
1967static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1968 mbedtls_cipher_mode_t mode )
1969{
1970#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1971 if( mode == MBEDTLS_MODE_CBC )
1972 return( MBEDTLS_SSL_MODE_CBC );
1973#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1974
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001975#if defined(MBEDTLS_GCM_C) || \
1976 defined(MBEDTLS_CCM_C) || \
1977 defined(MBEDTLS_CHACHAPOLY_C)
1978 if( mode == MBEDTLS_MODE_GCM ||
1979 mode == MBEDTLS_MODE_CCM ||
1980 mode == MBEDTLS_MODE_CHACHAPOLY )
1981 return( MBEDTLS_SSL_MODE_AEAD );
1982#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001983
1984 return( MBEDTLS_SSL_MODE_STREAM );
1985}
Gilles Peskine301711e2022-04-26 16:57:05 +02001986#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001987
Gilles Peskinee108d982022-04-26 16:50:40 +02001988static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1989 mbedtls_ssl_mode_t base_mode,
1990 int encrypt_then_mac )
1991{
1992#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1993 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1994 base_mode == MBEDTLS_SSL_MODE_CBC )
1995 {
1996 return( MBEDTLS_SSL_MODE_CBC_ETM );
1997 }
1998#else
1999 (void) encrypt_then_mac;
2000#endif
2001 return( base_mode );
2002}
2003
Neil Armstrongab555e02022-04-04 11:07:59 +02002004mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002005 const mbedtls_ssl_transform *transform )
2006{
Gilles Peskinee108d982022-04-26 16:50:40 +02002007 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002008#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002009 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002010#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002011 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2012#endif
2013 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002014
Gilles Peskinee108d982022-04-26 16:50:40 +02002015 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002016#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002017 encrypt_then_mac = transform->encrypt_then_mac;
2018#endif
2019 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002020}
2021
Neil Armstrongab555e02022-04-04 11:07:59 +02002022mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002023#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002024 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002025#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002026 const mbedtls_ssl_ciphersuite_t *suite )
2027{
Gilles Peskinee108d982022-04-26 16:50:40 +02002028 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2029
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002030#if defined(MBEDTLS_USE_PSA_CRYPTO)
2031 psa_status_t status;
2032 psa_algorithm_t alg;
2033 psa_key_type_t type;
2034 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002035 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2036 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002037 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002038#else
2039 const mbedtls_cipher_info_t *cipher =
2040 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002041 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002042 {
2043 base_mode =
2044 mbedtls_ssl_get_base_mode(
2045 mbedtls_cipher_info_get_mode( cipher ) );
2046 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002047#endif /* MBEDTLS_USE_PSA_CRYPTO */
2048
Gilles Peskinee108d982022-04-26 16:50:40 +02002049#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2050 int encrypt_then_mac = 0;
2051#endif
2052 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002053}
2054
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002055#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002056
2057#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002058/* Serialization of TLS 1.3 sessions:
2059 *
2060 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002061 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002062 * uint64 ticket_received;
2063 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002064 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002065 * } ClientOnlyData;
2066 *
2067 * struct {
2068 * uint8 endpoint;
2069 * uint8 ciphersuite[2];
2070 * uint32 ticket_age_add;
2071 * uint8 ticket_flags;
2072 * opaque resumption_key<0..255>;
2073 * select ( endpoint ) {
2074 * case client: ClientOnlyData;
2075 * case server: uint64 start_time;
2076 * };
2077 * } serialized_session_tls13;
2078 *
2079 */
2080#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002081MBEDTLS_CHECK_RETURN_CRITICAL
2082static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2083 unsigned char *buf,
2084 size_t buf_len,
2085 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002086{
2087 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002088#if defined(MBEDTLS_SSL_CLI_C) && \
2089 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2090 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002091 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002092#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002093 size_t needed = 1 /* endpoint */
2094 + 2 /* ciphersuite */
2095 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002096 + 1 /* ticket_flags */
2097 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002098 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002099
2100 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2102 needed += session->resumption_key_len; /* resumption_key */
2103
Jerry Yu438ddd82022-07-07 06:55:50 +00002104#if defined(MBEDTLS_HAVE_TIME)
2105 needed += 8; /* start_time or ticket_received */
2106#endif
2107
2108#if defined(MBEDTLS_SSL_CLI_C)
2109 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2110 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002111#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002112 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002113 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002114#endif
2115
Jerry Yu438ddd82022-07-07 06:55:50 +00002116 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002117 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002118
2119 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002120 if( session->ticket_len > SIZE_MAX - needed )
2121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002122
Jerry Yue28d9742022-08-18 15:44:03 +08002123 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002124 }
2125#endif /* MBEDTLS_SSL_CLI_C */
2126
Jerry Yue36fdd62022-08-17 21:31:36 +08002127 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002128 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002129 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002130
2131 p[0] = session->endpoint;
2132 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2133 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2134 p[7] = session->ticket_flags;
2135
2136 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002137 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002138 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002139 memcpy( p, session->resumption_key, session->resumption_key_len );
2140 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002141
2142#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2143 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2144 {
2145 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, 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 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2155 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002156 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002157 {
2158 /* save host name */
2159 memcpy( p, session->hostname, hostname_len );
2160 p += hostname_len;
2161 }
2162#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2163
Jerry Yu438ddd82022-07-07 06:55:50 +00002164#if defined(MBEDTLS_HAVE_TIME)
2165 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2166 p += 8;
2167#endif
2168 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2169 p += 4;
2170
2171 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2172 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002173
2174 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002175 {
2176 memcpy( p, session->ticket, session->ticket_len );
2177 p += session->ticket_len;
2178 }
2179 }
2180#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002181 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002182}
2183
2184MBEDTLS_CHECK_RETURN_CRITICAL
2185static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2186 const unsigned char *buf,
2187 size_t len )
2188{
2189 const unsigned char *p = buf;
2190 const unsigned char *end = buf + len;
2191
2192 if( end - p < 9 )
2193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2194 session->endpoint = p[0];
2195 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2196 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2197 session->ticket_flags = p[7];
2198
2199 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002200 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002201 p += 9;
2202
Jerry Yubc7c1a42022-07-21 22:57:37 +08002203 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2205
Jerry Yubc7c1a42022-07-21 22:57:37 +08002206 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002208 memcpy( session->resumption_key, p, session->resumption_key_len );
2209 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002210
2211#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2212 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2213 {
2214 if( end - p < 8 )
2215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2216 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2217 p += 8;
2218 }
2219#endif /* MBEDTLS_HAVE_TIME */
2220
2221#if defined(MBEDTLS_SSL_CLI_C)
2222 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2223 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002224#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2225 defined(MBEDTLS_SSL_SESSION_TICKETS)
2226 size_t hostname_len;
2227 /* load host name */
2228 if( end - p < 2 )
2229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2230 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2231 p += 2;
2232
2233 if( end - p < ( long int )hostname_len )
2234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2235 if( hostname_len > 0 )
2236 {
2237 session->hostname = mbedtls_calloc( 1, hostname_len );
2238 if( session->hostname == NULL )
2239 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2240 memcpy( session->hostname, p, hostname_len );
2241 p += hostname_len;
2242 }
2243#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2244 MBEDTLS_SSL_SESSION_TICKETS */
2245
Jerry Yu438ddd82022-07-07 06:55:50 +00002246#if defined(MBEDTLS_HAVE_TIME)
2247 if( end - p < 8 )
2248 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2249 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2250 p += 8;
2251#endif
2252 if( end - p < 4 )
2253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2254 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2255 p += 4;
2256
2257 if( end - p < 2 )
2258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2259 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2260 p += 2;
2261
2262 if( end - p < ( long int )session->ticket_len )
2263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2264 if( session->ticket_len > 0 )
2265 {
2266 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2267 if( session->ticket == NULL )
2268 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2269 memcpy( session->ticket, p, session->ticket_len );
2270 p += session->ticket_len;
2271 }
2272 }
2273#endif /* MBEDTLS_SSL_CLI_C */
2274
2275 return( 0 );
2276
2277}
2278#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002279MBEDTLS_CHECK_RETURN_CRITICAL
2280static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2281 unsigned char *buf,
2282 size_t buf_len,
2283 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002284{
2285 ((void) session);
2286 ((void) buf);
2287 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002288 *olen = 0;
2289 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002290}
Jerry Yu438ddd82022-07-07 06:55:50 +00002291
2292static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2293 unsigned char *buf,
2294 size_t buf_len )
2295{
2296 ((void) session);
2297 ((void) buf);
2298 ((void) buf_len);
2299 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2300}
2301#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002302#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2303
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002304psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002305 size_t taglen,
2306 psa_algorithm_t *alg,
2307 psa_key_type_t *key_type,
2308 size_t *key_size )
2309{
2310 switch ( mbedtls_cipher_type )
2311 {
2312 case MBEDTLS_CIPHER_AES_128_CBC:
2313 *alg = PSA_ALG_CBC_NO_PADDING;
2314 *key_type = PSA_KEY_TYPE_AES;
2315 *key_size = 128;
2316 break;
2317 case MBEDTLS_CIPHER_AES_128_CCM:
2318 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2319 *key_type = PSA_KEY_TYPE_AES;
2320 *key_size = 128;
2321 break;
2322 case MBEDTLS_CIPHER_AES_128_GCM:
2323 *alg = PSA_ALG_GCM;
2324 *key_type = PSA_KEY_TYPE_AES;
2325 *key_size = 128;
2326 break;
2327 case MBEDTLS_CIPHER_AES_192_CCM:
2328 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2329 *key_type = PSA_KEY_TYPE_AES;
2330 *key_size = 192;
2331 break;
2332 case MBEDTLS_CIPHER_AES_192_GCM:
2333 *alg = PSA_ALG_GCM;
2334 *key_type = PSA_KEY_TYPE_AES;
2335 *key_size = 192;
2336 break;
2337 case MBEDTLS_CIPHER_AES_256_CBC:
2338 *alg = PSA_ALG_CBC_NO_PADDING;
2339 *key_type = PSA_KEY_TYPE_AES;
2340 *key_size = 256;
2341 break;
2342 case MBEDTLS_CIPHER_AES_256_CCM:
2343 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2344 *key_type = PSA_KEY_TYPE_AES;
2345 *key_size = 256;
2346 break;
2347 case MBEDTLS_CIPHER_AES_256_GCM:
2348 *alg = PSA_ALG_GCM;
2349 *key_type = PSA_KEY_TYPE_AES;
2350 *key_size = 256;
2351 break;
2352 case MBEDTLS_CIPHER_ARIA_128_CBC:
2353 *alg = PSA_ALG_CBC_NO_PADDING;
2354 *key_type = PSA_KEY_TYPE_ARIA;
2355 *key_size = 128;
2356 break;
2357 case MBEDTLS_CIPHER_ARIA_128_CCM:
2358 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2359 *key_type = PSA_KEY_TYPE_ARIA;
2360 *key_size = 128;
2361 break;
2362 case MBEDTLS_CIPHER_ARIA_128_GCM:
2363 *alg = PSA_ALG_GCM;
2364 *key_type = PSA_KEY_TYPE_ARIA;
2365 *key_size = 128;
2366 break;
2367 case MBEDTLS_CIPHER_ARIA_192_CCM:
2368 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2369 *key_type = PSA_KEY_TYPE_ARIA;
2370 *key_size = 192;
2371 break;
2372 case MBEDTLS_CIPHER_ARIA_192_GCM:
2373 *alg = PSA_ALG_GCM;
2374 *key_type = PSA_KEY_TYPE_ARIA;
2375 *key_size = 192;
2376 break;
2377 case MBEDTLS_CIPHER_ARIA_256_CBC:
2378 *alg = PSA_ALG_CBC_NO_PADDING;
2379 *key_type = PSA_KEY_TYPE_ARIA;
2380 *key_size = 256;
2381 break;
2382 case MBEDTLS_CIPHER_ARIA_256_CCM:
2383 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2384 *key_type = PSA_KEY_TYPE_ARIA;
2385 *key_size = 256;
2386 break;
2387 case MBEDTLS_CIPHER_ARIA_256_GCM:
2388 *alg = PSA_ALG_GCM;
2389 *key_type = PSA_KEY_TYPE_ARIA;
2390 *key_size = 256;
2391 break;
2392 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2393 *alg = PSA_ALG_CBC_NO_PADDING;
2394 *key_type = PSA_KEY_TYPE_CAMELLIA;
2395 *key_size = 128;
2396 break;
2397 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2398 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2399 *key_type = PSA_KEY_TYPE_CAMELLIA;
2400 *key_size = 128;
2401 break;
2402 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2403 *alg = PSA_ALG_GCM;
2404 *key_type = PSA_KEY_TYPE_CAMELLIA;
2405 *key_size = 128;
2406 break;
2407 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2408 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2409 *key_type = PSA_KEY_TYPE_CAMELLIA;
2410 *key_size = 192;
2411 break;
2412 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2413 *alg = PSA_ALG_GCM;
2414 *key_type = PSA_KEY_TYPE_CAMELLIA;
2415 *key_size = 192;
2416 break;
2417 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2418 *alg = PSA_ALG_CBC_NO_PADDING;
2419 *key_type = PSA_KEY_TYPE_CAMELLIA;
2420 *key_size = 256;
2421 break;
2422 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2423 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2424 *key_type = PSA_KEY_TYPE_CAMELLIA;
2425 *key_size = 256;
2426 break;
2427 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2428 *alg = PSA_ALG_GCM;
2429 *key_type = PSA_KEY_TYPE_CAMELLIA;
2430 *key_size = 256;
2431 break;
2432 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2433 *alg = PSA_ALG_CHACHA20_POLY1305;
2434 *key_type = PSA_KEY_TYPE_CHACHA20;
2435 *key_size = 256;
2436 break;
2437 case MBEDTLS_CIPHER_NULL:
2438 *alg = MBEDTLS_SSL_NULL_CIPHER;
2439 *key_type = 0;
2440 *key_size = 0;
2441 break;
2442 default:
2443 return PSA_ERROR_NOT_SUPPORTED;
2444 }
2445
2446 return PSA_SUCCESS;
2447}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002448#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002449
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002450#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002451int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2452 const unsigned char *dhm_P, size_t P_len,
2453 const unsigned char *dhm_G, size_t G_len )
2454{
Janos Follath865b3eb2019-12-16 11:46:15 +00002455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002456
Glenn Strausscee11292021-12-20 01:43:17 -05002457 mbedtls_mpi_free( &conf->dhm_P );
2458 mbedtls_mpi_free( &conf->dhm_G );
2459
Hanno Beckera90658f2017-10-04 15:29:08 +01002460 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2461 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2462 {
2463 mbedtls_mpi_free( &conf->dhm_P );
2464 mbedtls_mpi_free( &conf->dhm_G );
2465 return( ret );
2466 }
2467
2468 return( 0 );
2469}
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002471int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002472{
Janos Follath865b3eb2019-12-16 11:46:15 +00002473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002474
Glenn Strausscee11292021-12-20 01:43:17 -05002475 mbedtls_mpi_free( &conf->dhm_P );
2476 mbedtls_mpi_free( &conf->dhm_G );
2477
Gilles Peskinee5702482021-06-11 21:59:08 +02002478 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2479 &conf->dhm_P ) ) != 0 ||
2480 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2481 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002482 {
2483 mbedtls_mpi_free( &conf->dhm_P );
2484 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002485 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002486 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002487
2488 return( 0 );
2489}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002490#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002491
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002492#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2493/*
2494 * Set the minimum length for Diffie-Hellman parameters
2495 */
2496void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2497 unsigned int bitlen )
2498{
2499 conf->dhm_min_bitlen = bitlen;
2500}
2501#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2502
Ronald Crone68ab4f2022-10-05 12:46:29 +02002503#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002504#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002505/*
2506 * Set allowed/preferred hashes for handshake signatures
2507 */
2508void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2509 const int *hashes )
2510{
2511 conf->sig_hashes = hashes;
2512}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002513#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002514
Jerry Yuf017ee42022-01-12 15:49:48 +08002515/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002516void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2517 const uint16_t* sig_algs )
2518{
Jerry Yuf017ee42022-01-12 15:49:48 +08002519#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2520 conf->sig_hashes = NULL;
2521#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2522 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002523}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002524#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002525
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002526#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002527#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002528/*
2529 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002530 *
2531 * mbedtls_ssl_setup() takes the provided list
2532 * and translates it to a list of IANA TLS group identifiers,
2533 * stored in ssl->handshake->group_list.
2534 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002535 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002536void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002537 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002539 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002540 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002541}
Brett Warrene0edc842021-08-17 09:53:13 +01002542#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002543#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002544
Brett Warrene0edc842021-08-17 09:53:13 +01002545/*
2546 * Set the allowed groups
2547 */
2548void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2549 const uint16_t *group_list )
2550{
2551#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2552 conf->curve_list = NULL;
2553#endif
2554 conf->group_list = group_list;
2555}
2556
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002557#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002559{
Hanno Becker947194e2017-04-07 13:25:49 +01002560 /* Initialize to suppress unnecessary compiler warning */
2561 size_t hostname_len = 0;
2562
2563 /* Check if new hostname is valid before
2564 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002565 if( hostname != NULL )
2566 {
2567 hostname_len = strlen( hostname );
2568
2569 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2571 }
2572
2573 /* Now it's clear that we will overwrite the old hostname,
2574 * so we can free it safely */
2575
2576 if( ssl->hostname != NULL )
2577 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002578 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002579 mbedtls_free( ssl->hostname );
2580 }
2581
2582 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002583
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002585 {
2586 ssl->hostname = NULL;
2587 }
2588 else
2589 {
2590 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002591 if( ssl->hostname == NULL )
2592 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002593
Hanno Becker947194e2017-04-07 13:25:49 +01002594 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002595
Hanno Becker947194e2017-04-07 13:25:49 +01002596 ssl->hostname[hostname_len] = '\0';
2597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
2599 return( 0 );
2600}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002601#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002603#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002604void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002606 const unsigned char *, size_t),
2607 void *p_sni )
2608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002609 conf->f_sni = f_sni;
2610 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002615int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002616{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002617 size_t cur_len, tot_len;
2618 const char **p;
2619
2620 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002621 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2622 * MUST NOT be truncated."
2623 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002624 */
2625 tot_len = 0;
2626 for( p = protos; *p != NULL; p++ )
2627 {
2628 cur_len = strlen( *p );
2629 tot_len += cur_len;
2630
Ronald Cron8216dd32020-04-23 16:41:44 +02002631 if( ( cur_len == 0 ) ||
2632 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2633 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002635 }
2636
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002637 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002638
2639 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002640}
2641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002643{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002644 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002647
Johan Pascalb62bb512015-12-03 21:56:45 +01002648#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002649void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2650 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002651{
2652 conf->dtls_srtp_mki_support = support_mki_value;
2653}
2654
Ron Eldoref72faf2018-07-12 11:54:20 +03002655int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2656 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002657 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002658{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002659 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002660 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002662 }
Ron Eldor591f1622018-01-22 12:30:04 +02002663
2664 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002665 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002666 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002667 }
Ron Eldor591f1622018-01-22 12:30:04 +02002668
2669 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2670 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002671 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002672}
2673
Ron Eldoref72faf2018-07-12 11:54:20 +03002674int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002675 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002676{
Johan Pascal253d0262020-09-22 13:04:45 +02002677 const mbedtls_ssl_srtp_profile *p;
2678 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002679
Johan Pascal253d0262020-09-22 13:04:45 +02002680 /* check the profiles list: all entry must be valid,
2681 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002682 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2683 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2684 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002685 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002686 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002687 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002688 list_size++;
2689 }
2690 else
2691 {
2692 /* unsupported value, stop parsing and set the size to an error value */
2693 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002694 }
2695 }
2696
Johan Pascal5ef72d22020-10-28 17:05:47 +01002697 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002698 {
Johan Pascal253d0262020-09-22 13:04:45 +02002699 conf->dtls_srtp_profile_list = NULL;
2700 conf->dtls_srtp_profile_list_len = 0;
2701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2702 }
2703
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002704 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002705 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002706
2707 return( 0 );
2708}
2709
Johan Pascal5ef72d22020-10-28 17:05:47 +01002710void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2711 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002712{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002713 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2714 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002715 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002716 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002717 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002718 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002719 else
2720 {
2721 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002722 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2723 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002724 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002725}
Johan Pascalb62bb512015-12-03 21:56:45 +01002726#endif /* MBEDTLS_SSL_DTLS_SRTP */
2727
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302728#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002729void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002730{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002731 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002732}
2733
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002734void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002735{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002736 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002737}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302738#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002739
Janos Follath088ce432017-04-10 12:42:31 +01002740#if defined(MBEDTLS_SSL_SRV_C)
2741void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2742 char cert_req_ca_list )
2743{
2744 conf->cert_req_ca_list = cert_req_ca_list;
2745}
2746#endif
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002749void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002750{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002751 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002752}
2753#endif
2754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002756void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002757{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002758 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002759}
2760#endif
2761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002763int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002766 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002769 }
2770
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002771 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002772
2773 return( 0 );
2774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002777void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002778{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002779 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002780}
2781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002783void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002784{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002785 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002786}
2787
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002788void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002789{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002790 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002791}
2792
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002793void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002794 const unsigned char period[8] )
2795{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002796 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002801#if defined(MBEDTLS_SSL_CLI_C)
2802void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002803{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002804 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002805}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002806#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002807
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002808#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002809
Jerry Yud0766ec2022-09-22 10:46:57 +08002810#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002811void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2812 uint16_t num_tickets )
2813{
Jerry Yud0766ec2022-09-22 10:46:57 +08002814 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002815}
2816#endif
2817
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002818void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2819 mbedtls_ssl_ticket_write_t *f_ticket_write,
2820 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2821 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002822{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002823 conf->f_ticket_write = f_ticket_write;
2824 conf->f_ticket_parse = f_ticket_parse;
2825 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002826}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002827#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002829
Hanno Becker7e6c1782021-06-08 09:24:55 +01002830void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2831 mbedtls_ssl_export_keys_t *f_export_keys,
2832 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002833{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002834 ssl->f_export_keys = f_export_keys;
2835 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002836}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002837
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002838#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002839void mbedtls_ssl_conf_async_private_cb(
2840 mbedtls_ssl_config *conf,
2841 mbedtls_ssl_async_sign_t *f_async_sign,
2842 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2843 mbedtls_ssl_async_resume_t *f_async_resume,
2844 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002845 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002846{
2847 conf->f_async_sign_start = f_async_sign;
2848 conf->f_async_decrypt_start = f_async_decrypt;
2849 conf->f_async_resume = f_async_resume;
2850 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002851 conf->p_async_config_data = async_config_data;
2852}
2853
Gilles Peskine8f97af72018-04-26 11:46:10 +02002854void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2855{
2856 return( conf->p_async_config_data );
2857}
2858
Gilles Peskine1febfef2018-04-30 11:54:39 +02002859void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002860{
2861 if( ssl->handshake == NULL )
2862 return( NULL );
2863 else
2864 return( ssl->handshake->user_async_ctx );
2865}
2866
Gilles Peskine1febfef2018-04-30 11:54:39 +02002867void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002868 void *ctx )
2869{
2870 if( ssl->handshake != NULL )
2871 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002872}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002873#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002874
Paul Bakker5121ce52009-01-03 21:22:43 +00002875/*
2876 * SSL get accessors
2877 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002878uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002879{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002880 if( ssl->session != NULL )
2881 return( ssl->session->verify_result );
2882
2883 if( ssl->session_negotiate != NULL )
2884 return( ssl->session_negotiate->verify_result );
2885
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002886 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887}
2888
Glenn Strauss8f526902022-01-13 00:04:49 -05002889int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2890{
2891 if( ssl == NULL || ssl->session == NULL )
2892 return( 0 );
2893
2894 return( ssl->session->ciphersuite );
2895}
2896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002898{
Paul Bakker926c8e42013-03-06 10:23:34 +01002899 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002900 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002903}
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002906{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002909 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002910 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002911 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002912 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002913 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002914 default:
2915 return( "unknown (DTLS)" );
2916 }
2917 }
2918#endif
2919
Glenn Strauss60bfe602022-03-14 19:04:24 -04002920 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002921 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002922 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002924 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002925 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002926 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002927 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002928 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002929}
2930
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002931#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002932size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2933{
David Horstmann95d516f2021-05-04 18:36:56 +01002934 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002935 size_t read_mfl;
2936
2937 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2939 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2940 {
2941 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2942 }
2943
2944 /* Check if a smaller max length was negotiated */
2945 if( ssl->session_out != NULL )
2946 {
2947 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2948 if( read_mfl < max_len )
2949 {
2950 max_len = read_mfl;
2951 }
2952 }
2953
2954 // During a handshake, use the value being negotiated
2955 if( ssl->session_negotiate != NULL )
2956 {
2957 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2958 if( read_mfl < max_len )
2959 {
2960 max_len = read_mfl;
2961 }
2962 }
2963
2964 return( max_len );
2965}
2966
2967size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002968{
2969 size_t max_len;
2970
2971 /*
2972 * Assume mfl_code is correct since it was checked when set
2973 */
Angus Grattond8213d02016-05-25 20:56:48 +10002974 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002975
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002976 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002977 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002978 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002979 {
Angus Grattond8213d02016-05-25 20:56:48 +10002980 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002981 }
2982
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002983 /* During a handshake, use the value being negotiated */
2984 if( ssl->session_negotiate != NULL &&
2985 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2986 {
2987 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2988 }
2989
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002990 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002991}
2992#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2993
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002995size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002996{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002997 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2998 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2999 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3000 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3001 return ( 0 );
3002
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003003 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3004 return( ssl->mtu );
3005
3006 if( ssl->mtu == 0 )
3007 return( ssl->handshake->mtu );
3008
3009 return( ssl->mtu < ssl->handshake->mtu ?
3010 ssl->mtu : ssl->handshake->mtu );
3011}
3012#endif /* MBEDTLS_SSL_PROTO_DTLS */
3013
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003014int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3015{
3016 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3017
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003018#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3019 !defined(MBEDTLS_SSL_PROTO_DTLS)
3020 (void) ssl;
3021#endif
3022
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003023#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003024 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003025
3026 if( max_len > mfl )
3027 max_len = mfl;
3028#endif
3029
3030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003031 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003032 {
Hanno Becker89490712020-02-05 10:50:12 +00003033 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003034 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3035 const size_t overhead = (size_t) ret;
3036
3037 if( ret < 0 )
3038 return( ret );
3039
3040 if( mtu <= overhead )
3041 {
3042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3043 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3044 }
3045
3046 if( max_len > mtu - overhead )
3047 max_len = mtu - overhead;
3048 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003049#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003050
Hanno Becker0defedb2018-08-10 12:35:02 +01003051#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3052 !defined(MBEDTLS_SSL_PROTO_DTLS)
3053 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003054#endif
3055
3056 return( (int) max_len );
3057}
3058
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003059int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3060{
3061 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3062
3063#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3064 (void) ssl;
3065#endif
3066
3067#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3068 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3069
3070 if( max_len > mfl )
3071 max_len = mfl;
3072#endif
3073
3074 return( (int) max_len );
3075}
3076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#if defined(MBEDTLS_X509_CRT_PARSE_C)
3078const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003079{
3080 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003081 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003082
Hanno Beckere6824572019-02-07 13:18:46 +00003083#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003084 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003085#else
3086 return( NULL );
3087#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003092int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3093 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003094{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003095 int ret;
3096
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003097 if( ssl == NULL ||
3098 dst == NULL ||
3099 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003100 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003103 }
3104
Hanno Beckere810bbc2021-05-14 16:01:05 +01003105 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3106 * idempotent: Each session can only be exported once.
3107 *
3108 * (This is in preparation for TLS 1.3 support where we will
3109 * need the ability to export multiple sessions (aka tickets),
3110 * which will be achieved by calling mbedtls_ssl_get_session()
3111 * multiple times until it fails.)
3112 *
3113 * Check whether we have already exported the current session,
3114 * and fail if so.
3115 */
3116 if( ssl->session->exported == 1 )
3117 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3118
3119 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3120 if( ret != 0 )
3121 return( ret );
3122
3123 /* Remember that we've exported the session. */
3124 ssl->session->exported = 1;
3125 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003128
Paul Bakker5121ce52009-01-03 21:22:43 +00003129/*
Hanno Beckera835da52019-05-16 12:39:07 +01003130 * Define ticket header determining Mbed TLS version
3131 * and structure of the ticket.
3132 */
3133
Hanno Becker94ef3b32019-05-16 12:50:45 +01003134/*
Hanno Becker50b59662019-05-28 14:30:45 +01003135 * Define bitflag determining compile-time settings influencing
3136 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003137 */
3138
Hanno Becker50b59662019-05-28 14:30:45 +01003139#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003140#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003141#else
Hanno Becker3e088662019-05-29 11:10:18 +01003142#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003143#endif /* MBEDTLS_HAVE_TIME */
3144
3145#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003146#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003147#else
Hanno Becker3e088662019-05-29 11:10:18 +01003148#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003149#endif /* MBEDTLS_X509_CRT_PARSE_C */
3150
3151#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003152#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003153#else
Hanno Becker3e088662019-05-29 11:10:18 +01003154#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003155#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3156
3157#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003158#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003159#else
Hanno Becker3e088662019-05-29 11:10:18 +01003160#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003161#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3162
Hanno Becker94ef3b32019-05-16 12:50:45 +01003163#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003164#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003165#else
Hanno Becker3e088662019-05-29 11:10:18 +01003166#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003167#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3168
Hanno Becker94ef3b32019-05-16 12:50:45 +01003169#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3170#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3171#else
3172#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3173#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3174
Hanno Becker3e088662019-05-29 11:10:18 +01003175#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3176#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3177#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3178#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003179#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3180#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003181
Hanno Becker50b59662019-05-28 14:30:45 +01003182#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003183 ( (uint16_t) ( \
3184 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3185 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3186 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3187 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003188 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003189 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003190
Hanno Beckerf8787072019-05-16 12:41:07 +01003191static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003192 MBEDTLS_VERSION_MAJOR,
3193 MBEDTLS_VERSION_MINOR,
3194 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003195 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3196 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003197};
Hanno Beckera835da52019-05-16 12:39:07 +01003198
3199/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003200 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003201 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003202 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003203 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003204 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003205 * opaque mbedtls_version[3]; // library version: major, minor, patch
3206 * opaque session_format[2]; // library-version specific 16-bit field
3207 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003208 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003209 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003210 * Note: When updating the format, remember to keep
3211 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003212 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003213 * // In this version, `session_format` determines
3214 * // the setting of those compile-time
3215 * // configuration options which influence
3216 * // the structure of mbedtls_ssl_session.
3217 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003218 * uint8_t minor_ver; // Protocol minor version. Possible values:
3219 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003220 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003221 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003222 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003223 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003224 * serialized_session_tls12 data;
3225 *
3226 * };
3227 *
3228 * } serialized_session;
3229 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003230 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003231
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003232MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003233static int ssl_session_save( const mbedtls_ssl_session *session,
3234 unsigned char omit_header,
3235 unsigned char *buf,
3236 size_t buf_len,
3237 size_t *olen )
3238{
3239 unsigned char *p = buf;
3240 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003241 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003242#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3243 size_t out_len;
3244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3245#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003246 if( session == NULL )
3247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3248
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003249 if( !omit_header )
3250 {
3251 /*
3252 * Add Mbed TLS version identifier
3253 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003254 used += sizeof( ssl_serialized_session_header );
3255
3256 if( used <= buf_len )
3257 {
3258 memcpy( p, ssl_serialized_session_header,
3259 sizeof( ssl_serialized_session_header ) );
3260 p += sizeof( ssl_serialized_session_header );
3261 }
3262 }
3263
3264 /*
3265 * TLS version identifier
3266 */
3267 used += 1;
3268 if( used <= buf_len )
3269 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003270 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003271 }
3272
3273 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003274 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003275 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003276 {
3277#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003278 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003279 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003280 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003281#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3282
Jerry Yu251a12e2022-07-13 15:15:48 +08003283#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3284 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003285 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3286 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003287 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003288 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003289 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003290#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3291
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003292 default:
3293 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3294 }
3295
3296 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003297 if( used > buf_len )
3298 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003299
3300 return( 0 );
3301}
3302
3303/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003304 * Public wrapper for ssl_session_save()
3305 */
3306int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3307 unsigned char *buf,
3308 size_t buf_len,
3309 size_t *olen )
3310{
3311 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3312}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003313
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003314/*
3315 * Deserialize session, see mbedtls_ssl_session_save() for format.
3316 *
3317 * This internal version is wrapped by a public function that cleans up in
3318 * case of error, and has an extra option omit_header.
3319 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003320MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003321static int ssl_session_load( mbedtls_ssl_session *session,
3322 unsigned char omit_header,
3323 const unsigned char *buf,
3324 size_t len )
3325{
3326 const unsigned char *p = buf;
3327 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003328 size_t remaining_len;
3329
3330
3331 if( session == NULL )
3332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003333
3334 if( !omit_header )
3335 {
3336 /*
3337 * Check Mbed TLS version identifier
3338 */
3339
3340 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3342
3343 if( memcmp( p, ssl_serialized_session_header,
3344 sizeof( ssl_serialized_session_header ) ) != 0 )
3345 {
3346 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3347 }
3348 p += sizeof( ssl_serialized_session_header );
3349 }
3350
3351 /*
3352 * TLS version identifier
3353 */
3354 if( 1 > (size_t)( end - p ) )
3355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003356 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003357
3358 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003359 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003360 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003361 {
3362#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003363 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003364 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003365#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3366
Jerry Yu438ddd82022-07-07 06:55:50 +00003367#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3368 case MBEDTLS_SSL_VERSION_TLS1_3:
3369 return( ssl_tls13_session_load( session, p, remaining_len ) );
3370#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3371
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003372 default:
3373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3374 }
3375}
3376
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003377/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003378 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003379 */
3380int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3381 const unsigned char *buf,
3382 size_t len )
3383{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003384 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003385
3386 if( ret != 0 )
3387 mbedtls_ssl_session_free( session );
3388
3389 return( ret );
3390}
3391
3392/*
Paul Bakker1961b702013-01-25 14:49:24 +01003393 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003395MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003396static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3397{
3398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3399
Ronald Cron66dbf912022-02-02 15:33:46 +01003400 /*
3401 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003402 * that were written into the output buffer by the previous handshake step,
3403 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003404 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3405 * We proceed to the next handshake step only when all data from the
3406 * previous one have been sent to the peer, thus we make sure that this is
3407 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3408 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3409 * we have to wait before to go ahead.
3410 * In the case of TLS 1.3, handshake step handlers do not send data to the
3411 * peer. Data are only sent here and through
3412 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003413 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003414 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003415 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3416 return( ret );
3417
3418#if defined(MBEDTLS_SSL_PROTO_DTLS)
3419 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3420 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3421 {
3422 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3423 return( ret );
3424 }
3425#endif /* MBEDTLS_SSL_PROTO_DTLS */
3426
3427 return( ret );
3428}
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003431{
Hanno Becker41934dd2021-08-07 19:13:43 +01003432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
Hanno Becker41934dd2021-08-07 19:13:43 +01003434 if( ssl == NULL ||
3435 ssl->conf == NULL ||
3436 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003437 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003438 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003440 }
3441
3442 ret = ssl_prepare_handshake_step( ssl );
3443 if( ret != 0 )
3444 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003445
Jerry Yue7047812021-09-13 19:26:39 +08003446 ret = mbedtls_ssl_handle_pending_alert( ssl );
3447 if( ret != 0 )
3448 goto cleanup;
3449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003451 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003452 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3454 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003455
Ronald Cron9f0fba32022-02-10 16:45:15 +01003456 switch( ssl->state )
3457 {
3458 case MBEDTLS_SSL_HELLO_REQUEST:
3459 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3460 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003461
Ronald Cron9f0fba32022-02-10 16:45:15 +01003462 case MBEDTLS_SSL_CLIENT_HELLO:
3463 ret = mbedtls_ssl_write_client_hello( ssl );
3464 break;
3465
3466 default:
3467#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003468 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003469 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3470 else
3471 ret = mbedtls_ssl_handshake_client_step( ssl );
3472#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3473 ret = mbedtls_ssl_handshake_client_step( ssl );
3474#else
3475 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3476#endif
3477 }
Jerry Yub9930e72021-08-06 17:11:51 +08003478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003481 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003482 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003483#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003484 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003485 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003486#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003487
3488#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3489 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3490 ret = mbedtls_ssl_handshake_server_step( ssl );
3491#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003493#endif
3494
Jerry Yue7047812021-09-13 19:26:39 +08003495 if( ret != 0 )
3496 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003497 /* handshake_step return error. And it is same
3498 * with alert_reason.
3499 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003500 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003501 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003502 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003503 goto cleanup;
3504 }
3505 }
3506
3507cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003508 return( ret );
3509}
3510
3511/*
3512 * Perform the SSL handshake
3513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003515{
3516 int ret = 0;
3517
Hanno Beckera817ea42020-10-20 15:20:23 +01003518 /* Sanity checks */
3519
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003520 if( ssl == NULL || ssl->conf == NULL )
3521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3522
Hanno Beckera817ea42020-10-20 15:20:23 +01003523#if defined(MBEDTLS_SSL_PROTO_DTLS)
3524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3525 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3526 {
3527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3528 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3530 }
3531#endif /* MBEDTLS_SSL_PROTO_DTLS */
3532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003534
Hanno Beckera817ea42020-10-20 15:20:23 +01003535 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003536 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003539
3540 if( ret != 0 )
3541 break;
3542 }
3543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003545
3546 return( ret );
3547}
3548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549#if defined(MBEDTLS_SSL_RENEGOTIATION)
3550#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003551/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003552 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003553 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003554MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003556{
Janos Follath865b3eb2019-12-16 11:46:15 +00003557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003560
3561 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3563 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003564
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003565 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003566 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003568 return( ret );
3569 }
3570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003572
3573 return( 0 );
3574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003576
3577/*
3578 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 * - any side: calling mbedtls_ssl_renegotiate(),
3580 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3581 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003582 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003583 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003585 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003586int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003587{
Janos Follath865b3eb2019-12-16 11:46:15 +00003588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003591
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003592 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3593 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003594
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003595 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3596 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003598 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003600 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003601 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003602 ssl->handshake->out_msg_seq = 1;
3603 else
3604 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003605 }
3606#endif
3607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3609 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003614 return( ret );
3615 }
3616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003618
3619 return( 0 );
3620}
3621
3622/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003623 * Renegotiate current connection on client,
3624 * or request renegotiation on server
3625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003629
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003630 if( ssl == NULL || ssl->conf == NULL )
3631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003634 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003635 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003636 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003637 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003641
3642 /* Did we already try/start sending HelloRequest? */
3643 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003645
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003646 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003651 /*
3652 * On client, either start the renegotiation process or,
3653 * if already in progress, continue the handshake
3654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003656 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003657 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003659
Hanno Becker40cdaa12020-02-05 10:48:27 +00003660 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003661 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003663 return( ret );
3664 }
3665 }
3666 else
3667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003671 return( ret );
3672 }
3673 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003675
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003676 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003679
Gilles Peskine9b562d52018-04-25 20:32:43 +02003680void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003681{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003682 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3683
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003684 if( handshake == NULL )
3685 return;
3686
Brett Warrene0edc842021-08-17 09:53:13 +01003687#if defined(MBEDTLS_ECP_C)
3688#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3689 if ( ssl->handshake->group_list_heap_allocated )
3690 mbedtls_free( (void*) handshake->group_list );
3691 handshake->group_list = NULL;
3692#endif /* MBEDTLS_DEPRECATED_REMOVED */
3693#endif /* MBEDTLS_ECP_C */
3694
Ronald Crone68ab4f2022-10-05 12:46:29 +02003695#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003696#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3697 if ( ssl->handshake->sig_algs_heap_allocated )
3698 mbedtls_free( (void*) handshake->sig_algs );
3699 handshake->sig_algs = NULL;
3700#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003701#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3702 if( ssl->handshake->certificate_request_context )
3703 {
3704 mbedtls_free( (void*) handshake->certificate_request_context );
3705 }
3706#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003707#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003708
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003709#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3710 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3711 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003712 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003713 handshake->async_in_progress = 0;
3714 }
3715#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3716
Andrzej Kurek25f27152022-08-17 16:09:31 -04003717#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003718#if defined(MBEDTLS_USE_PSA_CRYPTO)
3719 psa_hash_abort( &handshake->fin_sha256_psa );
3720#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003721 mbedtls_sha256_free( &handshake->fin_sha256 );
3722#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003723#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003724#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003725#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003726 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003727#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003728 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003729#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003730#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732#if defined(MBEDTLS_DHM_C)
3733 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003734#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003735#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003737#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003738#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003739 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003740#if defined(MBEDTLS_SSL_CLI_C)
3741 mbedtls_free( handshake->ecjpake_cache );
3742 handshake->ecjpake_cache = NULL;
3743 handshake->ecjpake_cache_len = 0;
3744#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003745#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003746
Janos Follath4ae5c292016-02-10 11:27:43 +00003747#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3748 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003749 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003751#endif
3752
Ronald Cron73fe8df2022-10-05 14:31:43 +02003753#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003754#if defined(MBEDTLS_USE_PSA_CRYPTO)
3755 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3756 {
3757 /* The maintenance of the external PSK key slot is the
3758 * user's responsibility. */
3759 if( ssl->handshake->psk_opaque_is_internal )
3760 {
3761 psa_destroy_key( ssl->handshake->psk_opaque );
3762 ssl->handshake->psk_opaque_is_internal = 0;
3763 }
3764 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3765 }
Neil Armstronge952a302022-05-03 10:22:14 +02003766#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003767 if( handshake->psk != NULL )
3768 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003769 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003770 mbedtls_free( handshake->psk );
3771 }
Neil Armstronge952a302022-05-03 10:22:14 +02003772#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003773#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3776 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003777 /*
3778 * Free only the linked list wrapper, not the keys themselves
3779 * since the belong to the SNI callback
3780 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003781 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003783
Gilles Peskineeccd8882020-03-10 12:19:08 +01003784#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003785 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003786 if( handshake->ecrs_peer_cert != NULL )
3787 {
3788 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3789 mbedtls_free( handshake->ecrs_peer_cert );
3790 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003791#endif
3792
Hanno Becker75173122019-02-06 16:18:31 +00003793#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3794 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3795 mbedtls_pk_free( &handshake->peer_pubkey );
3796#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3797
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003798#if defined(MBEDTLS_SSL_CLI_C) && \
3799 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3800 mbedtls_free( handshake->cookie );
3801#endif /* MBEDTLS_SSL_CLI_C &&
3802 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003803
3804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003805 mbedtls_ssl_flight_free( handshake->flight );
3806 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003807#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003808
Ronald Cronf12b81d2022-03-15 10:42:41 +01003809#if defined(MBEDTLS_ECDH_C) && \
3810 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003811 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003812 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003813#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3814
Ronald Cron6f135e12021-12-08 16:57:54 +01003815#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003816 mbedtls_ssl_transform_free( handshake->transform_handshake );
3817 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003818 mbedtls_free( handshake->transform_earlydata );
3819 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003820#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003821
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003822
3823#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3824 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3825 * processes datagrams and the fact that a datagram is allowed to have
3826 * several records in it, it is possible that the I/O buffers are not
3827 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003828 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3829 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003830#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003831
Jerry Yuba9c7272021-10-30 11:54:10 +08003832 /* mbedtls_platform_zeroize MUST be last one in this function */
3833 mbedtls_platform_zeroize( handshake,
3834 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003835}
3836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003838{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003839 if( session == NULL )
3840 return;
3841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003843 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003844#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003845
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003846#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003847#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3848 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003849 mbedtls_free( session->hostname );
3850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003852#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003853
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003854 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003855}
3856
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003857#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003858
3859#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3860#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3861#else
3862#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3863#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3864
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003865#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003866
3867#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3868#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3869#else
3870#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3871#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3872
3873#if defined(MBEDTLS_SSL_ALPN)
3874#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3875#else
3876#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3877#endif /* MBEDTLS_SSL_ALPN */
3878
3879#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3880#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3881#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3882#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3883
3884#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3885 ( (uint32_t) ( \
3886 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3887 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3888 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3889 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3890 0u ) )
3891
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003892static unsigned char ssl_serialized_context_header[] = {
3893 MBEDTLS_VERSION_MAJOR,
3894 MBEDTLS_VERSION_MINOR,
3895 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003896 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3897 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3898 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3899 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3900 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003901};
3902
Paul Bakker5121ce52009-01-03 21:22:43 +00003903/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003904 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003905 *
3906 * The format of the serialized data is:
3907 * (in the presentation language of TLS, RFC 8446 section 3)
3908 *
3909 * // header
3910 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003911 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003912 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003913 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003914 * Note: When updating the format, remember to keep these
3915 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003916 *
3917 * // session sub-structure
3918 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3919 * // transform sub-structure
3920 * uint8 random[64]; // ServerHello.random+ClientHello.random
3921 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3922 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3923 * // fields from ssl_context
3924 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3925 * uint64 in_window_top; // DTLS: last validated record seq_num
3926 * uint64 in_window; // DTLS: bitmask for replay protection
3927 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3928 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3929 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3930 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3931 *
3932 * Note that many fields of the ssl_context or sub-structures are not
3933 * serialized, as they fall in one of the following categories:
3934 *
3935 * 1. forced value (eg in_left must be 0)
3936 * 2. pointer to dynamically-allocated memory (eg session, transform)
3937 * 3. value can be re-derived from other data (eg session keys from MS)
3938 * 4. value was temporary (eg content of input buffer)
3939 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003940 */
3941int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3942 unsigned char *buf,
3943 size_t buf_len,
3944 size_t *olen )
3945{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003946 unsigned char *p = buf;
3947 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003948 size_t session_len;
3949 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003950
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003951 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003952 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3953 * this function's documentation.
3954 *
3955 * These are due to assumptions/limitations in the implementation. Some of
3956 * them are likely to stay (no handshake in progress) some might go away
3957 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003958 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003959 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003960 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003961 {
3962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003964 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003965 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003966 {
3967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003969 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003970 /* Double-check that sub-structures are indeed ready */
3971 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003972 {
3973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003975 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003976 /* There must be no pending incoming or outgoing data */
3977 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003978 {
3979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003981 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003982 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003983 {
3984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003986 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003987 /* Protocol must be DLTS, not TLS */
3988 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003989 {
3990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003992 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003993 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003994 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003995 {
3996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003998 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003999 /* We must be using an AEAD ciphersuite */
4000 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004001 {
4002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004004 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004005 /* Renegotiation must not be enabled */
4006#if defined(MBEDTLS_SSL_RENEGOTIATION)
4007 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004008 {
4009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004011 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004012#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004013
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004014 /*
4015 * Version and format identifier
4016 */
4017 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004018
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004019 if( used <= buf_len )
4020 {
4021 memcpy( p, ssl_serialized_context_header,
4022 sizeof( ssl_serialized_context_header ) );
4023 p += sizeof( ssl_serialized_context_header );
4024 }
4025
4026 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004027 * Session (length + data)
4028 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004029 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004030 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4031 return( ret );
4032
4033 used += 4 + session_len;
4034 if( used <= buf_len )
4035 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004036 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4037 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004038
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004039 ret = ssl_session_save( ssl->session, 1,
4040 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004041 if( ret != 0 )
4042 return( ret );
4043
4044 p += session_len;
4045 }
4046
4047 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004048 * Transform
4049 */
4050 used += sizeof( ssl->transform->randbytes );
4051 if( used <= buf_len )
4052 {
4053 memcpy( p, ssl->transform->randbytes,
4054 sizeof( ssl->transform->randbytes ) );
4055 p += sizeof( ssl->transform->randbytes );
4056 }
4057
4058#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4059 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4060 if( used <= buf_len )
4061 {
4062 *p++ = ssl->transform->in_cid_len;
4063 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4064 p += ssl->transform->in_cid_len;
4065
4066 *p++ = ssl->transform->out_cid_len;
4067 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4068 p += ssl->transform->out_cid_len;
4069 }
4070#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4071
4072 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004073 * Saved fields from top-level ssl_context structure
4074 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004075 used += 4;
4076 if( used <= buf_len )
4077 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004078 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4079 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004080 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004081
4082#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4083 used += 16;
4084 if( used <= buf_len )
4085 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004086 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4087 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004088
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004089 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4090 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004091 }
4092#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4093
4094#if defined(MBEDTLS_SSL_PROTO_DTLS)
4095 used += 1;
4096 if( used <= buf_len )
4097 {
4098 *p++ = ssl->disable_datagram_packing;
4099 }
4100#endif /* MBEDTLS_SSL_PROTO_DTLS */
4101
Jerry Yuae0b2e22021-10-08 15:21:19 +08004102 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004103 if( used <= buf_len )
4104 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004105 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4106 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004107 }
4108
4109#if defined(MBEDTLS_SSL_PROTO_DTLS)
4110 used += 2;
4111 if( used <= buf_len )
4112 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004113 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4114 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004115 }
4116#endif /* MBEDTLS_SSL_PROTO_DTLS */
4117
4118#if defined(MBEDTLS_SSL_ALPN)
4119 {
4120 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004121 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004122 : 0;
4123
4124 used += 1 + alpn_len;
4125 if( used <= buf_len )
4126 {
4127 *p++ = alpn_len;
4128
4129 if( ssl->alpn_chosen != NULL )
4130 {
4131 memcpy( p, ssl->alpn_chosen, alpn_len );
4132 p += alpn_len;
4133 }
4134 }
4135 }
4136#endif /* MBEDTLS_SSL_ALPN */
4137
4138 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004139 * Done
4140 */
4141 *olen = used;
4142
4143 if( used > buf_len )
4144 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004145
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004146 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4147
Hanno Becker43aefe22020-02-05 10:44:56 +00004148 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004149}
4150
4151/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004152 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004153 *
4154 * This internal version is wrapped by a public function that cleans up in
4155 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004156 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004157MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004158static int ssl_context_load( mbedtls_ssl_context *ssl,
4159 const unsigned char *buf,
4160 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004161{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004162 const unsigned char *p = buf;
4163 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004164 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004166#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004167 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004168#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004169
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004170 /*
4171 * The context should have been freshly setup or reset.
4172 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004173 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004174 * renegotiating, or if the user mistakenly loaded a session first.)
4175 */
4176 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4177 ssl->session != NULL )
4178 {
4179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4180 }
4181
4182 /*
4183 * We can't check that the config matches the initial one, but we can at
4184 * least check it matches the requirements for serializing.
4185 */
4186 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004187 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4188 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004189#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004190 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004191#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004192 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004193 {
4194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4195 }
4196
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004197 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4198
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004199 /*
4200 * Check version identifier
4201 */
4202 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4204
4205 if( memcmp( p, ssl_serialized_context_header,
4206 sizeof( ssl_serialized_context_header ) ) != 0 )
4207 {
4208 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4209 }
4210 p += sizeof( ssl_serialized_context_header );
4211
4212 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004213 * Session
4214 */
4215 if( (size_t)( end - p ) < 4 )
4216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4217
4218 session_len = ( (size_t) p[0] << 24 ) |
4219 ( (size_t) p[1] << 16 ) |
4220 ( (size_t) p[2] << 8 ) |
4221 ( (size_t) p[3] );
4222 p += 4;
4223
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004224 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004225 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004226 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004227 ssl->session_in = ssl->session;
4228 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004229 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004230
4231 if( (size_t)( end - p ) < session_len )
4232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4233
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004234 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004235 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004236 {
4237 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004238 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004239 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004240
4241 p += session_len;
4242
4243 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004244 * Transform
4245 */
4246
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004247 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004248 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004249 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004250 ssl->transform_in = ssl->transform;
4251 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004252 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004253
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004255 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4256 if( prf_func == NULL )
4257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4258
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004259 /* Read random bytes and populate structure */
4260 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004262
Hanno Beckerbd257552021-03-22 06:59:27 +00004263 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004264 ssl->session->ciphersuite,
4265 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004266#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004267 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004268#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004269 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004270 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004271 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004272 ssl->conf->endpoint,
4273 ssl );
4274 if( ret != 0 )
4275 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004276#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004277 p += sizeof( ssl->transform->randbytes );
4278
4279#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4280 /* Read connection IDs and store them */
4281 if( (size_t)( end - p ) < 1 )
4282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4283
4284 ssl->transform->in_cid_len = *p++;
4285
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004286 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4288
4289 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4290 p += ssl->transform->in_cid_len;
4291
4292 ssl->transform->out_cid_len = *p++;
4293
4294 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4296
4297 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4298 p += ssl->transform->out_cid_len;
4299#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4300
4301 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004302 * Saved fields from top-level ssl_context structure
4303 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004304 if( (size_t)( end - p ) < 4 )
4305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4306
4307 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4308 ( (uint32_t) p[1] << 16 ) |
4309 ( (uint32_t) p[2] << 8 ) |
4310 ( (uint32_t) p[3] );
4311 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004312
4313#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4314 if( (size_t)( end - p ) < 16 )
4315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4316
4317 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4318 ( (uint64_t) p[1] << 48 ) |
4319 ( (uint64_t) p[2] << 40 ) |
4320 ( (uint64_t) p[3] << 32 ) |
4321 ( (uint64_t) p[4] << 24 ) |
4322 ( (uint64_t) p[5] << 16 ) |
4323 ( (uint64_t) p[6] << 8 ) |
4324 ( (uint64_t) p[7] );
4325 p += 8;
4326
4327 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4328 ( (uint64_t) p[1] << 48 ) |
4329 ( (uint64_t) p[2] << 40 ) |
4330 ( (uint64_t) p[3] << 32 ) |
4331 ( (uint64_t) p[4] << 24 ) |
4332 ( (uint64_t) p[5] << 16 ) |
4333 ( (uint64_t) p[6] << 8 ) |
4334 ( (uint64_t) p[7] );
4335 p += 8;
4336#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4337
4338#if defined(MBEDTLS_SSL_PROTO_DTLS)
4339 if( (size_t)( end - p ) < 1 )
4340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4341
4342 ssl->disable_datagram_packing = *p++;
4343#endif /* MBEDTLS_SSL_PROTO_DTLS */
4344
Jerry Yud9a94fe2021-09-28 18:58:59 +08004345 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004347 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4348 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004349
4350#if defined(MBEDTLS_SSL_PROTO_DTLS)
4351 if( (size_t)( end - p ) < 2 )
4352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4353
4354 ssl->mtu = ( p[0] << 8 ) | p[1];
4355 p += 2;
4356#endif /* MBEDTLS_SSL_PROTO_DTLS */
4357
4358#if defined(MBEDTLS_SSL_ALPN)
4359 {
4360 uint8_t alpn_len;
4361 const char **cur;
4362
4363 if( (size_t)( end - p ) < 1 )
4364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4365
4366 alpn_len = *p++;
4367
4368 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4369 {
4370 /* alpn_chosen should point to an item in the configured list */
4371 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4372 {
4373 if( strlen( *cur ) == alpn_len &&
4374 memcmp( p, cur, alpn_len ) == 0 )
4375 {
4376 ssl->alpn_chosen = *cur;
4377 break;
4378 }
4379 }
4380 }
4381
4382 /* can only happen on conf mismatch */
4383 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4385
4386 p += alpn_len;
4387 }
4388#endif /* MBEDTLS_SSL_ALPN */
4389
4390 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004391 * Forced fields from top-level ssl_context structure
4392 *
4393 * Most of them already set to the correct value by mbedtls_ssl_init() and
4394 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4395 */
4396 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004397 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004398
Hanno Becker361b10d2019-08-30 10:42:49 +01004399 /* Adjust pointers for header fields of outgoing records to
4400 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004401 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004402
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004403#if defined(MBEDTLS_SSL_PROTO_DTLS)
4404 ssl->in_epoch = 1;
4405#endif
4406
4407 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4408 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004409 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4410 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004411 if( ssl->handshake != NULL )
4412 {
4413 mbedtls_ssl_handshake_free( ssl );
4414 mbedtls_free( ssl->handshake );
4415 ssl->handshake = NULL;
4416 }
4417
4418 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004419 * Done - should have consumed entire buffer
4420 */
4421 if( p != end )
4422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004423
4424 return( 0 );
4425}
4426
4427/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004428 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004429 */
4430int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4431 const unsigned char *buf,
4432 size_t len )
4433{
4434 int ret = ssl_context_load( context, buf, len );
4435
4436 if( ret != 0 )
4437 mbedtls_ssl_free( context );
4438
4439 return( ret );
4440}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004441#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004442
4443/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 * Free an SSL context
4445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004447{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004448 if( ssl == NULL )
4449 return;
4450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004452
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004453 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 {
sander-visserb8aa2072020-05-06 22:05:13 +02004455#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4456 size_t out_buf_len = ssl->out_buf_len;
4457#else
4458 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4459#endif
4460
Darryl Greenb33cc762019-11-28 14:29:44 +00004461 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004463 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004464 }
4465
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004466 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 {
sander-visserb8aa2072020-05-06 22:05:13 +02004468#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4469 size_t in_buf_len = ssl->in_buf_len;
4470#else
4471 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4472#endif
4473
Darryl Greenb33cc762019-11-28 14:29:44 +00004474 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004476 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004477 }
4478
Paul Bakker48916f92012-09-16 19:57:18 +00004479 if( ssl->transform )
4480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481 mbedtls_ssl_transform_free( ssl->transform );
4482 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004483 }
4484
4485 if( ssl->handshake )
4486 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004487 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4489 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 mbedtls_free( ssl->handshake );
4492 mbedtls_free( ssl->transform_negotiate );
4493 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004494 }
4495
Ronald Cron6f135e12021-12-08 16:57:54 +01004496#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004497 mbedtls_ssl_transform_free( ssl->transform_application );
4498 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004499#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004500
Paul Bakkerc0463502013-02-14 11:19:38 +01004501 if( ssl->session )
4502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 mbedtls_ssl_session_free( ssl->session );
4504 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004505 }
4506
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004507#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004508 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004509 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004510 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004512 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004514
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004515#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004517#endif
4518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004520
Paul Bakker86f04f42013-02-14 11:20:09 +01004521 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004522 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004523}
4524
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004525/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004526 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004527 */
4528void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4529{
4530 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4531}
4532
Gilles Peskineae270bf2021-06-02 00:05:29 +02004533/* The selection should be the same as mbedtls_x509_crt_profile_default in
4534 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004535 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004536 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004537 * about this list.
4538 */
Brett Warrene0edc842021-08-17 09:53:13 +01004539static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004540#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004541 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004542#endif
4543#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004544 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004545#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004546#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004547 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004548#endif
4549#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004550 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004551#endif
4552#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004553 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004554#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004555#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004556 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004557#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004558#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004559 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004560#endif
4561#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004562 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004563#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004564 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004565};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004566
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004567static int ssl_preset_suiteb_ciphersuites[] = {
4568 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4569 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4570 0
4571};
4572
Ronald Crone68ab4f2022-10-05 12:46:29 +02004573#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004574
Jerry Yu909df7b2022-01-22 11:56:27 +08004575/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004576 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004577 * rules SHOULD be upheld.
4578 * - No duplicate entries.
4579 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004580 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004581 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004582 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004583static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004584
Andrzej Kurek25f27152022-08-17 16:09:31 -04004585#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 +08004586 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4587 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004588#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004589 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004590
Andrzej Kurek25f27152022-08-17 16:09:31 -04004591#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 +08004592 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4593 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004594#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004595 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4596
Andrzej Kurek25f27152022-08-17 16:09:31 -04004597#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 +08004598 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4599 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004600#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004601 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004602
Andrzej Kurek25f27152022-08-17 16:09:31 -04004603#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 +08004604 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004605#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 +08004606
Andrzej Kurek25f27152022-08-17 16:09:31 -04004607#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 +08004608 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004609#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 +08004610
Andrzej Kurek25f27152022-08-17 16:09:31 -04004611#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 +08004612 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004613#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 +08004614
Andrzej Kurek25f27152022-08-17 16:09:31 -04004615#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 +08004616 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4617#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4618
Andrzej Kurek25f27152022-08-17 16:09:31 -04004619#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 +08004620 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4621#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4622
Andrzej Kurek25f27152022-08-17 16:09:31 -04004623#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 +08004624 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4625#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4626
Gabor Mezei15b95a62022-05-09 16:37:58 +02004627 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004628};
4629
4630/* NOTICE: see above */
4631#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4632static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004633#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004634#if defined(MBEDTLS_ECDSA_C)
4635 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004636#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004637#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4638 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4639#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004640#if defined(MBEDTLS_RSA_C)
4641 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4642#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004643#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004644#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004645#if defined(MBEDTLS_ECDSA_C)
4646 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004647#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004648#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4649 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4650#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004651#if defined(MBEDTLS_RSA_C)
4652 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4653#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004654#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004655#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004656#if defined(MBEDTLS_ECDSA_C)
4657 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004658#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004659#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4660 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4661#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004662#if defined(MBEDTLS_RSA_C)
4663 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4664#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004665#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004666 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004667};
4668#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4669/* NOTICE: see above */
4670static uint16_t ssl_preset_suiteb_sig_algs[] = {
4671
Andrzej Kurek25f27152022-08-17 16:09:31 -04004672#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 +08004673 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4674 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004675#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004676 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4677
Andrzej Kurek25f27152022-08-17 16:09:31 -04004678#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 +08004679 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4680 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004681#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004682 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004683
Andrzej Kurek25f27152022-08-17 16:09:31 -04004684#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 +08004685 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004686#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 +08004687
Andrzej Kurek25f27152022-08-17 16:09:31 -04004688#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 +08004689 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004690#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004691
Gabor Mezei15b95a62022-05-09 16:37:58 +02004692 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004693};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004694
Jerry Yu909df7b2022-01-22 11:56:27 +08004695/* NOTICE: see above */
4696#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4697static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004698#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004699#if defined(MBEDTLS_ECDSA_C)
4700 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004701#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004702#if defined(MBEDTLS_RSA_C)
4703 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4704#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004705#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004706#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004707#if defined(MBEDTLS_ECDSA_C)
4708 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004709#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004710#if defined(MBEDTLS_RSA_C)
4711 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4712#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004713#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004714 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004715};
Jerry Yu909df7b2022-01-22 11:56:27 +08004716#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4717
Ronald Crone68ab4f2022-10-05 12:46:29 +02004718#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004719
Brett Warrene0edc842021-08-17 09:53:13 +01004720static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004721#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004722 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004723#endif
4724#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004725 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004726#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004727 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004728};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004729
Ronald Crone68ab4f2022-10-05 12:46:29 +02004730#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004731/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004732 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004733MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004734static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004735{
4736 size_t i, j;
4737 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004738
Gabor Mezei15b95a62022-05-09 16:37:58 +02004739 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004740 {
Jerry Yuf377d642022-01-25 10:43:59 +08004741 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004742 {
Jerry Yuf377d642022-01-25 10:43:59 +08004743 if( sig_algs[i] != sig_algs[j] )
4744 continue;
4745 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4746 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4747 sig_algs[i], j, i );
4748 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004749 }
4750 }
4751 return( ret );
4752}
4753
Ronald Crone68ab4f2022-10-05 12:46:29 +02004754#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004755
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004756/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004757 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004758 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004759int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004760 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004761{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004762#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004764#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004765
Ronald Crone68ab4f2022-10-05 12:46:29 +02004766#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004767 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004768 {
4769 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4770 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4771 }
4772
Jerry Yuf377d642022-01-25 10:43:59 +08004773 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004774 {
4775 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4776 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4777 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004778
4779#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004780 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004781 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004782 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004783 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4784 }
4785
Jerry Yuf377d642022-01-25 10:43:59 +08004786 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004787 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004788 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004789 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4790 }
4791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004792#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004793
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004794 /* Use the functions here so that they are covered in tests,
4795 * but otherwise access member directly for efficiency */
4796 mbedtls_ssl_conf_endpoint( conf, endpoint );
4797 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004798
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004799 /*
4800 * Things that are common to all presets
4801 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004802#if defined(MBEDTLS_SSL_CLI_C)
4803 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4804 {
4805 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4806#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4807 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4808#endif
4809 }
4810#endif
4811
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004812#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4813 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4814#endif
4815
4816#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4817 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4818#endif
4819
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004820#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004821 conf->f_cookie_write = ssl_cookie_write_dummy;
4822 conf->f_cookie_check = ssl_cookie_check_dummy;
4823#endif
4824
4825#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4826 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4827#endif
4828
Janos Follath088ce432017-04-10 12:42:31 +01004829#if defined(MBEDTLS_SSL_SRV_C)
4830 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004831 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004832#endif
4833
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004834#if defined(MBEDTLS_SSL_PROTO_DTLS)
4835 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4836 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4837#endif
4838
4839#if defined(MBEDTLS_SSL_RENEGOTIATION)
4840 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004841 memset( conf->renego_period, 0x00, 2 );
4842 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004843#endif
4844
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004845#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004846 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4847 {
4848 const unsigned char dhm_p[] =
4849 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4850 const unsigned char dhm_g[] =
4851 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004852
Hanno Beckere2defad2021-07-24 05:59:17 +01004853 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4854 dhm_p, sizeof( dhm_p ),
4855 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4856 {
4857 return( ret );
4858 }
4859 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004860#endif
4861
Ronald Cron6f135e12021-12-08 16:57:54 +01004862#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004863#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004864 mbedtls_ssl_conf_new_session_tickets(
4865 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4866#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004867 /*
4868 * Allow all TLS 1.3 key exchange modes by default.
4869 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004870 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004871#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004872
XiaokangQian4d3a6042022-04-21 13:46:17 +00004873 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4874 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004875#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004876 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004877 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004878#else
XiaokangQian060d8672022-04-21 09:24:56 +00004879 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004880#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004881 }
4882 else
4883 {
4884#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4885 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4886 {
4887 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4888 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4889 }
4890 else
4891 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4892 {
4893 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4894 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4895 }
4896#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4897 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4898 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4899#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4900 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4901 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4902#else
4903 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4904#endif
4905 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004906
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004907 /*
4908 * Preset-specific defaults
4909 */
4910 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004911 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004912 /*
4913 * NSA Suite B
4914 */
4915 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004916
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004917 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004918
4919#if defined(MBEDTLS_X509_CRT_PARSE_C)
4920 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004921#endif
4922
Ronald Crone68ab4f2022-10-05 12:46:29 +02004923#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004924#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4925 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4926 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4927 else
4928#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4929 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004930#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004931
Brett Warrene0edc842021-08-17 09:53:13 +01004932#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4933 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004934#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004935 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004936 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004937
4938 /*
4939 * Default
4940 */
4941 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004942
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004943 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004944
4945#if defined(MBEDTLS_X509_CRT_PARSE_C)
4946 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4947#endif
4948
Ronald Crone68ab4f2022-10-05 12:46:29 +02004949#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004950#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4951 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4952 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4953 else
4954#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4955 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004956#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004957
Brett Warrene0edc842021-08-17 09:53:13 +01004958#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4959 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004960#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004961 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004962
4963#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4964 conf->dhm_min_bitlen = 1024;
4965#endif
4966 }
4967
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004968 return( 0 );
4969}
4970
4971/*
4972 * Free mbedtls_ssl_config
4973 */
4974void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4975{
4976#if defined(MBEDTLS_DHM_C)
4977 mbedtls_mpi_free( &conf->dhm_P );
4978 mbedtls_mpi_free( &conf->dhm_G );
4979#endif
4980
Ronald Cron73fe8df2022-10-05 14:31:43 +02004981#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004982#if defined(MBEDTLS_USE_PSA_CRYPTO)
4983 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4984 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004985 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4986 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004987#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004988 if( conf->psk != NULL )
4989 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004990 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004991 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004992 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004993 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004994 }
4995
4996 if( conf->psk_identity != NULL )
4997 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004998 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004999 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005000 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005001 conf->psk_identity_len = 0;
5002 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005003#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005004
5005#if defined(MBEDTLS_X509_CRT_PARSE_C)
5006 ssl_key_cert_free( conf->key_cert );
5007#endif
5008
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005009 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005010}
5011
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005012#if defined(MBEDTLS_PK_C) && \
5013 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005014/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019#if defined(MBEDTLS_RSA_C)
5020 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5021 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023#if defined(MBEDTLS_ECDSA_C)
5024 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5025 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005028}
5029
Hanno Becker7e5437a2017-04-28 17:15:26 +01005030unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5031{
5032 switch( type ) {
5033 case MBEDTLS_PK_RSA:
5034 return( MBEDTLS_SSL_SIG_RSA );
5035 case MBEDTLS_PK_ECDSA:
5036 case MBEDTLS_PK_ECKEY:
5037 return( MBEDTLS_SSL_SIG_ECDSA );
5038 default:
5039 return( MBEDTLS_SSL_SIG_ANON );
5040 }
5041}
5042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005044{
5045 switch( sig )
5046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047#if defined(MBEDTLS_RSA_C)
5048 case MBEDTLS_SSL_SIG_RSA:
5049 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051#if defined(MBEDTLS_ECDSA_C)
5052 case MBEDTLS_SSL_SIG_ECDSA:
5053 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005054#endif
5055 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005057 }
5058}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005059#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005060
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005061/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005062 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005065{
5066 switch( hash )
5067 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005068#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069 case MBEDTLS_SSL_HASH_MD5:
5070 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005071#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005072#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 case MBEDTLS_SSL_HASH_SHA1:
5074 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005075#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005076#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 case MBEDTLS_SSL_HASH_SHA224:
5078 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005079#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005080#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 case MBEDTLS_SSL_HASH_SHA256:
5082 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005083#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005084#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 case MBEDTLS_SSL_HASH_SHA384:
5086 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005087#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005088#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 case MBEDTLS_SSL_HASH_SHA512:
5090 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005091#endif
5092 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005094 }
5095}
5096
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005097/*
5098 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5099 */
5100unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5101{
5102 switch( md )
5103 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005104#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005105 case MBEDTLS_MD_MD5:
5106 return( MBEDTLS_SSL_HASH_MD5 );
5107#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005108#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005109 case MBEDTLS_MD_SHA1:
5110 return( MBEDTLS_SSL_HASH_SHA1 );
5111#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005112#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005113 case MBEDTLS_MD_SHA224:
5114 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005115#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005116#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005117 case MBEDTLS_MD_SHA256:
5118 return( MBEDTLS_SSL_HASH_SHA256 );
5119#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005120#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005121 case MBEDTLS_MD_SHA384:
5122 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005123#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005124#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005125 case MBEDTLS_MD_SHA512:
5126 return( MBEDTLS_SSL_HASH_SHA512 );
5127#endif
5128 default:
5129 return( MBEDTLS_SSL_HASH_NONE );
5130 }
5131}
5132
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005133/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005134 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005135 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005136 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005137int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005138{
Brett Warrene0edc842021-08-17 09:53:13 +01005139 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005140
Brett Warrene0edc842021-08-17 09:53:13 +01005141 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005142 return( -1 );
5143
Brett Warrene0edc842021-08-17 09:53:13 +01005144 for( ; *group_list != 0; group_list++ )
5145 {
5146 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005147 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005148 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005149
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005150 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005151}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005152
5153#if defined(MBEDTLS_ECP_C)
5154/*
5155 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5156 */
5157int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5158{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005159 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005160 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005161
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005162 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005163 return -1;
5164
5165 uint16_t tls_id = grp_info->tls_id;
5166
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005167 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5168}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005169#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171#if defined(MBEDTLS_X509_CRT_PARSE_C)
5172int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5173 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005174 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005175 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005176{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005177 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005178 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005179 const char *ext_oid;
5180 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005183 {
5184 /* Server part of the key exchange */
5185 switch( ciphersuite->key_exchange )
5186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187 case MBEDTLS_KEY_EXCHANGE_RSA:
5188 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005189 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005190 break;
5191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5193 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5194 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5195 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005196 break;
5197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5199 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005200 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005201 break;
5202
5203 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204 case MBEDTLS_KEY_EXCHANGE_NONE:
5205 case MBEDTLS_KEY_EXCHANGE_PSK:
5206 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5207 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005208 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005209 usage = 0;
5210 }
5211 }
5212 else
5213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5215 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005216 }
5217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005219 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005220 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005221 ret = -1;
5222 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5227 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005228 }
5229 else
5230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5232 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005233 }
5234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005236 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005237 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005238 ret = -1;
5239 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005240
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005241 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005242}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005244
Jerry Yu148165c2021-09-24 23:20:59 +08005245#if defined(MBEDTLS_USE_PSA_CRYPTO)
5246int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5247 const mbedtls_md_type_t md,
5248 unsigned char *dst,
5249 size_t dst_len,
5250 size_t *olen )
5251{
Ronald Cronf6893e12022-01-07 22:09:01 +01005252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5253 psa_hash_operation_t *hash_operation_to_clone;
5254 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5255
Jerry Yu148165c2021-09-24 23:20:59 +08005256 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005257
5258 switch( md )
5259 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005260#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005261 case MBEDTLS_MD_SHA384:
5262 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5263 break;
5264#endif
5265
Andrzej Kurek25f27152022-08-17 16:09:31 -04005266#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005267 case MBEDTLS_MD_SHA256:
5268 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5269 break;
5270#endif
5271
5272 default:
5273 goto exit;
5274 }
5275
5276 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5277 if( status != PSA_SUCCESS )
5278 goto exit;
5279
5280 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5281 if( status != PSA_SUCCESS )
5282 goto exit;
5283
5284exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005285#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5286 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5287 (void) ssl;
5288#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005289 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005290}
5291#else /* MBEDTLS_USE_PSA_CRYPTO */
5292
Andrzej Kurek25f27152022-08-17 16:09:31 -04005293#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005294MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005295static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5296 unsigned char *dst,
5297 size_t dst_len,
5298 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005299{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005300 int ret;
5301 mbedtls_sha512_context sha512;
5302
5303 if( dst_len < 48 )
5304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5305
5306 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005307 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005308
5309 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5310 {
5311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5312 goto exit;
5313 }
5314
5315 *olen = 48;
5316
5317exit:
5318
5319 mbedtls_sha512_free( &sha512 );
5320 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005321}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005322#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005323
Andrzej Kurek25f27152022-08-17 16:09:31 -04005324#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005325MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005326static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5327 unsigned char *dst,
5328 size_t dst_len,
5329 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005330{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005331 int ret;
5332 mbedtls_sha256_context sha256;
5333
5334 if( dst_len < 32 )
5335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5336
5337 mbedtls_sha256_init( &sha256 );
5338 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005339
Jerry Yu24c0ec32021-09-09 14:21:07 +08005340 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5341 {
5342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5343 goto exit;
5344 }
5345
5346 *olen = 32;
5347
5348exit:
5349
5350 mbedtls_sha256_free( &sha256 );
5351 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005352}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005353#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005354
Jerry Yu000f9762021-09-14 11:12:51 +08005355int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5356 const mbedtls_md_type_t md,
5357 unsigned char *dst,
5358 size_t dst_len,
5359 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005360{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005361 switch( md )
5362 {
5363
Andrzej Kurek25f27152022-08-17 16:09:31 -04005364#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005365 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005366 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005367#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005368
Andrzej Kurek25f27152022-08-17 16:09:31 -04005369#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005370 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005371 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005372#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005373
5374 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005375#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5376 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5377 (void) ssl;
5378 (void) dst;
5379 (void) dst_len;
5380 (void) olen;
5381#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005382 break;
5383 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005384 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005385}
XiaokangQian647719a2021-12-07 09:16:29 +00005386
Jerry Yu148165c2021-09-24 23:20:59 +08005387#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005388
Ronald Crone68ab4f2022-10-05 12:46:29 +02005389#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005390/* mbedtls_ssl_parse_sig_alg_ext()
5391 *
5392 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5393 * value (TLS 1.3 RFC8446):
5394 * enum {
5395 * ....
5396 * ecdsa_secp256r1_sha256( 0x0403 ),
5397 * ecdsa_secp384r1_sha384( 0x0503 ),
5398 * ecdsa_secp521r1_sha512( 0x0603 ),
5399 * ....
5400 * } SignatureScheme;
5401 *
5402 * struct {
5403 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5404 * } SignatureSchemeList;
5405 *
5406 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5407 * value (TLS 1.2 RFC5246):
5408 * enum {
5409 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5410 * sha512(6), (255)
5411 * } HashAlgorithm;
5412 *
5413 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5414 * SignatureAlgorithm;
5415 *
5416 * struct {
5417 * HashAlgorithm hash;
5418 * SignatureAlgorithm signature;
5419 * } SignatureAndHashAlgorithm;
5420 *
5421 * SignatureAndHashAlgorithm
5422 * supported_signature_algorithms<2..2^16-2>;
5423 *
5424 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5425 * generalization of the TLS 1.2 signature algorithm extension.
5426 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5427 * `SignatureScheme` field of TLS 1.3
5428 *
5429 */
5430int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5431 const unsigned char *buf,
5432 const unsigned char *end )
5433{
5434 const unsigned char *p = buf;
5435 size_t supported_sig_algs_len = 0;
5436 const unsigned char *supported_sig_algs_end;
5437 uint16_t sig_alg;
5438 uint32_t common_idx = 0;
5439
5440 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5441 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5442 p += 2;
5443
5444 memset( ssl->handshake->received_sig_algs, 0,
5445 sizeof(ssl->handshake->received_sig_algs) );
5446
5447 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5448 supported_sig_algs_end = p + supported_sig_algs_len;
5449 while( p < supported_sig_algs_end )
5450 {
5451 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5452 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5453 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005454 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5455 sig_alg,
5456 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005457#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005458 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005459 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5460 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5461 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005462 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005463 }
5464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005465
Jerry Yuf3b46b52022-06-19 16:52:27 +08005466 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5467 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005468
5469 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5470 {
5471 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5472 common_idx += 1;
5473 }
5474 }
5475 /* Check that we consumed all the message. */
5476 if( p != end )
5477 {
5478 MBEDTLS_SSL_DEBUG_MSG( 1,
5479 ( "Signature algorithms extension length misaligned" ) );
5480 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5481 MBEDTLS_ERR_SSL_DECODE_ERROR );
5482 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5483 }
5484
5485 if( common_idx == 0 )
5486 {
5487 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5488 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5489 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5490 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5491 }
5492
Gabor Mezei15b95a62022-05-09 16:37:58 +02005493 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005494 return( 0 );
5495}
5496
Ronald Crone68ab4f2022-10-05 12:46:29 +02005497#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005498
Jerry Yudc7bd172022-02-17 13:44:15 +08005499#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5500
5501#if defined(MBEDTLS_USE_PSA_CRYPTO)
5502
5503static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5504 mbedtls_svc_key_id_t key,
5505 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005506 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005507 const unsigned char* seed, size_t seed_length,
5508 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005509 const unsigned char* other_secret,
5510 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005511 size_t capacity )
5512{
5513 psa_status_t status;
5514
5515 status = psa_key_derivation_setup( derivation, alg );
5516 if( status != PSA_SUCCESS )
5517 return( status );
5518
5519 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5520 {
5521 status = psa_key_derivation_input_bytes( derivation,
5522 PSA_KEY_DERIVATION_INPUT_SEED,
5523 seed, seed_length );
5524 if( status != PSA_SUCCESS )
5525 return( status );
5526
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005527 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005528 {
5529 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005530 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5531 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005532 if( status != PSA_SUCCESS )
5533 return( status );
5534 }
5535
Jerry Yudc7bd172022-02-17 13:44:15 +08005536 if( mbedtls_svc_key_id_is_null( key ) )
5537 {
5538 status = psa_key_derivation_input_bytes(
5539 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005540 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005541 }
5542 else
5543 {
5544 status = psa_key_derivation_input_key(
5545 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5546 }
5547 if( status != PSA_SUCCESS )
5548 return( status );
5549
5550 status = psa_key_derivation_input_bytes( derivation,
5551 PSA_KEY_DERIVATION_INPUT_LABEL,
5552 label, label_length );
5553 if( status != PSA_SUCCESS )
5554 return( status );
5555 }
5556 else
5557 {
5558 return( PSA_ERROR_NOT_SUPPORTED );
5559 }
5560
5561 status = psa_key_derivation_set_capacity( derivation, capacity );
5562 if( status != PSA_SUCCESS )
5563 return( status );
5564
5565 return( PSA_SUCCESS );
5566}
5567
Andrzej Kurek57d10632022-10-24 10:32:01 -04005568#if defined(PSA_WANT_ALG_SHA_384) || \
5569 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005570MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005571static int tls_prf_generic( mbedtls_md_type_t md_type,
5572 const unsigned char *secret, size_t slen,
5573 const char *label,
5574 const unsigned char *random, size_t rlen,
5575 unsigned char *dstbuf, size_t dlen )
5576{
5577 psa_status_t status;
5578 psa_algorithm_t alg;
5579 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5580 psa_key_derivation_operation_t derivation =
5581 PSA_KEY_DERIVATION_OPERATION_INIT;
5582
5583 if( md_type == MBEDTLS_MD_SHA384 )
5584 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5585 else
5586 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5587
5588 /* Normally a "secret" should be long enough to be impossible to
5589 * find by brute force, and in particular should not be empty. But
5590 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5591 * and for this use case it makes sense to have a 0-length "secret".
5592 * Since the key API doesn't allow importing a key of length 0,
5593 * keep master_key=0, which setup_psa_key_derivation() understands
5594 * to mean a 0-length "secret" input. */
5595 if( slen != 0 )
5596 {
5597 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5598 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5599 psa_set_key_algorithm( &key_attributes, alg );
5600 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5601
5602 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5603 if( status != PSA_SUCCESS )
5604 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5605 }
5606
5607 status = setup_psa_key_derivation( &derivation,
5608 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005609 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005610 random, rlen,
5611 (unsigned char const *) label,
5612 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005613 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005614 dlen );
5615 if( status != PSA_SUCCESS )
5616 {
5617 psa_key_derivation_abort( &derivation );
5618 psa_destroy_key( master_key );
5619 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5620 }
5621
5622 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5623 if( status != PSA_SUCCESS )
5624 {
5625 psa_key_derivation_abort( &derivation );
5626 psa_destroy_key( master_key );
5627 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5628 }
5629
5630 status = psa_key_derivation_abort( &derivation );
5631 if( status != PSA_SUCCESS )
5632 {
5633 psa_destroy_key( master_key );
5634 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5635 }
5636
5637 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5638 status = psa_destroy_key( master_key );
5639 if( status != PSA_SUCCESS )
5640 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5641
5642 return( 0 );
5643}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005644#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005645#else /* MBEDTLS_USE_PSA_CRYPTO */
5646
Andrzej Kurek57d10632022-10-24 10:32:01 -04005647#if defined(MBEDTLS_MD_C) && \
5648 ( defined(MBEDTLS_SHA256_C) || \
5649 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005650MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005651static int tls_prf_generic( mbedtls_md_type_t md_type,
5652 const unsigned char *secret, size_t slen,
5653 const char *label,
5654 const unsigned char *random, size_t rlen,
5655 unsigned char *dstbuf, size_t dlen )
5656{
5657 size_t nb;
5658 size_t i, j, k, md_len;
5659 unsigned char *tmp;
5660 size_t tmp_len = 0;
5661 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5662 const mbedtls_md_info_t *md_info;
5663 mbedtls_md_context_t md_ctx;
5664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5665
5666 mbedtls_md_init( &md_ctx );
5667
5668 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5670
5671 md_len = mbedtls_md_get_size( md_info );
5672
5673 tmp_len = md_len + strlen( label ) + rlen;
5674 tmp = mbedtls_calloc( 1, tmp_len );
5675 if( tmp == NULL )
5676 {
5677 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5678 goto exit;
5679 }
5680
5681 nb = strlen( label );
5682 memcpy( tmp + md_len, label, nb );
5683 memcpy( tmp + md_len + nb, random, rlen );
5684 nb += rlen;
5685
5686 /*
5687 * Compute P_<hash>(secret, label + random)[0..dlen]
5688 */
5689 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5690 goto exit;
5691
5692 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5693 if( ret != 0 )
5694 goto exit;
5695 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5696 if( ret != 0 )
5697 goto exit;
5698 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5699 if( ret != 0 )
5700 goto exit;
5701
5702 for( i = 0; i < dlen; i += md_len )
5703 {
5704 ret = mbedtls_md_hmac_reset ( &md_ctx );
5705 if( ret != 0 )
5706 goto exit;
5707 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5708 if( ret != 0 )
5709 goto exit;
5710 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5711 if( ret != 0 )
5712 goto exit;
5713
5714 ret = mbedtls_md_hmac_reset ( &md_ctx );
5715 if( ret != 0 )
5716 goto exit;
5717 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5718 if( ret != 0 )
5719 goto exit;
5720 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5721 if( ret != 0 )
5722 goto exit;
5723
5724 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5725
5726 for( j = 0; j < k; j++ )
5727 dstbuf[i + j] = h_i[j];
5728 }
5729
5730exit:
5731 mbedtls_md_free( &md_ctx );
5732
5733 mbedtls_platform_zeroize( tmp, tmp_len );
5734 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5735
5736 mbedtls_free( tmp );
5737
5738 return( ret );
5739}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005740#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005741#endif /* MBEDTLS_USE_PSA_CRYPTO */
5742
Andrzej Kurek25f27152022-08-17 16:09:31 -04005743#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005744MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005745static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5746 const char *label,
5747 const unsigned char *random, size_t rlen,
5748 unsigned char *dstbuf, size_t dlen )
5749{
5750 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5751 label, random, rlen, dstbuf, dlen ) );
5752}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005753#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005754
Andrzej Kurek25f27152022-08-17 16:09:31 -04005755#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005756MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005757static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5758 const char *label,
5759 const unsigned char *random, size_t rlen,
5760 unsigned char *dstbuf, size_t dlen )
5761{
5762 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5763 label, random, rlen, dstbuf, dlen ) );
5764}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005765#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005766
Jerry Yuf009d862022-02-17 14:01:37 +08005767/*
5768 * Set appropriate PRF function and other SSL / TLS1.2 functions
5769 *
5770 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005771 * - hash associated with the ciphersuite (only used by TLS 1.2)
5772 *
5773 * Outputs:
5774 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5775 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005776MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005777static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005778 mbedtls_md_type_t hash )
5779{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005780#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005781 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005782 {
5783 handshake->tls_prf = tls_prf_sha384;
5784 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5785 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5786 }
5787 else
5788#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005789#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005790 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005791 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005792 handshake->tls_prf = tls_prf_sha256;
5793 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5794 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5795 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005796#else
Jerry Yuf009d862022-02-17 14:01:37 +08005797 {
Jerry Yuf009d862022-02-17 14:01:37 +08005798 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005799 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5801 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005802#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005803
5804 return( 0 );
5805}
Jerry Yud6ab2352022-02-17 14:03:43 +08005806
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005807/*
5808 * Compute master secret if needed
5809 *
5810 * Parameters:
5811 * [in/out] handshake
5812 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5813 * (PSA-PSK) ciphersuite_info, psk_opaque
5814 * [out] premaster (cleared)
5815 * [out] master
5816 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5817 * debug: conf->f_dbg, conf->p_dbg
5818 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005819 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005820 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005821MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005822static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5823 unsigned char *master,
5824 const mbedtls_ssl_context *ssl )
5825{
5826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5827
5828 /* cf. RFC 5246, Section 8.1:
5829 * "The master secret is always exactly 48 bytes in length." */
5830 size_t const master_secret_len = 48;
5831
5832#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5833 unsigned char session_hash[48];
5834#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5835
5836 /* The label for the KDF used for key expansion.
5837 * This is either "master secret" or "extended master secret"
5838 * depending on whether the Extended Master Secret extension
5839 * is used. */
5840 char const *lbl = "master secret";
5841
Przemek Stekielae4ed302022-04-05 17:15:55 +02005842 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005843 * - If the Extended Master Secret extension is not used,
5844 * this is ClientHello.Random + ServerHello.Random
5845 * (see Sect. 8.1 in RFC 5246).
5846 * - If the Extended Master Secret extension is used,
5847 * this is the transcript of the handshake so far.
5848 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005849 unsigned char const *seed = handshake->randbytes;
5850 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005851
5852#if !defined(MBEDTLS_DEBUG_C) && \
5853 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5854 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5855 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5856 ssl = NULL; /* make sure we don't use it except for those cases */
5857 (void) ssl;
5858#endif
5859
5860 if( handshake->resume != 0 )
5861 {
5862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5863 return( 0 );
5864 }
5865
5866#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5867 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5868 {
5869 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005870 seed = session_hash;
5871 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005872
5873 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005874 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005875 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005876#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005877
Przemek Stekiel99114f32022-04-22 11:20:09 +02005878#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005879 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005880 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005881 {
5882 /* Perform PSK-to-MS expansion in a single step. */
5883 psa_status_t status;
5884 psa_algorithm_t alg;
5885 mbedtls_svc_key_id_t psk;
5886 psa_key_derivation_operation_t derivation =
5887 PSA_KEY_DERIVATION_OPERATION_INIT;
5888 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5889
5890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5891
5892 psk = mbedtls_ssl_get_opaque_psk( ssl );
5893
5894 if( hash_alg == MBEDTLS_MD_SHA384 )
5895 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5896 else
5897 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5898
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005899 size_t other_secret_len = 0;
5900 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005901
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005902 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005903 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005904 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005905 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005906 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005907 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005908 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005909 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005910 other_secret_len = 48;
5911 other_secret = handshake->premaster + 2;
5912 break;
5913 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005914 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005915 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5916 other_secret = handshake->premaster + 2;
5917 break;
5918 default:
5919 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005920 }
5921
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005922 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005923 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005924 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005925 (unsigned char const *) lbl,
5926 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005927 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005928 master_secret_len );
5929 if( status != PSA_SUCCESS )
5930 {
5931 psa_key_derivation_abort( &derivation );
5932 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5933 }
5934
5935 status = psa_key_derivation_output_bytes( &derivation,
5936 master,
5937 master_secret_len );
5938 if( status != PSA_SUCCESS )
5939 {
5940 psa_key_derivation_abort( &derivation );
5941 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5942 }
5943
5944 status = psa_key_derivation_abort( &derivation );
5945 if( status != PSA_SUCCESS )
5946 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5947 }
5948 else
5949#endif
5950 {
5951 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005952 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005953 master,
5954 master_secret_len );
5955 if( ret != 0 )
5956 {
5957 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5958 return( ret );
5959 }
5960
5961 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5962 handshake->premaster,
5963 handshake->pmslen );
5964
5965 mbedtls_platform_zeroize( handshake->premaster,
5966 sizeof(handshake->premaster) );
5967 }
5968
5969 return( 0 );
5970}
5971
Jerry Yud62f87e2022-02-17 14:09:02 +08005972int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5973{
5974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5975 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5976 ssl->handshake->ciphersuite_info;
5977
5978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5979
5980 /* Set PRF, calc_verify and calc_finished function pointers */
5981 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005982 ciphersuite_info->mac );
5983 if( ret != 0 )
5984 {
5985 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5986 return( ret );
5987 }
5988
5989 /* Compute master secret if needed */
5990 ret = ssl_compute_master( ssl->handshake,
5991 ssl->session_negotiate->master,
5992 ssl );
5993 if( ret != 0 )
5994 {
5995 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5996 return( ret );
5997 }
5998
5999 /* Swap the client and server random values:
6000 * - MS derivation wanted client+server (RFC 5246 8.1)
6001 * - key derivation wants server+client (RFC 5246 6.3) */
6002 {
6003 unsigned char tmp[64];
6004 memcpy( tmp, ssl->handshake->randbytes, 64 );
6005 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6006 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6007 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6008 }
6009
6010 /* Populate transform structure */
6011 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6012 ssl->session_negotiate->ciphersuite,
6013 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006014#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006015 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006016#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006017 ssl->handshake->tls_prf,
6018 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006019 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006020 ssl->conf->endpoint,
6021 ssl );
6022 if( ret != 0 )
6023 {
6024 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6025 return( ret );
6026 }
6027
6028 /* We no longer need Server/ClientHello.random values */
6029 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6030 sizeof( ssl->handshake->randbytes ) );
6031
6032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6033
6034 return( 0 );
6035}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006036
Ronald Cron4dcbca92022-03-07 10:21:40 +01006037int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6038{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006039 switch( md )
6040 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006041#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006042 case MBEDTLS_SSL_HASH_SHA384:
6043 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6044 break;
6045#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006046#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006047 case MBEDTLS_SSL_HASH_SHA256:
6048 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6049 break;
6050#endif
6051 default:
6052 return( -1 );
6053 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006054#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6055 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6056 (void) ssl;
6057#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006058 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006059}
6060
Andrzej Kurek25f27152022-08-17 16:09:31 -04006061#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006062void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6063 unsigned char *hash,
6064 size_t *hlen )
6065{
6066#if defined(MBEDTLS_USE_PSA_CRYPTO)
6067 size_t hash_size;
6068 psa_status_t status;
6069 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6070
6071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6072 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6073 if( status != PSA_SUCCESS )
6074 {
6075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6076 return;
6077 }
6078
6079 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6080 if( status != PSA_SUCCESS )
6081 {
6082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6083 return;
6084 }
6085
6086 *hlen = 32;
6087 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6089#else
6090 mbedtls_sha256_context sha256;
6091
6092 mbedtls_sha256_init( &sha256 );
6093
6094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6095
6096 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6097 mbedtls_sha256_finish( &sha256, hash );
6098
6099 *hlen = 32;
6100
6101 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6103
6104 mbedtls_sha256_free( &sha256 );
6105#endif /* MBEDTLS_USE_PSA_CRYPTO */
6106 return;
6107}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006108#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006109
Andrzej Kurek25f27152022-08-17 16:09:31 -04006110#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006111void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6112 unsigned char *hash,
6113 size_t *hlen )
6114{
6115#if defined(MBEDTLS_USE_PSA_CRYPTO)
6116 size_t hash_size;
6117 psa_status_t status;
6118 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6119
6120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6121 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6122 if( status != PSA_SUCCESS )
6123 {
6124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6125 return;
6126 }
6127
6128 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6129 if( status != PSA_SUCCESS )
6130 {
6131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6132 return;
6133 }
6134
6135 *hlen = 48;
6136 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6138#else
6139 mbedtls_sha512_context sha512;
6140
6141 mbedtls_sha512_init( &sha512 );
6142
6143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6144
Andrzej Kureka242e832022-08-11 10:03:14 -04006145 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006146 mbedtls_sha512_finish( &sha512, hash );
6147
6148 *hlen = 48;
6149
6150 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6152
6153 mbedtls_sha512_free( &sha512 );
6154#endif /* MBEDTLS_USE_PSA_CRYPTO */
6155 return;
6156}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006157#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006158
Neil Armstrong80f6f322022-05-03 17:56:38 +02006159#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6160 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006161int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6162{
6163 unsigned char *p = ssl->handshake->premaster;
6164 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6165 const unsigned char *psk = NULL;
6166 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006167 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006168
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006169 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006170 {
6171 /*
6172 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006173 * checked before calling this function.
6174 *
6175 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006176 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006177 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006178 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6179 {
6180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6182 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006183 }
6184
6185 /*
6186 * PMS = struct {
6187 * opaque other_secret<0..2^16-1>;
6188 * opaque psk<0..2^16-1>;
6189 * };
6190 * with "other_secret" depending on the particular key exchange
6191 */
6192#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6193 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6194 {
6195 if( end - p < 2 )
6196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6197
6198 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6199 p += 2;
6200
6201 if( end < p || (size_t)( end - p ) < psk_len )
6202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6203
6204 memset( p, 0, psk_len );
6205 p += psk_len;
6206 }
6207 else
6208#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6209#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6210 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6211 {
6212 /*
6213 * other_secret already set by the ClientKeyExchange message,
6214 * and is 48 bytes long
6215 */
6216 if( end - p < 2 )
6217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6218
6219 *p++ = 0;
6220 *p++ = 48;
6221 p += 48;
6222 }
6223 else
6224#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6225#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6226 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6227 {
6228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6229 size_t len;
6230
6231 /* Write length only when we know the actual value */
6232 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6233 p + 2, end - ( p + 2 ), &len,
6234 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6235 {
6236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6237 return( ret );
6238 }
6239 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6240 p += 2 + len;
6241
6242 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6243 }
6244 else
6245#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006246#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006247 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6248 {
6249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6250 size_t zlen;
6251
6252 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6253 p + 2, end - ( p + 2 ),
6254 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6255 {
6256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6257 return( ret );
6258 }
6259
6260 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6261 p += 2 + zlen;
6262
6263 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6264 MBEDTLS_DEBUG_ECDH_Z );
6265 }
6266 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006267#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006268 {
6269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6271 }
6272
6273 /* opaque psk<0..2^16-1>; */
6274 if( end - p < 2 )
6275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6276
6277 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6278 p += 2;
6279
6280 if( end < p || (size_t)( end - p ) < psk_len )
6281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6282
6283 memcpy( p, psk, psk_len );
6284 p += psk_len;
6285
6286 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6287
6288 return( 0 );
6289}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006290#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006291
6292#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006293MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006294static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6295
6296#if defined(MBEDTLS_SSL_PROTO_DTLS)
6297int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6298{
6299 /* If renegotiation is not enforced, retransmit until we would reach max
6300 * timeout if we were using the usual handshake doubling scheme */
6301 if( ssl->conf->renego_max_records < 0 )
6302 {
6303 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6304 unsigned char doublings = 1;
6305
6306 while( ratio != 0 )
6307 {
6308 ++doublings;
6309 ratio >>= 1;
6310 }
6311
6312 if( ++ssl->renego_records_seen > doublings )
6313 {
6314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6315 return( 0 );
6316 }
6317 }
6318
6319 return( ssl_write_hello_request( ssl ) );
6320}
6321#endif
6322#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006323
Jerry Yud9526692022-02-17 14:23:47 +08006324/*
6325 * Handshake functions
6326 */
6327#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6328/* No certificate support -> dummy functions */
6329int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6330{
6331 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6332 ssl->handshake->ciphersuite_info;
6333
6334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6335
6336 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6337 {
6338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6339 ssl->state++;
6340 return( 0 );
6341 }
6342
6343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6345}
6346
6347int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6348{
6349 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6350 ssl->handshake->ciphersuite_info;
6351
6352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6353
6354 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6355 {
6356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6357 ssl->state++;
6358 return( 0 );
6359 }
6360
6361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6363}
6364
6365#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6366/* Some certificate support -> implement write and parse */
6367
6368int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6369{
6370 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6371 size_t i, n;
6372 const mbedtls_x509_crt *crt;
6373 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6374 ssl->handshake->ciphersuite_info;
6375
6376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6377
6378 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6379 {
6380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6381 ssl->state++;
6382 return( 0 );
6383 }
6384
6385#if defined(MBEDTLS_SSL_CLI_C)
6386 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6387 {
6388 if( ssl->handshake->client_auth == 0 )
6389 {
6390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6391 ssl->state++;
6392 return( 0 );
6393 }
6394 }
6395#endif /* MBEDTLS_SSL_CLI_C */
6396#if defined(MBEDTLS_SSL_SRV_C)
6397 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6398 {
6399 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6400 {
6401 /* Should never happen because we shouldn't have picked the
6402 * ciphersuite if we don't have a certificate. */
6403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6404 }
6405 }
6406#endif
6407
6408 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6409
6410 /*
6411 * 0 . 0 handshake type
6412 * 1 . 3 handshake length
6413 * 4 . 6 length of all certs
6414 * 7 . 9 length of cert. 1
6415 * 10 . n-1 peer certificate
6416 * n . n+2 length of cert. 2
6417 * n+3 . ... upper level cert, etc.
6418 */
6419 i = 7;
6420 crt = mbedtls_ssl_own_cert( ssl );
6421
6422 while( crt != NULL )
6423 {
6424 n = crt->raw.len;
6425 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6426 {
6427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6428 " > %" MBEDTLS_PRINTF_SIZET,
6429 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6430 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6431 }
6432
6433 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6434 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6435 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6436
6437 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6438 i += n; crt = crt->next;
6439 }
6440
6441 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6442 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6443 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6444
6445 ssl->out_msglen = i;
6446 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6447 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6448
6449 ssl->state++;
6450
6451 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6452 {
6453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6454 return( ret );
6455 }
6456
6457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6458
6459 return( ret );
6460}
6461
6462#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6463
6464#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006465MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006466static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6467 unsigned char *crt_buf,
6468 size_t crt_buf_len )
6469{
6470 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6471
6472 if( peer_crt == NULL )
6473 return( -1 );
6474
6475 if( peer_crt->raw.len != crt_buf_len )
6476 return( -1 );
6477
6478 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6479}
6480#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006481MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006482static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6483 unsigned char *crt_buf,
6484 size_t crt_buf_len )
6485{
6486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6487 unsigned char const * const peer_cert_digest =
6488 ssl->session->peer_cert_digest;
6489 mbedtls_md_type_t const peer_cert_digest_type =
6490 ssl->session->peer_cert_digest_type;
6491 mbedtls_md_info_t const * const digest_info =
6492 mbedtls_md_info_from_type( peer_cert_digest_type );
6493 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6494 size_t digest_len;
6495
6496 if( peer_cert_digest == NULL || digest_info == NULL )
6497 return( -1 );
6498
6499 digest_len = mbedtls_md_get_size( digest_info );
6500 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6501 return( -1 );
6502
6503 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6504 if( ret != 0 )
6505 return( -1 );
6506
6507 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6508}
6509#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6510#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6511
6512/*
6513 * Once the certificate message is read, parse it into a cert chain and
6514 * perform basic checks, but leave actual verification to the caller
6515 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006516MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006517static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6518 mbedtls_x509_crt *chain )
6519{
6520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6521#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6522 int crt_cnt=0;
6523#endif
6524 size_t i, n;
6525 uint8_t alert;
6526
6527 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6528 {
6529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6530 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6531 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6532 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6533 }
6534
6535 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6536 {
6537 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6538 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6539 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6540 }
6541
6542 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6543 {
6544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6545 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6546 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6547 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6548 }
6549
6550 i = mbedtls_ssl_hs_hdr_len( ssl );
6551
6552 /*
6553 * Same message structure as in mbedtls_ssl_write_certificate()
6554 */
6555 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6556
6557 if( ssl->in_msg[i] != 0 ||
6558 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6559 {
6560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6562 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6563 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6564 }
6565
6566 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6567 i += 3;
6568
6569 /* Iterate through and parse the CRTs in the provided chain. */
6570 while( i < ssl->in_hslen )
6571 {
6572 /* Check that there's room for the next CRT's length fields. */
6573 if ( i + 3 > ssl->in_hslen ) {
6574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6575 mbedtls_ssl_send_alert_message( ssl,
6576 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6577 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6578 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6579 }
6580 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6581 * anything beyond 2**16 ~ 64K. */
6582 if( ssl->in_msg[i] != 0 )
6583 {
6584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6585 mbedtls_ssl_send_alert_message( ssl,
6586 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6587 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6588 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6589 }
6590
6591 /* Read length of the next CRT in the chain. */
6592 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6593 | (unsigned int) ssl->in_msg[i + 2];
6594 i += 3;
6595
6596 if( n < 128 || i + n > ssl->in_hslen )
6597 {
6598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6599 mbedtls_ssl_send_alert_message( ssl,
6600 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6601 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6602 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6603 }
6604
6605 /* Check if we're handling the first CRT in the chain. */
6606#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6607 if( crt_cnt++ == 0 &&
6608 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6609 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6610 {
6611 /* During client-side renegotiation, check that the server's
6612 * end-CRTs hasn't changed compared to the initial handshake,
6613 * mitigating the triple handshake attack. On success, reuse
6614 * the original end-CRT instead of parsing it again. */
6615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6616 if( ssl_check_peer_crt_unchanged( ssl,
6617 &ssl->in_msg[i],
6618 n ) != 0 )
6619 {
6620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6621 mbedtls_ssl_send_alert_message( ssl,
6622 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6623 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6624 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6625 }
6626
6627 /* Now we can safely free the original chain. */
6628 ssl_clear_peer_cert( ssl->session );
6629 }
6630#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6631
6632 /* Parse the next certificate in the chain. */
6633#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6634 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6635#else
6636 /* If we don't need to store the CRT chain permanently, parse
6637 * it in-place from the input buffer instead of making a copy. */
6638 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6639#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6640 switch( ret )
6641 {
6642 case 0: /*ok*/
6643 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6644 /* Ignore certificate with an unknown algorithm: maybe a
6645 prior certificate was already trusted. */
6646 break;
6647
6648 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6649 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6650 goto crt_parse_der_failed;
6651
6652 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6653 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6654 goto crt_parse_der_failed;
6655
6656 default:
6657 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6658 crt_parse_der_failed:
6659 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6660 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6661 return( ret );
6662 }
6663
6664 i += n;
6665 }
6666
6667 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6668 return( 0 );
6669}
6670
6671#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006672MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006673static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6674{
6675 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6676 return( -1 );
6677
6678 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6679 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6680 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6681 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6682 {
Ronald Cron19385882022-06-15 16:26:13 +02006683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006684 return( 0 );
6685 }
6686 return( -1 );
6687}
6688#endif /* MBEDTLS_SSL_SRV_C */
6689
6690/* Check if a certificate message is expected.
6691 * Return either
6692 * - SSL_CERTIFICATE_EXPECTED, or
6693 * - SSL_CERTIFICATE_SKIP
6694 * indicating whether a Certificate message is expected or not.
6695 */
6696#define SSL_CERTIFICATE_EXPECTED 0
6697#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006698MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006699static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6700 int authmode )
6701{
6702 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6703 ssl->handshake->ciphersuite_info;
6704
6705 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6706 return( SSL_CERTIFICATE_SKIP );
6707
6708#if defined(MBEDTLS_SSL_SRV_C)
6709 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6710 {
6711 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6712 return( SSL_CERTIFICATE_SKIP );
6713
6714 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6715 {
6716 ssl->session_negotiate->verify_result =
6717 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6718 return( SSL_CERTIFICATE_SKIP );
6719 }
6720 }
6721#else
6722 ((void) authmode);
6723#endif /* MBEDTLS_SSL_SRV_C */
6724
6725 return( SSL_CERTIFICATE_EXPECTED );
6726}
6727
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006728MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006729static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6730 int authmode,
6731 mbedtls_x509_crt *chain,
6732 void *rs_ctx )
6733{
6734 int ret = 0;
6735 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6736 ssl->handshake->ciphersuite_info;
6737 int have_ca_chain = 0;
6738
6739 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6740 void *p_vrfy;
6741
6742 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6743 return( 0 );
6744
6745 if( ssl->f_vrfy != NULL )
6746 {
6747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6748 f_vrfy = ssl->f_vrfy;
6749 p_vrfy = ssl->p_vrfy;
6750 }
6751 else
6752 {
6753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6754 f_vrfy = ssl->conf->f_vrfy;
6755 p_vrfy = ssl->conf->p_vrfy;
6756 }
6757
6758 /*
6759 * Main check: verify certificate
6760 */
6761#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6762 if( ssl->conf->f_ca_cb != NULL )
6763 {
6764 ((void) rs_ctx);
6765 have_ca_chain = 1;
6766
6767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6768 ret = mbedtls_x509_crt_verify_with_ca_cb(
6769 chain,
6770 ssl->conf->f_ca_cb,
6771 ssl->conf->p_ca_cb,
6772 ssl->conf->cert_profile,
6773 ssl->hostname,
6774 &ssl->session_negotiate->verify_result,
6775 f_vrfy, p_vrfy );
6776 }
6777 else
6778#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6779 {
6780 mbedtls_x509_crt *ca_chain;
6781 mbedtls_x509_crl *ca_crl;
6782
6783#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6784 if( ssl->handshake->sni_ca_chain != NULL )
6785 {
6786 ca_chain = ssl->handshake->sni_ca_chain;
6787 ca_crl = ssl->handshake->sni_ca_crl;
6788 }
6789 else
6790#endif
6791 {
6792 ca_chain = ssl->conf->ca_chain;
6793 ca_crl = ssl->conf->ca_crl;
6794 }
6795
6796 if( ca_chain != NULL )
6797 have_ca_chain = 1;
6798
6799 ret = mbedtls_x509_crt_verify_restartable(
6800 chain,
6801 ca_chain, ca_crl,
6802 ssl->conf->cert_profile,
6803 ssl->hostname,
6804 &ssl->session_negotiate->verify_result,
6805 f_vrfy, p_vrfy, rs_ctx );
6806 }
6807
6808 if( ret != 0 )
6809 {
6810 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6811 }
6812
6813#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6814 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6815 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6816#endif
6817
6818 /*
6819 * Secondary checks: always done, but change 'ret' only if it was 0
6820 */
6821
6822#if defined(MBEDTLS_ECP_C)
6823 {
6824 const mbedtls_pk_context *pk = &chain->pk;
6825
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006826 /* If certificate uses an EC key, make sure the curve is OK.
6827 * This is a public key, so it can't be opaque, so can_do() is a good
6828 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006829 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006830 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006831 /* and in the unlikely case the above assumption no longer holds
6832 * we are making sure that pk_ec() here does not return a NULL
6833 */
6834 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6835 if( ec == NULL )
6836 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6839 }
Jerry Yud9526692022-02-17 14:23:47 +08006840
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006841 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6842 {
6843 ssl->session_negotiate->verify_result |=
6844 MBEDTLS_X509_BADCERT_BAD_KEY;
6845
6846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6847 if( ret == 0 )
6848 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6849 }
Jerry Yud9526692022-02-17 14:23:47 +08006850 }
6851 }
6852#endif /* MBEDTLS_ECP_C */
6853
6854 if( mbedtls_ssl_check_cert_usage( chain,
6855 ciphersuite_info,
6856 ! ssl->conf->endpoint,
6857 &ssl->session_negotiate->verify_result ) != 0 )
6858 {
6859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6860 if( ret == 0 )
6861 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6862 }
6863
6864 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6865 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6866 * with details encoded in the verification flags. All other kinds
6867 * of error codes, including those from the user provided f_vrfy
6868 * functions, are treated as fatal and lead to a failure of
6869 * ssl_parse_certificate even if verification was optional. */
6870 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6871 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6872 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6873 {
6874 ret = 0;
6875 }
6876
6877 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6878 {
6879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6880 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6881 }
6882
6883 if( ret != 0 )
6884 {
6885 uint8_t alert;
6886
6887 /* The certificate may have been rejected for several reasons.
6888 Pick one and send the corresponding alert. Which alert to send
6889 may be a subject of debate in some cases. */
6890 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6891 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6892 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6893 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6894 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6895 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6896 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6897 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6898 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6899 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6900 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6901 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6902 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6903 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6904 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6905 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6906 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6907 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6908 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6909 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6910 else
6911 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6912 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6913 alert );
6914 }
6915
6916#if defined(MBEDTLS_DEBUG_C)
6917 if( ssl->session_negotiate->verify_result != 0 )
6918 {
6919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6920 (unsigned int) ssl->session_negotiate->verify_result ) );
6921 }
6922 else
6923 {
6924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6925 }
6926#endif /* MBEDTLS_DEBUG_C */
6927
6928 return( ret );
6929}
6930
6931#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006932MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006933static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6934 unsigned char *start, size_t len )
6935{
6936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6937 /* Remember digest of the peer's end-CRT. */
6938 ssl->session_negotiate->peer_cert_digest =
6939 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6940 if( ssl->session_negotiate->peer_cert_digest == NULL )
6941 {
6942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6943 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6944 mbedtls_ssl_send_alert_message( ssl,
6945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6946 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6947
6948 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6949 }
6950
6951 ret = mbedtls_md( mbedtls_md_info_from_type(
6952 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6953 start, len,
6954 ssl->session_negotiate->peer_cert_digest );
6955
6956 ssl->session_negotiate->peer_cert_digest_type =
6957 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6958 ssl->session_negotiate->peer_cert_digest_len =
6959 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6960
6961 return( ret );
6962}
6963
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006964MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006965static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6966 unsigned char *start, size_t len )
6967{
6968 unsigned char *end = start + len;
6969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6970
6971 /* Make a copy of the peer's raw public key. */
6972 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6973 ret = mbedtls_pk_parse_subpubkey( &start, end,
6974 &ssl->handshake->peer_pubkey );
6975 if( ret != 0 )
6976 {
6977 /* We should have parsed the public key before. */
6978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6979 }
6980
6981 return( 0 );
6982}
6983#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6984
6985int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6986{
6987 int ret = 0;
6988 int crt_expected;
6989#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6990 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6991 ? ssl->handshake->sni_authmode
6992 : ssl->conf->authmode;
6993#else
6994 const int authmode = ssl->conf->authmode;
6995#endif
6996 void *rs_ctx = NULL;
6997 mbedtls_x509_crt *chain = NULL;
6998
6999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7000
7001 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7002 if( crt_expected == SSL_CERTIFICATE_SKIP )
7003 {
7004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7005 goto exit;
7006 }
7007
7008#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7009 if( ssl->handshake->ecrs_enabled &&
7010 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7011 {
7012 chain = ssl->handshake->ecrs_peer_cert;
7013 ssl->handshake->ecrs_peer_cert = NULL;
7014 goto crt_verify;
7015 }
7016#endif
7017
7018 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7019 {
7020 /* mbedtls_ssl_read_record may have sent an alert already. We
7021 let it decide whether to alert. */
7022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7023 goto exit;
7024 }
7025
7026#if defined(MBEDTLS_SSL_SRV_C)
7027 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7028 {
7029 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7030
7031 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7032 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7033
7034 goto exit;
7035 }
7036#endif /* MBEDTLS_SSL_SRV_C */
7037
7038 /* Clear existing peer CRT structure in case we tried to
7039 * reuse a session but it failed, and allocate a new one. */
7040 ssl_clear_peer_cert( ssl->session_negotiate );
7041
7042 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7043 if( chain == NULL )
7044 {
7045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7046 sizeof( mbedtls_x509_crt ) ) );
7047 mbedtls_ssl_send_alert_message( ssl,
7048 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7049 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7050
7051 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7052 goto exit;
7053 }
7054 mbedtls_x509_crt_init( chain );
7055
7056 ret = ssl_parse_certificate_chain( ssl, chain );
7057 if( ret != 0 )
7058 goto exit;
7059
7060#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7061 if( ssl->handshake->ecrs_enabled)
7062 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7063
7064crt_verify:
7065 if( ssl->handshake->ecrs_enabled)
7066 rs_ctx = &ssl->handshake->ecrs_ctx;
7067#endif
7068
7069 ret = ssl_parse_certificate_verify( ssl, authmode,
7070 chain, rs_ctx );
7071 if( ret != 0 )
7072 goto exit;
7073
7074#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7075 {
7076 unsigned char *crt_start, *pk_start;
7077 size_t crt_len, pk_len;
7078
7079 /* We parse the CRT chain without copying, so
7080 * these pointers point into the input buffer,
7081 * and are hence still valid after freeing the
7082 * CRT chain. */
7083
7084 crt_start = chain->raw.p;
7085 crt_len = chain->raw.len;
7086
7087 pk_start = chain->pk_raw.p;
7088 pk_len = chain->pk_raw.len;
7089
7090 /* Free the CRT structures before computing
7091 * digest and copying the peer's public key. */
7092 mbedtls_x509_crt_free( chain );
7093 mbedtls_free( chain );
7094 chain = NULL;
7095
7096 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7097 if( ret != 0 )
7098 goto exit;
7099
7100 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7101 if( ret != 0 )
7102 goto exit;
7103 }
7104#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7105 /* Pass ownership to session structure. */
7106 ssl->session_negotiate->peer_cert = chain;
7107 chain = NULL;
7108#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7109
7110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7111
7112exit:
7113
7114 if( ret == 0 )
7115 ssl->state++;
7116
7117#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7118 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7119 {
7120 ssl->handshake->ecrs_peer_cert = chain;
7121 chain = NULL;
7122 }
7123#endif
7124
7125 if( chain != NULL )
7126 {
7127 mbedtls_x509_crt_free( chain );
7128 mbedtls_free( chain );
7129 }
7130
7131 return( ret );
7132}
7133#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7134
Andrzej Kurek25f27152022-08-17 16:09:31 -04007135#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007136static void ssl_calc_finished_tls_sha256(
7137 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7138{
7139 int len = 12;
7140 const char *sender;
7141 unsigned char padbuf[32];
7142#if defined(MBEDTLS_USE_PSA_CRYPTO)
7143 size_t hash_size;
7144 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7145 psa_status_t status;
7146#else
7147 mbedtls_sha256_context sha256;
7148#endif
7149
7150 mbedtls_ssl_session *session = ssl->session_negotiate;
7151 if( !session )
7152 session = ssl->session;
7153
7154 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7155 ? "client finished"
7156 : "server finished";
7157
7158#if defined(MBEDTLS_USE_PSA_CRYPTO)
7159 sha256_psa = psa_hash_operation_init();
7160
7161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7162
7163 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7164 if( status != PSA_SUCCESS )
7165 {
7166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7167 return;
7168 }
7169
7170 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7171 if( status != PSA_SUCCESS )
7172 {
7173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7174 return;
7175 }
7176 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7177#else
7178
7179 mbedtls_sha256_init( &sha256 );
7180
7181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7182
7183 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7184
7185 /*
7186 * TLSv1.2:
7187 * hash = PRF( master, finished_label,
7188 * Hash( handshake ) )[0.11]
7189 */
7190
7191#if !defined(MBEDTLS_SHA256_ALT)
7192 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7193 sha256.state, sizeof( sha256.state ) );
7194#endif
7195
7196 mbedtls_sha256_finish( &sha256, padbuf );
7197 mbedtls_sha256_free( &sha256 );
7198#endif /* MBEDTLS_USE_PSA_CRYPTO */
7199
7200 ssl->handshake->tls_prf( session->master, 48, sender,
7201 padbuf, 32, buf, len );
7202
7203 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7204
7205 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7206
7207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7208}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007209#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007210
Jerry Yub7ba49e2022-02-17 14:25:53 +08007211
Andrzej Kurek25f27152022-08-17 16:09:31 -04007212#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007213static void ssl_calc_finished_tls_sha384(
7214 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7215{
7216 int len = 12;
7217 const char *sender;
7218 unsigned char padbuf[48];
7219#if defined(MBEDTLS_USE_PSA_CRYPTO)
7220 size_t hash_size;
7221 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7222 psa_status_t status;
7223#else
7224 mbedtls_sha512_context sha512;
7225#endif
7226
7227 mbedtls_ssl_session *session = ssl->session_negotiate;
7228 if( !session )
7229 session = ssl->session;
7230
7231 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7232 ? "client finished"
7233 : "server finished";
7234
7235#if defined(MBEDTLS_USE_PSA_CRYPTO)
7236 sha384_psa = psa_hash_operation_init();
7237
7238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7239
7240 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7241 if( status != PSA_SUCCESS )
7242 {
7243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7244 return;
7245 }
7246
7247 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7248 if( status != PSA_SUCCESS )
7249 {
7250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7251 return;
7252 }
7253 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7254#else
7255 mbedtls_sha512_init( &sha512 );
7256
7257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7258
Andrzej Kureka242e832022-08-11 10:03:14 -04007259 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007260
7261 /*
7262 * TLSv1.2:
7263 * hash = PRF( master, finished_label,
7264 * Hash( handshake ) )[0.11]
7265 */
7266
7267#if !defined(MBEDTLS_SHA512_ALT)
7268 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7269 sha512.state, sizeof( sha512.state ) );
7270#endif
7271 mbedtls_sha512_finish( &sha512, padbuf );
7272
7273 mbedtls_sha512_free( &sha512 );
7274#endif
7275
7276 ssl->handshake->tls_prf( session->master, 48, sender,
7277 padbuf, 48, buf, len );
7278
7279 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7280
7281 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7282
7283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7284}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007285#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007286
Jerry Yuaef00152022-02-17 14:27:31 +08007287void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7288{
7289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7290
7291 /*
7292 * Free our handshake params
7293 */
7294 mbedtls_ssl_handshake_free( ssl );
7295 mbedtls_free( ssl->handshake );
7296 ssl->handshake = NULL;
7297
7298 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007299 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007300 */
7301 if( ssl->transform )
7302 {
7303 mbedtls_ssl_transform_free( ssl->transform );
7304 mbedtls_free( ssl->transform );
7305 }
7306 ssl->transform = ssl->transform_negotiate;
7307 ssl->transform_negotiate = NULL;
7308
7309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7310}
7311
Jerry Yu2a9fff52022-02-17 14:28:51 +08007312void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7313{
7314 int resume = ssl->handshake->resume;
7315
7316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7317
7318#if defined(MBEDTLS_SSL_RENEGOTIATION)
7319 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7320 {
7321 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7322 ssl->renego_records_seen = 0;
7323 }
7324#endif
7325
7326 /*
7327 * Free the previous session and switch in the current one
7328 */
7329 if( ssl->session )
7330 {
7331#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7332 /* RFC 7366 3.1: keep the EtM state */
7333 ssl->session_negotiate->encrypt_then_mac =
7334 ssl->session->encrypt_then_mac;
7335#endif
7336
7337 mbedtls_ssl_session_free( ssl->session );
7338 mbedtls_free( ssl->session );
7339 }
7340 ssl->session = ssl->session_negotiate;
7341 ssl->session_negotiate = NULL;
7342
7343 /*
7344 * Add cache entry
7345 */
7346 if( ssl->conf->f_set_cache != NULL &&
7347 ssl->session->id_len != 0 &&
7348 resume == 0 )
7349 {
7350 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7351 ssl->session->id,
7352 ssl->session->id_len,
7353 ssl->session ) != 0 )
7354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7355 }
7356
7357#if defined(MBEDTLS_SSL_PROTO_DTLS)
7358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7359 ssl->handshake->flight != NULL )
7360 {
7361 /* Cancel handshake timer */
7362 mbedtls_ssl_set_timer( ssl, 0 );
7363
7364 /* Keep last flight around in case we need to resend it:
7365 * we need the handshake and transform structures for that */
7366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7367 }
7368 else
7369#endif
7370 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7371
7372 ssl->state++;
7373
7374 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7375}
7376
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007377int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7378{
7379 int ret, hash_len;
7380
7381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7382
7383 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7384
7385 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7386
7387 /*
7388 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7389 * may define some other value. Currently (early 2016), no defined
7390 * ciphersuite does this (and this is unlikely to change as activity has
7391 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7392 */
7393 hash_len = 12;
7394
7395#if defined(MBEDTLS_SSL_RENEGOTIATION)
7396 ssl->verify_data_len = hash_len;
7397 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7398#endif
7399
7400 ssl->out_msglen = 4 + hash_len;
7401 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7402 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7403
7404 /*
7405 * In case of session resuming, invert the client and server
7406 * ChangeCipherSpec messages order.
7407 */
7408 if( ssl->handshake->resume != 0 )
7409 {
7410#if defined(MBEDTLS_SSL_CLI_C)
7411 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7412 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7413#endif
7414#if defined(MBEDTLS_SSL_SRV_C)
7415 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7416 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7417#endif
7418 }
7419 else
7420 ssl->state++;
7421
7422 /*
7423 * Switch to our negotiated transform and session parameters for outbound
7424 * data.
7425 */
7426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7427
7428#if defined(MBEDTLS_SSL_PROTO_DTLS)
7429 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7430 {
7431 unsigned char i;
7432
7433 /* Remember current epoch settings for resending */
7434 ssl->handshake->alt_transform_out = ssl->transform_out;
7435 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7436 sizeof( ssl->handshake->alt_out_ctr ) );
7437
7438 /* Set sequence_number to zero */
7439 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7440
7441
7442 /* Increment epoch */
7443 for( i = 2; i > 0; i-- )
7444 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7445 break;
7446
7447 /* The loop goes to its end iff the counter is wrapping */
7448 if( i == 0 )
7449 {
7450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7451 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7452 }
7453 }
7454 else
7455#endif /* MBEDTLS_SSL_PROTO_DTLS */
7456 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7457
7458 ssl->transform_out = ssl->transform_negotiate;
7459 ssl->session_out = ssl->session_negotiate;
7460
7461#if defined(MBEDTLS_SSL_PROTO_DTLS)
7462 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7463 mbedtls_ssl_send_flight_completed( ssl );
7464#endif
7465
7466 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7467 {
7468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7469 return( ret );
7470 }
7471
7472#if defined(MBEDTLS_SSL_PROTO_DTLS)
7473 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7474 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7475 {
7476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7477 return( ret );
7478 }
7479#endif
7480
7481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7482
7483 return( 0 );
7484}
7485
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007486#define SSL_MAX_HASH_LEN 12
7487
7488int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7489{
7490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7491 unsigned int hash_len = 12;
7492 unsigned char buf[SSL_MAX_HASH_LEN];
7493
7494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7495
7496 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7497
7498 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7499 {
7500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7501 goto exit;
7502 }
7503
7504 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7505 {
7506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7508 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7509 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7510 goto exit;
7511 }
7512
7513 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7514 {
7515 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7516 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7517 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7518 goto exit;
7519 }
7520
7521 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7522 {
7523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7524 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7526 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7527 goto exit;
7528 }
7529
7530 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7531 buf, hash_len ) != 0 )
7532 {
7533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7534 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7535 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7536 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7537 goto exit;
7538 }
7539
7540#if defined(MBEDTLS_SSL_RENEGOTIATION)
7541 ssl->verify_data_len = hash_len;
7542 memcpy( ssl->peer_verify_data, buf, hash_len );
7543#endif
7544
7545 if( ssl->handshake->resume != 0 )
7546 {
7547#if defined(MBEDTLS_SSL_CLI_C)
7548 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7549 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7550#endif
7551#if defined(MBEDTLS_SSL_SRV_C)
7552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7553 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7554#endif
7555 }
7556 else
7557 ssl->state++;
7558
7559#if defined(MBEDTLS_SSL_PROTO_DTLS)
7560 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7561 mbedtls_ssl_recv_flight_completed( ssl );
7562#endif
7563
7564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7565
7566exit:
7567 mbedtls_platform_zeroize( buf, hash_len );
7568 return( ret );
7569}
7570
Jerry Yu392112c2022-02-17 14:34:10 +08007571#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7572/*
7573 * Helper to get TLS 1.2 PRF from ciphersuite
7574 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7575 */
7576static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7577{
Jerry Yu392112c2022-02-17 14:34:10 +08007578 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007579 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7580#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007581 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007582 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007583 else
Jerry Yu392112c2022-02-17 14:34:10 +08007584#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007585#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7586 {
7587 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7588 return( tls_prf_sha256 );
7589 }
7590#endif
7591#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7592 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7593 (void) ciphersuite_info;
7594#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007595
Andrzej Kurek894edde2022-09-29 06:31:14 -04007596 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007597}
7598#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007599
7600static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7601{
7602 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007603#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007604 if( tls_prf == tls_prf_sha384 )
7605 {
7606 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7607 }
7608 else
7609#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007610#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007611 if( tls_prf == tls_prf_sha256 )
7612 {
7613 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7614 }
7615 else
7616#endif
7617 return( MBEDTLS_SSL_TLS_PRF_NONE );
7618}
7619
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007620/*
7621 * Populate a transform structure with session keys and all the other
7622 * necessary information.
7623 *
7624 * Parameters:
7625 * - [in/out]: transform: structure to populate
7626 * [in] must be just initialised with mbedtls_ssl_transform_init()
7627 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7628 * - [in] ciphersuite
7629 * - [in] master
7630 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007631 * - [in] tls_prf: pointer to PRF to use for key derivation
7632 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007633 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007634 * - [in] endpoint: client or server
7635 * - [in] ssl: used for:
7636 * - ssl->conf->{f,p}_export_keys
7637 * [in] optionally used for:
7638 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7639 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007640MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007641static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7642 int ciphersuite,
7643 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007644#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007645 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007646#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007647 ssl_tls_prf_t tls_prf,
7648 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007649 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007650 unsigned endpoint,
7651 const mbedtls_ssl_context *ssl )
7652{
7653 int ret = 0;
7654 unsigned char keyblk[256];
7655 unsigned char *key1;
7656 unsigned char *key2;
7657 unsigned char *mac_enc;
7658 unsigned char *mac_dec;
7659 size_t mac_key_len = 0;
7660 size_t iv_copy_len;
7661 size_t keylen;
7662 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007663 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007664#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007665 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007666 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007667#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007668
7669#if defined(MBEDTLS_USE_PSA_CRYPTO)
7670 psa_key_type_t key_type;
7671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7672 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007673 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007674 size_t key_bits;
7675 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7676#endif
7677
7678#if !defined(MBEDTLS_DEBUG_C) && \
7679 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7680 if( ssl->f_export_keys == NULL )
7681 {
7682 ssl = NULL; /* make sure we don't use it except for these cases */
7683 (void) ssl;
7684 }
7685#endif
7686
7687 /*
7688 * Some data just needs copying into the structure
7689 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007690#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007691 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007692#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007693 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007694
7695#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7696 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7697#endif
7698
7699#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007700 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007701 {
7702 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7703 * generation separate. This should never happen. */
7704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7705 }
7706#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7707
7708 /*
7709 * Get various info structures
7710 */
7711 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7712 if( ciphersuite_info == NULL )
7713 {
7714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7715 ciphersuite ) );
7716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7717 }
7718
Neil Armstrongab555e02022-04-04 11:07:59 +02007719 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007720#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007721 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007722#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007723 ciphersuite_info );
7724
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007725 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7726 transform->taglen =
7727 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7728
7729#if defined(MBEDTLS_USE_PSA_CRYPTO)
7730 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7731 transform->taglen,
7732 &alg,
7733 &key_type,
7734 &key_bits ) ) != PSA_SUCCESS )
7735 {
7736 ret = psa_ssl_status_to_mbedtls( status );
7737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7738 goto end;
7739 }
7740#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007741 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7742 if( cipher_info == NULL )
7743 {
7744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7745 ciphersuite_info->cipher ) );
7746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7747 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007748#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007749
Neil Armstronge4512952022-03-08 09:08:22 +01007750#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007751 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007752 if( mac_alg == 0 )
7753 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007755 (unsigned) ciphersuite_info->mac ) );
7756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7757 }
7758#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007759 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7760 if( md_info == NULL )
7761 {
7762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7763 (unsigned) ciphersuite_info->mac ) );
7764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7765 }
Neil Armstronge4512952022-03-08 09:08:22 +01007766#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007767
7768#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7769 /* Copy own and peer's CID if the use of the CID
7770 * extension has been negotiated. */
7771 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7772 {
7773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7774
7775 transform->in_cid_len = ssl->own_cid_len;
7776 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7777 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7778 transform->in_cid_len );
7779
7780 transform->out_cid_len = ssl->handshake->peer_cid_len;
7781 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7782 ssl->handshake->peer_cid_len );
7783 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7784 transform->out_cid_len );
7785 }
7786#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7787
7788 /*
7789 * Compute key block using the PRF
7790 */
7791 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7792 if( ret != 0 )
7793 {
7794 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7795 return( ret );
7796 }
7797
7798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7799 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7800 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7801 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7802 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7803
7804 /*
7805 * Determine the appropriate key, IV and MAC length.
7806 */
7807
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007808#if defined(MBEDTLS_USE_PSA_CRYPTO)
7809 keylen = PSA_BITS_TO_BYTES(key_bits);
7810#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007811 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007812#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007813
7814#if defined(MBEDTLS_GCM_C) || \
7815 defined(MBEDTLS_CCM_C) || \
7816 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007817 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007818 {
7819 size_t explicit_ivlen;
7820
7821 transform->maclen = 0;
7822 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007823
7824 /* All modes haves 96-bit IVs, but the length of the static parts vary
7825 * with mode and version:
7826 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7827 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7828 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7829 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7830 * sequence number).
7831 */
7832 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007833
7834 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007835#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007836 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007837#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007838 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7839 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007840#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007841
7842 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007843 transform->fixed_ivlen = 12;
7844 else
7845 transform->fixed_ivlen = 4;
7846
7847 /* Minimum length of encrypted record */
7848 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7849 transform->minlen = explicit_ivlen + transform->taglen;
7850 }
7851 else
7852#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7853#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007854 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7855 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7856 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007857 {
Neil Armstronge4512952022-03-08 09:08:22 +01007858#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007859 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007860#else
7861 size_t block_size = cipher_info->block_size;
7862#endif /* MBEDTLS_USE_PSA_CRYPTO */
7863
7864#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007865 /* Get MAC length */
7866 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7867#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007868 /* Initialize HMAC contexts */
7869 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7870 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7871 {
7872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7873 goto end;
7874 }
7875
7876 /* Get MAC length */
7877 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007878#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007879 transform->maclen = mac_key_len;
7880
7881 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007882#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007883 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007884#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007885 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007886#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007887
7888 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007889 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007890 transform->minlen = transform->maclen;
7891 else
7892 {
7893 /*
7894 * GenericBlockCipher:
7895 * 1. if EtM is in use: one block plus MAC
7896 * otherwise: * first multiple of blocklen greater than maclen
7897 * 2. IV
7898 */
7899#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007900 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007901 {
7902 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007903 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007904 }
7905 else
7906#endif
7907 {
7908 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007909 + block_size
7910 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007911 }
7912
Glenn Strauss07c64162022-03-14 12:34:51 -04007913 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007914 {
7915 transform->minlen += transform->ivlen;
7916 }
7917 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007918 {
7919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7920 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7921 goto end;
7922 }
7923 }
7924 }
7925 else
7926#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7927 {
7928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7930 }
7931
7932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7933 (unsigned) keylen,
7934 (unsigned) transform->minlen,
7935 (unsigned) transform->ivlen,
7936 (unsigned) transform->maclen ) );
7937
7938 /*
7939 * Finally setup the cipher contexts, IVs and MAC secrets.
7940 */
7941#if defined(MBEDTLS_SSL_CLI_C)
7942 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7943 {
7944 key1 = keyblk + mac_key_len * 2;
7945 key2 = keyblk + mac_key_len * 2 + keylen;
7946
7947 mac_enc = keyblk;
7948 mac_dec = keyblk + mac_key_len;
7949
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007950 iv_copy_len = ( transform->fixed_ivlen ) ?
7951 transform->fixed_ivlen : transform->ivlen;
7952 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7953 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7954 iv_copy_len );
7955 }
7956 else
7957#endif /* MBEDTLS_SSL_CLI_C */
7958#if defined(MBEDTLS_SSL_SRV_C)
7959 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7960 {
7961 key1 = keyblk + mac_key_len * 2 + keylen;
7962 key2 = keyblk + mac_key_len * 2;
7963
7964 mac_enc = keyblk + mac_key_len;
7965 mac_dec = keyblk;
7966
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007967 iv_copy_len = ( transform->fixed_ivlen ) ?
7968 transform->fixed_ivlen : transform->ivlen;
7969 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7970 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7971 iv_copy_len );
7972 }
7973 else
7974#endif /* MBEDTLS_SSL_SRV_C */
7975 {
7976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7977 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7978 goto end;
7979 }
7980
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007981 if( ssl != NULL && ssl->f_export_keys != NULL )
7982 {
7983 ssl->f_export_keys( ssl->p_export_keys,
7984 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7985 master, 48,
7986 randbytes + 32,
7987 randbytes,
7988 tls_prf_get_type( tls_prf ) );
7989 }
7990
7991#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007992 transform->psa_alg = alg;
7993
7994 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7995 {
7996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7997 psa_set_key_algorithm( &attributes, alg );
7998 psa_set_key_type( &attributes, key_type );
7999
8000 if( ( status = psa_import_key( &attributes,
8001 key1,
8002 PSA_BITS_TO_BYTES( key_bits ),
8003 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8004 {
8005 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8006 ret = psa_ssl_status_to_mbedtls( status );
8007 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8008 goto end;
8009 }
8010
8011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8012
8013 if( ( status = psa_import_key( &attributes,
8014 key2,
8015 PSA_BITS_TO_BYTES( key_bits ),
8016 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8017 {
8018 ret = psa_ssl_status_to_mbedtls( status );
8019 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8020 goto end;
8021 }
8022 }
8023#else
8024 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8025 cipher_info ) ) != 0 )
8026 {
8027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8028 goto end;
8029 }
8030
8031 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8032 cipher_info ) ) != 0 )
8033 {
8034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8035 goto end;
8036 }
8037
8038 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8039 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8040 MBEDTLS_ENCRYPT ) ) != 0 )
8041 {
8042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8043 goto end;
8044 }
8045
8046 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8047 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8048 MBEDTLS_DECRYPT ) ) != 0 )
8049 {
8050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8051 goto end;
8052 }
8053
8054#if defined(MBEDTLS_CIPHER_MODE_CBC)
8055 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8056 {
8057 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8058 MBEDTLS_PADDING_NONE ) ) != 0 )
8059 {
8060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8061 goto end;
8062 }
8063
8064 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8065 MBEDTLS_PADDING_NONE ) ) != 0 )
8066 {
8067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8068 goto end;
8069 }
8070 }
8071#endif /* MBEDTLS_CIPHER_MODE_CBC */
8072#endif /* MBEDTLS_USE_PSA_CRYPTO */
8073
Neil Armstrong29c0c042022-03-17 17:47:28 +01008074#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8075 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8076 For AEAD-based ciphersuites, there is nothing to do here. */
8077 if( mac_key_len != 0 )
8078 {
8079#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008080 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008081
8082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008083 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008084 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8085
8086 if( ( status = psa_import_key( &attributes,
8087 mac_enc, mac_key_len,
8088 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8089 {
8090 ret = psa_ssl_status_to_mbedtls( status );
8091 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8092 goto end;
8093 }
8094
Ronald Cronfb39f152022-03-25 14:36:28 +01008095 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008096 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8097#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8098 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8099#endif
8100 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008101 /* mbedtls_ct_hmac() requires the key to be exportable */
8102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8103 PSA_KEY_USAGE_VERIFY_HASH );
8104 else
8105 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8106
8107 if( ( status = psa_import_key( &attributes,
8108 mac_dec, mac_key_len,
8109 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8110 {
8111 ret = psa_ssl_status_to_mbedtls( status );
8112 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8113 goto end;
8114 }
8115#else
8116 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8117 if( ret != 0 )
8118 goto end;
8119 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8120 if( ret != 0 )
8121 goto end;
8122#endif /* MBEDTLS_USE_PSA_CRYPTO */
8123 }
8124#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8125
8126 ((void) mac_dec);
8127 ((void) mac_enc);
8128
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008129end:
8130 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8131 return( ret );
8132}
8133
Jerry Yuee40f9d2022-02-17 14:55:16 +08008134#if defined(MBEDTLS_USE_PSA_CRYPTO)
8135int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8136 unsigned char *hash, size_t *hashlen,
8137 unsigned char *data, size_t data_len,
8138 mbedtls_md_type_t md_alg )
8139{
8140 psa_status_t status;
8141 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008142 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008143
8144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8145
8146 if( ( status = psa_hash_setup( &hash_operation,
8147 hash_alg ) ) != PSA_SUCCESS )
8148 {
8149 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8150 goto exit;
8151 }
8152
8153 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8154 64 ) ) != PSA_SUCCESS )
8155 {
8156 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8157 goto exit;
8158 }
8159
8160 if( ( status = psa_hash_update( &hash_operation,
8161 data, data_len ) ) != PSA_SUCCESS )
8162 {
8163 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8164 goto exit;
8165 }
8166
8167 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8168 hashlen ) ) != PSA_SUCCESS )
8169 {
8170 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8171 goto exit;
8172 }
8173
8174exit:
8175 if( status != PSA_SUCCESS )
8176 {
8177 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8178 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8179 switch( status )
8180 {
8181 case PSA_ERROR_NOT_SUPPORTED:
8182 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8183 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8184 case PSA_ERROR_BUFFER_TOO_SMALL:
8185 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8186 case PSA_ERROR_INSUFFICIENT_MEMORY:
8187 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8188 default:
8189 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8190 }
8191 }
8192 return( 0 );
8193}
8194
8195#else
8196
8197int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8198 unsigned char *hash, size_t *hashlen,
8199 unsigned char *data, size_t data_len,
8200 mbedtls_md_type_t md_alg )
8201{
8202 int ret = 0;
8203 mbedtls_md_context_t ctx;
8204 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8205 *hashlen = mbedtls_md_get_size( md_info );
8206
8207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8208
8209 mbedtls_md_init( &ctx );
8210
8211 /*
8212 * digitally-signed struct {
8213 * opaque client_random[32];
8214 * opaque server_random[32];
8215 * ServerDHParams params;
8216 * };
8217 */
8218 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8219 {
8220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8221 goto exit;
8222 }
8223 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8224 {
8225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8226 goto exit;
8227 }
8228 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8229 {
8230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8231 goto exit;
8232 }
8233 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8234 {
8235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8236 goto exit;
8237 }
8238 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8239 {
8240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8241 goto exit;
8242 }
8243
8244exit:
8245 mbedtls_md_free( &ctx );
8246
8247 if( ret != 0 )
8248 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8249 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8250
8251 return( ret );
8252}
8253#endif /* MBEDTLS_USE_PSA_CRYPTO */
8254
Jerry Yud9d91da2022-02-17 14:57:06 +08008255#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8256
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008257/* Find the preferred hash for a given signature algorithm. */
8258unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8259 mbedtls_ssl_context *ssl,
8260 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008261{
Gabor Mezei078e8032022-04-27 21:17:56 +02008262 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008263 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008264
8265 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008266 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008267
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008268 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008269 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008270 unsigned int hash_alg_received =
8271 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8272 received_sig_algs[i] );
8273 unsigned int sig_alg_received =
8274 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8275 received_sig_algs[i] );
8276
8277 if( sig_alg == sig_alg_received )
8278 {
8279#if defined(MBEDTLS_USE_PSA_CRYPTO)
8280 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8281 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008282 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008283 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008284
Neil Armstrong96eceb82022-06-30 18:05:05 +02008285 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8286 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8287 PSA_ALG_ECDSA( psa_hash_alg ),
8288 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008289 continue;
8290
Neil Armstrong96eceb82022-06-30 18:05:05 +02008291 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008292 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8293 PSA_ALG_RSA_PKCS1V15_SIGN(
8294 psa_hash_alg ),
8295 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008296 continue;
8297 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008298#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008299
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008300 return( hash_alg_received );
8301 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008302 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008303
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008304 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008305}
8306
8307#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8308
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008309/* Serialization of TLS 1.2 sessions:
8310 *
8311 * struct {
8312 * uint64 start_time;
8313 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008314 * uint8 session_id_len; // at most 32
8315 * opaque session_id[32];
8316 * opaque master[48]; // fixed length in the standard
8317 * uint32 verify_result;
8318 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8319 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8320 * uint32 ticket_lifetime;
8321 * uint8 mfl_code; // up to 255 according to standard
8322 * uint8 encrypt_then_mac; // 0 or 1
8323 * } serialized_session_tls12;
8324 *
8325 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008326static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008327 unsigned char *buf,
8328 size_t buf_len )
8329{
8330 unsigned char *p = buf;
8331 size_t used = 0;
8332
8333#if defined(MBEDTLS_HAVE_TIME)
8334 uint64_t start;
8335#endif
8336#if defined(MBEDTLS_X509_CRT_PARSE_C)
8337#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8338 size_t cert_len;
8339#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8340#endif /* MBEDTLS_X509_CRT_PARSE_C */
8341
8342 /*
8343 * Time
8344 */
8345#if defined(MBEDTLS_HAVE_TIME)
8346 used += 8;
8347
8348 if( used <= buf_len )
8349 {
8350 start = (uint64_t) session->start;
8351
8352 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8353 p += 8;
8354 }
8355#endif /* MBEDTLS_HAVE_TIME */
8356
8357 /*
8358 * Basic mandatory fields
8359 */
8360 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008361 + 1 /* id_len */
8362 + sizeof( session->id )
8363 + sizeof( session->master )
8364 + 4; /* verify_result */
8365
8366 if( used <= buf_len )
8367 {
8368 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8369 p += 2;
8370
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008371 *p++ = MBEDTLS_BYTE_0( session->id_len );
8372 memcpy( p, session->id, 32 );
8373 p += 32;
8374
8375 memcpy( p, session->master, 48 );
8376 p += 48;
8377
8378 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8379 p += 4;
8380 }
8381
8382 /*
8383 * Peer's end-entity certificate
8384 */
8385#if defined(MBEDTLS_X509_CRT_PARSE_C)
8386#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8387 if( session->peer_cert == NULL )
8388 cert_len = 0;
8389 else
8390 cert_len = session->peer_cert->raw.len;
8391
8392 used += 3 + cert_len;
8393
8394 if( used <= buf_len )
8395 {
8396 *p++ = MBEDTLS_BYTE_2( cert_len );
8397 *p++ = MBEDTLS_BYTE_1( cert_len );
8398 *p++ = MBEDTLS_BYTE_0( cert_len );
8399
8400 if( session->peer_cert != NULL )
8401 {
8402 memcpy( p, session->peer_cert->raw.p, cert_len );
8403 p += cert_len;
8404 }
8405 }
8406#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8407 if( session->peer_cert_digest != NULL )
8408 {
8409 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8410 if( used <= buf_len )
8411 {
8412 *p++ = (unsigned char) session->peer_cert_digest_type;
8413 *p++ = (unsigned char) session->peer_cert_digest_len;
8414 memcpy( p, session->peer_cert_digest,
8415 session->peer_cert_digest_len );
8416 p += session->peer_cert_digest_len;
8417 }
8418 }
8419 else
8420 {
8421 used += 2;
8422 if( used <= buf_len )
8423 {
8424 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8425 *p++ = 0;
8426 }
8427 }
8428#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8429#endif /* MBEDTLS_X509_CRT_PARSE_C */
8430
8431 /*
8432 * Session ticket if any, plus associated data
8433 */
8434#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8435 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8436
8437 if( used <= buf_len )
8438 {
8439 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8440 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8441 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8442
8443 if( session->ticket != NULL )
8444 {
8445 memcpy( p, session->ticket, session->ticket_len );
8446 p += session->ticket_len;
8447 }
8448
8449 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8450 p += 4;
8451 }
8452#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8453
8454 /*
8455 * Misc extension-related info
8456 */
8457#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8458 used += 1;
8459
8460 if( used <= buf_len )
8461 *p++ = session->mfl_code;
8462#endif
8463
8464#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8465 used += 1;
8466
8467 if( used <= buf_len )
8468 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8469#endif
8470
8471 return( used );
8472}
8473
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008474MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008475static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008476 const unsigned char *buf,
8477 size_t len )
8478{
8479#if defined(MBEDTLS_HAVE_TIME)
8480 uint64_t start;
8481#endif
8482#if defined(MBEDTLS_X509_CRT_PARSE_C)
8483#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8484 size_t cert_len;
8485#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8486#endif /* MBEDTLS_X509_CRT_PARSE_C */
8487
8488 const unsigned char *p = buf;
8489 const unsigned char * const end = buf + len;
8490
8491 /*
8492 * Time
8493 */
8494#if defined(MBEDTLS_HAVE_TIME)
8495 if( 8 > (size_t)( end - p ) )
8496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8497
8498 start = ( (uint64_t) p[0] << 56 ) |
8499 ( (uint64_t) p[1] << 48 ) |
8500 ( (uint64_t) p[2] << 40 ) |
8501 ( (uint64_t) p[3] << 32 ) |
8502 ( (uint64_t) p[4] << 24 ) |
8503 ( (uint64_t) p[5] << 16 ) |
8504 ( (uint64_t) p[6] << 8 ) |
8505 ( (uint64_t) p[7] );
8506 p += 8;
8507
8508 session->start = (time_t) start;
8509#endif /* MBEDTLS_HAVE_TIME */
8510
8511 /*
8512 * Basic mandatory fields
8513 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008514 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8516
8517 session->ciphersuite = ( p[0] << 8 ) | p[1];
8518 p += 2;
8519
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008520 session->id_len = *p++;
8521 memcpy( session->id, p, 32 );
8522 p += 32;
8523
8524 memcpy( session->master, p, 48 );
8525 p += 48;
8526
8527 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8528 ( (uint32_t) p[1] << 16 ) |
8529 ( (uint32_t) p[2] << 8 ) |
8530 ( (uint32_t) p[3] );
8531 p += 4;
8532
8533 /* Immediately clear invalid pointer values that have been read, in case
8534 * we exit early before we replaced them with valid ones. */
8535#if defined(MBEDTLS_X509_CRT_PARSE_C)
8536#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8537 session->peer_cert = NULL;
8538#else
8539 session->peer_cert_digest = NULL;
8540#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8541#endif /* MBEDTLS_X509_CRT_PARSE_C */
8542#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8543 session->ticket = NULL;
8544#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8545
8546 /*
8547 * Peer certificate
8548 */
8549#if defined(MBEDTLS_X509_CRT_PARSE_C)
8550#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8551 /* Deserialize CRT from the end of the ticket. */
8552 if( 3 > (size_t)( end - p ) )
8553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8554
8555 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8556 p += 3;
8557
8558 if( cert_len != 0 )
8559 {
8560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8561
8562 if( cert_len > (size_t)( end - p ) )
8563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8564
8565 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8566
8567 if( session->peer_cert == NULL )
8568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8569
8570 mbedtls_x509_crt_init( session->peer_cert );
8571
8572 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8573 p, cert_len ) ) != 0 )
8574 {
8575 mbedtls_x509_crt_free( session->peer_cert );
8576 mbedtls_free( session->peer_cert );
8577 session->peer_cert = NULL;
8578 return( ret );
8579 }
8580
8581 p += cert_len;
8582 }
8583#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8584 /* Deserialize CRT digest from the end of the ticket. */
8585 if( 2 > (size_t)( end - p ) )
8586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8587
8588 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8589 session->peer_cert_digest_len = (size_t) *p++;
8590
8591 if( session->peer_cert_digest_len != 0 )
8592 {
8593 const mbedtls_md_info_t *md_info =
8594 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8595 if( md_info == NULL )
8596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8597 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8599
8600 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8602
8603 session->peer_cert_digest =
8604 mbedtls_calloc( 1, session->peer_cert_digest_len );
8605 if( session->peer_cert_digest == NULL )
8606 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8607
8608 memcpy( session->peer_cert_digest, p,
8609 session->peer_cert_digest_len );
8610 p += session->peer_cert_digest_len;
8611 }
8612#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8613#endif /* MBEDTLS_X509_CRT_PARSE_C */
8614
8615 /*
8616 * Session ticket and associated data
8617 */
8618#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8619 if( 3 > (size_t)( end - p ) )
8620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8621
8622 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8623 p += 3;
8624
8625 if( session->ticket_len != 0 )
8626 {
8627 if( session->ticket_len > (size_t)( end - p ) )
8628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8629
8630 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8631 if( session->ticket == NULL )
8632 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8633
8634 memcpy( session->ticket, p, session->ticket_len );
8635 p += session->ticket_len;
8636 }
8637
8638 if( 4 > (size_t)( end - p ) )
8639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8640
8641 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8642 ( (uint32_t) p[1] << 16 ) |
8643 ( (uint32_t) p[2] << 8 ) |
8644 ( (uint32_t) p[3] );
8645 p += 4;
8646#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8647
8648 /*
8649 * Misc extension-related info
8650 */
8651#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8652 if( 1 > (size_t)( end - p ) )
8653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8654
8655 session->mfl_code = *p++;
8656#endif
8657
8658#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8659 if( 1 > (size_t)( end - p ) )
8660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8661
8662 session->encrypt_then_mac = *p++;
8663#endif
8664
8665 /* Done, should have consumed entire buffer */
8666 if( p != end )
8667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8668
8669 return( 0 );
8670}
Jerry Yudc7bd172022-02-17 13:44:15 +08008671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8672
XiaokangQian75d40ef2022-04-20 11:05:24 +00008673int mbedtls_ssl_validate_ciphersuite(
8674 const mbedtls_ssl_context *ssl,
8675 const mbedtls_ssl_ciphersuite_t *suite_info,
8676 mbedtls_ssl_protocol_version min_tls_version,
8677 mbedtls_ssl_protocol_version max_tls_version )
8678{
8679 (void) ssl;
8680
8681 if( suite_info == NULL )
8682 return( -1 );
8683
8684 if( ( suite_info->min_tls_version > max_tls_version ) ||
8685 ( suite_info->max_tls_version < min_tls_version ) )
8686 {
8687 return( -1 );
8688 }
8689
XiaokangQian060d8672022-04-21 09:24:56 +00008690#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008691#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8692 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8693 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8694 {
8695 return( -1 );
8696 }
8697#endif
8698
8699 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8700#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8701 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8702 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8703 {
8704 return( -1 );
8705 }
8706#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8707#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8708
8709 return( 0 );
8710}
8711
Ronald Crone68ab4f2022-10-05 12:46:29 +02008712#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008713/*
8714 * Function for writing a signature algorithm extension.
8715 *
8716 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8717 * value (TLS 1.3 RFC8446):
8718 * enum {
8719 * ....
8720 * ecdsa_secp256r1_sha256( 0x0403 ),
8721 * ecdsa_secp384r1_sha384( 0x0503 ),
8722 * ecdsa_secp521r1_sha512( 0x0603 ),
8723 * ....
8724 * } SignatureScheme;
8725 *
8726 * struct {
8727 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8728 * } SignatureSchemeList;
8729 *
8730 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8731 * value (TLS 1.2 RFC5246):
8732 * enum {
8733 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8734 * sha512(6), (255)
8735 * } HashAlgorithm;
8736 *
8737 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8738 * SignatureAlgorithm;
8739 *
8740 * struct {
8741 * HashAlgorithm hash;
8742 * SignatureAlgorithm signature;
8743 * } SignatureAndHashAlgorithm;
8744 *
8745 * SignatureAndHashAlgorithm
8746 * supported_signature_algorithms<2..2^16-2>;
8747 *
8748 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8749 * generalization of the TLS 1.2 signature algorithm extension.
8750 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8751 * `SignatureScheme` field of TLS 1.3
8752 *
8753 */
8754int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8755 const unsigned char *end, size_t *out_len )
8756{
8757 unsigned char *p = buf;
8758 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8759 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8760
8761 *out_len = 0;
8762
8763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8764
8765 /* Check if we have space for header and length field:
8766 * - extension_type (2 bytes)
8767 * - extension_data_length (2 bytes)
8768 * - supported_signature_algorithms_length (2 bytes)
8769 */
8770 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8771 p += 6;
8772
8773 /*
8774 * Write supported_signature_algorithms
8775 */
8776 supported_sig_alg = p;
8777 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8778 if( sig_alg == NULL )
8779 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8780
8781 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8782 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8784 *sig_alg,
8785 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008786 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8787 continue;
8788 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8789 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8790 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008792 *sig_alg,
8793 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008794 }
8795
8796 /* Length of supported_signature_algorithms */
8797 supported_sig_alg_len = p - supported_sig_alg;
8798 if( supported_sig_alg_len == 0 )
8799 {
8800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8802 }
8803
XiaokangQianeaf36512022-04-24 09:07:44 +00008804 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008805 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008806 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8807
XiaokangQianeaf36512022-04-24 09:07:44 +00008808 *out_len = p - buf;
8809
8810#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08008811 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
Jerry Yu0c354a22022-08-29 15:25:36 +08008812 MBEDTLS_SSL_DEBUG_MSG(
8813 4, ( "sent %s extension",
8814 mbedtls_tls13_get_extension_name(
8815 MBEDTLS_TLS_EXT_SIG_ALG ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008816#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08008817
XiaokangQianeaf36512022-04-24 09:07:44 +00008818 return( 0 );
8819}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008820#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008821
XiaokangQian40a35232022-05-07 09:02:40 +00008822#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008823/*
8824 * mbedtls_ssl_parse_server_name_ext
8825 *
8826 * Structure of server_name extension:
8827 *
8828 * enum {
8829 * host_name(0), (255)
8830 * } NameType;
8831 * opaque HostName<1..2^16-1>;
8832 *
8833 * struct {
8834 * NameType name_type;
8835 * select (name_type) {
8836 * case host_name: HostName;
8837 * } name;
8838 * } ServerName;
8839 * struct {
8840 * ServerName server_name_list<1..2^16-1>
8841 * } ServerNameList;
8842 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008843MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008844int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8845 const unsigned char *buf,
8846 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008847{
8848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8849 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008850 size_t server_name_list_len, hostname_len;
8851 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008852
XiaokangQianf2a94202022-05-20 06:44:24 +00008853 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008854
8855 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008856 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008857 p += 2;
8858
XiaokangQian9b2b7712022-05-17 02:57:00 +00008859 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8860 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008861 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008862 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008863 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008864 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008865 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8866 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008867
8868 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8869 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008870 /* sni_name is intended to be used only during the parsing of the
8871 * ClientHello message (it is reset to NULL before the end of
8872 * the message parsing). Thus it is ok to just point to the
8873 * reception buffer and not make a copy of it.
8874 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008875 ssl->handshake->sni_name = p + 3;
8876 ssl->handshake->sni_name_len = hostname_len;
8877 if( ssl->conf->f_sni == NULL )
8878 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008879 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008880 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008881 if( ret != 0 )
8882 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008883 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008884 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8885 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008886 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8887 }
8888 return( 0 );
8889 }
8890
8891 p += hostname_len + 3;
8892 }
8893
8894 return( 0 );
8895}
8896#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8897
XiaokangQianacb39922022-06-17 10:18:48 +00008898#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008899MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008900int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8901 const unsigned char *buf,
8902 const unsigned char *end )
8903{
8904 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008905 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008906 const unsigned char *protocol_name_list;
8907 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008908 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008909
8910 /* If ALPN not configured, just ignore the extension */
8911 if( ssl->conf->alpn_list == NULL )
8912 return( 0 );
8913
8914 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008915 * RFC7301, section 3.1
8916 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008917 *
XiaokangQianc7403452022-06-23 03:24:12 +00008918 * struct {
8919 * ProtocolName protocol_name_list<2..2^16-1>
8920 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008921 */
8922
XiaokangQianc7403452022-06-23 03:24:12 +00008923 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008924 * protocol_name_list_len 2 bytes
8925 * protocol_name_len 1 bytes
8926 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008927 */
XiaokangQianacb39922022-06-17 10:18:48 +00008928 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8929
XiaokangQianc7403452022-06-23 03:24:12 +00008930 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008931 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008932 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008933 protocol_name_list = p;
8934 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008935
8936 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008937 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008938 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008939 protocol_name_len = *p++;
8940 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8941 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008942 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008943 {
8944 MBEDTLS_SSL_PEND_FATAL_ALERT(
8945 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8946 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008947 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008948 }
8949
8950 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008951 }
8952
8953 /* Use our order of preference */
8954 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8955 {
8956 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008957 p = protocol_name_list;
8958 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008959 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008960 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008961 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008962 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008963 {
8964 ssl->alpn_chosen = *alpn;
8965 return( 0 );
8966 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008967
8968 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008969 }
8970 }
8971
XiaokangQian95d5f542022-06-24 02:29:26 +00008972 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008973 MBEDTLS_SSL_PEND_FATAL_ALERT(
8974 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8975 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8976 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8977}
8978
8979int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8980 unsigned char *buf,
8981 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008982 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008983{
8984 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008985 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008986 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008987
8988 if( ssl->alpn_chosen == NULL )
8989 {
8990 return( 0 );
8991 }
8992
XiaokangQian95d5f542022-06-24 02:29:26 +00008993 protocol_name_len = strlen( ssl->alpn_chosen );
8994 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008995
8996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8997 /*
8998 * 0 . 1 ext identifier
8999 * 2 . 3 ext length
9000 * 4 . 5 protocol list length
9001 * 6 . 6 protocol name length
9002 * 7 . 7+n protocol name
9003 */
9004 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9005
XiaokangQian95d5f542022-06-24 02:29:26 +00009006 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009007
XiaokangQian95d5f542022-06-24 02:29:26 +00009008 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9009 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009010 /* Note: the length of the chosen protocol has been checked to be less
9011 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9012 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009013 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009014
XiaokangQian95d5f542022-06-24 02:29:26 +00009015 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009016 return ( 0 );
9017}
9018#endif /* MBEDTLS_SSL_ALPN */
9019
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009020#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009021 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009022 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009023 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009024int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9025 const char *hostname )
9026{
9027 /* Initialize to suppress unnecessary compiler warning */
9028 size_t hostname_len = 0;
9029
9030 /* Check if new hostname is valid before
9031 * making any change to current one */
9032 if( hostname != NULL )
9033 {
9034 hostname_len = strlen( hostname );
9035
9036 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9038 }
9039
9040 /* Now it's clear that we will overwrite the old hostname,
9041 * so we can free it safely */
9042 if( session->hostname != NULL )
9043 {
9044 mbedtls_platform_zeroize( session->hostname,
9045 strlen( session->hostname ) );
9046 mbedtls_free( session->hostname );
9047 }
9048
9049 /* Passing NULL as hostname shall clear the old one */
9050 if( hostname == NULL )
9051 {
9052 session->hostname = NULL;
9053 }
9054 else
9055 {
9056 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9057 if( session->hostname == NULL )
9058 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9059
9060 memcpy( session->hostname, hostname, hostname_len );
9061 }
9062
9063 return( 0 );
9064}
Xiaokang Qian03409292022-10-12 02:49:52 +00009065#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9066 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009067 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009068 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070#endif /* MBEDTLS_SSL_TLS_C */