blob: 062259b5cd40412f30ecb339539adbdd4c60449b [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#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020057#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254 if( src->peer_cert != NULL )
255 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200257
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200258 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200265 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 dst->peer_cert = NULL;
269 return( ret );
270 }
271 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 if( src->peer_cert_digest != NULL )
274 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 if( dst->peer_cert_digest == NULL )
278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
279
280 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
281 src->peer_cert_digest_len );
282 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000283 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 }
285#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200289#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290 if( src->ticket != NULL )
291 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200293 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 memcpy( dst->ticket, src->ticket, src->ticket_len );
297 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000300#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
301 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
302 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000303 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000304 {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000305 dst->hostname = NULL;
306 mbedtls_ssl_session_set_hostname( dst,
307 src->hostname );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000308 }
309#endif
310
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200311 return( 0 );
312}
313
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500316static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
317{
318 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
319 if( resized_buffer == NULL )
320 return -1;
321
322 /* We want to copy len_new bytes when downsizing the buffer, and
323 * len_old bytes when upsizing, so we choose the smaller of two sizes,
324 * to fit one buffer into another. Size checks, ensuring that no data is
325 * lost, are done outside of this function. */
326 memcpy( resized_buffer, *buffer,
327 ( len_new < *len_old ) ? len_new : *len_old );
328 mbedtls_platform_zeroize( *buffer, *len_old );
329 mbedtls_free( *buffer );
330
331 *buffer = resized_buffer;
332 *len_old = len_new;
333
334 return 0;
335}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336
337static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500338 size_t in_buf_new_len,
339 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200340{
341 int modified = 0;
342 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
343 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
344 if( ssl->in_buf != NULL )
345 {
346 written_in = ssl->in_msg - ssl->in_buf;
347 iv_offset_in = ssl->in_iv - ssl->in_buf;
348 len_offset_in = ssl->in_len - ssl->in_buf;
349 if( downsizing ?
350 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
351 ssl->in_buf_len < in_buf_new_len )
352 {
353 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
354 {
355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
356 }
357 else
358 {
Paul Elliottb7449902021-03-10 18:14:58 +0000359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
360 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200361 modified = 1;
362 }
363 }
364 }
365
366 if( ssl->out_buf != NULL )
367 {
368 written_out = ssl->out_msg - ssl->out_buf;
369 iv_offset_out = ssl->out_iv - ssl->out_buf;
370 len_offset_out = ssl->out_len - ssl->out_buf;
371 if( downsizing ?
372 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
373 ssl->out_buf_len < out_buf_new_len )
374 {
375 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
376 {
377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
378 }
379 else
380 {
Paul Elliottb7449902021-03-10 18:14:58 +0000381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
382 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200383 modified = 1;
384 }
385 }
386 }
387 if( modified )
388 {
389 /* Update pointers here to avoid doing it twice. */
390 mbedtls_ssl_reset_in_out_pointers( ssl );
391 /* Fields below might not be properly updated with record
392 * splitting or with CID, so they are manually updated here. */
393 ssl->out_msg = ssl->out_buf + written_out;
394 ssl->out_len = ssl->out_buf + len_offset_out;
395 ssl->out_iv = ssl->out_buf + iv_offset_out;
396
397 ssl->in_msg = ssl->in_buf + written_in;
398 ssl->in_len = ssl->in_buf + len_offset_in;
399 ssl->in_iv = ssl->in_buf + iv_offset_in;
400 }
401}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500402#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
403
Jerry Yudb8c48a2022-01-27 14:54:54 +0800404#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800405
406#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
407typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
408 const char *label,
409 const unsigned char *random, size_t rlen,
410 unsigned char *dstbuf, size_t dlen );
411
412static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
413
414#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
415
416/* Type for the TLS PRF */
417typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
418 const unsigned char *, size_t,
419 unsigned char *, size_t);
420
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200421MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800422static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
423 int ciphersuite,
424 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200425#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800426 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200427#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800428 ssl_tls_prf_t tls_prf,
429 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400430 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800431 unsigned endpoint,
432 const mbedtls_ssl_context *ssl );
433
Andrzej Kurek25f27152022-08-17 16:09:31 -0400434#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200435MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800436static int tls_prf_sha256( const unsigned char *secret, size_t slen,
437 const char *label,
438 const unsigned char *random, size_t rlen,
439 unsigned char *dstbuf, size_t dlen );
440static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
441static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
442
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400443#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800444
Andrzej Kurek25f27152022-08-17 16:09:31 -0400445#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200446MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800447static int tls_prf_sha384( const unsigned char *secret, size_t slen,
448 const char *label,
449 const unsigned char *random, size_t rlen,
450 unsigned char *dstbuf, size_t dlen );
451
452static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
453static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400454#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800455
Jerry Yu438ddd82022-07-07 06:55:50 +0000456static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800457 unsigned char *buf,
458 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800459
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200460MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000461static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800462 const unsigned char *buf,
463 size_t len );
464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
465
Jerry Yu53d23e22022-02-09 16:25:09 +0800466static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800471
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800473static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400474#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475
Ron Eldor51d3ab52019-05-12 14:54:30 +0300476int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
477 const unsigned char *secret, size_t slen,
478 const char *label,
479 const unsigned char *random, size_t rlen,
480 unsigned char *dstbuf, size_t dlen )
481{
482 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
483
484 switch( prf )
485 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400487#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300488 case MBEDTLS_SSL_TLS_PRF_SHA384:
489 tls_prf = tls_prf_sha384;
490 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400491#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400492#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300493 case MBEDTLS_SSL_TLS_PRF_SHA256:
494 tls_prf = tls_prf_sha256;
495 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400496#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300498 default:
499 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
500 }
501
502 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
503}
504
Jerry Yuc73c6182022-02-08 20:29:25 +0800505#if defined(MBEDTLS_X509_CRT_PARSE_C)
506static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
507{
508#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
509 if( session->peer_cert != NULL )
510 {
511 mbedtls_x509_crt_free( session->peer_cert );
512 mbedtls_free( session->peer_cert );
513 session->peer_cert = NULL;
514 }
515#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
516 if( session->peer_cert_digest != NULL )
517 {
518 /* Zeroization is not necessary. */
519 mbedtls_free( session->peer_cert_digest );
520 session->peer_cert_digest = NULL;
521 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
522 session->peer_cert_digest_len = 0;
523 }
524#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
525}
526#endif /* MBEDTLS_X509_CRT_PARSE_C */
527
528void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
529 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
530{
531 ((void) ciphersuite_info);
532
Andrzej Kurek25f27152022-08-17 16:09:31 -0400533#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800534 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
535 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
536 else
537#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400538#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800539 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
540 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
541 else
542#endif
543 {
544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
545 return;
546 }
547}
548
XiaokangQianadab9a62022-07-18 07:41:26 +0000549void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
550 unsigned hs_type,
551 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100552{
553 unsigned char hs_hdr[4];
554
555 /* Build HS header for checksum update. */
556 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
557 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
558 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
559 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
560
561 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
562}
563
564void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
565 unsigned hs_type,
566 unsigned char const *msg,
567 size_t msg_len )
568{
569 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
570 ssl->handshake->update_checksum( ssl, msg, msg_len );
571}
572
Jerry Yuc73c6182022-02-08 20:29:25 +0800573void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
574{
575 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400576#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800577#if defined(MBEDTLS_USE_PSA_CRYPTO)
578 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
579 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
580#else
581 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
582#endif
583#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400584#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800585#if defined(MBEDTLS_USE_PSA_CRYPTO)
586 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
587 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
588#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400589 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800590#endif
591#endif
592}
593
594static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
595 const unsigned char *buf, size_t len )
596{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400597#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800598#if defined(MBEDTLS_USE_PSA_CRYPTO)
599 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
600#else
601 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
602#endif
603#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400604#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800605#if defined(MBEDTLS_USE_PSA_CRYPTO)
606 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
607#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400608 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800609#endif
610#endif
611}
612
Andrzej Kurek25f27152022-08-17 16:09:31 -0400613#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800614static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
615 const unsigned char *buf, size_t len )
616{
617#if defined(MBEDTLS_USE_PSA_CRYPTO)
618 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
619#else
620 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
621#endif
622}
623#endif
624
Andrzej Kurek25f27152022-08-17 16:09:31 -0400625#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800626static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
627 const unsigned char *buf, size_t len )
628{
629#if defined(MBEDTLS_USE_PSA_CRYPTO)
630 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
631#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400632 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800633#endif
634}
635#endif
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640
Andrzej Kurek25f27152022-08-17 16:09:31 -0400641#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500642#if defined(MBEDTLS_USE_PSA_CRYPTO)
643 handshake->fin_sha256_psa = psa_hash_operation_init();
644 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
645#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200647 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500649#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400650#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500651#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500652 handshake->fin_sha384_psa = psa_hash_operation_init();
653 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500654#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400655 mbedtls_sha512_init( &handshake->fin_sha384 );
656 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200657#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500658#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659
660 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_DHM_C)
663 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200664#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200665#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200667#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200668#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200669 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200670#if defined(MBEDTLS_SSL_CLI_C)
671 handshake->ecjpake_cache = NULL;
672 handshake->ecjpake_cache_len = 0;
673#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200674#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200675
Gilles Peskineeccd8882020-03-10 12:19:08 +0100676#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200677 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200678#endif
679
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200680#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
681 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
682#endif
Hanno Becker75173122019-02-06 16:18:31 +0000683
684#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
685 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
686 mbedtls_pk_init( &handshake->peer_pubkey );
687#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200688}
689
Hanno Beckera18d1322018-01-03 14:27:32 +0000690void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200693
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100694#if defined(MBEDTLS_USE_PSA_CRYPTO)
695 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
696 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100697#else
698 mbedtls_cipher_init( &transform->cipher_ctx_enc );
699 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100700#endif
701
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000702#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100703#if defined(MBEDTLS_USE_PSA_CRYPTO)
704 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
705 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100706#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_md_init( &transform->md_ctx_enc );
708 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000709#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100710#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200711}
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716}
717
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200718MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000720{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200721 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000722 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200724 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200726 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200727 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728
729 /*
730 * Either the pointers are now NULL or cleared properly and can be freed.
731 * Now allocate missing structures.
732 */
733 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200734 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200735 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 }
Paul Bakker48916f92012-09-16 19:57:18 +0000737
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200738 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200740 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 }
Paul Bakker48916f92012-09-16 19:57:18 +0000742
Paul Bakker82788fb2014-10-20 13:59:19 +0200743 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200744 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200745 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500747#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
748 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400749
Andrzej Kurek4a063792020-10-21 15:08:44 +0200750 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
751 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500752#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000753
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200754 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000755 if( ssl->handshake == NULL ||
756 ssl->transform_negotiate == NULL ||
757 ssl->session_negotiate == NULL )
758 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_free( ssl->handshake );
762 mbedtls_free( ssl->transform_negotiate );
763 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200764
765 ssl->handshake = NULL;
766 ssl->transform_negotiate = NULL;
767 ssl->session_negotiate = NULL;
768
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200769 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000770 }
771
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200772 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000774 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200775 ssl_handshake_params_init( ssl->handshake );
776
Jerry Yud0766ec2022-09-22 10:46:57 +0800777#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
778 defined(MBEDTLS_SSL_SRV_C) && \
779 defined(MBEDTLS_SSL_SESSION_TICKETS)
780 ssl->handshake->new_session_tickets_count =
781 ssl->conf->new_session_tickets_count ;
782#endif
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200785 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
786 {
787 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200788
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200789 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
790 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
791 else
792 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200793
Hanno Becker0f57a652020-02-05 10:37:26 +0000794 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200795 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200796#endif
797
Brett Warrene0edc842021-08-17 09:53:13 +0100798/*
799 * curve_list is translated to IANA TLS group identifiers here because
800 * mbedtls_ssl_conf_curves returns void and so can't return
801 * any error codes.
802 */
803#if defined(MBEDTLS_ECP_C)
804#if !defined(MBEDTLS_DEPRECATED_REMOVED)
805 /* Heap allocate and translate curve_list from internal to IANA group ids */
806 if ( ssl->conf->curve_list != NULL )
807 {
808 size_t length;
809 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
810
811 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
812 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
813
814 /* Leave room for zero termination */
815 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
816 if ( group_list == NULL )
817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
818
819 for( size_t i = 0; i < length; i++ )
820 {
821 const mbedtls_ecp_curve_info *info =
822 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
823 if ( info == NULL )
824 {
825 mbedtls_free( group_list );
826 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
827 }
828 group_list[i] = info->tls_id;
829 }
830
831 group_list[length] = 0;
832
833 ssl->handshake->group_list = group_list;
834 ssl->handshake->group_list_heap_allocated = 1;
835 }
836 else
837 {
838 ssl->handshake->group_list = ssl->conf->group_list;
839 ssl->handshake->group_list_heap_allocated = 0;
840 }
841#endif /* MBEDTLS_DEPRECATED_REMOVED */
842#endif /* MBEDTLS_ECP_C */
843
Jerry Yuf017ee42022-01-12 15:49:48 +0800844#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
845#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800846#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800847 /* Heap allocate and translate sig_hashes from internal hash identifiers to
848 signature algorithms IANA identifiers. */
849 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800850 ssl->conf->sig_hashes != NULL )
851 {
852 const int *md;
853 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800854 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800855 uint16_t *p;
856
Jerry Yub476a442022-01-21 18:14:45 +0800857#if defined(static_assert)
858 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
859 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
860 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800861#endif
862
Jerry Yuf017ee42022-01-12 15:49:48 +0800863 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
864 {
865 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
866 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800867#if defined(MBEDTLS_ECDSA_C)
868 sig_algs_len += sizeof( uint16_t );
869#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800870
Jerry Yub476a442022-01-21 18:14:45 +0800871#if defined(MBEDTLS_RSA_C)
872 sig_algs_len += sizeof( uint16_t );
873#endif
874 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
875 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800876 }
877
Jerry Yub476a442022-01-21 18:14:45 +0800878 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800879 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
880
Jerry Yub476a442022-01-21 18:14:45 +0800881 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
882 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800883 if( ssl->handshake->sig_algs == NULL )
884 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
885
886 p = (uint16_t *)ssl->handshake->sig_algs;
887 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
888 {
889 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
890 if( hash == MBEDTLS_SSL_HASH_NONE )
891 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800892#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800893 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
894 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800895#endif
896#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800897 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
898 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800899#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800900 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200901 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 ssl->handshake->sig_algs_heap_allocated = 1;
903 }
904 else
Jerry Yua69269a2022-01-17 21:06:01 +0800905#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800906 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800907 ssl->handshake->sig_algs_heap_allocated = 0;
908 }
Jerry Yucc539102022-06-27 16:27:35 +0800909#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800910#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000911 return( 0 );
912}
913
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200914#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200915/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200916MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917static int ssl_cookie_write_dummy( void *ctx,
918 unsigned char **p, unsigned char *end,
919 const unsigned char *cli_id, size_t cli_id_len )
920{
921 ((void) ctx);
922 ((void) p);
923 ((void) end);
924 ((void) cli_id);
925 ((void) cli_id_len);
926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200928}
929
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200930MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200931static int ssl_cookie_check_dummy( void *ctx,
932 const unsigned char *cookie, size_t cookie_len,
933 const unsigned char *cli_id, size_t cli_id_len )
934{
935 ((void) ctx);
936 ((void) cookie);
937 ((void) cookie_len);
938 ((void) cli_id);
939 ((void) cli_id_len);
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200942}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200943#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200944
Paul Bakker5121ce52009-01-03 21:22:43 +0000945/*
946 * Initialize an SSL context
947 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200948void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
949{
950 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
951}
952
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200953MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800954static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
955{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100956 const mbedtls_ssl_config *conf = ssl->conf;
957
Ronald Cron6f135e12021-12-08 16:57:54 +0100958#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100959 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
960 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000961 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
962 {
963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
964 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
965 }
966
Jerry Yu60835a82021-08-04 10:13:52 +0800967 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
968 return( 0 );
969 }
970#endif
971
972#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100973 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800974 {
975 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
976 return( 0 );
977 }
978#endif
979
Ronald Cron6f135e12021-12-08 16:57:54 +0100980#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100981 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800982 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
984 {
985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
986 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
987 }
988
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000989 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
990 {
991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
992 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
993 }
994
995
Ronald Crone1d3f062022-02-10 14:50:54 +0100996 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
997 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800998 }
999#endif
1000
1001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1002 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1003}
1004
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001005MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001006static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1007{
1008 int ret;
1009 ret = ssl_conf_version_check( ssl );
1010 if( ret != 0 )
1011 return( ret );
1012
1013 /* Space for further checks */
1014
1015 return( 0 );
1016}
1017
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001018/*
1019 * Setup an SSL context
1020 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001021
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001022int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001023 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001024{
Janos Follath865b3eb2019-12-16 11:46:15 +00001025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001026 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1027 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001029 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001030
Jerry Yu60835a82021-08-04 10:13:52 +08001031 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1032 return( ret );
1033
Paul Bakker62f2dee2012-09-28 07:31:51 +00001034 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001035 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001036 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001037
1038 /* Set to NULL in case of an error condition */
1039 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001040
Darryl Greenb33cc762019-11-28 14:29:44 +00001041#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1042 ssl->in_buf_len = in_buf_len;
1043#endif
1044 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001045 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001048 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001049 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001050 }
1051
Darryl Greenb33cc762019-11-28 14:29:44 +00001052#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1053 ssl->out_buf_len = out_buf_len;
1054#endif
1055 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001056 if( ssl->out_buf == NULL )
1057 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001059 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001060 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 }
1062
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001063 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001064
Johan Pascalb62bb512015-12-03 21:56:45 +01001065#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001066 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001067#endif
1068
Paul Bakker48916f92012-09-16 19:57:18 +00001069 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001070 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
1072 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001073
1074error:
1075 mbedtls_free( ssl->in_buf );
1076 mbedtls_free( ssl->out_buf );
1077
1078 ssl->conf = NULL;
1079
Darryl Greenb33cc762019-11-28 14:29:44 +00001080#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1081 ssl->in_buf_len = 0;
1082 ssl->out_buf_len = 0;
1083#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001084 ssl->in_buf = NULL;
1085 ssl->out_buf = NULL;
1086
1087 ssl->in_hdr = NULL;
1088 ssl->in_ctr = NULL;
1089 ssl->in_len = NULL;
1090 ssl->in_iv = NULL;
1091 ssl->in_msg = NULL;
1092
1093 ssl->out_hdr = NULL;
1094 ssl->out_ctr = NULL;
1095 ssl->out_len = NULL;
1096 ssl->out_iv = NULL;
1097 ssl->out_msg = NULL;
1098
k-stachowiak9f7798e2018-07-31 16:52:32 +02001099 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001100}
1101
1102/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001103 * Reset an initialized and used SSL context for re-use while retaining
1104 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001105 *
1106 * If partial is non-zero, keep data in the input buffer and client ID.
1107 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001108 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001109void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1110 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001111{
Darryl Greenb33cc762019-11-28 14:29:44 +00001112#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1113 size_t in_buf_len = ssl->in_buf_len;
1114 size_t out_buf_len = ssl->out_buf_len;
1115#else
1116 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1117 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1118#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001119
Hanno Beckerb0302c42021-08-03 09:39:42 +01001120#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1121 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001122#endif
1123
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001124 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001125 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001126
Hanno Beckerb0302c42021-08-03 09:39:42 +01001127 mbedtls_ssl_reset_in_out_pointers( ssl );
1128
1129 /* Reset incoming message parsing */
1130 ssl->in_offt = NULL;
1131 ssl->nb_zero = 0;
1132 ssl->in_msgtype = 0;
1133 ssl->in_msglen = 0;
1134 ssl->in_hslen = 0;
1135 ssl->keep_current_message = 0;
1136 ssl->transform_in = NULL;
1137
1138#if defined(MBEDTLS_SSL_PROTO_DTLS)
1139 ssl->next_record_offset = 0;
1140 ssl->in_epoch = 0;
1141#endif
1142
1143 /* Keep current datagram if partial == 1 */
1144 if( partial == 0 )
1145 {
1146 ssl->in_left = 0;
1147 memset( ssl->in_buf, 0, in_buf_len );
1148 }
1149
Ronald Cronad8c17b2022-06-10 17:18:09 +02001150 ssl->send_alert = 0;
1151
Hanno Beckerb0302c42021-08-03 09:39:42 +01001152 /* Reset outgoing message writing */
1153 ssl->out_msgtype = 0;
1154 ssl->out_msglen = 0;
1155 ssl->out_left = 0;
1156 memset( ssl->out_buf, 0, out_buf_len );
1157 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1158 ssl->transform_out = NULL;
1159
1160#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1161 mbedtls_ssl_dtls_replay_reset( ssl );
1162#endif
1163
XiaokangQian2b01dc32022-01-21 02:53:13 +00001164#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001165 if( ssl->transform )
1166 {
1167 mbedtls_ssl_transform_free( ssl->transform );
1168 mbedtls_free( ssl->transform );
1169 ssl->transform = NULL;
1170 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1172
XiaokangQian2b01dc32022-01-21 02:53:13 +00001173#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1174 mbedtls_ssl_transform_free( ssl->transform_application );
1175 mbedtls_free( ssl->transform_application );
1176 ssl->transform_application = NULL;
1177
1178 if( ssl->handshake != NULL )
1179 {
1180 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1181 mbedtls_free( ssl->handshake->transform_earlydata );
1182 ssl->handshake->transform_earlydata = NULL;
1183
1184 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1185 mbedtls_free( ssl->handshake->transform_handshake );
1186 ssl->handshake->transform_handshake = NULL;
1187 }
1188
XiaokangQian2b01dc32022-01-21 02:53:13 +00001189#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001190}
1191
1192int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1193{
1194 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1195
1196 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1197
XiaokangQian78b1fa72022-01-19 06:56:30 +00001198 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001199
1200 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#if defined(MBEDTLS_SSL_RENEGOTIATION)
1202 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001203 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001204
1205 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1207 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001210
Hanno Beckerb0302c42021-08-03 09:39:42 +01001211 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001212 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001213 if( ssl->session )
1214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_ssl_session_free( ssl->session );
1216 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001217 ssl->session = NULL;
1218 }
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001221 ssl->alpn_chosen = NULL;
1222#endif
1223
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001224#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001225#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001226 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001227#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001228 {
1229 mbedtls_free( ssl->cli_id );
1230 ssl->cli_id = NULL;
1231 ssl->cli_id_len = 0;
1232 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001233#endif
1234
Paul Bakker48916f92012-09-16 19:57:18 +00001235 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1236 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001237
1238 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001239}
1240
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001241/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001242 * Reset an initialized and used SSL context for re-use while retaining
1243 * all application-set variables, function pointers and data.
1244 */
1245int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1246{
Hanno Becker43aefe22020-02-05 10:44:56 +00001247 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001248}
1249
1250/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 * SSL set accessors
1252 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001253void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001255 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256}
1257
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001258void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001259{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001260 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001261}
1262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001264void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001266 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001267}
1268#endif
1269
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001270void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001271{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001272 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001273}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001276
Hanno Becker1841b0a2018-08-24 11:13:57 +01001277void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1278 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001279{
1280 ssl->disable_datagram_packing = !allow_packing;
1281}
1282
1283void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1284 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001285{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001286 conf->hs_timeout_min = min;
1287 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001288}
1289#endif
1290
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001291void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001293 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001294}
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001298 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001299 void *p_vrfy )
1300{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001301 conf->f_vrfy = f_vrfy;
1302 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001305
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001306void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001307 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 void *p_rng )
1309{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001310 conf->f_rng = f_rng;
1311 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312}
1313
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001314void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001315 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 void *p_dbg )
1317{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001318 conf->f_dbg = f_dbg;
1319 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320}
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001323 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001324 mbedtls_ssl_send_t *f_send,
1325 mbedtls_ssl_recv_t *f_recv,
1326 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001327{
1328 ssl->p_bio = p_bio;
1329 ssl->f_send = f_send;
1330 ssl->f_recv = f_recv;
1331 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001332}
1333
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001334#if defined(MBEDTLS_SSL_PROTO_DTLS)
1335void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1336{
1337 ssl->mtu = mtu;
1338}
1339#endif
1340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001341void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001342{
1343 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001344}
1345
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001346void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1347 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001348 mbedtls_ssl_set_timer_t *f_set_timer,
1349 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001350{
1351 ssl->p_timer = p_timer;
1352 ssl->f_set_timer = f_set_timer;
1353 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001354
1355 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001356 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001357}
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001360void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001361 void *p_cache,
1362 mbedtls_ssl_cache_get_t *f_get_cache,
1363 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001365 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001366 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001367 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_SSL_CLI_C)
1372int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373{
Janos Follath865b3eb2019-12-16 11:46:15 +00001374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001375
1376 if( ssl == NULL ||
1377 session == NULL ||
1378 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001379 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001382 }
1383
Hanno Beckere810bbc2021-05-14 16:01:05 +01001384 if( ssl->handshake->resume == 1 )
1385 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1386
Jerry Yu21092062022-10-10 21:21:31 +08001387#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1388 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001389 {
Jerry Yu21092062022-10-10 21:21:31 +08001390 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1391 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1392
1393 if( mbedtls_ssl_validate_ciphersuite(
1394 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1395 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1396 {
1397 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1398 session->ciphersuite ) );
1399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1400 }
Jerry Yu40afab62022-10-08 10:42:13 +08001401 }
Jerry Yu21092062022-10-10 21:21:31 +08001402#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001403
Hanno Becker52055ae2019-02-06 14:30:46 +00001404 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1405 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001406 return( ret );
1407
Paul Bakker0a597072012-09-25 21:55:46 +00001408 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001409
1410 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001414void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1415 const int *ciphersuites )
1416{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001417 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001418}
1419
Ronald Cron6f135e12021-12-08 16:57:54 +01001420#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001421void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001422 const int kex_modes )
1423{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001424 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001425}
Ronald Cron6f135e12021-12-08 16:57:54 +01001426#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001429void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001430 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001431{
1432 conf->cert_profile = profile;
1433}
1434
Glenn Strauss36872db2022-01-22 05:06:31 -05001435static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1436{
1437 mbedtls_ssl_key_cert *cur = key_cert, *next;
1438
1439 while( cur != NULL )
1440 {
1441 next = cur->next;
1442 mbedtls_free( cur );
1443 cur = next;
1444 }
1445}
1446
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001447/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001448MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001449static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1450 mbedtls_x509_crt *cert,
1451 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001452{
niisato8ee24222018-06-25 19:05:48 +09001453 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001454
Glenn Strauss36872db2022-01-22 05:06:31 -05001455 if( cert == NULL )
1456 {
1457 /* Free list if cert is null */
1458 ssl_key_cert_free( *head );
1459 *head = NULL;
1460 return( 0 );
1461 }
1462
niisato8ee24222018-06-25 19:05:48 +09001463 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1464 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001465 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001466
niisato8ee24222018-06-25 19:05:48 +09001467 new_cert->cert = cert;
1468 new_cert->key = key;
1469 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001470
Glenn Strauss36872db2022-01-22 05:06:31 -05001471 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001472 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001473 {
niisato8ee24222018-06-25 19:05:48 +09001474 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001475 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001476 else
1477 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001478 mbedtls_ssl_key_cert *cur = *head;
1479 while( cur->next != NULL )
1480 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001481 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001482 }
1483
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001484 return( 0 );
1485}
1486
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001487int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001488 mbedtls_x509_crt *own_cert,
1489 mbedtls_pk_context *pk_key )
1490{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001491 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001492}
1493
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001494void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001495 mbedtls_x509_crt *ca_chain,
1496 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001498 conf->ca_chain = ca_chain;
1499 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001500
1501#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1502 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1503 * cannot be used together. */
1504 conf->f_ca_cb = NULL;
1505 conf->p_ca_cb = NULL;
1506#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001507}
Hanno Becker5adaad92019-03-27 16:54:37 +00001508
1509#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1510void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001511 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001512 void *p_ca_cb )
1513{
1514 conf->f_ca_cb = f_ca_cb;
1515 conf->p_ca_cb = p_ca_cb;
1516
1517 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1518 * cannot be used together. */
1519 conf->ca_chain = NULL;
1520 conf->ca_crl = NULL;
1521}
1522#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001524
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001525#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001526const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1527 size_t *name_len )
1528{
1529 *name_len = ssl->handshake->sni_name_len;
1530 return( ssl->handshake->sni_name );
1531}
1532
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001533int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1534 mbedtls_x509_crt *own_cert,
1535 mbedtls_pk_context *pk_key )
1536{
1537 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1538 own_cert, pk_key ) );
1539}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001540
1541void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1542 mbedtls_x509_crt *ca_chain,
1543 mbedtls_x509_crl *ca_crl )
1544{
1545 ssl->handshake->sni_ca_chain = ca_chain;
1546 ssl->handshake->sni_ca_crl = ca_crl;
1547}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001548
Glenn Strauss999ef702022-03-11 01:37:23 -05001549#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1550void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1551 const mbedtls_x509_crt *crt)
1552{
1553 ssl->handshake->dn_hints = crt;
1554}
1555#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1556
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001557void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1558 int authmode )
1559{
1560 ssl->handshake->sni_authmode = authmode;
1561}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001562#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1563
Hanno Becker8927c832019-04-03 12:52:50 +01001564#if defined(MBEDTLS_X509_CRT_PARSE_C)
1565void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1566 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1567 void *p_vrfy )
1568{
1569 ssl->f_vrfy = f_vrfy;
1570 ssl->p_vrfy = p_vrfy;
1571}
1572#endif
1573
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001574#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001575/*
1576 * Set EC J-PAKE password for current handshake
1577 */
1578int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1579 const unsigned char *pw,
1580 size_t pw_len )
1581{
1582 mbedtls_ecjpake_role role;
1583
Janos Follath8eb64132016-06-03 15:40:57 +01001584 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1586
1587 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1588 role = MBEDTLS_ECJPAKE_SERVER;
1589 else
1590 role = MBEDTLS_ECJPAKE_CLIENT;
1591
1592 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1593 role,
1594 MBEDTLS_MD_SHA256,
1595 MBEDTLS_ECP_DP_SECP256R1,
1596 pw, pw_len ) );
1597}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001598#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001599
Gilles Peskineeccd8882020-03-10 12:19:08 +01001600#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001601
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001602MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001603static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1604{
1605#if defined(MBEDTLS_USE_PSA_CRYPTO)
1606 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1607 return( 1 );
1608#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001609 if( conf->psk != NULL )
1610 return( 1 );
1611
1612 return( 0 );
1613}
1614
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001615static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1616{
1617 /* Remove reference to existing PSK, if any. */
1618#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001620 {
1621 /* The maintenance of the PSK key slot is the
1622 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001623 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001624 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001625#endif /* MBEDTLS_USE_PSA_CRYPTO */
1626 if( conf->psk != NULL )
1627 {
1628 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1629
1630 mbedtls_free( conf->psk );
1631 conf->psk = NULL;
1632 conf->psk_len = 0;
1633 }
1634
1635 /* Remove reference to PSK identity, if any. */
1636 if( conf->psk_identity != NULL )
1637 {
1638 mbedtls_free( conf->psk_identity );
1639 conf->psk_identity = NULL;
1640 conf->psk_identity_len = 0;
1641 }
1642}
1643
Hanno Becker7390c712018-11-15 13:33:04 +00001644/* This function assumes that PSK identity in the SSL config is unset.
1645 * It checks that the provided identity is well-formed and attempts
1646 * to make a copy of it in the SSL config.
1647 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001648MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001649static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1650 unsigned char const *psk_identity,
1651 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001652{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001653 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001654 if( psk_identity == NULL ||
1655 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001656 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001657 {
1658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1659 }
1660
Hanno Becker7390c712018-11-15 13:33:04 +00001661 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1662 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001663 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001664
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001665 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001666 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001667
1668 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001669}
1670
Hanno Becker7390c712018-11-15 13:33:04 +00001671int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1672 const unsigned char *psk, size_t psk_len,
1673 const unsigned char *psk_identity, size_t psk_identity_len )
1674{
Janos Follath865b3eb2019-12-16 11:46:15 +00001675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001676
1677 /* We currently only support one PSK, raw or opaque. */
1678 if( ssl_conf_psk_is_configured( conf ) )
1679 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001680
1681 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001682 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001684 if( psk_len == 0 )
1685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1686 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1688
Hanno Becker7390c712018-11-15 13:33:04 +00001689 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1690 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1691 conf->psk_len = psk_len;
1692 memcpy( conf->psk, psk, conf->psk_len );
1693
1694 /* Check and set PSK Identity */
1695 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1696 if( ret != 0 )
1697 ssl_conf_remove_psk( conf );
1698
1699 return( ret );
1700}
1701
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001702static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1703{
1704#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001705 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001706 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001707 /* The maintenance of the external PSK key slot is the
1708 * user's responsibility. */
1709 if( ssl->handshake->psk_opaque_is_internal )
1710 {
1711 psa_destroy_key( ssl->handshake->psk_opaque );
1712 ssl->handshake->psk_opaque_is_internal = 0;
1713 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001714 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001715 }
Neil Armstronge952a302022-05-03 10:22:14 +02001716#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001717 if( ssl->handshake->psk != NULL )
1718 {
1719 mbedtls_platform_zeroize( ssl->handshake->psk,
1720 ssl->handshake->psk_len );
1721 mbedtls_free( ssl->handshake->psk );
1722 ssl->handshake->psk_len = 0;
1723 }
Neil Armstronge952a302022-05-03 10:22:14 +02001724#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001725}
1726
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001727int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1728 const unsigned char *psk, size_t psk_len )
1729{
Neil Armstrong501c9322022-05-03 09:35:09 +02001730#if defined(MBEDTLS_USE_PSA_CRYPTO)
1731 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001733 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001734 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001735#endif /* MBEDTLS_USE_PSA_CRYPTO */
1736
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001737 if( psk == NULL || ssl->handshake == NULL )
1738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1739
1740 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1742
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001743 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001744
Neil Armstrong501c9322022-05-03 09:35:09 +02001745#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001746#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1747 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1748 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001749 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001750 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1751 else
1752 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1753 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1754 }
1755#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001756
Jerry Yu568ec252022-07-22 21:27:34 +08001757#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001758 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1759 {
1760 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1761 psa_set_key_usage_flags( &key_attributes,
1762 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1763 }
1764#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1765
Neil Armstrong501c9322022-05-03 09:35:09 +02001766 psa_set_key_algorithm( &key_attributes, alg );
1767 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1768
1769 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1770 if( status != PSA_SUCCESS )
1771 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1772
1773 /* Allow calling psa_destroy_key() on psk remove */
1774 ssl->handshake->psk_opaque_is_internal = 1;
1775 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1776#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001777 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001779
1780 ssl->handshake->psk_len = psk_len;
1781 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1782
1783 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001784#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001785}
1786
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001787#if defined(MBEDTLS_USE_PSA_CRYPTO)
1788int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001789 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001790 const unsigned char *psk_identity,
1791 size_t psk_identity_len )
1792{
Janos Follath865b3eb2019-12-16 11:46:15 +00001793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001794
1795 /* We currently only support one PSK, raw or opaque. */
1796 if( ssl_conf_psk_is_configured( conf ) )
1797 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001798
Hanno Becker7390c712018-11-15 13:33:04 +00001799 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001800 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001802 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001803
1804 /* Check and set PSK Identity */
1805 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1806 psk_identity_len );
1807 if( ret != 0 )
1808 ssl_conf_remove_psk( conf );
1809
1810 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001811}
1812
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001813int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1814 mbedtls_svc_key_id_t psk )
1815{
1816 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1817 ( ssl->handshake == NULL ) )
1818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1819
1820 ssl_remove_psk( ssl );
1821 ssl->handshake->psk_opaque = psk;
1822 return( 0 );
1823}
1824#endif /* MBEDTLS_USE_PSA_CRYPTO */
1825
Jerry Yu8897c072022-08-12 13:56:53 +08001826#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001827void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1828 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1829 size_t),
1830 void *p_psk )
1831{
1832 conf->f_psk = f_psk;
1833 conf->p_psk = p_psk;
1834}
Jerry Yu8897c072022-08-12 13:56:53 +08001835#endif /* MBEDTLS_SSL_SRV_C */
1836
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001837#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1838
1839#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001840static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1841 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001842{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001843#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001844 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001846#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001847 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001848 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001849 return( MBEDTLS_SSL_MODE_STREAM );
1850}
1851
1852#else /* MBEDTLS_USE_PSA_CRYPTO */
1853
1854static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1855 mbedtls_cipher_mode_t mode )
1856{
1857#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1858 if( mode == MBEDTLS_MODE_CBC )
1859 return( MBEDTLS_SSL_MODE_CBC );
1860#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1861
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001862#if defined(MBEDTLS_GCM_C) || \
1863 defined(MBEDTLS_CCM_C) || \
1864 defined(MBEDTLS_CHACHAPOLY_C)
1865 if( mode == MBEDTLS_MODE_GCM ||
1866 mode == MBEDTLS_MODE_CCM ||
1867 mode == MBEDTLS_MODE_CHACHAPOLY )
1868 return( MBEDTLS_SSL_MODE_AEAD );
1869#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001870
1871 return( MBEDTLS_SSL_MODE_STREAM );
1872}
Gilles Peskine301711e2022-04-26 16:57:05 +02001873#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001874
Gilles Peskinee108d982022-04-26 16:50:40 +02001875static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1876 mbedtls_ssl_mode_t base_mode,
1877 int encrypt_then_mac )
1878{
1879#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1880 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1881 base_mode == MBEDTLS_SSL_MODE_CBC )
1882 {
1883 return( MBEDTLS_SSL_MODE_CBC_ETM );
1884 }
1885#else
1886 (void) encrypt_then_mac;
1887#endif
1888 return( base_mode );
1889}
1890
Neil Armstrongab555e02022-04-04 11:07:59 +02001891mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001892 const mbedtls_ssl_transform *transform )
1893{
Gilles Peskinee108d982022-04-26 16:50:40 +02001894 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001895#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001896 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001897#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001898 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1899#endif
1900 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001901
Gilles Peskinee108d982022-04-26 16:50:40 +02001902 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001903#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001904 encrypt_then_mac = transform->encrypt_then_mac;
1905#endif
1906 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001907}
1908
Neil Armstrongab555e02022-04-04 11:07:59 +02001909mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001910#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001911 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001912#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001913 const mbedtls_ssl_ciphersuite_t *suite )
1914{
Gilles Peskinee108d982022-04-26 16:50:40 +02001915 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1916
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001917#if defined(MBEDTLS_USE_PSA_CRYPTO)
1918 psa_status_t status;
1919 psa_algorithm_t alg;
1920 psa_key_type_t type;
1921 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001922 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1923 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001924 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001925#else
1926 const mbedtls_cipher_info_t *cipher =
1927 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001928 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001929 {
1930 base_mode =
1931 mbedtls_ssl_get_base_mode(
1932 mbedtls_cipher_info_get_mode( cipher ) );
1933 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001934#endif /* MBEDTLS_USE_PSA_CRYPTO */
1935
Gilles Peskinee108d982022-04-26 16:50:40 +02001936#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1937 int encrypt_then_mac = 0;
1938#endif
1939 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001940}
1941
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001942#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001943
1944#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001945/* Serialization of TLS 1.3 sessions:
1946 *
1947 * struct {
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00001948 * opaque hostname<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001949 * uint64 ticket_received;
1950 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001951 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001952 * } ClientOnlyData;
1953 *
1954 * struct {
1955 * uint8 endpoint;
1956 * uint8 ciphersuite[2];
1957 * uint32 ticket_age_add;
1958 * uint8 ticket_flags;
1959 * opaque resumption_key<0..255>;
1960 * select ( endpoint ) {
1961 * case client: ClientOnlyData;
1962 * case server: uint64 start_time;
1963 * };
1964 * } serialized_session_tls13;
1965 *
1966 */
1967#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001968MBEDTLS_CHECK_RETURN_CRITICAL
1969static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1970 unsigned char *buf,
1971 size_t buf_len,
1972 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001973{
1974 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001975#if defined(MBEDTLS_SSL_CLI_C) && \
1976 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1977 size_t hostname_len = ( session->hostname == NULL ) ?
1978 0 : strlen( session->hostname );
1979#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001980 size_t needed = 1 /* endpoint */
1981 + 2 /* ciphersuite */
1982 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001983 + 1 /* ticket_flags */
1984 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001985 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001986
1987 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1989 needed += session->resumption_key_len; /* resumption_key */
1990
Jerry Yu438ddd82022-07-07 06:55:50 +00001991#if defined(MBEDTLS_HAVE_TIME)
1992 needed += 8; /* start_time or ticket_received */
1993#endif
1994
1995#if defined(MBEDTLS_SSL_CLI_C)
1996 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1997 {
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00001998#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00001999 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002000 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002001#endif
2002
Jerry Yu438ddd82022-07-07 06:55:50 +00002003 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002004 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002005
2006 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002007 if( session->ticket_len > SIZE_MAX - needed )
2008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002009
Jerry Yue28d9742022-08-18 15:44:03 +08002010 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002011 }
2012#endif /* MBEDTLS_SSL_CLI_C */
2013
Jerry Yue36fdd62022-08-17 21:31:36 +08002014 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002015 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002016 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002017
2018 p[0] = session->endpoint;
2019 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2020 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2021 p[7] = session->ticket_flags;
2022
2023 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002024 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002025 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002026 memcpy( p, session->resumption_key, session->resumption_key_len );
2027 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002028
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002029#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002030 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002031 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002032 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002033 p += 2;
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002034 if ( hostname_len > 0 &&
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002035 session->hostname != NULL )
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002036 {
2037 /* save host name */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002038 memcpy( p, session->hostname, hostname_len );
2039 p += hostname_len;
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002040 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002041 }
2042#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2043
Jerry Yu438ddd82022-07-07 06:55:50 +00002044#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2045 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2046 {
2047 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2048 p += 8;
2049 }
2050#endif /* MBEDTLS_HAVE_TIME */
2051
2052#if defined(MBEDTLS_SSL_CLI_C)
2053 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2054 {
2055#if defined(MBEDTLS_HAVE_TIME)
2056 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2057 p += 8;
2058#endif
2059 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2060 p += 4;
2061
2062 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2063 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002064
2065 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002066 {
2067 memcpy( p, session->ticket, session->ticket_len );
2068 p += session->ticket_len;
2069 }
2070 }
2071#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002072 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002073}
2074
2075MBEDTLS_CHECK_RETURN_CRITICAL
2076static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2077 const unsigned char *buf,
2078 size_t len )
2079{
2080 const unsigned char *p = buf;
2081 const unsigned char *end = buf + len;
2082
2083 if( end - p < 9 )
2084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2085 session->endpoint = p[0];
2086 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2087 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2088 session->ticket_flags = p[7];
2089
2090 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002091 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002092 p += 9;
2093
Jerry Yubc7c1a42022-07-21 22:57:37 +08002094 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2096
Jerry Yubc7c1a42022-07-21 22:57:37 +08002097 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002099 memcpy( session->resumption_key, p, session->resumption_key_len );
2100 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002101
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002102#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
2103 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2104 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002105 size_t hostname_len;
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002106 /* load host name */
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002107 if( end - p < 2 )
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002109 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002110 p += 2;
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002111
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002112 if( end - p < ( long int )hostname_len )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002114 if( hostname_len > 0 )
Xiaokang Qianecd75282022-09-28 07:11:02 +00002115 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002116 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Xiaokang Qianecd75282022-09-28 07:11:02 +00002117 if( session->hostname == NULL )
2118 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002119 memcpy( session->hostname, p, hostname_len );
2120 p += hostname_len;
Xiaokang Qianecd75282022-09-28 07:11:02 +00002121 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002122 }
2123#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2124
Jerry Yu438ddd82022-07-07 06:55:50 +00002125#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2126 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2127 {
2128 if( end - p < 8 )
2129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2130 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2131 p += 8;
2132 }
2133#endif /* MBEDTLS_HAVE_TIME */
2134
2135#if defined(MBEDTLS_SSL_CLI_C)
2136 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2137 {
2138#if defined(MBEDTLS_HAVE_TIME)
2139 if( end - p < 8 )
2140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2141 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2142 p += 8;
2143#endif
2144 if( end - p < 4 )
2145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2146 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2147 p += 4;
2148
2149 if( end - p < 2 )
2150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2151 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2152 p += 2;
2153
2154 if( end - p < ( long int )session->ticket_len )
2155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2156 if( session->ticket_len > 0 )
2157 {
2158 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2159 if( session->ticket == NULL )
2160 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2161 memcpy( session->ticket, p, session->ticket_len );
2162 p += session->ticket_len;
2163 }
2164 }
2165#endif /* MBEDTLS_SSL_CLI_C */
2166
2167 return( 0 );
2168
2169}
2170#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002171MBEDTLS_CHECK_RETURN_CRITICAL
2172static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2173 unsigned char *buf,
2174 size_t buf_len,
2175 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002176{
2177 ((void) session);
2178 ((void) buf);
2179 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002180 *olen = 0;
2181 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002182}
Jerry Yu438ddd82022-07-07 06:55:50 +00002183
2184static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2185 unsigned char *buf,
2186 size_t buf_len )
2187{
2188 ((void) session);
2189 ((void) buf);
2190 ((void) buf_len);
2191 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2192}
2193#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002194#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2195
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002196psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002197 size_t taglen,
2198 psa_algorithm_t *alg,
2199 psa_key_type_t *key_type,
2200 size_t *key_size )
2201{
2202 switch ( mbedtls_cipher_type )
2203 {
2204 case MBEDTLS_CIPHER_AES_128_CBC:
2205 *alg = PSA_ALG_CBC_NO_PADDING;
2206 *key_type = PSA_KEY_TYPE_AES;
2207 *key_size = 128;
2208 break;
2209 case MBEDTLS_CIPHER_AES_128_CCM:
2210 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2211 *key_type = PSA_KEY_TYPE_AES;
2212 *key_size = 128;
2213 break;
2214 case MBEDTLS_CIPHER_AES_128_GCM:
2215 *alg = PSA_ALG_GCM;
2216 *key_type = PSA_KEY_TYPE_AES;
2217 *key_size = 128;
2218 break;
2219 case MBEDTLS_CIPHER_AES_192_CCM:
2220 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2221 *key_type = PSA_KEY_TYPE_AES;
2222 *key_size = 192;
2223 break;
2224 case MBEDTLS_CIPHER_AES_192_GCM:
2225 *alg = PSA_ALG_GCM;
2226 *key_type = PSA_KEY_TYPE_AES;
2227 *key_size = 192;
2228 break;
2229 case MBEDTLS_CIPHER_AES_256_CBC:
2230 *alg = PSA_ALG_CBC_NO_PADDING;
2231 *key_type = PSA_KEY_TYPE_AES;
2232 *key_size = 256;
2233 break;
2234 case MBEDTLS_CIPHER_AES_256_CCM:
2235 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2236 *key_type = PSA_KEY_TYPE_AES;
2237 *key_size = 256;
2238 break;
2239 case MBEDTLS_CIPHER_AES_256_GCM:
2240 *alg = PSA_ALG_GCM;
2241 *key_type = PSA_KEY_TYPE_AES;
2242 *key_size = 256;
2243 break;
2244 case MBEDTLS_CIPHER_ARIA_128_CBC:
2245 *alg = PSA_ALG_CBC_NO_PADDING;
2246 *key_type = PSA_KEY_TYPE_ARIA;
2247 *key_size = 128;
2248 break;
2249 case MBEDTLS_CIPHER_ARIA_128_CCM:
2250 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2251 *key_type = PSA_KEY_TYPE_ARIA;
2252 *key_size = 128;
2253 break;
2254 case MBEDTLS_CIPHER_ARIA_128_GCM:
2255 *alg = PSA_ALG_GCM;
2256 *key_type = PSA_KEY_TYPE_ARIA;
2257 *key_size = 128;
2258 break;
2259 case MBEDTLS_CIPHER_ARIA_192_CCM:
2260 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2261 *key_type = PSA_KEY_TYPE_ARIA;
2262 *key_size = 192;
2263 break;
2264 case MBEDTLS_CIPHER_ARIA_192_GCM:
2265 *alg = PSA_ALG_GCM;
2266 *key_type = PSA_KEY_TYPE_ARIA;
2267 *key_size = 192;
2268 break;
2269 case MBEDTLS_CIPHER_ARIA_256_CBC:
2270 *alg = PSA_ALG_CBC_NO_PADDING;
2271 *key_type = PSA_KEY_TYPE_ARIA;
2272 *key_size = 256;
2273 break;
2274 case MBEDTLS_CIPHER_ARIA_256_CCM:
2275 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2276 *key_type = PSA_KEY_TYPE_ARIA;
2277 *key_size = 256;
2278 break;
2279 case MBEDTLS_CIPHER_ARIA_256_GCM:
2280 *alg = PSA_ALG_GCM;
2281 *key_type = PSA_KEY_TYPE_ARIA;
2282 *key_size = 256;
2283 break;
2284 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2285 *alg = PSA_ALG_CBC_NO_PADDING;
2286 *key_type = PSA_KEY_TYPE_CAMELLIA;
2287 *key_size = 128;
2288 break;
2289 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2290 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2291 *key_type = PSA_KEY_TYPE_CAMELLIA;
2292 *key_size = 128;
2293 break;
2294 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2295 *alg = PSA_ALG_GCM;
2296 *key_type = PSA_KEY_TYPE_CAMELLIA;
2297 *key_size = 128;
2298 break;
2299 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2300 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2301 *key_type = PSA_KEY_TYPE_CAMELLIA;
2302 *key_size = 192;
2303 break;
2304 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2305 *alg = PSA_ALG_GCM;
2306 *key_type = PSA_KEY_TYPE_CAMELLIA;
2307 *key_size = 192;
2308 break;
2309 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2310 *alg = PSA_ALG_CBC_NO_PADDING;
2311 *key_type = PSA_KEY_TYPE_CAMELLIA;
2312 *key_size = 256;
2313 break;
2314 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2315 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2316 *key_type = PSA_KEY_TYPE_CAMELLIA;
2317 *key_size = 256;
2318 break;
2319 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2320 *alg = PSA_ALG_GCM;
2321 *key_type = PSA_KEY_TYPE_CAMELLIA;
2322 *key_size = 256;
2323 break;
2324 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2325 *alg = PSA_ALG_CHACHA20_POLY1305;
2326 *key_type = PSA_KEY_TYPE_CHACHA20;
2327 *key_size = 256;
2328 break;
2329 case MBEDTLS_CIPHER_NULL:
2330 *alg = MBEDTLS_SSL_NULL_CIPHER;
2331 *key_type = 0;
2332 *key_size = 0;
2333 break;
2334 default:
2335 return PSA_ERROR_NOT_SUPPORTED;
2336 }
2337
2338 return PSA_SUCCESS;
2339}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002340#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002341
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002342#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002343int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2344 const unsigned char *dhm_P, size_t P_len,
2345 const unsigned char *dhm_G, size_t G_len )
2346{
Janos Follath865b3eb2019-12-16 11:46:15 +00002347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002348
Glenn Strausscee11292021-12-20 01:43:17 -05002349 mbedtls_mpi_free( &conf->dhm_P );
2350 mbedtls_mpi_free( &conf->dhm_G );
2351
Hanno Beckera90658f2017-10-04 15:29:08 +01002352 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2353 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2354 {
2355 mbedtls_mpi_free( &conf->dhm_P );
2356 mbedtls_mpi_free( &conf->dhm_G );
2357 return( ret );
2358 }
2359
2360 return( 0 );
2361}
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002363int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002364{
Janos Follath865b3eb2019-12-16 11:46:15 +00002365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002366
Glenn Strausscee11292021-12-20 01:43:17 -05002367 mbedtls_mpi_free( &conf->dhm_P );
2368 mbedtls_mpi_free( &conf->dhm_G );
2369
Gilles Peskinee5702482021-06-11 21:59:08 +02002370 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2371 &conf->dhm_P ) ) != 0 ||
2372 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2373 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002374 {
2375 mbedtls_mpi_free( &conf->dhm_P );
2376 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002377 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002378 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002379
2380 return( 0 );
2381}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002382#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002383
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002384#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2385/*
2386 * Set the minimum length for Diffie-Hellman parameters
2387 */
2388void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2389 unsigned int bitlen )
2390{
2391 conf->dhm_min_bitlen = bitlen;
2392}
2393#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2394
Gilles Peskineeccd8882020-03-10 12:19:08 +01002395#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002396#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002397/*
2398 * Set allowed/preferred hashes for handshake signatures
2399 */
2400void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2401 const int *hashes )
2402{
2403 conf->sig_hashes = hashes;
2404}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002405#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002406
Jerry Yuf017ee42022-01-12 15:49:48 +08002407/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002408void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2409 const uint16_t* sig_algs )
2410{
Jerry Yuf017ee42022-01-12 15:49:48 +08002411#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2412 conf->sig_hashes = NULL;
2413#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2414 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002415}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002416#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002417
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002418#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002419#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002420/*
2421 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002422 *
2423 * mbedtls_ssl_setup() takes the provided list
2424 * and translates it to a list of IANA TLS group identifiers,
2425 * stored in ssl->handshake->group_list.
2426 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002427 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002428void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002429 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002431 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002432 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002433}
Brett Warrene0edc842021-08-17 09:53:13 +01002434#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002435#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002436
Brett Warrene0edc842021-08-17 09:53:13 +01002437/*
2438 * Set the allowed groups
2439 */
2440void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2441 const uint16_t *group_list )
2442{
2443#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2444 conf->curve_list = NULL;
2445#endif
2446 conf->group_list = group_list;
2447}
2448
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002449#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451{
Hanno Becker947194e2017-04-07 13:25:49 +01002452 /* Initialize to suppress unnecessary compiler warning */
2453 size_t hostname_len = 0;
2454
2455 /* Check if new hostname is valid before
2456 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002457 if( hostname != NULL )
2458 {
2459 hostname_len = strlen( hostname );
2460
2461 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2463 }
2464
2465 /* Now it's clear that we will overwrite the old hostname,
2466 * so we can free it safely */
Hanno Becker947194e2017-04-07 13:25:49 +01002467 if( ssl->hostname != NULL )
2468 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002469 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002470 mbedtls_free( ssl->hostname );
2471 }
2472
2473 /* Passing NULL as hostname shall clear the old one */
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002475 {
2476 ssl->hostname = NULL;
2477 }
2478 else
2479 {
2480 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002481 if( ssl->hostname == NULL )
2482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002483
Hanno Becker947194e2017-04-07 13:25:49 +01002484 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002485
Hanno Becker947194e2017-04-07 13:25:49 +01002486 ssl->hostname[hostname_len] = '\0';
2487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
2489 return( 0 );
2490}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002491#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002493#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002494void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002496 const unsigned char *, size_t),
2497 void *p_sni )
2498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002499 conf->f_sni = f_sni;
2500 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002505int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002506{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002507 size_t cur_len, tot_len;
2508 const char **p;
2509
2510 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002511 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2512 * MUST NOT be truncated."
2513 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002514 */
2515 tot_len = 0;
2516 for( p = protos; *p != NULL; p++ )
2517 {
2518 cur_len = strlen( *p );
2519 tot_len += cur_len;
2520
Ronald Cron8216dd32020-04-23 16:41:44 +02002521 if( ( cur_len == 0 ) ||
2522 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2523 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002525 }
2526
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002527 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002528
2529 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002530}
2531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002533{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002534 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002537
Johan Pascalb62bb512015-12-03 21:56:45 +01002538#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002539void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2540 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002541{
2542 conf->dtls_srtp_mki_support = support_mki_value;
2543}
2544
Ron Eldoref72faf2018-07-12 11:54:20 +03002545int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2546 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002547 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002548{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002549 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002550 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002552 }
Ron Eldor591f1622018-01-22 12:30:04 +02002553
2554 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002555 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002556 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002557 }
Ron Eldor591f1622018-01-22 12:30:04 +02002558
2559 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2560 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002561 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002562}
2563
Ron Eldoref72faf2018-07-12 11:54:20 +03002564int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002565 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002566{
Johan Pascal253d0262020-09-22 13:04:45 +02002567 const mbedtls_ssl_srtp_profile *p;
2568 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002569
Johan Pascal253d0262020-09-22 13:04:45 +02002570 /* check the profiles list: all entry must be valid,
2571 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002572 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2573 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2574 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002575 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002576 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002577 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002578 list_size++;
2579 }
2580 else
2581 {
2582 /* unsupported value, stop parsing and set the size to an error value */
2583 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002584 }
2585 }
2586
Johan Pascal5ef72d22020-10-28 17:05:47 +01002587 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002588 {
Johan Pascal253d0262020-09-22 13:04:45 +02002589 conf->dtls_srtp_profile_list = NULL;
2590 conf->dtls_srtp_profile_list_len = 0;
2591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2592 }
2593
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002594 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002595 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002596
2597 return( 0 );
2598}
2599
Johan Pascal5ef72d22020-10-28 17:05:47 +01002600void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2601 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002602{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002603 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2604 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002605 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002606 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002607 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002608 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002609 else
2610 {
2611 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002612 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2613 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002614 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002615}
Johan Pascalb62bb512015-12-03 21:56:45 +01002616#endif /* MBEDTLS_SSL_DTLS_SRTP */
2617
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302618#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002619void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002620{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002621 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002622}
2623
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002624void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002625{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002626 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002627}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302628#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002629
Janos Follath088ce432017-04-10 12:42:31 +01002630#if defined(MBEDTLS_SSL_SRV_C)
2631void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2632 char cert_req_ca_list )
2633{
2634 conf->cert_req_ca_list = cert_req_ca_list;
2635}
2636#endif
2637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002639void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002640{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002641 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002642}
2643#endif
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002646void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002648 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002649}
2650#endif
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002653int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002656 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002659 }
2660
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002661 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002662
2663 return( 0 );
2664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002666
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002667void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002669 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002670}
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002673void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002674{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002675 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002676}
2677
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002678void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002680 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002681}
2682
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002683void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002684 const unsigned char period[8] )
2685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002686 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002691#if defined(MBEDTLS_SSL_CLI_C)
2692void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002693{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002694 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002695}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002696#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002697
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002698#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002699
Jerry Yud0766ec2022-09-22 10:46:57 +08002700#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002701void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2702 uint16_t num_tickets )
2703{
Jerry Yud0766ec2022-09-22 10:46:57 +08002704 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002705}
2706#endif
2707
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002708void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2709 mbedtls_ssl_ticket_write_t *f_ticket_write,
2710 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2711 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002712{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002713 conf->f_ticket_write = f_ticket_write;
2714 conf->f_ticket_parse = f_ticket_parse;
2715 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002716}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002717#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002719
Hanno Becker7e6c1782021-06-08 09:24:55 +01002720void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2721 mbedtls_ssl_export_keys_t *f_export_keys,
2722 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002723{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002724 ssl->f_export_keys = f_export_keys;
2725 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002726}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002727
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002728#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002729void mbedtls_ssl_conf_async_private_cb(
2730 mbedtls_ssl_config *conf,
2731 mbedtls_ssl_async_sign_t *f_async_sign,
2732 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2733 mbedtls_ssl_async_resume_t *f_async_resume,
2734 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002735 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002736{
2737 conf->f_async_sign_start = f_async_sign;
2738 conf->f_async_decrypt_start = f_async_decrypt;
2739 conf->f_async_resume = f_async_resume;
2740 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002741 conf->p_async_config_data = async_config_data;
2742}
2743
Gilles Peskine8f97af72018-04-26 11:46:10 +02002744void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2745{
2746 return( conf->p_async_config_data );
2747}
2748
Gilles Peskine1febfef2018-04-30 11:54:39 +02002749void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002750{
2751 if( ssl->handshake == NULL )
2752 return( NULL );
2753 else
2754 return( ssl->handshake->user_async_ctx );
2755}
2756
Gilles Peskine1febfef2018-04-30 11:54:39 +02002757void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002758 void *ctx )
2759{
2760 if( ssl->handshake != NULL )
2761 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002762}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002763#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002764
Paul Bakker5121ce52009-01-03 21:22:43 +00002765/*
2766 * SSL get accessors
2767 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002768uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002770 if( ssl->session != NULL )
2771 return( ssl->session->verify_result );
2772
2773 if( ssl->session_negotiate != NULL )
2774 return( ssl->session_negotiate->verify_result );
2775
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002776 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777}
2778
Glenn Strauss8f526902022-01-13 00:04:49 -05002779int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2780{
2781 if( ssl == NULL || ssl->session == NULL )
2782 return( 0 );
2783
2784 return( ssl->session->ciphersuite );
2785}
2786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002788{
Paul Bakker926c8e42013-03-06 10:23:34 +01002789 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002790 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002793}
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002798 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002799 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002800 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002801 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002802 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002803 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002804 default:
2805 return( "unknown (DTLS)" );
2806 }
2807 }
2808#endif
2809
Glenn Strauss60bfe602022-03-14 19:04:24 -04002810 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002811 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002812 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002813 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002814 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002815 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002816 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002817 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002818 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002819}
2820
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002821#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002822size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2823{
David Horstmann95d516f2021-05-04 18:36:56 +01002824 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002825 size_t read_mfl;
2826
2827 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2828 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2829 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2830 {
2831 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2832 }
2833
2834 /* Check if a smaller max length was negotiated */
2835 if( ssl->session_out != NULL )
2836 {
2837 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2838 if( read_mfl < max_len )
2839 {
2840 max_len = read_mfl;
2841 }
2842 }
2843
2844 // During a handshake, use the value being negotiated
2845 if( ssl->session_negotiate != NULL )
2846 {
2847 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2848 if( read_mfl < max_len )
2849 {
2850 max_len = read_mfl;
2851 }
2852 }
2853
2854 return( max_len );
2855}
2856
2857size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002858{
2859 size_t max_len;
2860
2861 /*
2862 * Assume mfl_code is correct since it was checked when set
2863 */
Angus Grattond8213d02016-05-25 20:56:48 +10002864 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002865
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002866 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002867 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002868 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002869 {
Angus Grattond8213d02016-05-25 20:56:48 +10002870 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002871 }
2872
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002873 /* During a handshake, use the value being negotiated */
2874 if( ssl->session_negotiate != NULL &&
2875 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2876 {
2877 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2878 }
2879
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002880 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002881}
2882#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2883
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002885size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002886{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002887 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2888 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2889 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2890 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2891 return ( 0 );
2892
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002893 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2894 return( ssl->mtu );
2895
2896 if( ssl->mtu == 0 )
2897 return( ssl->handshake->mtu );
2898
2899 return( ssl->mtu < ssl->handshake->mtu ?
2900 ssl->mtu : ssl->handshake->mtu );
2901}
2902#endif /* MBEDTLS_SSL_PROTO_DTLS */
2903
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002904int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2905{
2906 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2907
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002908#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2909 !defined(MBEDTLS_SSL_PROTO_DTLS)
2910 (void) ssl;
2911#endif
2912
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002913#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002914 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002915
2916 if( max_len > mfl )
2917 max_len = mfl;
2918#endif
2919
2920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002921 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002922 {
Hanno Becker89490712020-02-05 10:50:12 +00002923 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002924 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2925 const size_t overhead = (size_t) ret;
2926
2927 if( ret < 0 )
2928 return( ret );
2929
2930 if( mtu <= overhead )
2931 {
2932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2933 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2934 }
2935
2936 if( max_len > mtu - overhead )
2937 max_len = mtu - overhead;
2938 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002939#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002940
Hanno Becker0defedb2018-08-10 12:35:02 +01002941#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2942 !defined(MBEDTLS_SSL_PROTO_DTLS)
2943 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002944#endif
2945
2946 return( (int) max_len );
2947}
2948
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002949int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2950{
2951 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2952
2953#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2954 (void) ssl;
2955#endif
2956
2957#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2958 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2959
2960 if( max_len > mfl )
2961 max_len = mfl;
2962#endif
2963
2964 return( (int) max_len );
2965}
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_X509_CRT_PARSE_C)
2968const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002969{
2970 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002971 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002972
Hanno Beckere6824572019-02-07 13:18:46 +00002973#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002974 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002975#else
2976 return( NULL );
2977#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002982int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2983 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002984{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002985 int ret;
2986
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002987 if( ssl == NULL ||
2988 dst == NULL ||
2989 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002990 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002993 }
2994
Hanno Beckere810bbc2021-05-14 16:01:05 +01002995 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2996 * idempotent: Each session can only be exported once.
2997 *
2998 * (This is in preparation for TLS 1.3 support where we will
2999 * need the ability to export multiple sessions (aka tickets),
3000 * which will be achieved by calling mbedtls_ssl_get_session()
3001 * multiple times until it fails.)
3002 *
3003 * Check whether we have already exported the current session,
3004 * and fail if so.
3005 */
3006 if( ssl->session->exported == 1 )
3007 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3008
3009 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3010 if( ret != 0 )
3011 return( ret );
3012
3013 /* Remember that we've exported the session. */
3014 ssl->session->exported = 1;
3015 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003018
Paul Bakker5121ce52009-01-03 21:22:43 +00003019/*
Hanno Beckera835da52019-05-16 12:39:07 +01003020 * Define ticket header determining Mbed TLS version
3021 * and structure of the ticket.
3022 */
3023
Hanno Becker94ef3b32019-05-16 12:50:45 +01003024/*
Hanno Becker50b59662019-05-28 14:30:45 +01003025 * Define bitflag determining compile-time settings influencing
3026 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003027 */
3028
Hanno Becker50b59662019-05-28 14:30:45 +01003029#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003030#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003031#else
Hanno Becker3e088662019-05-29 11:10:18 +01003032#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003033#endif /* MBEDTLS_HAVE_TIME */
3034
3035#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003036#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003037#else
Hanno Becker3e088662019-05-29 11:10:18 +01003038#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003039#endif /* MBEDTLS_X509_CRT_PARSE_C */
3040
3041#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003042#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003043#else
Hanno Becker3e088662019-05-29 11:10:18 +01003044#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003045#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3046
3047#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003048#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003049#else
Hanno Becker3e088662019-05-29 11:10:18 +01003050#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003051#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3052
Hanno Becker94ef3b32019-05-16 12:50:45 +01003053#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003054#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003055#else
Hanno Becker3e088662019-05-29 11:10:18 +01003056#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003057#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3058
Hanno Becker94ef3b32019-05-16 12:50:45 +01003059#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3060#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3061#else
3062#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3063#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3064
Hanno Becker3e088662019-05-29 11:10:18 +01003065#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3066#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3067#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3068#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003069#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3070#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003071
Hanno Becker50b59662019-05-28 14:30:45 +01003072#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003073 ( (uint16_t) ( \
3074 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3075 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3076 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3077 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003078 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003079 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003080
Hanno Beckerf8787072019-05-16 12:41:07 +01003081static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003082 MBEDTLS_VERSION_MAJOR,
3083 MBEDTLS_VERSION_MINOR,
3084 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003085 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3086 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003087};
Hanno Beckera835da52019-05-16 12:39:07 +01003088
3089/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003090 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003091 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003092 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003093 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003094 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003095 * opaque mbedtls_version[3]; // library version: major, minor, patch
3096 * opaque session_format[2]; // library-version specific 16-bit field
3097 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003098 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003099 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003100 * Note: When updating the format, remember to keep
3101 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003102 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003103 * // In this version, `session_format` determines
3104 * // the setting of those compile-time
3105 * // configuration options which influence
3106 * // the structure of mbedtls_ssl_session.
3107 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003108 * uint8_t minor_ver; // Protocol minor version. Possible values:
3109 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003110 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003111 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003112 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003113 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003114 * serialized_session_tls12 data;
3115 *
3116 * };
3117 *
3118 * } serialized_session;
3119 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003120 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003121
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003122MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003123static int ssl_session_save( const mbedtls_ssl_session *session,
3124 unsigned char omit_header,
3125 unsigned char *buf,
3126 size_t buf_len,
3127 size_t *olen )
3128{
3129 unsigned char *p = buf;
3130 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003131 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003132#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3133 size_t out_len;
3134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3135#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003136 if( session == NULL )
3137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3138
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003139 if( !omit_header )
3140 {
3141 /*
3142 * Add Mbed TLS version identifier
3143 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003144 used += sizeof( ssl_serialized_session_header );
3145
3146 if( used <= buf_len )
3147 {
3148 memcpy( p, ssl_serialized_session_header,
3149 sizeof( ssl_serialized_session_header ) );
3150 p += sizeof( ssl_serialized_session_header );
3151 }
3152 }
3153
3154 /*
3155 * TLS version identifier
3156 */
3157 used += 1;
3158 if( used <= buf_len )
3159 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003160 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003161 }
3162
3163 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003164 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003165 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003166 {
3167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003168 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003169 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003170 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3172
Jerry Yu251a12e2022-07-13 15:15:48 +08003173#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3174 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003175 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3176 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003177 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003178 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003179 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003180#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3181
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003182 default:
3183 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3184 }
3185
3186 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003187 if( used > buf_len )
3188 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003189
3190 return( 0 );
3191}
3192
3193/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003194 * Public wrapper for ssl_session_save()
3195 */
3196int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3197 unsigned char *buf,
3198 size_t buf_len,
3199 size_t *olen )
3200{
3201 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3202}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003203
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003204/*
3205 * Deserialize session, see mbedtls_ssl_session_save() for format.
3206 *
3207 * This internal version is wrapped by a public function that cleans up in
3208 * case of error, and has an extra option omit_header.
3209 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003210MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003211static int ssl_session_load( mbedtls_ssl_session *session,
3212 unsigned char omit_header,
3213 const unsigned char *buf,
3214 size_t len )
3215{
3216 const unsigned char *p = buf;
3217 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003218 size_t remaining_len;
3219
3220
3221 if( session == NULL )
3222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003223
3224 if( !omit_header )
3225 {
3226 /*
3227 * Check Mbed TLS version identifier
3228 */
3229
3230 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3232
3233 if( memcmp( p, ssl_serialized_session_header,
3234 sizeof( ssl_serialized_session_header ) ) != 0 )
3235 {
3236 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3237 }
3238 p += sizeof( ssl_serialized_session_header );
3239 }
3240
3241 /*
3242 * TLS version identifier
3243 */
3244 if( 1 > (size_t)( end - p ) )
3245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003246 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003247
3248 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003249 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003250 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003251 {
3252#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003253 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003254 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3256
Jerry Yu438ddd82022-07-07 06:55:50 +00003257#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3258 case MBEDTLS_SSL_VERSION_TLS1_3:
3259 return( ssl_tls13_session_load( session, p, remaining_len ) );
3260#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3261
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003262 default:
3263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3264 }
3265}
3266
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003267/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003268 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003269 */
3270int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3271 const unsigned char *buf,
3272 size_t len )
3273{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003274 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003275
3276 if( ret != 0 )
3277 mbedtls_ssl_session_free( session );
3278
3279 return( ret );
3280}
3281
3282/*
Paul Bakker1961b702013-01-25 14:49:24 +01003283 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003285MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003286static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3287{
3288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3289
Ronald Cron66dbf912022-02-02 15:33:46 +01003290 /*
3291 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003292 * that were written into the output buffer by the previous handshake step,
3293 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003294 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3295 * We proceed to the next handshake step only when all data from the
3296 * previous one have been sent to the peer, thus we make sure that this is
3297 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3298 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3299 * we have to wait before to go ahead.
3300 * In the case of TLS 1.3, handshake step handlers do not send data to the
3301 * peer. Data are only sent here and through
3302 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003303 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003304 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003305 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3306 return( ret );
3307
3308#if defined(MBEDTLS_SSL_PROTO_DTLS)
3309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3310 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3311 {
3312 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3313 return( ret );
3314 }
3315#endif /* MBEDTLS_SSL_PROTO_DTLS */
3316
3317 return( ret );
3318}
3319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003321{
Hanno Becker41934dd2021-08-07 19:13:43 +01003322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
Hanno Becker41934dd2021-08-07 19:13:43 +01003324 if( ssl == NULL ||
3325 ssl->conf == NULL ||
3326 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003327 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003328 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003330 }
3331
3332 ret = ssl_prepare_handshake_step( ssl );
3333 if( ret != 0 )
3334 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003335
Jerry Yue7047812021-09-13 19:26:39 +08003336 ret = mbedtls_ssl_handle_pending_alert( ssl );
3337 if( ret != 0 )
3338 goto cleanup;
3339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003341 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003342 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3344 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003345
Ronald Cron9f0fba32022-02-10 16:45:15 +01003346 switch( ssl->state )
3347 {
3348 case MBEDTLS_SSL_HELLO_REQUEST:
3349 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3350 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003351
Ronald Cron9f0fba32022-02-10 16:45:15 +01003352 case MBEDTLS_SSL_CLIENT_HELLO:
3353 ret = mbedtls_ssl_write_client_hello( ssl );
3354 break;
3355
3356 default:
3357#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003358 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003359 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3360 else
3361 ret = mbedtls_ssl_handshake_client_step( ssl );
3362#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3363 ret = mbedtls_ssl_handshake_client_step( ssl );
3364#else
3365 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3366#endif
3367 }
Jerry Yub9930e72021-08-06 17:11:51 +08003368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003372 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003373#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003374 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003375 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003376#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003377
3378#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3379 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3380 ret = mbedtls_ssl_handshake_server_step( ssl );
3381#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003383#endif
3384
Jerry Yue7047812021-09-13 19:26:39 +08003385 if( ret != 0 )
3386 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003387 /* handshake_step return error. And it is same
3388 * with alert_reason.
3389 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003390 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003391 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003392 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003393 goto cleanup;
3394 }
3395 }
3396
3397cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003398 return( ret );
3399}
3400
3401/*
3402 * Perform the SSL handshake
3403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003405{
3406 int ret = 0;
3407
Hanno Beckera817ea42020-10-20 15:20:23 +01003408 /* Sanity checks */
3409
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003410 if( ssl == NULL || ssl->conf == NULL )
3411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3412
Hanno Beckera817ea42020-10-20 15:20:23 +01003413#if defined(MBEDTLS_SSL_PROTO_DTLS)
3414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3415 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3416 {
3417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3418 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3420 }
3421#endif /* MBEDTLS_SSL_PROTO_DTLS */
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003424
Hanno Beckera817ea42020-10-20 15:20:23 +01003425 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003426 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003429
3430 if( ret != 0 )
3431 break;
3432 }
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
3436 return( ret );
3437}
3438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#if defined(MBEDTLS_SSL_RENEGOTIATION)
3440#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003441/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003442 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003443 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003444MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003446{
Janos Follath865b3eb2019-12-16 11:46:15 +00003447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003450
3451 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3453 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003454
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003455 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003456 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003458 return( ret );
3459 }
3460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003462
3463 return( 0 );
3464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003466
3467/*
3468 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 * - any side: calling mbedtls_ssl_renegotiate(),
3470 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3471 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003472 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003473 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003475 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003476int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003477{
Janos Follath865b3eb2019-12-16 11:46:15 +00003478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003481
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003482 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3483 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003484
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003485 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3486 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003490 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003491 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003492 ssl->handshake->out_msg_seq = 1;
3493 else
3494 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003495 }
3496#endif
3497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3499 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003504 return( ret );
3505 }
3506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003508
3509 return( 0 );
3510}
3511
3512/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003513 * Renegotiate current connection on client,
3514 * or request renegotiation on server
3515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003519
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003520 if( ssl == NULL || ssl->conf == NULL )
3521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003524 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003525 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003526 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003527 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003531
3532 /* Did we already try/start sending HelloRequest? */
3533 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003535
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003536 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003541 /*
3542 * On client, either start the renegotiation process or,
3543 * if already in progress, continue the handshake
3544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003546 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003547 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003549
Hanno Becker40cdaa12020-02-05 10:48:27 +00003550 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003551 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003553 return( ret );
3554 }
3555 }
3556 else
3557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003561 return( ret );
3562 }
3563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003565
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003566 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003569
Gilles Peskine9b562d52018-04-25 20:32:43 +02003570void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003571{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003572 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3573
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003574 if( handshake == NULL )
3575 return;
3576
Brett Warrene0edc842021-08-17 09:53:13 +01003577#if defined(MBEDTLS_ECP_C)
3578#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3579 if ( ssl->handshake->group_list_heap_allocated )
3580 mbedtls_free( (void*) handshake->group_list );
3581 handshake->group_list = NULL;
3582#endif /* MBEDTLS_DEPRECATED_REMOVED */
3583#endif /* MBEDTLS_ECP_C */
3584
Jerry Yuf017ee42022-01-12 15:49:48 +08003585#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3586#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3587 if ( ssl->handshake->sig_algs_heap_allocated )
3588 mbedtls_free( (void*) handshake->sig_algs );
3589 handshake->sig_algs = NULL;
3590#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003591#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3592 if( ssl->handshake->certificate_request_context )
3593 {
3594 mbedtls_free( (void*) handshake->certificate_request_context );
3595 }
3596#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003597#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3598
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003599#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3600 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3601 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003602 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003603 handshake->async_in_progress = 0;
3604 }
3605#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3606
Andrzej Kurek25f27152022-08-17 16:09:31 -04003607#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003608#if defined(MBEDTLS_USE_PSA_CRYPTO)
3609 psa_hash_abort( &handshake->fin_sha256_psa );
3610#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003611 mbedtls_sha256_free( &handshake->fin_sha256 );
3612#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003613#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003614#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003615#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003616 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003617#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003618 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003619#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003620#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#if defined(MBEDTLS_DHM_C)
3623 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003624#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003625#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003627#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003628#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003629 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003630#if defined(MBEDTLS_SSL_CLI_C)
3631 mbedtls_free( handshake->ecjpake_cache );
3632 handshake->ecjpake_cache = NULL;
3633 handshake->ecjpake_cache_len = 0;
3634#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003635#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003636
Janos Follath4ae5c292016-02-10 11:27:43 +00003637#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3638 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003639 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003641#endif
3642
Gilles Peskineeccd8882020-03-10 12:19:08 +01003643#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003644#if defined(MBEDTLS_USE_PSA_CRYPTO)
3645 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3646 {
3647 /* The maintenance of the external PSK key slot is the
3648 * user's responsibility. */
3649 if( ssl->handshake->psk_opaque_is_internal )
3650 {
3651 psa_destroy_key( ssl->handshake->psk_opaque );
3652 ssl->handshake->psk_opaque_is_internal = 0;
3653 }
3654 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3655 }
Neil Armstronge952a302022-05-03 10:22:14 +02003656#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003657 if( handshake->psk != NULL )
3658 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003659 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003660 mbedtls_free( handshake->psk );
3661 }
Neil Armstronge952a302022-05-03 10:22:14 +02003662#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003663#endif
3664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3666 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003667 /*
3668 * Free only the linked list wrapper, not the keys themselves
3669 * since the belong to the SNI callback
3670 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003671 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003673
Gilles Peskineeccd8882020-03-10 12:19:08 +01003674#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003675 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003676 if( handshake->ecrs_peer_cert != NULL )
3677 {
3678 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3679 mbedtls_free( handshake->ecrs_peer_cert );
3680 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003681#endif
3682
Hanno Becker75173122019-02-06 16:18:31 +00003683#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3684 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3685 mbedtls_pk_free( &handshake->peer_pubkey );
3686#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3687
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003688#if defined(MBEDTLS_SSL_CLI_C) && \
3689 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3690 mbedtls_free( handshake->cookie );
3691#endif /* MBEDTLS_SSL_CLI_C &&
3692 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003693
3694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003695 mbedtls_ssl_flight_free( handshake->flight );
3696 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003697#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003698
Ronald Cronf12b81d2022-03-15 10:42:41 +01003699#if defined(MBEDTLS_ECDH_C) && \
3700 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003701 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003702 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003703#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3704
Ronald Cron6f135e12021-12-08 16:57:54 +01003705#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003706 mbedtls_ssl_transform_free( handshake->transform_handshake );
3707 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003708 mbedtls_free( handshake->transform_earlydata );
3709 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003710#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003711
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003712
3713#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3714 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3715 * processes datagrams and the fact that a datagram is allowed to have
3716 * several records in it, it is possible that the I/O buffers are not
3717 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003718 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3719 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003720#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003721
Jerry Yuba9c7272021-10-30 11:54:10 +08003722 /* mbedtls_platform_zeroize MUST be last one in this function */
3723 mbedtls_platform_zeroize( handshake,
3724 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003725}
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003728{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003729 if( session == NULL )
3730 return;
3731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003733 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003734#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003735
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003736#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003738#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003739
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003740#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3741 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003742 mbedtls_free( session->hostname );
3743#endif
3744
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003745 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003746}
3747
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003748#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003749
3750#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3751#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3752#else
3753#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3754#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3755
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003756#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003757
3758#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3759#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3760#else
3761#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3762#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3763
3764#if defined(MBEDTLS_SSL_ALPN)
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3766#else
3767#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3768#endif /* MBEDTLS_SSL_ALPN */
3769
3770#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3771#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3772#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3773#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3774
3775#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3776 ( (uint32_t) ( \
3777 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3778 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3779 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3780 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3781 0u ) )
3782
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003783static unsigned char ssl_serialized_context_header[] = {
3784 MBEDTLS_VERSION_MAJOR,
3785 MBEDTLS_VERSION_MINOR,
3786 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003787 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3788 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3789 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3790 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3791 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003792};
3793
Paul Bakker5121ce52009-01-03 21:22:43 +00003794/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003795 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003796 *
3797 * The format of the serialized data is:
3798 * (in the presentation language of TLS, RFC 8446 section 3)
3799 *
3800 * // header
3801 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003802 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003803 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003804 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003805 * Note: When updating the format, remember to keep these
3806 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003807 *
3808 * // session sub-structure
3809 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3810 * // transform sub-structure
3811 * uint8 random[64]; // ServerHello.random+ClientHello.random
3812 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3813 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3814 * // fields from ssl_context
3815 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3816 * uint64 in_window_top; // DTLS: last validated record seq_num
3817 * uint64 in_window; // DTLS: bitmask for replay protection
3818 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3819 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3820 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3821 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3822 *
3823 * Note that many fields of the ssl_context or sub-structures are not
3824 * serialized, as they fall in one of the following categories:
3825 *
3826 * 1. forced value (eg in_left must be 0)
3827 * 2. pointer to dynamically-allocated memory (eg session, transform)
3828 * 3. value can be re-derived from other data (eg session keys from MS)
3829 * 4. value was temporary (eg content of input buffer)
3830 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003831 */
3832int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3833 unsigned char *buf,
3834 size_t buf_len,
3835 size_t *olen )
3836{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003837 unsigned char *p = buf;
3838 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003839 size_t session_len;
3840 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003841
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003842 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003843 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3844 * this function's documentation.
3845 *
3846 * These are due to assumptions/limitations in the implementation. Some of
3847 * them are likely to stay (no handshake in progress) some might go away
3848 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003849 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003850 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003851 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003852 {
3853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003854 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003855 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003856 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003857 {
3858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003860 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003861 /* Double-check that sub-structures are indeed ready */
3862 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003863 {
3864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003866 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003867 /* There must be no pending incoming or outgoing data */
3868 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003869 {
3870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003871 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003872 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003873 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003874 {
3875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003877 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003878 /* Protocol must be DLTS, not TLS */
3879 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003883 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003884 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003885 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003886 {
3887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003889 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003890 /* We must be using an AEAD ciphersuite */
3891 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003892 {
3893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003895 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003896 /* Renegotiation must not be enabled */
3897#if defined(MBEDTLS_SSL_RENEGOTIATION)
3898 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003899 {
3900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003902 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003903#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003904
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003905 /*
3906 * Version and format identifier
3907 */
3908 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003909
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003910 if( used <= buf_len )
3911 {
3912 memcpy( p, ssl_serialized_context_header,
3913 sizeof( ssl_serialized_context_header ) );
3914 p += sizeof( ssl_serialized_context_header );
3915 }
3916
3917 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003918 * Session (length + data)
3919 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003920 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003921 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3922 return( ret );
3923
3924 used += 4 + session_len;
3925 if( used <= buf_len )
3926 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003927 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3928 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003929
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003930 ret = ssl_session_save( ssl->session, 1,
3931 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003932 if( ret != 0 )
3933 return( ret );
3934
3935 p += session_len;
3936 }
3937
3938 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003939 * Transform
3940 */
3941 used += sizeof( ssl->transform->randbytes );
3942 if( used <= buf_len )
3943 {
3944 memcpy( p, ssl->transform->randbytes,
3945 sizeof( ssl->transform->randbytes ) );
3946 p += sizeof( ssl->transform->randbytes );
3947 }
3948
3949#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3950 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3951 if( used <= buf_len )
3952 {
3953 *p++ = ssl->transform->in_cid_len;
3954 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3955 p += ssl->transform->in_cid_len;
3956
3957 *p++ = ssl->transform->out_cid_len;
3958 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3959 p += ssl->transform->out_cid_len;
3960 }
3961#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3962
3963 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003964 * Saved fields from top-level ssl_context structure
3965 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003966 used += 4;
3967 if( used <= buf_len )
3968 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003969 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3970 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003971 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003972
3973#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3974 used += 16;
3975 if( used <= buf_len )
3976 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003977 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3978 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003979
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003980 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3981 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003982 }
3983#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3984
3985#if defined(MBEDTLS_SSL_PROTO_DTLS)
3986 used += 1;
3987 if( used <= buf_len )
3988 {
3989 *p++ = ssl->disable_datagram_packing;
3990 }
3991#endif /* MBEDTLS_SSL_PROTO_DTLS */
3992
Jerry Yuae0b2e22021-10-08 15:21:19 +08003993 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003994 if( used <= buf_len )
3995 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003996 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3997 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003998 }
3999
4000#if defined(MBEDTLS_SSL_PROTO_DTLS)
4001 used += 2;
4002 if( used <= buf_len )
4003 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004004 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4005 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004006 }
4007#endif /* MBEDTLS_SSL_PROTO_DTLS */
4008
4009#if defined(MBEDTLS_SSL_ALPN)
4010 {
4011 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004012 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004013 : 0;
4014
4015 used += 1 + alpn_len;
4016 if( used <= buf_len )
4017 {
4018 *p++ = alpn_len;
4019
4020 if( ssl->alpn_chosen != NULL )
4021 {
4022 memcpy( p, ssl->alpn_chosen, alpn_len );
4023 p += alpn_len;
4024 }
4025 }
4026 }
4027#endif /* MBEDTLS_SSL_ALPN */
4028
4029 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004030 * Done
4031 */
4032 *olen = used;
4033
4034 if( used > buf_len )
4035 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004036
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004037 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4038
Hanno Becker43aefe22020-02-05 10:44:56 +00004039 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004040}
4041
4042/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004043 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004044 *
4045 * This internal version is wrapped by a public function that cleans up in
4046 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004047 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004048MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004049static int ssl_context_load( mbedtls_ssl_context *ssl,
4050 const unsigned char *buf,
4051 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004052{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004053 const unsigned char *p = buf;
4054 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004055 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004057
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004058 /*
4059 * The context should have been freshly setup or reset.
4060 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004061 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004062 * renegotiating, or if the user mistakenly loaded a session first.)
4063 */
4064 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4065 ssl->session != NULL )
4066 {
4067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4068 }
4069
4070 /*
4071 * We can't check that the config matches the initial one, but we can at
4072 * least check it matches the requirements for serializing.
4073 */
4074 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004075 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4076 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004077#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004078 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004079#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004080 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004081 {
4082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4083 }
4084
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004085 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4086
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004087 /*
4088 * Check version identifier
4089 */
4090 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4092
4093 if( memcmp( p, ssl_serialized_context_header,
4094 sizeof( ssl_serialized_context_header ) ) != 0 )
4095 {
4096 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4097 }
4098 p += sizeof( ssl_serialized_context_header );
4099
4100 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004101 * Session
4102 */
4103 if( (size_t)( end - p ) < 4 )
4104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4105
4106 session_len = ( (size_t) p[0] << 24 ) |
4107 ( (size_t) p[1] << 16 ) |
4108 ( (size_t) p[2] << 8 ) |
4109 ( (size_t) p[3] );
4110 p += 4;
4111
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004112 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004113 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004114 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004115 ssl->session_in = ssl->session;
4116 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004117 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004118
4119 if( (size_t)( end - p ) < session_len )
4120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4121
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004122 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004123 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004124 {
4125 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004126 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004127 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004128
4129 p += session_len;
4130
4131 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004132 * Transform
4133 */
4134
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004135 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004136 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004137 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004138 ssl->transform_in = ssl->transform;
4139 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004140 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004141
4142 /* Read random bytes and populate structure */
4143 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004146 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004147 ssl->session->ciphersuite,
4148 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004149#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004150 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004151#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004152 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4153 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004154 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004155 ssl->conf->endpoint,
4156 ssl );
4157 if( ret != 0 )
4158 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004159#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004160 p += sizeof( ssl->transform->randbytes );
4161
4162#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4163 /* Read connection IDs and store them */
4164 if( (size_t)( end - p ) < 1 )
4165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4166
4167 ssl->transform->in_cid_len = *p++;
4168
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004169 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4171
4172 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4173 p += ssl->transform->in_cid_len;
4174
4175 ssl->transform->out_cid_len = *p++;
4176
4177 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4179
4180 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4181 p += ssl->transform->out_cid_len;
4182#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4183
4184 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004185 * Saved fields from top-level ssl_context structure
4186 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004187 if( (size_t)( end - p ) < 4 )
4188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4189
4190 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4191 ( (uint32_t) p[1] << 16 ) |
4192 ( (uint32_t) p[2] << 8 ) |
4193 ( (uint32_t) p[3] );
4194 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004195
4196#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4197 if( (size_t)( end - p ) < 16 )
4198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4199
4200 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4201 ( (uint64_t) p[1] << 48 ) |
4202 ( (uint64_t) p[2] << 40 ) |
4203 ( (uint64_t) p[3] << 32 ) |
4204 ( (uint64_t) p[4] << 24 ) |
4205 ( (uint64_t) p[5] << 16 ) |
4206 ( (uint64_t) p[6] << 8 ) |
4207 ( (uint64_t) p[7] );
4208 p += 8;
4209
4210 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4211 ( (uint64_t) p[1] << 48 ) |
4212 ( (uint64_t) p[2] << 40 ) |
4213 ( (uint64_t) p[3] << 32 ) |
4214 ( (uint64_t) p[4] << 24 ) |
4215 ( (uint64_t) p[5] << 16 ) |
4216 ( (uint64_t) p[6] << 8 ) |
4217 ( (uint64_t) p[7] );
4218 p += 8;
4219#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4220
4221#if defined(MBEDTLS_SSL_PROTO_DTLS)
4222 if( (size_t)( end - p ) < 1 )
4223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4224
4225 ssl->disable_datagram_packing = *p++;
4226#endif /* MBEDTLS_SSL_PROTO_DTLS */
4227
Jerry Yud9a94fe2021-09-28 18:58:59 +08004228 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004230 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4231 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004232
4233#if defined(MBEDTLS_SSL_PROTO_DTLS)
4234 if( (size_t)( end - p ) < 2 )
4235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4236
4237 ssl->mtu = ( p[0] << 8 ) | p[1];
4238 p += 2;
4239#endif /* MBEDTLS_SSL_PROTO_DTLS */
4240
4241#if defined(MBEDTLS_SSL_ALPN)
4242 {
4243 uint8_t alpn_len;
4244 const char **cur;
4245
4246 if( (size_t)( end - p ) < 1 )
4247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4248
4249 alpn_len = *p++;
4250
4251 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4252 {
4253 /* alpn_chosen should point to an item in the configured list */
4254 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4255 {
4256 if( strlen( *cur ) == alpn_len &&
4257 memcmp( p, cur, alpn_len ) == 0 )
4258 {
4259 ssl->alpn_chosen = *cur;
4260 break;
4261 }
4262 }
4263 }
4264
4265 /* can only happen on conf mismatch */
4266 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4268
4269 p += alpn_len;
4270 }
4271#endif /* MBEDTLS_SSL_ALPN */
4272
4273 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004274 * Forced fields from top-level ssl_context structure
4275 *
4276 * Most of them already set to the correct value by mbedtls_ssl_init() and
4277 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4278 */
4279 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004280 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004281
Hanno Becker361b10d2019-08-30 10:42:49 +01004282 /* Adjust pointers for header fields of outgoing records to
4283 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004284 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004285
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004286#if defined(MBEDTLS_SSL_PROTO_DTLS)
4287 ssl->in_epoch = 1;
4288#endif
4289
4290 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4291 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004292 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4293 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004294 if( ssl->handshake != NULL )
4295 {
4296 mbedtls_ssl_handshake_free( ssl );
4297 mbedtls_free( ssl->handshake );
4298 ssl->handshake = NULL;
4299 }
4300
4301 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004302 * Done - should have consumed entire buffer
4303 */
4304 if( p != end )
4305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004306
4307 return( 0 );
4308}
4309
4310/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004311 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004312 */
4313int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4314 const unsigned char *buf,
4315 size_t len )
4316{
4317 int ret = ssl_context_load( context, buf, len );
4318
4319 if( ret != 0 )
4320 mbedtls_ssl_free( context );
4321
4322 return( ret );
4323}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004324#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004325
4326/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004327 * Free an SSL context
4328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004330{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004331 if( ssl == NULL )
4332 return;
4333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004335
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004336 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004337 {
sander-visserb8aa2072020-05-06 22:05:13 +02004338#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4339 size_t out_buf_len = ssl->out_buf_len;
4340#else
4341 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4342#endif
4343
Darryl Greenb33cc762019-11-28 14:29:44 +00004344 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004346 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 }
4348
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004349 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 {
sander-visserb8aa2072020-05-06 22:05:13 +02004351#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4352 size_t in_buf_len = ssl->in_buf_len;
4353#else
4354 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4355#endif
4356
Darryl Greenb33cc762019-11-28 14:29:44 +00004357 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004359 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 }
4361
Paul Bakker48916f92012-09-16 19:57:18 +00004362 if( ssl->transform )
4363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 mbedtls_ssl_transform_free( ssl->transform );
4365 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004366 }
4367
4368 if( ssl->handshake )
4369 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004370 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4372 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 mbedtls_free( ssl->handshake );
4375 mbedtls_free( ssl->transform_negotiate );
4376 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004377 }
4378
Ronald Cron6f135e12021-12-08 16:57:54 +01004379#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004380 mbedtls_ssl_transform_free( ssl->transform_application );
4381 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004382#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004383
Paul Bakkerc0463502013-02-14 11:19:38 +01004384 if( ssl->session )
4385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386 mbedtls_ssl_session_free( ssl->session );
4387 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004388 }
4389
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004390#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004391 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004393 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004395 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004396#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004397
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004398#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004400#endif
4401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004403
Paul Bakker86f04f42013-02-14 11:20:09 +01004404 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004405 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004406}
4407
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004408/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004409 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004410 */
4411void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4412{
4413 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4414}
4415
Gilles Peskineae270bf2021-06-02 00:05:29 +02004416/* The selection should be the same as mbedtls_x509_crt_profile_default in
4417 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004418 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004419 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004420 * about this list.
4421 */
Brett Warrene0edc842021-08-17 09:53:13 +01004422static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004423#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004424 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004425#endif
4426#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004427 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004428#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004429#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004430 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004431#endif
4432#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004433 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004434#endif
4435#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004436 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004437#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004438#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004439 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004440#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004441#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004442 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004443#endif
4444#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004445 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004446#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004447 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004448};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004449
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004450static int ssl_preset_suiteb_ciphersuites[] = {
4451 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4452 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4453 0
4454};
4455
Gilles Peskineeccd8882020-03-10 12:19:08 +01004456#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004457
Jerry Yu909df7b2022-01-22 11:56:27 +08004458/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004459 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004460 * rules SHOULD be upheld.
4461 * - No duplicate entries.
4462 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004463 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004464 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004465 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004466static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004467
Andrzej Kurek25f27152022-08-17 16:09:31 -04004468#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 +08004469 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4470 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004471#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004472 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004473
Andrzej Kurek25f27152022-08-17 16:09:31 -04004474#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 +08004475 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4476 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004477#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004478 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4479
Andrzej Kurek25f27152022-08-17 16:09:31 -04004480#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 +08004481 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4482 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004483#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004484 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004485
Andrzej Kurek25f27152022-08-17 16:09:31 -04004486#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 +08004487 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#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 +08004489
Andrzej Kurek25f27152022-08-17 16:09:31 -04004490#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 +08004491 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#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 +08004493
Andrzej Kurek25f27152022-08-17 16:09:31 -04004494#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 +08004495 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004496#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 +08004497
Andrzej Kurek25f27152022-08-17 16:09:31 -04004498#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 +08004499 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4500#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4501
Andrzej Kurek25f27152022-08-17 16:09:31 -04004502#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 +08004503 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4504#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4505
Andrzej Kurek25f27152022-08-17 16:09:31 -04004506#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 +08004507 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4508#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4509
Gabor Mezei15b95a62022-05-09 16:37:58 +02004510 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004511};
4512
4513/* NOTICE: see above */
4514#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4515static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004516#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004517#if defined(MBEDTLS_ECDSA_C)
4518 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004519#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004520#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4521 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4522#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004523#if defined(MBEDTLS_RSA_C)
4524 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4525#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004526#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004527#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004528#if defined(MBEDTLS_ECDSA_C)
4529 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004530#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004531#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4532 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4533#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004534#if defined(MBEDTLS_RSA_C)
4535 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4536#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004537#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004538#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004539#if defined(MBEDTLS_ECDSA_C)
4540 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004541#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004542#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4543 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4544#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004545#if defined(MBEDTLS_RSA_C)
4546 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4547#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004548#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004549 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004550};
4551#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4552/* NOTICE: see above */
4553static uint16_t ssl_preset_suiteb_sig_algs[] = {
4554
Andrzej Kurek25f27152022-08-17 16:09:31 -04004555#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 +08004556 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4557 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004558#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004559 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4560
Andrzej Kurek25f27152022-08-17 16:09:31 -04004561#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 +08004562 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4563 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004564#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004565 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004566
Andrzej Kurek25f27152022-08-17 16:09:31 -04004567#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 +08004568 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004569#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 +08004570
Andrzej Kurek25f27152022-08-17 16:09:31 -04004571#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 +08004572 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004573#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004574
Gabor Mezei15b95a62022-05-09 16:37:58 +02004575 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004576};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004577
Jerry Yu909df7b2022-01-22 11:56:27 +08004578/* NOTICE: see above */
4579#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4580static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004581#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004582#if defined(MBEDTLS_ECDSA_C)
4583 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004584#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004585#if defined(MBEDTLS_RSA_C)
4586 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4587#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004588#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004589#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004590#if defined(MBEDTLS_ECDSA_C)
4591 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004592#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004593#if defined(MBEDTLS_RSA_C)
4594 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4595#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004596#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004597 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004598};
Jerry Yu909df7b2022-01-22 11:56:27 +08004599#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4600
Jerry Yu1a8b4812022-01-20 17:56:50 +08004601#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004602
Brett Warrene0edc842021-08-17 09:53:13 +01004603static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004604#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004605 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004606#endif
4607#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004608 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004609#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004610 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004611};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004612
Jerry Yu1a8b4812022-01-20 17:56:50 +08004613#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004614/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004615 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004616MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004617static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004618{
4619 size_t i, j;
4620 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004621
Gabor Mezei15b95a62022-05-09 16:37:58 +02004622 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004623 {
Jerry Yuf377d642022-01-25 10:43:59 +08004624 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004625 {
Jerry Yuf377d642022-01-25 10:43:59 +08004626 if( sig_algs[i] != sig_algs[j] )
4627 continue;
4628 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4629 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4630 sig_algs[i], j, i );
4631 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004632 }
4633 }
4634 return( ret );
4635}
4636
4637#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4638
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004639/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004640 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004641 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004642int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004643 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004644{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004645#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004647#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004648
Jerry Yu1a8b4812022-01-20 17:56:50 +08004649#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004650 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004651 {
4652 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4653 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4654 }
4655
Jerry Yuf377d642022-01-25 10:43:59 +08004656 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004657 {
4658 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4659 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4660 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004661
4662#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004663 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004664 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004665 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004666 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4667 }
4668
Jerry Yuf377d642022-01-25 10:43:59 +08004669 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004670 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004671 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004672 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4673 }
4674#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004675#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4676
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004677 /* Use the functions here so that they are covered in tests,
4678 * but otherwise access member directly for efficiency */
4679 mbedtls_ssl_conf_endpoint( conf, endpoint );
4680 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004681
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004682 /*
4683 * Things that are common to all presets
4684 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004685#if defined(MBEDTLS_SSL_CLI_C)
4686 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4687 {
4688 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4689#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4690 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4691#endif
4692 }
4693#endif
4694
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004695#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4696 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4697#endif
4698
4699#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4700 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4701#endif
4702
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004703#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004704 conf->f_cookie_write = ssl_cookie_write_dummy;
4705 conf->f_cookie_check = ssl_cookie_check_dummy;
4706#endif
4707
4708#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4709 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4710#endif
4711
Janos Follath088ce432017-04-10 12:42:31 +01004712#if defined(MBEDTLS_SSL_SRV_C)
4713 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004714 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004715#endif
4716
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004717#if defined(MBEDTLS_SSL_PROTO_DTLS)
4718 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4719 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4720#endif
4721
4722#if defined(MBEDTLS_SSL_RENEGOTIATION)
4723 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004724 memset( conf->renego_period, 0x00, 2 );
4725 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004726#endif
4727
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004728#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004729 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4730 {
4731 const unsigned char dhm_p[] =
4732 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4733 const unsigned char dhm_g[] =
4734 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004735
Hanno Beckere2defad2021-07-24 05:59:17 +01004736 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4737 dhm_p, sizeof( dhm_p ),
4738 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4739 {
4740 return( ret );
4741 }
4742 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004743#endif
4744
Ronald Cron6f135e12021-12-08 16:57:54 +01004745#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004746#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004747 mbedtls_ssl_conf_new_session_tickets(
4748 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4749#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004750 /*
4751 * Allow all TLS 1.3 key exchange modes by default.
4752 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004753 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004754#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004755
XiaokangQian4d3a6042022-04-21 13:46:17 +00004756 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4757 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004758#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004759 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004760 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004761#else
XiaokangQian060d8672022-04-21 09:24:56 +00004762 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004763#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004764 }
4765 else
4766 {
4767#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4768 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4769 {
4770 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4771 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4772 }
4773 else
4774 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4775 {
4776 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4777 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4778 }
4779#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4780 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4781 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4782#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4783 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4784 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4785#else
4786 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4787#endif
4788 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004789
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004790 /*
4791 * Preset-specific defaults
4792 */
4793 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004794 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004795 /*
4796 * NSA Suite B
4797 */
4798 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004799
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004800 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004801
4802#if defined(MBEDTLS_X509_CRT_PARSE_C)
4803 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004804#endif
4805
Gilles Peskineeccd8882020-03-10 12:19:08 +01004806#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004807#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4808 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4809 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4810 else
4811#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4812 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004813#endif
4814
Brett Warrene0edc842021-08-17 09:53:13 +01004815#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4816 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004817#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004818 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004819 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004820
4821 /*
4822 * Default
4823 */
4824 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004825
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004826 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004827
4828#if defined(MBEDTLS_X509_CRT_PARSE_C)
4829 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4830#endif
4831
Gilles Peskineeccd8882020-03-10 12:19:08 +01004832#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004833#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4834 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4835 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4836 else
4837#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4838 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004839#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004840
Brett Warrene0edc842021-08-17 09:53:13 +01004841#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4842 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004843#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004844 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004845
4846#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4847 conf->dhm_min_bitlen = 1024;
4848#endif
4849 }
4850
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004851 return( 0 );
4852}
4853
4854/*
4855 * Free mbedtls_ssl_config
4856 */
4857void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4858{
4859#if defined(MBEDTLS_DHM_C)
4860 mbedtls_mpi_free( &conf->dhm_P );
4861 mbedtls_mpi_free( &conf->dhm_G );
4862#endif
4863
Gilles Peskineeccd8882020-03-10 12:19:08 +01004864#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004865#if defined(MBEDTLS_USE_PSA_CRYPTO)
4866 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4867 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004868 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4869 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004870#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004871 if( conf->psk != NULL )
4872 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004873 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004874 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004875 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004876 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004877 }
4878
4879 if( conf->psk_identity != NULL )
4880 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004881 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004882 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004883 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004884 conf->psk_identity_len = 0;
4885 }
4886#endif
4887
4888#if defined(MBEDTLS_X509_CRT_PARSE_C)
4889 ssl_key_cert_free( conf->key_cert );
4890#endif
4891
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004892 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004893}
4894
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004895#if defined(MBEDTLS_PK_C) && \
4896 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004897/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902#if defined(MBEDTLS_RSA_C)
4903 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4904 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906#if defined(MBEDTLS_ECDSA_C)
4907 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4908 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004911}
4912
Hanno Becker7e5437a2017-04-28 17:15:26 +01004913unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4914{
4915 switch( type ) {
4916 case MBEDTLS_PK_RSA:
4917 return( MBEDTLS_SSL_SIG_RSA );
4918 case MBEDTLS_PK_ECDSA:
4919 case MBEDTLS_PK_ECKEY:
4920 return( MBEDTLS_SSL_SIG_ECDSA );
4921 default:
4922 return( MBEDTLS_SSL_SIG_ANON );
4923 }
4924}
4925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004927{
4928 switch( sig )
4929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930#if defined(MBEDTLS_RSA_C)
4931 case MBEDTLS_SSL_SIG_RSA:
4932 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if defined(MBEDTLS_ECDSA_C)
4935 case MBEDTLS_SSL_SIG_ECDSA:
4936 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004937#endif
4938 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004940 }
4941}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004942#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004943
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004944/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004945 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004948{
4949 switch( hash )
4950 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004951#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952 case MBEDTLS_SSL_HASH_MD5:
4953 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004954#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004955#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956 case MBEDTLS_SSL_HASH_SHA1:
4957 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004958#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004959#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960 case MBEDTLS_SSL_HASH_SHA224:
4961 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004962#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004963#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 case MBEDTLS_SSL_HASH_SHA256:
4965 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004966#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004967#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 case MBEDTLS_SSL_HASH_SHA384:
4969 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004970#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004971#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 case MBEDTLS_SSL_HASH_SHA512:
4973 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004974#endif
4975 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004977 }
4978}
4979
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004980/*
4981 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4982 */
4983unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4984{
4985 switch( md )
4986 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004987#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004988 case MBEDTLS_MD_MD5:
4989 return( MBEDTLS_SSL_HASH_MD5 );
4990#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004991#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004992 case MBEDTLS_MD_SHA1:
4993 return( MBEDTLS_SSL_HASH_SHA1 );
4994#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004995#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004996 case MBEDTLS_MD_SHA224:
4997 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004998#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004999#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005000 case MBEDTLS_MD_SHA256:
5001 return( MBEDTLS_SSL_HASH_SHA256 );
5002#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005003#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005004 case MBEDTLS_MD_SHA384:
5005 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005006#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005007#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005008 case MBEDTLS_MD_SHA512:
5009 return( MBEDTLS_SSL_HASH_SHA512 );
5010#endif
5011 default:
5012 return( MBEDTLS_SSL_HASH_NONE );
5013 }
5014}
5015
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005016/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005017 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005018 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005019 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005020int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005021{
Brett Warrene0edc842021-08-17 09:53:13 +01005022 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005023
Brett Warrene0edc842021-08-17 09:53:13 +01005024 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005025 return( -1 );
5026
Brett Warrene0edc842021-08-17 09:53:13 +01005027 for( ; *group_list != 0; group_list++ )
5028 {
5029 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005030 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005031 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005032
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005033 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005034}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005035
5036#if defined(MBEDTLS_ECP_C)
5037/*
5038 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5039 */
5040int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5041{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005042 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005043 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005044
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005045 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005046 return -1;
5047
5048 uint16_t tls_id = grp_info->tls_id;
5049
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005050 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5051}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005052#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005054#if defined(MBEDTLS_X509_CRT_PARSE_C)
5055int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5056 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005057 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005058 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005059{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005060 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005061 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005062 const char *ext_oid;
5063 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005066 {
5067 /* Server part of the key exchange */
5068 switch( ciphersuite->key_exchange )
5069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 case MBEDTLS_KEY_EXCHANGE_RSA:
5071 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005072 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005073 break;
5074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5076 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5077 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5078 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005079 break;
5080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5082 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005083 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005084 break;
5085
5086 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087 case MBEDTLS_KEY_EXCHANGE_NONE:
5088 case MBEDTLS_KEY_EXCHANGE_PSK:
5089 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5090 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005091 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005092 usage = 0;
5093 }
5094 }
5095 else
5096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5098 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005099 }
5100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005102 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005103 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005104 ret = -1;
5105 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5110 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005111 }
5112 else
5113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5115 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005116 }
5117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005119 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005120 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005121 ret = -1;
5122 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005123
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005124 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005127
Jerry Yu148165c2021-09-24 23:20:59 +08005128#if defined(MBEDTLS_USE_PSA_CRYPTO)
5129int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5130 const mbedtls_md_type_t md,
5131 unsigned char *dst,
5132 size_t dst_len,
5133 size_t *olen )
5134{
Ronald Cronf6893e12022-01-07 22:09:01 +01005135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5136 psa_hash_operation_t *hash_operation_to_clone;
5137 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5138
Jerry Yu148165c2021-09-24 23:20:59 +08005139 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005140
5141 switch( md )
5142 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005143#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005144 case MBEDTLS_MD_SHA384:
5145 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5146 break;
5147#endif
5148
Andrzej Kurek25f27152022-08-17 16:09:31 -04005149#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005150 case MBEDTLS_MD_SHA256:
5151 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5152 break;
5153#endif
5154
5155 default:
5156 goto exit;
5157 }
5158
5159 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5160 if( status != PSA_SUCCESS )
5161 goto exit;
5162
5163 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5164 if( status != PSA_SUCCESS )
5165 goto exit;
5166
5167exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005168 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005169}
5170#else /* MBEDTLS_USE_PSA_CRYPTO */
5171
Andrzej Kurek25f27152022-08-17 16:09:31 -04005172#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005173MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005174static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5175 unsigned char *dst,
5176 size_t dst_len,
5177 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005178{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005179 int ret;
5180 mbedtls_sha512_context sha512;
5181
5182 if( dst_len < 48 )
5183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5184
5185 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005186 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005187
5188 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5189 {
5190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5191 goto exit;
5192 }
5193
5194 *olen = 48;
5195
5196exit:
5197
5198 mbedtls_sha512_free( &sha512 );
5199 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005200}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005201#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005202
Andrzej Kurek25f27152022-08-17 16:09:31 -04005203#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005204MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005205static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5206 unsigned char *dst,
5207 size_t dst_len,
5208 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005209{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005210 int ret;
5211 mbedtls_sha256_context sha256;
5212
5213 if( dst_len < 32 )
5214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5215
5216 mbedtls_sha256_init( &sha256 );
5217 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005218
Jerry Yu24c0ec32021-09-09 14:21:07 +08005219 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5220 {
5221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5222 goto exit;
5223 }
5224
5225 *olen = 32;
5226
5227exit:
5228
5229 mbedtls_sha256_free( &sha256 );
5230 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005231}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005232#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005233
Jerry Yu000f9762021-09-14 11:12:51 +08005234int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5235 const mbedtls_md_type_t md,
5236 unsigned char *dst,
5237 size_t dst_len,
5238 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005239{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005240 switch( md )
5241 {
5242
Andrzej Kurek25f27152022-08-17 16:09:31 -04005243#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005244 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005245 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005246#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005247
Andrzej Kurek25f27152022-08-17 16:09:31 -04005248#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005249 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005250 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005251#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005252
5253 default:
5254 break;
5255 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005257}
XiaokangQian647719a2021-12-07 09:16:29 +00005258
Jerry Yu148165c2021-09-24 23:20:59 +08005259#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005260
Gabor Mezei078e8032022-04-27 21:17:56 +02005261#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5262/* mbedtls_ssl_parse_sig_alg_ext()
5263 *
5264 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5265 * value (TLS 1.3 RFC8446):
5266 * enum {
5267 * ....
5268 * ecdsa_secp256r1_sha256( 0x0403 ),
5269 * ecdsa_secp384r1_sha384( 0x0503 ),
5270 * ecdsa_secp521r1_sha512( 0x0603 ),
5271 * ....
5272 * } SignatureScheme;
5273 *
5274 * struct {
5275 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5276 * } SignatureSchemeList;
5277 *
5278 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5279 * value (TLS 1.2 RFC5246):
5280 * enum {
5281 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5282 * sha512(6), (255)
5283 * } HashAlgorithm;
5284 *
5285 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5286 * SignatureAlgorithm;
5287 *
5288 * struct {
5289 * HashAlgorithm hash;
5290 * SignatureAlgorithm signature;
5291 * } SignatureAndHashAlgorithm;
5292 *
5293 * SignatureAndHashAlgorithm
5294 * supported_signature_algorithms<2..2^16-2>;
5295 *
5296 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5297 * generalization of the TLS 1.2 signature algorithm extension.
5298 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5299 * `SignatureScheme` field of TLS 1.3
5300 *
5301 */
5302int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5303 const unsigned char *buf,
5304 const unsigned char *end )
5305{
5306 const unsigned char *p = buf;
5307 size_t supported_sig_algs_len = 0;
5308 const unsigned char *supported_sig_algs_end;
5309 uint16_t sig_alg;
5310 uint32_t common_idx = 0;
5311
5312 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5313 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5314 p += 2;
5315
5316 memset( ssl->handshake->received_sig_algs, 0,
5317 sizeof(ssl->handshake->received_sig_algs) );
5318
5319 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5320 supported_sig_algs_end = p + supported_sig_algs_len;
5321 while( p < supported_sig_algs_end )
5322 {
5323 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5324 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5325 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005326 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5327 sig_alg,
5328 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005329#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005330 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005331 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5332 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5333 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005334 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005335 }
5336#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005337
Jerry Yuf3b46b52022-06-19 16:52:27 +08005338 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5339 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005340
5341 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5342 {
5343 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5344 common_idx += 1;
5345 }
5346 }
5347 /* Check that we consumed all the message. */
5348 if( p != end )
5349 {
5350 MBEDTLS_SSL_DEBUG_MSG( 1,
5351 ( "Signature algorithms extension length misaligned" ) );
5352 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5353 MBEDTLS_ERR_SSL_DECODE_ERROR );
5354 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5355 }
5356
5357 if( common_idx == 0 )
5358 {
5359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5360 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5361 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5362 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5363 }
5364
Gabor Mezei15b95a62022-05-09 16:37:58 +02005365 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005366 return( 0 );
5367}
5368
5369#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5370
Jerry Yudc7bd172022-02-17 13:44:15 +08005371#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5372
5373#if defined(MBEDTLS_USE_PSA_CRYPTO)
5374
5375static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5376 mbedtls_svc_key_id_t key,
5377 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005378 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005379 const unsigned char* seed, size_t seed_length,
5380 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005381 const unsigned char* other_secret,
5382 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005383 size_t capacity )
5384{
5385 psa_status_t status;
5386
5387 status = psa_key_derivation_setup( derivation, alg );
5388 if( status != PSA_SUCCESS )
5389 return( status );
5390
5391 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5392 {
5393 status = psa_key_derivation_input_bytes( derivation,
5394 PSA_KEY_DERIVATION_INPUT_SEED,
5395 seed, seed_length );
5396 if( status != PSA_SUCCESS )
5397 return( status );
5398
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005399 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005400 {
5401 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005402 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5403 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005404 if( status != PSA_SUCCESS )
5405 return( status );
5406 }
5407
Jerry Yudc7bd172022-02-17 13:44:15 +08005408 if( mbedtls_svc_key_id_is_null( key ) )
5409 {
5410 status = psa_key_derivation_input_bytes(
5411 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005412 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005413 }
5414 else
5415 {
5416 status = psa_key_derivation_input_key(
5417 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5418 }
5419 if( status != PSA_SUCCESS )
5420 return( status );
5421
5422 status = psa_key_derivation_input_bytes( derivation,
5423 PSA_KEY_DERIVATION_INPUT_LABEL,
5424 label, label_length );
5425 if( status != PSA_SUCCESS )
5426 return( status );
5427 }
5428 else
5429 {
5430 return( PSA_ERROR_NOT_SUPPORTED );
5431 }
5432
5433 status = psa_key_derivation_set_capacity( derivation, capacity );
5434 if( status != PSA_SUCCESS )
5435 return( status );
5436
5437 return( PSA_SUCCESS );
5438}
5439
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005440MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005441static int tls_prf_generic( mbedtls_md_type_t md_type,
5442 const unsigned char *secret, size_t slen,
5443 const char *label,
5444 const unsigned char *random, size_t rlen,
5445 unsigned char *dstbuf, size_t dlen )
5446{
5447 psa_status_t status;
5448 psa_algorithm_t alg;
5449 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5450 psa_key_derivation_operation_t derivation =
5451 PSA_KEY_DERIVATION_OPERATION_INIT;
5452
5453 if( md_type == MBEDTLS_MD_SHA384 )
5454 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5455 else
5456 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5457
5458 /* Normally a "secret" should be long enough to be impossible to
5459 * find by brute force, and in particular should not be empty. But
5460 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5461 * and for this use case it makes sense to have a 0-length "secret".
5462 * Since the key API doesn't allow importing a key of length 0,
5463 * keep master_key=0, which setup_psa_key_derivation() understands
5464 * to mean a 0-length "secret" input. */
5465 if( slen != 0 )
5466 {
5467 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5468 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5469 psa_set_key_algorithm( &key_attributes, alg );
5470 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5471
5472 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5473 if( status != PSA_SUCCESS )
5474 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5475 }
5476
5477 status = setup_psa_key_derivation( &derivation,
5478 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005479 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005480 random, rlen,
5481 (unsigned char const *) label,
5482 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005483 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005484 dlen );
5485 if( status != PSA_SUCCESS )
5486 {
5487 psa_key_derivation_abort( &derivation );
5488 psa_destroy_key( master_key );
5489 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5490 }
5491
5492 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5493 if( status != PSA_SUCCESS )
5494 {
5495 psa_key_derivation_abort( &derivation );
5496 psa_destroy_key( master_key );
5497 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5498 }
5499
5500 status = psa_key_derivation_abort( &derivation );
5501 if( status != PSA_SUCCESS )
5502 {
5503 psa_destroy_key( master_key );
5504 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5505 }
5506
5507 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5508 status = psa_destroy_key( master_key );
5509 if( status != PSA_SUCCESS )
5510 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5511
5512 return( 0 );
5513}
5514
5515#else /* MBEDTLS_USE_PSA_CRYPTO */
5516
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005517MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005518static int tls_prf_generic( mbedtls_md_type_t md_type,
5519 const unsigned char *secret, size_t slen,
5520 const char *label,
5521 const unsigned char *random, size_t rlen,
5522 unsigned char *dstbuf, size_t dlen )
5523{
5524 size_t nb;
5525 size_t i, j, k, md_len;
5526 unsigned char *tmp;
5527 size_t tmp_len = 0;
5528 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5529 const mbedtls_md_info_t *md_info;
5530 mbedtls_md_context_t md_ctx;
5531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5532
5533 mbedtls_md_init( &md_ctx );
5534
5535 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5537
5538 md_len = mbedtls_md_get_size( md_info );
5539
5540 tmp_len = md_len + strlen( label ) + rlen;
5541 tmp = mbedtls_calloc( 1, tmp_len );
5542 if( tmp == NULL )
5543 {
5544 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5545 goto exit;
5546 }
5547
5548 nb = strlen( label );
5549 memcpy( tmp + md_len, label, nb );
5550 memcpy( tmp + md_len + nb, random, rlen );
5551 nb += rlen;
5552
5553 /*
5554 * Compute P_<hash>(secret, label + random)[0..dlen]
5555 */
5556 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5557 goto exit;
5558
5559 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5560 if( ret != 0 )
5561 goto exit;
5562 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5563 if( ret != 0 )
5564 goto exit;
5565 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5566 if( ret != 0 )
5567 goto exit;
5568
5569 for( i = 0; i < dlen; i += md_len )
5570 {
5571 ret = mbedtls_md_hmac_reset ( &md_ctx );
5572 if( ret != 0 )
5573 goto exit;
5574 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5575 if( ret != 0 )
5576 goto exit;
5577 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5578 if( ret != 0 )
5579 goto exit;
5580
5581 ret = mbedtls_md_hmac_reset ( &md_ctx );
5582 if( ret != 0 )
5583 goto exit;
5584 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5585 if( ret != 0 )
5586 goto exit;
5587 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5588 if( ret != 0 )
5589 goto exit;
5590
5591 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5592
5593 for( j = 0; j < k; j++ )
5594 dstbuf[i + j] = h_i[j];
5595 }
5596
5597exit:
5598 mbedtls_md_free( &md_ctx );
5599
5600 mbedtls_platform_zeroize( tmp, tmp_len );
5601 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5602
5603 mbedtls_free( tmp );
5604
5605 return( ret );
5606}
5607#endif /* MBEDTLS_USE_PSA_CRYPTO */
5608
Andrzej Kurek25f27152022-08-17 16:09:31 -04005609#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005610MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005611static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5612 const char *label,
5613 const unsigned char *random, size_t rlen,
5614 unsigned char *dstbuf, size_t dlen )
5615{
5616 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5617 label, random, rlen, dstbuf, dlen ) );
5618}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005619#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005620
Andrzej Kurek25f27152022-08-17 16:09:31 -04005621#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005622MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005623static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5624 const char *label,
5625 const unsigned char *random, size_t rlen,
5626 unsigned char *dstbuf, size_t dlen )
5627{
5628 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5629 label, random, rlen, dstbuf, dlen ) );
5630}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005631#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005632
Jerry Yuf009d862022-02-17 14:01:37 +08005633/*
5634 * Set appropriate PRF function and other SSL / TLS1.2 functions
5635 *
5636 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005637 * - hash associated with the ciphersuite (only used by TLS 1.2)
5638 *
5639 * Outputs:
5640 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5641 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005642MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005643static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005644 mbedtls_md_type_t hash )
5645{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005646#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005647 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005648 {
5649 handshake->tls_prf = tls_prf_sha384;
5650 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5651 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5652 }
5653 else
5654#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005655#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005656 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005657 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005658 handshake->tls_prf = tls_prf_sha256;
5659 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5660 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5661 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005662#else
Jerry Yuf009d862022-02-17 14:01:37 +08005663 {
Jerry Yuf009d862022-02-17 14:01:37 +08005664 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005665 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005666 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5667 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005668#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005669
5670 return( 0 );
5671}
Jerry Yud6ab2352022-02-17 14:03:43 +08005672
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005673/*
5674 * Compute master secret if needed
5675 *
5676 * Parameters:
5677 * [in/out] handshake
5678 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5679 * (PSA-PSK) ciphersuite_info, psk_opaque
5680 * [out] premaster (cleared)
5681 * [out] master
5682 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5683 * debug: conf->f_dbg, conf->p_dbg
5684 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005685 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005686 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005687MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005688static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5689 unsigned char *master,
5690 const mbedtls_ssl_context *ssl )
5691{
5692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5693
5694 /* cf. RFC 5246, Section 8.1:
5695 * "The master secret is always exactly 48 bytes in length." */
5696 size_t const master_secret_len = 48;
5697
5698#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5699 unsigned char session_hash[48];
5700#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5701
5702 /* The label for the KDF used for key expansion.
5703 * This is either "master secret" or "extended master secret"
5704 * depending on whether the Extended Master Secret extension
5705 * is used. */
5706 char const *lbl = "master secret";
5707
Przemek Stekielae4ed302022-04-05 17:15:55 +02005708 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005709 * - If the Extended Master Secret extension is not used,
5710 * this is ClientHello.Random + ServerHello.Random
5711 * (see Sect. 8.1 in RFC 5246).
5712 * - If the Extended Master Secret extension is used,
5713 * this is the transcript of the handshake so far.
5714 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005715 unsigned char const *seed = handshake->randbytes;
5716 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005717
5718#if !defined(MBEDTLS_DEBUG_C) && \
5719 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5720 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5721 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5722 ssl = NULL; /* make sure we don't use it except for those cases */
5723 (void) ssl;
5724#endif
5725
5726 if( handshake->resume != 0 )
5727 {
5728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5729 return( 0 );
5730 }
5731
5732#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5733 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5734 {
5735 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005736 seed = session_hash;
5737 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005738
5739 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005740 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005741 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005742#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005743
Przemek Stekiel99114f32022-04-22 11:20:09 +02005744#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005745 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005746 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005747 {
5748 /* Perform PSK-to-MS expansion in a single step. */
5749 psa_status_t status;
5750 psa_algorithm_t alg;
5751 mbedtls_svc_key_id_t psk;
5752 psa_key_derivation_operation_t derivation =
5753 PSA_KEY_DERIVATION_OPERATION_INIT;
5754 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5755
5756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5757
5758 psk = mbedtls_ssl_get_opaque_psk( ssl );
5759
5760 if( hash_alg == MBEDTLS_MD_SHA384 )
5761 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5762 else
5763 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5764
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005765 size_t other_secret_len = 0;
5766 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005767
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005768 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005769 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005770 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005771 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005772 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005773 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005774 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005775 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005776 other_secret_len = 48;
5777 other_secret = handshake->premaster + 2;
5778 break;
5779 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005780 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005781 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5782 other_secret = handshake->premaster + 2;
5783 break;
5784 default:
5785 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005786 }
5787
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005788 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005789 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005790 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005791 (unsigned char const *) lbl,
5792 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005793 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005794 master_secret_len );
5795 if( status != PSA_SUCCESS )
5796 {
5797 psa_key_derivation_abort( &derivation );
5798 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5799 }
5800
5801 status = psa_key_derivation_output_bytes( &derivation,
5802 master,
5803 master_secret_len );
5804 if( status != PSA_SUCCESS )
5805 {
5806 psa_key_derivation_abort( &derivation );
5807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5808 }
5809
5810 status = psa_key_derivation_abort( &derivation );
5811 if( status != PSA_SUCCESS )
5812 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5813 }
5814 else
5815#endif
5816 {
5817 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005818 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005819 master,
5820 master_secret_len );
5821 if( ret != 0 )
5822 {
5823 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5824 return( ret );
5825 }
5826
5827 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5828 handshake->premaster,
5829 handshake->pmslen );
5830
5831 mbedtls_platform_zeroize( handshake->premaster,
5832 sizeof(handshake->premaster) );
5833 }
5834
5835 return( 0 );
5836}
5837
Jerry Yud62f87e2022-02-17 14:09:02 +08005838int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5839{
5840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5841 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5842 ssl->handshake->ciphersuite_info;
5843
5844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5845
5846 /* Set PRF, calc_verify and calc_finished function pointers */
5847 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005848 ciphersuite_info->mac );
5849 if( ret != 0 )
5850 {
5851 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5852 return( ret );
5853 }
5854
5855 /* Compute master secret if needed */
5856 ret = ssl_compute_master( ssl->handshake,
5857 ssl->session_negotiate->master,
5858 ssl );
5859 if( ret != 0 )
5860 {
5861 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5862 return( ret );
5863 }
5864
5865 /* Swap the client and server random values:
5866 * - MS derivation wanted client+server (RFC 5246 8.1)
5867 * - key derivation wants server+client (RFC 5246 6.3) */
5868 {
5869 unsigned char tmp[64];
5870 memcpy( tmp, ssl->handshake->randbytes, 64 );
5871 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5872 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5873 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5874 }
5875
5876 /* Populate transform structure */
5877 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5878 ssl->session_negotiate->ciphersuite,
5879 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005880#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005881 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005882#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005883 ssl->handshake->tls_prf,
5884 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005885 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005886 ssl->conf->endpoint,
5887 ssl );
5888 if( ret != 0 )
5889 {
5890 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5891 return( ret );
5892 }
5893
5894 /* We no longer need Server/ClientHello.random values */
5895 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5896 sizeof( ssl->handshake->randbytes ) );
5897
5898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5899
5900 return( 0 );
5901}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005902
Ronald Cron4dcbca92022-03-07 10:21:40 +01005903int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5904{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005905 switch( md )
5906 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005907#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005908 case MBEDTLS_SSL_HASH_SHA384:
5909 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5910 break;
5911#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005912#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005913 case MBEDTLS_SSL_HASH_SHA256:
5914 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5915 break;
5916#endif
5917 default:
5918 return( -1 );
5919 }
5920
Ronald Cronc2f13a02022-03-07 10:25:24 +01005921 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005922}
5923
Andrzej Kurek25f27152022-08-17 16:09:31 -04005924#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005925void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5926 unsigned char *hash,
5927 size_t *hlen )
5928{
5929#if defined(MBEDTLS_USE_PSA_CRYPTO)
5930 size_t hash_size;
5931 psa_status_t status;
5932 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5933
5934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5935 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5936 if( status != PSA_SUCCESS )
5937 {
5938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5939 return;
5940 }
5941
5942 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5943 if( status != PSA_SUCCESS )
5944 {
5945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5946 return;
5947 }
5948
5949 *hlen = 32;
5950 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5952#else
5953 mbedtls_sha256_context sha256;
5954
5955 mbedtls_sha256_init( &sha256 );
5956
5957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5958
5959 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5960 mbedtls_sha256_finish( &sha256, hash );
5961
5962 *hlen = 32;
5963
5964 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5966
5967 mbedtls_sha256_free( &sha256 );
5968#endif /* MBEDTLS_USE_PSA_CRYPTO */
5969 return;
5970}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005971#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005972
Andrzej Kurek25f27152022-08-17 16:09:31 -04005973#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005974void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5975 unsigned char *hash,
5976 size_t *hlen )
5977{
5978#if defined(MBEDTLS_USE_PSA_CRYPTO)
5979 size_t hash_size;
5980 psa_status_t status;
5981 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5982
5983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5984 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5985 if( status != PSA_SUCCESS )
5986 {
5987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5988 return;
5989 }
5990
5991 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5992 if( status != PSA_SUCCESS )
5993 {
5994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5995 return;
5996 }
5997
5998 *hlen = 48;
5999 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6001#else
6002 mbedtls_sha512_context sha512;
6003
6004 mbedtls_sha512_init( &sha512 );
6005
6006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6007
Andrzej Kureka242e832022-08-11 10:03:14 -04006008 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006009 mbedtls_sha512_finish( &sha512, hash );
6010
6011 *hlen = 48;
6012
6013 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6015
6016 mbedtls_sha512_free( &sha512 );
6017#endif /* MBEDTLS_USE_PSA_CRYPTO */
6018 return;
6019}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006020#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006021
Neil Armstrong80f6f322022-05-03 17:56:38 +02006022#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6023 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006024int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6025{
6026 unsigned char *p = ssl->handshake->premaster;
6027 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6028 const unsigned char *psk = NULL;
6029 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006030 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006031
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006032 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006033 {
6034 /*
6035 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006036 * checked before calling this function.
6037 *
6038 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006039 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006040 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006041 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6042 {
6043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6045 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006046 }
6047
6048 /*
6049 * PMS = struct {
6050 * opaque other_secret<0..2^16-1>;
6051 * opaque psk<0..2^16-1>;
6052 * };
6053 * with "other_secret" depending on the particular key exchange
6054 */
6055#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6056 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6057 {
6058 if( end - p < 2 )
6059 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6060
6061 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6062 p += 2;
6063
6064 if( end < p || (size_t)( end - p ) < psk_len )
6065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6066
6067 memset( p, 0, psk_len );
6068 p += psk_len;
6069 }
6070 else
6071#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6072#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6073 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6074 {
6075 /*
6076 * other_secret already set by the ClientKeyExchange message,
6077 * and is 48 bytes long
6078 */
6079 if( end - p < 2 )
6080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6081
6082 *p++ = 0;
6083 *p++ = 48;
6084 p += 48;
6085 }
6086 else
6087#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6088#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6089 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6090 {
6091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6092 size_t len;
6093
6094 /* Write length only when we know the actual value */
6095 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6096 p + 2, end - ( p + 2 ), &len,
6097 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6098 {
6099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6100 return( ret );
6101 }
6102 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6103 p += 2 + len;
6104
6105 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6106 }
6107 else
6108#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006109#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006110 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6111 {
6112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6113 size_t zlen;
6114
6115 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6116 p + 2, end - ( p + 2 ),
6117 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6118 {
6119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6120 return( ret );
6121 }
6122
6123 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6124 p += 2 + zlen;
6125
6126 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6127 MBEDTLS_DEBUG_ECDH_Z );
6128 }
6129 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006130#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006131 {
6132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6134 }
6135
6136 /* opaque psk<0..2^16-1>; */
6137 if( end - p < 2 )
6138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6139
6140 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6141 p += 2;
6142
6143 if( end < p || (size_t)( end - p ) < psk_len )
6144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6145
6146 memcpy( p, psk, psk_len );
6147 p += psk_len;
6148
6149 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6150
6151 return( 0 );
6152}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006153#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006154
6155#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006156MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006157static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6158
6159#if defined(MBEDTLS_SSL_PROTO_DTLS)
6160int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6161{
6162 /* If renegotiation is not enforced, retransmit until we would reach max
6163 * timeout if we were using the usual handshake doubling scheme */
6164 if( ssl->conf->renego_max_records < 0 )
6165 {
6166 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6167 unsigned char doublings = 1;
6168
6169 while( ratio != 0 )
6170 {
6171 ++doublings;
6172 ratio >>= 1;
6173 }
6174
6175 if( ++ssl->renego_records_seen > doublings )
6176 {
6177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6178 return( 0 );
6179 }
6180 }
6181
6182 return( ssl_write_hello_request( ssl ) );
6183}
6184#endif
6185#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006186
Jerry Yud9526692022-02-17 14:23:47 +08006187/*
6188 * Handshake functions
6189 */
6190#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6191/* No certificate support -> dummy functions */
6192int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6193{
6194 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6195 ssl->handshake->ciphersuite_info;
6196
6197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6198
6199 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6200 {
6201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6202 ssl->state++;
6203 return( 0 );
6204 }
6205
6206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6208}
6209
6210int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6211{
6212 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6213 ssl->handshake->ciphersuite_info;
6214
6215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6216
6217 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6218 {
6219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6220 ssl->state++;
6221 return( 0 );
6222 }
6223
6224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6226}
6227
6228#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6229/* Some certificate support -> implement write and parse */
6230
6231int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6232{
6233 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6234 size_t i, n;
6235 const mbedtls_x509_crt *crt;
6236 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6237 ssl->handshake->ciphersuite_info;
6238
6239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6240
6241 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6242 {
6243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6244 ssl->state++;
6245 return( 0 );
6246 }
6247
6248#if defined(MBEDTLS_SSL_CLI_C)
6249 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6250 {
6251 if( ssl->handshake->client_auth == 0 )
6252 {
6253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6254 ssl->state++;
6255 return( 0 );
6256 }
6257 }
6258#endif /* MBEDTLS_SSL_CLI_C */
6259#if defined(MBEDTLS_SSL_SRV_C)
6260 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6261 {
6262 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6263 {
6264 /* Should never happen because we shouldn't have picked the
6265 * ciphersuite if we don't have a certificate. */
6266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6267 }
6268 }
6269#endif
6270
6271 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6272
6273 /*
6274 * 0 . 0 handshake type
6275 * 1 . 3 handshake length
6276 * 4 . 6 length of all certs
6277 * 7 . 9 length of cert. 1
6278 * 10 . n-1 peer certificate
6279 * n . n+2 length of cert. 2
6280 * n+3 . ... upper level cert, etc.
6281 */
6282 i = 7;
6283 crt = mbedtls_ssl_own_cert( ssl );
6284
6285 while( crt != NULL )
6286 {
6287 n = crt->raw.len;
6288 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6289 {
6290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6291 " > %" MBEDTLS_PRINTF_SIZET,
6292 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6293 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6294 }
6295
6296 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6297 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6298 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6299
6300 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6301 i += n; crt = crt->next;
6302 }
6303
6304 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6305 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6306 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6307
6308 ssl->out_msglen = i;
6309 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6310 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6311
6312 ssl->state++;
6313
6314 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6315 {
6316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6317 return( ret );
6318 }
6319
6320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6321
6322 return( ret );
6323}
6324
6325#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6326
6327#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006329static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6330 unsigned char *crt_buf,
6331 size_t crt_buf_len )
6332{
6333 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6334
6335 if( peer_crt == NULL )
6336 return( -1 );
6337
6338 if( peer_crt->raw.len != crt_buf_len )
6339 return( -1 );
6340
6341 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6342}
6343#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006344MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006345static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6346 unsigned char *crt_buf,
6347 size_t crt_buf_len )
6348{
6349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6350 unsigned char const * const peer_cert_digest =
6351 ssl->session->peer_cert_digest;
6352 mbedtls_md_type_t const peer_cert_digest_type =
6353 ssl->session->peer_cert_digest_type;
6354 mbedtls_md_info_t const * const digest_info =
6355 mbedtls_md_info_from_type( peer_cert_digest_type );
6356 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6357 size_t digest_len;
6358
6359 if( peer_cert_digest == NULL || digest_info == NULL )
6360 return( -1 );
6361
6362 digest_len = mbedtls_md_get_size( digest_info );
6363 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6364 return( -1 );
6365
6366 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6367 if( ret != 0 )
6368 return( -1 );
6369
6370 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6371}
6372#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6373#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6374
6375/*
6376 * Once the certificate message is read, parse it into a cert chain and
6377 * perform basic checks, but leave actual verification to the caller
6378 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006379MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006380static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6381 mbedtls_x509_crt *chain )
6382{
6383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6384#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6385 int crt_cnt=0;
6386#endif
6387 size_t i, n;
6388 uint8_t alert;
6389
6390 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6393 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6394 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6395 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6396 }
6397
6398 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6399 {
6400 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6401 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6402 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6403 }
6404
6405 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6406 {
6407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6408 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6409 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6410 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6411 }
6412
6413 i = mbedtls_ssl_hs_hdr_len( ssl );
6414
6415 /*
6416 * Same message structure as in mbedtls_ssl_write_certificate()
6417 */
6418 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6419
6420 if( ssl->in_msg[i] != 0 ||
6421 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6422 {
6423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6424 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6425 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6426 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6427 }
6428
6429 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6430 i += 3;
6431
6432 /* Iterate through and parse the CRTs in the provided chain. */
6433 while( i < ssl->in_hslen )
6434 {
6435 /* Check that there's room for the next CRT's length fields. */
6436 if ( i + 3 > ssl->in_hslen ) {
6437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6438 mbedtls_ssl_send_alert_message( ssl,
6439 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6440 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6441 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6442 }
6443 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6444 * anything beyond 2**16 ~ 64K. */
6445 if( ssl->in_msg[i] != 0 )
6446 {
6447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6448 mbedtls_ssl_send_alert_message( ssl,
6449 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6450 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6451 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6452 }
6453
6454 /* Read length of the next CRT in the chain. */
6455 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6456 | (unsigned int) ssl->in_msg[i + 2];
6457 i += 3;
6458
6459 if( n < 128 || i + n > ssl->in_hslen )
6460 {
6461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6462 mbedtls_ssl_send_alert_message( ssl,
6463 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6464 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6465 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6466 }
6467
6468 /* Check if we're handling the first CRT in the chain. */
6469#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6470 if( crt_cnt++ == 0 &&
6471 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6472 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6473 {
6474 /* During client-side renegotiation, check that the server's
6475 * end-CRTs hasn't changed compared to the initial handshake,
6476 * mitigating the triple handshake attack. On success, reuse
6477 * the original end-CRT instead of parsing it again. */
6478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6479 if( ssl_check_peer_crt_unchanged( ssl,
6480 &ssl->in_msg[i],
6481 n ) != 0 )
6482 {
6483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6484 mbedtls_ssl_send_alert_message( ssl,
6485 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6486 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6487 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6488 }
6489
6490 /* Now we can safely free the original chain. */
6491 ssl_clear_peer_cert( ssl->session );
6492 }
6493#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6494
6495 /* Parse the next certificate in the chain. */
6496#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6497 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6498#else
6499 /* If we don't need to store the CRT chain permanently, parse
6500 * it in-place from the input buffer instead of making a copy. */
6501 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6502#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6503 switch( ret )
6504 {
6505 case 0: /*ok*/
6506 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6507 /* Ignore certificate with an unknown algorithm: maybe a
6508 prior certificate was already trusted. */
6509 break;
6510
6511 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6512 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6513 goto crt_parse_der_failed;
6514
6515 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6516 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6517 goto crt_parse_der_failed;
6518
6519 default:
6520 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6521 crt_parse_der_failed:
6522 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6523 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6524 return( ret );
6525 }
6526
6527 i += n;
6528 }
6529
6530 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6531 return( 0 );
6532}
6533
6534#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006535MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006536static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6537{
6538 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6539 return( -1 );
6540
6541 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6542 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6543 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6544 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6545 {
Ronald Cron19385882022-06-15 16:26:13 +02006546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006547 return( 0 );
6548 }
6549 return( -1 );
6550}
6551#endif /* MBEDTLS_SSL_SRV_C */
6552
6553/* Check if a certificate message is expected.
6554 * Return either
6555 * - SSL_CERTIFICATE_EXPECTED, or
6556 * - SSL_CERTIFICATE_SKIP
6557 * indicating whether a Certificate message is expected or not.
6558 */
6559#define SSL_CERTIFICATE_EXPECTED 0
6560#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006561MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006562static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6563 int authmode )
6564{
6565 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6566 ssl->handshake->ciphersuite_info;
6567
6568 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6569 return( SSL_CERTIFICATE_SKIP );
6570
6571#if defined(MBEDTLS_SSL_SRV_C)
6572 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6573 {
6574 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6575 return( SSL_CERTIFICATE_SKIP );
6576
6577 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6578 {
6579 ssl->session_negotiate->verify_result =
6580 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6581 return( SSL_CERTIFICATE_SKIP );
6582 }
6583 }
6584#else
6585 ((void) authmode);
6586#endif /* MBEDTLS_SSL_SRV_C */
6587
6588 return( SSL_CERTIFICATE_EXPECTED );
6589}
6590
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006591MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006592static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6593 int authmode,
6594 mbedtls_x509_crt *chain,
6595 void *rs_ctx )
6596{
6597 int ret = 0;
6598 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6599 ssl->handshake->ciphersuite_info;
6600 int have_ca_chain = 0;
6601
6602 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6603 void *p_vrfy;
6604
6605 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6606 return( 0 );
6607
6608 if( ssl->f_vrfy != NULL )
6609 {
6610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6611 f_vrfy = ssl->f_vrfy;
6612 p_vrfy = ssl->p_vrfy;
6613 }
6614 else
6615 {
6616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6617 f_vrfy = ssl->conf->f_vrfy;
6618 p_vrfy = ssl->conf->p_vrfy;
6619 }
6620
6621 /*
6622 * Main check: verify certificate
6623 */
6624#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6625 if( ssl->conf->f_ca_cb != NULL )
6626 {
6627 ((void) rs_ctx);
6628 have_ca_chain = 1;
6629
6630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6631 ret = mbedtls_x509_crt_verify_with_ca_cb(
6632 chain,
6633 ssl->conf->f_ca_cb,
6634 ssl->conf->p_ca_cb,
6635 ssl->conf->cert_profile,
6636 ssl->hostname,
6637 &ssl->session_negotiate->verify_result,
6638 f_vrfy, p_vrfy );
6639 }
6640 else
6641#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6642 {
6643 mbedtls_x509_crt *ca_chain;
6644 mbedtls_x509_crl *ca_crl;
6645
6646#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6647 if( ssl->handshake->sni_ca_chain != NULL )
6648 {
6649 ca_chain = ssl->handshake->sni_ca_chain;
6650 ca_crl = ssl->handshake->sni_ca_crl;
6651 }
6652 else
6653#endif
6654 {
6655 ca_chain = ssl->conf->ca_chain;
6656 ca_crl = ssl->conf->ca_crl;
6657 }
6658
6659 if( ca_chain != NULL )
6660 have_ca_chain = 1;
6661
6662 ret = mbedtls_x509_crt_verify_restartable(
6663 chain,
6664 ca_chain, ca_crl,
6665 ssl->conf->cert_profile,
6666 ssl->hostname,
6667 &ssl->session_negotiate->verify_result,
6668 f_vrfy, p_vrfy, rs_ctx );
6669 }
6670
6671 if( ret != 0 )
6672 {
6673 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6674 }
6675
6676#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6677 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6678 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6679#endif
6680
6681 /*
6682 * Secondary checks: always done, but change 'ret' only if it was 0
6683 */
6684
6685#if defined(MBEDTLS_ECP_C)
6686 {
6687 const mbedtls_pk_context *pk = &chain->pk;
6688
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006689 /* If certificate uses an EC key, make sure the curve is OK.
6690 * This is a public key, so it can't be opaque, so can_do() is a good
6691 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006692 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006693 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006694 /* and in the unlikely case the above assumption no longer holds
6695 * we are making sure that pk_ec() here does not return a NULL
6696 */
6697 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6698 if( ec == NULL )
6699 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6702 }
Jerry Yud9526692022-02-17 14:23:47 +08006703
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006704 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6705 {
6706 ssl->session_negotiate->verify_result |=
6707 MBEDTLS_X509_BADCERT_BAD_KEY;
6708
6709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6710 if( ret == 0 )
6711 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6712 }
Jerry Yud9526692022-02-17 14:23:47 +08006713 }
6714 }
6715#endif /* MBEDTLS_ECP_C */
6716
6717 if( mbedtls_ssl_check_cert_usage( chain,
6718 ciphersuite_info,
6719 ! ssl->conf->endpoint,
6720 &ssl->session_negotiate->verify_result ) != 0 )
6721 {
6722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6723 if( ret == 0 )
6724 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6725 }
6726
6727 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6728 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6729 * with details encoded in the verification flags. All other kinds
6730 * of error codes, including those from the user provided f_vrfy
6731 * functions, are treated as fatal and lead to a failure of
6732 * ssl_parse_certificate even if verification was optional. */
6733 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6734 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6735 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6736 {
6737 ret = 0;
6738 }
6739
6740 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6741 {
6742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6743 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6744 }
6745
6746 if( ret != 0 )
6747 {
6748 uint8_t alert;
6749
6750 /* The certificate may have been rejected for several reasons.
6751 Pick one and send the corresponding alert. Which alert to send
6752 may be a subject of debate in some cases. */
6753 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6754 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6755 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6756 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6757 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6758 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6759 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6760 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6761 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6762 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6763 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6764 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6765 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6766 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6767 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6768 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6769 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6770 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6771 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6772 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6773 else
6774 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6775 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6776 alert );
6777 }
6778
6779#if defined(MBEDTLS_DEBUG_C)
6780 if( ssl->session_negotiate->verify_result != 0 )
6781 {
6782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6783 (unsigned int) ssl->session_negotiate->verify_result ) );
6784 }
6785 else
6786 {
6787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6788 }
6789#endif /* MBEDTLS_DEBUG_C */
6790
6791 return( ret );
6792}
6793
6794#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006795MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006796static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6797 unsigned char *start, size_t len )
6798{
6799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6800 /* Remember digest of the peer's end-CRT. */
6801 ssl->session_negotiate->peer_cert_digest =
6802 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6803 if( ssl->session_negotiate->peer_cert_digest == NULL )
6804 {
6805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6806 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6807 mbedtls_ssl_send_alert_message( ssl,
6808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6809 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6810
6811 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6812 }
6813
6814 ret = mbedtls_md( mbedtls_md_info_from_type(
6815 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6816 start, len,
6817 ssl->session_negotiate->peer_cert_digest );
6818
6819 ssl->session_negotiate->peer_cert_digest_type =
6820 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6821 ssl->session_negotiate->peer_cert_digest_len =
6822 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6823
6824 return( ret );
6825}
6826
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006827MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006828static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6829 unsigned char *start, size_t len )
6830{
6831 unsigned char *end = start + len;
6832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6833
6834 /* Make a copy of the peer's raw public key. */
6835 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6836 ret = mbedtls_pk_parse_subpubkey( &start, end,
6837 &ssl->handshake->peer_pubkey );
6838 if( ret != 0 )
6839 {
6840 /* We should have parsed the public key before. */
6841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6842 }
6843
6844 return( 0 );
6845}
6846#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6847
6848int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6849{
6850 int ret = 0;
6851 int crt_expected;
6852#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6853 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6854 ? ssl->handshake->sni_authmode
6855 : ssl->conf->authmode;
6856#else
6857 const int authmode = ssl->conf->authmode;
6858#endif
6859 void *rs_ctx = NULL;
6860 mbedtls_x509_crt *chain = NULL;
6861
6862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6863
6864 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6865 if( crt_expected == SSL_CERTIFICATE_SKIP )
6866 {
6867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6868 goto exit;
6869 }
6870
6871#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6872 if( ssl->handshake->ecrs_enabled &&
6873 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6874 {
6875 chain = ssl->handshake->ecrs_peer_cert;
6876 ssl->handshake->ecrs_peer_cert = NULL;
6877 goto crt_verify;
6878 }
6879#endif
6880
6881 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6882 {
6883 /* mbedtls_ssl_read_record may have sent an alert already. We
6884 let it decide whether to alert. */
6885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6886 goto exit;
6887 }
6888
6889#if defined(MBEDTLS_SSL_SRV_C)
6890 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6891 {
6892 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6893
6894 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6895 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6896
6897 goto exit;
6898 }
6899#endif /* MBEDTLS_SSL_SRV_C */
6900
6901 /* Clear existing peer CRT structure in case we tried to
6902 * reuse a session but it failed, and allocate a new one. */
6903 ssl_clear_peer_cert( ssl->session_negotiate );
6904
6905 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6906 if( chain == NULL )
6907 {
6908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6909 sizeof( mbedtls_x509_crt ) ) );
6910 mbedtls_ssl_send_alert_message( ssl,
6911 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6912 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6913
6914 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6915 goto exit;
6916 }
6917 mbedtls_x509_crt_init( chain );
6918
6919 ret = ssl_parse_certificate_chain( ssl, chain );
6920 if( ret != 0 )
6921 goto exit;
6922
6923#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6924 if( ssl->handshake->ecrs_enabled)
6925 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6926
6927crt_verify:
6928 if( ssl->handshake->ecrs_enabled)
6929 rs_ctx = &ssl->handshake->ecrs_ctx;
6930#endif
6931
6932 ret = ssl_parse_certificate_verify( ssl, authmode,
6933 chain, rs_ctx );
6934 if( ret != 0 )
6935 goto exit;
6936
6937#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6938 {
6939 unsigned char *crt_start, *pk_start;
6940 size_t crt_len, pk_len;
6941
6942 /* We parse the CRT chain without copying, so
6943 * these pointers point into the input buffer,
6944 * and are hence still valid after freeing the
6945 * CRT chain. */
6946
6947 crt_start = chain->raw.p;
6948 crt_len = chain->raw.len;
6949
6950 pk_start = chain->pk_raw.p;
6951 pk_len = chain->pk_raw.len;
6952
6953 /* Free the CRT structures before computing
6954 * digest and copying the peer's public key. */
6955 mbedtls_x509_crt_free( chain );
6956 mbedtls_free( chain );
6957 chain = NULL;
6958
6959 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6960 if( ret != 0 )
6961 goto exit;
6962
6963 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6964 if( ret != 0 )
6965 goto exit;
6966 }
6967#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6968 /* Pass ownership to session structure. */
6969 ssl->session_negotiate->peer_cert = chain;
6970 chain = NULL;
6971#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6972
6973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6974
6975exit:
6976
6977 if( ret == 0 )
6978 ssl->state++;
6979
6980#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6981 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6982 {
6983 ssl->handshake->ecrs_peer_cert = chain;
6984 chain = NULL;
6985 }
6986#endif
6987
6988 if( chain != NULL )
6989 {
6990 mbedtls_x509_crt_free( chain );
6991 mbedtls_free( chain );
6992 }
6993
6994 return( ret );
6995}
6996#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6997
Andrzej Kurek25f27152022-08-17 16:09:31 -04006998#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006999static void ssl_calc_finished_tls_sha256(
7000 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7001{
7002 int len = 12;
7003 const char *sender;
7004 unsigned char padbuf[32];
7005#if defined(MBEDTLS_USE_PSA_CRYPTO)
7006 size_t hash_size;
7007 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7008 psa_status_t status;
7009#else
7010 mbedtls_sha256_context sha256;
7011#endif
7012
7013 mbedtls_ssl_session *session = ssl->session_negotiate;
7014 if( !session )
7015 session = ssl->session;
7016
7017 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7018 ? "client finished"
7019 : "server finished";
7020
7021#if defined(MBEDTLS_USE_PSA_CRYPTO)
7022 sha256_psa = psa_hash_operation_init();
7023
7024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7025
7026 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7027 if( status != PSA_SUCCESS )
7028 {
7029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7030 return;
7031 }
7032
7033 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7034 if( status != PSA_SUCCESS )
7035 {
7036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7037 return;
7038 }
7039 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7040#else
7041
7042 mbedtls_sha256_init( &sha256 );
7043
7044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7045
7046 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7047
7048 /*
7049 * TLSv1.2:
7050 * hash = PRF( master, finished_label,
7051 * Hash( handshake ) )[0.11]
7052 */
7053
7054#if !defined(MBEDTLS_SHA256_ALT)
7055 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7056 sha256.state, sizeof( sha256.state ) );
7057#endif
7058
7059 mbedtls_sha256_finish( &sha256, padbuf );
7060 mbedtls_sha256_free( &sha256 );
7061#endif /* MBEDTLS_USE_PSA_CRYPTO */
7062
7063 ssl->handshake->tls_prf( session->master, 48, sender,
7064 padbuf, 32, buf, len );
7065
7066 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7067
7068 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7069
7070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7071}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007072#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007073
Jerry Yub7ba49e2022-02-17 14:25:53 +08007074
Andrzej Kurek25f27152022-08-17 16:09:31 -04007075#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007076static void ssl_calc_finished_tls_sha384(
7077 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7078{
7079 int len = 12;
7080 const char *sender;
7081 unsigned char padbuf[48];
7082#if defined(MBEDTLS_USE_PSA_CRYPTO)
7083 size_t hash_size;
7084 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7085 psa_status_t status;
7086#else
7087 mbedtls_sha512_context sha512;
7088#endif
7089
7090 mbedtls_ssl_session *session = ssl->session_negotiate;
7091 if( !session )
7092 session = ssl->session;
7093
7094 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7095 ? "client finished"
7096 : "server finished";
7097
7098#if defined(MBEDTLS_USE_PSA_CRYPTO)
7099 sha384_psa = psa_hash_operation_init();
7100
7101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7102
7103 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7104 if( status != PSA_SUCCESS )
7105 {
7106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7107 return;
7108 }
7109
7110 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7111 if( status != PSA_SUCCESS )
7112 {
7113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7114 return;
7115 }
7116 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7117#else
7118 mbedtls_sha512_init( &sha512 );
7119
7120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7121
Andrzej Kureka242e832022-08-11 10:03:14 -04007122 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007123
7124 /*
7125 * TLSv1.2:
7126 * hash = PRF( master, finished_label,
7127 * Hash( handshake ) )[0.11]
7128 */
7129
7130#if !defined(MBEDTLS_SHA512_ALT)
7131 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7132 sha512.state, sizeof( sha512.state ) );
7133#endif
7134 mbedtls_sha512_finish( &sha512, padbuf );
7135
7136 mbedtls_sha512_free( &sha512 );
7137#endif
7138
7139 ssl->handshake->tls_prf( session->master, 48, sender,
7140 padbuf, 48, buf, len );
7141
7142 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7143
7144 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7145
7146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7147}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007148#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007149
Jerry Yuaef00152022-02-17 14:27:31 +08007150void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7151{
7152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7153
7154 /*
7155 * Free our handshake params
7156 */
7157 mbedtls_ssl_handshake_free( ssl );
7158 mbedtls_free( ssl->handshake );
7159 ssl->handshake = NULL;
7160
7161 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007162 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007163 */
7164 if( ssl->transform )
7165 {
7166 mbedtls_ssl_transform_free( ssl->transform );
7167 mbedtls_free( ssl->transform );
7168 }
7169 ssl->transform = ssl->transform_negotiate;
7170 ssl->transform_negotiate = NULL;
7171
7172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7173}
7174
Jerry Yu2a9fff52022-02-17 14:28:51 +08007175void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7176{
7177 int resume = ssl->handshake->resume;
7178
7179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7180
7181#if defined(MBEDTLS_SSL_RENEGOTIATION)
7182 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7183 {
7184 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7185 ssl->renego_records_seen = 0;
7186 }
7187#endif
7188
7189 /*
7190 * Free the previous session and switch in the current one
7191 */
7192 if( ssl->session )
7193 {
7194#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7195 /* RFC 7366 3.1: keep the EtM state */
7196 ssl->session_negotiate->encrypt_then_mac =
7197 ssl->session->encrypt_then_mac;
7198#endif
7199
7200 mbedtls_ssl_session_free( ssl->session );
7201 mbedtls_free( ssl->session );
7202 }
7203 ssl->session = ssl->session_negotiate;
7204 ssl->session_negotiate = NULL;
7205
7206 /*
7207 * Add cache entry
7208 */
7209 if( ssl->conf->f_set_cache != NULL &&
7210 ssl->session->id_len != 0 &&
7211 resume == 0 )
7212 {
7213 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7214 ssl->session->id,
7215 ssl->session->id_len,
7216 ssl->session ) != 0 )
7217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7218 }
7219
7220#if defined(MBEDTLS_SSL_PROTO_DTLS)
7221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7222 ssl->handshake->flight != NULL )
7223 {
7224 /* Cancel handshake timer */
7225 mbedtls_ssl_set_timer( ssl, 0 );
7226
7227 /* Keep last flight around in case we need to resend it:
7228 * we need the handshake and transform structures for that */
7229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7230 }
7231 else
7232#endif
7233 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7234
7235 ssl->state++;
7236
7237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7238}
7239
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007240int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7241{
7242 int ret, hash_len;
7243
7244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7245
7246 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7247
7248 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7249
7250 /*
7251 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7252 * may define some other value. Currently (early 2016), no defined
7253 * ciphersuite does this (and this is unlikely to change as activity has
7254 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7255 */
7256 hash_len = 12;
7257
7258#if defined(MBEDTLS_SSL_RENEGOTIATION)
7259 ssl->verify_data_len = hash_len;
7260 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7261#endif
7262
7263 ssl->out_msglen = 4 + hash_len;
7264 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7265 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7266
7267 /*
7268 * In case of session resuming, invert the client and server
7269 * ChangeCipherSpec messages order.
7270 */
7271 if( ssl->handshake->resume != 0 )
7272 {
7273#if defined(MBEDTLS_SSL_CLI_C)
7274 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7275 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7276#endif
7277#if defined(MBEDTLS_SSL_SRV_C)
7278 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7279 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7280#endif
7281 }
7282 else
7283 ssl->state++;
7284
7285 /*
7286 * Switch to our negotiated transform and session parameters for outbound
7287 * data.
7288 */
7289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7290
7291#if defined(MBEDTLS_SSL_PROTO_DTLS)
7292 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7293 {
7294 unsigned char i;
7295
7296 /* Remember current epoch settings for resending */
7297 ssl->handshake->alt_transform_out = ssl->transform_out;
7298 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7299 sizeof( ssl->handshake->alt_out_ctr ) );
7300
7301 /* Set sequence_number to zero */
7302 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7303
7304
7305 /* Increment epoch */
7306 for( i = 2; i > 0; i-- )
7307 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7308 break;
7309
7310 /* The loop goes to its end iff the counter is wrapping */
7311 if( i == 0 )
7312 {
7313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7314 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7315 }
7316 }
7317 else
7318#endif /* MBEDTLS_SSL_PROTO_DTLS */
7319 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7320
7321 ssl->transform_out = ssl->transform_negotiate;
7322 ssl->session_out = ssl->session_negotiate;
7323
7324#if defined(MBEDTLS_SSL_PROTO_DTLS)
7325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7326 mbedtls_ssl_send_flight_completed( ssl );
7327#endif
7328
7329 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7330 {
7331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7332 return( ret );
7333 }
7334
7335#if defined(MBEDTLS_SSL_PROTO_DTLS)
7336 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7337 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7338 {
7339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7340 return( ret );
7341 }
7342#endif
7343
7344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7345
7346 return( 0 );
7347}
7348
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007349#define SSL_MAX_HASH_LEN 12
7350
7351int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7352{
7353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7354 unsigned int hash_len = 12;
7355 unsigned char buf[SSL_MAX_HASH_LEN];
7356
7357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7358
7359 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7360
7361 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7362 {
7363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7364 goto exit;
7365 }
7366
7367 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7368 {
7369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7370 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7371 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7372 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7373 goto exit;
7374 }
7375
7376 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7377 {
7378 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7379 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7380 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7381 goto exit;
7382 }
7383
7384 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7385 {
7386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7387 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7388 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7389 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7390 goto exit;
7391 }
7392
7393 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7394 buf, hash_len ) != 0 )
7395 {
7396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7398 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7399 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7400 goto exit;
7401 }
7402
7403#if defined(MBEDTLS_SSL_RENEGOTIATION)
7404 ssl->verify_data_len = hash_len;
7405 memcpy( ssl->peer_verify_data, buf, hash_len );
7406#endif
7407
7408 if( ssl->handshake->resume != 0 )
7409 {
7410#if defined(MBEDTLS_SSL_CLI_C)
7411 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7412 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7413#endif
7414#if defined(MBEDTLS_SSL_SRV_C)
7415 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7416 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7417#endif
7418 }
7419 else
7420 ssl->state++;
7421
7422#if defined(MBEDTLS_SSL_PROTO_DTLS)
7423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7424 mbedtls_ssl_recv_flight_completed( ssl );
7425#endif
7426
7427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7428
7429exit:
7430 mbedtls_platform_zeroize( buf, hash_len );
7431 return( ret );
7432}
7433
Jerry Yu392112c2022-02-17 14:34:10 +08007434#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7435/*
7436 * Helper to get TLS 1.2 PRF from ciphersuite
7437 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7438 */
7439static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7440{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007442 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7443 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7444
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007445 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007446 return( tls_prf_sha384 );
7447#else
7448 (void) ciphersuite_id;
7449#endif
7450 return( tls_prf_sha256 );
7451}
7452#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007453
7454static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7455{
7456 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007457#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007458 if( tls_prf == tls_prf_sha384 )
7459 {
7460 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7461 }
7462 else
7463#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007465 if( tls_prf == tls_prf_sha256 )
7466 {
7467 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7468 }
7469 else
7470#endif
7471 return( MBEDTLS_SSL_TLS_PRF_NONE );
7472}
7473
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007474/*
7475 * Populate a transform structure with session keys and all the other
7476 * necessary information.
7477 *
7478 * Parameters:
7479 * - [in/out]: transform: structure to populate
7480 * [in] must be just initialised with mbedtls_ssl_transform_init()
7481 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7482 * - [in] ciphersuite
7483 * - [in] master
7484 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007485 * - [in] tls_prf: pointer to PRF to use for key derivation
7486 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007487 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007488 * - [in] endpoint: client or server
7489 * - [in] ssl: used for:
7490 * - ssl->conf->{f,p}_export_keys
7491 * [in] optionally used for:
7492 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7493 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007494MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007495static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7496 int ciphersuite,
7497 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007498#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007499 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007500#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007501 ssl_tls_prf_t tls_prf,
7502 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007503 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007504 unsigned endpoint,
7505 const mbedtls_ssl_context *ssl )
7506{
7507 int ret = 0;
7508 unsigned char keyblk[256];
7509 unsigned char *key1;
7510 unsigned char *key2;
7511 unsigned char *mac_enc;
7512 unsigned char *mac_dec;
7513 size_t mac_key_len = 0;
7514 size_t iv_copy_len;
7515 size_t keylen;
7516 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007517 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007518#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007519 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007520 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007521#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007522
7523#if defined(MBEDTLS_USE_PSA_CRYPTO)
7524 psa_key_type_t key_type;
7525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7526 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007527 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007528 size_t key_bits;
7529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7530#endif
7531
7532#if !defined(MBEDTLS_DEBUG_C) && \
7533 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7534 if( ssl->f_export_keys == NULL )
7535 {
7536 ssl = NULL; /* make sure we don't use it except for these cases */
7537 (void) ssl;
7538 }
7539#endif
7540
7541 /*
7542 * Some data just needs copying into the structure
7543 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007544#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007545 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007546#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007547 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007548
7549#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7550 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7551#endif
7552
7553#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007554 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007555 {
7556 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7557 * generation separate. This should never happen. */
7558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7559 }
7560#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7561
7562 /*
7563 * Get various info structures
7564 */
7565 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7566 if( ciphersuite_info == NULL )
7567 {
7568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7569 ciphersuite ) );
7570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7571 }
7572
Neil Armstrongab555e02022-04-04 11:07:59 +02007573 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007574#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007575 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007576#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007577 ciphersuite_info );
7578
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007579 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7580 transform->taglen =
7581 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7582
7583#if defined(MBEDTLS_USE_PSA_CRYPTO)
7584 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7585 transform->taglen,
7586 &alg,
7587 &key_type,
7588 &key_bits ) ) != PSA_SUCCESS )
7589 {
7590 ret = psa_ssl_status_to_mbedtls( status );
7591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7592 goto end;
7593 }
7594#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007595 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7596 if( cipher_info == NULL )
7597 {
7598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7599 ciphersuite_info->cipher ) );
7600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7601 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007602#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007603
Neil Armstronge4512952022-03-08 09:08:22 +01007604#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007605 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007606 if( mac_alg == 0 )
7607 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007609 (unsigned) ciphersuite_info->mac ) );
7610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7611 }
7612#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007613 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7614 if( md_info == NULL )
7615 {
7616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7617 (unsigned) ciphersuite_info->mac ) );
7618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7619 }
Neil Armstronge4512952022-03-08 09:08:22 +01007620#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007621
7622#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7623 /* Copy own and peer's CID if the use of the CID
7624 * extension has been negotiated. */
7625 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7626 {
7627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7628
7629 transform->in_cid_len = ssl->own_cid_len;
7630 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7631 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7632 transform->in_cid_len );
7633
7634 transform->out_cid_len = ssl->handshake->peer_cid_len;
7635 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7636 ssl->handshake->peer_cid_len );
7637 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7638 transform->out_cid_len );
7639 }
7640#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7641
7642 /*
7643 * Compute key block using the PRF
7644 */
7645 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7646 if( ret != 0 )
7647 {
7648 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7649 return( ret );
7650 }
7651
7652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7653 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7654 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7655 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7656 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7657
7658 /*
7659 * Determine the appropriate key, IV and MAC length.
7660 */
7661
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007662#if defined(MBEDTLS_USE_PSA_CRYPTO)
7663 keylen = PSA_BITS_TO_BYTES(key_bits);
7664#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007665 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007666#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007667
7668#if defined(MBEDTLS_GCM_C) || \
7669 defined(MBEDTLS_CCM_C) || \
7670 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007671 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007672 {
7673 size_t explicit_ivlen;
7674
7675 transform->maclen = 0;
7676 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007677
7678 /* All modes haves 96-bit IVs, but the length of the static parts vary
7679 * with mode and version:
7680 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7681 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7682 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7683 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7684 * sequence number).
7685 */
7686 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007687#if defined(MBEDTLS_USE_PSA_CRYPTO)
7688 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7689#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007690 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007691#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007692 transform->fixed_ivlen = 12;
7693 else
7694 transform->fixed_ivlen = 4;
7695
7696 /* Minimum length of encrypted record */
7697 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7698 transform->minlen = explicit_ivlen + transform->taglen;
7699 }
7700 else
7701#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7702#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007703 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7704 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7705 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007706 {
Neil Armstronge4512952022-03-08 09:08:22 +01007707#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007708 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007709#else
7710 size_t block_size = cipher_info->block_size;
7711#endif /* MBEDTLS_USE_PSA_CRYPTO */
7712
7713#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007714 /* Get MAC length */
7715 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7716#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007717 /* Initialize HMAC contexts */
7718 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7719 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7720 {
7721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7722 goto end;
7723 }
7724
7725 /* Get MAC length */
7726 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007727#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007728 transform->maclen = mac_key_len;
7729
7730 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007731#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007732 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007733#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007734 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007735#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007736
7737 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007738 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007739 transform->minlen = transform->maclen;
7740 else
7741 {
7742 /*
7743 * GenericBlockCipher:
7744 * 1. if EtM is in use: one block plus MAC
7745 * otherwise: * first multiple of blocklen greater than maclen
7746 * 2. IV
7747 */
7748#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007749 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007750 {
7751 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007752 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007753 }
7754 else
7755#endif
7756 {
7757 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007758 + block_size
7759 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007760 }
7761
Glenn Strauss07c64162022-03-14 12:34:51 -04007762 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007763 {
7764 transform->minlen += transform->ivlen;
7765 }
7766 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007767 {
7768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7769 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7770 goto end;
7771 }
7772 }
7773 }
7774 else
7775#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7776 {
7777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7779 }
7780
7781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7782 (unsigned) keylen,
7783 (unsigned) transform->minlen,
7784 (unsigned) transform->ivlen,
7785 (unsigned) transform->maclen ) );
7786
7787 /*
7788 * Finally setup the cipher contexts, IVs and MAC secrets.
7789 */
7790#if defined(MBEDTLS_SSL_CLI_C)
7791 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7792 {
7793 key1 = keyblk + mac_key_len * 2;
7794 key2 = keyblk + mac_key_len * 2 + keylen;
7795
7796 mac_enc = keyblk;
7797 mac_dec = keyblk + mac_key_len;
7798
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007799 iv_copy_len = ( transform->fixed_ivlen ) ?
7800 transform->fixed_ivlen : transform->ivlen;
7801 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7802 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7803 iv_copy_len );
7804 }
7805 else
7806#endif /* MBEDTLS_SSL_CLI_C */
7807#if defined(MBEDTLS_SSL_SRV_C)
7808 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7809 {
7810 key1 = keyblk + mac_key_len * 2 + keylen;
7811 key2 = keyblk + mac_key_len * 2;
7812
7813 mac_enc = keyblk + mac_key_len;
7814 mac_dec = keyblk;
7815
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007816 iv_copy_len = ( transform->fixed_ivlen ) ?
7817 transform->fixed_ivlen : transform->ivlen;
7818 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7819 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7820 iv_copy_len );
7821 }
7822 else
7823#endif /* MBEDTLS_SSL_SRV_C */
7824 {
7825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7826 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7827 goto end;
7828 }
7829
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007830 if( ssl != NULL && ssl->f_export_keys != NULL )
7831 {
7832 ssl->f_export_keys( ssl->p_export_keys,
7833 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7834 master, 48,
7835 randbytes + 32,
7836 randbytes,
7837 tls_prf_get_type( tls_prf ) );
7838 }
7839
7840#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007841 transform->psa_alg = alg;
7842
7843 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7844 {
7845 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7846 psa_set_key_algorithm( &attributes, alg );
7847 psa_set_key_type( &attributes, key_type );
7848
7849 if( ( status = psa_import_key( &attributes,
7850 key1,
7851 PSA_BITS_TO_BYTES( key_bits ),
7852 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7853 {
7854 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7855 ret = psa_ssl_status_to_mbedtls( status );
7856 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7857 goto end;
7858 }
7859
7860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7861
7862 if( ( status = psa_import_key( &attributes,
7863 key2,
7864 PSA_BITS_TO_BYTES( key_bits ),
7865 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7866 {
7867 ret = psa_ssl_status_to_mbedtls( status );
7868 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7869 goto end;
7870 }
7871 }
7872#else
7873 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7874 cipher_info ) ) != 0 )
7875 {
7876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7877 goto end;
7878 }
7879
7880 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7881 cipher_info ) ) != 0 )
7882 {
7883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7884 goto end;
7885 }
7886
7887 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7888 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7889 MBEDTLS_ENCRYPT ) ) != 0 )
7890 {
7891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7892 goto end;
7893 }
7894
7895 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7896 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7897 MBEDTLS_DECRYPT ) ) != 0 )
7898 {
7899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7900 goto end;
7901 }
7902
7903#if defined(MBEDTLS_CIPHER_MODE_CBC)
7904 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7905 {
7906 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7907 MBEDTLS_PADDING_NONE ) ) != 0 )
7908 {
7909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7910 goto end;
7911 }
7912
7913 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7914 MBEDTLS_PADDING_NONE ) ) != 0 )
7915 {
7916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7917 goto end;
7918 }
7919 }
7920#endif /* MBEDTLS_CIPHER_MODE_CBC */
7921#endif /* MBEDTLS_USE_PSA_CRYPTO */
7922
Neil Armstrong29c0c042022-03-17 17:47:28 +01007923#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7924 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7925 For AEAD-based ciphersuites, there is nothing to do here. */
7926 if( mac_key_len != 0 )
7927 {
7928#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007929 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007930
7931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007932 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007933 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7934
7935 if( ( status = psa_import_key( &attributes,
7936 mac_enc, mac_key_len,
7937 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7938 {
7939 ret = psa_ssl_status_to_mbedtls( status );
7940 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7941 goto end;
7942 }
7943
Ronald Cronfb39f152022-03-25 14:36:28 +01007944 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007945 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7946#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7947 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7948#endif
7949 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007950 /* mbedtls_ct_hmac() requires the key to be exportable */
7951 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7952 PSA_KEY_USAGE_VERIFY_HASH );
7953 else
7954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7955
7956 if( ( status = psa_import_key( &attributes,
7957 mac_dec, mac_key_len,
7958 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7959 {
7960 ret = psa_ssl_status_to_mbedtls( status );
7961 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7962 goto end;
7963 }
7964#else
7965 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7966 if( ret != 0 )
7967 goto end;
7968 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7969 if( ret != 0 )
7970 goto end;
7971#endif /* MBEDTLS_USE_PSA_CRYPTO */
7972 }
7973#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7974
7975 ((void) mac_dec);
7976 ((void) mac_enc);
7977
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007978end:
7979 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7980 return( ret );
7981}
7982
Jerry Yuee40f9d2022-02-17 14:55:16 +08007983#if defined(MBEDTLS_USE_PSA_CRYPTO)
7984int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7985 unsigned char *hash, size_t *hashlen,
7986 unsigned char *data, size_t data_len,
7987 mbedtls_md_type_t md_alg )
7988{
7989 psa_status_t status;
7990 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007991 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007992
7993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7994
7995 if( ( status = psa_hash_setup( &hash_operation,
7996 hash_alg ) ) != PSA_SUCCESS )
7997 {
7998 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7999 goto exit;
8000 }
8001
8002 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8003 64 ) ) != PSA_SUCCESS )
8004 {
8005 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8006 goto exit;
8007 }
8008
8009 if( ( status = psa_hash_update( &hash_operation,
8010 data, data_len ) ) != PSA_SUCCESS )
8011 {
8012 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8013 goto exit;
8014 }
8015
8016 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8017 hashlen ) ) != PSA_SUCCESS )
8018 {
8019 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8020 goto exit;
8021 }
8022
8023exit:
8024 if( status != PSA_SUCCESS )
8025 {
8026 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8027 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8028 switch( status )
8029 {
8030 case PSA_ERROR_NOT_SUPPORTED:
8031 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8032 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8033 case PSA_ERROR_BUFFER_TOO_SMALL:
8034 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8035 case PSA_ERROR_INSUFFICIENT_MEMORY:
8036 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8037 default:
8038 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8039 }
8040 }
8041 return( 0 );
8042}
8043
8044#else
8045
8046int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8047 unsigned char *hash, size_t *hashlen,
8048 unsigned char *data, size_t data_len,
8049 mbedtls_md_type_t md_alg )
8050{
8051 int ret = 0;
8052 mbedtls_md_context_t ctx;
8053 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8054 *hashlen = mbedtls_md_get_size( md_info );
8055
8056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8057
8058 mbedtls_md_init( &ctx );
8059
8060 /*
8061 * digitally-signed struct {
8062 * opaque client_random[32];
8063 * opaque server_random[32];
8064 * ServerDHParams params;
8065 * };
8066 */
8067 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8068 {
8069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8070 goto exit;
8071 }
8072 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8073 {
8074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8075 goto exit;
8076 }
8077 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8078 {
8079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8080 goto exit;
8081 }
8082 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8083 {
8084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8085 goto exit;
8086 }
8087 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8088 {
8089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8090 goto exit;
8091 }
8092
8093exit:
8094 mbedtls_md_free( &ctx );
8095
8096 if( ret != 0 )
8097 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8098 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8099
8100 return( ret );
8101}
8102#endif /* MBEDTLS_USE_PSA_CRYPTO */
8103
Jerry Yud9d91da2022-02-17 14:57:06 +08008104#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8105
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008106/* Find the preferred hash for a given signature algorithm. */
8107unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8108 mbedtls_ssl_context *ssl,
8109 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008110{
Gabor Mezei078e8032022-04-27 21:17:56 +02008111 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008112 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008113
8114 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008115 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008116
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008117 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008118 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008119 unsigned int hash_alg_received =
8120 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8121 received_sig_algs[i] );
8122 unsigned int sig_alg_received =
8123 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8124 received_sig_algs[i] );
8125
8126 if( sig_alg == sig_alg_received )
8127 {
8128#if defined(MBEDTLS_USE_PSA_CRYPTO)
8129 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8130 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008131 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008132 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008133
Neil Armstrong96eceb82022-06-30 18:05:05 +02008134 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8135 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8136 PSA_ALG_ECDSA( psa_hash_alg ),
8137 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008138 continue;
8139
Neil Armstrong96eceb82022-06-30 18:05:05 +02008140 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008141 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8142 PSA_ALG_RSA_PKCS1V15_SIGN(
8143 psa_hash_alg ),
8144 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008145 continue;
8146 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008147#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008148
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008149 return( hash_alg_received );
8150 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008151 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008152
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008153 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008154}
8155
8156#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8157
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008158/* Serialization of TLS 1.2 sessions:
8159 *
8160 * struct {
8161 * uint64 start_time;
8162 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008163 * uint8 session_id_len; // at most 32
8164 * opaque session_id[32];
8165 * opaque master[48]; // fixed length in the standard
8166 * uint32 verify_result;
8167 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8168 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8169 * uint32 ticket_lifetime;
8170 * uint8 mfl_code; // up to 255 according to standard
8171 * uint8 encrypt_then_mac; // 0 or 1
8172 * } serialized_session_tls12;
8173 *
8174 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008175static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008176 unsigned char *buf,
8177 size_t buf_len )
8178{
8179 unsigned char *p = buf;
8180 size_t used = 0;
8181
8182#if defined(MBEDTLS_HAVE_TIME)
8183 uint64_t start;
8184#endif
8185#if defined(MBEDTLS_X509_CRT_PARSE_C)
8186#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8187 size_t cert_len;
8188#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8189#endif /* MBEDTLS_X509_CRT_PARSE_C */
8190
8191 /*
8192 * Time
8193 */
8194#if defined(MBEDTLS_HAVE_TIME)
8195 used += 8;
8196
8197 if( used <= buf_len )
8198 {
8199 start = (uint64_t) session->start;
8200
8201 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8202 p += 8;
8203 }
8204#endif /* MBEDTLS_HAVE_TIME */
8205
8206 /*
8207 * Basic mandatory fields
8208 */
8209 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008210 + 1 /* id_len */
8211 + sizeof( session->id )
8212 + sizeof( session->master )
8213 + 4; /* verify_result */
8214
8215 if( used <= buf_len )
8216 {
8217 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8218 p += 2;
8219
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008220 *p++ = MBEDTLS_BYTE_0( session->id_len );
8221 memcpy( p, session->id, 32 );
8222 p += 32;
8223
8224 memcpy( p, session->master, 48 );
8225 p += 48;
8226
8227 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8228 p += 4;
8229 }
8230
8231 /*
8232 * Peer's end-entity certificate
8233 */
8234#if defined(MBEDTLS_X509_CRT_PARSE_C)
8235#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8236 if( session->peer_cert == NULL )
8237 cert_len = 0;
8238 else
8239 cert_len = session->peer_cert->raw.len;
8240
8241 used += 3 + cert_len;
8242
8243 if( used <= buf_len )
8244 {
8245 *p++ = MBEDTLS_BYTE_2( cert_len );
8246 *p++ = MBEDTLS_BYTE_1( cert_len );
8247 *p++ = MBEDTLS_BYTE_0( cert_len );
8248
8249 if( session->peer_cert != NULL )
8250 {
8251 memcpy( p, session->peer_cert->raw.p, cert_len );
8252 p += cert_len;
8253 }
8254 }
8255#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8256 if( session->peer_cert_digest != NULL )
8257 {
8258 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8259 if( used <= buf_len )
8260 {
8261 *p++ = (unsigned char) session->peer_cert_digest_type;
8262 *p++ = (unsigned char) session->peer_cert_digest_len;
8263 memcpy( p, session->peer_cert_digest,
8264 session->peer_cert_digest_len );
8265 p += session->peer_cert_digest_len;
8266 }
8267 }
8268 else
8269 {
8270 used += 2;
8271 if( used <= buf_len )
8272 {
8273 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8274 *p++ = 0;
8275 }
8276 }
8277#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8278#endif /* MBEDTLS_X509_CRT_PARSE_C */
8279
8280 /*
8281 * Session ticket if any, plus associated data
8282 */
8283#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8284 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8285
8286 if( used <= buf_len )
8287 {
8288 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8289 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8290 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8291
8292 if( session->ticket != NULL )
8293 {
8294 memcpy( p, session->ticket, session->ticket_len );
8295 p += session->ticket_len;
8296 }
8297
8298 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8299 p += 4;
8300 }
8301#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8302
8303 /*
8304 * Misc extension-related info
8305 */
8306#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8307 used += 1;
8308
8309 if( used <= buf_len )
8310 *p++ = session->mfl_code;
8311#endif
8312
8313#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8314 used += 1;
8315
8316 if( used <= buf_len )
8317 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8318#endif
8319
8320 return( used );
8321}
8322
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008323MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008324static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008325 const unsigned char *buf,
8326 size_t len )
8327{
8328#if defined(MBEDTLS_HAVE_TIME)
8329 uint64_t start;
8330#endif
8331#if defined(MBEDTLS_X509_CRT_PARSE_C)
8332#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8333 size_t cert_len;
8334#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8335#endif /* MBEDTLS_X509_CRT_PARSE_C */
8336
8337 const unsigned char *p = buf;
8338 const unsigned char * const end = buf + len;
8339
8340 /*
8341 * Time
8342 */
8343#if defined(MBEDTLS_HAVE_TIME)
8344 if( 8 > (size_t)( end - p ) )
8345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8346
8347 start = ( (uint64_t) p[0] << 56 ) |
8348 ( (uint64_t) p[1] << 48 ) |
8349 ( (uint64_t) p[2] << 40 ) |
8350 ( (uint64_t) p[3] << 32 ) |
8351 ( (uint64_t) p[4] << 24 ) |
8352 ( (uint64_t) p[5] << 16 ) |
8353 ( (uint64_t) p[6] << 8 ) |
8354 ( (uint64_t) p[7] );
8355 p += 8;
8356
8357 session->start = (time_t) start;
8358#endif /* MBEDTLS_HAVE_TIME */
8359
8360 /*
8361 * Basic mandatory fields
8362 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008363 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8365
8366 session->ciphersuite = ( p[0] << 8 ) | p[1];
8367 p += 2;
8368
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008369 session->id_len = *p++;
8370 memcpy( session->id, p, 32 );
8371 p += 32;
8372
8373 memcpy( session->master, p, 48 );
8374 p += 48;
8375
8376 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8377 ( (uint32_t) p[1] << 16 ) |
8378 ( (uint32_t) p[2] << 8 ) |
8379 ( (uint32_t) p[3] );
8380 p += 4;
8381
8382 /* Immediately clear invalid pointer values that have been read, in case
8383 * we exit early before we replaced them with valid ones. */
8384#if defined(MBEDTLS_X509_CRT_PARSE_C)
8385#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8386 session->peer_cert = NULL;
8387#else
8388 session->peer_cert_digest = NULL;
8389#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8390#endif /* MBEDTLS_X509_CRT_PARSE_C */
8391#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8392 session->ticket = NULL;
8393#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8394
8395 /*
8396 * Peer certificate
8397 */
8398#if defined(MBEDTLS_X509_CRT_PARSE_C)
8399#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8400 /* Deserialize CRT from the end of the ticket. */
8401 if( 3 > (size_t)( end - p ) )
8402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8403
8404 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8405 p += 3;
8406
8407 if( cert_len != 0 )
8408 {
8409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8410
8411 if( cert_len > (size_t)( end - p ) )
8412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8413
8414 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8415
8416 if( session->peer_cert == NULL )
8417 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8418
8419 mbedtls_x509_crt_init( session->peer_cert );
8420
8421 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8422 p, cert_len ) ) != 0 )
8423 {
8424 mbedtls_x509_crt_free( session->peer_cert );
8425 mbedtls_free( session->peer_cert );
8426 session->peer_cert = NULL;
8427 return( ret );
8428 }
8429
8430 p += cert_len;
8431 }
8432#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8433 /* Deserialize CRT digest from the end of the ticket. */
8434 if( 2 > (size_t)( end - p ) )
8435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8436
8437 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8438 session->peer_cert_digest_len = (size_t) *p++;
8439
8440 if( session->peer_cert_digest_len != 0 )
8441 {
8442 const mbedtls_md_info_t *md_info =
8443 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8444 if( md_info == NULL )
8445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8446 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8448
8449 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8451
8452 session->peer_cert_digest =
8453 mbedtls_calloc( 1, session->peer_cert_digest_len );
8454 if( session->peer_cert_digest == NULL )
8455 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8456
8457 memcpy( session->peer_cert_digest, p,
8458 session->peer_cert_digest_len );
8459 p += session->peer_cert_digest_len;
8460 }
8461#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8462#endif /* MBEDTLS_X509_CRT_PARSE_C */
8463
8464 /*
8465 * Session ticket and associated data
8466 */
8467#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8468 if( 3 > (size_t)( end - p ) )
8469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8470
8471 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8472 p += 3;
8473
8474 if( session->ticket_len != 0 )
8475 {
8476 if( session->ticket_len > (size_t)( end - p ) )
8477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8478
8479 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8480 if( session->ticket == NULL )
8481 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8482
8483 memcpy( session->ticket, p, session->ticket_len );
8484 p += session->ticket_len;
8485 }
8486
8487 if( 4 > (size_t)( end - p ) )
8488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8489
8490 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8491 ( (uint32_t) p[1] << 16 ) |
8492 ( (uint32_t) p[2] << 8 ) |
8493 ( (uint32_t) p[3] );
8494 p += 4;
8495#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8496
8497 /*
8498 * Misc extension-related info
8499 */
8500#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8501 if( 1 > (size_t)( end - p ) )
8502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8503
8504 session->mfl_code = *p++;
8505#endif
8506
8507#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8508 if( 1 > (size_t)( end - p ) )
8509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8510
8511 session->encrypt_then_mac = *p++;
8512#endif
8513
8514 /* Done, should have consumed entire buffer */
8515 if( p != end )
8516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8517
8518 return( 0 );
8519}
Jerry Yudc7bd172022-02-17 13:44:15 +08008520#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8521
XiaokangQian75d40ef2022-04-20 11:05:24 +00008522int mbedtls_ssl_validate_ciphersuite(
8523 const mbedtls_ssl_context *ssl,
8524 const mbedtls_ssl_ciphersuite_t *suite_info,
8525 mbedtls_ssl_protocol_version min_tls_version,
8526 mbedtls_ssl_protocol_version max_tls_version )
8527{
8528 (void) ssl;
8529
8530 if( suite_info == NULL )
8531 return( -1 );
8532
8533 if( ( suite_info->min_tls_version > max_tls_version ) ||
8534 ( suite_info->max_tls_version < min_tls_version ) )
8535 {
8536 return( -1 );
8537 }
8538
XiaokangQian060d8672022-04-21 09:24:56 +00008539#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008540#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8541 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8542 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8543 {
8544 return( -1 );
8545 }
8546#endif
8547
8548 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8549#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8550 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8551 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8552 {
8553 return( -1 );
8554 }
8555#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8556#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8557
8558 return( 0 );
8559}
8560
XiaokangQianeaf36512022-04-24 09:07:44 +00008561#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8562/*
8563 * Function for writing a signature algorithm extension.
8564 *
8565 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8566 * value (TLS 1.3 RFC8446):
8567 * enum {
8568 * ....
8569 * ecdsa_secp256r1_sha256( 0x0403 ),
8570 * ecdsa_secp384r1_sha384( 0x0503 ),
8571 * ecdsa_secp521r1_sha512( 0x0603 ),
8572 * ....
8573 * } SignatureScheme;
8574 *
8575 * struct {
8576 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8577 * } SignatureSchemeList;
8578 *
8579 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8580 * value (TLS 1.2 RFC5246):
8581 * enum {
8582 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8583 * sha512(6), (255)
8584 * } HashAlgorithm;
8585 *
8586 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8587 * SignatureAlgorithm;
8588 *
8589 * struct {
8590 * HashAlgorithm hash;
8591 * SignatureAlgorithm signature;
8592 * } SignatureAndHashAlgorithm;
8593 *
8594 * SignatureAndHashAlgorithm
8595 * supported_signature_algorithms<2..2^16-2>;
8596 *
8597 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8598 * generalization of the TLS 1.2 signature algorithm extension.
8599 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8600 * `SignatureScheme` field of TLS 1.3
8601 *
8602 */
8603int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8604 const unsigned char *end, size_t *out_len )
8605{
8606 unsigned char *p = buf;
8607 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8608 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8609
8610 *out_len = 0;
8611
8612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8613
8614 /* Check if we have space for header and length field:
8615 * - extension_type (2 bytes)
8616 * - extension_data_length (2 bytes)
8617 * - supported_signature_algorithms_length (2 bytes)
8618 */
8619 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8620 p += 6;
8621
8622 /*
8623 * Write supported_signature_algorithms
8624 */
8625 supported_sig_alg = p;
8626 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8627 if( sig_alg == NULL )
8628 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8629
8630 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8631 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8633 *sig_alg,
8634 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008635 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8636 continue;
8637 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8638 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8639 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008641 *sig_alg,
8642 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008643 }
8644
8645 /* Length of supported_signature_algorithms */
8646 supported_sig_alg_len = p - supported_sig_alg;
8647 if( supported_sig_alg_len == 0 )
8648 {
8649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8651 }
8652
XiaokangQianeaf36512022-04-24 09:07:44 +00008653 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008654 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008655 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8656
XiaokangQianeaf36512022-04-24 09:07:44 +00008657 *out_len = p - buf;
8658
8659#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8660 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8661#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8662 return( 0 );
8663}
8664#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8665
XiaokangQian40a35232022-05-07 09:02:40 +00008666#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008667/*
8668 * mbedtls_ssl_parse_server_name_ext
8669 *
8670 * Structure of server_name extension:
8671 *
8672 * enum {
8673 * host_name(0), (255)
8674 * } NameType;
8675 * opaque HostName<1..2^16-1>;
8676 *
8677 * struct {
8678 * NameType name_type;
8679 * select (name_type) {
8680 * case host_name: HostName;
8681 * } name;
8682 * } ServerName;
8683 * struct {
8684 * ServerName server_name_list<1..2^16-1>
8685 * } ServerNameList;
8686 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008687MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008688int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8689 const unsigned char *buf,
8690 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008691{
8692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8693 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008694 size_t server_name_list_len, hostname_len;
8695 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008696
XiaokangQianf2a94202022-05-20 06:44:24 +00008697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008698
8699 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008700 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008701 p += 2;
8702
XiaokangQian9b2b7712022-05-17 02:57:00 +00008703 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8704 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008705 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008706 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008707 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008708 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8710 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008711
8712 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8713 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008714 /* sni_name is intended to be used only during the parsing of the
8715 * ClientHello message (it is reset to NULL before the end of
8716 * the message parsing). Thus it is ok to just point to the
8717 * reception buffer and not make a copy of it.
8718 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008719 ssl->handshake->sni_name = p + 3;
8720 ssl->handshake->sni_name_len = hostname_len;
8721 if( ssl->conf->f_sni == NULL )
8722 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008723 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008724 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008725 if( ret != 0 )
8726 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008727 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008728 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8729 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008730 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8731 }
8732 return( 0 );
8733 }
8734
8735 p += hostname_len + 3;
8736 }
8737
8738 return( 0 );
8739}
8740#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8741
XiaokangQianacb39922022-06-17 10:18:48 +00008742#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008743MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008744int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8745 const unsigned char *buf,
8746 const unsigned char *end )
8747{
8748 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008749 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008750 const unsigned char *protocol_name_list;
8751 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008752 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008753
8754 /* If ALPN not configured, just ignore the extension */
8755 if( ssl->conf->alpn_list == NULL )
8756 return( 0 );
8757
8758 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008759 * RFC7301, section 3.1
8760 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008761 *
XiaokangQianc7403452022-06-23 03:24:12 +00008762 * struct {
8763 * ProtocolName protocol_name_list<2..2^16-1>
8764 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008765 */
8766
XiaokangQianc7403452022-06-23 03:24:12 +00008767 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008768 * protocol_name_list_len 2 bytes
8769 * protocol_name_len 1 bytes
8770 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008771 */
XiaokangQianacb39922022-06-17 10:18:48 +00008772 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8773
XiaokangQianc7403452022-06-23 03:24:12 +00008774 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008775 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008776 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008777 protocol_name_list = p;
8778 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008779
8780 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008781 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008782 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008783 protocol_name_len = *p++;
8784 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8785 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008786 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008787 {
8788 MBEDTLS_SSL_PEND_FATAL_ALERT(
8789 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8790 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008791 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008792 }
8793
8794 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008795 }
8796
8797 /* Use our order of preference */
8798 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8799 {
8800 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008801 p = protocol_name_list;
8802 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008803 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008804 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008805 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008806 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008807 {
8808 ssl->alpn_chosen = *alpn;
8809 return( 0 );
8810 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008811
8812 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008813 }
8814 }
8815
XiaokangQian95d5f542022-06-24 02:29:26 +00008816 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008817 MBEDTLS_SSL_PEND_FATAL_ALERT(
8818 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8819 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8820 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8821}
8822
8823int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8824 unsigned char *buf,
8825 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008826 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008827{
8828 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008829 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008830 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008831
8832 if( ssl->alpn_chosen == NULL )
8833 {
8834 return( 0 );
8835 }
8836
XiaokangQian95d5f542022-06-24 02:29:26 +00008837 protocol_name_len = strlen( ssl->alpn_chosen );
8838 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008839
8840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8841 /*
8842 * 0 . 1 ext identifier
8843 * 2 . 3 ext length
8844 * 4 . 5 protocol list length
8845 * 6 . 6 protocol name length
8846 * 7 . 7+n protocol name
8847 */
8848 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8849
XiaokangQian95d5f542022-06-24 02:29:26 +00008850 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008851
XiaokangQian95d5f542022-06-24 02:29:26 +00008852 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8853 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008854 /* Note: the length of the chosen protocol has been checked to be less
8855 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8856 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008857 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008858
XiaokangQian95d5f542022-06-24 02:29:26 +00008859 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008860 return ( 0 );
8861}
8862#endif /* MBEDTLS_SSL_ALPN */
8863
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008864#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
8865 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8866int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8867 const char *hostname )
8868{
8869 /* Initialize to suppress unnecessary compiler warning */
8870 size_t hostname_len = 0;
8871
8872 /* Check if new hostname is valid before
8873 * making any change to current one */
8874 if( hostname != NULL )
8875 {
8876 hostname_len = strlen( hostname );
8877
8878 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8880 }
8881
8882 /* Now it's clear that we will overwrite the old hostname,
8883 * so we can free it safely */
8884 if( session->hostname != NULL )
8885 {
8886 mbedtls_platform_zeroize( session->hostname,
8887 strlen( session->hostname ) );
8888 mbedtls_free( session->hostname );
8889 }
8890
8891 /* Passing NULL as hostname shall clear the old one */
8892 if( hostname == NULL )
8893 {
8894 session->hostname = NULL;
8895 }
8896 else
8897 {
8898 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8899 if( session->hostname == NULL )
8900 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8901
8902 memcpy( session->hostname, hostname, hostname_len );
8903 }
8904
8905 return( 0 );
8906}
8907#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SERVER_NAME_INDICATION */
8908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909#endif /* MBEDTLS_SSL_TLS_C */