blob: 227f8841db893aeb792daf32e92115eb92048026 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Jerry Yu6848a612022-10-27 13:03:26 +0800171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yud25cab02022-10-31 12:48:30 +0800619#if defined(MBEDTLS_DEBUG_C)
620static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800621 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800622 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
623 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
624 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
625 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
626 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
627 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
628 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
629 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
630 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
631 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
632 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
633 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
634 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
635 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
636 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
637 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
638 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
639 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
640 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
641 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
642 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
643 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
644 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
646 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
647 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
648 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
649};
650
Jerry Yuea52ed92022-11-08 21:01:17 +0800651static unsigned int extension_type_table[]={
Jerry Yud25cab02022-10-31 12:48:30 +0800652 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
653 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
654 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
655 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
657 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
658 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
659 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
660 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
661 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
662 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
664 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
665 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
666 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
668 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
669 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
670 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
671 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
672 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
673 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
674 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
675 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
676 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
677 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
678 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
679 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
680};
681
682const char *mbedtls_ssl_get_extension_name( unsigned int extension_type )
683{
684 return( extension_name_table[
685 mbedtls_ssl_get_extension_id( extension_type ) ] );
686}
687
688static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type )
689{
690 switch( hs_msg_type )
691 {
692 case MBEDTLS_SSL_HS_CLIENT_HELLO:
693 return( "ClientHello" );
694 case MBEDTLS_SSL_HS_SERVER_HELLO:
695 return( "ServerHello" );
696 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
697 return( "HelloRetryRequest" );
698 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
699 return( "NewSessionTicket" );
700 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
701 return( "EncryptedExtensions" );
702 case MBEDTLS_SSL_HS_CERTIFICATE:
703 return( "Certificate" );
704 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
705 return( "CertificateRequest" );
706 }
Jerry Yu79aa7212022-11-08 21:30:21 +0800707 return( "Unknown" );
Jerry Yud25cab02022-10-31 12:48:30 +0800708}
709
Jerry Yu79aa7212022-11-08 21:30:21 +0800710void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type, unsigned int extension_type,
713 const char *extra_msg0, const char *extra_msg1 )
Jerry Yud25cab02022-10-31 12:48:30 +0800714{
715 const char *extra_msg;
716 if( extra_msg0 && extra_msg1 )
717 {
718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
720 "%s: %s(%u) extension %s %s.",
721 ssl_tls13_get_hs_msg_name( hs_msg_type ),
722 mbedtls_ssl_get_extension_name( extension_type ),
723 extension_type,
724 extra_msg0, extra_msg1 );
725 return;
726 }
727
728 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
729 if( extra_msg )
730 {
731 mbedtls_debug_print_msg(
732 ssl, level, file, line,
733 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
734 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
735 extra_msg );
736 return;
737 }
738
739 mbedtls_debug_print_msg(
740 ssl, level, file, line,
741 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
742 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
743}
744
745void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
746 int level, const char *file, int line,
747 int hs_msg_type, uint32_t extensions_mask,
748 const char *extra )
749{
750
751 for( unsigned i = 0;
752 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
753 i++ )
754 {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yu97be6a92022-11-09 22:43:31 +0800757 extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra );
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
761#endif /* MBEDTLS_DEBUG_C */
762
Jerry Yuc73c6182022-02-08 20:29:25 +0800763void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
765{
766 ((void) ciphersuite_info);
767
Andrzej Kurek25f27152022-08-17 16:09:31 -0400768#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800769 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
770 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
771 else
772#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400773#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
775 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
776 else
777#endif
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
780 return;
781 }
782}
783
XiaokangQianadab9a62022-07-18 07:41:26 +0000784void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
785 unsigned hs_type,
786 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100787{
788 unsigned char hs_hdr[4];
789
790 /* Build HS header for checksum update. */
791 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
792 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
793 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
794 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
795
796 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
797}
798
799void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
800 unsigned hs_type,
801 unsigned char const *msg,
802 size_t msg_len )
803{
804 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
805 ssl->handshake->update_checksum( ssl, msg, msg_len );
806}
807
Jerry Yuc73c6182022-02-08 20:29:25 +0800808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
809{
810 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#if defined(MBEDTLS_USE_PSA_CRYPTO)
813 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
814 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
815#else
816 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
817#endif
818#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400819#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
822 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
823#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400824 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#endif
826#endif
827}
828
829static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
830 const unsigned char *buf, size_t len )
831{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
834 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
835#else
836 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
837#endif
838#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
842#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400843 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400846#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
847 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
848 (void) ssl;
849 (void) buf;
850 (void) len;
851#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800852}
853
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
856 const unsigned char *buf, size_t len )
857{
858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
860#else
861 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
862#endif
863}
864#endif
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
868 const unsigned char *buf, size_t len )
869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
871 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
872#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400873 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875}
876#endif
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881
Andrzej Kurek25f27152022-08-17 16:09:31 -0400882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884 handshake->fin_sha256_psa = psa_hash_operation_init();
885 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200888 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500893 handshake->fin_sha384_psa = psa_hash_operation_init();
894 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400896 mbedtls_sha512_init( &handshake->fin_sha384 );
897 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900
901 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_DHM_C)
904 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200906#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200910#if defined(MBEDTLS_USE_PSA_CRYPTO)
911 handshake->psa_pake_ctx = psa_pake_operation_init();
912 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
913#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200914 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200916#if defined(MBEDTLS_SSL_CLI_C)
917 handshake->ecjpake_cache = NULL;
918 handshake->ecjpake_cache_len = 0;
919#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200920#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200921
Gilles Peskineeccd8882020-03-10 12:19:08 +0100922#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200923 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200924#endif
925
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
927 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
928#endif
Hanno Becker75173122019-02-06 16:18:31 +0000929
930#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
931 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
932 mbedtls_pk_init( &handshake->peer_pubkey );
933#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934}
935
Hanno Beckera18d1322018-01-03 14:27:32 +0000936void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200939
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
942 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100943#else
944 mbedtls_cipher_init( &transform->cipher_ctx_enc );
945 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100946#endif
947
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
951 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100952#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_md_init( &transform->md_ctx_enc );
954 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000955#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100956#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957}
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962}
963
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200964MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000966{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000968 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200973 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974
975 /*
976 * Either the pointers are now NULL or cleared properly and can be freed.
977 * Now allocate missing structures.
978 */
979 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200981 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Paul Bakker48916f92012-09-16 19:57:18 +0000988
Paul Bakker82788fb2014-10-20 13:59:19 +0200989 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200991 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200992 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500993#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
994 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400995
Andrzej Kurek4a063792020-10-21 15:08:44 +0200996 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
997 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500998#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000999
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001000 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00001001 if( ssl->handshake == NULL ||
1002 ssl->transform_negotiate == NULL ||
1003 ssl->session_negotiate == NULL )
1004 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_free( ssl->handshake );
1008 mbedtls_free( ssl->transform_negotiate );
1009 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010
1011 ssl->handshake = NULL;
1012 ssl->transform_negotiate = NULL;
1013 ssl->session_negotiate = NULL;
1014
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001015 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001016 }
1017
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001020 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001021 ssl_handshake_params_init( ssl->handshake );
1022
Jerry Yud0766ec2022-09-22 10:46:57 +08001023#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1024 defined(MBEDTLS_SSL_SRV_C) && \
1025 defined(MBEDTLS_SSL_SESSION_TICKETS)
1026 ssl->handshake->new_session_tickets_count =
1027 ssl->conf->new_session_tickets_count ;
1028#endif
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1032 {
1033 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001034
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1036 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1037 else
1038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001039
Hanno Becker0f57a652020-02-05 10:37:26 +00001040 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001041 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001042#endif
1043
Brett Warrene0edc842021-08-17 09:53:13 +01001044/*
1045 * curve_list is translated to IANA TLS group identifiers here because
1046 * mbedtls_ssl_conf_curves returns void and so can't return
1047 * any error codes.
1048 */
1049#if defined(MBEDTLS_ECP_C)
1050#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1051 /* Heap allocate and translate curve_list from internal to IANA group ids */
1052 if ( ssl->conf->curve_list != NULL )
1053 {
1054 size_t length;
1055 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1056
1057 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1058 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1059
1060 /* Leave room for zero termination */
1061 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1062 if ( group_list == NULL )
1063 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1064
1065 for( size_t i = 0; i < length; i++ )
1066 {
1067 const mbedtls_ecp_curve_info *info =
1068 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1069 if ( info == NULL )
1070 {
1071 mbedtls_free( group_list );
1072 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1073 }
1074 group_list[i] = info->tls_id;
1075 }
1076
1077 group_list[length] = 0;
1078
1079 ssl->handshake->group_list = group_list;
1080 ssl->handshake->group_list_heap_allocated = 1;
1081 }
1082 else
1083 {
1084 ssl->handshake->group_list = ssl->conf->group_list;
1085 ssl->handshake->group_list_heap_allocated = 0;
1086 }
1087#endif /* MBEDTLS_DEPRECATED_REMOVED */
1088#endif /* MBEDTLS_ECP_C */
1089
Ronald Crone68ab4f2022-10-05 12:46:29 +02001090#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001091#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001093 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1094 signature algorithms IANA identifiers. */
1095 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001096 ssl->conf->sig_hashes != NULL )
1097 {
1098 const int *md;
1099 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001100 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001101 uint16_t *p;
1102
Jerry Yub476a442022-01-21 18:14:45 +08001103#if defined(static_assert)
1104 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1105 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1106 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001107#endif
1108
Jerry Yuf017ee42022-01-12 15:49:48 +08001109 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1110 {
1111 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1112 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001113#if defined(MBEDTLS_ECDSA_C)
1114 sig_algs_len += sizeof( uint16_t );
1115#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001116
Jerry Yub476a442022-01-21 18:14:45 +08001117#if defined(MBEDTLS_RSA_C)
1118 sig_algs_len += sizeof( uint16_t );
1119#endif
1120 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1121 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001122 }
1123
Jerry Yub476a442022-01-21 18:14:45 +08001124 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001125 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1126
Jerry Yub476a442022-01-21 18:14:45 +08001127 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1128 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001129 if( ssl->handshake->sig_algs == NULL )
1130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1131
1132 p = (uint16_t *)ssl->handshake->sig_algs;
1133 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1134 {
1135 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1136 if( hash == MBEDTLS_SSL_HASH_NONE )
1137 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001138#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001139 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1140 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001141#endif
1142#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1144 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001145#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001146 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001147 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001148 ssl->handshake->sig_algs_heap_allocated = 1;
1149 }
1150 else
Jerry Yua69269a2022-01-17 21:06:01 +08001151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001152 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001153 ssl->handshake->sig_algs_heap_allocated = 0;
1154 }
Jerry Yucc539102022-06-27 16:27:35 +08001155#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001156#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001161/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001162MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001163static int ssl_cookie_write_dummy( void *ctx,
1164 unsigned char **p, unsigned char *end,
1165 const unsigned char *cli_id, size_t cli_id_len )
1166{
1167 ((void) ctx);
1168 ((void) p);
1169 ((void) end);
1170 ((void) cli_id);
1171 ((void) cli_id_len);
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001174}
1175
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001176MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001177static int ssl_cookie_check_dummy( void *ctx,
1178 const unsigned char *cookie, size_t cookie_len,
1179 const unsigned char *cli_id, size_t cli_id_len )
1180{
1181 ((void) ctx);
1182 ((void) cookie);
1183 ((void) cookie_len);
1184 ((void) cli_id);
1185 ((void) cli_id_len);
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001188}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001189#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001190
Paul Bakker5121ce52009-01-03 21:22:43 +00001191/*
1192 * Initialize an SSL context
1193 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001194void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1195{
1196 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1197}
1198
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001199MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001200static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1201{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001202 const mbedtls_ssl_config *conf = ssl->conf;
1203
Ronald Cron6f135e12021-12-08 16:57:54 +01001204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001205 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1206 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1208 {
1209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1211 }
1212
Jerry Yu60835a82021-08-04 10:13:52 +08001213 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1214 return( 0 );
1215 }
1216#endif
1217
1218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001219 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1222 return( 0 );
1223 }
1224#endif
1225
Ronald Cron6f135e12021-12-08 16:57:54 +01001226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001227 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001228 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1230 {
1231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1232 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1233 }
1234
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001235 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1236 {
1237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1238 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1239 }
1240
1241
Ronald Crone1d3f062022-02-10 14:50:54 +01001242 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1243 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001244 }
1245#endif
1246
1247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1248 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1249}
1250
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001251MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001252static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1253{
1254 int ret;
1255 ret = ssl_conf_version_check( ssl );
1256 if( ret != 0 )
1257 return( ret );
1258
Jerry Yudef7ae42022-10-30 14:13:19 +08001259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1260 /* RFC 8446 section 4.4.3
1261 *
1262 * If the verification fails, the receiver MUST terminate the handshake with
1263 * a "decrypt_error" alert.
1264 *
1265 * If the client is configured as TLS 1.3 only with optional verify, return
1266 * bad config.
1267 *
1268 */
1269 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1270 (mbedtls_ssl_context *)ssl ) &&
1271 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1272 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1273 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1274 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1275 {
1276 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001277 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001278 "is not available for TLS 1.3 client" ) );
1279 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1280 }
1281#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1282
Jerry Yu60835a82021-08-04 10:13:52 +08001283 /* Space for further checks */
1284
1285 return( 0 );
1286}
1287
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001288/*
1289 * Setup an SSL context
1290 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001291
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001292int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001293 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294{
Janos Follath865b3eb2019-12-16 11:46:15 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001296 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1297 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001299 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001300
Jerry Yu60835a82021-08-04 10:13:52 +08001301 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1302 return( ret );
1303
Paul Bakker62f2dee2012-09-28 07:31:51 +00001304 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001305 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001306 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001307
1308 /* Set to NULL in case of an error condition */
1309 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001310
Darryl Greenb33cc762019-11-28 14:29:44 +00001311#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1312 ssl->in_buf_len = in_buf_len;
1313#endif
1314 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001315 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001318 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001319 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001320 }
1321
Darryl Greenb33cc762019-11-28 14:29:44 +00001322#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1323 ssl->out_buf_len = out_buf_len;
1324#endif
1325 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001326 if( ssl->out_buf == NULL )
1327 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001330 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
1332
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001333 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001334
Johan Pascalb62bb512015-12-03 21:56:45 +01001335#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001336 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001337#endif
1338
Paul Bakker48916f92012-09-16 19:57:18 +00001339 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001340 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001343
1344error:
1345 mbedtls_free( ssl->in_buf );
1346 mbedtls_free( ssl->out_buf );
1347
1348 ssl->conf = NULL;
1349
Darryl Greenb33cc762019-11-28 14:29:44 +00001350#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1351 ssl->in_buf_len = 0;
1352 ssl->out_buf_len = 0;
1353#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001354 ssl->in_buf = NULL;
1355 ssl->out_buf = NULL;
1356
1357 ssl->in_hdr = NULL;
1358 ssl->in_ctr = NULL;
1359 ssl->in_len = NULL;
1360 ssl->in_iv = NULL;
1361 ssl->in_msg = NULL;
1362
1363 ssl->out_hdr = NULL;
1364 ssl->out_ctr = NULL;
1365 ssl->out_len = NULL;
1366 ssl->out_iv = NULL;
1367 ssl->out_msg = NULL;
1368
k-stachowiak9f7798e2018-07-31 16:52:32 +02001369 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370}
1371
1372/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001373 * Reset an initialized and used SSL context for re-use while retaining
1374 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001375 *
1376 * If partial is non-zero, keep data in the input buffer and client ID.
1377 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001378 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001379void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1380 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001381{
Darryl Greenb33cc762019-11-28 14:29:44 +00001382#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1383 size_t in_buf_len = ssl->in_buf_len;
1384 size_t out_buf_len = ssl->out_buf_len;
1385#else
1386 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1388#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001389
Hanno Beckerb0302c42021-08-03 09:39:42 +01001390#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1391 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001392#endif
1393
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001394 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001395 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001396
Hanno Beckerb0302c42021-08-03 09:39:42 +01001397 mbedtls_ssl_reset_in_out_pointers( ssl );
1398
1399 /* Reset incoming message parsing */
1400 ssl->in_offt = NULL;
1401 ssl->nb_zero = 0;
1402 ssl->in_msgtype = 0;
1403 ssl->in_msglen = 0;
1404 ssl->in_hslen = 0;
1405 ssl->keep_current_message = 0;
1406 ssl->transform_in = NULL;
1407
1408#if defined(MBEDTLS_SSL_PROTO_DTLS)
1409 ssl->next_record_offset = 0;
1410 ssl->in_epoch = 0;
1411#endif
1412
1413 /* Keep current datagram if partial == 1 */
1414 if( partial == 0 )
1415 {
1416 ssl->in_left = 0;
1417 memset( ssl->in_buf, 0, in_buf_len );
1418 }
1419
Ronald Cronad8c17b2022-06-10 17:18:09 +02001420 ssl->send_alert = 0;
1421
Hanno Beckerb0302c42021-08-03 09:39:42 +01001422 /* Reset outgoing message writing */
1423 ssl->out_msgtype = 0;
1424 ssl->out_msglen = 0;
1425 ssl->out_left = 0;
1426 memset( ssl->out_buf, 0, out_buf_len );
1427 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1428 ssl->transform_out = NULL;
1429
1430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1431 mbedtls_ssl_dtls_replay_reset( ssl );
1432#endif
1433
XiaokangQian2b01dc32022-01-21 02:53:13 +00001434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435 if( ssl->transform )
1436 {
1437 mbedtls_ssl_transform_free( ssl->transform );
1438 mbedtls_free( ssl->transform );
1439 ssl->transform = NULL;
1440 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1442
XiaokangQian2b01dc32022-01-21 02:53:13 +00001443#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1444 mbedtls_ssl_transform_free( ssl->transform_application );
1445 mbedtls_free( ssl->transform_application );
1446 ssl->transform_application = NULL;
1447
1448 if( ssl->handshake != NULL )
1449 {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001450#if defined(MBEDTLS_SSL_EARLY_DATA)
XiaokangQian2b01dc32022-01-21 02:53:13 +00001451 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1452 mbedtls_free( ssl->handshake->transform_earlydata );
1453 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001454#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455
1456 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1457 mbedtls_free( ssl->handshake->transform_handshake );
1458 ssl->handshake->transform_handshake = NULL;
1459 }
1460
XiaokangQian2b01dc32022-01-21 02:53:13 +00001461#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001462}
1463
1464int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1465{
1466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1467
1468 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1469
XiaokangQian78b1fa72022-01-19 06:56:30 +00001470 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001471
1472 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_RENEGOTIATION)
1474 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001475 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001476
1477 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1479 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001482
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001484 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001485 if( ssl->session )
1486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 mbedtls_ssl_session_free( ssl->session );
1488 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001489 ssl->session = NULL;
1490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001493 ssl->alpn_chosen = NULL;
1494#endif
1495
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001496#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001497 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001498#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001499 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001500#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001501 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001502 {
1503 mbedtls_free( ssl->cli_id );
1504 ssl->cli_id = NULL;
1505 ssl->cli_id_len = 0;
1506 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001507#endif
1508
Paul Bakker48916f92012-09-16 19:57:18 +00001509 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1510 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001511
1512 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001513}
1514
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001515/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001516 * Reset an initialized and used SSL context for re-use while retaining
1517 * all application-set variables, function pointers and data.
1518 */
1519int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1520{
Hanno Becker43aefe22020-02-05 10:44:56 +00001521 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001522}
1523
1524/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 * SSL set accessors
1526 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001527void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001529 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530}
1531
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001532void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001534 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001538void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001540 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001541}
1542#endif
1543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001544void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001547}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001550
Hanno Becker1841b0a2018-08-24 11:13:57 +01001551void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1552 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001553{
1554 ssl->disable_datagram_packing = !allow_packing;
1555}
1556
1557void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1558 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001559{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001560 conf->hs_timeout_min = min;
1561 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001562}
1563#endif
1564
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001565void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001567 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568}
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001571void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001572 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001573 void *p_vrfy )
1574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001575 conf->f_vrfy = f_vrfy;
1576 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001579
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001580void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001581 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 void *p_rng )
1583{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001584 conf->f_rng = f_rng;
1585 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001586}
1587
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001588void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001589 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 void *p_dbg )
1591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001592 conf->f_dbg = f_dbg;
1593 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001597 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001598 mbedtls_ssl_send_t *f_send,
1599 mbedtls_ssl_recv_t *f_recv,
1600 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001601{
1602 ssl->p_bio = p_bio;
1603 ssl->f_send = f_send;
1604 ssl->f_recv = f_recv;
1605 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001606}
1607
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001608#if defined(MBEDTLS_SSL_PROTO_DTLS)
1609void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1610{
1611 ssl->mtu = mtu;
1612}
1613#endif
1614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001615void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001616{
1617 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001618}
1619
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001620void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1621 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001622 mbedtls_ssl_set_timer_t *f_set_timer,
1623 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001624{
1625 ssl->p_timer = p_timer;
1626 ssl->f_set_timer = f_set_timer;
1627 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001628
1629 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001630 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001631}
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001634void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001635 void *p_cache,
1636 mbedtls_ssl_cache_get_t *f_get_cache,
1637 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001639 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001640 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001641 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_SSL_CLI_C)
1646int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001647{
Janos Follath865b3eb2019-12-16 11:46:15 +00001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001649
1650 if( ssl == NULL ||
1651 session == NULL ||
1652 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001653 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001656 }
1657
Hanno Beckere810bbc2021-05-14 16:01:05 +01001658 if( ssl->handshake->resume == 1 )
1659 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1660
Jerry Yu21092062022-10-10 21:21:31 +08001661#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1662 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001663 {
Jerry Yu21092062022-10-10 21:21:31 +08001664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1665 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1666
1667 if( mbedtls_ssl_validate_ciphersuite(
1668 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1669 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1670 {
1671 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1672 session->ciphersuite ) );
1673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1674 }
Jerry Yu40afab62022-10-08 10:42:13 +08001675 }
Jerry Yu21092062022-10-10 21:21:31 +08001676#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001677
Hanno Becker52055ae2019-02-06 14:30:46 +00001678 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1679 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001680 return( ret );
1681
Paul Bakker0a597072012-09-25 21:55:46 +00001682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001683
1684 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001688void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1689 const int *ciphersuites )
1690{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001691 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692}
1693
Ronald Cron6f135e12021-12-08 16:57:54 +01001694#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001695void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001696 const int kex_modes )
1697{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001698 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001699}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001700
1701#if defined(MBEDTLS_SSL_EARLY_DATA)
1702void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001703 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001704{
1705 conf->early_data_enabled = early_data_enabled;
1706}
Jerry Yucc4e0072022-11-22 17:22:22 +08001707
1708#if defined(MBEDTLS_SSL_SRV_C)
1709void mbedtls_ssl_tls13_conf_max_early_data_size(
1710 mbedtls_ssl_config *conf, uint32_t max_early_data_size )
1711{
1712 conf->max_early_data_size =
1713 max_early_data_size < MBEDTLS_SSL_MAX_EARLY_DATA_SIZE ?
1714 max_early_data_size : MBEDTLS_SSL_MAX_EARLY_DATA_SIZE;
1715}
1716#endif /* MBEDTLS_SSL_SRV_C */
1717
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001718#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001719#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001722void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001723 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001724{
1725 conf->cert_profile = profile;
1726}
1727
Glenn Strauss36872db2022-01-22 05:06:31 -05001728static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1729{
1730 mbedtls_ssl_key_cert *cur = key_cert, *next;
1731
1732 while( cur != NULL )
1733 {
1734 next = cur->next;
1735 mbedtls_free( cur );
1736 cur = next;
1737 }
1738}
1739
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001740/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001741MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001742static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1743 mbedtls_x509_crt *cert,
1744 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001745{
niisato8ee24222018-06-25 19:05:48 +09001746 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001747
Glenn Strauss36872db2022-01-22 05:06:31 -05001748 if( cert == NULL )
1749 {
1750 /* Free list if cert is null */
1751 ssl_key_cert_free( *head );
1752 *head = NULL;
1753 return( 0 );
1754 }
1755
niisato8ee24222018-06-25 19:05:48 +09001756 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1757 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001759
niisato8ee24222018-06-25 19:05:48 +09001760 new_cert->cert = cert;
1761 new_cert->key = key;
1762 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001763
Glenn Strauss36872db2022-01-22 05:06:31 -05001764 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001765 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001766 {
niisato8ee24222018-06-25 19:05:48 +09001767 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001768 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001769 else
1770 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001771 mbedtls_ssl_key_cert *cur = *head;
1772 while( cur->next != NULL )
1773 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001774 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001775 }
1776
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001777 return( 0 );
1778}
1779
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001780int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001781 mbedtls_x509_crt *own_cert,
1782 mbedtls_pk_context *pk_key )
1783{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001784 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001785}
1786
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001787void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001788 mbedtls_x509_crt *ca_chain,
1789 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001790{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001791 conf->ca_chain = ca_chain;
1792 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001793
1794#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1795 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1796 * cannot be used together. */
1797 conf->f_ca_cb = NULL;
1798 conf->p_ca_cb = NULL;
1799#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001800}
Hanno Becker5adaad92019-03-27 16:54:37 +00001801
1802#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1803void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001804 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001805 void *p_ca_cb )
1806{
1807 conf->f_ca_cb = f_ca_cb;
1808 conf->p_ca_cb = p_ca_cb;
1809
1810 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1811 * cannot be used together. */
1812 conf->ca_chain = NULL;
1813 conf->ca_crl = NULL;
1814}
1815#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001817
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001818#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001819const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1820 size_t *name_len )
1821{
1822 *name_len = ssl->handshake->sni_name_len;
1823 return( ssl->handshake->sni_name );
1824}
1825
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001826int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1827 mbedtls_x509_crt *own_cert,
1828 mbedtls_pk_context *pk_key )
1829{
1830 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1831 own_cert, pk_key ) );
1832}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001833
1834void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1835 mbedtls_x509_crt *ca_chain,
1836 mbedtls_x509_crl *ca_crl )
1837{
1838 ssl->handshake->sni_ca_chain = ca_chain;
1839 ssl->handshake->sni_ca_crl = ca_crl;
1840}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001841
Glenn Strauss999ef702022-03-11 01:37:23 -05001842#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1843void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1844 const mbedtls_x509_crt *crt)
1845{
1846 ssl->handshake->dn_hints = crt;
1847}
1848#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1849
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001850void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1851 int authmode )
1852{
1853 ssl->handshake->sni_authmode = authmode;
1854}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001855#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1856
Hanno Becker8927c832019-04-03 12:52:50 +01001857#if defined(MBEDTLS_X509_CRT_PARSE_C)
1858void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1859 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1860 void *p_vrfy )
1861{
1862 ssl->f_vrfy = f_vrfy;
1863 ssl->p_vrfy = p_vrfy;
1864}
1865#endif
1866
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001867#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001868/*
1869 * Set EC J-PAKE password for current handshake
1870 */
Valerio Setti02c25b52022-11-15 14:08:42 +01001871#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001872int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1873 const unsigned char *pw,
1874 size_t pw_len )
1875{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001876 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1878 psa_pake_role_t psa_role;
1879 psa_status_t status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001880
Janos Follath8eb64132016-06-03 15:40:57 +01001881 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1883
Neil Armstrongca7d5062022-05-31 14:43:23 +02001884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1885 psa_role = PSA_PAKE_ROLE_SERVER;
1886 else
1887 psa_role = PSA_PAKE_ROLE_CLIENT;
1888
Valerio Settiaca21b72022-11-17 18:17:01 +01001889 /* Empty password is not valid */
1890 if( ( pw == NULL) || ( pw_len == 0 ) )
1891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001892
Valerio Settiaca21b72022-11-17 18:17:01 +01001893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1894 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1895 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001896
Valerio Settiaca21b72022-11-17 18:17:01 +01001897 status = psa_import_key( &attributes, pw, pw_len,
1898 &ssl->handshake->psa_pake_password );
1899 if( status != PSA_SUCCESS )
1900 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001901
1902 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1903 psa_pake_cs_set_primitive( &cipher_suite,
1904 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1905 PSA_ECC_FAMILY_SECP_R1,
1906 256) );
1907 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1908
1909 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1910 if( status != PSA_SUCCESS )
1911 {
1912 psa_destroy_key( ssl->handshake->psa_pake_password );
1913 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1914 }
1915
1916 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1917 if( status != PSA_SUCCESS )
1918 {
1919 psa_destroy_key( ssl->handshake->psa_pake_password );
1920 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1921 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1922 }
1923
Valerio Settiaca21b72022-11-17 18:17:01 +01001924 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1925 ssl->handshake->psa_pake_password );
1926 if( status != PSA_SUCCESS )
Neil Armstrongca7d5062022-05-31 14:43:23 +02001927 {
Valerio Settiaca21b72022-11-17 18:17:01 +01001928 psa_destroy_key( ssl->handshake->psa_pake_password );
1929 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1930 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001931 }
1932
1933 ssl->handshake->psa_pake_ctx_is_ok = 1;
1934
1935 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001936}
1937#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001938int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1939 const unsigned char *pw,
1940 size_t pw_len )
1941{
1942 mbedtls_ecjpake_role role;
1943
1944 if( ssl->handshake == NULL || ssl->conf == NULL )
1945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1946
1947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1948 role = MBEDTLS_ECJPAKE_SERVER;
1949 else
1950 role = MBEDTLS_ECJPAKE_CLIENT;
1951
1952 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1953 role,
1954 MBEDTLS_MD_SHA256,
1955 MBEDTLS_ECP_DP_SECP256R1,
1956 pw, pw_len ) );
1957}
Valerio Setti02c25b52022-11-15 14:08:42 +01001958#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001959#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001960
Ronald Cron73fe8df2022-10-05 14:31:43 +02001961#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001962int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001963{
Ronald Crond29e13e2022-10-19 10:33:48 +02001964 if( conf->psk_identity == NULL ||
1965 conf->psk_identity_len == 0 )
1966 {
1967 return( 0 );
1968 }
1969
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001970#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001971 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001972 return( 1 );
1973#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001974
1975 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001976 return( 1 );
1977
1978 return( 0 );
1979}
1980
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001981static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1982{
1983 /* Remove reference to existing PSK, if any. */
1984#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001985 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001986 {
1987 /* The maintenance of the PSK key slot is the
1988 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001989 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001990 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001991#endif /* MBEDTLS_USE_PSA_CRYPTO */
1992 if( conf->psk != NULL )
1993 {
1994 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1995
1996 mbedtls_free( conf->psk );
1997 conf->psk = NULL;
1998 conf->psk_len = 0;
1999 }
2000
2001 /* Remove reference to PSK identity, if any. */
2002 if( conf->psk_identity != NULL )
2003 {
2004 mbedtls_free( conf->psk_identity );
2005 conf->psk_identity = NULL;
2006 conf->psk_identity_len = 0;
2007 }
2008}
2009
Hanno Becker7390c712018-11-15 13:33:04 +00002010/* This function assumes that PSK identity in the SSL config is unset.
2011 * It checks that the provided identity is well-formed and attempts
2012 * to make a copy of it in the SSL config.
2013 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002014MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002015static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2016 unsigned char const *psk_identity,
2017 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002018{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002019 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002020 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002021 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002022 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002023 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002024 {
2025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2026 }
2027
Hanno Becker7390c712018-11-15 13:33:04 +00002028 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2029 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002030 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002031
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002032 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002033 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002034
2035 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002036}
2037
Hanno Becker7390c712018-11-15 13:33:04 +00002038int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2039 const unsigned char *psk, size_t psk_len,
2040 const unsigned char *psk_identity, size_t psk_identity_len )
2041{
Janos Follath865b3eb2019-12-16 11:46:15 +00002042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002043
2044 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002045 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002046 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002047
2048 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002049 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002051 if( psk_len == 0 )
2052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2053 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2055
Hanno Becker7390c712018-11-15 13:33:04 +00002056 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2057 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2058 conf->psk_len = psk_len;
2059 memcpy( conf->psk, psk, conf->psk_len );
2060
2061 /* Check and set PSK Identity */
2062 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2063 if( ret != 0 )
2064 ssl_conf_remove_psk( conf );
2065
2066 return( ret );
2067}
2068
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002069static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2070{
2071#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002072 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002073 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002074 /* The maintenance of the external PSK key slot is the
2075 * user's responsibility. */
2076 if( ssl->handshake->psk_opaque_is_internal )
2077 {
2078 psa_destroy_key( ssl->handshake->psk_opaque );
2079 ssl->handshake->psk_opaque_is_internal = 0;
2080 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002081 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002082 }
Neil Armstronge952a302022-05-03 10:22:14 +02002083#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002084 if( ssl->handshake->psk != NULL )
2085 {
2086 mbedtls_platform_zeroize( ssl->handshake->psk,
2087 ssl->handshake->psk_len );
2088 mbedtls_free( ssl->handshake->psk );
2089 ssl->handshake->psk_len = 0;
2090 }
Neil Armstronge952a302022-05-03 10:22:14 +02002091#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002092}
2093
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002094int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2095 const unsigned char *psk, size_t psk_len )
2096{
Neil Armstrong501c9322022-05-03 09:35:09 +02002097#if defined(MBEDTLS_USE_PSA_CRYPTO)
2098 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002100 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002102#endif /* MBEDTLS_USE_PSA_CRYPTO */
2103
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002104 if( psk == NULL || ssl->handshake == NULL )
2105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2106
2107 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2109
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002110 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002111
Neil Armstrong501c9322022-05-03 09:35:09 +02002112#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002113#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2114 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2115 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002116 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002117 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2118 else
2119 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2120 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2121 }
2122#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002123
Jerry Yu568ec252022-07-22 21:27:34 +08002124#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002125 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2126 {
2127 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2128 psa_set_key_usage_flags( &key_attributes,
2129 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2130 }
2131#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2132
Neil Armstrong501c9322022-05-03 09:35:09 +02002133 psa_set_key_algorithm( &key_attributes, alg );
2134 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2135
2136 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2137 if( status != PSA_SUCCESS )
2138 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2139
2140 /* Allow calling psa_destroy_key() on psk remove */
2141 ssl->handshake->psk_opaque_is_internal = 1;
2142 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2143#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002144 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002145 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002146
2147 ssl->handshake->psk_len = psk_len;
2148 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2149
2150 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002151#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002152}
2153
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002154#if defined(MBEDTLS_USE_PSA_CRYPTO)
2155int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002156 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002157 const unsigned char *psk_identity,
2158 size_t psk_identity_len )
2159{
Janos Follath865b3eb2019-12-16 11:46:15 +00002160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002161
2162 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002163 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002164 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002165
Hanno Becker7390c712018-11-15 13:33:04 +00002166 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002167 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002169 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002170
2171 /* Check and set PSK Identity */
2172 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2173 psk_identity_len );
2174 if( ret != 0 )
2175 ssl_conf_remove_psk( conf );
2176
2177 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002178}
2179
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002180int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2181 mbedtls_svc_key_id_t psk )
2182{
2183 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2184 ( ssl->handshake == NULL ) )
2185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2186
2187 ssl_remove_psk( ssl );
2188 ssl->handshake->psk_opaque = psk;
2189 return( 0 );
2190}
2191#endif /* MBEDTLS_USE_PSA_CRYPTO */
2192
Jerry Yu8897c072022-08-12 13:56:53 +08002193#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002194void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2195 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2196 size_t),
2197 void *p_psk )
2198{
2199 conf->f_psk = f_psk;
2200 conf->p_psk = p_psk;
2201}
Jerry Yu8897c072022-08-12 13:56:53 +08002202#endif /* MBEDTLS_SSL_SRV_C */
2203
Ronald Cron73fe8df2022-10-05 14:31:43 +02002204#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002205
2206#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002207static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2208 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002209{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002210#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002211 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002212 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002213#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002214 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002215 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002216 return( MBEDTLS_SSL_MODE_STREAM );
2217}
2218
2219#else /* MBEDTLS_USE_PSA_CRYPTO */
2220
2221static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2222 mbedtls_cipher_mode_t mode )
2223{
2224#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2225 if( mode == MBEDTLS_MODE_CBC )
2226 return( MBEDTLS_SSL_MODE_CBC );
2227#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2228
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002229#if defined(MBEDTLS_GCM_C) || \
2230 defined(MBEDTLS_CCM_C) || \
2231 defined(MBEDTLS_CHACHAPOLY_C)
2232 if( mode == MBEDTLS_MODE_GCM ||
2233 mode == MBEDTLS_MODE_CCM ||
2234 mode == MBEDTLS_MODE_CHACHAPOLY )
2235 return( MBEDTLS_SSL_MODE_AEAD );
2236#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002237
2238 return( MBEDTLS_SSL_MODE_STREAM );
2239}
Gilles Peskine301711e2022-04-26 16:57:05 +02002240#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002241
Gilles Peskinee108d982022-04-26 16:50:40 +02002242static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2243 mbedtls_ssl_mode_t base_mode,
2244 int encrypt_then_mac )
2245{
2246#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2247 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2248 base_mode == MBEDTLS_SSL_MODE_CBC )
2249 {
2250 return( MBEDTLS_SSL_MODE_CBC_ETM );
2251 }
2252#else
2253 (void) encrypt_then_mac;
2254#endif
2255 return( base_mode );
2256}
2257
Neil Armstrongab555e02022-04-04 11:07:59 +02002258mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002259 const mbedtls_ssl_transform *transform )
2260{
Gilles Peskinee108d982022-04-26 16:50:40 +02002261 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002262#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002263 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002264#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002265 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2266#endif
2267 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002268
Gilles Peskinee108d982022-04-26 16:50:40 +02002269 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002270#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002271 encrypt_then_mac = transform->encrypt_then_mac;
2272#endif
2273 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002274}
2275
Neil Armstrongab555e02022-04-04 11:07:59 +02002276mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002277#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002278 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002279#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002280 const mbedtls_ssl_ciphersuite_t *suite )
2281{
Gilles Peskinee108d982022-04-26 16:50:40 +02002282 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2283
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002284#if defined(MBEDTLS_USE_PSA_CRYPTO)
2285 psa_status_t status;
2286 psa_algorithm_t alg;
2287 psa_key_type_t type;
2288 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002289 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2290 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002291 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002292#else
2293 const mbedtls_cipher_info_t *cipher =
2294 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002295 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002296 {
2297 base_mode =
2298 mbedtls_ssl_get_base_mode(
2299 mbedtls_cipher_info_get_mode( cipher ) );
2300 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002301#endif /* MBEDTLS_USE_PSA_CRYPTO */
2302
Gilles Peskinee108d982022-04-26 16:50:40 +02002303#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2304 int encrypt_then_mac = 0;
2305#endif
2306 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002307}
2308
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002309#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002310
2311#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002312/* Serialization of TLS 1.3 sessions:
2313 *
2314 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002315 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002316 * uint64 ticket_received;
2317 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002318 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002319 * } ClientOnlyData;
2320 *
2321 * struct {
2322 * uint8 endpoint;
2323 * uint8 ciphersuite[2];
2324 * uint32 ticket_age_add;
2325 * uint8 ticket_flags;
2326 * opaque resumption_key<0..255>;
2327 * select ( endpoint ) {
2328 * case client: ClientOnlyData;
2329 * case server: uint64 start_time;
2330 * };
2331 * } serialized_session_tls13;
2332 *
2333 */
2334#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002335MBEDTLS_CHECK_RETURN_CRITICAL
2336static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2337 unsigned char *buf,
2338 size_t buf_len,
2339 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002340{
2341 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002342#if defined(MBEDTLS_SSL_CLI_C) && \
2343 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2344 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002345 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002346#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002347 size_t needed = 1 /* endpoint */
2348 + 2 /* ciphersuite */
2349 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002350 + 1 /* ticket_flags */
2351 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002352 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002353
2354 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2356 needed += session->resumption_key_len; /* resumption_key */
2357
Jerry Yu438ddd82022-07-07 06:55:50 +00002358#if defined(MBEDTLS_HAVE_TIME)
2359 needed += 8; /* start_time or ticket_received */
2360#endif
2361
2362#if defined(MBEDTLS_SSL_CLI_C)
2363 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2364 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002365#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002366 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002367 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002368#endif
2369
Jerry Yu438ddd82022-07-07 06:55:50 +00002370 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002371 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002372
2373 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002374 if( session->ticket_len > SIZE_MAX - needed )
2375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002376
Jerry Yue28d9742022-08-18 15:44:03 +08002377 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002378 }
2379#endif /* MBEDTLS_SSL_CLI_C */
2380
Jerry Yue36fdd62022-08-17 21:31:36 +08002381 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002382 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002383 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002384
2385 p[0] = session->endpoint;
2386 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2387 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2388 p[7] = session->ticket_flags;
2389
2390 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002391 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002392 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002393 memcpy( p, session->resumption_key, session->resumption_key_len );
2394 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002395
2396#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2397 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2398 {
2399 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2400 p += 8;
2401 }
2402#endif /* MBEDTLS_HAVE_TIME */
2403
2404#if defined(MBEDTLS_SSL_CLI_C)
2405 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2406 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002407#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2408 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2409 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002410 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002411 {
2412 /* save host name */
2413 memcpy( p, session->hostname, hostname_len );
2414 p += hostname_len;
2415 }
2416#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2417
Jerry Yu438ddd82022-07-07 06:55:50 +00002418#if defined(MBEDTLS_HAVE_TIME)
2419 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2420 p += 8;
2421#endif
2422 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2423 p += 4;
2424
2425 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2426 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002427
2428 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002429 {
2430 memcpy( p, session->ticket, session->ticket_len );
2431 p += session->ticket_len;
2432 }
2433 }
2434#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002435 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002436}
2437
2438MBEDTLS_CHECK_RETURN_CRITICAL
2439static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2440 const unsigned char *buf,
2441 size_t len )
2442{
2443 const unsigned char *p = buf;
2444 const unsigned char *end = buf + len;
2445
2446 if( end - p < 9 )
2447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2448 session->endpoint = p[0];
2449 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2450 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2451 session->ticket_flags = p[7];
2452
2453 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002454 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002455 p += 9;
2456
Jerry Yubc7c1a42022-07-21 22:57:37 +08002457 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2459
Jerry Yubc7c1a42022-07-21 22:57:37 +08002460 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002462 memcpy( session->resumption_key, p, session->resumption_key_len );
2463 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002464
2465#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2466 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2467 {
2468 if( end - p < 8 )
2469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2470 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2471 p += 8;
2472 }
2473#endif /* MBEDTLS_HAVE_TIME */
2474
2475#if defined(MBEDTLS_SSL_CLI_C)
2476 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2477 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002478#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2479 defined(MBEDTLS_SSL_SESSION_TICKETS)
2480 size_t hostname_len;
2481 /* load host name */
2482 if( end - p < 2 )
2483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2484 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2485 p += 2;
2486
2487 if( end - p < ( long int )hostname_len )
2488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2489 if( hostname_len > 0 )
2490 {
2491 session->hostname = mbedtls_calloc( 1, hostname_len );
2492 if( session->hostname == NULL )
2493 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2494 memcpy( session->hostname, p, hostname_len );
2495 p += hostname_len;
2496 }
2497#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2498 MBEDTLS_SSL_SESSION_TICKETS */
2499
Jerry Yu438ddd82022-07-07 06:55:50 +00002500#if defined(MBEDTLS_HAVE_TIME)
2501 if( end - p < 8 )
2502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2503 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2504 p += 8;
2505#endif
2506 if( end - p < 4 )
2507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2508 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2509 p += 4;
2510
2511 if( end - p < 2 )
2512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2513 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2514 p += 2;
2515
2516 if( end - p < ( long int )session->ticket_len )
2517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2518 if( session->ticket_len > 0 )
2519 {
2520 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2521 if( session->ticket == NULL )
2522 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2523 memcpy( session->ticket, p, session->ticket_len );
2524 p += session->ticket_len;
2525 }
2526 }
2527#endif /* MBEDTLS_SSL_CLI_C */
2528
2529 return( 0 );
2530
2531}
2532#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002533MBEDTLS_CHECK_RETURN_CRITICAL
2534static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2535 unsigned char *buf,
2536 size_t buf_len,
2537 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002538{
2539 ((void) session);
2540 ((void) buf);
2541 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002542 *olen = 0;
2543 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002544}
Jerry Yu438ddd82022-07-07 06:55:50 +00002545
2546static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2547 unsigned char *buf,
2548 size_t buf_len )
2549{
2550 ((void) session);
2551 ((void) buf);
2552 ((void) buf_len);
2553 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2554}
2555#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002556#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2557
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002558psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002559 size_t taglen,
2560 psa_algorithm_t *alg,
2561 psa_key_type_t *key_type,
2562 size_t *key_size )
2563{
2564 switch ( mbedtls_cipher_type )
2565 {
2566 case MBEDTLS_CIPHER_AES_128_CBC:
2567 *alg = PSA_ALG_CBC_NO_PADDING;
2568 *key_type = PSA_KEY_TYPE_AES;
2569 *key_size = 128;
2570 break;
2571 case MBEDTLS_CIPHER_AES_128_CCM:
2572 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2573 *key_type = PSA_KEY_TYPE_AES;
2574 *key_size = 128;
2575 break;
2576 case MBEDTLS_CIPHER_AES_128_GCM:
2577 *alg = PSA_ALG_GCM;
2578 *key_type = PSA_KEY_TYPE_AES;
2579 *key_size = 128;
2580 break;
2581 case MBEDTLS_CIPHER_AES_192_CCM:
2582 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2583 *key_type = PSA_KEY_TYPE_AES;
2584 *key_size = 192;
2585 break;
2586 case MBEDTLS_CIPHER_AES_192_GCM:
2587 *alg = PSA_ALG_GCM;
2588 *key_type = PSA_KEY_TYPE_AES;
2589 *key_size = 192;
2590 break;
2591 case MBEDTLS_CIPHER_AES_256_CBC:
2592 *alg = PSA_ALG_CBC_NO_PADDING;
2593 *key_type = PSA_KEY_TYPE_AES;
2594 *key_size = 256;
2595 break;
2596 case MBEDTLS_CIPHER_AES_256_CCM:
2597 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2598 *key_type = PSA_KEY_TYPE_AES;
2599 *key_size = 256;
2600 break;
2601 case MBEDTLS_CIPHER_AES_256_GCM:
2602 *alg = PSA_ALG_GCM;
2603 *key_type = PSA_KEY_TYPE_AES;
2604 *key_size = 256;
2605 break;
2606 case MBEDTLS_CIPHER_ARIA_128_CBC:
2607 *alg = PSA_ALG_CBC_NO_PADDING;
2608 *key_type = PSA_KEY_TYPE_ARIA;
2609 *key_size = 128;
2610 break;
2611 case MBEDTLS_CIPHER_ARIA_128_CCM:
2612 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2613 *key_type = PSA_KEY_TYPE_ARIA;
2614 *key_size = 128;
2615 break;
2616 case MBEDTLS_CIPHER_ARIA_128_GCM:
2617 *alg = PSA_ALG_GCM;
2618 *key_type = PSA_KEY_TYPE_ARIA;
2619 *key_size = 128;
2620 break;
2621 case MBEDTLS_CIPHER_ARIA_192_CCM:
2622 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2623 *key_type = PSA_KEY_TYPE_ARIA;
2624 *key_size = 192;
2625 break;
2626 case MBEDTLS_CIPHER_ARIA_192_GCM:
2627 *alg = PSA_ALG_GCM;
2628 *key_type = PSA_KEY_TYPE_ARIA;
2629 *key_size = 192;
2630 break;
2631 case MBEDTLS_CIPHER_ARIA_256_CBC:
2632 *alg = PSA_ALG_CBC_NO_PADDING;
2633 *key_type = PSA_KEY_TYPE_ARIA;
2634 *key_size = 256;
2635 break;
2636 case MBEDTLS_CIPHER_ARIA_256_CCM:
2637 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2638 *key_type = PSA_KEY_TYPE_ARIA;
2639 *key_size = 256;
2640 break;
2641 case MBEDTLS_CIPHER_ARIA_256_GCM:
2642 *alg = PSA_ALG_GCM;
2643 *key_type = PSA_KEY_TYPE_ARIA;
2644 *key_size = 256;
2645 break;
2646 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2647 *alg = PSA_ALG_CBC_NO_PADDING;
2648 *key_type = PSA_KEY_TYPE_CAMELLIA;
2649 *key_size = 128;
2650 break;
2651 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2652 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2653 *key_type = PSA_KEY_TYPE_CAMELLIA;
2654 *key_size = 128;
2655 break;
2656 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2657 *alg = PSA_ALG_GCM;
2658 *key_type = PSA_KEY_TYPE_CAMELLIA;
2659 *key_size = 128;
2660 break;
2661 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2662 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2663 *key_type = PSA_KEY_TYPE_CAMELLIA;
2664 *key_size = 192;
2665 break;
2666 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2667 *alg = PSA_ALG_GCM;
2668 *key_type = PSA_KEY_TYPE_CAMELLIA;
2669 *key_size = 192;
2670 break;
2671 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2672 *alg = PSA_ALG_CBC_NO_PADDING;
2673 *key_type = PSA_KEY_TYPE_CAMELLIA;
2674 *key_size = 256;
2675 break;
2676 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2677 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2678 *key_type = PSA_KEY_TYPE_CAMELLIA;
2679 *key_size = 256;
2680 break;
2681 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2682 *alg = PSA_ALG_GCM;
2683 *key_type = PSA_KEY_TYPE_CAMELLIA;
2684 *key_size = 256;
2685 break;
2686 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2687 *alg = PSA_ALG_CHACHA20_POLY1305;
2688 *key_type = PSA_KEY_TYPE_CHACHA20;
2689 *key_size = 256;
2690 break;
2691 case MBEDTLS_CIPHER_NULL:
2692 *alg = MBEDTLS_SSL_NULL_CIPHER;
2693 *key_type = 0;
2694 *key_size = 0;
2695 break;
2696 default:
2697 return PSA_ERROR_NOT_SUPPORTED;
2698 }
2699
2700 return PSA_SUCCESS;
2701}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002702#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002703
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002704#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002705int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2706 const unsigned char *dhm_P, size_t P_len,
2707 const unsigned char *dhm_G, size_t G_len )
2708{
Janos Follath865b3eb2019-12-16 11:46:15 +00002709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002710
Glenn Strausscee11292021-12-20 01:43:17 -05002711 mbedtls_mpi_free( &conf->dhm_P );
2712 mbedtls_mpi_free( &conf->dhm_G );
2713
Hanno Beckera90658f2017-10-04 15:29:08 +01002714 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2715 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2716 {
2717 mbedtls_mpi_free( &conf->dhm_P );
2718 mbedtls_mpi_free( &conf->dhm_G );
2719 return( ret );
2720 }
2721
2722 return( 0 );
2723}
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002725int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002726{
Janos Follath865b3eb2019-12-16 11:46:15 +00002727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002728
Glenn Strausscee11292021-12-20 01:43:17 -05002729 mbedtls_mpi_free( &conf->dhm_P );
2730 mbedtls_mpi_free( &conf->dhm_G );
2731
Gilles Peskinee5702482021-06-11 21:59:08 +02002732 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2733 &conf->dhm_P ) ) != 0 ||
2734 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2735 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002736 {
2737 mbedtls_mpi_free( &conf->dhm_P );
2738 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002739 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002740 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002741
2742 return( 0 );
2743}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002744#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002745
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002746#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2747/*
2748 * Set the minimum length for Diffie-Hellman parameters
2749 */
2750void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2751 unsigned int bitlen )
2752{
2753 conf->dhm_min_bitlen = bitlen;
2754}
2755#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2756
Ronald Crone68ab4f2022-10-05 12:46:29 +02002757#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002758#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002759/*
2760 * Set allowed/preferred hashes for handshake signatures
2761 */
2762void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2763 const int *hashes )
2764{
2765 conf->sig_hashes = hashes;
2766}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002767#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002768
Jerry Yuf017ee42022-01-12 15:49:48 +08002769/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002770void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2771 const uint16_t* sig_algs )
2772{
Jerry Yuf017ee42022-01-12 15:49:48 +08002773#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2774 conf->sig_hashes = NULL;
2775#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2776 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002777}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002778#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002779
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002780#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002781#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002782/*
2783 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002784 *
2785 * mbedtls_ssl_setup() takes the provided list
2786 * and translates it to a list of IANA TLS group identifiers,
2787 * stored in ssl->handshake->group_list.
2788 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002789 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002790void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002791 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002792{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002793 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002794 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002795}
Brett Warrene0edc842021-08-17 09:53:13 +01002796#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002797#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002798
Brett Warrene0edc842021-08-17 09:53:13 +01002799/*
2800 * Set the allowed groups
2801 */
2802void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2803 const uint16_t *group_list )
2804{
2805#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2806 conf->curve_list = NULL;
2807#endif
2808 conf->group_list = group_list;
2809}
2810
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002811#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002813{
Hanno Becker947194e2017-04-07 13:25:49 +01002814 /* Initialize to suppress unnecessary compiler warning */
2815 size_t hostname_len = 0;
2816
2817 /* Check if new hostname is valid before
2818 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002819 if( hostname != NULL )
2820 {
2821 hostname_len = strlen( hostname );
2822
2823 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2825 }
2826
2827 /* Now it's clear that we will overwrite the old hostname,
2828 * so we can free it safely */
2829
2830 if( ssl->hostname != NULL )
2831 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002832 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002833 mbedtls_free( ssl->hostname );
2834 }
2835
2836 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002837
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002839 {
2840 ssl->hostname = NULL;
2841 }
2842 else
2843 {
2844 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002845 if( ssl->hostname == NULL )
2846 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002847
Hanno Becker947194e2017-04-07 13:25:49 +01002848 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002849
Hanno Becker947194e2017-04-07 13:25:49 +01002850 ssl->hostname[hostname_len] = '\0';
2851 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
2853 return( 0 );
2854}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002855#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002857#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002858void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002860 const unsigned char *, size_t),
2861 void *p_sni )
2862{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002863 conf->f_sni = f_sni;
2864 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002869int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002870{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002871 size_t cur_len, tot_len;
2872 const char **p;
2873
2874 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002875 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2876 * MUST NOT be truncated."
2877 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002878 */
2879 tot_len = 0;
2880 for( p = protos; *p != NULL; p++ )
2881 {
2882 cur_len = strlen( *p );
2883 tot_len += cur_len;
2884
Ronald Cron8216dd32020-04-23 16:41:44 +02002885 if( ( cur_len == 0 ) ||
2886 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2887 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002889 }
2890
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002891 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002892
2893 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002894}
2895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002897{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002898 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002901
Johan Pascalb62bb512015-12-03 21:56:45 +01002902#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002903void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2904 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002905{
2906 conf->dtls_srtp_mki_support = support_mki_value;
2907}
2908
Ron Eldoref72faf2018-07-12 11:54:20 +03002909int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2910 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002911 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002912{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002913 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002914 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002916 }
Ron Eldor591f1622018-01-22 12:30:04 +02002917
2918 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002919 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002920 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002921 }
Ron Eldor591f1622018-01-22 12:30:04 +02002922
2923 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2924 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002925 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002926}
2927
Ron Eldoref72faf2018-07-12 11:54:20 +03002928int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002929 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002930{
Johan Pascal253d0262020-09-22 13:04:45 +02002931 const mbedtls_ssl_srtp_profile *p;
2932 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002933
Johan Pascal253d0262020-09-22 13:04:45 +02002934 /* check the profiles list: all entry must be valid,
2935 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002936 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2937 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2938 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002939 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002940 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002941 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002942 list_size++;
2943 }
2944 else
2945 {
2946 /* unsupported value, stop parsing and set the size to an error value */
2947 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002948 }
2949 }
2950
Johan Pascal5ef72d22020-10-28 17:05:47 +01002951 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002952 {
Johan Pascal253d0262020-09-22 13:04:45 +02002953 conf->dtls_srtp_profile_list = NULL;
2954 conf->dtls_srtp_profile_list_len = 0;
2955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2956 }
2957
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002958 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002959 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002960
2961 return( 0 );
2962}
2963
Johan Pascal5ef72d22020-10-28 17:05:47 +01002964void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2965 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002966{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002967 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2968 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002969 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002970 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002971 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002972 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002973 else
2974 {
2975 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002976 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2977 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002978 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002979}
Johan Pascalb62bb512015-12-03 21:56:45 +01002980#endif /* MBEDTLS_SSL_DTLS_SRTP */
2981
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302982#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002983void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002984{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002985 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002986}
2987
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002988void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002989{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002990 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002991}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302992#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002993
Janos Follath088ce432017-04-10 12:42:31 +01002994#if defined(MBEDTLS_SSL_SRV_C)
2995void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2996 char cert_req_ca_list )
2997{
2998 conf->cert_req_ca_list = cert_req_ca_list;
2999}
3000#endif
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003003void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003004{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003005 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003006}
3007#endif
3008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003010void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003011{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003012 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003013}
3014#endif
3015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003017int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003020 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003023 }
3024
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003025 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003026
3027 return( 0 );
3028}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003030
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003031void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003032{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003033 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003034}
3035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003037void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003038{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003039 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003040}
3041
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003042void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003043{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003044 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003045}
3046
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003047void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003048 const unsigned char period[8] )
3049{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003050 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003055#if defined(MBEDTLS_SSL_CLI_C)
3056void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003057{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003058 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003059}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003060#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003061
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003062#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003063
Jerry Yud0766ec2022-09-22 10:46:57 +08003064#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003065void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3066 uint16_t num_tickets )
3067{
Jerry Yud0766ec2022-09-22 10:46:57 +08003068 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003069}
3070#endif
3071
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003072void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3073 mbedtls_ssl_ticket_write_t *f_ticket_write,
3074 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3075 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003076{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003077 conf->f_ticket_write = f_ticket_write;
3078 conf->f_ticket_parse = f_ticket_parse;
3079 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003080}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003081#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003083
Hanno Becker7e6c1782021-06-08 09:24:55 +01003084void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3085 mbedtls_ssl_export_keys_t *f_export_keys,
3086 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003087{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003088 ssl->f_export_keys = f_export_keys;
3089 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003090}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003091
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003092#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003093void mbedtls_ssl_conf_async_private_cb(
3094 mbedtls_ssl_config *conf,
3095 mbedtls_ssl_async_sign_t *f_async_sign,
3096 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3097 mbedtls_ssl_async_resume_t *f_async_resume,
3098 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003099 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003100{
3101 conf->f_async_sign_start = f_async_sign;
3102 conf->f_async_decrypt_start = f_async_decrypt;
3103 conf->f_async_resume = f_async_resume;
3104 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003105 conf->p_async_config_data = async_config_data;
3106}
3107
Gilles Peskine8f97af72018-04-26 11:46:10 +02003108void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3109{
3110 return( conf->p_async_config_data );
3111}
3112
Gilles Peskine1febfef2018-04-30 11:54:39 +02003113void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003114{
3115 if( ssl->handshake == NULL )
3116 return( NULL );
3117 else
3118 return( ssl->handshake->user_async_ctx );
3119}
3120
Gilles Peskine1febfef2018-04-30 11:54:39 +02003121void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003122 void *ctx )
3123{
3124 if( ssl->handshake != NULL )
3125 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003126}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003127#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003128
Paul Bakker5121ce52009-01-03 21:22:43 +00003129/*
3130 * SSL get accessors
3131 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003132uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003133{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003134 if( ssl->session != NULL )
3135 return( ssl->session->verify_result );
3136
3137 if( ssl->session_negotiate != NULL )
3138 return( ssl->session_negotiate->verify_result );
3139
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003140 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003141}
3142
Glenn Strauss8f526902022-01-13 00:04:49 -05003143int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3144{
3145 if( ssl == NULL || ssl->session == NULL )
3146 return( 0 );
3147
3148 return( ssl->session->ciphersuite );
3149}
3150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003152{
Paul Bakker926c8e42013-03-06 10:23:34 +01003153 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003154 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003157}
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003163 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003164 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003165 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003166 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003167 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003168 default:
3169 return( "unknown (DTLS)" );
3170 }
3171 }
3172#endif
3173
Glenn Strauss60bfe602022-03-14 19:04:24 -04003174 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003175 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003176 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003177 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003178 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003179 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003180 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003181 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003182 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003183}
3184
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003185#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003186size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3187{
David Horstmann95d516f2021-05-04 18:36:56 +01003188 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003189 size_t read_mfl;
3190
3191 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3192 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3193 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3194 {
3195 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3196 }
3197
3198 /* Check if a smaller max length was negotiated */
3199 if( ssl->session_out != NULL )
3200 {
3201 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3202 if( read_mfl < max_len )
3203 {
3204 max_len = read_mfl;
3205 }
3206 }
3207
3208 // During a handshake, use the value being negotiated
3209 if( ssl->session_negotiate != NULL )
3210 {
3211 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3212 if( read_mfl < max_len )
3213 {
3214 max_len = read_mfl;
3215 }
3216 }
3217
3218 return( max_len );
3219}
3220
3221size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003222{
3223 size_t max_len;
3224
3225 /*
3226 * Assume mfl_code is correct since it was checked when set
3227 */
Angus Grattond8213d02016-05-25 20:56:48 +10003228 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003229
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003230 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003231 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003232 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003233 {
Angus Grattond8213d02016-05-25 20:56:48 +10003234 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003235 }
3236
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003237 /* During a handshake, use the value being negotiated */
3238 if( ssl->session_negotiate != NULL &&
3239 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3240 {
3241 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3242 }
3243
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003244 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003245}
3246#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3247
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003248#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003249size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003250{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003251 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3252 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3253 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3254 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3255 return ( 0 );
3256
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003257 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3258 return( ssl->mtu );
3259
3260 if( ssl->mtu == 0 )
3261 return( ssl->handshake->mtu );
3262
3263 return( ssl->mtu < ssl->handshake->mtu ?
3264 ssl->mtu : ssl->handshake->mtu );
3265}
3266#endif /* MBEDTLS_SSL_PROTO_DTLS */
3267
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003268int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3269{
3270 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3271
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003272#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3273 !defined(MBEDTLS_SSL_PROTO_DTLS)
3274 (void) ssl;
3275#endif
3276
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003277#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003278 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003279
3280 if( max_len > mfl )
3281 max_len = mfl;
3282#endif
3283
3284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003285 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003286 {
Hanno Becker89490712020-02-05 10:50:12 +00003287 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003288 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3289 const size_t overhead = (size_t) ret;
3290
3291 if( ret < 0 )
3292 return( ret );
3293
3294 if( mtu <= overhead )
3295 {
3296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3297 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3298 }
3299
3300 if( max_len > mtu - overhead )
3301 max_len = mtu - overhead;
3302 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003303#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003304
Hanno Becker0defedb2018-08-10 12:35:02 +01003305#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3306 !defined(MBEDTLS_SSL_PROTO_DTLS)
3307 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003308#endif
3309
3310 return( (int) max_len );
3311}
3312
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003313int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3314{
3315 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3316
3317#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3318 (void) ssl;
3319#endif
3320
3321#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3322 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3323
3324 if( max_len > mfl )
3325 max_len = mfl;
3326#endif
3327
3328 return( (int) max_len );
3329}
3330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#if defined(MBEDTLS_X509_CRT_PARSE_C)
3332const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003333{
3334 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003335 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003336
Hanno Beckere6824572019-02-07 13:18:46 +00003337#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003338 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003339#else
3340 return( NULL );
3341#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003346int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3347 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003348{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003349 int ret;
3350
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003351 if( ssl == NULL ||
3352 dst == NULL ||
3353 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003354 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003357 }
3358
Hanno Beckere810bbc2021-05-14 16:01:05 +01003359 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3360 * idempotent: Each session can only be exported once.
3361 *
3362 * (This is in preparation for TLS 1.3 support where we will
3363 * need the ability to export multiple sessions (aka tickets),
3364 * which will be achieved by calling mbedtls_ssl_get_session()
3365 * multiple times until it fails.)
3366 *
3367 * Check whether we have already exported the current session,
3368 * and fail if so.
3369 */
3370 if( ssl->session->exported == 1 )
3371 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3372
3373 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3374 if( ret != 0 )
3375 return( ret );
3376
3377 /* Remember that we've exported the session. */
3378 ssl->session->exported = 1;
3379 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003382
Paul Bakker5121ce52009-01-03 21:22:43 +00003383/*
Hanno Beckera835da52019-05-16 12:39:07 +01003384 * Define ticket header determining Mbed TLS version
3385 * and structure of the ticket.
3386 */
3387
Hanno Becker94ef3b32019-05-16 12:50:45 +01003388/*
Hanno Becker50b59662019-05-28 14:30:45 +01003389 * Define bitflag determining compile-time settings influencing
3390 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003391 */
3392
Hanno Becker50b59662019-05-28 14:30:45 +01003393#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003394#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003395#else
Hanno Becker3e088662019-05-29 11:10:18 +01003396#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003397#endif /* MBEDTLS_HAVE_TIME */
3398
3399#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003400#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003401#else
Hanno Becker3e088662019-05-29 11:10:18 +01003402#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003403#endif /* MBEDTLS_X509_CRT_PARSE_C */
3404
3405#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003406#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003407#else
Hanno Becker3e088662019-05-29 11:10:18 +01003408#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003409#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3410
3411#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003412#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003413#else
Hanno Becker3e088662019-05-29 11:10:18 +01003414#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003415#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3416
Hanno Becker94ef3b32019-05-16 12:50:45 +01003417#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003418#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003419#else
Hanno Becker3e088662019-05-29 11:10:18 +01003420#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003421#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3422
Hanno Becker94ef3b32019-05-16 12:50:45 +01003423#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3424#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3425#else
3426#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3427#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3428
Hanno Becker3e088662019-05-29 11:10:18 +01003429#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3430#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3431#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3432#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003433#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3434#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003435
Hanno Becker50b59662019-05-28 14:30:45 +01003436#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003437 ( (uint16_t) ( \
3438 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3439 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3440 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3441 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003442 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003443 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003444
Hanno Beckerf8787072019-05-16 12:41:07 +01003445static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003446 MBEDTLS_VERSION_MAJOR,
3447 MBEDTLS_VERSION_MINOR,
3448 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003449 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3450 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003451};
Hanno Beckera835da52019-05-16 12:39:07 +01003452
3453/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003454 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003455 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003456 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003457 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003458 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003459 * opaque mbedtls_version[3]; // library version: major, minor, patch
3460 * opaque session_format[2]; // library-version specific 16-bit field
3461 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003462 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003463 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003464 * Note: When updating the format, remember to keep
3465 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003466 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003467 * // In this version, `session_format` determines
3468 * // the setting of those compile-time
3469 * // configuration options which influence
3470 * // the structure of mbedtls_ssl_session.
3471 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003472 * uint8_t minor_ver; // Protocol minor version. Possible values:
3473 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003474 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003475 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003476 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003477 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003478 * serialized_session_tls12 data;
3479 *
3480 * };
3481 *
3482 * } serialized_session;
3483 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003484 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003485
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003486MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003487static int ssl_session_save( const mbedtls_ssl_session *session,
3488 unsigned char omit_header,
3489 unsigned char *buf,
3490 size_t buf_len,
3491 size_t *olen )
3492{
3493 unsigned char *p = buf;
3494 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003495 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003496#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3497 size_t out_len;
3498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3499#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003500 if( session == NULL )
3501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3502
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003503 if( !omit_header )
3504 {
3505 /*
3506 * Add Mbed TLS version identifier
3507 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003508 used += sizeof( ssl_serialized_session_header );
3509
3510 if( used <= buf_len )
3511 {
3512 memcpy( p, ssl_serialized_session_header,
3513 sizeof( ssl_serialized_session_header ) );
3514 p += sizeof( ssl_serialized_session_header );
3515 }
3516 }
3517
3518 /*
3519 * TLS version identifier
3520 */
3521 used += 1;
3522 if( used <= buf_len )
3523 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003524 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003525 }
3526
3527 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003528 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003529 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003530 {
3531#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003532 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003533 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003534 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003535#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3536
Jerry Yu251a12e2022-07-13 15:15:48 +08003537#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3538 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003539 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3540 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003541 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003542 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003543 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003544#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3545
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003546 default:
3547 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3548 }
3549
3550 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003551 if( used > buf_len )
3552 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003553
3554 return( 0 );
3555}
3556
3557/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003558 * Public wrapper for ssl_session_save()
3559 */
3560int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3561 unsigned char *buf,
3562 size_t buf_len,
3563 size_t *olen )
3564{
3565 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3566}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003567
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003568/*
3569 * Deserialize session, see mbedtls_ssl_session_save() for format.
3570 *
3571 * This internal version is wrapped by a public function that cleans up in
3572 * case of error, and has an extra option omit_header.
3573 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003574MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003575static int ssl_session_load( mbedtls_ssl_session *session,
3576 unsigned char omit_header,
3577 const unsigned char *buf,
3578 size_t len )
3579{
3580 const unsigned char *p = buf;
3581 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003582 size_t remaining_len;
3583
3584
3585 if( session == NULL )
3586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003587
3588 if( !omit_header )
3589 {
3590 /*
3591 * Check Mbed TLS version identifier
3592 */
3593
3594 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3596
3597 if( memcmp( p, ssl_serialized_session_header,
3598 sizeof( ssl_serialized_session_header ) ) != 0 )
3599 {
3600 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3601 }
3602 p += sizeof( ssl_serialized_session_header );
3603 }
3604
3605 /*
3606 * TLS version identifier
3607 */
3608 if( 1 > (size_t)( end - p ) )
3609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003610 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003611
3612 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003613 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003614 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003615 {
3616#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003617 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003618 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003619#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3620
Jerry Yu438ddd82022-07-07 06:55:50 +00003621#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3622 case MBEDTLS_SSL_VERSION_TLS1_3:
3623 return( ssl_tls13_session_load( session, p, remaining_len ) );
3624#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3625
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626 default:
3627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3628 }
3629}
3630
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003631/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003632 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003633 */
3634int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3635 const unsigned char *buf,
3636 size_t len )
3637{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003638 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003639
3640 if( ret != 0 )
3641 mbedtls_ssl_session_free( session );
3642
3643 return( ret );
3644}
3645
3646/*
Paul Bakker1961b702013-01-25 14:49:24 +01003647 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003649MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003650static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3651{
3652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3653
Ronald Cron66dbf912022-02-02 15:33:46 +01003654 /*
3655 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003656 * that were written into the output buffer by the previous handshake step,
3657 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003658 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3659 * We proceed to the next handshake step only when all data from the
3660 * previous one have been sent to the peer, thus we make sure that this is
3661 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3662 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3663 * we have to wait before to go ahead.
3664 * In the case of TLS 1.3, handshake step handlers do not send data to the
3665 * peer. Data are only sent here and through
3666 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003667 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003668 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003669 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3670 return( ret );
3671
3672#if defined(MBEDTLS_SSL_PROTO_DTLS)
3673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3674 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3675 {
3676 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3677 return( ret );
3678 }
3679#endif /* MBEDTLS_SSL_PROTO_DTLS */
3680
3681 return( ret );
3682}
3683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003685{
Hanno Becker41934dd2021-08-07 19:13:43 +01003686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Hanno Becker41934dd2021-08-07 19:13:43 +01003688 if( ssl == NULL ||
3689 ssl->conf == NULL ||
3690 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003691 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003692 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003694 }
3695
3696 ret = ssl_prepare_handshake_step( ssl );
3697 if( ret != 0 )
3698 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003699
Jerry Yue7047812021-09-13 19:26:39 +08003700 ret = mbedtls_ssl_handle_pending_alert( ssl );
3701 if( ret != 0 )
3702 goto cleanup;
3703
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003704 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3705 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3706 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003709 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003710 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3712 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003713
Ronald Cron9f0fba32022-02-10 16:45:15 +01003714 switch( ssl->state )
3715 {
3716 case MBEDTLS_SSL_HELLO_REQUEST:
3717 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003718 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003719 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003720
Ronald Cron9f0fba32022-02-10 16:45:15 +01003721 case MBEDTLS_SSL_CLIENT_HELLO:
3722 ret = mbedtls_ssl_write_client_hello( ssl );
3723 break;
3724
3725 default:
3726#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003727 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003728 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3729 else
3730 ret = mbedtls_ssl_handshake_client_step( ssl );
3731#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3732 ret = mbedtls_ssl_handshake_client_step( ssl );
3733#else
3734 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3735#endif
3736 }
Jerry Yub9930e72021-08-06 17:11:51 +08003737 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003740 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003741 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003742#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003743 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003744 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003745#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003746
3747#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3748 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3749 ret = mbedtls_ssl_handshake_server_step( ssl );
3750#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003752#endif
3753
Jerry Yue7047812021-09-13 19:26:39 +08003754 if( ret != 0 )
3755 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003756 /* handshake_step return error. And it is same
3757 * with alert_reason.
3758 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003759 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003760 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003761 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003762 goto cleanup;
3763 }
3764 }
3765
3766cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003767 return( ret );
3768}
3769
3770/*
3771 * Perform the SSL handshake
3772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003774{
3775 int ret = 0;
3776
Hanno Beckera817ea42020-10-20 15:20:23 +01003777 /* Sanity checks */
3778
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003779 if( ssl == NULL || ssl->conf == NULL )
3780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3781
Hanno Beckera817ea42020-10-20 15:20:23 +01003782#if defined(MBEDTLS_SSL_PROTO_DTLS)
3783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3784 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3785 {
3786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3787 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3789 }
3790#endif /* MBEDTLS_SSL_PROTO_DTLS */
3791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003793
Hanno Beckera817ea42020-10-20 15:20:23 +01003794 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003795 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003798
3799 if( ret != 0 )
3800 break;
3801 }
3802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003804
3805 return( ret );
3806}
3807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808#if defined(MBEDTLS_SSL_RENEGOTIATION)
3809#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003810/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003811 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003812 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003813MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003815{
Janos Follath865b3eb2019-12-16 11:46:15 +00003816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003819
3820 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3822 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003823
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003824 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003825 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003827 return( ret );
3828 }
3829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003831
3832 return( 0 );
3833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003835
3836/*
3837 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 * - any side: calling mbedtls_ssl_renegotiate(),
3839 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3840 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003841 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003842 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003844 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003845int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003846{
Janos Follath865b3eb2019-12-16 11:46:15 +00003847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003850
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003851 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3852 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003853
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003854 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3855 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003859 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003860 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003861 ssl->handshake->out_msg_seq = 1;
3862 else
3863 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003864 }
3865#endif
3866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3868 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003873 return( ret );
3874 }
3875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003877
3878 return( 0 );
3879}
3880
3881/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003882 * Renegotiate current connection on client,
3883 * or request renegotiation on server
3884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003886{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003888
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003889 if( ssl == NULL || ssl->conf == NULL )
3890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003893 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003894 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003895 {
Jerry Yu6848a612022-10-27 13:03:26 +08003896 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003900
3901 /* Did we already try/start sending HelloRequest? */
3902 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003904
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003905 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003906 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003910 /*
3911 * On client, either start the renegotiation process or,
3912 * if already in progress, continue the handshake
3913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003915 {
Jerry Yu6848a612022-10-27 13:03:26 +08003916 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003918
Hanno Becker40cdaa12020-02-05 10:48:27 +00003919 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003920 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003922 return( ret );
3923 }
3924 }
3925 else
3926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003930 return( ret );
3931 }
3932 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003934
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003935 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003938
Gilles Peskine9b562d52018-04-25 20:32:43 +02003939void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003940{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003941 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3942
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003943 if( handshake == NULL )
3944 return;
3945
Brett Warrene0edc842021-08-17 09:53:13 +01003946#if defined(MBEDTLS_ECP_C)
3947#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3948 if ( ssl->handshake->group_list_heap_allocated )
3949 mbedtls_free( (void*) handshake->group_list );
3950 handshake->group_list = NULL;
3951#endif /* MBEDTLS_DEPRECATED_REMOVED */
3952#endif /* MBEDTLS_ECP_C */
3953
Ronald Crone68ab4f2022-10-05 12:46:29 +02003954#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003955#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3956 if ( ssl->handshake->sig_algs_heap_allocated )
3957 mbedtls_free( (void*) handshake->sig_algs );
3958 handshake->sig_algs = NULL;
3959#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003960#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3961 if( ssl->handshake->certificate_request_context )
3962 {
3963 mbedtls_free( (void*) handshake->certificate_request_context );
3964 }
3965#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003966#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003967
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003968#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3969 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3970 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003971 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003972 handshake->async_in_progress = 0;
3973 }
3974#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3975
Andrzej Kurek25f27152022-08-17 16:09:31 -04003976#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003977#if defined(MBEDTLS_USE_PSA_CRYPTO)
3978 psa_hash_abort( &handshake->fin_sha256_psa );
3979#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003980 mbedtls_sha256_free( &handshake->fin_sha256 );
3981#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003982#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003983#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003984#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003985 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003986#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003987 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003988#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003989#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991#if defined(MBEDTLS_DHM_C)
3992 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003993#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003994#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003996#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01003997
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003998#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02003999#if defined(MBEDTLS_USE_PSA_CRYPTO)
4000 psa_pake_abort( &handshake->psa_pake_ctx );
4001 psa_destroy_key( handshake->psa_pake_password );
4002 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4003#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004004 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004005#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004006#if defined(MBEDTLS_SSL_CLI_C)
4007 mbedtls_free( handshake->ecjpake_cache );
4008 handshake->ecjpake_cache = NULL;
4009 handshake->ecjpake_cache_len = 0;
4010#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004011#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004012
Janos Follath4ae5c292016-02-10 11:27:43 +00004013#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4014 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004015 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004017#endif
4018
Ronald Cron73fe8df2022-10-05 14:31:43 +02004019#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004020#if defined(MBEDTLS_USE_PSA_CRYPTO)
4021 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4022 {
4023 /* The maintenance of the external PSK key slot is the
4024 * user's responsibility. */
4025 if( ssl->handshake->psk_opaque_is_internal )
4026 {
4027 psa_destroy_key( ssl->handshake->psk_opaque );
4028 ssl->handshake->psk_opaque_is_internal = 0;
4029 }
4030 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4031 }
Neil Armstronge952a302022-05-03 10:22:14 +02004032#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004033 if( handshake->psk != NULL )
4034 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004035 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004036 mbedtls_free( handshake->psk );
4037 }
Neil Armstronge952a302022-05-03 10:22:14 +02004038#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004039#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4042 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004043 /*
4044 * Free only the linked list wrapper, not the keys themselves
4045 * since the belong to the SNI callback
4046 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004047 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004049
Gilles Peskineeccd8882020-03-10 12:19:08 +01004050#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004051 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004052 if( handshake->ecrs_peer_cert != NULL )
4053 {
4054 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4055 mbedtls_free( handshake->ecrs_peer_cert );
4056 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004057#endif
4058
Hanno Becker75173122019-02-06 16:18:31 +00004059#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4060 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4061 mbedtls_pk_free( &handshake->peer_pubkey );
4062#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4063
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004064#if defined(MBEDTLS_SSL_CLI_C) && \
4065 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4066 mbedtls_free( handshake->cookie );
4067#endif /* MBEDTLS_SSL_CLI_C &&
4068 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004069
4070#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004071 mbedtls_ssl_flight_free( handshake->flight );
4072 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004073#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004074
Ronald Cronf12b81d2022-03-15 10:42:41 +01004075#if defined(MBEDTLS_ECDH_C) && \
4076 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004077 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004078 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004079#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4080
Ronald Cron6f135e12021-12-08 16:57:54 +01004081#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004082 mbedtls_ssl_transform_free( handshake->transform_handshake );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004083 mbedtls_free( handshake->transform_handshake );
4084#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yua1a568c2021-11-09 10:17:21 +08004085 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004086 mbedtls_free( handshake->transform_earlydata );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004087#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004088#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004089
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004090
4091#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4092 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4093 * processes datagrams and the fact that a datagram is allowed to have
4094 * several records in it, it is possible that the I/O buffers are not
4095 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004096 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4097 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004098#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004099
Jerry Yuba9c7272021-10-30 11:54:10 +08004100 /* mbedtls_platform_zeroize MUST be last one in this function */
4101 mbedtls_platform_zeroize( handshake,
4102 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004103}
4104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004106{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004107 if( session == NULL )
4108 return;
4109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004111 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004112#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004113
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004114#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004115#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4116 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004117 mbedtls_free( session->hostname );
4118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004120#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004121
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004122 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004123}
4124
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004125#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004126
4127#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4128#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4129#else
4130#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4131#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4132
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004133#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004134
4135#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4136#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4137#else
4138#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4139#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4140
4141#if defined(MBEDTLS_SSL_ALPN)
4142#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4143#else
4144#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4145#endif /* MBEDTLS_SSL_ALPN */
4146
4147#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4148#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4149#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4150#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4151
4152#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4153 ( (uint32_t) ( \
4154 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4155 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4156 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4157 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4158 0u ) )
4159
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004160static unsigned char ssl_serialized_context_header[] = {
4161 MBEDTLS_VERSION_MAJOR,
4162 MBEDTLS_VERSION_MINOR,
4163 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004164 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4165 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4166 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4167 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4168 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004169};
4170
Paul Bakker5121ce52009-01-03 21:22:43 +00004171/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004172 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004173 *
4174 * The format of the serialized data is:
4175 * (in the presentation language of TLS, RFC 8446 section 3)
4176 *
4177 * // header
4178 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004179 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004180 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004181 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004182 * Note: When updating the format, remember to keep these
4183 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004184 *
4185 * // session sub-structure
4186 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4187 * // transform sub-structure
4188 * uint8 random[64]; // ServerHello.random+ClientHello.random
4189 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4190 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4191 * // fields from ssl_context
4192 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4193 * uint64 in_window_top; // DTLS: last validated record seq_num
4194 * uint64 in_window; // DTLS: bitmask for replay protection
4195 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4196 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4197 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4198 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4199 *
4200 * Note that many fields of the ssl_context or sub-structures are not
4201 * serialized, as they fall in one of the following categories:
4202 *
4203 * 1. forced value (eg in_left must be 0)
4204 * 2. pointer to dynamically-allocated memory (eg session, transform)
4205 * 3. value can be re-derived from other data (eg session keys from MS)
4206 * 4. value was temporary (eg content of input buffer)
4207 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004208 */
4209int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4210 unsigned char *buf,
4211 size_t buf_len,
4212 size_t *olen )
4213{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004214 unsigned char *p = buf;
4215 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004216 size_t session_len;
4217 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004218
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004219 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004220 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4221 * this function's documentation.
4222 *
4223 * These are due to assumptions/limitations in the implementation. Some of
4224 * them are likely to stay (no handshake in progress) some might go away
4225 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004226 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004227 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004228 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004229 {
4230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004232 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004233 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004234 {
4235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004237 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004238 /* Double-check that sub-structures are indeed ready */
4239 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004240 {
4241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004243 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004244 /* There must be no pending incoming or outgoing data */
4245 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004246 {
4247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004248 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004249 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004250 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004251 {
4252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004254 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004255 /* Protocol must be DLTS, not TLS */
4256 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004257 {
4258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004260 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004261 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004262 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004263 {
4264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004266 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004267 /* We must be using an AEAD ciphersuite */
4268 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004269 {
4270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004272 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004273 /* Renegotiation must not be enabled */
4274#if defined(MBEDTLS_SSL_RENEGOTIATION)
4275 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004276 {
4277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004279 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004280#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004281
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004282 /*
4283 * Version and format identifier
4284 */
4285 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004286
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004287 if( used <= buf_len )
4288 {
4289 memcpy( p, ssl_serialized_context_header,
4290 sizeof( ssl_serialized_context_header ) );
4291 p += sizeof( ssl_serialized_context_header );
4292 }
4293
4294 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004295 * Session (length + data)
4296 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004297 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004298 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4299 return( ret );
4300
4301 used += 4 + session_len;
4302 if( used <= buf_len )
4303 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004304 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4305 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004306
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004307 ret = ssl_session_save( ssl->session, 1,
4308 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004309 if( ret != 0 )
4310 return( ret );
4311
4312 p += session_len;
4313 }
4314
4315 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004316 * Transform
4317 */
4318 used += sizeof( ssl->transform->randbytes );
4319 if( used <= buf_len )
4320 {
4321 memcpy( p, ssl->transform->randbytes,
4322 sizeof( ssl->transform->randbytes ) );
4323 p += sizeof( ssl->transform->randbytes );
4324 }
4325
4326#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4327 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4328 if( used <= buf_len )
4329 {
4330 *p++ = ssl->transform->in_cid_len;
4331 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4332 p += ssl->transform->in_cid_len;
4333
4334 *p++ = ssl->transform->out_cid_len;
4335 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4336 p += ssl->transform->out_cid_len;
4337 }
4338#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4339
4340 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004341 * Saved fields from top-level ssl_context structure
4342 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004343 used += 4;
4344 if( used <= buf_len )
4345 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004346 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4347 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004348 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004349
4350#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4351 used += 16;
4352 if( used <= buf_len )
4353 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004354 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4355 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004356
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004357 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4358 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004359 }
4360#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4361
4362#if defined(MBEDTLS_SSL_PROTO_DTLS)
4363 used += 1;
4364 if( used <= buf_len )
4365 {
4366 *p++ = ssl->disable_datagram_packing;
4367 }
4368#endif /* MBEDTLS_SSL_PROTO_DTLS */
4369
Jerry Yuae0b2e22021-10-08 15:21:19 +08004370 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004371 if( used <= buf_len )
4372 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004373 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4374 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004375 }
4376
4377#if defined(MBEDTLS_SSL_PROTO_DTLS)
4378 used += 2;
4379 if( used <= buf_len )
4380 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004381 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4382 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004383 }
4384#endif /* MBEDTLS_SSL_PROTO_DTLS */
4385
4386#if defined(MBEDTLS_SSL_ALPN)
4387 {
4388 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004389 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004390 : 0;
4391
4392 used += 1 + alpn_len;
4393 if( used <= buf_len )
4394 {
4395 *p++ = alpn_len;
4396
4397 if( ssl->alpn_chosen != NULL )
4398 {
4399 memcpy( p, ssl->alpn_chosen, alpn_len );
4400 p += alpn_len;
4401 }
4402 }
4403 }
4404#endif /* MBEDTLS_SSL_ALPN */
4405
4406 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004407 * Done
4408 */
4409 *olen = used;
4410
4411 if( used > buf_len )
4412 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004413
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004414 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4415
Hanno Becker43aefe22020-02-05 10:44:56 +00004416 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004417}
4418
4419/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004420 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004421 *
4422 * This internal version is wrapped by a public function that cleans up in
4423 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004424 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004425MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004426static int ssl_context_load( mbedtls_ssl_context *ssl,
4427 const unsigned char *buf,
4428 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004429{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004430 const unsigned char *p = buf;
4431 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004432 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004435 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004436#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004437
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004438 /*
4439 * The context should have been freshly setup or reset.
4440 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004441 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004442 * renegotiating, or if the user mistakenly loaded a session first.)
4443 */
4444 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4445 ssl->session != NULL )
4446 {
4447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4448 }
4449
4450 /*
4451 * We can't check that the config matches the initial one, but we can at
4452 * least check it matches the requirements for serializing.
4453 */
4454 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004455 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4456 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004457#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004458 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004459#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004460 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004461 {
4462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4463 }
4464
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004465 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4466
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004467 /*
4468 * Check version identifier
4469 */
4470 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4472
4473 if( memcmp( p, ssl_serialized_context_header,
4474 sizeof( ssl_serialized_context_header ) ) != 0 )
4475 {
4476 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4477 }
4478 p += sizeof( ssl_serialized_context_header );
4479
4480 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004481 * Session
4482 */
4483 if( (size_t)( end - p ) < 4 )
4484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4485
4486 session_len = ( (size_t) p[0] << 24 ) |
4487 ( (size_t) p[1] << 16 ) |
4488 ( (size_t) p[2] << 8 ) |
4489 ( (size_t) p[3] );
4490 p += 4;
4491
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004492 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004493 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004494 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004495 ssl->session_in = ssl->session;
4496 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004497 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004498
4499 if( (size_t)( end - p ) < session_len )
4500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4501
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004502 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004503 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004504 {
4505 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004506 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004507 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004508
4509 p += session_len;
4510
4511 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004512 * Transform
4513 */
4514
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004515 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004516 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004517 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004518 ssl->transform_in = ssl->transform;
4519 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004520 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004521
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004522#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004523 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4524 if( prf_func == NULL )
4525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4526
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004527 /* Read random bytes and populate structure */
4528 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004530
Hanno Beckerbd257552021-03-22 06:59:27 +00004531 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004532 ssl->session->ciphersuite,
4533 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004534#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004535 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004536#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004537 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004538 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004539 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004540 ssl->conf->endpoint,
4541 ssl );
4542 if( ret != 0 )
4543 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004544#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004545 p += sizeof( ssl->transform->randbytes );
4546
4547#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4548 /* Read connection IDs and store them */
4549 if( (size_t)( end - p ) < 1 )
4550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4551
4552 ssl->transform->in_cid_len = *p++;
4553
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004554 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4556
4557 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4558 p += ssl->transform->in_cid_len;
4559
4560 ssl->transform->out_cid_len = *p++;
4561
4562 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4564
4565 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4566 p += ssl->transform->out_cid_len;
4567#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4568
4569 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004570 * Saved fields from top-level ssl_context structure
4571 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004572 if( (size_t)( end - p ) < 4 )
4573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4574
4575 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4576 ( (uint32_t) p[1] << 16 ) |
4577 ( (uint32_t) p[2] << 8 ) |
4578 ( (uint32_t) p[3] );
4579 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004580
4581#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4582 if( (size_t)( end - p ) < 16 )
4583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4584
4585 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4586 ( (uint64_t) p[1] << 48 ) |
4587 ( (uint64_t) p[2] << 40 ) |
4588 ( (uint64_t) p[3] << 32 ) |
4589 ( (uint64_t) p[4] << 24 ) |
4590 ( (uint64_t) p[5] << 16 ) |
4591 ( (uint64_t) p[6] << 8 ) |
4592 ( (uint64_t) p[7] );
4593 p += 8;
4594
4595 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4596 ( (uint64_t) p[1] << 48 ) |
4597 ( (uint64_t) p[2] << 40 ) |
4598 ( (uint64_t) p[3] << 32 ) |
4599 ( (uint64_t) p[4] << 24 ) |
4600 ( (uint64_t) p[5] << 16 ) |
4601 ( (uint64_t) p[6] << 8 ) |
4602 ( (uint64_t) p[7] );
4603 p += 8;
4604#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4605
4606#if defined(MBEDTLS_SSL_PROTO_DTLS)
4607 if( (size_t)( end - p ) < 1 )
4608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4609
4610 ssl->disable_datagram_packing = *p++;
4611#endif /* MBEDTLS_SSL_PROTO_DTLS */
4612
Jerry Yud9a94fe2021-09-28 18:58:59 +08004613 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004615 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4616 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004617
4618#if defined(MBEDTLS_SSL_PROTO_DTLS)
4619 if( (size_t)( end - p ) < 2 )
4620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4621
4622 ssl->mtu = ( p[0] << 8 ) | p[1];
4623 p += 2;
4624#endif /* MBEDTLS_SSL_PROTO_DTLS */
4625
4626#if defined(MBEDTLS_SSL_ALPN)
4627 {
4628 uint8_t alpn_len;
4629 const char **cur;
4630
4631 if( (size_t)( end - p ) < 1 )
4632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4633
4634 alpn_len = *p++;
4635
4636 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4637 {
4638 /* alpn_chosen should point to an item in the configured list */
4639 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4640 {
4641 if( strlen( *cur ) == alpn_len &&
4642 memcmp( p, cur, alpn_len ) == 0 )
4643 {
4644 ssl->alpn_chosen = *cur;
4645 break;
4646 }
4647 }
4648 }
4649
4650 /* can only happen on conf mismatch */
4651 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4653
4654 p += alpn_len;
4655 }
4656#endif /* MBEDTLS_SSL_ALPN */
4657
4658 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004659 * Forced fields from top-level ssl_context structure
4660 *
4661 * Most of them already set to the correct value by mbedtls_ssl_init() and
4662 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4663 */
4664 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004665 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004666
Hanno Becker361b10d2019-08-30 10:42:49 +01004667 /* Adjust pointers for header fields of outgoing records to
4668 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004669 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004670
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004671#if defined(MBEDTLS_SSL_PROTO_DTLS)
4672 ssl->in_epoch = 1;
4673#endif
4674
4675 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4676 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004677 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4678 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004679 if( ssl->handshake != NULL )
4680 {
4681 mbedtls_ssl_handshake_free( ssl );
4682 mbedtls_free( ssl->handshake );
4683 ssl->handshake = NULL;
4684 }
4685
4686 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004687 * Done - should have consumed entire buffer
4688 */
4689 if( p != end )
4690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004691
4692 return( 0 );
4693}
4694
4695/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004696 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004697 */
4698int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4699 const unsigned char *buf,
4700 size_t len )
4701{
4702 int ret = ssl_context_load( context, buf, len );
4703
4704 if( ret != 0 )
4705 mbedtls_ssl_free( context );
4706
4707 return( ret );
4708}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004709#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004710
4711/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004712 * Free an SSL context
4713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004715{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004716 if( ssl == NULL )
4717 return;
4718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004720
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004721 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 {
sander-visserb8aa2072020-05-06 22:05:13 +02004723#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4724 size_t out_buf_len = ssl->out_buf_len;
4725#else
4726 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4727#endif
4728
Darryl Greenb33cc762019-11-28 14:29:44 +00004729 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004731 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004732 }
4733
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004734 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 {
sander-visserb8aa2072020-05-06 22:05:13 +02004736#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4737 size_t in_buf_len = ssl->in_buf_len;
4738#else
4739 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4740#endif
4741
Darryl Greenb33cc762019-11-28 14:29:44 +00004742 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004744 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004745 }
4746
Paul Bakker48916f92012-09-16 19:57:18 +00004747 if( ssl->transform )
4748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 mbedtls_ssl_transform_free( ssl->transform );
4750 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004751 }
4752
4753 if( ssl->handshake )
4754 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004755 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4757 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 mbedtls_free( ssl->handshake );
4760 mbedtls_free( ssl->transform_negotiate );
4761 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004762 }
4763
Ronald Cron6f135e12021-12-08 16:57:54 +01004764#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004765 mbedtls_ssl_transform_free( ssl->transform_application );
4766 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004767#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004768
Paul Bakkerc0463502013-02-14 11:19:38 +01004769 if( ssl->session )
4770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 mbedtls_ssl_session_free( ssl->session );
4772 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004773 }
4774
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004775#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004776 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004778 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004781#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004782
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004783#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004785#endif
4786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004788
Paul Bakker86f04f42013-02-14 11:20:09 +01004789 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004790 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004791}
4792
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004793/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004794 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004795 */
4796void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4797{
4798 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4799}
4800
Gilles Peskineae270bf2021-06-02 00:05:29 +02004801/* The selection should be the same as mbedtls_x509_crt_profile_default in
4802 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004803 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004804 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004805 * about this list.
4806 */
Brett Warrene0edc842021-08-17 09:53:13 +01004807static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004808#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004809 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004810#endif
4811#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004812 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004813#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004814#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004815 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004816#endif
4817#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004818 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004819#endif
4820#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004821 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004822#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004823#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004824 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004825#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004826#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004827 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004828#endif
4829#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004830 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004831#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004832 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004833};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004834
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004835static int ssl_preset_suiteb_ciphersuites[] = {
4836 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4837 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4838 0
4839};
4840
Ronald Crone68ab4f2022-10-05 12:46:29 +02004841#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004842
Jerry Yu909df7b2022-01-22 11:56:27 +08004843/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004844 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004845 * rules SHOULD be upheld.
4846 * - No duplicate entries.
4847 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004848 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004849 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004850 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004851static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004852
Andrzej Kurek25f27152022-08-17 16:09:31 -04004853#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 +08004854 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4855 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004856#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004857 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004858
Andrzej Kurek25f27152022-08-17 16:09:31 -04004859#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 +08004860 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4861 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004862#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004863 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4864
Andrzej Kurek25f27152022-08-17 16:09:31 -04004865#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 +08004866 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4867 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004868#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004869 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004870
Andrzej Kurek25f27152022-08-17 16:09:31 -04004871#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 +08004872 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004873#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 +08004874
Andrzej Kurek25f27152022-08-17 16:09:31 -04004875#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 +08004876 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004877#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 +08004878
Andrzej Kurek25f27152022-08-17 16:09:31 -04004879#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 +08004880 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004881#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 +08004882
Andrzej Kurek25f27152022-08-17 16:09:31 -04004883#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 +08004884 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4885#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4886
Andrzej Kurek25f27152022-08-17 16:09:31 -04004887#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 +08004888 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4889#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4890
Andrzej Kurek25f27152022-08-17 16:09:31 -04004891#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 +08004892 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4893#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4894
Gabor Mezei15b95a62022-05-09 16:37:58 +02004895 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004896};
4897
4898/* NOTICE: see above */
4899#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4900static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004901#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004902#if defined(MBEDTLS_ECDSA_C)
4903 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004904#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004905#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4906 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4907#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004908#if defined(MBEDTLS_RSA_C)
4909 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4910#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004911#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004912#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004913#if defined(MBEDTLS_ECDSA_C)
4914 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004915#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004916#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4917 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4918#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004919#if defined(MBEDTLS_RSA_C)
4920 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4921#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004922#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004923#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004924#if defined(MBEDTLS_ECDSA_C)
4925 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004926#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004927#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4928 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4929#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004930#if defined(MBEDTLS_RSA_C)
4931 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4932#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004933#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004934 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004935};
4936#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4937/* NOTICE: see above */
4938static uint16_t ssl_preset_suiteb_sig_algs[] = {
4939
Andrzej Kurek25f27152022-08-17 16:09:31 -04004940#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 +08004941 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4942 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004943#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004944 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4945
Andrzej Kurek25f27152022-08-17 16:09:31 -04004946#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 +08004947 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4948 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004949#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004950 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004951
Andrzej Kurek25f27152022-08-17 16:09:31 -04004952#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 +08004953 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004954#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 +08004955
Andrzej Kurek25f27152022-08-17 16:09:31 -04004956#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 +08004957 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004958#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004959
Gabor Mezei15b95a62022-05-09 16:37:58 +02004960 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004961};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004962
Jerry Yu909df7b2022-01-22 11:56:27 +08004963/* NOTICE: see above */
4964#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4965static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004966#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004967#if defined(MBEDTLS_ECDSA_C)
4968 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004969#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004970#if defined(MBEDTLS_RSA_C)
4971 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4972#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004973#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004974#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004975#if defined(MBEDTLS_ECDSA_C)
4976 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004977#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004978#if defined(MBEDTLS_RSA_C)
4979 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4980#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004981#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004982 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004983};
Jerry Yu909df7b2022-01-22 11:56:27 +08004984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4985
Ronald Crone68ab4f2022-10-05 12:46:29 +02004986#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004987
Brett Warrene0edc842021-08-17 09:53:13 +01004988static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004989#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004990 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004991#endif
4992#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004993 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004994#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004995 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004996};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004997
Ronald Crone68ab4f2022-10-05 12:46:29 +02004998#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004999/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005000 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005001MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005002static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005003{
5004 size_t i, j;
5005 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005006
Gabor Mezei15b95a62022-05-09 16:37:58 +02005007 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005008 {
Jerry Yuf377d642022-01-25 10:43:59 +08005009 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005010 {
Jerry Yuf377d642022-01-25 10:43:59 +08005011 if( sig_algs[i] != sig_algs[j] )
5012 continue;
5013 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5014 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5015 sig_algs[i], j, i );
5016 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005017 }
5018 }
5019 return( ret );
5020}
5021
Ronald Crone68ab4f2022-10-05 12:46:29 +02005022#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005023
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005024/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005025 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005026 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005027int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005028 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005029{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005030#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005032#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005033
Ronald Crone68ab4f2022-10-05 12:46:29 +02005034#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005035 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005036 {
5037 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5038 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5039 }
5040
Jerry Yuf377d642022-01-25 10:43:59 +08005041 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005042 {
5043 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5044 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5045 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005046
5047#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005048 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005049 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005050 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005051 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5052 }
5053
Jerry Yuf377d642022-01-25 10:43:59 +08005054 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005055 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005056 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005057 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5058 }
5059#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005060#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005061
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005062 /* Use the functions here so that they are covered in tests,
5063 * but otherwise access member directly for efficiency */
5064 mbedtls_ssl_conf_endpoint( conf, endpoint );
5065 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005066
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005067 /*
5068 * Things that are common to all presets
5069 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005070#if defined(MBEDTLS_SSL_CLI_C)
5071 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5072 {
5073 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5074#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5075 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5076#endif
5077 }
5078#endif
5079
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005080#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5081 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5082#endif
5083
5084#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5085 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5086#endif
5087
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005088#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005089 conf->f_cookie_write = ssl_cookie_write_dummy;
5090 conf->f_cookie_check = ssl_cookie_check_dummy;
5091#endif
5092
5093#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5094 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5095#endif
5096
Janos Follath088ce432017-04-10 12:42:31 +01005097#if defined(MBEDTLS_SSL_SRV_C)
5098 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005099 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005100#endif
5101
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005102#if defined(MBEDTLS_SSL_PROTO_DTLS)
5103 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5104 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5105#endif
5106
5107#if defined(MBEDTLS_SSL_RENEGOTIATION)
5108 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005109 memset( conf->renego_period, 0x00, 2 );
5110 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005111#endif
5112
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005113#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005114 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5115 {
5116 const unsigned char dhm_p[] =
5117 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5118 const unsigned char dhm_g[] =
5119 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005120
Hanno Beckere2defad2021-07-24 05:59:17 +01005121 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5122 dhm_p, sizeof( dhm_p ),
5123 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5124 {
5125 return( ret );
5126 }
5127 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005128#endif
5129
Ronald Cron6f135e12021-12-08 16:57:54 +01005130#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005131#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005132 mbedtls_ssl_conf_new_session_tickets(
5133 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5134#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005135 /*
5136 * Allow all TLS 1.3 key exchange modes by default.
5137 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005138 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005139#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005140
XiaokangQian4d3a6042022-04-21 13:46:17 +00005141 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5142 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005143#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005144 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005145 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005146#else
XiaokangQian060d8672022-04-21 09:24:56 +00005147 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005148#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005149 }
5150 else
5151 {
5152#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5153 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5154 {
5155 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5156 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5157 }
5158 else
5159 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5160 {
5161 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5162 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5163 }
5164#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5165 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5166 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5167#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5168 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5169 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5170#else
5171 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5172#endif
5173 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005174
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005175 /*
5176 * Preset-specific defaults
5177 */
5178 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005179 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005180 /*
5181 * NSA Suite B
5182 */
5183 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005184
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005185 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005186
5187#if defined(MBEDTLS_X509_CRT_PARSE_C)
5188 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005189#endif
5190
Ronald Crone68ab4f2022-10-05 12:46:29 +02005191#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005192#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5193 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5194 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5195 else
5196#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5197 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005198#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005199
Brett Warrene0edc842021-08-17 09:53:13 +01005200#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5201 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005202#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005203 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005204 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005205
5206 /*
5207 * Default
5208 */
5209 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005210
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005211 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005212
5213#if defined(MBEDTLS_X509_CRT_PARSE_C)
5214 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5215#endif
5216
Ronald Crone68ab4f2022-10-05 12:46:29 +02005217#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5219 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5220 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5221 else
5222#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5223 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005224#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005225
Brett Warrene0edc842021-08-17 09:53:13 +01005226#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5227 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005228#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005229 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005230
5231#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5232 conf->dhm_min_bitlen = 1024;
5233#endif
5234 }
5235
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005236 return( 0 );
5237}
5238
5239/*
5240 * Free mbedtls_ssl_config
5241 */
5242void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5243{
5244#if defined(MBEDTLS_DHM_C)
5245 mbedtls_mpi_free( &conf->dhm_P );
5246 mbedtls_mpi_free( &conf->dhm_G );
5247#endif
5248
Ronald Cron73fe8df2022-10-05 14:31:43 +02005249#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005250#if defined(MBEDTLS_USE_PSA_CRYPTO)
5251 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5252 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005253 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5254 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005255#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005256 if( conf->psk != NULL )
5257 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005258 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005259 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005260 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005261 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005262 }
5263
5264 if( conf->psk_identity != NULL )
5265 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005266 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005267 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005268 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005269 conf->psk_identity_len = 0;
5270 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005271#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005272
5273#if defined(MBEDTLS_X509_CRT_PARSE_C)
5274 ssl_key_cert_free( conf->key_cert );
5275#endif
5276
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005277 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005278}
5279
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005280#if defined(MBEDTLS_PK_C) && \
5281 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005282/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287#if defined(MBEDTLS_RSA_C)
5288 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5289 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291#if defined(MBEDTLS_ECDSA_C)
5292 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5293 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005296}
5297
Hanno Becker7e5437a2017-04-28 17:15:26 +01005298unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5299{
5300 switch( type ) {
5301 case MBEDTLS_PK_RSA:
5302 return( MBEDTLS_SSL_SIG_RSA );
5303 case MBEDTLS_PK_ECDSA:
5304 case MBEDTLS_PK_ECKEY:
5305 return( MBEDTLS_SSL_SIG_ECDSA );
5306 default:
5307 return( MBEDTLS_SSL_SIG_ANON );
5308 }
5309}
5310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005312{
5313 switch( sig )
5314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315#if defined(MBEDTLS_RSA_C)
5316 case MBEDTLS_SSL_SIG_RSA:
5317 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319#if defined(MBEDTLS_ECDSA_C)
5320 case MBEDTLS_SSL_SIG_ECDSA:
5321 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005322#endif
5323 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005325 }
5326}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005327#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005328
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005329/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005330 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005333{
5334 switch( hash )
5335 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005336#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 case MBEDTLS_SSL_HASH_MD5:
5338 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005339#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005340#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 case MBEDTLS_SSL_HASH_SHA1:
5342 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005343#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005344#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 case MBEDTLS_SSL_HASH_SHA224:
5346 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005347#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005348#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 case MBEDTLS_SSL_HASH_SHA256:
5350 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005351#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005352#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 case MBEDTLS_SSL_HASH_SHA384:
5354 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005355#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005356#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 case MBEDTLS_SSL_HASH_SHA512:
5358 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005359#endif
5360 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005362 }
5363}
5364
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005365/*
5366 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5367 */
5368unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5369{
5370 switch( md )
5371 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005372#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005373 case MBEDTLS_MD_MD5:
5374 return( MBEDTLS_SSL_HASH_MD5 );
5375#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005376#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005377 case MBEDTLS_MD_SHA1:
5378 return( MBEDTLS_SSL_HASH_SHA1 );
5379#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005380#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005381 case MBEDTLS_MD_SHA224:
5382 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005383#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005384#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005385 case MBEDTLS_MD_SHA256:
5386 return( MBEDTLS_SSL_HASH_SHA256 );
5387#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005388#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005389 case MBEDTLS_MD_SHA384:
5390 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005391#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005392#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005393 case MBEDTLS_MD_SHA512:
5394 return( MBEDTLS_SSL_HASH_SHA512 );
5395#endif
5396 default:
5397 return( MBEDTLS_SSL_HASH_NONE );
5398 }
5399}
5400
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005401/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005402 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005403 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005404 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005405int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005406{
Brett Warrene0edc842021-08-17 09:53:13 +01005407 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005408
Brett Warrene0edc842021-08-17 09:53:13 +01005409 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005410 return( -1 );
5411
Brett Warrene0edc842021-08-17 09:53:13 +01005412 for( ; *group_list != 0; group_list++ )
5413 {
5414 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005415 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005416 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005417
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005418 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005419}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005420
5421#if defined(MBEDTLS_ECP_C)
5422/*
5423 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5424 */
5425int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5426{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005427 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005428 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005429
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005430 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005431 return -1;
5432
5433 uint16_t tls_id = grp_info->tls_id;
5434
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005435 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5436}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005437#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439#if defined(MBEDTLS_X509_CRT_PARSE_C)
5440int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5441 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005442 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005443 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005444{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005445 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005446 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005447 const char *ext_oid;
5448 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005451 {
5452 /* Server part of the key exchange */
5453 switch( ciphersuite->key_exchange )
5454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 case MBEDTLS_KEY_EXCHANGE_RSA:
5456 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005457 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005458 break;
5459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5461 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5462 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5463 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005464 break;
5465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5467 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005468 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005469 break;
5470
5471 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 case MBEDTLS_KEY_EXCHANGE_NONE:
5473 case MBEDTLS_KEY_EXCHANGE_PSK:
5474 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5475 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005476 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005477 usage = 0;
5478 }
5479 }
5480 else
5481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5483 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005484 }
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005487 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005488 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005489 ret = -1;
5490 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5495 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005496 }
5497 else
5498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5500 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005501 }
5502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005504 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005505 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005506 ret = -1;
5507 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005508
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005509 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005512
Jerry Yu148165c2021-09-24 23:20:59 +08005513#if defined(MBEDTLS_USE_PSA_CRYPTO)
5514int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5515 const mbedtls_md_type_t md,
5516 unsigned char *dst,
5517 size_t dst_len,
5518 size_t *olen )
5519{
Ronald Cronf6893e12022-01-07 22:09:01 +01005520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5521 psa_hash_operation_t *hash_operation_to_clone;
5522 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5523
Jerry Yu148165c2021-09-24 23:20:59 +08005524 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005525
5526 switch( md )
5527 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005528#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005529 case MBEDTLS_MD_SHA384:
5530 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5531 break;
5532#endif
5533
Andrzej Kurek25f27152022-08-17 16:09:31 -04005534#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005535 case MBEDTLS_MD_SHA256:
5536 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5537 break;
5538#endif
5539
5540 default:
5541 goto exit;
5542 }
5543
5544 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5545 if( status != PSA_SUCCESS )
5546 goto exit;
5547
5548 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5549 if( status != PSA_SUCCESS )
5550 goto exit;
5551
5552exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005553#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5554 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5555 (void) ssl;
5556#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005557 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005558}
5559#else /* MBEDTLS_USE_PSA_CRYPTO */
5560
Andrzej Kurek25f27152022-08-17 16:09:31 -04005561#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005562MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005563static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5564 unsigned char *dst,
5565 size_t dst_len,
5566 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005567{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005568 int ret;
5569 mbedtls_sha512_context sha512;
5570
5571 if( dst_len < 48 )
5572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5573
5574 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005575 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005576
5577 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5578 {
5579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5580 goto exit;
5581 }
5582
5583 *olen = 48;
5584
5585exit:
5586
5587 mbedtls_sha512_free( &sha512 );
5588 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005589}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005590#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005591
Andrzej Kurek25f27152022-08-17 16:09:31 -04005592#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005593MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005594static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5595 unsigned char *dst,
5596 size_t dst_len,
5597 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005598{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005599 int ret;
5600 mbedtls_sha256_context sha256;
5601
5602 if( dst_len < 32 )
5603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5604
5605 mbedtls_sha256_init( &sha256 );
5606 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005607
Jerry Yu24c0ec32021-09-09 14:21:07 +08005608 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5609 {
5610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5611 goto exit;
5612 }
5613
5614 *olen = 32;
5615
5616exit:
5617
5618 mbedtls_sha256_free( &sha256 );
5619 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005620}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005621#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005622
Jerry Yu000f9762021-09-14 11:12:51 +08005623int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5624 const mbedtls_md_type_t md,
5625 unsigned char *dst,
5626 size_t dst_len,
5627 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005628{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005629 switch( md )
5630 {
5631
Andrzej Kurek25f27152022-08-17 16:09:31 -04005632#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005633 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005634 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005635#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005636
Andrzej Kurek25f27152022-08-17 16:09:31 -04005637#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005638 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005639 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005640#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005641
5642 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005643#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5644 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5645 (void) ssl;
5646 (void) dst;
5647 (void) dst_len;
5648 (void) olen;
5649#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005650 break;
5651 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005653}
XiaokangQian647719a2021-12-07 09:16:29 +00005654
Jerry Yu148165c2021-09-24 23:20:59 +08005655#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005656
Ronald Crone68ab4f2022-10-05 12:46:29 +02005657#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005658/* mbedtls_ssl_parse_sig_alg_ext()
5659 *
5660 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5661 * value (TLS 1.3 RFC8446):
5662 * enum {
5663 * ....
5664 * ecdsa_secp256r1_sha256( 0x0403 ),
5665 * ecdsa_secp384r1_sha384( 0x0503 ),
5666 * ecdsa_secp521r1_sha512( 0x0603 ),
5667 * ....
5668 * } SignatureScheme;
5669 *
5670 * struct {
5671 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5672 * } SignatureSchemeList;
5673 *
5674 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5675 * value (TLS 1.2 RFC5246):
5676 * enum {
5677 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5678 * sha512(6), (255)
5679 * } HashAlgorithm;
5680 *
5681 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5682 * SignatureAlgorithm;
5683 *
5684 * struct {
5685 * HashAlgorithm hash;
5686 * SignatureAlgorithm signature;
5687 * } SignatureAndHashAlgorithm;
5688 *
5689 * SignatureAndHashAlgorithm
5690 * supported_signature_algorithms<2..2^16-2>;
5691 *
5692 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5693 * generalization of the TLS 1.2 signature algorithm extension.
5694 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5695 * `SignatureScheme` field of TLS 1.3
5696 *
5697 */
5698int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5699 const unsigned char *buf,
5700 const unsigned char *end )
5701{
5702 const unsigned char *p = buf;
5703 size_t supported_sig_algs_len = 0;
5704 const unsigned char *supported_sig_algs_end;
5705 uint16_t sig_alg;
5706 uint32_t common_idx = 0;
5707
5708 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5709 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5710 p += 2;
5711
5712 memset( ssl->handshake->received_sig_algs, 0,
5713 sizeof(ssl->handshake->received_sig_algs) );
5714
5715 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5716 supported_sig_algs_end = p + supported_sig_algs_len;
5717 while( p < supported_sig_algs_end )
5718 {
5719 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5720 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5721 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005722 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5723 sig_alg,
5724 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005725#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005726 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005727 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5728 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5729 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005730 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005731 }
5732#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005733
Jerry Yuf3b46b52022-06-19 16:52:27 +08005734 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5735 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005736
5737 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5738 {
5739 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5740 common_idx += 1;
5741 }
5742 }
5743 /* Check that we consumed all the message. */
5744 if( p != end )
5745 {
5746 MBEDTLS_SSL_DEBUG_MSG( 1,
5747 ( "Signature algorithms extension length misaligned" ) );
5748 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5749 MBEDTLS_ERR_SSL_DECODE_ERROR );
5750 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5751 }
5752
5753 if( common_idx == 0 )
5754 {
5755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5756 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5757 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5758 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5759 }
5760
Gabor Mezei15b95a62022-05-09 16:37:58 +02005761 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005762 return( 0 );
5763}
5764
Ronald Crone68ab4f2022-10-05 12:46:29 +02005765#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005766
Jerry Yudc7bd172022-02-17 13:44:15 +08005767#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5768
5769#if defined(MBEDTLS_USE_PSA_CRYPTO)
5770
5771static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5772 mbedtls_svc_key_id_t key,
5773 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005774 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005775 const unsigned char* seed, size_t seed_length,
5776 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005777 const unsigned char* other_secret,
5778 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005779 size_t capacity )
5780{
5781 psa_status_t status;
5782
5783 status = psa_key_derivation_setup( derivation, alg );
5784 if( status != PSA_SUCCESS )
5785 return( status );
5786
5787 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5788 {
5789 status = psa_key_derivation_input_bytes( derivation,
5790 PSA_KEY_DERIVATION_INPUT_SEED,
5791 seed, seed_length );
5792 if( status != PSA_SUCCESS )
5793 return( status );
5794
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005795 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005796 {
5797 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005798 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5799 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005800 if( status != PSA_SUCCESS )
5801 return( status );
5802 }
5803
Jerry Yudc7bd172022-02-17 13:44:15 +08005804 if( mbedtls_svc_key_id_is_null( key ) )
5805 {
5806 status = psa_key_derivation_input_bytes(
5807 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005808 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005809 }
5810 else
5811 {
5812 status = psa_key_derivation_input_key(
5813 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5814 }
5815 if( status != PSA_SUCCESS )
5816 return( status );
5817
5818 status = psa_key_derivation_input_bytes( derivation,
5819 PSA_KEY_DERIVATION_INPUT_LABEL,
5820 label, label_length );
5821 if( status != PSA_SUCCESS )
5822 return( status );
5823 }
5824 else
5825 {
5826 return( PSA_ERROR_NOT_SUPPORTED );
5827 }
5828
5829 status = psa_key_derivation_set_capacity( derivation, capacity );
5830 if( status != PSA_SUCCESS )
5831 return( status );
5832
5833 return( PSA_SUCCESS );
5834}
5835
Andrzej Kurek57d10632022-10-24 10:32:01 -04005836#if defined(PSA_WANT_ALG_SHA_384) || \
5837 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005838MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005839static int tls_prf_generic( mbedtls_md_type_t md_type,
5840 const unsigned char *secret, size_t slen,
5841 const char *label,
5842 const unsigned char *random, size_t rlen,
5843 unsigned char *dstbuf, size_t dlen )
5844{
5845 psa_status_t status;
5846 psa_algorithm_t alg;
5847 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5848 psa_key_derivation_operation_t derivation =
5849 PSA_KEY_DERIVATION_OPERATION_INIT;
5850
5851 if( md_type == MBEDTLS_MD_SHA384 )
5852 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5853 else
5854 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5855
5856 /* Normally a "secret" should be long enough to be impossible to
5857 * find by brute force, and in particular should not be empty. But
5858 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5859 * and for this use case it makes sense to have a 0-length "secret".
5860 * Since the key API doesn't allow importing a key of length 0,
5861 * keep master_key=0, which setup_psa_key_derivation() understands
5862 * to mean a 0-length "secret" input. */
5863 if( slen != 0 )
5864 {
5865 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5866 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5867 psa_set_key_algorithm( &key_attributes, alg );
5868 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5869
5870 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5871 if( status != PSA_SUCCESS )
5872 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5873 }
5874
5875 status = setup_psa_key_derivation( &derivation,
5876 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005877 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005878 random, rlen,
5879 (unsigned char const *) label,
5880 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005881 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005882 dlen );
5883 if( status != PSA_SUCCESS )
5884 {
5885 psa_key_derivation_abort( &derivation );
5886 psa_destroy_key( master_key );
5887 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5888 }
5889
5890 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5891 if( status != PSA_SUCCESS )
5892 {
5893 psa_key_derivation_abort( &derivation );
5894 psa_destroy_key( master_key );
5895 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5896 }
5897
5898 status = psa_key_derivation_abort( &derivation );
5899 if( status != PSA_SUCCESS )
5900 {
5901 psa_destroy_key( master_key );
5902 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5903 }
5904
5905 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5906 status = psa_destroy_key( master_key );
5907 if( status != PSA_SUCCESS )
5908 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5909
5910 return( 0 );
5911}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005912#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005913#else /* MBEDTLS_USE_PSA_CRYPTO */
5914
Andrzej Kurek57d10632022-10-24 10:32:01 -04005915#if defined(MBEDTLS_MD_C) && \
5916 ( defined(MBEDTLS_SHA256_C) || \
5917 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005918MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005919static int tls_prf_generic( mbedtls_md_type_t md_type,
5920 const unsigned char *secret, size_t slen,
5921 const char *label,
5922 const unsigned char *random, size_t rlen,
5923 unsigned char *dstbuf, size_t dlen )
5924{
5925 size_t nb;
5926 size_t i, j, k, md_len;
5927 unsigned char *tmp;
5928 size_t tmp_len = 0;
5929 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5930 const mbedtls_md_info_t *md_info;
5931 mbedtls_md_context_t md_ctx;
5932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5933
5934 mbedtls_md_init( &md_ctx );
5935
5936 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5937 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5938
5939 md_len = mbedtls_md_get_size( md_info );
5940
5941 tmp_len = md_len + strlen( label ) + rlen;
5942 tmp = mbedtls_calloc( 1, tmp_len );
5943 if( tmp == NULL )
5944 {
5945 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5946 goto exit;
5947 }
5948
5949 nb = strlen( label );
5950 memcpy( tmp + md_len, label, nb );
5951 memcpy( tmp + md_len + nb, random, rlen );
5952 nb += rlen;
5953
5954 /*
5955 * Compute P_<hash>(secret, label + random)[0..dlen]
5956 */
5957 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5958 goto exit;
5959
5960 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5961 if( ret != 0 )
5962 goto exit;
5963 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5964 if( ret != 0 )
5965 goto exit;
5966 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5967 if( ret != 0 )
5968 goto exit;
5969
5970 for( i = 0; i < dlen; i += md_len )
5971 {
5972 ret = mbedtls_md_hmac_reset ( &md_ctx );
5973 if( ret != 0 )
5974 goto exit;
5975 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5976 if( ret != 0 )
5977 goto exit;
5978 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5979 if( ret != 0 )
5980 goto exit;
5981
5982 ret = mbedtls_md_hmac_reset ( &md_ctx );
5983 if( ret != 0 )
5984 goto exit;
5985 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5986 if( ret != 0 )
5987 goto exit;
5988 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5989 if( ret != 0 )
5990 goto exit;
5991
5992 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5993
5994 for( j = 0; j < k; j++ )
5995 dstbuf[i + j] = h_i[j];
5996 }
5997
5998exit:
5999 mbedtls_md_free( &md_ctx );
6000
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006001 if ( tmp != NULL )
6002 mbedtls_platform_zeroize( tmp, tmp_len );
6003
Jerry Yudc7bd172022-02-17 13:44:15 +08006004 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6005
6006 mbedtls_free( tmp );
6007
6008 return( ret );
6009}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006010#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006011#endif /* MBEDTLS_USE_PSA_CRYPTO */
6012
Andrzej Kurek25f27152022-08-17 16:09:31 -04006013#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006014MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006015static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6016 const char *label,
6017 const unsigned char *random, size_t rlen,
6018 unsigned char *dstbuf, size_t dlen )
6019{
6020 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6021 label, random, rlen, dstbuf, dlen ) );
6022}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006023#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006024
Andrzej Kurek25f27152022-08-17 16:09:31 -04006025#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006026MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006027static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6028 const char *label,
6029 const unsigned char *random, size_t rlen,
6030 unsigned char *dstbuf, size_t dlen )
6031{
6032 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6033 label, random, rlen, dstbuf, dlen ) );
6034}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006035#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006036
Jerry Yuf009d862022-02-17 14:01:37 +08006037/*
6038 * Set appropriate PRF function and other SSL / TLS1.2 functions
6039 *
6040 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006041 * - hash associated with the ciphersuite (only used by TLS 1.2)
6042 *
6043 * Outputs:
6044 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6045 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006046MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006047static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006048 mbedtls_md_type_t hash )
6049{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006050#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006051 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006052 {
6053 handshake->tls_prf = tls_prf_sha384;
6054 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6055 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6056 }
6057 else
6058#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006059#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006060 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006061 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006062 handshake->tls_prf = tls_prf_sha256;
6063 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6064 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6065 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006066#else
Jerry Yuf009d862022-02-17 14:01:37 +08006067 {
Jerry Yuf009d862022-02-17 14:01:37 +08006068 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006069 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006070 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6071 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006072#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006073
6074 return( 0 );
6075}
Jerry Yud6ab2352022-02-17 14:03:43 +08006076
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006077/*
6078 * Compute master secret if needed
6079 *
6080 * Parameters:
6081 * [in/out] handshake
6082 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6083 * (PSA-PSK) ciphersuite_info, psk_opaque
6084 * [out] premaster (cleared)
6085 * [out] master
6086 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6087 * debug: conf->f_dbg, conf->p_dbg
6088 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006089 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006090 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006091MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006092static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6093 unsigned char *master,
6094 const mbedtls_ssl_context *ssl )
6095{
6096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6097
6098 /* cf. RFC 5246, Section 8.1:
6099 * "The master secret is always exactly 48 bytes in length." */
6100 size_t const master_secret_len = 48;
6101
6102#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6103 unsigned char session_hash[48];
6104#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6105
6106 /* The label for the KDF used for key expansion.
6107 * This is either "master secret" or "extended master secret"
6108 * depending on whether the Extended Master Secret extension
6109 * is used. */
6110 char const *lbl = "master secret";
6111
Przemek Stekielae4ed302022-04-05 17:15:55 +02006112 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006113 * - If the Extended Master Secret extension is not used,
6114 * this is ClientHello.Random + ServerHello.Random
6115 * (see Sect. 8.1 in RFC 5246).
6116 * - If the Extended Master Secret extension is used,
6117 * this is the transcript of the handshake so far.
6118 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006119 unsigned char const *seed = handshake->randbytes;
6120 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006121
6122#if !defined(MBEDTLS_DEBUG_C) && \
6123 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6124 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6125 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6126 ssl = NULL; /* make sure we don't use it except for those cases */
6127 (void) ssl;
6128#endif
6129
6130 if( handshake->resume != 0 )
6131 {
6132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6133 return( 0 );
6134 }
6135
6136#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6137 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6138 {
6139 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006140 seed = session_hash;
6141 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006142
6143 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006144 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006145 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006146#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006147
Przemek Stekiel99114f32022-04-22 11:20:09 +02006148#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006149 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006150 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006151 {
6152 /* Perform PSK-to-MS expansion in a single step. */
6153 psa_status_t status;
6154 psa_algorithm_t alg;
6155 mbedtls_svc_key_id_t psk;
6156 psa_key_derivation_operation_t derivation =
6157 PSA_KEY_DERIVATION_OPERATION_INIT;
6158 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6159
6160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6161
6162 psk = mbedtls_ssl_get_opaque_psk( ssl );
6163
6164 if( hash_alg == MBEDTLS_MD_SHA384 )
6165 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6166 else
6167 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6168
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006169 size_t other_secret_len = 0;
6170 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006171
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006172 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006173 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006174 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006175 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006176 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006177 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006178 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006179 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006180 other_secret_len = 48;
6181 other_secret = handshake->premaster + 2;
6182 break;
6183 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006184 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006185 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6186 other_secret = handshake->premaster + 2;
6187 break;
6188 default:
6189 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006190 }
6191
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006192 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006193 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006194 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006195 (unsigned char const *) lbl,
6196 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006197 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006198 master_secret_len );
6199 if( status != PSA_SUCCESS )
6200 {
6201 psa_key_derivation_abort( &derivation );
6202 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6203 }
6204
6205 status = psa_key_derivation_output_bytes( &derivation,
6206 master,
6207 master_secret_len );
6208 if( status != PSA_SUCCESS )
6209 {
6210 psa_key_derivation_abort( &derivation );
6211 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6212 }
6213
6214 status = psa_key_derivation_abort( &derivation );
6215 if( status != PSA_SUCCESS )
6216 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6217 }
6218 else
6219#endif
6220 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006221#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6222 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6223 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6224 {
6225 psa_status_t status;
6226 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6227 psa_key_derivation_operation_t derivation =
6228 PSA_KEY_DERIVATION_OPERATION_INIT;
6229
6230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6231
6232 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6233
6234 status = psa_key_derivation_setup( &derivation, alg );
6235 if( status != PSA_SUCCESS )
6236 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6237
6238 status = psa_key_derivation_set_capacity( &derivation,
6239 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6240 if( status != PSA_SUCCESS )
6241 {
6242 psa_key_derivation_abort( &derivation );
6243 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6244 }
6245
6246 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6247 &derivation );
6248 if( status != PSA_SUCCESS )
6249 {
6250 psa_key_derivation_abort( &derivation );
6251 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6252 }
6253
6254 status = psa_key_derivation_output_bytes( &derivation,
6255 handshake->premaster,
6256 handshake->pmslen );
6257 if( status != PSA_SUCCESS )
6258 {
6259 psa_key_derivation_abort( &derivation );
6260 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6261 }
6262
6263 status = psa_key_derivation_abort( &derivation );
6264 if( status != PSA_SUCCESS )
6265 {
6266 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6267 }
6268 }
6269#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006270 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006271 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006272 master,
6273 master_secret_len );
6274 if( ret != 0 )
6275 {
6276 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6277 return( ret );
6278 }
6279
6280 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6281 handshake->premaster,
6282 handshake->pmslen );
6283
6284 mbedtls_platform_zeroize( handshake->premaster,
6285 sizeof(handshake->premaster) );
6286 }
6287
6288 return( 0 );
6289}
6290
Jerry Yud62f87e2022-02-17 14:09:02 +08006291int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6292{
6293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6294 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6295 ssl->handshake->ciphersuite_info;
6296
6297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6298
6299 /* Set PRF, calc_verify and calc_finished function pointers */
6300 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006301 ciphersuite_info->mac );
6302 if( ret != 0 )
6303 {
6304 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6305 return( ret );
6306 }
6307
6308 /* Compute master secret if needed */
6309 ret = ssl_compute_master( ssl->handshake,
6310 ssl->session_negotiate->master,
6311 ssl );
6312 if( ret != 0 )
6313 {
6314 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6315 return( ret );
6316 }
6317
6318 /* Swap the client and server random values:
6319 * - MS derivation wanted client+server (RFC 5246 8.1)
6320 * - key derivation wants server+client (RFC 5246 6.3) */
6321 {
6322 unsigned char tmp[64];
6323 memcpy( tmp, ssl->handshake->randbytes, 64 );
6324 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6325 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6326 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6327 }
6328
6329 /* Populate transform structure */
6330 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6331 ssl->session_negotiate->ciphersuite,
6332 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006333#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006334 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006335#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006336 ssl->handshake->tls_prf,
6337 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006338 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006339 ssl->conf->endpoint,
6340 ssl );
6341 if( ret != 0 )
6342 {
6343 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6344 return( ret );
6345 }
6346
6347 /* We no longer need Server/ClientHello.random values */
6348 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6349 sizeof( ssl->handshake->randbytes ) );
6350
6351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6352
6353 return( 0 );
6354}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006355
Ronald Cron4dcbca92022-03-07 10:21:40 +01006356int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6357{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006358 switch( md )
6359 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006360#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006361 case MBEDTLS_SSL_HASH_SHA384:
6362 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6363 break;
6364#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006365#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006366 case MBEDTLS_SSL_HASH_SHA256:
6367 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6368 break;
6369#endif
6370 default:
6371 return( -1 );
6372 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006373#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6374 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6375 (void) ssl;
6376#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006377 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006378}
6379
Andrzej Kurek25f27152022-08-17 16:09:31 -04006380#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006381void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6382 unsigned char *hash,
6383 size_t *hlen )
6384{
6385#if defined(MBEDTLS_USE_PSA_CRYPTO)
6386 size_t hash_size;
6387 psa_status_t status;
6388 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6389
6390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6391 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6392 if( status != PSA_SUCCESS )
6393 {
6394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6395 return;
6396 }
6397
6398 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6399 if( status != PSA_SUCCESS )
6400 {
6401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6402 return;
6403 }
6404
6405 *hlen = 32;
6406 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6408#else
6409 mbedtls_sha256_context sha256;
6410
6411 mbedtls_sha256_init( &sha256 );
6412
6413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6414
6415 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6416 mbedtls_sha256_finish( &sha256, hash );
6417
6418 *hlen = 32;
6419
6420 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6422
6423 mbedtls_sha256_free( &sha256 );
6424#endif /* MBEDTLS_USE_PSA_CRYPTO */
6425 return;
6426}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006427#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006428
Andrzej Kurek25f27152022-08-17 16:09:31 -04006429#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006430void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6431 unsigned char *hash,
6432 size_t *hlen )
6433{
6434#if defined(MBEDTLS_USE_PSA_CRYPTO)
6435 size_t hash_size;
6436 psa_status_t status;
6437 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6438
6439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6440 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6441 if( status != PSA_SUCCESS )
6442 {
6443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6444 return;
6445 }
6446
6447 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6448 if( status != PSA_SUCCESS )
6449 {
6450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6451 return;
6452 }
6453
6454 *hlen = 48;
6455 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6457#else
6458 mbedtls_sha512_context sha512;
6459
6460 mbedtls_sha512_init( &sha512 );
6461
6462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6463
Andrzej Kureka242e832022-08-11 10:03:14 -04006464 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006465 mbedtls_sha512_finish( &sha512, hash );
6466
6467 *hlen = 48;
6468
6469 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6471
6472 mbedtls_sha512_free( &sha512 );
6473#endif /* MBEDTLS_USE_PSA_CRYPTO */
6474 return;
6475}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006476#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006477
Neil Armstrong80f6f322022-05-03 17:56:38 +02006478#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6479 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006480int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6481{
6482 unsigned char *p = ssl->handshake->premaster;
6483 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6484 const unsigned char *psk = NULL;
6485 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006486 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006487
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006488 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006489 {
6490 /*
6491 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006492 * checked before calling this function.
6493 *
6494 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006495 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006496 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006497 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6498 {
6499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6501 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006502 }
6503
6504 /*
6505 * PMS = struct {
6506 * opaque other_secret<0..2^16-1>;
6507 * opaque psk<0..2^16-1>;
6508 * };
6509 * with "other_secret" depending on the particular key exchange
6510 */
6511#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6512 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6513 {
6514 if( end - p < 2 )
6515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6516
6517 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6518 p += 2;
6519
6520 if( end < p || (size_t)( end - p ) < psk_len )
6521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6522
6523 memset( p, 0, psk_len );
6524 p += psk_len;
6525 }
6526 else
6527#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6528#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6529 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6530 {
6531 /*
6532 * other_secret already set by the ClientKeyExchange message,
6533 * and is 48 bytes long
6534 */
6535 if( end - p < 2 )
6536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6537
6538 *p++ = 0;
6539 *p++ = 48;
6540 p += 48;
6541 }
6542 else
6543#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6544#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6545 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6546 {
6547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6548 size_t len;
6549
6550 /* Write length only when we know the actual value */
6551 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6552 p + 2, end - ( p + 2 ), &len,
6553 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6554 {
6555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6556 return( ret );
6557 }
6558 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6559 p += 2 + len;
6560
6561 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6562 }
6563 else
6564#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006565#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006566 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6567 {
6568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6569 size_t zlen;
6570
6571 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6572 p + 2, end - ( p + 2 ),
6573 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6574 {
6575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6576 return( ret );
6577 }
6578
6579 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6580 p += 2 + zlen;
6581
6582 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6583 MBEDTLS_DEBUG_ECDH_Z );
6584 }
6585 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006586#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006587 {
6588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6590 }
6591
6592 /* opaque psk<0..2^16-1>; */
6593 if( end - p < 2 )
6594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6595
6596 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6597 p += 2;
6598
6599 if( end < p || (size_t)( end - p ) < psk_len )
6600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6601
6602 memcpy( p, psk, psk_len );
6603 p += psk_len;
6604
6605 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6606
6607 return( 0 );
6608}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006609#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006610
6611#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006612MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006613static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6614
6615#if defined(MBEDTLS_SSL_PROTO_DTLS)
6616int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6617{
6618 /* If renegotiation is not enforced, retransmit until we would reach max
6619 * timeout if we were using the usual handshake doubling scheme */
6620 if( ssl->conf->renego_max_records < 0 )
6621 {
6622 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6623 unsigned char doublings = 1;
6624
6625 while( ratio != 0 )
6626 {
6627 ++doublings;
6628 ratio >>= 1;
6629 }
6630
6631 if( ++ssl->renego_records_seen > doublings )
6632 {
6633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6634 return( 0 );
6635 }
6636 }
6637
6638 return( ssl_write_hello_request( ssl ) );
6639}
6640#endif
6641#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006642
Jerry Yud9526692022-02-17 14:23:47 +08006643/*
6644 * Handshake functions
6645 */
6646#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6647/* No certificate support -> dummy functions */
6648int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6649{
6650 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6651 ssl->handshake->ciphersuite_info;
6652
6653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6654
6655 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6656 {
6657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6658 ssl->state++;
6659 return( 0 );
6660 }
6661
6662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6664}
6665
6666int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6667{
6668 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6669 ssl->handshake->ciphersuite_info;
6670
6671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6672
6673 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6674 {
6675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6676 ssl->state++;
6677 return( 0 );
6678 }
6679
6680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6682}
6683
6684#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6685/* Some certificate support -> implement write and parse */
6686
6687int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6688{
6689 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6690 size_t i, n;
6691 const mbedtls_x509_crt *crt;
6692 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6693 ssl->handshake->ciphersuite_info;
6694
6695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6696
6697 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6698 {
6699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6700 ssl->state++;
6701 return( 0 );
6702 }
6703
6704#if defined(MBEDTLS_SSL_CLI_C)
6705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6706 {
6707 if( ssl->handshake->client_auth == 0 )
6708 {
6709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6710 ssl->state++;
6711 return( 0 );
6712 }
6713 }
6714#endif /* MBEDTLS_SSL_CLI_C */
6715#if defined(MBEDTLS_SSL_SRV_C)
6716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6717 {
6718 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6719 {
6720 /* Should never happen because we shouldn't have picked the
6721 * ciphersuite if we don't have a certificate. */
6722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6723 }
6724 }
6725#endif
6726
6727 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6728
6729 /*
6730 * 0 . 0 handshake type
6731 * 1 . 3 handshake length
6732 * 4 . 6 length of all certs
6733 * 7 . 9 length of cert. 1
6734 * 10 . n-1 peer certificate
6735 * n . n+2 length of cert. 2
6736 * n+3 . ... upper level cert, etc.
6737 */
6738 i = 7;
6739 crt = mbedtls_ssl_own_cert( ssl );
6740
6741 while( crt != NULL )
6742 {
6743 n = crt->raw.len;
6744 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6745 {
6746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6747 " > %" MBEDTLS_PRINTF_SIZET,
6748 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6749 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6750 }
6751
6752 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6753 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6754 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6755
6756 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6757 i += n; crt = crt->next;
6758 }
6759
6760 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6761 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6762 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6763
6764 ssl->out_msglen = i;
6765 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6766 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6767
6768 ssl->state++;
6769
6770 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6771 {
6772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6773 return( ret );
6774 }
6775
6776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6777
6778 return( ret );
6779}
6780
6781#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6782
6783#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006784MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006785static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6786 unsigned char *crt_buf,
6787 size_t crt_buf_len )
6788{
6789 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6790
6791 if( peer_crt == NULL )
6792 return( -1 );
6793
6794 if( peer_crt->raw.len != crt_buf_len )
6795 return( -1 );
6796
6797 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6798}
6799#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006800MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006801static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6802 unsigned char *crt_buf,
6803 size_t crt_buf_len )
6804{
6805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6806 unsigned char const * const peer_cert_digest =
6807 ssl->session->peer_cert_digest;
6808 mbedtls_md_type_t const peer_cert_digest_type =
6809 ssl->session->peer_cert_digest_type;
6810 mbedtls_md_info_t const * const digest_info =
6811 mbedtls_md_info_from_type( peer_cert_digest_type );
6812 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6813 size_t digest_len;
6814
6815 if( peer_cert_digest == NULL || digest_info == NULL )
6816 return( -1 );
6817
6818 digest_len = mbedtls_md_get_size( digest_info );
6819 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6820 return( -1 );
6821
6822 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6823 if( ret != 0 )
6824 return( -1 );
6825
6826 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6827}
6828#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6829#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6830
6831/*
6832 * Once the certificate message is read, parse it into a cert chain and
6833 * perform basic checks, but leave actual verification to the caller
6834 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006835MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006836static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6837 mbedtls_x509_crt *chain )
6838{
6839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6840#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6841 int crt_cnt=0;
6842#endif
6843 size_t i, n;
6844 uint8_t alert;
6845
6846 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6847 {
6848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6849 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6850 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6851 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6852 }
6853
6854 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6855 {
6856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6857 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6858 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6859 }
6860
6861 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6862 {
6863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6864 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6865 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6866 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6867 }
6868
6869 i = mbedtls_ssl_hs_hdr_len( ssl );
6870
6871 /*
6872 * Same message structure as in mbedtls_ssl_write_certificate()
6873 */
6874 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6875
6876 if( ssl->in_msg[i] != 0 ||
6877 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6878 {
6879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6880 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6881 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6882 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6883 }
6884
6885 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6886 i += 3;
6887
6888 /* Iterate through and parse the CRTs in the provided chain. */
6889 while( i < ssl->in_hslen )
6890 {
6891 /* Check that there's room for the next CRT's length fields. */
6892 if ( i + 3 > ssl->in_hslen ) {
6893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6894 mbedtls_ssl_send_alert_message( ssl,
6895 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6896 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6897 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6898 }
6899 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6900 * anything beyond 2**16 ~ 64K. */
6901 if( ssl->in_msg[i] != 0 )
6902 {
6903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6904 mbedtls_ssl_send_alert_message( ssl,
6905 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6906 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6907 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6908 }
6909
6910 /* Read length of the next CRT in the chain. */
6911 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6912 | (unsigned int) ssl->in_msg[i + 2];
6913 i += 3;
6914
6915 if( n < 128 || i + n > ssl->in_hslen )
6916 {
6917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6918 mbedtls_ssl_send_alert_message( ssl,
6919 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6920 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6921 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6922 }
6923
6924 /* Check if we're handling the first CRT in the chain. */
6925#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6926 if( crt_cnt++ == 0 &&
6927 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6928 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6929 {
6930 /* During client-side renegotiation, check that the server's
6931 * end-CRTs hasn't changed compared to the initial handshake,
6932 * mitigating the triple handshake attack. On success, reuse
6933 * the original end-CRT instead of parsing it again. */
6934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6935 if( ssl_check_peer_crt_unchanged( ssl,
6936 &ssl->in_msg[i],
6937 n ) != 0 )
6938 {
6939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6940 mbedtls_ssl_send_alert_message( ssl,
6941 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6942 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6943 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6944 }
6945
6946 /* Now we can safely free the original chain. */
6947 ssl_clear_peer_cert( ssl->session );
6948 }
6949#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6950
6951 /* Parse the next certificate in the chain. */
6952#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6953 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6954#else
6955 /* If we don't need to store the CRT chain permanently, parse
6956 * it in-place from the input buffer instead of making a copy. */
6957 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6958#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6959 switch( ret )
6960 {
6961 case 0: /*ok*/
6962 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6963 /* Ignore certificate with an unknown algorithm: maybe a
6964 prior certificate was already trusted. */
6965 break;
6966
6967 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6968 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6969 goto crt_parse_der_failed;
6970
6971 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6972 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6973 goto crt_parse_der_failed;
6974
6975 default:
6976 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6977 crt_parse_der_failed:
6978 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6979 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6980 return( ret );
6981 }
6982
6983 i += n;
6984 }
6985
6986 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6987 return( 0 );
6988}
6989
6990#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006991MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006992static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6993{
6994 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6995 return( -1 );
6996
6997 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6998 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6999 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7000 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7001 {
Ronald Cron19385882022-06-15 16:26:13 +02007002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007003 return( 0 );
7004 }
7005 return( -1 );
7006}
7007#endif /* MBEDTLS_SSL_SRV_C */
7008
7009/* Check if a certificate message is expected.
7010 * Return either
7011 * - SSL_CERTIFICATE_EXPECTED, or
7012 * - SSL_CERTIFICATE_SKIP
7013 * indicating whether a Certificate message is expected or not.
7014 */
7015#define SSL_CERTIFICATE_EXPECTED 0
7016#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007017MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007018static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7019 int authmode )
7020{
7021 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7022 ssl->handshake->ciphersuite_info;
7023
7024 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7025 return( SSL_CERTIFICATE_SKIP );
7026
7027#if defined(MBEDTLS_SSL_SRV_C)
7028 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7029 {
7030 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7031 return( SSL_CERTIFICATE_SKIP );
7032
7033 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7034 {
7035 ssl->session_negotiate->verify_result =
7036 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7037 return( SSL_CERTIFICATE_SKIP );
7038 }
7039 }
7040#else
7041 ((void) authmode);
7042#endif /* MBEDTLS_SSL_SRV_C */
7043
7044 return( SSL_CERTIFICATE_EXPECTED );
7045}
7046
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007047MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007048static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7049 int authmode,
7050 mbedtls_x509_crt *chain,
7051 void *rs_ctx )
7052{
7053 int ret = 0;
7054 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7055 ssl->handshake->ciphersuite_info;
7056 int have_ca_chain = 0;
7057
7058 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7059 void *p_vrfy;
7060
7061 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7062 return( 0 );
7063
7064 if( ssl->f_vrfy != NULL )
7065 {
7066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7067 f_vrfy = ssl->f_vrfy;
7068 p_vrfy = ssl->p_vrfy;
7069 }
7070 else
7071 {
7072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7073 f_vrfy = ssl->conf->f_vrfy;
7074 p_vrfy = ssl->conf->p_vrfy;
7075 }
7076
7077 /*
7078 * Main check: verify certificate
7079 */
7080#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7081 if( ssl->conf->f_ca_cb != NULL )
7082 {
7083 ((void) rs_ctx);
7084 have_ca_chain = 1;
7085
7086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7087 ret = mbedtls_x509_crt_verify_with_ca_cb(
7088 chain,
7089 ssl->conf->f_ca_cb,
7090 ssl->conf->p_ca_cb,
7091 ssl->conf->cert_profile,
7092 ssl->hostname,
7093 &ssl->session_negotiate->verify_result,
7094 f_vrfy, p_vrfy );
7095 }
7096 else
7097#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7098 {
7099 mbedtls_x509_crt *ca_chain;
7100 mbedtls_x509_crl *ca_crl;
7101
7102#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7103 if( ssl->handshake->sni_ca_chain != NULL )
7104 {
7105 ca_chain = ssl->handshake->sni_ca_chain;
7106 ca_crl = ssl->handshake->sni_ca_crl;
7107 }
7108 else
7109#endif
7110 {
7111 ca_chain = ssl->conf->ca_chain;
7112 ca_crl = ssl->conf->ca_crl;
7113 }
7114
7115 if( ca_chain != NULL )
7116 have_ca_chain = 1;
7117
7118 ret = mbedtls_x509_crt_verify_restartable(
7119 chain,
7120 ca_chain, ca_crl,
7121 ssl->conf->cert_profile,
7122 ssl->hostname,
7123 &ssl->session_negotiate->verify_result,
7124 f_vrfy, p_vrfy, rs_ctx );
7125 }
7126
7127 if( ret != 0 )
7128 {
7129 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7130 }
7131
7132#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7133 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7134 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7135#endif
7136
7137 /*
7138 * Secondary checks: always done, but change 'ret' only if it was 0
7139 */
7140
7141#if defined(MBEDTLS_ECP_C)
7142 {
7143 const mbedtls_pk_context *pk = &chain->pk;
7144
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007145 /* If certificate uses an EC key, make sure the curve is OK.
7146 * This is a public key, so it can't be opaque, so can_do() is a good
7147 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007148 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007149 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007150 /* and in the unlikely case the above assumption no longer holds
7151 * we are making sure that pk_ec() here does not return a NULL
7152 */
7153 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7154 if( ec == NULL )
7155 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7158 }
Jerry Yud9526692022-02-17 14:23:47 +08007159
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007160 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7161 {
7162 ssl->session_negotiate->verify_result |=
7163 MBEDTLS_X509_BADCERT_BAD_KEY;
7164
7165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7166 if( ret == 0 )
7167 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7168 }
Jerry Yud9526692022-02-17 14:23:47 +08007169 }
7170 }
7171#endif /* MBEDTLS_ECP_C */
7172
7173 if( mbedtls_ssl_check_cert_usage( chain,
7174 ciphersuite_info,
7175 ! ssl->conf->endpoint,
7176 &ssl->session_negotiate->verify_result ) != 0 )
7177 {
7178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7179 if( ret == 0 )
7180 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7181 }
7182
7183 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7184 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7185 * with details encoded in the verification flags. All other kinds
7186 * of error codes, including those from the user provided f_vrfy
7187 * functions, are treated as fatal and lead to a failure of
7188 * ssl_parse_certificate even if verification was optional. */
7189 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7190 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7191 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7192 {
7193 ret = 0;
7194 }
7195
7196 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7197 {
7198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7199 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7200 }
7201
7202 if( ret != 0 )
7203 {
7204 uint8_t alert;
7205
7206 /* The certificate may have been rejected for several reasons.
7207 Pick one and send the corresponding alert. Which alert to send
7208 may be a subject of debate in some cases. */
7209 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7210 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7211 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7212 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7213 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7214 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7215 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7216 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7217 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7218 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7219 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7220 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7221 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7222 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7223 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7224 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7225 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7226 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7227 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7228 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7229 else
7230 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7231 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7232 alert );
7233 }
7234
7235#if defined(MBEDTLS_DEBUG_C)
7236 if( ssl->session_negotiate->verify_result != 0 )
7237 {
7238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7239 (unsigned int) ssl->session_negotiate->verify_result ) );
7240 }
7241 else
7242 {
7243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7244 }
7245#endif /* MBEDTLS_DEBUG_C */
7246
7247 return( ret );
7248}
7249
7250#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007251MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007252static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7253 unsigned char *start, size_t len )
7254{
7255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7256 /* Remember digest of the peer's end-CRT. */
7257 ssl->session_negotiate->peer_cert_digest =
7258 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7259 if( ssl->session_negotiate->peer_cert_digest == NULL )
7260 {
7261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7262 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7263 mbedtls_ssl_send_alert_message( ssl,
7264 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7265 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7266
7267 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7268 }
7269
7270 ret = mbedtls_md( mbedtls_md_info_from_type(
7271 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7272 start, len,
7273 ssl->session_negotiate->peer_cert_digest );
7274
7275 ssl->session_negotiate->peer_cert_digest_type =
7276 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7277 ssl->session_negotiate->peer_cert_digest_len =
7278 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7279
7280 return( ret );
7281}
7282
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007283MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007284static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7285 unsigned char *start, size_t len )
7286{
7287 unsigned char *end = start + len;
7288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7289
7290 /* Make a copy of the peer's raw public key. */
7291 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7292 ret = mbedtls_pk_parse_subpubkey( &start, end,
7293 &ssl->handshake->peer_pubkey );
7294 if( ret != 0 )
7295 {
7296 /* We should have parsed the public key before. */
7297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7298 }
7299
7300 return( 0 );
7301}
7302#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7303
7304int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7305{
7306 int ret = 0;
7307 int crt_expected;
7308#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7309 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7310 ? ssl->handshake->sni_authmode
7311 : ssl->conf->authmode;
7312#else
7313 const int authmode = ssl->conf->authmode;
7314#endif
7315 void *rs_ctx = NULL;
7316 mbedtls_x509_crt *chain = NULL;
7317
7318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7319
7320 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7321 if( crt_expected == SSL_CERTIFICATE_SKIP )
7322 {
7323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7324 goto exit;
7325 }
7326
7327#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7328 if( ssl->handshake->ecrs_enabled &&
7329 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7330 {
7331 chain = ssl->handshake->ecrs_peer_cert;
7332 ssl->handshake->ecrs_peer_cert = NULL;
7333 goto crt_verify;
7334 }
7335#endif
7336
7337 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7338 {
7339 /* mbedtls_ssl_read_record may have sent an alert already. We
7340 let it decide whether to alert. */
7341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7342 goto exit;
7343 }
7344
7345#if defined(MBEDTLS_SSL_SRV_C)
7346 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7347 {
7348 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7349
7350 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7351 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7352
7353 goto exit;
7354 }
7355#endif /* MBEDTLS_SSL_SRV_C */
7356
7357 /* Clear existing peer CRT structure in case we tried to
7358 * reuse a session but it failed, and allocate a new one. */
7359 ssl_clear_peer_cert( ssl->session_negotiate );
7360
7361 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7362 if( chain == NULL )
7363 {
7364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7365 sizeof( mbedtls_x509_crt ) ) );
7366 mbedtls_ssl_send_alert_message( ssl,
7367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7368 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7369
7370 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7371 goto exit;
7372 }
7373 mbedtls_x509_crt_init( chain );
7374
7375 ret = ssl_parse_certificate_chain( ssl, chain );
7376 if( ret != 0 )
7377 goto exit;
7378
7379#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7380 if( ssl->handshake->ecrs_enabled)
7381 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7382
7383crt_verify:
7384 if( ssl->handshake->ecrs_enabled)
7385 rs_ctx = &ssl->handshake->ecrs_ctx;
7386#endif
7387
7388 ret = ssl_parse_certificate_verify( ssl, authmode,
7389 chain, rs_ctx );
7390 if( ret != 0 )
7391 goto exit;
7392
7393#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7394 {
7395 unsigned char *crt_start, *pk_start;
7396 size_t crt_len, pk_len;
7397
7398 /* We parse the CRT chain without copying, so
7399 * these pointers point into the input buffer,
7400 * and are hence still valid after freeing the
7401 * CRT chain. */
7402
7403 crt_start = chain->raw.p;
7404 crt_len = chain->raw.len;
7405
7406 pk_start = chain->pk_raw.p;
7407 pk_len = chain->pk_raw.len;
7408
7409 /* Free the CRT structures before computing
7410 * digest and copying the peer's public key. */
7411 mbedtls_x509_crt_free( chain );
7412 mbedtls_free( chain );
7413 chain = NULL;
7414
7415 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7416 if( ret != 0 )
7417 goto exit;
7418
7419 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7420 if( ret != 0 )
7421 goto exit;
7422 }
7423#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7424 /* Pass ownership to session structure. */
7425 ssl->session_negotiate->peer_cert = chain;
7426 chain = NULL;
7427#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7428
7429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7430
7431exit:
7432
7433 if( ret == 0 )
7434 ssl->state++;
7435
7436#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7437 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7438 {
7439 ssl->handshake->ecrs_peer_cert = chain;
7440 chain = NULL;
7441 }
7442#endif
7443
7444 if( chain != NULL )
7445 {
7446 mbedtls_x509_crt_free( chain );
7447 mbedtls_free( chain );
7448 }
7449
7450 return( ret );
7451}
7452#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7453
Andrzej Kurek25f27152022-08-17 16:09:31 -04007454#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007455static void ssl_calc_finished_tls_sha256(
7456 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7457{
7458 int len = 12;
7459 const char *sender;
7460 unsigned char padbuf[32];
7461#if defined(MBEDTLS_USE_PSA_CRYPTO)
7462 size_t hash_size;
7463 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7464 psa_status_t status;
7465#else
7466 mbedtls_sha256_context sha256;
7467#endif
7468
7469 mbedtls_ssl_session *session = ssl->session_negotiate;
7470 if( !session )
7471 session = ssl->session;
7472
7473 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7474 ? "client finished"
7475 : "server finished";
7476
7477#if defined(MBEDTLS_USE_PSA_CRYPTO)
7478 sha256_psa = psa_hash_operation_init();
7479
7480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7481
7482 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7483 if( status != PSA_SUCCESS )
7484 {
7485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7486 return;
7487 }
7488
7489 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7490 if( status != PSA_SUCCESS )
7491 {
7492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7493 return;
7494 }
7495 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7496#else
7497
7498 mbedtls_sha256_init( &sha256 );
7499
7500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7501
7502 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7503
7504 /*
7505 * TLSv1.2:
7506 * hash = PRF( master, finished_label,
7507 * Hash( handshake ) )[0.11]
7508 */
7509
7510#if !defined(MBEDTLS_SHA256_ALT)
7511 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7512 sha256.state, sizeof( sha256.state ) );
7513#endif
7514
7515 mbedtls_sha256_finish( &sha256, padbuf );
7516 mbedtls_sha256_free( &sha256 );
7517#endif /* MBEDTLS_USE_PSA_CRYPTO */
7518
7519 ssl->handshake->tls_prf( session->master, 48, sender,
7520 padbuf, 32, buf, len );
7521
7522 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7523
7524 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7525
7526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7527}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007528#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007529
Jerry Yub7ba49e2022-02-17 14:25:53 +08007530
Andrzej Kurek25f27152022-08-17 16:09:31 -04007531#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007532static void ssl_calc_finished_tls_sha384(
7533 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7534{
7535 int len = 12;
7536 const char *sender;
7537 unsigned char padbuf[48];
7538#if defined(MBEDTLS_USE_PSA_CRYPTO)
7539 size_t hash_size;
7540 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7541 psa_status_t status;
7542#else
7543 mbedtls_sha512_context sha512;
7544#endif
7545
7546 mbedtls_ssl_session *session = ssl->session_negotiate;
7547 if( !session )
7548 session = ssl->session;
7549
7550 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7551 ? "client finished"
7552 : "server finished";
7553
7554#if defined(MBEDTLS_USE_PSA_CRYPTO)
7555 sha384_psa = psa_hash_operation_init();
7556
7557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7558
7559 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7560 if( status != PSA_SUCCESS )
7561 {
7562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7563 return;
7564 }
7565
7566 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7567 if( status != PSA_SUCCESS )
7568 {
7569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7570 return;
7571 }
7572 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7573#else
7574 mbedtls_sha512_init( &sha512 );
7575
7576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7577
Andrzej Kureka242e832022-08-11 10:03:14 -04007578 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007579
7580 /*
7581 * TLSv1.2:
7582 * hash = PRF( master, finished_label,
7583 * Hash( handshake ) )[0.11]
7584 */
7585
7586#if !defined(MBEDTLS_SHA512_ALT)
7587 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7588 sha512.state, sizeof( sha512.state ) );
7589#endif
7590 mbedtls_sha512_finish( &sha512, padbuf );
7591
7592 mbedtls_sha512_free( &sha512 );
7593#endif
7594
7595 ssl->handshake->tls_prf( session->master, 48, sender,
7596 padbuf, 48, buf, len );
7597
7598 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7599
7600 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7601
7602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7603}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007604#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007605
Jerry Yuaef00152022-02-17 14:27:31 +08007606void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7607{
7608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7609
7610 /*
7611 * Free our handshake params
7612 */
7613 mbedtls_ssl_handshake_free( ssl );
7614 mbedtls_free( ssl->handshake );
7615 ssl->handshake = NULL;
7616
7617 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007618 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007619 */
7620 if( ssl->transform )
7621 {
7622 mbedtls_ssl_transform_free( ssl->transform );
7623 mbedtls_free( ssl->transform );
7624 }
7625 ssl->transform = ssl->transform_negotiate;
7626 ssl->transform_negotiate = NULL;
7627
7628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7629}
7630
Jerry Yu2a9fff52022-02-17 14:28:51 +08007631void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7632{
7633 int resume = ssl->handshake->resume;
7634
7635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7636
7637#if defined(MBEDTLS_SSL_RENEGOTIATION)
7638 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7639 {
7640 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7641 ssl->renego_records_seen = 0;
7642 }
7643#endif
7644
7645 /*
7646 * Free the previous session and switch in the current one
7647 */
7648 if( ssl->session )
7649 {
7650#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7651 /* RFC 7366 3.1: keep the EtM state */
7652 ssl->session_negotiate->encrypt_then_mac =
7653 ssl->session->encrypt_then_mac;
7654#endif
7655
7656 mbedtls_ssl_session_free( ssl->session );
7657 mbedtls_free( ssl->session );
7658 }
7659 ssl->session = ssl->session_negotiate;
7660 ssl->session_negotiate = NULL;
7661
7662 /*
7663 * Add cache entry
7664 */
7665 if( ssl->conf->f_set_cache != NULL &&
7666 ssl->session->id_len != 0 &&
7667 resume == 0 )
7668 {
7669 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7670 ssl->session->id,
7671 ssl->session->id_len,
7672 ssl->session ) != 0 )
7673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7674 }
7675
7676#if defined(MBEDTLS_SSL_PROTO_DTLS)
7677 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7678 ssl->handshake->flight != NULL )
7679 {
7680 /* Cancel handshake timer */
7681 mbedtls_ssl_set_timer( ssl, 0 );
7682
7683 /* Keep last flight around in case we need to resend it:
7684 * we need the handshake and transform structures for that */
7685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7686 }
7687 else
7688#endif
7689 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7690
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007691 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007692
7693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7694}
7695
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007696int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7697{
7698 int ret, hash_len;
7699
7700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7701
7702 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7703
7704 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7705
7706 /*
7707 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7708 * may define some other value. Currently (early 2016), no defined
7709 * ciphersuite does this (and this is unlikely to change as activity has
7710 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7711 */
7712 hash_len = 12;
7713
7714#if defined(MBEDTLS_SSL_RENEGOTIATION)
7715 ssl->verify_data_len = hash_len;
7716 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7717#endif
7718
7719 ssl->out_msglen = 4 + hash_len;
7720 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7721 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7722
7723 /*
7724 * In case of session resuming, invert the client and server
7725 * ChangeCipherSpec messages order.
7726 */
7727 if( ssl->handshake->resume != 0 )
7728 {
7729#if defined(MBEDTLS_SSL_CLI_C)
7730 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7731 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7732#endif
7733#if defined(MBEDTLS_SSL_SRV_C)
7734 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7735 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7736#endif
7737 }
7738 else
7739 ssl->state++;
7740
7741 /*
7742 * Switch to our negotiated transform and session parameters for outbound
7743 * data.
7744 */
7745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7746
7747#if defined(MBEDTLS_SSL_PROTO_DTLS)
7748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7749 {
7750 unsigned char i;
7751
7752 /* Remember current epoch settings for resending */
7753 ssl->handshake->alt_transform_out = ssl->transform_out;
7754 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7755 sizeof( ssl->handshake->alt_out_ctr ) );
7756
7757 /* Set sequence_number to zero */
7758 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7759
7760
7761 /* Increment epoch */
7762 for( i = 2; i > 0; i-- )
7763 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7764 break;
7765
7766 /* The loop goes to its end iff the counter is wrapping */
7767 if( i == 0 )
7768 {
7769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7770 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7771 }
7772 }
7773 else
7774#endif /* MBEDTLS_SSL_PROTO_DTLS */
7775 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7776
7777 ssl->transform_out = ssl->transform_negotiate;
7778 ssl->session_out = ssl->session_negotiate;
7779
7780#if defined(MBEDTLS_SSL_PROTO_DTLS)
7781 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7782 mbedtls_ssl_send_flight_completed( ssl );
7783#endif
7784
7785 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7786 {
7787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7788 return( ret );
7789 }
7790
7791#if defined(MBEDTLS_SSL_PROTO_DTLS)
7792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7793 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7794 {
7795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7796 return( ret );
7797 }
7798#endif
7799
7800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7801
7802 return( 0 );
7803}
7804
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007805#define SSL_MAX_HASH_LEN 12
7806
7807int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7808{
7809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7810 unsigned int hash_len = 12;
7811 unsigned char buf[SSL_MAX_HASH_LEN];
7812
7813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7814
7815 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7816
7817 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7818 {
7819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7820 goto exit;
7821 }
7822
7823 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7824 {
7825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7826 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7827 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7828 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7829 goto exit;
7830 }
7831
7832 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7833 {
7834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7835 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7836 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7837 goto exit;
7838 }
7839
7840 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7841 {
7842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7843 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7844 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7845 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7846 goto exit;
7847 }
7848
7849 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7850 buf, hash_len ) != 0 )
7851 {
7852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7854 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7855 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7856 goto exit;
7857 }
7858
7859#if defined(MBEDTLS_SSL_RENEGOTIATION)
7860 ssl->verify_data_len = hash_len;
7861 memcpy( ssl->peer_verify_data, buf, hash_len );
7862#endif
7863
7864 if( ssl->handshake->resume != 0 )
7865 {
7866#if defined(MBEDTLS_SSL_CLI_C)
7867 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7868 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7869#endif
7870#if defined(MBEDTLS_SSL_SRV_C)
7871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7872 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7873#endif
7874 }
7875 else
7876 ssl->state++;
7877
7878#if defined(MBEDTLS_SSL_PROTO_DTLS)
7879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7880 mbedtls_ssl_recv_flight_completed( ssl );
7881#endif
7882
7883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7884
7885exit:
7886 mbedtls_platform_zeroize( buf, hash_len );
7887 return( ret );
7888}
7889
Jerry Yu392112c2022-02-17 14:34:10 +08007890#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7891/*
7892 * Helper to get TLS 1.2 PRF from ciphersuite
7893 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7894 */
7895static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7896{
Jerry Yu392112c2022-02-17 14:34:10 +08007897 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007898 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7899#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007900 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007901 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007902 else
Jerry Yu392112c2022-02-17 14:34:10 +08007903#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007904#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7905 {
7906 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7907 return( tls_prf_sha256 );
7908 }
7909#endif
7910#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7911 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7912 (void) ciphersuite_info;
7913#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007914
Andrzej Kurek894edde2022-09-29 06:31:14 -04007915 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007916}
7917#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007918
7919static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7920{
7921 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007922#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007923 if( tls_prf == tls_prf_sha384 )
7924 {
7925 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7926 }
7927 else
7928#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007929#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007930 if( tls_prf == tls_prf_sha256 )
7931 {
7932 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7933 }
7934 else
7935#endif
7936 return( MBEDTLS_SSL_TLS_PRF_NONE );
7937}
7938
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007939/*
7940 * Populate a transform structure with session keys and all the other
7941 * necessary information.
7942 *
7943 * Parameters:
7944 * - [in/out]: transform: structure to populate
7945 * [in] must be just initialised with mbedtls_ssl_transform_init()
7946 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7947 * - [in] ciphersuite
7948 * - [in] master
7949 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007950 * - [in] tls_prf: pointer to PRF to use for key derivation
7951 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007952 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007953 * - [in] endpoint: client or server
7954 * - [in] ssl: used for:
7955 * - ssl->conf->{f,p}_export_keys
7956 * [in] optionally used for:
7957 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7958 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007959MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007960static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7961 int ciphersuite,
7962 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007963#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007964 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007965#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007966 ssl_tls_prf_t tls_prf,
7967 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007968 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007969 unsigned endpoint,
7970 const mbedtls_ssl_context *ssl )
7971{
7972 int ret = 0;
7973 unsigned char keyblk[256];
7974 unsigned char *key1;
7975 unsigned char *key2;
7976 unsigned char *mac_enc;
7977 unsigned char *mac_dec;
7978 size_t mac_key_len = 0;
7979 size_t iv_copy_len;
7980 size_t keylen;
7981 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007982 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007983#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007984 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007985 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007986#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007987
7988#if defined(MBEDTLS_USE_PSA_CRYPTO)
7989 psa_key_type_t key_type;
7990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7991 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007992 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007993 size_t key_bits;
7994 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7995#endif
7996
7997#if !defined(MBEDTLS_DEBUG_C) && \
7998 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7999 if( ssl->f_export_keys == NULL )
8000 {
8001 ssl = NULL; /* make sure we don't use it except for these cases */
8002 (void) ssl;
8003 }
8004#endif
8005
8006 /*
8007 * Some data just needs copying into the structure
8008 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008009#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008010 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008011#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008012 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008013
8014#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8015 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8016#endif
8017
8018#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008019 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008020 {
8021 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8022 * generation separate. This should never happen. */
8023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8024 }
8025#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8026
8027 /*
8028 * Get various info structures
8029 */
8030 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8031 if( ciphersuite_info == NULL )
8032 {
8033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8034 ciphersuite ) );
8035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8036 }
8037
Neil Armstrongab555e02022-04-04 11:07:59 +02008038 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008039#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008040 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008041#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008042 ciphersuite_info );
8043
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008044 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8045 transform->taglen =
8046 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8047
8048#if defined(MBEDTLS_USE_PSA_CRYPTO)
8049 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8050 transform->taglen,
8051 &alg,
8052 &key_type,
8053 &key_bits ) ) != PSA_SUCCESS )
8054 {
8055 ret = psa_ssl_status_to_mbedtls( status );
8056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8057 goto end;
8058 }
8059#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008060 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8061 if( cipher_info == NULL )
8062 {
8063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8064 ciphersuite_info->cipher ) );
8065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8066 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008067#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008068
Neil Armstronge4512952022-03-08 09:08:22 +01008069#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008070 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008071 if( mac_alg == 0 )
8072 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008074 (unsigned) ciphersuite_info->mac ) );
8075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8076 }
8077#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008078 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8079 if( md_info == NULL )
8080 {
8081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8082 (unsigned) ciphersuite_info->mac ) );
8083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8084 }
Neil Armstronge4512952022-03-08 09:08:22 +01008085#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008086
8087#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8088 /* Copy own and peer's CID if the use of the CID
8089 * extension has been negotiated. */
8090 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8091 {
8092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8093
8094 transform->in_cid_len = ssl->own_cid_len;
8095 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8096 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8097 transform->in_cid_len );
8098
8099 transform->out_cid_len = ssl->handshake->peer_cid_len;
8100 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8101 ssl->handshake->peer_cid_len );
8102 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8103 transform->out_cid_len );
8104 }
8105#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8106
8107 /*
8108 * Compute key block using the PRF
8109 */
8110 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8111 if( ret != 0 )
8112 {
8113 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8114 return( ret );
8115 }
8116
8117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8118 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8119 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8120 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8121 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8122
8123 /*
8124 * Determine the appropriate key, IV and MAC length.
8125 */
8126
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008127#if defined(MBEDTLS_USE_PSA_CRYPTO)
8128 keylen = PSA_BITS_TO_BYTES(key_bits);
8129#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008130 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008131#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008132
8133#if defined(MBEDTLS_GCM_C) || \
8134 defined(MBEDTLS_CCM_C) || \
8135 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008136 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008137 {
8138 size_t explicit_ivlen;
8139
8140 transform->maclen = 0;
8141 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008142
8143 /* All modes haves 96-bit IVs, but the length of the static parts vary
8144 * with mode and version:
8145 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8146 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8147 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8148 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8149 * sequence number).
8150 */
8151 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008152
8153 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008154#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008155 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008156#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008157 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8158 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008159#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008160
8161 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008162 transform->fixed_ivlen = 12;
8163 else
8164 transform->fixed_ivlen = 4;
8165
8166 /* Minimum length of encrypted record */
8167 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8168 transform->minlen = explicit_ivlen + transform->taglen;
8169 }
8170 else
8171#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8172#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008173 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8174 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8175 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008176 {
Neil Armstronge4512952022-03-08 09:08:22 +01008177#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008178 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008179#else
8180 size_t block_size = cipher_info->block_size;
8181#endif /* MBEDTLS_USE_PSA_CRYPTO */
8182
8183#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008184 /* Get MAC length */
8185 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8186#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008187 /* Initialize HMAC contexts */
8188 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8189 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8190 {
8191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8192 goto end;
8193 }
8194
8195 /* Get MAC length */
8196 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008197#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008198 transform->maclen = mac_key_len;
8199
8200 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008201#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008202 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008203#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008204 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008206
8207 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008208 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209 transform->minlen = transform->maclen;
8210 else
8211 {
8212 /*
8213 * GenericBlockCipher:
8214 * 1. if EtM is in use: one block plus MAC
8215 * otherwise: * first multiple of blocklen greater than maclen
8216 * 2. IV
8217 */
8218#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008219 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008220 {
8221 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008222 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008223 }
8224 else
8225#endif
8226 {
8227 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008228 + block_size
8229 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008230 }
8231
Glenn Strauss07c64162022-03-14 12:34:51 -04008232 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008233 {
8234 transform->minlen += transform->ivlen;
8235 }
8236 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008237 {
8238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8239 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8240 goto end;
8241 }
8242 }
8243 }
8244 else
8245#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8246 {
8247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8249 }
8250
8251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8252 (unsigned) keylen,
8253 (unsigned) transform->minlen,
8254 (unsigned) transform->ivlen,
8255 (unsigned) transform->maclen ) );
8256
8257 /*
8258 * Finally setup the cipher contexts, IVs and MAC secrets.
8259 */
8260#if defined(MBEDTLS_SSL_CLI_C)
8261 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8262 {
8263 key1 = keyblk + mac_key_len * 2;
8264 key2 = keyblk + mac_key_len * 2 + keylen;
8265
8266 mac_enc = keyblk;
8267 mac_dec = keyblk + mac_key_len;
8268
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008269 iv_copy_len = ( transform->fixed_ivlen ) ?
8270 transform->fixed_ivlen : transform->ivlen;
8271 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8272 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8273 iv_copy_len );
8274 }
8275 else
8276#endif /* MBEDTLS_SSL_CLI_C */
8277#if defined(MBEDTLS_SSL_SRV_C)
8278 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8279 {
8280 key1 = keyblk + mac_key_len * 2 + keylen;
8281 key2 = keyblk + mac_key_len * 2;
8282
8283 mac_enc = keyblk + mac_key_len;
8284 mac_dec = keyblk;
8285
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008286 iv_copy_len = ( transform->fixed_ivlen ) ?
8287 transform->fixed_ivlen : transform->ivlen;
8288 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8289 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8290 iv_copy_len );
8291 }
8292 else
8293#endif /* MBEDTLS_SSL_SRV_C */
8294 {
8295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8297 goto end;
8298 }
8299
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300 if( ssl != NULL && ssl->f_export_keys != NULL )
8301 {
8302 ssl->f_export_keys( ssl->p_export_keys,
8303 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8304 master, 48,
8305 randbytes + 32,
8306 randbytes,
8307 tls_prf_get_type( tls_prf ) );
8308 }
8309
8310#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008311 transform->psa_alg = alg;
8312
8313 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8314 {
8315 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8316 psa_set_key_algorithm( &attributes, alg );
8317 psa_set_key_type( &attributes, key_type );
8318
8319 if( ( status = psa_import_key( &attributes,
8320 key1,
8321 PSA_BITS_TO_BYTES( key_bits ),
8322 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8323 {
8324 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8325 ret = psa_ssl_status_to_mbedtls( status );
8326 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8327 goto end;
8328 }
8329
8330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8331
8332 if( ( status = psa_import_key( &attributes,
8333 key2,
8334 PSA_BITS_TO_BYTES( key_bits ),
8335 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8336 {
8337 ret = psa_ssl_status_to_mbedtls( status );
8338 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8339 goto end;
8340 }
8341 }
8342#else
8343 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8344 cipher_info ) ) != 0 )
8345 {
8346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8347 goto end;
8348 }
8349
8350 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8351 cipher_info ) ) != 0 )
8352 {
8353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8354 goto end;
8355 }
8356
8357 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8358 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8359 MBEDTLS_ENCRYPT ) ) != 0 )
8360 {
8361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8362 goto end;
8363 }
8364
8365 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8366 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8367 MBEDTLS_DECRYPT ) ) != 0 )
8368 {
8369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8370 goto end;
8371 }
8372
8373#if defined(MBEDTLS_CIPHER_MODE_CBC)
8374 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8375 {
8376 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8377 MBEDTLS_PADDING_NONE ) ) != 0 )
8378 {
8379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8380 goto end;
8381 }
8382
8383 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8384 MBEDTLS_PADDING_NONE ) ) != 0 )
8385 {
8386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8387 goto end;
8388 }
8389 }
8390#endif /* MBEDTLS_CIPHER_MODE_CBC */
8391#endif /* MBEDTLS_USE_PSA_CRYPTO */
8392
Neil Armstrong29c0c042022-03-17 17:47:28 +01008393#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8394 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8395 For AEAD-based ciphersuites, there is nothing to do here. */
8396 if( mac_key_len != 0 )
8397 {
8398#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008399 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008400
8401 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008402 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008403 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8404
8405 if( ( status = psa_import_key( &attributes,
8406 mac_enc, mac_key_len,
8407 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8408 {
8409 ret = psa_ssl_status_to_mbedtls( status );
8410 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8411 goto end;
8412 }
8413
Ronald Cronfb39f152022-03-25 14:36:28 +01008414 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008415 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8416#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8417 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8418#endif
8419 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008420 /* mbedtls_ct_hmac() requires the key to be exportable */
8421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8422 PSA_KEY_USAGE_VERIFY_HASH );
8423 else
8424 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8425
8426 if( ( status = psa_import_key( &attributes,
8427 mac_dec, mac_key_len,
8428 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8429 {
8430 ret = psa_ssl_status_to_mbedtls( status );
8431 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8432 goto end;
8433 }
8434#else
8435 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8436 if( ret != 0 )
8437 goto end;
8438 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8439 if( ret != 0 )
8440 goto end;
8441#endif /* MBEDTLS_USE_PSA_CRYPTO */
8442 }
8443#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8444
8445 ((void) mac_dec);
8446 ((void) mac_enc);
8447
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008448end:
8449 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8450 return( ret );
8451}
8452
Valerio Settia08b1a42022-11-17 15:10:02 +01008453#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8454 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008455int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008456 psa_pake_operation_t *pake_ctx,
8457 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008458 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008459{
8460 psa_status_t status;
8461 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008462 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008463 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8464 * At round two perform a single cycle
8465 */
8466 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008467
Valerio Setti6b3dab02022-11-17 17:14:54 +01008468 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008469 {
8470 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8471 step <= PSA_PAKE_STEP_ZK_PROOF;
8472 ++step )
8473 {
8474 /* Length is stored at the first byte */
8475 size_t length = buf[input_offset];
8476 input_offset += 1;
8477
8478 if( input_offset + length > len )
8479 {
8480 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8481 }
8482
8483 status = psa_pake_input( pake_ctx, step,
8484 buf + input_offset, length );
8485 if( status != PSA_SUCCESS)
8486 {
8487 return psa_ssl_status_to_mbedtls( status );
8488 }
8489
8490 input_offset += length;
8491 }
8492 }
8493
Valerio Setti819de862022-11-17 18:05:19 +01008494 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008495 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008496
Valerio Settia08b1a42022-11-17 15:10:02 +01008497 return( 0 );
8498}
8499
Valerio Setti6b3dab02022-11-17 17:14:54 +01008500int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008501 psa_pake_operation_t *pake_ctx,
8502 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008503 size_t len, size_t *olen,
8504 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008505{
8506 psa_status_t status;
8507 size_t output_offset = 0;
8508 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008509 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008510 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8511 * At round two perform a single cycle
8512 */
8513 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008514
Valerio Setti6b3dab02022-11-17 17:14:54 +01008515 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008516 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008517 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8518 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008519 ++step )
8520 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008521 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008522 * For each step, prepend 1 byte with the length of the data as
8523 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008524 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008525 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008526 buf + output_offset + 1,
8527 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008528 &output_len );
8529 if( status != PSA_SUCCESS )
8530 {
8531 return( psa_ssl_status_to_mbedtls( status ) );
8532 }
8533
Valerio Setti99d88c12022-11-22 16:03:43 +01008534 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008535
8536 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008537 }
8538 }
8539
8540 *olen = output_offset;
8541
8542 return( 0 );
8543}
Valerio Settia08b1a42022-11-17 15:10:02 +01008544#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8545
Jerry Yuee40f9d2022-02-17 14:55:16 +08008546#if defined(MBEDTLS_USE_PSA_CRYPTO)
8547int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8548 unsigned char *hash, size_t *hashlen,
8549 unsigned char *data, size_t data_len,
8550 mbedtls_md_type_t md_alg )
8551{
8552 psa_status_t status;
8553 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008554 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008555
8556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8557
8558 if( ( status = psa_hash_setup( &hash_operation,
8559 hash_alg ) ) != PSA_SUCCESS )
8560 {
8561 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8562 goto exit;
8563 }
8564
8565 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8566 64 ) ) != PSA_SUCCESS )
8567 {
8568 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8569 goto exit;
8570 }
8571
8572 if( ( status = psa_hash_update( &hash_operation,
8573 data, data_len ) ) != PSA_SUCCESS )
8574 {
8575 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8576 goto exit;
8577 }
8578
8579 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8580 hashlen ) ) != PSA_SUCCESS )
8581 {
8582 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8583 goto exit;
8584 }
8585
8586exit:
8587 if( status != PSA_SUCCESS )
8588 {
8589 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8590 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8591 switch( status )
8592 {
8593 case PSA_ERROR_NOT_SUPPORTED:
8594 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8595 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8596 case PSA_ERROR_BUFFER_TOO_SMALL:
8597 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8598 case PSA_ERROR_INSUFFICIENT_MEMORY:
8599 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8600 default:
8601 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8602 }
8603 }
8604 return( 0 );
8605}
8606
8607#else
8608
8609int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8610 unsigned char *hash, size_t *hashlen,
8611 unsigned char *data, size_t data_len,
8612 mbedtls_md_type_t md_alg )
8613{
8614 int ret = 0;
8615 mbedtls_md_context_t ctx;
8616 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8617 *hashlen = mbedtls_md_get_size( md_info );
8618
8619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8620
8621 mbedtls_md_init( &ctx );
8622
8623 /*
8624 * digitally-signed struct {
8625 * opaque client_random[32];
8626 * opaque server_random[32];
8627 * ServerDHParams params;
8628 * };
8629 */
8630 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8631 {
8632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8633 goto exit;
8634 }
8635 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8636 {
8637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8638 goto exit;
8639 }
8640 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8641 {
8642 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8643 goto exit;
8644 }
8645 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8646 {
8647 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8648 goto exit;
8649 }
8650 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8651 {
8652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8653 goto exit;
8654 }
8655
8656exit:
8657 mbedtls_md_free( &ctx );
8658
8659 if( ret != 0 )
8660 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8661 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8662
8663 return( ret );
8664}
8665#endif /* MBEDTLS_USE_PSA_CRYPTO */
8666
Jerry Yud9d91da2022-02-17 14:57:06 +08008667#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8668
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008669/* Find the preferred hash for a given signature algorithm. */
8670unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8671 mbedtls_ssl_context *ssl,
8672 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008673{
Gabor Mezei078e8032022-04-27 21:17:56 +02008674 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008675 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008676
8677 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008678 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008679
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008680 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008681 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008682 unsigned int hash_alg_received =
8683 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8684 received_sig_algs[i] );
8685 unsigned int sig_alg_received =
8686 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8687 received_sig_algs[i] );
8688
8689 if( sig_alg == sig_alg_received )
8690 {
8691#if defined(MBEDTLS_USE_PSA_CRYPTO)
8692 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8693 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008694 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008695 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008696
Neil Armstrong96eceb82022-06-30 18:05:05 +02008697 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8698 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8699 PSA_ALG_ECDSA( psa_hash_alg ),
8700 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008701 continue;
8702
Neil Armstrong96eceb82022-06-30 18:05:05 +02008703 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008704 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8705 PSA_ALG_RSA_PKCS1V15_SIGN(
8706 psa_hash_alg ),
8707 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008708 continue;
8709 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008710#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008711
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008712 return( hash_alg_received );
8713 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008714 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008715
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008716 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008717}
8718
8719#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8720
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008721/* Serialization of TLS 1.2 sessions:
8722 *
8723 * struct {
8724 * uint64 start_time;
8725 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008726 * uint8 session_id_len; // at most 32
8727 * opaque session_id[32];
8728 * opaque master[48]; // fixed length in the standard
8729 * uint32 verify_result;
8730 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8731 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8732 * uint32 ticket_lifetime;
8733 * uint8 mfl_code; // up to 255 according to standard
8734 * uint8 encrypt_then_mac; // 0 or 1
8735 * } serialized_session_tls12;
8736 *
8737 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008738static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008739 unsigned char *buf,
8740 size_t buf_len )
8741{
8742 unsigned char *p = buf;
8743 size_t used = 0;
8744
8745#if defined(MBEDTLS_HAVE_TIME)
8746 uint64_t start;
8747#endif
8748#if defined(MBEDTLS_X509_CRT_PARSE_C)
8749#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8750 size_t cert_len;
8751#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8752#endif /* MBEDTLS_X509_CRT_PARSE_C */
8753
8754 /*
8755 * Time
8756 */
8757#if defined(MBEDTLS_HAVE_TIME)
8758 used += 8;
8759
8760 if( used <= buf_len )
8761 {
8762 start = (uint64_t) session->start;
8763
8764 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8765 p += 8;
8766 }
8767#endif /* MBEDTLS_HAVE_TIME */
8768
8769 /*
8770 * Basic mandatory fields
8771 */
8772 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008773 + 1 /* id_len */
8774 + sizeof( session->id )
8775 + sizeof( session->master )
8776 + 4; /* verify_result */
8777
8778 if( used <= buf_len )
8779 {
8780 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8781 p += 2;
8782
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008783 *p++ = MBEDTLS_BYTE_0( session->id_len );
8784 memcpy( p, session->id, 32 );
8785 p += 32;
8786
8787 memcpy( p, session->master, 48 );
8788 p += 48;
8789
8790 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8791 p += 4;
8792 }
8793
8794 /*
8795 * Peer's end-entity certificate
8796 */
8797#if defined(MBEDTLS_X509_CRT_PARSE_C)
8798#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8799 if( session->peer_cert == NULL )
8800 cert_len = 0;
8801 else
8802 cert_len = session->peer_cert->raw.len;
8803
8804 used += 3 + cert_len;
8805
8806 if( used <= buf_len )
8807 {
8808 *p++ = MBEDTLS_BYTE_2( cert_len );
8809 *p++ = MBEDTLS_BYTE_1( cert_len );
8810 *p++ = MBEDTLS_BYTE_0( cert_len );
8811
8812 if( session->peer_cert != NULL )
8813 {
8814 memcpy( p, session->peer_cert->raw.p, cert_len );
8815 p += cert_len;
8816 }
8817 }
8818#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8819 if( session->peer_cert_digest != NULL )
8820 {
8821 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8822 if( used <= buf_len )
8823 {
8824 *p++ = (unsigned char) session->peer_cert_digest_type;
8825 *p++ = (unsigned char) session->peer_cert_digest_len;
8826 memcpy( p, session->peer_cert_digest,
8827 session->peer_cert_digest_len );
8828 p += session->peer_cert_digest_len;
8829 }
8830 }
8831 else
8832 {
8833 used += 2;
8834 if( used <= buf_len )
8835 {
8836 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8837 *p++ = 0;
8838 }
8839 }
8840#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8841#endif /* MBEDTLS_X509_CRT_PARSE_C */
8842
8843 /*
8844 * Session ticket if any, plus associated data
8845 */
8846#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8847 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8848
8849 if( used <= buf_len )
8850 {
8851 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8852 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8853 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8854
8855 if( session->ticket != NULL )
8856 {
8857 memcpy( p, session->ticket, session->ticket_len );
8858 p += session->ticket_len;
8859 }
8860
8861 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8862 p += 4;
8863 }
8864#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8865
8866 /*
8867 * Misc extension-related info
8868 */
8869#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8870 used += 1;
8871
8872 if( used <= buf_len )
8873 *p++ = session->mfl_code;
8874#endif
8875
8876#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8877 used += 1;
8878
8879 if( used <= buf_len )
8880 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8881#endif
8882
8883 return( used );
8884}
8885
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008886MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008887static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008888 const unsigned char *buf,
8889 size_t len )
8890{
8891#if defined(MBEDTLS_HAVE_TIME)
8892 uint64_t start;
8893#endif
8894#if defined(MBEDTLS_X509_CRT_PARSE_C)
8895#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8896 size_t cert_len;
8897#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8898#endif /* MBEDTLS_X509_CRT_PARSE_C */
8899
8900 const unsigned char *p = buf;
8901 const unsigned char * const end = buf + len;
8902
8903 /*
8904 * Time
8905 */
8906#if defined(MBEDTLS_HAVE_TIME)
8907 if( 8 > (size_t)( end - p ) )
8908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8909
8910 start = ( (uint64_t) p[0] << 56 ) |
8911 ( (uint64_t) p[1] << 48 ) |
8912 ( (uint64_t) p[2] << 40 ) |
8913 ( (uint64_t) p[3] << 32 ) |
8914 ( (uint64_t) p[4] << 24 ) |
8915 ( (uint64_t) p[5] << 16 ) |
8916 ( (uint64_t) p[6] << 8 ) |
8917 ( (uint64_t) p[7] );
8918 p += 8;
8919
8920 session->start = (time_t) start;
8921#endif /* MBEDTLS_HAVE_TIME */
8922
8923 /*
8924 * Basic mandatory fields
8925 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008926 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008927 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8928
8929 session->ciphersuite = ( p[0] << 8 ) | p[1];
8930 p += 2;
8931
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008932 session->id_len = *p++;
8933 memcpy( session->id, p, 32 );
8934 p += 32;
8935
8936 memcpy( session->master, p, 48 );
8937 p += 48;
8938
8939 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8940 ( (uint32_t) p[1] << 16 ) |
8941 ( (uint32_t) p[2] << 8 ) |
8942 ( (uint32_t) p[3] );
8943 p += 4;
8944
8945 /* Immediately clear invalid pointer values that have been read, in case
8946 * we exit early before we replaced them with valid ones. */
8947#if defined(MBEDTLS_X509_CRT_PARSE_C)
8948#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8949 session->peer_cert = NULL;
8950#else
8951 session->peer_cert_digest = NULL;
8952#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8953#endif /* MBEDTLS_X509_CRT_PARSE_C */
8954#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8955 session->ticket = NULL;
8956#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8957
8958 /*
8959 * Peer certificate
8960 */
8961#if defined(MBEDTLS_X509_CRT_PARSE_C)
8962#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8963 /* Deserialize CRT from the end of the ticket. */
8964 if( 3 > (size_t)( end - p ) )
8965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8966
8967 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8968 p += 3;
8969
8970 if( cert_len != 0 )
8971 {
8972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8973
8974 if( cert_len > (size_t)( end - p ) )
8975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8976
8977 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8978
8979 if( session->peer_cert == NULL )
8980 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8981
8982 mbedtls_x509_crt_init( session->peer_cert );
8983
8984 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8985 p, cert_len ) ) != 0 )
8986 {
8987 mbedtls_x509_crt_free( session->peer_cert );
8988 mbedtls_free( session->peer_cert );
8989 session->peer_cert = NULL;
8990 return( ret );
8991 }
8992
8993 p += cert_len;
8994 }
8995#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8996 /* Deserialize CRT digest from the end of the ticket. */
8997 if( 2 > (size_t)( end - p ) )
8998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8999
9000 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9001 session->peer_cert_digest_len = (size_t) *p++;
9002
9003 if( session->peer_cert_digest_len != 0 )
9004 {
9005 const mbedtls_md_info_t *md_info =
9006 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9007 if( md_info == NULL )
9008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9009 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9011
9012 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9014
9015 session->peer_cert_digest =
9016 mbedtls_calloc( 1, session->peer_cert_digest_len );
9017 if( session->peer_cert_digest == NULL )
9018 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9019
9020 memcpy( session->peer_cert_digest, p,
9021 session->peer_cert_digest_len );
9022 p += session->peer_cert_digest_len;
9023 }
9024#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9025#endif /* MBEDTLS_X509_CRT_PARSE_C */
9026
9027 /*
9028 * Session ticket and associated data
9029 */
9030#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9031 if( 3 > (size_t)( end - p ) )
9032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9033
9034 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9035 p += 3;
9036
9037 if( session->ticket_len != 0 )
9038 {
9039 if( session->ticket_len > (size_t)( end - p ) )
9040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9041
9042 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9043 if( session->ticket == NULL )
9044 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9045
9046 memcpy( session->ticket, p, session->ticket_len );
9047 p += session->ticket_len;
9048 }
9049
9050 if( 4 > (size_t)( end - p ) )
9051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9052
9053 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9054 ( (uint32_t) p[1] << 16 ) |
9055 ( (uint32_t) p[2] << 8 ) |
9056 ( (uint32_t) p[3] );
9057 p += 4;
9058#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9059
9060 /*
9061 * Misc extension-related info
9062 */
9063#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9064 if( 1 > (size_t)( end - p ) )
9065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9066
9067 session->mfl_code = *p++;
9068#endif
9069
9070#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9071 if( 1 > (size_t)( end - p ) )
9072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9073
9074 session->encrypt_then_mac = *p++;
9075#endif
9076
9077 /* Done, should have consumed entire buffer */
9078 if( p != end )
9079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9080
9081 return( 0 );
9082}
Jerry Yudc7bd172022-02-17 13:44:15 +08009083#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9084
XiaokangQian75d40ef2022-04-20 11:05:24 +00009085int mbedtls_ssl_validate_ciphersuite(
9086 const mbedtls_ssl_context *ssl,
9087 const mbedtls_ssl_ciphersuite_t *suite_info,
9088 mbedtls_ssl_protocol_version min_tls_version,
9089 mbedtls_ssl_protocol_version max_tls_version )
9090{
9091 (void) ssl;
9092
9093 if( suite_info == NULL )
9094 return( -1 );
9095
9096 if( ( suite_info->min_tls_version > max_tls_version ) ||
9097 ( suite_info->max_tls_version < min_tls_version ) )
9098 {
9099 return( -1 );
9100 }
9101
XiaokangQian060d8672022-04-21 09:24:56 +00009102#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009103#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009104#if defined(MBEDTLS_USE_PSA_CRYPTO)
9105 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9106 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9107#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009108 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9109 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009110#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009111 {
9112 return( -1 );
9113 }
9114#endif
9115
9116 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9117#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9118 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9119 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9120 {
9121 return( -1 );
9122 }
9123#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9125
9126 return( 0 );
9127}
9128
Ronald Crone68ab4f2022-10-05 12:46:29 +02009129#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009130/*
9131 * Function for writing a signature algorithm extension.
9132 *
9133 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9134 * value (TLS 1.3 RFC8446):
9135 * enum {
9136 * ....
9137 * ecdsa_secp256r1_sha256( 0x0403 ),
9138 * ecdsa_secp384r1_sha384( 0x0503 ),
9139 * ecdsa_secp521r1_sha512( 0x0603 ),
9140 * ....
9141 * } SignatureScheme;
9142 *
9143 * struct {
9144 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9145 * } SignatureSchemeList;
9146 *
9147 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9148 * value (TLS 1.2 RFC5246):
9149 * enum {
9150 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9151 * sha512(6), (255)
9152 * } HashAlgorithm;
9153 *
9154 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9155 * SignatureAlgorithm;
9156 *
9157 * struct {
9158 * HashAlgorithm hash;
9159 * SignatureAlgorithm signature;
9160 * } SignatureAndHashAlgorithm;
9161 *
9162 * SignatureAndHashAlgorithm
9163 * supported_signature_algorithms<2..2^16-2>;
9164 *
9165 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9166 * generalization of the TLS 1.2 signature algorithm extension.
9167 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9168 * `SignatureScheme` field of TLS 1.3
9169 *
9170 */
9171int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9172 const unsigned char *end, size_t *out_len )
9173{
9174 unsigned char *p = buf;
9175 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9176 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9177
9178 *out_len = 0;
9179
9180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9181
9182 /* Check if we have space for header and length field:
9183 * - extension_type (2 bytes)
9184 * - extension_data_length (2 bytes)
9185 * - supported_signature_algorithms_length (2 bytes)
9186 */
9187 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9188 p += 6;
9189
9190 /*
9191 * Write supported_signature_algorithms
9192 */
9193 supported_sig_alg = p;
9194 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9195 if( sig_alg == NULL )
9196 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9197
9198 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9199 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9201 *sig_alg,
9202 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009203 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9204 continue;
9205 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9206 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9207 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009209 *sig_alg,
9210 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009211 }
9212
9213 /* Length of supported_signature_algorithms */
9214 supported_sig_alg_len = p - supported_sig_alg;
9215 if( supported_sig_alg_len == 0 )
9216 {
9217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9219 }
9220
XiaokangQianeaf36512022-04-24 09:07:44 +00009221 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009222 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009223 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9224
XiaokangQianeaf36512022-04-24 09:07:44 +00009225 *out_len = p - buf;
9226
9227#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009228 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009229#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009230
XiaokangQianeaf36512022-04-24 09:07:44 +00009231 return( 0 );
9232}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009233#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009234
XiaokangQian40a35232022-05-07 09:02:40 +00009235#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009236/*
9237 * mbedtls_ssl_parse_server_name_ext
9238 *
9239 * Structure of server_name extension:
9240 *
9241 * enum {
9242 * host_name(0), (255)
9243 * } NameType;
9244 * opaque HostName<1..2^16-1>;
9245 *
9246 * struct {
9247 * NameType name_type;
9248 * select (name_type) {
9249 * case host_name: HostName;
9250 * } name;
9251 * } ServerName;
9252 * struct {
9253 * ServerName server_name_list<1..2^16-1>
9254 * } ServerNameList;
9255 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009256MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009257int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9258 const unsigned char *buf,
9259 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009260{
9261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9262 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009263 size_t server_name_list_len, hostname_len;
9264 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009265
XiaokangQianf2a94202022-05-20 06:44:24 +00009266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009267
9268 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009269 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009270 p += 2;
9271
XiaokangQian9b2b7712022-05-17 02:57:00 +00009272 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9273 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009274 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009275 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009276 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009277 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009278 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9279 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009280
9281 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9282 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009283 /* sni_name is intended to be used only during the parsing of the
9284 * ClientHello message (it is reset to NULL before the end of
9285 * the message parsing). Thus it is ok to just point to the
9286 * reception buffer and not make a copy of it.
9287 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009288 ssl->handshake->sni_name = p + 3;
9289 ssl->handshake->sni_name_len = hostname_len;
9290 if( ssl->conf->f_sni == NULL )
9291 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009292 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009293 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009294 if( ret != 0 )
9295 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009296 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009297 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9298 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009299 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9300 }
9301 return( 0 );
9302 }
9303
9304 p += hostname_len + 3;
9305 }
9306
9307 return( 0 );
9308}
9309#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9310
XiaokangQianacb39922022-06-17 10:18:48 +00009311#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009312MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009313int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9314 const unsigned char *buf,
9315 const unsigned char *end )
9316{
9317 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009318 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009319 const unsigned char *protocol_name_list;
9320 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009321 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009322
9323 /* If ALPN not configured, just ignore the extension */
9324 if( ssl->conf->alpn_list == NULL )
9325 return( 0 );
9326
9327 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009328 * RFC7301, section 3.1
9329 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009330 *
XiaokangQianc7403452022-06-23 03:24:12 +00009331 * struct {
9332 * ProtocolName protocol_name_list<2..2^16-1>
9333 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009334 */
9335
XiaokangQianc7403452022-06-23 03:24:12 +00009336 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009337 * protocol_name_list_len 2 bytes
9338 * protocol_name_len 1 bytes
9339 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009340 */
XiaokangQianacb39922022-06-17 10:18:48 +00009341 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9342
XiaokangQianc7403452022-06-23 03:24:12 +00009343 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009344 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009345 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009346 protocol_name_list = p;
9347 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009348
9349 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009350 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009351 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009352 protocol_name_len = *p++;
9353 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9354 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009355 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009356 {
9357 MBEDTLS_SSL_PEND_FATAL_ALERT(
9358 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9359 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009360 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009361 }
9362
9363 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009364 }
9365
9366 /* Use our order of preference */
9367 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9368 {
9369 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009370 p = protocol_name_list;
9371 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009372 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009373 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009374 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009375 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009376 {
9377 ssl->alpn_chosen = *alpn;
9378 return( 0 );
9379 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009380
9381 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009382 }
9383 }
9384
XiaokangQian95d5f542022-06-24 02:29:26 +00009385 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009386 MBEDTLS_SSL_PEND_FATAL_ALERT(
9387 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9388 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9389 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9390}
9391
9392int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9393 unsigned char *buf,
9394 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009395 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009396{
9397 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009398 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009399 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009400
9401 if( ssl->alpn_chosen == NULL )
9402 {
9403 return( 0 );
9404 }
9405
XiaokangQian95d5f542022-06-24 02:29:26 +00009406 protocol_name_len = strlen( ssl->alpn_chosen );
9407 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009408
9409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9410 /*
9411 * 0 . 1 ext identifier
9412 * 2 . 3 ext length
9413 * 4 . 5 protocol list length
9414 * 6 . 6 protocol name length
9415 * 7 . 7+n protocol name
9416 */
9417 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9418
XiaokangQian95d5f542022-06-24 02:29:26 +00009419 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009420
XiaokangQian95d5f542022-06-24 02:29:26 +00009421 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9422 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009423 /* Note: the length of the chosen protocol has been checked to be less
9424 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9425 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009426 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009427
XiaokangQian95d5f542022-06-24 02:29:26 +00009428 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009429
9430#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9431 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9432#endif
9433
XiaokangQianacb39922022-06-17 10:18:48 +00009434 return ( 0 );
9435}
9436#endif /* MBEDTLS_SSL_ALPN */
9437
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009438#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009439 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009440 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009441 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009442int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9443 const char *hostname )
9444{
9445 /* Initialize to suppress unnecessary compiler warning */
9446 size_t hostname_len = 0;
9447
9448 /* Check if new hostname is valid before
9449 * making any change to current one */
9450 if( hostname != NULL )
9451 {
9452 hostname_len = strlen( hostname );
9453
9454 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9456 }
9457
9458 /* Now it's clear that we will overwrite the old hostname,
9459 * so we can free it safely */
9460 if( session->hostname != NULL )
9461 {
9462 mbedtls_platform_zeroize( session->hostname,
9463 strlen( session->hostname ) );
9464 mbedtls_free( session->hostname );
9465 }
9466
9467 /* Passing NULL as hostname shall clear the old one */
9468 if( hostname == NULL )
9469 {
9470 session->hostname = NULL;
9471 }
9472 else
9473 {
9474 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9475 if( session->hostname == NULL )
9476 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9477
9478 memcpy( session->hostname, hostname, hostname_len );
9479 }
9480
9481 return( 0 );
9482}
Xiaokang Qian03409292022-10-12 02:49:52 +00009483#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9484 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009485 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009486 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#endif /* MBEDTLS_SSL_TLS_C */