blob: c677455132551c1cee44ba15c32ac8073bc2eaed [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 }
298
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000299#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000300 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000301 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000302 {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000303 dst->hostname = NULL;
Xiaokang Qian03409292022-10-12 02:49:52 +0000304 return mbedtls_ssl_session_set_hostname( dst,
305 src->hostname );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000306 }
307#endif
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000308#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000309
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310 return( 0 );
311}
312
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200314MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500315static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
316{
317 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
318 if( resized_buffer == NULL )
319 return -1;
320
321 /* We want to copy len_new bytes when downsizing the buffer, and
322 * len_old bytes when upsizing, so we choose the smaller of two sizes,
323 * to fit one buffer into another. Size checks, ensuring that no data is
324 * lost, are done outside of this function. */
325 memcpy( resized_buffer, *buffer,
326 ( len_new < *len_old ) ? len_new : *len_old );
327 mbedtls_platform_zeroize( *buffer, *len_old );
328 mbedtls_free( *buffer );
329
330 *buffer = resized_buffer;
331 *len_old = len_new;
332
333 return 0;
334}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200335
336static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500337 size_t in_buf_new_len,
338 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200339{
340 int modified = 0;
341 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
342 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
343 if( ssl->in_buf != NULL )
344 {
345 written_in = ssl->in_msg - ssl->in_buf;
346 iv_offset_in = ssl->in_iv - ssl->in_buf;
347 len_offset_in = ssl->in_len - ssl->in_buf;
348 if( downsizing ?
349 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
350 ssl->in_buf_len < in_buf_new_len )
351 {
352 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
353 {
354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
355 }
356 else
357 {
Paul Elliottb7449902021-03-10 18:14:58 +0000358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
359 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200360 modified = 1;
361 }
362 }
363 }
364
365 if( ssl->out_buf != NULL )
366 {
367 written_out = ssl->out_msg - ssl->out_buf;
368 iv_offset_out = ssl->out_iv - ssl->out_buf;
369 len_offset_out = ssl->out_len - ssl->out_buf;
370 if( downsizing ?
371 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
372 ssl->out_buf_len < out_buf_new_len )
373 {
374 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
375 {
376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
377 }
378 else
379 {
Paul Elliottb7449902021-03-10 18:14:58 +0000380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
381 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200382 modified = 1;
383 }
384 }
385 }
386 if( modified )
387 {
388 /* Update pointers here to avoid doing it twice. */
389 mbedtls_ssl_reset_in_out_pointers( ssl );
390 /* Fields below might not be properly updated with record
391 * splitting or with CID, so they are manually updated here. */
392 ssl->out_msg = ssl->out_buf + written_out;
393 ssl->out_len = ssl->out_buf + len_offset_out;
394 ssl->out_iv = ssl->out_buf + iv_offset_out;
395
396 ssl->in_msg = ssl->in_buf + written_in;
397 ssl->in_len = ssl->in_buf + len_offset_in;
398 ssl->in_iv = ssl->in_buf + iv_offset_in;
399 }
400}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500401#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
402
Jerry Yudb8c48a2022-01-27 14:54:54 +0800403#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800404
405#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
406typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
407 const char *label,
408 const unsigned char *random, size_t rlen,
409 unsigned char *dstbuf, size_t dlen );
410
411static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
412
413#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
414
415/* Type for the TLS PRF */
416typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
417 const unsigned char *, size_t,
418 unsigned char *, size_t);
419
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200420MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800421static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
422 int ciphersuite,
423 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200424#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800425 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200426#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800427 ssl_tls_prf_t tls_prf,
428 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400429 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800430 unsigned endpoint,
431 const mbedtls_ssl_context *ssl );
432
Andrzej Kurek25f27152022-08-17 16:09:31 -0400433#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200434MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800435static int tls_prf_sha256( const unsigned char *secret, size_t slen,
436 const char *label,
437 const unsigned char *random, size_t rlen,
438 unsigned char *dstbuf, size_t dlen );
439static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
440static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
441
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400442#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800443
Andrzej Kurek25f27152022-08-17 16:09:31 -0400444#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200445MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800446static int tls_prf_sha384( const unsigned char *secret, size_t slen,
447 const char *label,
448 const unsigned char *random, size_t rlen,
449 unsigned char *dstbuf, size_t dlen );
450
451static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
452static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400453#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800454
Jerry Yu438ddd82022-07-07 06:55:50 +0000455static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800456 unsigned char *buf,
457 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800458
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200459MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000460static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800461 const unsigned char *buf,
462 size_t len );
463#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
464
Jerry Yu53d23e22022-02-09 16:25:09 +0800465static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
466
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800468static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400469#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800470
Andrzej Kurek25f27152022-08-17 16:09:31 -0400471#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800472static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400473#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000474
Ron Eldor51d3ab52019-05-12 14:54:30 +0300475int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
476 const unsigned char *secret, size_t slen,
477 const char *label,
478 const unsigned char *random, size_t rlen,
479 unsigned char *dstbuf, size_t dlen )
480{
481 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
482
483 switch( prf )
484 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300485#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400486#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487 case MBEDTLS_SSL_TLS_PRF_SHA384:
488 tls_prf = tls_prf_sha384;
489 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400490#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400491#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300492 case MBEDTLS_SSL_TLS_PRF_SHA256:
493 tls_prf = tls_prf_sha256;
494 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400495#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300496#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300497 default:
498 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
499 }
500
501 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
502}
503
Jerry Yuc73c6182022-02-08 20:29:25 +0800504#if defined(MBEDTLS_X509_CRT_PARSE_C)
505static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
506{
507#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
508 if( session->peer_cert != NULL )
509 {
510 mbedtls_x509_crt_free( session->peer_cert );
511 mbedtls_free( session->peer_cert );
512 session->peer_cert = NULL;
513 }
514#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
515 if( session->peer_cert_digest != NULL )
516 {
517 /* Zeroization is not necessary. */
518 mbedtls_free( session->peer_cert_digest );
519 session->peer_cert_digest = NULL;
520 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
521 session->peer_cert_digest_len = 0;
522 }
523#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
524}
525#endif /* MBEDTLS_X509_CRT_PARSE_C */
526
527void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
528 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
529{
530 ((void) ciphersuite_info);
531
Andrzej Kurek25f27152022-08-17 16:09:31 -0400532#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800533 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
534 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
535 else
536#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400537#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800538 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
539 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
540 else
541#endif
542 {
543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
544 return;
545 }
546}
547
XiaokangQianadab9a62022-07-18 07:41:26 +0000548void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
549 unsigned hs_type,
550 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100551{
552 unsigned char hs_hdr[4];
553
554 /* Build HS header for checksum update. */
555 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
556 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
557 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
558 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
559
560 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
561}
562
563void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
564 unsigned hs_type,
565 unsigned char const *msg,
566 size_t msg_len )
567{
568 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
569 ssl->handshake->update_checksum( ssl, msg, msg_len );
570}
571
Jerry Yuc73c6182022-02-08 20:29:25 +0800572void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
573{
574 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400575#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800576#if defined(MBEDTLS_USE_PSA_CRYPTO)
577 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
578 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
579#else
580 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
581#endif
582#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400583#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800584#if defined(MBEDTLS_USE_PSA_CRYPTO)
585 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
586 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
587#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400588 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800589#endif
590#endif
591}
592
593static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
594 const unsigned char *buf, size_t len )
595{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400596#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800597#if defined(MBEDTLS_USE_PSA_CRYPTO)
598 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
599#else
600 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
601#endif
602#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400603#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800604#if defined(MBEDTLS_USE_PSA_CRYPTO)
605 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
606#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400607 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800608#endif
609#endif
610}
611
Andrzej Kurek25f27152022-08-17 16:09:31 -0400612#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800613static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
614 const unsigned char *buf, size_t len )
615{
616#if defined(MBEDTLS_USE_PSA_CRYPTO)
617 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
618#else
619 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
620#endif
621}
622#endif
623
Andrzej Kurek25f27152022-08-17 16:09:31 -0400624#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800625static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
626 const unsigned char *buf, size_t len )
627{
628#if defined(MBEDTLS_USE_PSA_CRYPTO)
629 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
630#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400631 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800632#endif
633}
634#endif
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200639
Andrzej Kurek25f27152022-08-17 16:09:31 -0400640#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500641#if defined(MBEDTLS_USE_PSA_CRYPTO)
642 handshake->fin_sha256_psa = psa_hash_operation_init();
643 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
644#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200646 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200647#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500648#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400649#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500650#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500651 handshake->fin_sha384_psa = psa_hash_operation_init();
652 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500653#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400654 mbedtls_sha512_init( &handshake->fin_sha384 );
655 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200656#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500657#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200658
659 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_DHM_C)
662 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200663#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200664#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200666#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200667#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200668 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200669#if defined(MBEDTLS_SSL_CLI_C)
670 handshake->ecjpake_cache = NULL;
671 handshake->ecjpake_cache_len = 0;
672#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200673#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200674
Gilles Peskineeccd8882020-03-10 12:19:08 +0100675#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200676 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200677#endif
678
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200679#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
680 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
681#endif
Hanno Becker75173122019-02-06 16:18:31 +0000682
683#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
684 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
685 mbedtls_pk_init( &handshake->peer_pubkey );
686#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200687}
688
Hanno Beckera18d1322018-01-03 14:27:32 +0000689void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200692
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100693#if defined(MBEDTLS_USE_PSA_CRYPTO)
694 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
695 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100696#else
697 mbedtls_cipher_init( &transform->cipher_ctx_enc );
698 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100699#endif
700
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000701#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100702#if defined(MBEDTLS_USE_PSA_CRYPTO)
703 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
704 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100705#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_md_init( &transform->md_ctx_enc );
707 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000708#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100709#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200710}
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200715}
716
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200717MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000719{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200720 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000721 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200725 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200726 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727
728 /*
729 * Either the pointers are now NULL or cleared properly and can be freed.
730 * Now allocate missing structures.
731 */
732 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200733 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200734 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200735 }
Paul Bakker48916f92012-09-16 19:57:18 +0000736
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200737 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200738 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200739 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200740 }
Paul Bakker48916f92012-09-16 19:57:18 +0000741
Paul Bakker82788fb2014-10-20 13:59:19 +0200742 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200743 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200744 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200745 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500746#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
747 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400748
Andrzej Kurek4a063792020-10-21 15:08:44 +0200749 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
750 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500751#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000752
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200753 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000754 if( ssl->handshake == NULL ||
755 ssl->transform_negotiate == NULL ||
756 ssl->session_negotiate == NULL )
757 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_free( ssl->handshake );
761 mbedtls_free( ssl->transform_negotiate );
762 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200763
764 ssl->handshake = NULL;
765 ssl->transform_negotiate = NULL;
766 ssl->session_negotiate = NULL;
767
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200768 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000769 }
770
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200771 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000773 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200774 ssl_handshake_params_init( ssl->handshake );
775
Jerry Yud0766ec2022-09-22 10:46:57 +0800776#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
777 defined(MBEDTLS_SSL_SRV_C) && \
778 defined(MBEDTLS_SSL_SESSION_TICKETS)
779 ssl->handshake->new_session_tickets_count =
780 ssl->conf->new_session_tickets_count ;
781#endif
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200784 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
785 {
786 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200787
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200788 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
789 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
790 else
791 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200792
Hanno Becker0f57a652020-02-05 10:37:26 +0000793 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200794 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200795#endif
796
Brett Warrene0edc842021-08-17 09:53:13 +0100797/*
798 * curve_list is translated to IANA TLS group identifiers here because
799 * mbedtls_ssl_conf_curves returns void and so can't return
800 * any error codes.
801 */
802#if defined(MBEDTLS_ECP_C)
803#if !defined(MBEDTLS_DEPRECATED_REMOVED)
804 /* Heap allocate and translate curve_list from internal to IANA group ids */
805 if ( ssl->conf->curve_list != NULL )
806 {
807 size_t length;
808 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
809
810 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
811 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
812
813 /* Leave room for zero termination */
814 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
815 if ( group_list == NULL )
816 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
817
818 for( size_t i = 0; i < length; i++ )
819 {
820 const mbedtls_ecp_curve_info *info =
821 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
822 if ( info == NULL )
823 {
824 mbedtls_free( group_list );
825 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
826 }
827 group_list[i] = info->tls_id;
828 }
829
830 group_list[length] = 0;
831
832 ssl->handshake->group_list = group_list;
833 ssl->handshake->group_list_heap_allocated = 1;
834 }
835 else
836 {
837 ssl->handshake->group_list = ssl->conf->group_list;
838 ssl->handshake->group_list_heap_allocated = 0;
839 }
840#endif /* MBEDTLS_DEPRECATED_REMOVED */
841#endif /* MBEDTLS_ECP_C */
842
Jerry Yuf017ee42022-01-12 15:49:48 +0800843#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
844#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800845#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800846 /* Heap allocate and translate sig_hashes from internal hash identifiers to
847 signature algorithms IANA identifiers. */
848 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800849 ssl->conf->sig_hashes != NULL )
850 {
851 const int *md;
852 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800853 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800854 uint16_t *p;
855
Jerry Yub476a442022-01-21 18:14:45 +0800856#if defined(static_assert)
857 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
858 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
859 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800860#endif
861
Jerry Yuf017ee42022-01-12 15:49:48 +0800862 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
863 {
864 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
865 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800866#if defined(MBEDTLS_ECDSA_C)
867 sig_algs_len += sizeof( uint16_t );
868#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800869
Jerry Yub476a442022-01-21 18:14:45 +0800870#if defined(MBEDTLS_RSA_C)
871 sig_algs_len += sizeof( uint16_t );
872#endif
873 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
874 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800875 }
876
Jerry Yub476a442022-01-21 18:14:45 +0800877 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800878 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
879
Jerry Yub476a442022-01-21 18:14:45 +0800880 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
881 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 if( ssl->handshake->sig_algs == NULL )
883 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
884
885 p = (uint16_t *)ssl->handshake->sig_algs;
886 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
887 {
888 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
889 if( hash == MBEDTLS_SSL_HASH_NONE )
890 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800891#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800892 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
893 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800894#endif
895#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800896 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
897 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800898#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800899 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200900 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800901 ssl->handshake->sig_algs_heap_allocated = 1;
902 }
903 else
Jerry Yua69269a2022-01-17 21:06:01 +0800904#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800905 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800906 ssl->handshake->sig_algs_heap_allocated = 0;
907 }
Jerry Yucc539102022-06-27 16:27:35 +0800908#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800909#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000910 return( 0 );
911}
912
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200913#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200914/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200915MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200916static int ssl_cookie_write_dummy( void *ctx,
917 unsigned char **p, unsigned char *end,
918 const unsigned char *cli_id, size_t cli_id_len )
919{
920 ((void) ctx);
921 ((void) p);
922 ((void) end);
923 ((void) cli_id);
924 ((void) cli_id_len);
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200927}
928
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200929MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200930static int ssl_cookie_check_dummy( void *ctx,
931 const unsigned char *cookie, size_t cookie_len,
932 const unsigned char *cli_id, size_t cli_id_len )
933{
934 ((void) ctx);
935 ((void) cookie);
936 ((void) cookie_len);
937 ((void) cli_id);
938 ((void) cli_id_len);
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200941}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200942#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200943
Paul Bakker5121ce52009-01-03 21:22:43 +0000944/*
945 * Initialize an SSL context
946 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200947void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
948{
949 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
950}
951
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200952MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800953static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
954{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100955 const mbedtls_ssl_config *conf = ssl->conf;
956
Ronald Cron6f135e12021-12-08 16:57:54 +0100957#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100958 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
959 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000960 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
961 {
962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
963 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
964 }
965
Jerry Yu60835a82021-08-04 10:13:52 +0800966 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
967 return( 0 );
968 }
969#endif
970
971#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100972 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800973 {
974 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
975 return( 0 );
976 }
977#endif
978
Ronald Cron6f135e12021-12-08 16:57:54 +0100979#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100980 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800981 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
983 {
984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
985 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
986 }
987
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000988 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
989 {
990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
991 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
992 }
993
994
Ronald Crone1d3f062022-02-10 14:50:54 +0100995 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
996 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800997 }
998#endif
999
1000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1001 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1002}
1003
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001004MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001005static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1006{
1007 int ret;
1008 ret = ssl_conf_version_check( ssl );
1009 if( ret != 0 )
1010 return( ret );
1011
1012 /* Space for further checks */
1013
1014 return( 0 );
1015}
1016
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001017/*
1018 * Setup an SSL context
1019 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001020
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001021int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001022 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001023{
Janos Follath865b3eb2019-12-16 11:46:15 +00001024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001025 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1026 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001028 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001029
Jerry Yu60835a82021-08-04 10:13:52 +08001030 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1031 return( ret );
1032
Paul Bakker62f2dee2012-09-28 07:31:51 +00001033 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001034 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001035 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001036
1037 /* Set to NULL in case of an error condition */
1038 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001039
Darryl Greenb33cc762019-11-28 14:29:44 +00001040#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1041 ssl->in_buf_len = in_buf_len;
1042#endif
1043 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001044 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001047 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001048 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001049 }
1050
Darryl Greenb33cc762019-11-28 14:29:44 +00001051#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1052 ssl->out_buf_len = out_buf_len;
1053#endif
1054 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001055 if( ssl->out_buf == NULL )
1056 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001058 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001059 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 }
1061
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001062 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001063
Johan Pascalb62bb512015-12-03 21:56:45 +01001064#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001065 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001066#endif
1067
Paul Bakker48916f92012-09-16 19:57:18 +00001068 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001069 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
1071 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001072
1073error:
1074 mbedtls_free( ssl->in_buf );
1075 mbedtls_free( ssl->out_buf );
1076
1077 ssl->conf = NULL;
1078
Darryl Greenb33cc762019-11-28 14:29:44 +00001079#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1080 ssl->in_buf_len = 0;
1081 ssl->out_buf_len = 0;
1082#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001083 ssl->in_buf = NULL;
1084 ssl->out_buf = NULL;
1085
1086 ssl->in_hdr = NULL;
1087 ssl->in_ctr = NULL;
1088 ssl->in_len = NULL;
1089 ssl->in_iv = NULL;
1090 ssl->in_msg = NULL;
1091
1092 ssl->out_hdr = NULL;
1093 ssl->out_ctr = NULL;
1094 ssl->out_len = NULL;
1095 ssl->out_iv = NULL;
1096 ssl->out_msg = NULL;
1097
k-stachowiak9f7798e2018-07-31 16:52:32 +02001098 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099}
1100
1101/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001102 * Reset an initialized and used SSL context for re-use while retaining
1103 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001104 *
1105 * If partial is non-zero, keep data in the input buffer and client ID.
1106 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001107 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001108void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1109 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001110{
Darryl Greenb33cc762019-11-28 14:29:44 +00001111#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1112 size_t in_buf_len = ssl->in_buf_len;
1113 size_t out_buf_len = ssl->out_buf_len;
1114#else
1115 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1116 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1117#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001118
Hanno Beckerb0302c42021-08-03 09:39:42 +01001119#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1120 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001121#endif
1122
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001123 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001124 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001125
Hanno Beckerb0302c42021-08-03 09:39:42 +01001126 mbedtls_ssl_reset_in_out_pointers( ssl );
1127
1128 /* Reset incoming message parsing */
1129 ssl->in_offt = NULL;
1130 ssl->nb_zero = 0;
1131 ssl->in_msgtype = 0;
1132 ssl->in_msglen = 0;
1133 ssl->in_hslen = 0;
1134 ssl->keep_current_message = 0;
1135 ssl->transform_in = NULL;
1136
1137#if defined(MBEDTLS_SSL_PROTO_DTLS)
1138 ssl->next_record_offset = 0;
1139 ssl->in_epoch = 0;
1140#endif
1141
1142 /* Keep current datagram if partial == 1 */
1143 if( partial == 0 )
1144 {
1145 ssl->in_left = 0;
1146 memset( ssl->in_buf, 0, in_buf_len );
1147 }
1148
Ronald Cronad8c17b2022-06-10 17:18:09 +02001149 ssl->send_alert = 0;
1150
Hanno Beckerb0302c42021-08-03 09:39:42 +01001151 /* Reset outgoing message writing */
1152 ssl->out_msgtype = 0;
1153 ssl->out_msglen = 0;
1154 ssl->out_left = 0;
1155 memset( ssl->out_buf, 0, out_buf_len );
1156 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1157 ssl->transform_out = NULL;
1158
1159#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1160 mbedtls_ssl_dtls_replay_reset( ssl );
1161#endif
1162
XiaokangQian2b01dc32022-01-21 02:53:13 +00001163#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001164 if( ssl->transform )
1165 {
1166 mbedtls_ssl_transform_free( ssl->transform );
1167 mbedtls_free( ssl->transform );
1168 ssl->transform = NULL;
1169 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1171
XiaokangQian2b01dc32022-01-21 02:53:13 +00001172#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1173 mbedtls_ssl_transform_free( ssl->transform_application );
1174 mbedtls_free( ssl->transform_application );
1175 ssl->transform_application = NULL;
1176
1177 if( ssl->handshake != NULL )
1178 {
1179 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1180 mbedtls_free( ssl->handshake->transform_earlydata );
1181 ssl->handshake->transform_earlydata = NULL;
1182
1183 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1184 mbedtls_free( ssl->handshake->transform_handshake );
1185 ssl->handshake->transform_handshake = NULL;
1186 }
1187
XiaokangQian2b01dc32022-01-21 02:53:13 +00001188#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001189}
1190
1191int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1192{
1193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1194
1195 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1196
XiaokangQian78b1fa72022-01-19 06:56:30 +00001197 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001198
1199 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#if defined(MBEDTLS_SSL_RENEGOTIATION)
1201 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001202 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001203
1204 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1206 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001209
Hanno Beckerb0302c42021-08-03 09:39:42 +01001210 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001211 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001212 if( ssl->session )
1213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_ssl_session_free( ssl->session );
1215 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001216 ssl->session = NULL;
1217 }
1218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001220 ssl->alpn_chosen = NULL;
1221#endif
1222
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001223#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001224#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001225 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001226#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001227 {
1228 mbedtls_free( ssl->cli_id );
1229 ssl->cli_id = NULL;
1230 ssl->cli_id_len = 0;
1231 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001232#endif
1233
Paul Bakker48916f92012-09-16 19:57:18 +00001234 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1235 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001236
1237 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001238}
1239
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001240/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001241 * Reset an initialized and used SSL context for re-use while retaining
1242 * all application-set variables, function pointers and data.
1243 */
1244int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1245{
Hanno Becker43aefe22020-02-05 10:44:56 +00001246 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001247}
1248
1249/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 * SSL set accessors
1251 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001252void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001254 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001255}
1256
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001257void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001259 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001260}
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001263void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001265 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001266}
1267#endif
1268
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001269void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001270{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001271 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001272}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001275
Hanno Becker1841b0a2018-08-24 11:13:57 +01001276void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1277 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001278{
1279 ssl->disable_datagram_packing = !allow_packing;
1280}
1281
1282void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1283 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001284{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001285 conf->hs_timeout_min = min;
1286 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001287}
1288#endif
1289
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001290void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001291{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001292 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293}
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001296void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001297 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001298 void *p_vrfy )
1299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001300 conf->f_vrfy = f_vrfy;
1301 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001304
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001305void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001306 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 void *p_rng )
1308{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001309 conf->f_rng = f_rng;
1310 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001311}
1312
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001313void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001314 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 void *p_dbg )
1316{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001317 conf->f_dbg = f_dbg;
1318 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001319}
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001322 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001323 mbedtls_ssl_send_t *f_send,
1324 mbedtls_ssl_recv_t *f_recv,
1325 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001326{
1327 ssl->p_bio = p_bio;
1328 ssl->f_send = f_send;
1329 ssl->f_recv = f_recv;
1330 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001331}
1332
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001333#if defined(MBEDTLS_SSL_PROTO_DTLS)
1334void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1335{
1336 ssl->mtu = mtu;
1337}
1338#endif
1339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001340void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001341{
1342 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001343}
1344
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001345void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1346 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001347 mbedtls_ssl_set_timer_t *f_set_timer,
1348 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001349{
1350 ssl->p_timer = p_timer;
1351 ssl->f_set_timer = f_set_timer;
1352 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001353
1354 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001355 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001356}
1357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001359void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001360 void *p_cache,
1361 mbedtls_ssl_cache_get_t *f_get_cache,
1362 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001363{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001364 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001365 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001366 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_SSL_CLI_C)
1371int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001372{
Janos Follath865b3eb2019-12-16 11:46:15 +00001373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001374
1375 if( ssl == NULL ||
1376 session == NULL ||
1377 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001378 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001381 }
1382
Hanno Beckere810bbc2021-05-14 16:01:05 +01001383 if( ssl->handshake->resume == 1 )
1384 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1385
Jerry Yu21092062022-10-10 21:21:31 +08001386#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1387 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001388 {
Jerry Yu21092062022-10-10 21:21:31 +08001389 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1390 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1391
1392 if( mbedtls_ssl_validate_ciphersuite(
1393 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1394 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1395 {
1396 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1397 session->ciphersuite ) );
1398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1399 }
Jerry Yu40afab62022-10-08 10:42:13 +08001400 }
Jerry Yu21092062022-10-10 21:21:31 +08001401#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001402
Hanno Becker52055ae2019-02-06 14:30:46 +00001403 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1404 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001405 return( ret );
1406
Paul Bakker0a597072012-09-25 21:55:46 +00001407 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001408
1409 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001413void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1414 const int *ciphersuites )
1415{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001416 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001417}
1418
Ronald Cron6f135e12021-12-08 16:57:54 +01001419#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001420void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001421 const int kex_modes )
1422{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001423 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001424}
Ronald Cron6f135e12021-12-08 16:57:54 +01001425#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001428void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001429 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001430{
1431 conf->cert_profile = profile;
1432}
1433
Glenn Strauss36872db2022-01-22 05:06:31 -05001434static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1435{
1436 mbedtls_ssl_key_cert *cur = key_cert, *next;
1437
1438 while( cur != NULL )
1439 {
1440 next = cur->next;
1441 mbedtls_free( cur );
1442 cur = next;
1443 }
1444}
1445
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001446/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001447MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001448static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1449 mbedtls_x509_crt *cert,
1450 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001451{
niisato8ee24222018-06-25 19:05:48 +09001452 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001453
Glenn Strauss36872db2022-01-22 05:06:31 -05001454 if( cert == NULL )
1455 {
1456 /* Free list if cert is null */
1457 ssl_key_cert_free( *head );
1458 *head = NULL;
1459 return( 0 );
1460 }
1461
niisato8ee24222018-06-25 19:05:48 +09001462 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1463 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001464 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001465
niisato8ee24222018-06-25 19:05:48 +09001466 new_cert->cert = cert;
1467 new_cert->key = key;
1468 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001469
Glenn Strauss36872db2022-01-22 05:06:31 -05001470 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001471 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001472 {
niisato8ee24222018-06-25 19:05:48 +09001473 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001474 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001475 else
1476 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001477 mbedtls_ssl_key_cert *cur = *head;
1478 while( cur->next != NULL )
1479 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001480 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001481 }
1482
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001483 return( 0 );
1484}
1485
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001486int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001487 mbedtls_x509_crt *own_cert,
1488 mbedtls_pk_context *pk_key )
1489{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001490 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001491}
1492
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001493void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001494 mbedtls_x509_crt *ca_chain,
1495 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001496{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001497 conf->ca_chain = ca_chain;
1498 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001499
1500#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1501 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1502 * cannot be used together. */
1503 conf->f_ca_cb = NULL;
1504 conf->p_ca_cb = NULL;
1505#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001506}
Hanno Becker5adaad92019-03-27 16:54:37 +00001507
1508#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1509void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001510 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001511 void *p_ca_cb )
1512{
1513 conf->f_ca_cb = f_ca_cb;
1514 conf->p_ca_cb = p_ca_cb;
1515
1516 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1517 * cannot be used together. */
1518 conf->ca_chain = NULL;
1519 conf->ca_crl = NULL;
1520}
1521#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001523
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001524#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001525const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1526 size_t *name_len )
1527{
1528 *name_len = ssl->handshake->sni_name_len;
1529 return( ssl->handshake->sni_name );
1530}
1531
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001532int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1533 mbedtls_x509_crt *own_cert,
1534 mbedtls_pk_context *pk_key )
1535{
1536 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1537 own_cert, pk_key ) );
1538}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001539
1540void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1541 mbedtls_x509_crt *ca_chain,
1542 mbedtls_x509_crl *ca_crl )
1543{
1544 ssl->handshake->sni_ca_chain = ca_chain;
1545 ssl->handshake->sni_ca_crl = ca_crl;
1546}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001547
Glenn Strauss999ef702022-03-11 01:37:23 -05001548#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1549void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1550 const mbedtls_x509_crt *crt)
1551{
1552 ssl->handshake->dn_hints = crt;
1553}
1554#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1555
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001556void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1557 int authmode )
1558{
1559 ssl->handshake->sni_authmode = authmode;
1560}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001561#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1562
Hanno Becker8927c832019-04-03 12:52:50 +01001563#if defined(MBEDTLS_X509_CRT_PARSE_C)
1564void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1565 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1566 void *p_vrfy )
1567{
1568 ssl->f_vrfy = f_vrfy;
1569 ssl->p_vrfy = p_vrfy;
1570}
1571#endif
1572
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001573#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001574/*
1575 * Set EC J-PAKE password for current handshake
1576 */
1577int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1578 const unsigned char *pw,
1579 size_t pw_len )
1580{
1581 mbedtls_ecjpake_role role;
1582
Janos Follath8eb64132016-06-03 15:40:57 +01001583 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1585
1586 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1587 role = MBEDTLS_ECJPAKE_SERVER;
1588 else
1589 role = MBEDTLS_ECJPAKE_CLIENT;
1590
1591 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1592 role,
1593 MBEDTLS_MD_SHA256,
1594 MBEDTLS_ECP_DP_SECP256R1,
1595 pw, pw_len ) );
1596}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001597#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001598
Gilles Peskineeccd8882020-03-10 12:19:08 +01001599#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001600
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001601MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001602static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1603{
1604#if defined(MBEDTLS_USE_PSA_CRYPTO)
1605 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1606 return( 1 );
1607#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001608 if( conf->psk != NULL )
1609 return( 1 );
1610
1611 return( 0 );
1612}
1613
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001614static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1615{
1616 /* Remove reference to existing PSK, if any. */
1617#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001618 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001619 {
1620 /* The maintenance of the PSK key slot is the
1621 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001622 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001623 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001624#endif /* MBEDTLS_USE_PSA_CRYPTO */
1625 if( conf->psk != NULL )
1626 {
1627 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1628
1629 mbedtls_free( conf->psk );
1630 conf->psk = NULL;
1631 conf->psk_len = 0;
1632 }
1633
1634 /* Remove reference to PSK identity, if any. */
1635 if( conf->psk_identity != NULL )
1636 {
1637 mbedtls_free( conf->psk_identity );
1638 conf->psk_identity = NULL;
1639 conf->psk_identity_len = 0;
1640 }
1641}
1642
Hanno Becker7390c712018-11-15 13:33:04 +00001643/* This function assumes that PSK identity in the SSL config is unset.
1644 * It checks that the provided identity is well-formed and attempts
1645 * to make a copy of it in the SSL config.
1646 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001647MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001648static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1649 unsigned char const *psk_identity,
1650 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001651{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001652 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001653 if( psk_identity == NULL ||
1654 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001655 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001656 {
1657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1658 }
1659
Hanno Becker7390c712018-11-15 13:33:04 +00001660 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1661 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001662 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001663
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001664 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001665 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001666
1667 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001668}
1669
Hanno Becker7390c712018-11-15 13:33:04 +00001670int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1671 const unsigned char *psk, size_t psk_len,
1672 const unsigned char *psk_identity, size_t psk_identity_len )
1673{
Janos Follath865b3eb2019-12-16 11:46:15 +00001674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001675
1676 /* We currently only support one PSK, raw or opaque. */
1677 if( ssl_conf_psk_is_configured( conf ) )
1678 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001679
1680 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001681 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001683 if( psk_len == 0 )
1684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1685 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1687
Hanno Becker7390c712018-11-15 13:33:04 +00001688 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1689 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1690 conf->psk_len = psk_len;
1691 memcpy( conf->psk, psk, conf->psk_len );
1692
1693 /* Check and set PSK Identity */
1694 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1695 if( ret != 0 )
1696 ssl_conf_remove_psk( conf );
1697
1698 return( ret );
1699}
1700
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001701static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1702{
1703#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001704 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001705 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001706 /* The maintenance of the external PSK key slot is the
1707 * user's responsibility. */
1708 if( ssl->handshake->psk_opaque_is_internal )
1709 {
1710 psa_destroy_key( ssl->handshake->psk_opaque );
1711 ssl->handshake->psk_opaque_is_internal = 0;
1712 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001713 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001714 }
Neil Armstronge952a302022-05-03 10:22:14 +02001715#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001716 if( ssl->handshake->psk != NULL )
1717 {
1718 mbedtls_platform_zeroize( ssl->handshake->psk,
1719 ssl->handshake->psk_len );
1720 mbedtls_free( ssl->handshake->psk );
1721 ssl->handshake->psk_len = 0;
1722 }
Neil Armstronge952a302022-05-03 10:22:14 +02001723#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001724}
1725
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001726int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1727 const unsigned char *psk, size_t psk_len )
1728{
Neil Armstrong501c9322022-05-03 09:35:09 +02001729#if defined(MBEDTLS_USE_PSA_CRYPTO)
1730 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001731 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001732 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001733 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001734#endif /* MBEDTLS_USE_PSA_CRYPTO */
1735
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001736 if( psk == NULL || ssl->handshake == NULL )
1737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1738
1739 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1741
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001742 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001743
Neil Armstrong501c9322022-05-03 09:35:09 +02001744#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1746 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1747 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001748 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001749 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1750 else
1751 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1752 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1753 }
1754#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001755
Jerry Yu568ec252022-07-22 21:27:34 +08001756#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001757 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1758 {
1759 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1760 psa_set_key_usage_flags( &key_attributes,
1761 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1762 }
1763#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1764
Neil Armstrong501c9322022-05-03 09:35:09 +02001765 psa_set_key_algorithm( &key_attributes, alg );
1766 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1767
1768 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1769 if( status != PSA_SUCCESS )
1770 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1771
1772 /* Allow calling psa_destroy_key() on psk remove */
1773 ssl->handshake->psk_opaque_is_internal = 1;
1774 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1775#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001776 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001777 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001778
1779 ssl->handshake->psk_len = psk_len;
1780 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1781
1782 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001783#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001784}
1785
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001786#if defined(MBEDTLS_USE_PSA_CRYPTO)
1787int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001788 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001789 const unsigned char *psk_identity,
1790 size_t psk_identity_len )
1791{
Janos Follath865b3eb2019-12-16 11:46:15 +00001792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001793
1794 /* We currently only support one PSK, raw or opaque. */
1795 if( ssl_conf_psk_is_configured( conf ) )
1796 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001797
Hanno Becker7390c712018-11-15 13:33:04 +00001798 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001799 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001801 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001802
1803 /* Check and set PSK Identity */
1804 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1805 psk_identity_len );
1806 if( ret != 0 )
1807 ssl_conf_remove_psk( conf );
1808
1809 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001810}
1811
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001812int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1813 mbedtls_svc_key_id_t psk )
1814{
1815 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1816 ( ssl->handshake == NULL ) )
1817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1818
1819 ssl_remove_psk( ssl );
1820 ssl->handshake->psk_opaque = psk;
1821 return( 0 );
1822}
1823#endif /* MBEDTLS_USE_PSA_CRYPTO */
1824
Jerry Yu8897c072022-08-12 13:56:53 +08001825#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001826void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1827 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1828 size_t),
1829 void *p_psk )
1830{
1831 conf->f_psk = f_psk;
1832 conf->p_psk = p_psk;
1833}
Jerry Yu8897c072022-08-12 13:56:53 +08001834#endif /* MBEDTLS_SSL_SRV_C */
1835
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001836#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1837
1838#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001839static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1840 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001841{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001842#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001843 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001844 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001846 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001847 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001848 return( MBEDTLS_SSL_MODE_STREAM );
1849}
1850
1851#else /* MBEDTLS_USE_PSA_CRYPTO */
1852
1853static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1854 mbedtls_cipher_mode_t mode )
1855{
1856#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1857 if( mode == MBEDTLS_MODE_CBC )
1858 return( MBEDTLS_SSL_MODE_CBC );
1859#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1860
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001861#if defined(MBEDTLS_GCM_C) || \
1862 defined(MBEDTLS_CCM_C) || \
1863 defined(MBEDTLS_CHACHAPOLY_C)
1864 if( mode == MBEDTLS_MODE_GCM ||
1865 mode == MBEDTLS_MODE_CCM ||
1866 mode == MBEDTLS_MODE_CHACHAPOLY )
1867 return( MBEDTLS_SSL_MODE_AEAD );
1868#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001869
1870 return( MBEDTLS_SSL_MODE_STREAM );
1871}
Gilles Peskine301711e2022-04-26 16:57:05 +02001872#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001873
Gilles Peskinee108d982022-04-26 16:50:40 +02001874static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1875 mbedtls_ssl_mode_t base_mode,
1876 int encrypt_then_mac )
1877{
1878#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1879 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1880 base_mode == MBEDTLS_SSL_MODE_CBC )
1881 {
1882 return( MBEDTLS_SSL_MODE_CBC_ETM );
1883 }
1884#else
1885 (void) encrypt_then_mac;
1886#endif
1887 return( base_mode );
1888}
1889
Neil Armstrongab555e02022-04-04 11:07:59 +02001890mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001891 const mbedtls_ssl_transform *transform )
1892{
Gilles Peskinee108d982022-04-26 16:50:40 +02001893 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001895 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001896#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001897 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1898#endif
1899 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900
Gilles Peskinee108d982022-04-26 16:50:40 +02001901 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001902#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001903 encrypt_then_mac = transform->encrypt_then_mac;
1904#endif
1905 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001906}
1907
Neil Armstrongab555e02022-04-04 11:07:59 +02001908mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001909#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001910 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001911#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001912 const mbedtls_ssl_ciphersuite_t *suite )
1913{
Gilles Peskinee108d982022-04-26 16:50:40 +02001914 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1915
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001916#if defined(MBEDTLS_USE_PSA_CRYPTO)
1917 psa_status_t status;
1918 psa_algorithm_t alg;
1919 psa_key_type_t type;
1920 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001921 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1922 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001923 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001924#else
1925 const mbedtls_cipher_info_t *cipher =
1926 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001927 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001928 {
1929 base_mode =
1930 mbedtls_ssl_get_base_mode(
1931 mbedtls_cipher_info_get_mode( cipher ) );
1932 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001933#endif /* MBEDTLS_USE_PSA_CRYPTO */
1934
Gilles Peskinee108d982022-04-26 16:50:40 +02001935#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1936 int encrypt_then_mac = 0;
1937#endif
1938 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001939}
1940
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001941#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001942
1943#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001944/* Serialization of TLS 1.3 sessions:
1945 *
1946 * struct {
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00001947 * opaque hostname<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001948 * uint64 ticket_received;
1949 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001950 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001951 * } ClientOnlyData;
1952 *
1953 * struct {
1954 * uint8 endpoint;
1955 * uint8 ciphersuite[2];
1956 * uint32 ticket_age_add;
1957 * uint8 ticket_flags;
1958 * opaque resumption_key<0..255>;
1959 * select ( endpoint ) {
1960 * case client: ClientOnlyData;
1961 * case server: uint64 start_time;
1962 * };
1963 * } serialized_session_tls13;
1964 *
1965 */
1966#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001967MBEDTLS_CHECK_RETURN_CRITICAL
1968static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1969 unsigned char *buf,
1970 size_t buf_len,
1971 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001972{
1973 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001974#if defined(MBEDTLS_SSL_CLI_C) && \
1975 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1976 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001977 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001978#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001979 size_t needed = 1 /* endpoint */
1980 + 2 /* ciphersuite */
1981 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001982 + 1 /* ticket_flags */
1983 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001984 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001985
1986 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1988 needed += session->resumption_key_len; /* resumption_key */
1989
Jerry Yu438ddd82022-07-07 06:55:50 +00001990#if defined(MBEDTLS_HAVE_TIME)
1991 needed += 8; /* start_time or ticket_received */
1992#endif
1993
1994#if defined(MBEDTLS_SSL_CLI_C)
1995 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1996 {
Xiaokang Qian03409292022-10-12 02:49:52 +00001997#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
1998 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 Qianed0620c2022-10-12 06:58:13 +00002029
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002030
Jerry Yu438ddd82022-07-07 06:55:50 +00002031#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2032 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2033 {
2034 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2035 p += 8;
2036 }
2037#endif /* MBEDTLS_HAVE_TIME */
2038
2039#if defined(MBEDTLS_SSL_CLI_C)
2040 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2041 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002042#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2043 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2044 p += 2;
2045 if ( hostname_len > 0 )
2046 {
2047 /* save host name */
2048 memcpy( p, session->hostname, hostname_len );
2049 p += hostname_len;
2050 }
2051#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2052
Jerry Yu438ddd82022-07-07 06:55:50 +00002053#if defined(MBEDTLS_HAVE_TIME)
2054 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2055 p += 8;
2056#endif
2057 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2058 p += 4;
2059
2060 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2061 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002062
2063 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002064 {
2065 memcpy( p, session->ticket, session->ticket_len );
2066 p += session->ticket_len;
2067 }
2068 }
2069#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002070 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002071}
2072
2073MBEDTLS_CHECK_RETURN_CRITICAL
2074static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2075 const unsigned char *buf,
2076 size_t len )
2077{
2078 const unsigned char *p = buf;
2079 const unsigned char *end = buf + len;
2080
2081 if( end - p < 9 )
2082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2083 session->endpoint = p[0];
2084 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2085 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2086 session->ticket_flags = p[7];
2087
2088 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002089 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002090 p += 9;
2091
Jerry Yubc7c1a42022-07-21 22:57:37 +08002092 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2094
Jerry Yubc7c1a42022-07-21 22:57:37 +08002095 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002097 memcpy( session->resumption_key, p, session->resumption_key_len );
2098 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002099
2100#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2101 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2102 {
2103 if( end - p < 8 )
2104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2105 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2106 p += 8;
2107 }
2108#endif /* MBEDTLS_HAVE_TIME */
2109
2110#if defined(MBEDTLS_SSL_CLI_C)
2111 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2112 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002113#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2114 defined(MBEDTLS_SSL_SESSION_TICKETS)
2115 size_t hostname_len;
2116 /* load host name */
2117 if( end - p < 2 )
2118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2119 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2120 p += 2;
2121
2122 if( end - p < ( long int )hostname_len )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2124 if( hostname_len > 0 )
2125 {
2126 session->hostname = mbedtls_calloc( 1, hostname_len );
2127 if( session->hostname == NULL )
2128 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2129 memcpy( session->hostname, p, hostname_len );
2130 p += hostname_len;
2131 }
2132#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2133 MBEDTLS_SSL_SESSION_TICKETS */
2134
Jerry Yu438ddd82022-07-07 06:55:50 +00002135#if defined(MBEDTLS_HAVE_TIME)
2136 if( end - p < 8 )
2137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2138 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2139 p += 8;
2140#endif
2141 if( end - p < 4 )
2142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2143 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2144 p += 4;
2145
2146 if( end - p < 2 )
2147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2148 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2149 p += 2;
2150
2151 if( end - p < ( long int )session->ticket_len )
2152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2153 if( session->ticket_len > 0 )
2154 {
2155 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2156 if( session->ticket == NULL )
2157 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2158 memcpy( session->ticket, p, session->ticket_len );
2159 p += session->ticket_len;
2160 }
2161 }
2162#endif /* MBEDTLS_SSL_CLI_C */
2163
2164 return( 0 );
2165
2166}
2167#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002168MBEDTLS_CHECK_RETURN_CRITICAL
2169static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2170 unsigned char *buf,
2171 size_t buf_len,
2172 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002173{
2174 ((void) session);
2175 ((void) buf);
2176 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002177 *olen = 0;
2178 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002179}
Jerry Yu438ddd82022-07-07 06:55:50 +00002180
2181static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2182 unsigned char *buf,
2183 size_t buf_len )
2184{
2185 ((void) session);
2186 ((void) buf);
2187 ((void) buf_len);
2188 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2189}
2190#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002191#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2192
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002193psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002194 size_t taglen,
2195 psa_algorithm_t *alg,
2196 psa_key_type_t *key_type,
2197 size_t *key_size )
2198{
2199 switch ( mbedtls_cipher_type )
2200 {
2201 case MBEDTLS_CIPHER_AES_128_CBC:
2202 *alg = PSA_ALG_CBC_NO_PADDING;
2203 *key_type = PSA_KEY_TYPE_AES;
2204 *key_size = 128;
2205 break;
2206 case MBEDTLS_CIPHER_AES_128_CCM:
2207 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2208 *key_type = PSA_KEY_TYPE_AES;
2209 *key_size = 128;
2210 break;
2211 case MBEDTLS_CIPHER_AES_128_GCM:
2212 *alg = PSA_ALG_GCM;
2213 *key_type = PSA_KEY_TYPE_AES;
2214 *key_size = 128;
2215 break;
2216 case MBEDTLS_CIPHER_AES_192_CCM:
2217 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2218 *key_type = PSA_KEY_TYPE_AES;
2219 *key_size = 192;
2220 break;
2221 case MBEDTLS_CIPHER_AES_192_GCM:
2222 *alg = PSA_ALG_GCM;
2223 *key_type = PSA_KEY_TYPE_AES;
2224 *key_size = 192;
2225 break;
2226 case MBEDTLS_CIPHER_AES_256_CBC:
2227 *alg = PSA_ALG_CBC_NO_PADDING;
2228 *key_type = PSA_KEY_TYPE_AES;
2229 *key_size = 256;
2230 break;
2231 case MBEDTLS_CIPHER_AES_256_CCM:
2232 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2233 *key_type = PSA_KEY_TYPE_AES;
2234 *key_size = 256;
2235 break;
2236 case MBEDTLS_CIPHER_AES_256_GCM:
2237 *alg = PSA_ALG_GCM;
2238 *key_type = PSA_KEY_TYPE_AES;
2239 *key_size = 256;
2240 break;
2241 case MBEDTLS_CIPHER_ARIA_128_CBC:
2242 *alg = PSA_ALG_CBC_NO_PADDING;
2243 *key_type = PSA_KEY_TYPE_ARIA;
2244 *key_size = 128;
2245 break;
2246 case MBEDTLS_CIPHER_ARIA_128_CCM:
2247 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2248 *key_type = PSA_KEY_TYPE_ARIA;
2249 *key_size = 128;
2250 break;
2251 case MBEDTLS_CIPHER_ARIA_128_GCM:
2252 *alg = PSA_ALG_GCM;
2253 *key_type = PSA_KEY_TYPE_ARIA;
2254 *key_size = 128;
2255 break;
2256 case MBEDTLS_CIPHER_ARIA_192_CCM:
2257 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2258 *key_type = PSA_KEY_TYPE_ARIA;
2259 *key_size = 192;
2260 break;
2261 case MBEDTLS_CIPHER_ARIA_192_GCM:
2262 *alg = PSA_ALG_GCM;
2263 *key_type = PSA_KEY_TYPE_ARIA;
2264 *key_size = 192;
2265 break;
2266 case MBEDTLS_CIPHER_ARIA_256_CBC:
2267 *alg = PSA_ALG_CBC_NO_PADDING;
2268 *key_type = PSA_KEY_TYPE_ARIA;
2269 *key_size = 256;
2270 break;
2271 case MBEDTLS_CIPHER_ARIA_256_CCM:
2272 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2273 *key_type = PSA_KEY_TYPE_ARIA;
2274 *key_size = 256;
2275 break;
2276 case MBEDTLS_CIPHER_ARIA_256_GCM:
2277 *alg = PSA_ALG_GCM;
2278 *key_type = PSA_KEY_TYPE_ARIA;
2279 *key_size = 256;
2280 break;
2281 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2282 *alg = PSA_ALG_CBC_NO_PADDING;
2283 *key_type = PSA_KEY_TYPE_CAMELLIA;
2284 *key_size = 128;
2285 break;
2286 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2287 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2288 *key_type = PSA_KEY_TYPE_CAMELLIA;
2289 *key_size = 128;
2290 break;
2291 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2292 *alg = PSA_ALG_GCM;
2293 *key_type = PSA_KEY_TYPE_CAMELLIA;
2294 *key_size = 128;
2295 break;
2296 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2297 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2298 *key_type = PSA_KEY_TYPE_CAMELLIA;
2299 *key_size = 192;
2300 break;
2301 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2302 *alg = PSA_ALG_GCM;
2303 *key_type = PSA_KEY_TYPE_CAMELLIA;
2304 *key_size = 192;
2305 break;
2306 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2307 *alg = PSA_ALG_CBC_NO_PADDING;
2308 *key_type = PSA_KEY_TYPE_CAMELLIA;
2309 *key_size = 256;
2310 break;
2311 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2312 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2313 *key_type = PSA_KEY_TYPE_CAMELLIA;
2314 *key_size = 256;
2315 break;
2316 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2317 *alg = PSA_ALG_GCM;
2318 *key_type = PSA_KEY_TYPE_CAMELLIA;
2319 *key_size = 256;
2320 break;
2321 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2322 *alg = PSA_ALG_CHACHA20_POLY1305;
2323 *key_type = PSA_KEY_TYPE_CHACHA20;
2324 *key_size = 256;
2325 break;
2326 case MBEDTLS_CIPHER_NULL:
2327 *alg = MBEDTLS_SSL_NULL_CIPHER;
2328 *key_type = 0;
2329 *key_size = 0;
2330 break;
2331 default:
2332 return PSA_ERROR_NOT_SUPPORTED;
2333 }
2334
2335 return PSA_SUCCESS;
2336}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002337#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002338
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002339#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002340int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2341 const unsigned char *dhm_P, size_t P_len,
2342 const unsigned char *dhm_G, size_t G_len )
2343{
Janos Follath865b3eb2019-12-16 11:46:15 +00002344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002345
Glenn Strausscee11292021-12-20 01:43:17 -05002346 mbedtls_mpi_free( &conf->dhm_P );
2347 mbedtls_mpi_free( &conf->dhm_G );
2348
Hanno Beckera90658f2017-10-04 15:29:08 +01002349 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2350 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2351 {
2352 mbedtls_mpi_free( &conf->dhm_P );
2353 mbedtls_mpi_free( &conf->dhm_G );
2354 return( ret );
2355 }
2356
2357 return( 0 );
2358}
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002360int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002361{
Janos Follath865b3eb2019-12-16 11:46:15 +00002362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002363
Glenn Strausscee11292021-12-20 01:43:17 -05002364 mbedtls_mpi_free( &conf->dhm_P );
2365 mbedtls_mpi_free( &conf->dhm_G );
2366
Gilles Peskinee5702482021-06-11 21:59:08 +02002367 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2368 &conf->dhm_P ) ) != 0 ||
2369 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2370 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002371 {
2372 mbedtls_mpi_free( &conf->dhm_P );
2373 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002374 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002375 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002376
2377 return( 0 );
2378}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002379#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002380
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002381#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2382/*
2383 * Set the minimum length for Diffie-Hellman parameters
2384 */
2385void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2386 unsigned int bitlen )
2387{
2388 conf->dhm_min_bitlen = bitlen;
2389}
2390#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2391
Gilles Peskineeccd8882020-03-10 12:19:08 +01002392#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002393#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002394/*
2395 * Set allowed/preferred hashes for handshake signatures
2396 */
2397void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2398 const int *hashes )
2399{
2400 conf->sig_hashes = hashes;
2401}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002402#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002403
Jerry Yuf017ee42022-01-12 15:49:48 +08002404/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002405void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2406 const uint16_t* sig_algs )
2407{
Jerry Yuf017ee42022-01-12 15:49:48 +08002408#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2409 conf->sig_hashes = NULL;
2410#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2411 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002412}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002413#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002414
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002415#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002416#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002417/*
2418 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002419 *
2420 * mbedtls_ssl_setup() takes the provided list
2421 * and translates it to a list of IANA TLS group identifiers,
2422 * stored in ssl->handshake->group_list.
2423 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002424 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002425void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002426 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002428 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002429 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002430}
Brett Warrene0edc842021-08-17 09:53:13 +01002431#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002432#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002433
Brett Warrene0edc842021-08-17 09:53:13 +01002434/*
2435 * Set the allowed groups
2436 */
2437void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2438 const uint16_t *group_list )
2439{
2440#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2441 conf->curve_list = NULL;
2442#endif
2443 conf->group_list = group_list;
2444}
2445
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448{
Hanno Becker947194e2017-04-07 13:25:49 +01002449 /* Initialize to suppress unnecessary compiler warning */
2450 size_t hostname_len = 0;
2451
2452 /* Check if new hostname is valid before
2453 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002454 if( hostname != NULL )
2455 {
2456 hostname_len = strlen( hostname );
2457
2458 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2460 }
2461
2462 /* Now it's clear that we will overwrite the old hostname,
2463 * so we can free it safely */
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002464
Hanno Becker947194e2017-04-07 13:25:49 +01002465 if( ssl->hostname != NULL )
2466 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002467 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002468 mbedtls_free( ssl->hostname );
2469 }
2470
2471 /* Passing NULL as hostname shall clear the old one */
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002474 {
2475 ssl->hostname = NULL;
2476 }
2477 else
2478 {
2479 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002480 if( ssl->hostname == NULL )
2481 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002482
Hanno Becker947194e2017-04-07 13:25:49 +01002483 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002484
Hanno Becker947194e2017-04-07 13:25:49 +01002485 ssl->hostname[hostname_len] = '\0';
2486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 return( 0 );
2489}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002490#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002492#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002493void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002495 const unsigned char *, size_t),
2496 void *p_sni )
2497{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002498 conf->f_sni = f_sni;
2499 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002504int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002505{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002506 size_t cur_len, tot_len;
2507 const char **p;
2508
2509 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002510 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2511 * MUST NOT be truncated."
2512 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002513 */
2514 tot_len = 0;
2515 for( p = protos; *p != NULL; p++ )
2516 {
2517 cur_len = strlen( *p );
2518 tot_len += cur_len;
2519
Ronald Cron8216dd32020-04-23 16:41:44 +02002520 if( ( cur_len == 0 ) ||
2521 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2522 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002524 }
2525
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002526 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002527
2528 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002529}
2530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002532{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002533 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002536
Johan Pascalb62bb512015-12-03 21:56:45 +01002537#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002538void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2539 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002540{
2541 conf->dtls_srtp_mki_support = support_mki_value;
2542}
2543
Ron Eldoref72faf2018-07-12 11:54:20 +03002544int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2545 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002546 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002547{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002548 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002549 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002551 }
Ron Eldor591f1622018-01-22 12:30:04 +02002552
2553 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002554 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002555 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002556 }
Ron Eldor591f1622018-01-22 12:30:04 +02002557
2558 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2559 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002560 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002561}
2562
Ron Eldoref72faf2018-07-12 11:54:20 +03002563int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002564 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002565{
Johan Pascal253d0262020-09-22 13:04:45 +02002566 const mbedtls_ssl_srtp_profile *p;
2567 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002568
Johan Pascal253d0262020-09-22 13:04:45 +02002569 /* check the profiles list: all entry must be valid,
2570 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002571 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2572 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2573 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002574 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002575 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002576 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002577 list_size++;
2578 }
2579 else
2580 {
2581 /* unsupported value, stop parsing and set the size to an error value */
2582 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002583 }
2584 }
2585
Johan Pascal5ef72d22020-10-28 17:05:47 +01002586 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002587 {
Johan Pascal253d0262020-09-22 13:04:45 +02002588 conf->dtls_srtp_profile_list = NULL;
2589 conf->dtls_srtp_profile_list_len = 0;
2590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2591 }
2592
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002593 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002594 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002595
2596 return( 0 );
2597}
2598
Johan Pascal5ef72d22020-10-28 17:05:47 +01002599void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2600 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002601{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002602 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2603 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002604 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002605 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002606 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002607 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002608 else
2609 {
2610 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002611 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2612 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002613 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002614}
Johan Pascalb62bb512015-12-03 21:56:45 +01002615#endif /* MBEDTLS_SSL_DTLS_SRTP */
2616
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302617#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002618void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002619{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002620 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002621}
2622
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002623void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002624{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002625 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002626}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302627#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002628
Janos Follath088ce432017-04-10 12:42:31 +01002629#if defined(MBEDTLS_SSL_SRV_C)
2630void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2631 char cert_req_ca_list )
2632{
2633 conf->cert_req_ca_list = cert_req_ca_list;
2634}
2635#endif
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002638void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002640 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002641}
2642#endif
2643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002645void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002646{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002647 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002648}
2649#endif
2650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002652int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002655 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002658 }
2659
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002660 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002661
2662 return( 0 );
2663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002665
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002666void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002667{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002668 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002669}
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002672void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002674 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002675}
2676
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002677void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002678{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002679 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002680}
2681
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002682void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002683 const unsigned char period[8] )
2684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002685 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002690#if defined(MBEDTLS_SSL_CLI_C)
2691void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002692{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002693 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002694}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002695#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002696
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002697#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002698
Jerry Yud0766ec2022-09-22 10:46:57 +08002699#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002700void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2701 uint16_t num_tickets )
2702{
Jerry Yud0766ec2022-09-22 10:46:57 +08002703 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002704}
2705#endif
2706
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002707void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2708 mbedtls_ssl_ticket_write_t *f_ticket_write,
2709 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2710 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002711{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002712 conf->f_ticket_write = f_ticket_write;
2713 conf->f_ticket_parse = f_ticket_parse;
2714 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002715}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002718
Hanno Becker7e6c1782021-06-08 09:24:55 +01002719void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2720 mbedtls_ssl_export_keys_t *f_export_keys,
2721 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002722{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002723 ssl->f_export_keys = f_export_keys;
2724 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002725}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002726
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002727#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002728void mbedtls_ssl_conf_async_private_cb(
2729 mbedtls_ssl_config *conf,
2730 mbedtls_ssl_async_sign_t *f_async_sign,
2731 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2732 mbedtls_ssl_async_resume_t *f_async_resume,
2733 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002734 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002735{
2736 conf->f_async_sign_start = f_async_sign;
2737 conf->f_async_decrypt_start = f_async_decrypt;
2738 conf->f_async_resume = f_async_resume;
2739 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002740 conf->p_async_config_data = async_config_data;
2741}
2742
Gilles Peskine8f97af72018-04-26 11:46:10 +02002743void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2744{
2745 return( conf->p_async_config_data );
2746}
2747
Gilles Peskine1febfef2018-04-30 11:54:39 +02002748void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002749{
2750 if( ssl->handshake == NULL )
2751 return( NULL );
2752 else
2753 return( ssl->handshake->user_async_ctx );
2754}
2755
Gilles Peskine1febfef2018-04-30 11:54:39 +02002756void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002757 void *ctx )
2758{
2759 if( ssl->handshake != NULL )
2760 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002761}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002762#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002763
Paul Bakker5121ce52009-01-03 21:22:43 +00002764/*
2765 * SSL get accessors
2766 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002767uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002769 if( ssl->session != NULL )
2770 return( ssl->session->verify_result );
2771
2772 if( ssl->session_negotiate != NULL )
2773 return( ssl->session_negotiate->verify_result );
2774
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002775 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776}
2777
Glenn Strauss8f526902022-01-13 00:04:49 -05002778int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2779{
2780 if( ssl == NULL || ssl->session == NULL )
2781 return( 0 );
2782
2783 return( ssl->session->ciphersuite );
2784}
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002787{
Paul Bakker926c8e42013-03-06 10:23:34 +01002788 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002789 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002792}
2793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002795{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002798 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002799 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002800 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002801 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002802 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002803 default:
2804 return( "unknown (DTLS)" );
2805 }
2806 }
2807#endif
2808
Glenn Strauss60bfe602022-03-14 19:04:24 -04002809 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002810 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002811 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002812 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002813 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002814 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002815 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002816 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002817 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002818}
2819
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002820#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002821size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2822{
David Horstmann95d516f2021-05-04 18:36:56 +01002823 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002824 size_t read_mfl;
2825
2826 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2828 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2829 {
2830 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2831 }
2832
2833 /* Check if a smaller max length was negotiated */
2834 if( ssl->session_out != NULL )
2835 {
2836 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2837 if( read_mfl < max_len )
2838 {
2839 max_len = read_mfl;
2840 }
2841 }
2842
2843 // During a handshake, use the value being negotiated
2844 if( ssl->session_negotiate != NULL )
2845 {
2846 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2847 if( read_mfl < max_len )
2848 {
2849 max_len = read_mfl;
2850 }
2851 }
2852
2853 return( max_len );
2854}
2855
2856size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002857{
2858 size_t max_len;
2859
2860 /*
2861 * Assume mfl_code is correct since it was checked when set
2862 */
Angus Grattond8213d02016-05-25 20:56:48 +10002863 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002864
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002865 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002866 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002867 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002868 {
Angus Grattond8213d02016-05-25 20:56:48 +10002869 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002870 }
2871
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002872 /* During a handshake, use the value being negotiated */
2873 if( ssl->session_negotiate != NULL &&
2874 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2875 {
2876 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2877 }
2878
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002879 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002880}
2881#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2882
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002884size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002885{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002886 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2888 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2889 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2890 return ( 0 );
2891
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002892 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2893 return( ssl->mtu );
2894
2895 if( ssl->mtu == 0 )
2896 return( ssl->handshake->mtu );
2897
2898 return( ssl->mtu < ssl->handshake->mtu ?
2899 ssl->mtu : ssl->handshake->mtu );
2900}
2901#endif /* MBEDTLS_SSL_PROTO_DTLS */
2902
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002903int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2904{
2905 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2906
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002907#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2908 !defined(MBEDTLS_SSL_PROTO_DTLS)
2909 (void) ssl;
2910#endif
2911
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002912#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002913 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002914
2915 if( max_len > mfl )
2916 max_len = mfl;
2917#endif
2918
2919#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002920 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002921 {
Hanno Becker89490712020-02-05 10:50:12 +00002922 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002923 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2924 const size_t overhead = (size_t) ret;
2925
2926 if( ret < 0 )
2927 return( ret );
2928
2929 if( mtu <= overhead )
2930 {
2931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2932 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2933 }
2934
2935 if( max_len > mtu - overhead )
2936 max_len = mtu - overhead;
2937 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002938#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002939
Hanno Becker0defedb2018-08-10 12:35:02 +01002940#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2941 !defined(MBEDTLS_SSL_PROTO_DTLS)
2942 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002943#endif
2944
2945 return( (int) max_len );
2946}
2947
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002948int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2949{
2950 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2951
2952#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2953 (void) ssl;
2954#endif
2955
2956#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2957 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2958
2959 if( max_len > mfl )
2960 max_len = mfl;
2961#endif
2962
2963 return( (int) max_len );
2964}
2965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#if defined(MBEDTLS_X509_CRT_PARSE_C)
2967const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002968{
2969 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002970 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002971
Hanno Beckere6824572019-02-07 13:18:46 +00002972#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002973 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002974#else
2975 return( NULL );
2976#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002981int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2982 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002983{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002984 int ret;
2985
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002986 if( ssl == NULL ||
2987 dst == NULL ||
2988 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002989 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002992 }
2993
Hanno Beckere810bbc2021-05-14 16:01:05 +01002994 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2995 * idempotent: Each session can only be exported once.
2996 *
2997 * (This is in preparation for TLS 1.3 support where we will
2998 * need the ability to export multiple sessions (aka tickets),
2999 * which will be achieved by calling mbedtls_ssl_get_session()
3000 * multiple times until it fails.)
3001 *
3002 * Check whether we have already exported the current session,
3003 * and fail if so.
3004 */
3005 if( ssl->session->exported == 1 )
3006 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3007
3008 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3009 if( ret != 0 )
3010 return( ret );
3011
3012 /* Remember that we've exported the session. */
3013 ssl->session->exported = 1;
3014 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003017
Paul Bakker5121ce52009-01-03 21:22:43 +00003018/*
Hanno Beckera835da52019-05-16 12:39:07 +01003019 * Define ticket header determining Mbed TLS version
3020 * and structure of the ticket.
3021 */
3022
Hanno Becker94ef3b32019-05-16 12:50:45 +01003023/*
Hanno Becker50b59662019-05-28 14:30:45 +01003024 * Define bitflag determining compile-time settings influencing
3025 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003026 */
3027
Hanno Becker50b59662019-05-28 14:30:45 +01003028#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003029#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003030#else
Hanno Becker3e088662019-05-29 11:10:18 +01003031#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003032#endif /* MBEDTLS_HAVE_TIME */
3033
3034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003035#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003036#else
Hanno Becker3e088662019-05-29 11:10:18 +01003037#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003038#endif /* MBEDTLS_X509_CRT_PARSE_C */
3039
3040#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003041#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003042#else
Hanno Becker3e088662019-05-29 11:10:18 +01003043#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003044#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3045
3046#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003047#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048#else
Hanno Becker3e088662019-05-29 11:10:18 +01003049#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003050#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3051
Hanno Becker94ef3b32019-05-16 12:50:45 +01003052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003053#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003054#else
Hanno Becker3e088662019-05-29 11:10:18 +01003055#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003056#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3057
Hanno Becker94ef3b32019-05-16 12:50:45 +01003058#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3059#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3060#else
3061#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3062#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3063
Hanno Becker3e088662019-05-29 11:10:18 +01003064#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3065#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3066#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3067#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003068#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3069#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003070
Hanno Becker50b59662019-05-28 14:30:45 +01003071#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003072 ( (uint16_t) ( \
3073 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3074 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3075 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3076 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003077 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003078 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003079
Hanno Beckerf8787072019-05-16 12:41:07 +01003080static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003081 MBEDTLS_VERSION_MAJOR,
3082 MBEDTLS_VERSION_MINOR,
3083 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003084 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3085 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003086};
Hanno Beckera835da52019-05-16 12:39:07 +01003087
3088/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003089 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003090 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003091 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003092 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003093 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003094 * opaque mbedtls_version[3]; // library version: major, minor, patch
3095 * opaque session_format[2]; // library-version specific 16-bit field
3096 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003097 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003098 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003099 * Note: When updating the format, remember to keep
3100 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003101 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003102 * // In this version, `session_format` determines
3103 * // the setting of those compile-time
3104 * // configuration options which influence
3105 * // the structure of mbedtls_ssl_session.
3106 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003107 * uint8_t minor_ver; // Protocol minor version. Possible values:
3108 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003109 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003110 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003111 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003112 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003113 * serialized_session_tls12 data;
3114 *
3115 * };
3116 *
3117 * } serialized_session;
3118 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003119 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003120
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003121MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003122static int ssl_session_save( const mbedtls_ssl_session *session,
3123 unsigned char omit_header,
3124 unsigned char *buf,
3125 size_t buf_len,
3126 size_t *olen )
3127{
3128 unsigned char *p = buf;
3129 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003130 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003131#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3132 size_t out_len;
3133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3134#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003135 if( session == NULL )
3136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3137
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003138 if( !omit_header )
3139 {
3140 /*
3141 * Add Mbed TLS version identifier
3142 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003143 used += sizeof( ssl_serialized_session_header );
3144
3145 if( used <= buf_len )
3146 {
3147 memcpy( p, ssl_serialized_session_header,
3148 sizeof( ssl_serialized_session_header ) );
3149 p += sizeof( ssl_serialized_session_header );
3150 }
3151 }
3152
3153 /*
3154 * TLS version identifier
3155 */
3156 used += 1;
3157 if( used <= buf_len )
3158 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003159 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003160 }
3161
3162 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003163 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003164 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003165 {
3166#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003167 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003168 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003169 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3171
Jerry Yu251a12e2022-07-13 15:15:48 +08003172#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3173 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003174 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3175 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003176 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003177 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003178 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003179#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3180
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003181 default:
3182 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3183 }
3184
3185 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003186 if( used > buf_len )
3187 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003188
3189 return( 0 );
3190}
3191
3192/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003193 * Public wrapper for ssl_session_save()
3194 */
3195int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3196 unsigned char *buf,
3197 size_t buf_len,
3198 size_t *olen )
3199{
3200 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3201}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003202
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003203/*
3204 * Deserialize session, see mbedtls_ssl_session_save() for format.
3205 *
3206 * This internal version is wrapped by a public function that cleans up in
3207 * case of error, and has an extra option omit_header.
3208 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003209MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003210static int ssl_session_load( mbedtls_ssl_session *session,
3211 unsigned char omit_header,
3212 const unsigned char *buf,
3213 size_t len )
3214{
3215 const unsigned char *p = buf;
3216 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003217 size_t remaining_len;
3218
3219
3220 if( session == NULL )
3221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003222
3223 if( !omit_header )
3224 {
3225 /*
3226 * Check Mbed TLS version identifier
3227 */
3228
3229 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3231
3232 if( memcmp( p, ssl_serialized_session_header,
3233 sizeof( ssl_serialized_session_header ) ) != 0 )
3234 {
3235 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3236 }
3237 p += sizeof( ssl_serialized_session_header );
3238 }
3239
3240 /*
3241 * TLS version identifier
3242 */
3243 if( 1 > (size_t)( end - p ) )
3244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003245 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003246
3247 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003248 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003249 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003250 {
3251#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003252 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003253 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003254#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3255
Jerry Yu438ddd82022-07-07 06:55:50 +00003256#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3257 case MBEDTLS_SSL_VERSION_TLS1_3:
3258 return( ssl_tls13_session_load( session, p, remaining_len ) );
3259#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3260
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003261 default:
3262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3263 }
3264}
3265
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003266/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003267 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003268 */
3269int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3270 const unsigned char *buf,
3271 size_t len )
3272{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003273 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003274
3275 if( ret != 0 )
3276 mbedtls_ssl_session_free( session );
3277
3278 return( ret );
3279}
3280
3281/*
Paul Bakker1961b702013-01-25 14:49:24 +01003282 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003284MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003285static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3286{
3287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3288
Ronald Cron66dbf912022-02-02 15:33:46 +01003289 /*
3290 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003291 * that were written into the output buffer by the previous handshake step,
3292 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003293 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3294 * We proceed to the next handshake step only when all data from the
3295 * previous one have been sent to the peer, thus we make sure that this is
3296 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3297 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3298 * we have to wait before to go ahead.
3299 * In the case of TLS 1.3, handshake step handlers do not send data to the
3300 * peer. Data are only sent here and through
3301 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003302 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003303 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003304 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3305 return( ret );
3306
3307#if defined(MBEDTLS_SSL_PROTO_DTLS)
3308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3309 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3310 {
3311 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3312 return( ret );
3313 }
3314#endif /* MBEDTLS_SSL_PROTO_DTLS */
3315
3316 return( ret );
3317}
3318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003320{
Hanno Becker41934dd2021-08-07 19:13:43 +01003321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003322
Hanno Becker41934dd2021-08-07 19:13:43 +01003323 if( ssl == NULL ||
3324 ssl->conf == NULL ||
3325 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003326 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003327 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003329 }
3330
3331 ret = ssl_prepare_handshake_step( ssl );
3332 if( ret != 0 )
3333 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003334
Jerry Yue7047812021-09-13 19:26:39 +08003335 ret = mbedtls_ssl_handle_pending_alert( ssl );
3336 if( ret != 0 )
3337 goto cleanup;
3338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003341 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3343 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003344
Ronald Cron9f0fba32022-02-10 16:45:15 +01003345 switch( ssl->state )
3346 {
3347 case MBEDTLS_SSL_HELLO_REQUEST:
3348 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3349 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003350
Ronald Cron9f0fba32022-02-10 16:45:15 +01003351 case MBEDTLS_SSL_CLIENT_HELLO:
3352 ret = mbedtls_ssl_write_client_hello( ssl );
3353 break;
3354
3355 default:
3356#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003357 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003358 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3359 else
3360 ret = mbedtls_ssl_handshake_client_step( ssl );
3361#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3362 ret = mbedtls_ssl_handshake_client_step( ssl );
3363#else
3364 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3365#endif
3366 }
Jerry Yub9930e72021-08-06 17:11:51 +08003367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003370 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003371 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003372#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003373 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003374 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003375#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003376
3377#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3378 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3379 ret = mbedtls_ssl_handshake_server_step( ssl );
3380#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003382#endif
3383
Jerry Yue7047812021-09-13 19:26:39 +08003384 if( ret != 0 )
3385 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003386 /* handshake_step return error. And it is same
3387 * with alert_reason.
3388 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003389 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003390 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003391 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003392 goto cleanup;
3393 }
3394 }
3395
3396cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003397 return( ret );
3398}
3399
3400/*
3401 * Perform the SSL handshake
3402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003404{
3405 int ret = 0;
3406
Hanno Beckera817ea42020-10-20 15:20:23 +01003407 /* Sanity checks */
3408
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003409 if( ssl == NULL || ssl->conf == NULL )
3410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3411
Hanno Beckera817ea42020-10-20 15:20:23 +01003412#if defined(MBEDTLS_SSL_PROTO_DTLS)
3413 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3414 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3415 {
3416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3417 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3419 }
3420#endif /* MBEDTLS_SSL_PROTO_DTLS */
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003423
Hanno Beckera817ea42020-10-20 15:20:23 +01003424 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003425 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003428
3429 if( ret != 0 )
3430 break;
3431 }
3432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
3435 return( ret );
3436}
3437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#if defined(MBEDTLS_SSL_RENEGOTIATION)
3439#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003440/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003441 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003442 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003443MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003445{
Janos Follath865b3eb2019-12-16 11:46:15 +00003446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003449
3450 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3452 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003453
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003454 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003455 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003457 return( ret );
3458 }
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003461
3462 return( 0 );
3463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003465
3466/*
3467 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 * - any side: calling mbedtls_ssl_renegotiate(),
3469 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3470 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003471 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003472 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003474 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003475int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003476{
Janos Follath865b3eb2019-12-16 11:46:15 +00003477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003480
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003481 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3482 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003483
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003484 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3485 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003489 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003490 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003491 ssl->handshake->out_msg_seq = 1;
3492 else
3493 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003494 }
3495#endif
3496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3498 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003503 return( ret );
3504 }
3505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003507
3508 return( 0 );
3509}
3510
3511/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003512 * Renegotiate current connection on client,
3513 * or request renegotiation on server
3514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003518
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003519 if( ssl == NULL || ssl->conf == NULL )
3520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003523 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003524 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003525 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003526 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003530
3531 /* Did we already try/start sending HelloRequest? */
3532 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003534
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003535 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003540 /*
3541 * On client, either start the renegotiation process or,
3542 * if already in progress, continue the handshake
3543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003545 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003546 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548
Hanno Becker40cdaa12020-02-05 10:48:27 +00003549 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003550 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003552 return( ret );
3553 }
3554 }
3555 else
3556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003560 return( ret );
3561 }
3562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003564
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003565 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003568
Gilles Peskine9b562d52018-04-25 20:32:43 +02003569void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003570{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003571 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3572
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003573 if( handshake == NULL )
3574 return;
3575
Brett Warrene0edc842021-08-17 09:53:13 +01003576#if defined(MBEDTLS_ECP_C)
3577#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3578 if ( ssl->handshake->group_list_heap_allocated )
3579 mbedtls_free( (void*) handshake->group_list );
3580 handshake->group_list = NULL;
3581#endif /* MBEDTLS_DEPRECATED_REMOVED */
3582#endif /* MBEDTLS_ECP_C */
3583
Jerry Yuf017ee42022-01-12 15:49:48 +08003584#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3585#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3586 if ( ssl->handshake->sig_algs_heap_allocated )
3587 mbedtls_free( (void*) handshake->sig_algs );
3588 handshake->sig_algs = NULL;
3589#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003590#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3591 if( ssl->handshake->certificate_request_context )
3592 {
3593 mbedtls_free( (void*) handshake->certificate_request_context );
3594 }
3595#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003596#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3597
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003598#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3599 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3600 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003601 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003602 handshake->async_in_progress = 0;
3603 }
3604#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3605
Andrzej Kurek25f27152022-08-17 16:09:31 -04003606#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003607#if defined(MBEDTLS_USE_PSA_CRYPTO)
3608 psa_hash_abort( &handshake->fin_sha256_psa );
3609#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003610 mbedtls_sha256_free( &handshake->fin_sha256 );
3611#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003612#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003613#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003614#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003615 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003616#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003617 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003618#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003619#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#if defined(MBEDTLS_DHM_C)
3622 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003623#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003624#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003626#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003627#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003628 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003629#if defined(MBEDTLS_SSL_CLI_C)
3630 mbedtls_free( handshake->ecjpake_cache );
3631 handshake->ecjpake_cache = NULL;
3632 handshake->ecjpake_cache_len = 0;
3633#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003634#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003635
Janos Follath4ae5c292016-02-10 11:27:43 +00003636#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3637 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003638 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003640#endif
3641
Gilles Peskineeccd8882020-03-10 12:19:08 +01003642#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003643#if defined(MBEDTLS_USE_PSA_CRYPTO)
3644 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3645 {
3646 /* The maintenance of the external PSK key slot is the
3647 * user's responsibility. */
3648 if( ssl->handshake->psk_opaque_is_internal )
3649 {
3650 psa_destroy_key( ssl->handshake->psk_opaque );
3651 ssl->handshake->psk_opaque_is_internal = 0;
3652 }
3653 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3654 }
Neil Armstronge952a302022-05-03 10:22:14 +02003655#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003656 if( handshake->psk != NULL )
3657 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003658 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003659 mbedtls_free( handshake->psk );
3660 }
Neil Armstronge952a302022-05-03 10:22:14 +02003661#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003662#endif
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3665 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003666 /*
3667 * Free only the linked list wrapper, not the keys themselves
3668 * since the belong to the SNI callback
3669 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003670 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003672
Gilles Peskineeccd8882020-03-10 12:19:08 +01003673#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003674 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003675 if( handshake->ecrs_peer_cert != NULL )
3676 {
3677 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3678 mbedtls_free( handshake->ecrs_peer_cert );
3679 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003680#endif
3681
Hanno Becker75173122019-02-06 16:18:31 +00003682#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3683 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3684 mbedtls_pk_free( &handshake->peer_pubkey );
3685#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3686
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003687#if defined(MBEDTLS_SSL_CLI_C) && \
3688 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3689 mbedtls_free( handshake->cookie );
3690#endif /* MBEDTLS_SSL_CLI_C &&
3691 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003692
3693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003694 mbedtls_ssl_flight_free( handshake->flight );
3695 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003696#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003697
Ronald Cronf12b81d2022-03-15 10:42:41 +01003698#if defined(MBEDTLS_ECDH_C) && \
3699 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003700 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003701 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003702#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3703
Ronald Cron6f135e12021-12-08 16:57:54 +01003704#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003705 mbedtls_ssl_transform_free( handshake->transform_handshake );
3706 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003707 mbedtls_free( handshake->transform_earlydata );
3708 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003709#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003710
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003711
3712#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3713 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3714 * processes datagrams and the fact that a datagram is allowed to have
3715 * several records in it, it is possible that the I/O buffers are not
3716 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003717 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3718 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003719#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003720
Jerry Yuba9c7272021-10-30 11:54:10 +08003721 /* mbedtls_platform_zeroize MUST be last one in this function */
3722 mbedtls_platform_zeroize( handshake,
3723 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003724}
3725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003727{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003728 if( session == NULL )
3729 return;
3730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003732 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003733#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003734
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003735#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003736#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3737 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003738 mbedtls_free( session->hostname );
3739#endif
Xiaokang Qianed0620c2022-10-12 06:58:13 +00003740 mbedtls_free( session->ticket );
3741#endif
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003743 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003744}
3745
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003746#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003747
3748#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3749#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3750#else
3751#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3752#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3753
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003754#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003755
3756#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3757#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3758#else
3759#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3760#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3761
3762#if defined(MBEDTLS_SSL_ALPN)
3763#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3764#else
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3766#endif /* MBEDTLS_SSL_ALPN */
3767
3768#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3769#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3770#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3771#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3772
3773#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3774 ( (uint32_t) ( \
3775 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3776 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3777 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3778 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3779 0u ) )
3780
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003781static unsigned char ssl_serialized_context_header[] = {
3782 MBEDTLS_VERSION_MAJOR,
3783 MBEDTLS_VERSION_MINOR,
3784 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003785 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3786 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3787 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3788 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3789 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003790};
3791
Paul Bakker5121ce52009-01-03 21:22:43 +00003792/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003793 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003794 *
3795 * The format of the serialized data is:
3796 * (in the presentation language of TLS, RFC 8446 section 3)
3797 *
3798 * // header
3799 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003800 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003801 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003802 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003803 * Note: When updating the format, remember to keep these
3804 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003805 *
3806 * // session sub-structure
3807 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3808 * // transform sub-structure
3809 * uint8 random[64]; // ServerHello.random+ClientHello.random
3810 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3811 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3812 * // fields from ssl_context
3813 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3814 * uint64 in_window_top; // DTLS: last validated record seq_num
3815 * uint64 in_window; // DTLS: bitmask for replay protection
3816 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3817 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3818 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3819 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3820 *
3821 * Note that many fields of the ssl_context or sub-structures are not
3822 * serialized, as they fall in one of the following categories:
3823 *
3824 * 1. forced value (eg in_left must be 0)
3825 * 2. pointer to dynamically-allocated memory (eg session, transform)
3826 * 3. value can be re-derived from other data (eg session keys from MS)
3827 * 4. value was temporary (eg content of input buffer)
3828 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003829 */
3830int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3831 unsigned char *buf,
3832 size_t buf_len,
3833 size_t *olen )
3834{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003835 unsigned char *p = buf;
3836 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003837 size_t session_len;
3838 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003839
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003840 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003841 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3842 * this function's documentation.
3843 *
3844 * These are due to assumptions/limitations in the implementation. Some of
3845 * them are likely to stay (no handshake in progress) some might go away
3846 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003847 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003848 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003849 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003850 {
3851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003853 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003854 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003855 {
3856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003857 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003858 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003859 /* Double-check that sub-structures are indeed ready */
3860 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003861 {
3862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003864 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003865 /* There must be no pending incoming or outgoing data */
3866 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003867 {
3868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003870 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003871 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003872 {
3873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003875 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003876 /* Protocol must be DLTS, not TLS */
3877 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003878 {
3879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003881 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003883 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003884 {
3885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003887 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 /* We must be using an AEAD ciphersuite */
3889 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003890 {
3891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003893 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003894 /* Renegotiation must not be enabled */
3895#if defined(MBEDTLS_SSL_RENEGOTIATION)
3896 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003897 {
3898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003900 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003901#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003902
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003903 /*
3904 * Version and format identifier
3905 */
3906 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003907
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003908 if( used <= buf_len )
3909 {
3910 memcpy( p, ssl_serialized_context_header,
3911 sizeof( ssl_serialized_context_header ) );
3912 p += sizeof( ssl_serialized_context_header );
3913 }
3914
3915 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003916 * Session (length + data)
3917 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003918 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003919 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3920 return( ret );
3921
3922 used += 4 + session_len;
3923 if( used <= buf_len )
3924 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003925 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3926 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003927
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003928 ret = ssl_session_save( ssl->session, 1,
3929 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003930 if( ret != 0 )
3931 return( ret );
3932
3933 p += session_len;
3934 }
3935
3936 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003937 * Transform
3938 */
3939 used += sizeof( ssl->transform->randbytes );
3940 if( used <= buf_len )
3941 {
3942 memcpy( p, ssl->transform->randbytes,
3943 sizeof( ssl->transform->randbytes ) );
3944 p += sizeof( ssl->transform->randbytes );
3945 }
3946
3947#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3948 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3949 if( used <= buf_len )
3950 {
3951 *p++ = ssl->transform->in_cid_len;
3952 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3953 p += ssl->transform->in_cid_len;
3954
3955 *p++ = ssl->transform->out_cid_len;
3956 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3957 p += ssl->transform->out_cid_len;
3958 }
3959#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3960
3961 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003962 * Saved fields from top-level ssl_context structure
3963 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003964 used += 4;
3965 if( used <= buf_len )
3966 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003967 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3968 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003969 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003970
3971#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3972 used += 16;
3973 if( used <= buf_len )
3974 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003975 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3976 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003977
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003978 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3979 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003980 }
3981#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3982
3983#if defined(MBEDTLS_SSL_PROTO_DTLS)
3984 used += 1;
3985 if( used <= buf_len )
3986 {
3987 *p++ = ssl->disable_datagram_packing;
3988 }
3989#endif /* MBEDTLS_SSL_PROTO_DTLS */
3990
Jerry Yuae0b2e22021-10-08 15:21:19 +08003991 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003992 if( used <= buf_len )
3993 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003994 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3995 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003996 }
3997
3998#if defined(MBEDTLS_SSL_PROTO_DTLS)
3999 used += 2;
4000 if( used <= buf_len )
4001 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004002 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4003 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004004 }
4005#endif /* MBEDTLS_SSL_PROTO_DTLS */
4006
4007#if defined(MBEDTLS_SSL_ALPN)
4008 {
4009 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004010 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004011 : 0;
4012
4013 used += 1 + alpn_len;
4014 if( used <= buf_len )
4015 {
4016 *p++ = alpn_len;
4017
4018 if( ssl->alpn_chosen != NULL )
4019 {
4020 memcpy( p, ssl->alpn_chosen, alpn_len );
4021 p += alpn_len;
4022 }
4023 }
4024 }
4025#endif /* MBEDTLS_SSL_ALPN */
4026
4027 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004028 * Done
4029 */
4030 *olen = used;
4031
4032 if( used > buf_len )
4033 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004034
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004035 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4036
Hanno Becker43aefe22020-02-05 10:44:56 +00004037 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004038}
4039
4040/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004041 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004042 *
4043 * This internal version is wrapped by a public function that cleans up in
4044 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004045 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004046MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004047static int ssl_context_load( mbedtls_ssl_context *ssl,
4048 const unsigned char *buf,
4049 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004050{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004051 const unsigned char *p = buf;
4052 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004053 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004055
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004056 /*
4057 * The context should have been freshly setup or reset.
4058 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004059 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004060 * renegotiating, or if the user mistakenly loaded a session first.)
4061 */
4062 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4063 ssl->session != NULL )
4064 {
4065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4066 }
4067
4068 /*
4069 * We can't check that the config matches the initial one, but we can at
4070 * least check it matches the requirements for serializing.
4071 */
4072 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004073 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4074 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004075#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004076 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004077#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004078 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004079 {
4080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4081 }
4082
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004083 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4084
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004085 /*
4086 * Check version identifier
4087 */
4088 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4090
4091 if( memcmp( p, ssl_serialized_context_header,
4092 sizeof( ssl_serialized_context_header ) ) != 0 )
4093 {
4094 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4095 }
4096 p += sizeof( ssl_serialized_context_header );
4097
4098 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004099 * Session
4100 */
4101 if( (size_t)( end - p ) < 4 )
4102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4103
4104 session_len = ( (size_t) p[0] << 24 ) |
4105 ( (size_t) p[1] << 16 ) |
4106 ( (size_t) p[2] << 8 ) |
4107 ( (size_t) p[3] );
4108 p += 4;
4109
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004110 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004111 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004112 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004113 ssl->session_in = ssl->session;
4114 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004115 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004116
4117 if( (size_t)( end - p ) < session_len )
4118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4119
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004120 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004121 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004122 {
4123 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004124 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004125 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004126
4127 p += session_len;
4128
4129 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004130 * Transform
4131 */
4132
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004133 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004134 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004135 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004136 ssl->transform_in = ssl->transform;
4137 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004138 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004139
4140 /* Read random bytes and populate structure */
4141 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004143#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004144 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004145 ssl->session->ciphersuite,
4146 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004147#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004148 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004149#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004150 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4151 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004152 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004153 ssl->conf->endpoint,
4154 ssl );
4155 if( ret != 0 )
4156 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004157#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004158 p += sizeof( ssl->transform->randbytes );
4159
4160#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4161 /* Read connection IDs and store them */
4162 if( (size_t)( end - p ) < 1 )
4163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4164
4165 ssl->transform->in_cid_len = *p++;
4166
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004167 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4169
4170 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4171 p += ssl->transform->in_cid_len;
4172
4173 ssl->transform->out_cid_len = *p++;
4174
4175 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4177
4178 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4179 p += ssl->transform->out_cid_len;
4180#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4181
4182 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004183 * Saved fields from top-level ssl_context structure
4184 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004185 if( (size_t)( end - p ) < 4 )
4186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4187
4188 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4189 ( (uint32_t) p[1] << 16 ) |
4190 ( (uint32_t) p[2] << 8 ) |
4191 ( (uint32_t) p[3] );
4192 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004193
4194#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4195 if( (size_t)( end - p ) < 16 )
4196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4197
4198 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4199 ( (uint64_t) p[1] << 48 ) |
4200 ( (uint64_t) p[2] << 40 ) |
4201 ( (uint64_t) p[3] << 32 ) |
4202 ( (uint64_t) p[4] << 24 ) |
4203 ( (uint64_t) p[5] << 16 ) |
4204 ( (uint64_t) p[6] << 8 ) |
4205 ( (uint64_t) p[7] );
4206 p += 8;
4207
4208 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4209 ( (uint64_t) p[1] << 48 ) |
4210 ( (uint64_t) p[2] << 40 ) |
4211 ( (uint64_t) p[3] << 32 ) |
4212 ( (uint64_t) p[4] << 24 ) |
4213 ( (uint64_t) p[5] << 16 ) |
4214 ( (uint64_t) p[6] << 8 ) |
4215 ( (uint64_t) p[7] );
4216 p += 8;
4217#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4218
4219#if defined(MBEDTLS_SSL_PROTO_DTLS)
4220 if( (size_t)( end - p ) < 1 )
4221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4222
4223 ssl->disable_datagram_packing = *p++;
4224#endif /* MBEDTLS_SSL_PROTO_DTLS */
4225
Jerry Yud9a94fe2021-09-28 18:58:59 +08004226 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004228 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4229 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004230
4231#if defined(MBEDTLS_SSL_PROTO_DTLS)
4232 if( (size_t)( end - p ) < 2 )
4233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4234
4235 ssl->mtu = ( p[0] << 8 ) | p[1];
4236 p += 2;
4237#endif /* MBEDTLS_SSL_PROTO_DTLS */
4238
4239#if defined(MBEDTLS_SSL_ALPN)
4240 {
4241 uint8_t alpn_len;
4242 const char **cur;
4243
4244 if( (size_t)( end - p ) < 1 )
4245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4246
4247 alpn_len = *p++;
4248
4249 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4250 {
4251 /* alpn_chosen should point to an item in the configured list */
4252 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4253 {
4254 if( strlen( *cur ) == alpn_len &&
4255 memcmp( p, cur, alpn_len ) == 0 )
4256 {
4257 ssl->alpn_chosen = *cur;
4258 break;
4259 }
4260 }
4261 }
4262
4263 /* can only happen on conf mismatch */
4264 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4266
4267 p += alpn_len;
4268 }
4269#endif /* MBEDTLS_SSL_ALPN */
4270
4271 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004272 * Forced fields from top-level ssl_context structure
4273 *
4274 * Most of them already set to the correct value by mbedtls_ssl_init() and
4275 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4276 */
4277 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004278 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004279
Hanno Becker361b10d2019-08-30 10:42:49 +01004280 /* Adjust pointers for header fields of outgoing records to
4281 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004282 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004283
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004284#if defined(MBEDTLS_SSL_PROTO_DTLS)
4285 ssl->in_epoch = 1;
4286#endif
4287
4288 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4289 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004290 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4291 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004292 if( ssl->handshake != NULL )
4293 {
4294 mbedtls_ssl_handshake_free( ssl );
4295 mbedtls_free( ssl->handshake );
4296 ssl->handshake = NULL;
4297 }
4298
4299 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004300 * Done - should have consumed entire buffer
4301 */
4302 if( p != end )
4303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004304
4305 return( 0 );
4306}
4307
4308/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004309 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004310 */
4311int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4312 const unsigned char *buf,
4313 size_t len )
4314{
4315 int ret = ssl_context_load( context, buf, len );
4316
4317 if( ret != 0 )
4318 mbedtls_ssl_free( context );
4319
4320 return( ret );
4321}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004322#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004323
4324/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 * Free an SSL context
4326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004328{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004329 if( ssl == NULL )
4330 return;
4331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004334 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004335 {
sander-visserb8aa2072020-05-06 22:05:13 +02004336#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4337 size_t out_buf_len = ssl->out_buf_len;
4338#else
4339 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4340#endif
4341
Darryl Greenb33cc762019-11-28 14:29:44 +00004342 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004344 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004345 }
4346
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004347 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 {
sander-visserb8aa2072020-05-06 22:05:13 +02004349#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4350 size_t in_buf_len = ssl->in_buf_len;
4351#else
4352 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4353#endif
4354
Darryl Greenb33cc762019-11-28 14:29:44 +00004355 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004357 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 }
4359
Paul Bakker48916f92012-09-16 19:57:18 +00004360 if( ssl->transform )
4361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 mbedtls_ssl_transform_free( ssl->transform );
4363 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004364 }
4365
4366 if( ssl->handshake )
4367 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004368 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4370 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 mbedtls_free( ssl->handshake );
4373 mbedtls_free( ssl->transform_negotiate );
4374 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004375 }
4376
Ronald Cron6f135e12021-12-08 16:57:54 +01004377#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004378 mbedtls_ssl_transform_free( ssl->transform_application );
4379 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004380#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004381
Paul Bakkerc0463502013-02-14 11:19:38 +01004382 if( ssl->session )
4383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 mbedtls_ssl_session_free( ssl->session );
4385 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004386 }
4387
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004388#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004389 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004391 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004393 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004394#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004395
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004396#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004398#endif
4399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004401
Paul Bakker86f04f42013-02-14 11:20:09 +01004402 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004403 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004404}
4405
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004406/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004407 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004408 */
4409void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4410{
4411 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4412}
4413
Gilles Peskineae270bf2021-06-02 00:05:29 +02004414/* The selection should be the same as mbedtls_x509_crt_profile_default in
4415 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004416 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004417 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004418 * about this list.
4419 */
Brett Warrene0edc842021-08-17 09:53:13 +01004420static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004421#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004422 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004423#endif
4424#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004425 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004426#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004427#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004428 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004429#endif
4430#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004431 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004432#endif
4433#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004434 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004435#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004436#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004437 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004438#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004439#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004440 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004441#endif
4442#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004443 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004444#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004445 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004446};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004447
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004448static int ssl_preset_suiteb_ciphersuites[] = {
4449 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4450 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4451 0
4452};
4453
Gilles Peskineeccd8882020-03-10 12:19:08 +01004454#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004455
Jerry Yu909df7b2022-01-22 11:56:27 +08004456/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004457 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004458 * rules SHOULD be upheld.
4459 * - No duplicate entries.
4460 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004461 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004462 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004463 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004464static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004465
Andrzej Kurek25f27152022-08-17 16:09:31 -04004466#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 +08004467 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4468 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004469#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004470 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004471
Andrzej Kurek25f27152022-08-17 16:09:31 -04004472#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 +08004473 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4474 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004475#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004476 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4477
Andrzej Kurek25f27152022-08-17 16:09:31 -04004478#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 +08004479 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4480 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004481#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004482 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004483
Andrzej Kurek25f27152022-08-17 16:09:31 -04004484#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 +08004485 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004486#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 +08004487
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#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 +08004489 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004490#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 +08004491
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#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 +08004493 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004494#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 +08004495
Andrzej Kurek25f27152022-08-17 16:09:31 -04004496#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 +08004497 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4498#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4499
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#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 +08004501 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4502#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4503
Andrzej Kurek25f27152022-08-17 16:09:31 -04004504#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 +08004505 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4506#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4507
Gabor Mezei15b95a62022-05-09 16:37:58 +02004508 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004509};
4510
4511/* NOTICE: see above */
4512#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4513static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004514#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004515#if defined(MBEDTLS_ECDSA_C)
4516 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004517#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004518#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4519 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4520#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004521#if defined(MBEDTLS_RSA_C)
4522 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4523#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004524#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004525#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004526#if defined(MBEDTLS_ECDSA_C)
4527 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004528#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004529#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4530 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4531#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004532#if defined(MBEDTLS_RSA_C)
4533 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4534#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004535#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004536#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004537#if defined(MBEDTLS_ECDSA_C)
4538 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004539#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004540#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4541 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4542#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004543#if defined(MBEDTLS_RSA_C)
4544 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4545#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004546#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004547 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004548};
4549#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4550/* NOTICE: see above */
4551static uint16_t ssl_preset_suiteb_sig_algs[] = {
4552
Andrzej Kurek25f27152022-08-17 16:09:31 -04004553#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 +08004554 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4555 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004556#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004557 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4558
Andrzej Kurek25f27152022-08-17 16:09:31 -04004559#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 +08004560 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4561 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004562#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004563 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004564
Andrzej Kurek25f27152022-08-17 16:09:31 -04004565#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 +08004566 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004567#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 +08004568
Andrzej Kurek25f27152022-08-17 16:09:31 -04004569#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 +08004570 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004571#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004572
Gabor Mezei15b95a62022-05-09 16:37:58 +02004573 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004574};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004575
Jerry Yu909df7b2022-01-22 11:56:27 +08004576/* NOTICE: see above */
4577#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4578static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004579#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004580#if defined(MBEDTLS_ECDSA_C)
4581 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004582#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004583#if defined(MBEDTLS_RSA_C)
4584 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4585#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004586#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004587#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004588#if defined(MBEDTLS_ECDSA_C)
4589 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004590#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004591#if defined(MBEDTLS_RSA_C)
4592 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4593#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004594#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004595 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004596};
Jerry Yu909df7b2022-01-22 11:56:27 +08004597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4598
Jerry Yu1a8b4812022-01-20 17:56:50 +08004599#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004600
Brett Warrene0edc842021-08-17 09:53:13 +01004601static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004602#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004603 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004604#endif
4605#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004606 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004607#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004608 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004609};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004610
Jerry Yu1a8b4812022-01-20 17:56:50 +08004611#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004612/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004613 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004614MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004615static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004616{
4617 size_t i, j;
4618 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004619
Gabor Mezei15b95a62022-05-09 16:37:58 +02004620 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004621 {
Jerry Yuf377d642022-01-25 10:43:59 +08004622 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004623 {
Jerry Yuf377d642022-01-25 10:43:59 +08004624 if( sig_algs[i] != sig_algs[j] )
4625 continue;
4626 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4627 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4628 sig_algs[i], j, i );
4629 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004630 }
4631 }
4632 return( ret );
4633}
4634
4635#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4636
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004637/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004638 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004639 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004640int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004641 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004642{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004643#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004645#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004646
Jerry Yu1a8b4812022-01-20 17:56:50 +08004647#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004648 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004649 {
4650 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4651 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4652 }
4653
Jerry Yuf377d642022-01-25 10:43:59 +08004654 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004655 {
4656 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4657 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4658 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004659
4660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004661 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004662 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004663 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004664 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4665 }
4666
Jerry Yuf377d642022-01-25 10:43:59 +08004667 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004668 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004669 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004670 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4671 }
4672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004673#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4674
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004675 /* Use the functions here so that they are covered in tests,
4676 * but otherwise access member directly for efficiency */
4677 mbedtls_ssl_conf_endpoint( conf, endpoint );
4678 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004679
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004680 /*
4681 * Things that are common to all presets
4682 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004683#if defined(MBEDTLS_SSL_CLI_C)
4684 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4685 {
4686 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4687#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4688 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4689#endif
4690 }
4691#endif
4692
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004693#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4694 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4695#endif
4696
4697#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4698 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4699#endif
4700
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004701#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004702 conf->f_cookie_write = ssl_cookie_write_dummy;
4703 conf->f_cookie_check = ssl_cookie_check_dummy;
4704#endif
4705
4706#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4707 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4708#endif
4709
Janos Follath088ce432017-04-10 12:42:31 +01004710#if defined(MBEDTLS_SSL_SRV_C)
4711 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004712 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004713#endif
4714
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004715#if defined(MBEDTLS_SSL_PROTO_DTLS)
4716 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4717 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4718#endif
4719
4720#if defined(MBEDTLS_SSL_RENEGOTIATION)
4721 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004722 memset( conf->renego_period, 0x00, 2 );
4723 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004724#endif
4725
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004726#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004727 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4728 {
4729 const unsigned char dhm_p[] =
4730 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4731 const unsigned char dhm_g[] =
4732 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004733
Hanno Beckere2defad2021-07-24 05:59:17 +01004734 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4735 dhm_p, sizeof( dhm_p ),
4736 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4737 {
4738 return( ret );
4739 }
4740 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004741#endif
4742
Ronald Cron6f135e12021-12-08 16:57:54 +01004743#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004744#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004745 mbedtls_ssl_conf_new_session_tickets(
4746 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4747#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004748 /*
4749 * Allow all TLS 1.3 key exchange modes by default.
4750 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004751 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004752#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004753
XiaokangQian4d3a6042022-04-21 13:46:17 +00004754 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4755 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004756#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004757 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004758 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004759#else
XiaokangQian060d8672022-04-21 09:24:56 +00004760 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004761#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004762 }
4763 else
4764 {
4765#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4766 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4767 {
4768 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4769 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4770 }
4771 else
4772 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4773 {
4774 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4775 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4776 }
4777#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4778 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4779 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4780#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4781 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4782 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4783#else
4784 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4785#endif
4786 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004787
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004788 /*
4789 * Preset-specific defaults
4790 */
4791 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004792 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004793 /*
4794 * NSA Suite B
4795 */
4796 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004797
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004798 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004799
4800#if defined(MBEDTLS_X509_CRT_PARSE_C)
4801 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004802#endif
4803
Gilles Peskineeccd8882020-03-10 12:19:08 +01004804#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004805#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4806 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4807 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4808 else
4809#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4810 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004811#endif
4812
Brett Warrene0edc842021-08-17 09:53:13 +01004813#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4814 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004815#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004816 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004817 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004818
4819 /*
4820 * Default
4821 */
4822 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004823
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004824 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004825
4826#if defined(MBEDTLS_X509_CRT_PARSE_C)
4827 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4828#endif
4829
Gilles Peskineeccd8882020-03-10 12:19:08 +01004830#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004831#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4832 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4833 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4834 else
4835#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4836 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004837#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004838
Brett Warrene0edc842021-08-17 09:53:13 +01004839#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4840 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004841#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004842 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004843
4844#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4845 conf->dhm_min_bitlen = 1024;
4846#endif
4847 }
4848
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004849 return( 0 );
4850}
4851
4852/*
4853 * Free mbedtls_ssl_config
4854 */
4855void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4856{
4857#if defined(MBEDTLS_DHM_C)
4858 mbedtls_mpi_free( &conf->dhm_P );
4859 mbedtls_mpi_free( &conf->dhm_G );
4860#endif
4861
Gilles Peskineeccd8882020-03-10 12:19:08 +01004862#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004863#if defined(MBEDTLS_USE_PSA_CRYPTO)
4864 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4865 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004866 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4867 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004868#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004869 if( conf->psk != NULL )
4870 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004871 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004872 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004873 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004874 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004875 }
4876
4877 if( conf->psk_identity != NULL )
4878 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004879 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004880 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004881 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004882 conf->psk_identity_len = 0;
4883 }
4884#endif
4885
4886#if defined(MBEDTLS_X509_CRT_PARSE_C)
4887 ssl_key_cert_free( conf->key_cert );
4888#endif
4889
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004890 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004891}
4892
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004893#if defined(MBEDTLS_PK_C) && \
4894 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004895/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900#if defined(MBEDTLS_RSA_C)
4901 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4902 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904#if defined(MBEDTLS_ECDSA_C)
4905 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4906 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004909}
4910
Hanno Becker7e5437a2017-04-28 17:15:26 +01004911unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4912{
4913 switch( type ) {
4914 case MBEDTLS_PK_RSA:
4915 return( MBEDTLS_SSL_SIG_RSA );
4916 case MBEDTLS_PK_ECDSA:
4917 case MBEDTLS_PK_ECKEY:
4918 return( MBEDTLS_SSL_SIG_ECDSA );
4919 default:
4920 return( MBEDTLS_SSL_SIG_ANON );
4921 }
4922}
4923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004925{
4926 switch( sig )
4927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_RSA_C)
4929 case MBEDTLS_SSL_SIG_RSA:
4930 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932#if defined(MBEDTLS_ECDSA_C)
4933 case MBEDTLS_SSL_SIG_ECDSA:
4934 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004935#endif
4936 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004938 }
4939}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004940#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004941
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004942/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004943 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004946{
4947 switch( hash )
4948 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004949#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 case MBEDTLS_SSL_HASH_MD5:
4951 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004952#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004953#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 case MBEDTLS_SSL_HASH_SHA1:
4955 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004956#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004957#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 case MBEDTLS_SSL_HASH_SHA224:
4959 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004960#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004961#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 case MBEDTLS_SSL_HASH_SHA256:
4963 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004964#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004965#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 case MBEDTLS_SSL_HASH_SHA384:
4967 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004968#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 case MBEDTLS_SSL_HASH_SHA512:
4971 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004972#endif
4973 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004975 }
4976}
4977
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004978/*
4979 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4980 */
4981unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4982{
4983 switch( md )
4984 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004985#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004986 case MBEDTLS_MD_MD5:
4987 return( MBEDTLS_SSL_HASH_MD5 );
4988#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004989#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004990 case MBEDTLS_MD_SHA1:
4991 return( MBEDTLS_SSL_HASH_SHA1 );
4992#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004993#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004994 case MBEDTLS_MD_SHA224:
4995 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004996#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004998 case MBEDTLS_MD_SHA256:
4999 return( MBEDTLS_SSL_HASH_SHA256 );
5000#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005002 case MBEDTLS_MD_SHA384:
5003 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005004#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005005#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005006 case MBEDTLS_MD_SHA512:
5007 return( MBEDTLS_SSL_HASH_SHA512 );
5008#endif
5009 default:
5010 return( MBEDTLS_SSL_HASH_NONE );
5011 }
5012}
5013
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005014/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005015 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005016 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005017 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005018int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005019{
Brett Warrene0edc842021-08-17 09:53:13 +01005020 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005021
Brett Warrene0edc842021-08-17 09:53:13 +01005022 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005023 return( -1 );
5024
Brett Warrene0edc842021-08-17 09:53:13 +01005025 for( ; *group_list != 0; group_list++ )
5026 {
5027 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005028 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005029 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005030
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005031 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005032}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005033
5034#if defined(MBEDTLS_ECP_C)
5035/*
5036 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5037 */
5038int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5039{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005040 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005041 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005042
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005043 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005044 return -1;
5045
5046 uint16_t tls_id = grp_info->tls_id;
5047
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005048 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5049}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005050#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052#if defined(MBEDTLS_X509_CRT_PARSE_C)
5053int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5054 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005055 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005056 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005057{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005058 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005059 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005060 const char *ext_oid;
5061 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005064 {
5065 /* Server part of the key exchange */
5066 switch( ciphersuite->key_exchange )
5067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 case MBEDTLS_KEY_EXCHANGE_RSA:
5069 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005070 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005071 break;
5072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5074 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5075 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5076 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005077 break;
5078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5080 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005081 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005082 break;
5083
5084 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 case MBEDTLS_KEY_EXCHANGE_NONE:
5086 case MBEDTLS_KEY_EXCHANGE_PSK:
5087 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5088 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005089 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005090 usage = 0;
5091 }
5092 }
5093 else
5094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5096 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005097 }
5098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005100 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005101 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005102 ret = -1;
5103 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5108 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005109 }
5110 else
5111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5113 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005114 }
5115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005117 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005118 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005119 ret = -1;
5120 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005121
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005122 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005123}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005125
Jerry Yu148165c2021-09-24 23:20:59 +08005126#if defined(MBEDTLS_USE_PSA_CRYPTO)
5127int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5128 const mbedtls_md_type_t md,
5129 unsigned char *dst,
5130 size_t dst_len,
5131 size_t *olen )
5132{
Ronald Cronf6893e12022-01-07 22:09:01 +01005133 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5134 psa_hash_operation_t *hash_operation_to_clone;
5135 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5136
Jerry Yu148165c2021-09-24 23:20:59 +08005137 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005138
5139 switch( md )
5140 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005141#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005142 case MBEDTLS_MD_SHA384:
5143 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5144 break;
5145#endif
5146
Andrzej Kurek25f27152022-08-17 16:09:31 -04005147#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005148 case MBEDTLS_MD_SHA256:
5149 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5150 break;
5151#endif
5152
5153 default:
5154 goto exit;
5155 }
5156
5157 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5158 if( status != PSA_SUCCESS )
5159 goto exit;
5160
5161 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5162 if( status != PSA_SUCCESS )
5163 goto exit;
5164
5165exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005166 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005167}
5168#else /* MBEDTLS_USE_PSA_CRYPTO */
5169
Andrzej Kurek25f27152022-08-17 16:09:31 -04005170#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005171MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005172static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5173 unsigned char *dst,
5174 size_t dst_len,
5175 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005176{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005177 int ret;
5178 mbedtls_sha512_context sha512;
5179
5180 if( dst_len < 48 )
5181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5182
5183 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005184 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005185
5186 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5187 {
5188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5189 goto exit;
5190 }
5191
5192 *olen = 48;
5193
5194exit:
5195
5196 mbedtls_sha512_free( &sha512 );
5197 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005198}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005199#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005200
Andrzej Kurek25f27152022-08-17 16:09:31 -04005201#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005202MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005203static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5204 unsigned char *dst,
5205 size_t dst_len,
5206 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005207{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005208 int ret;
5209 mbedtls_sha256_context sha256;
5210
5211 if( dst_len < 32 )
5212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5213
5214 mbedtls_sha256_init( &sha256 );
5215 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005216
Jerry Yu24c0ec32021-09-09 14:21:07 +08005217 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5218 {
5219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5220 goto exit;
5221 }
5222
5223 *olen = 32;
5224
5225exit:
5226
5227 mbedtls_sha256_free( &sha256 );
5228 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005229}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005230#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005231
Jerry Yu000f9762021-09-14 11:12:51 +08005232int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5233 const mbedtls_md_type_t md,
5234 unsigned char *dst,
5235 size_t dst_len,
5236 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005237{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005238 switch( md )
5239 {
5240
Andrzej Kurek25f27152022-08-17 16:09:31 -04005241#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005242 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005243 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005244#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005245
Andrzej Kurek25f27152022-08-17 16:09:31 -04005246#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005247 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005248 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005249#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005250
5251 default:
5252 break;
5253 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005255}
XiaokangQian647719a2021-12-07 09:16:29 +00005256
Jerry Yu148165c2021-09-24 23:20:59 +08005257#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005258
Gabor Mezei078e8032022-04-27 21:17:56 +02005259#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5260/* mbedtls_ssl_parse_sig_alg_ext()
5261 *
5262 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5263 * value (TLS 1.3 RFC8446):
5264 * enum {
5265 * ....
5266 * ecdsa_secp256r1_sha256( 0x0403 ),
5267 * ecdsa_secp384r1_sha384( 0x0503 ),
5268 * ecdsa_secp521r1_sha512( 0x0603 ),
5269 * ....
5270 * } SignatureScheme;
5271 *
5272 * struct {
5273 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5274 * } SignatureSchemeList;
5275 *
5276 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5277 * value (TLS 1.2 RFC5246):
5278 * enum {
5279 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5280 * sha512(6), (255)
5281 * } HashAlgorithm;
5282 *
5283 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5284 * SignatureAlgorithm;
5285 *
5286 * struct {
5287 * HashAlgorithm hash;
5288 * SignatureAlgorithm signature;
5289 * } SignatureAndHashAlgorithm;
5290 *
5291 * SignatureAndHashAlgorithm
5292 * supported_signature_algorithms<2..2^16-2>;
5293 *
5294 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5295 * generalization of the TLS 1.2 signature algorithm extension.
5296 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5297 * `SignatureScheme` field of TLS 1.3
5298 *
5299 */
5300int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5301 const unsigned char *buf,
5302 const unsigned char *end )
5303{
5304 const unsigned char *p = buf;
5305 size_t supported_sig_algs_len = 0;
5306 const unsigned char *supported_sig_algs_end;
5307 uint16_t sig_alg;
5308 uint32_t common_idx = 0;
5309
5310 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5311 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5312 p += 2;
5313
5314 memset( ssl->handshake->received_sig_algs, 0,
5315 sizeof(ssl->handshake->received_sig_algs) );
5316
5317 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5318 supported_sig_algs_end = p + supported_sig_algs_len;
5319 while( p < supported_sig_algs_end )
5320 {
5321 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5322 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5323 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005324 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5325 sig_alg,
5326 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005327#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005328 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005329 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5330 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5331 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005332 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005333 }
5334#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005335
Jerry Yuf3b46b52022-06-19 16:52:27 +08005336 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5337 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005338
5339 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5340 {
5341 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5342 common_idx += 1;
5343 }
5344 }
5345 /* Check that we consumed all the message. */
5346 if( p != end )
5347 {
5348 MBEDTLS_SSL_DEBUG_MSG( 1,
5349 ( "Signature algorithms extension length misaligned" ) );
5350 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5351 MBEDTLS_ERR_SSL_DECODE_ERROR );
5352 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5353 }
5354
5355 if( common_idx == 0 )
5356 {
5357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5358 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5359 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5360 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5361 }
5362
Gabor Mezei15b95a62022-05-09 16:37:58 +02005363 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005364 return( 0 );
5365}
5366
5367#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5368
Jerry Yudc7bd172022-02-17 13:44:15 +08005369#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5370
5371#if defined(MBEDTLS_USE_PSA_CRYPTO)
5372
5373static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5374 mbedtls_svc_key_id_t key,
5375 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005376 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005377 const unsigned char* seed, size_t seed_length,
5378 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005379 const unsigned char* other_secret,
5380 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005381 size_t capacity )
5382{
5383 psa_status_t status;
5384
5385 status = psa_key_derivation_setup( derivation, alg );
5386 if( status != PSA_SUCCESS )
5387 return( status );
5388
5389 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5390 {
5391 status = psa_key_derivation_input_bytes( derivation,
5392 PSA_KEY_DERIVATION_INPUT_SEED,
5393 seed, seed_length );
5394 if( status != PSA_SUCCESS )
5395 return( status );
5396
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005397 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005398 {
5399 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005400 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5401 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005402 if( status != PSA_SUCCESS )
5403 return( status );
5404 }
5405
Jerry Yudc7bd172022-02-17 13:44:15 +08005406 if( mbedtls_svc_key_id_is_null( key ) )
5407 {
5408 status = psa_key_derivation_input_bytes(
5409 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005410 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005411 }
5412 else
5413 {
5414 status = psa_key_derivation_input_key(
5415 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5416 }
5417 if( status != PSA_SUCCESS )
5418 return( status );
5419
5420 status = psa_key_derivation_input_bytes( derivation,
5421 PSA_KEY_DERIVATION_INPUT_LABEL,
5422 label, label_length );
5423 if( status != PSA_SUCCESS )
5424 return( status );
5425 }
5426 else
5427 {
5428 return( PSA_ERROR_NOT_SUPPORTED );
5429 }
5430
5431 status = psa_key_derivation_set_capacity( derivation, capacity );
5432 if( status != PSA_SUCCESS )
5433 return( status );
5434
5435 return( PSA_SUCCESS );
5436}
5437
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005438MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005439static int tls_prf_generic( mbedtls_md_type_t md_type,
5440 const unsigned char *secret, size_t slen,
5441 const char *label,
5442 const unsigned char *random, size_t rlen,
5443 unsigned char *dstbuf, size_t dlen )
5444{
5445 psa_status_t status;
5446 psa_algorithm_t alg;
5447 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5448 psa_key_derivation_operation_t derivation =
5449 PSA_KEY_DERIVATION_OPERATION_INIT;
5450
5451 if( md_type == MBEDTLS_MD_SHA384 )
5452 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5453 else
5454 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5455
5456 /* Normally a "secret" should be long enough to be impossible to
5457 * find by brute force, and in particular should not be empty. But
5458 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5459 * and for this use case it makes sense to have a 0-length "secret".
5460 * Since the key API doesn't allow importing a key of length 0,
5461 * keep master_key=0, which setup_psa_key_derivation() understands
5462 * to mean a 0-length "secret" input. */
5463 if( slen != 0 )
5464 {
5465 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5466 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5467 psa_set_key_algorithm( &key_attributes, alg );
5468 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5469
5470 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5471 if( status != PSA_SUCCESS )
5472 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5473 }
5474
5475 status = setup_psa_key_derivation( &derivation,
5476 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005477 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005478 random, rlen,
5479 (unsigned char const *) label,
5480 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005481 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005482 dlen );
5483 if( status != PSA_SUCCESS )
5484 {
5485 psa_key_derivation_abort( &derivation );
5486 psa_destroy_key( master_key );
5487 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5488 }
5489
5490 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5491 if( status != PSA_SUCCESS )
5492 {
5493 psa_key_derivation_abort( &derivation );
5494 psa_destroy_key( master_key );
5495 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5496 }
5497
5498 status = psa_key_derivation_abort( &derivation );
5499 if( status != PSA_SUCCESS )
5500 {
5501 psa_destroy_key( master_key );
5502 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5503 }
5504
5505 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5506 status = psa_destroy_key( master_key );
5507 if( status != PSA_SUCCESS )
5508 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5509
5510 return( 0 );
5511}
5512
5513#else /* MBEDTLS_USE_PSA_CRYPTO */
5514
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005515MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005516static int tls_prf_generic( mbedtls_md_type_t md_type,
5517 const unsigned char *secret, size_t slen,
5518 const char *label,
5519 const unsigned char *random, size_t rlen,
5520 unsigned char *dstbuf, size_t dlen )
5521{
5522 size_t nb;
5523 size_t i, j, k, md_len;
5524 unsigned char *tmp;
5525 size_t tmp_len = 0;
5526 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5527 const mbedtls_md_info_t *md_info;
5528 mbedtls_md_context_t md_ctx;
5529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5530
5531 mbedtls_md_init( &md_ctx );
5532
5533 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5535
5536 md_len = mbedtls_md_get_size( md_info );
5537
5538 tmp_len = md_len + strlen( label ) + rlen;
5539 tmp = mbedtls_calloc( 1, tmp_len );
5540 if( tmp == NULL )
5541 {
5542 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5543 goto exit;
5544 }
5545
5546 nb = strlen( label );
5547 memcpy( tmp + md_len, label, nb );
5548 memcpy( tmp + md_len + nb, random, rlen );
5549 nb += rlen;
5550
5551 /*
5552 * Compute P_<hash>(secret, label + random)[0..dlen]
5553 */
5554 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5555 goto exit;
5556
5557 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5558 if( ret != 0 )
5559 goto exit;
5560 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5561 if( ret != 0 )
5562 goto exit;
5563 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5564 if( ret != 0 )
5565 goto exit;
5566
5567 for( i = 0; i < dlen; i += md_len )
5568 {
5569 ret = mbedtls_md_hmac_reset ( &md_ctx );
5570 if( ret != 0 )
5571 goto exit;
5572 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5573 if( ret != 0 )
5574 goto exit;
5575 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5576 if( ret != 0 )
5577 goto exit;
5578
5579 ret = mbedtls_md_hmac_reset ( &md_ctx );
5580 if( ret != 0 )
5581 goto exit;
5582 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5583 if( ret != 0 )
5584 goto exit;
5585 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5586 if( ret != 0 )
5587 goto exit;
5588
5589 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5590
5591 for( j = 0; j < k; j++ )
5592 dstbuf[i + j] = h_i[j];
5593 }
5594
5595exit:
5596 mbedtls_md_free( &md_ctx );
5597
5598 mbedtls_platform_zeroize( tmp, tmp_len );
5599 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5600
5601 mbedtls_free( tmp );
5602
5603 return( ret );
5604}
5605#endif /* MBEDTLS_USE_PSA_CRYPTO */
5606
Andrzej Kurek25f27152022-08-17 16:09:31 -04005607#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005608MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005609static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5610 const char *label,
5611 const unsigned char *random, size_t rlen,
5612 unsigned char *dstbuf, size_t dlen )
5613{
5614 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5615 label, random, rlen, dstbuf, dlen ) );
5616}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005617#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005618
Andrzej Kurek25f27152022-08-17 16:09:31 -04005619#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005620MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005621static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5622 const char *label,
5623 const unsigned char *random, size_t rlen,
5624 unsigned char *dstbuf, size_t dlen )
5625{
5626 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5627 label, random, rlen, dstbuf, dlen ) );
5628}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005629#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005630
Jerry Yuf009d862022-02-17 14:01:37 +08005631/*
5632 * Set appropriate PRF function and other SSL / TLS1.2 functions
5633 *
5634 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005635 * - hash associated with the ciphersuite (only used by TLS 1.2)
5636 *
5637 * Outputs:
5638 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5639 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005640MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005641static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005642 mbedtls_md_type_t hash )
5643{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005644#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005645 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005646 {
5647 handshake->tls_prf = tls_prf_sha384;
5648 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5649 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5650 }
5651 else
5652#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005653#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005654 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005655 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005656 handshake->tls_prf = tls_prf_sha256;
5657 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5658 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5659 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005660#else
Jerry Yuf009d862022-02-17 14:01:37 +08005661 {
Jerry Yuf009d862022-02-17 14:01:37 +08005662 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005663 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5665 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005666#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005667
5668 return( 0 );
5669}
Jerry Yud6ab2352022-02-17 14:03:43 +08005670
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005671/*
5672 * Compute master secret if needed
5673 *
5674 * Parameters:
5675 * [in/out] handshake
5676 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5677 * (PSA-PSK) ciphersuite_info, psk_opaque
5678 * [out] premaster (cleared)
5679 * [out] master
5680 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5681 * debug: conf->f_dbg, conf->p_dbg
5682 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005683 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005684 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005685MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005686static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5687 unsigned char *master,
5688 const mbedtls_ssl_context *ssl )
5689{
5690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5691
5692 /* cf. RFC 5246, Section 8.1:
5693 * "The master secret is always exactly 48 bytes in length." */
5694 size_t const master_secret_len = 48;
5695
5696#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5697 unsigned char session_hash[48];
5698#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5699
5700 /* The label for the KDF used for key expansion.
5701 * This is either "master secret" or "extended master secret"
5702 * depending on whether the Extended Master Secret extension
5703 * is used. */
5704 char const *lbl = "master secret";
5705
Przemek Stekielae4ed302022-04-05 17:15:55 +02005706 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005707 * - If the Extended Master Secret extension is not used,
5708 * this is ClientHello.Random + ServerHello.Random
5709 * (see Sect. 8.1 in RFC 5246).
5710 * - If the Extended Master Secret extension is used,
5711 * this is the transcript of the handshake so far.
5712 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005713 unsigned char const *seed = handshake->randbytes;
5714 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005715
5716#if !defined(MBEDTLS_DEBUG_C) && \
5717 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5718 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5719 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5720 ssl = NULL; /* make sure we don't use it except for those cases */
5721 (void) ssl;
5722#endif
5723
5724 if( handshake->resume != 0 )
5725 {
5726 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5727 return( 0 );
5728 }
5729
5730#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5731 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5732 {
5733 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005734 seed = session_hash;
5735 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005736
5737 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005738 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005739 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005740#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005741
Przemek Stekiel99114f32022-04-22 11:20:09 +02005742#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005743 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005744 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005745 {
5746 /* Perform PSK-to-MS expansion in a single step. */
5747 psa_status_t status;
5748 psa_algorithm_t alg;
5749 mbedtls_svc_key_id_t psk;
5750 psa_key_derivation_operation_t derivation =
5751 PSA_KEY_DERIVATION_OPERATION_INIT;
5752 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5753
5754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5755
5756 psk = mbedtls_ssl_get_opaque_psk( ssl );
5757
5758 if( hash_alg == MBEDTLS_MD_SHA384 )
5759 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5760 else
5761 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5762
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005763 size_t other_secret_len = 0;
5764 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005765
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005766 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005767 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005768 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005769 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005770 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005771 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005772 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005773 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005774 other_secret_len = 48;
5775 other_secret = handshake->premaster + 2;
5776 break;
5777 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005778 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005779 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5780 other_secret = handshake->premaster + 2;
5781 break;
5782 default:
5783 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005784 }
5785
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005786 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005787 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005788 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005789 (unsigned char const *) lbl,
5790 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005791 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005792 master_secret_len );
5793 if( status != PSA_SUCCESS )
5794 {
5795 psa_key_derivation_abort( &derivation );
5796 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5797 }
5798
5799 status = psa_key_derivation_output_bytes( &derivation,
5800 master,
5801 master_secret_len );
5802 if( status != PSA_SUCCESS )
5803 {
5804 psa_key_derivation_abort( &derivation );
5805 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5806 }
5807
5808 status = psa_key_derivation_abort( &derivation );
5809 if( status != PSA_SUCCESS )
5810 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5811 }
5812 else
5813#endif
5814 {
5815 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005816 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005817 master,
5818 master_secret_len );
5819 if( ret != 0 )
5820 {
5821 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5822 return( ret );
5823 }
5824
5825 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5826 handshake->premaster,
5827 handshake->pmslen );
5828
5829 mbedtls_platform_zeroize( handshake->premaster,
5830 sizeof(handshake->premaster) );
5831 }
5832
5833 return( 0 );
5834}
5835
Jerry Yud62f87e2022-02-17 14:09:02 +08005836int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5837{
5838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5839 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5840 ssl->handshake->ciphersuite_info;
5841
5842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5843
5844 /* Set PRF, calc_verify and calc_finished function pointers */
5845 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005846 ciphersuite_info->mac );
5847 if( ret != 0 )
5848 {
5849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5850 return( ret );
5851 }
5852
5853 /* Compute master secret if needed */
5854 ret = ssl_compute_master( ssl->handshake,
5855 ssl->session_negotiate->master,
5856 ssl );
5857 if( ret != 0 )
5858 {
5859 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5860 return( ret );
5861 }
5862
5863 /* Swap the client and server random values:
5864 * - MS derivation wanted client+server (RFC 5246 8.1)
5865 * - key derivation wants server+client (RFC 5246 6.3) */
5866 {
5867 unsigned char tmp[64];
5868 memcpy( tmp, ssl->handshake->randbytes, 64 );
5869 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5870 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5871 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5872 }
5873
5874 /* Populate transform structure */
5875 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5876 ssl->session_negotiate->ciphersuite,
5877 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005878#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005879 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005880#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005881 ssl->handshake->tls_prf,
5882 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005883 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005884 ssl->conf->endpoint,
5885 ssl );
5886 if( ret != 0 )
5887 {
5888 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5889 return( ret );
5890 }
5891
5892 /* We no longer need Server/ClientHello.random values */
5893 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5894 sizeof( ssl->handshake->randbytes ) );
5895
5896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5897
5898 return( 0 );
5899}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005900
Ronald Cron4dcbca92022-03-07 10:21:40 +01005901int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5902{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005903 switch( md )
5904 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005905#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005906 case MBEDTLS_SSL_HASH_SHA384:
5907 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5908 break;
5909#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005910#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005911 case MBEDTLS_SSL_HASH_SHA256:
5912 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5913 break;
5914#endif
5915 default:
5916 return( -1 );
5917 }
5918
Ronald Cronc2f13a02022-03-07 10:25:24 +01005919 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005920}
5921
Andrzej Kurek25f27152022-08-17 16:09:31 -04005922#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005923void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5924 unsigned char *hash,
5925 size_t *hlen )
5926{
5927#if defined(MBEDTLS_USE_PSA_CRYPTO)
5928 size_t hash_size;
5929 psa_status_t status;
5930 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5931
5932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5933 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5934 if( status != PSA_SUCCESS )
5935 {
5936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5937 return;
5938 }
5939
5940 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5941 if( status != PSA_SUCCESS )
5942 {
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5944 return;
5945 }
5946
5947 *hlen = 32;
5948 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5950#else
5951 mbedtls_sha256_context sha256;
5952
5953 mbedtls_sha256_init( &sha256 );
5954
5955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5956
5957 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5958 mbedtls_sha256_finish( &sha256, hash );
5959
5960 *hlen = 32;
5961
5962 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5964
5965 mbedtls_sha256_free( &sha256 );
5966#endif /* MBEDTLS_USE_PSA_CRYPTO */
5967 return;
5968}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005969#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005970
Andrzej Kurek25f27152022-08-17 16:09:31 -04005971#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005972void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5973 unsigned char *hash,
5974 size_t *hlen )
5975{
5976#if defined(MBEDTLS_USE_PSA_CRYPTO)
5977 size_t hash_size;
5978 psa_status_t status;
5979 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5980
5981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5982 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5983 if( status != PSA_SUCCESS )
5984 {
5985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5986 return;
5987 }
5988
5989 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5990 if( status != PSA_SUCCESS )
5991 {
5992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5993 return;
5994 }
5995
5996 *hlen = 48;
5997 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5999#else
6000 mbedtls_sha512_context sha512;
6001
6002 mbedtls_sha512_init( &sha512 );
6003
6004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6005
Andrzej Kureka242e832022-08-11 10:03:14 -04006006 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006007 mbedtls_sha512_finish( &sha512, hash );
6008
6009 *hlen = 48;
6010
6011 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6013
6014 mbedtls_sha512_free( &sha512 );
6015#endif /* MBEDTLS_USE_PSA_CRYPTO */
6016 return;
6017}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006018#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006019
Neil Armstrong80f6f322022-05-03 17:56:38 +02006020#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6021 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006022int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6023{
6024 unsigned char *p = ssl->handshake->premaster;
6025 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6026 const unsigned char *psk = NULL;
6027 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006028 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006029
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006030 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006031 {
6032 /*
6033 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006034 * checked before calling this function.
6035 *
6036 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006037 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006038 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006039 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6040 {
6041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6043 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006044 }
6045
6046 /*
6047 * PMS = struct {
6048 * opaque other_secret<0..2^16-1>;
6049 * opaque psk<0..2^16-1>;
6050 * };
6051 * with "other_secret" depending on the particular key exchange
6052 */
6053#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6054 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6055 {
6056 if( end - p < 2 )
6057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6058
6059 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6060 p += 2;
6061
6062 if( end < p || (size_t)( end - p ) < psk_len )
6063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6064
6065 memset( p, 0, psk_len );
6066 p += psk_len;
6067 }
6068 else
6069#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6070#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6071 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6072 {
6073 /*
6074 * other_secret already set by the ClientKeyExchange message,
6075 * and is 48 bytes long
6076 */
6077 if( end - p < 2 )
6078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6079
6080 *p++ = 0;
6081 *p++ = 48;
6082 p += 48;
6083 }
6084 else
6085#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6086#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6087 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6088 {
6089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6090 size_t len;
6091
6092 /* Write length only when we know the actual value */
6093 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6094 p + 2, end - ( p + 2 ), &len,
6095 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6096 {
6097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6098 return( ret );
6099 }
6100 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6101 p += 2 + len;
6102
6103 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6104 }
6105 else
6106#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006107#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006108 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6109 {
6110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6111 size_t zlen;
6112
6113 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6114 p + 2, end - ( p + 2 ),
6115 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6116 {
6117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6118 return( ret );
6119 }
6120
6121 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6122 p += 2 + zlen;
6123
6124 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6125 MBEDTLS_DEBUG_ECDH_Z );
6126 }
6127 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006128#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006129 {
6130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6132 }
6133
6134 /* opaque psk<0..2^16-1>; */
6135 if( end - p < 2 )
6136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6137
6138 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6139 p += 2;
6140
6141 if( end < p || (size_t)( end - p ) < psk_len )
6142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6143
6144 memcpy( p, psk, psk_len );
6145 p += psk_len;
6146
6147 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6148
6149 return( 0 );
6150}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006151#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006152
6153#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006154MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006155static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6156
6157#if defined(MBEDTLS_SSL_PROTO_DTLS)
6158int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6159{
6160 /* If renegotiation is not enforced, retransmit until we would reach max
6161 * timeout if we were using the usual handshake doubling scheme */
6162 if( ssl->conf->renego_max_records < 0 )
6163 {
6164 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6165 unsigned char doublings = 1;
6166
6167 while( ratio != 0 )
6168 {
6169 ++doublings;
6170 ratio >>= 1;
6171 }
6172
6173 if( ++ssl->renego_records_seen > doublings )
6174 {
6175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6176 return( 0 );
6177 }
6178 }
6179
6180 return( ssl_write_hello_request( ssl ) );
6181}
6182#endif
6183#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006184
Jerry Yud9526692022-02-17 14:23:47 +08006185/*
6186 * Handshake functions
6187 */
6188#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6189/* No certificate support -> dummy functions */
6190int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6191{
6192 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6193 ssl->handshake->ciphersuite_info;
6194
6195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6196
6197 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6198 {
6199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6200 ssl->state++;
6201 return( 0 );
6202 }
6203
6204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6206}
6207
6208int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6209{
6210 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6211 ssl->handshake->ciphersuite_info;
6212
6213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6214
6215 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6216 {
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6218 ssl->state++;
6219 return( 0 );
6220 }
6221
6222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6224}
6225
6226#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6227/* Some certificate support -> implement write and parse */
6228
6229int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6230{
6231 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6232 size_t i, n;
6233 const mbedtls_x509_crt *crt;
6234 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6235 ssl->handshake->ciphersuite_info;
6236
6237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6238
6239 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6240 {
6241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6242 ssl->state++;
6243 return( 0 );
6244 }
6245
6246#if defined(MBEDTLS_SSL_CLI_C)
6247 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6248 {
6249 if( ssl->handshake->client_auth == 0 )
6250 {
6251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6252 ssl->state++;
6253 return( 0 );
6254 }
6255 }
6256#endif /* MBEDTLS_SSL_CLI_C */
6257#if defined(MBEDTLS_SSL_SRV_C)
6258 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6259 {
6260 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6261 {
6262 /* Should never happen because we shouldn't have picked the
6263 * ciphersuite if we don't have a certificate. */
6264 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6265 }
6266 }
6267#endif
6268
6269 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6270
6271 /*
6272 * 0 . 0 handshake type
6273 * 1 . 3 handshake length
6274 * 4 . 6 length of all certs
6275 * 7 . 9 length of cert. 1
6276 * 10 . n-1 peer certificate
6277 * n . n+2 length of cert. 2
6278 * n+3 . ... upper level cert, etc.
6279 */
6280 i = 7;
6281 crt = mbedtls_ssl_own_cert( ssl );
6282
6283 while( crt != NULL )
6284 {
6285 n = crt->raw.len;
6286 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6287 {
6288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6289 " > %" MBEDTLS_PRINTF_SIZET,
6290 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6291 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6292 }
6293
6294 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6295 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6296 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6297
6298 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6299 i += n; crt = crt->next;
6300 }
6301
6302 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6303 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6304 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6305
6306 ssl->out_msglen = i;
6307 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6308 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6309
6310 ssl->state++;
6311
6312 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6313 {
6314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6315 return( ret );
6316 }
6317
6318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6319
6320 return( ret );
6321}
6322
6323#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6324
6325#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006326MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006327static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6328 unsigned char *crt_buf,
6329 size_t crt_buf_len )
6330{
6331 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6332
6333 if( peer_crt == NULL )
6334 return( -1 );
6335
6336 if( peer_crt->raw.len != crt_buf_len )
6337 return( -1 );
6338
6339 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6340}
6341#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006342MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006343static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6344 unsigned char *crt_buf,
6345 size_t crt_buf_len )
6346{
6347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6348 unsigned char const * const peer_cert_digest =
6349 ssl->session->peer_cert_digest;
6350 mbedtls_md_type_t const peer_cert_digest_type =
6351 ssl->session->peer_cert_digest_type;
6352 mbedtls_md_info_t const * const digest_info =
6353 mbedtls_md_info_from_type( peer_cert_digest_type );
6354 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6355 size_t digest_len;
6356
6357 if( peer_cert_digest == NULL || digest_info == NULL )
6358 return( -1 );
6359
6360 digest_len = mbedtls_md_get_size( digest_info );
6361 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6362 return( -1 );
6363
6364 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6365 if( ret != 0 )
6366 return( -1 );
6367
6368 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6369}
6370#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6371#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6372
6373/*
6374 * Once the certificate message is read, parse it into a cert chain and
6375 * perform basic checks, but leave actual verification to the caller
6376 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006377MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006378static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6379 mbedtls_x509_crt *chain )
6380{
6381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6382#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6383 int crt_cnt=0;
6384#endif
6385 size_t i, n;
6386 uint8_t alert;
6387
6388 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6389 {
6390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6391 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6392 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6393 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6394 }
6395
6396 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6397 {
6398 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6399 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6400 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6401 }
6402
6403 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6404 {
6405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6406 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6407 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6408 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6409 }
6410
6411 i = mbedtls_ssl_hs_hdr_len( ssl );
6412
6413 /*
6414 * Same message structure as in mbedtls_ssl_write_certificate()
6415 */
6416 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6417
6418 if( ssl->in_msg[i] != 0 ||
6419 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6420 {
6421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6422 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6423 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6424 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6425 }
6426
6427 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6428 i += 3;
6429
6430 /* Iterate through and parse the CRTs in the provided chain. */
6431 while( i < ssl->in_hslen )
6432 {
6433 /* Check that there's room for the next CRT's length fields. */
6434 if ( i + 3 > ssl->in_hslen ) {
6435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6436 mbedtls_ssl_send_alert_message( ssl,
6437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6438 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6439 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6440 }
6441 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6442 * anything beyond 2**16 ~ 64K. */
6443 if( ssl->in_msg[i] != 0 )
6444 {
6445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6446 mbedtls_ssl_send_alert_message( ssl,
6447 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6448 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6449 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6450 }
6451
6452 /* Read length of the next CRT in the chain. */
6453 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6454 | (unsigned int) ssl->in_msg[i + 2];
6455 i += 3;
6456
6457 if( n < 128 || i + n > ssl->in_hslen )
6458 {
6459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6460 mbedtls_ssl_send_alert_message( ssl,
6461 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6462 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6463 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6464 }
6465
6466 /* Check if we're handling the first CRT in the chain. */
6467#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6468 if( crt_cnt++ == 0 &&
6469 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6470 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6471 {
6472 /* During client-side renegotiation, check that the server's
6473 * end-CRTs hasn't changed compared to the initial handshake,
6474 * mitigating the triple handshake attack. On success, reuse
6475 * the original end-CRT instead of parsing it again. */
6476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6477 if( ssl_check_peer_crt_unchanged( ssl,
6478 &ssl->in_msg[i],
6479 n ) != 0 )
6480 {
6481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6482 mbedtls_ssl_send_alert_message( ssl,
6483 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6484 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6485 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6486 }
6487
6488 /* Now we can safely free the original chain. */
6489 ssl_clear_peer_cert( ssl->session );
6490 }
6491#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6492
6493 /* Parse the next certificate in the chain. */
6494#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6495 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6496#else
6497 /* If we don't need to store the CRT chain permanently, parse
6498 * it in-place from the input buffer instead of making a copy. */
6499 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6500#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6501 switch( ret )
6502 {
6503 case 0: /*ok*/
6504 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6505 /* Ignore certificate with an unknown algorithm: maybe a
6506 prior certificate was already trusted. */
6507 break;
6508
6509 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6510 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6511 goto crt_parse_der_failed;
6512
6513 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6514 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6515 goto crt_parse_der_failed;
6516
6517 default:
6518 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6519 crt_parse_der_failed:
6520 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6521 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6522 return( ret );
6523 }
6524
6525 i += n;
6526 }
6527
6528 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6529 return( 0 );
6530}
6531
6532#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006533MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006534static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6535{
6536 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6537 return( -1 );
6538
6539 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6540 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6541 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6542 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6543 {
Ronald Cron19385882022-06-15 16:26:13 +02006544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006545 return( 0 );
6546 }
6547 return( -1 );
6548}
6549#endif /* MBEDTLS_SSL_SRV_C */
6550
6551/* Check if a certificate message is expected.
6552 * Return either
6553 * - SSL_CERTIFICATE_EXPECTED, or
6554 * - SSL_CERTIFICATE_SKIP
6555 * indicating whether a Certificate message is expected or not.
6556 */
6557#define SSL_CERTIFICATE_EXPECTED 0
6558#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006559MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006560static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6561 int authmode )
6562{
6563 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6564 ssl->handshake->ciphersuite_info;
6565
6566 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6567 return( SSL_CERTIFICATE_SKIP );
6568
6569#if defined(MBEDTLS_SSL_SRV_C)
6570 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6571 {
6572 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6573 return( SSL_CERTIFICATE_SKIP );
6574
6575 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6576 {
6577 ssl->session_negotiate->verify_result =
6578 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6579 return( SSL_CERTIFICATE_SKIP );
6580 }
6581 }
6582#else
6583 ((void) authmode);
6584#endif /* MBEDTLS_SSL_SRV_C */
6585
6586 return( SSL_CERTIFICATE_EXPECTED );
6587}
6588
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006589MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006590static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6591 int authmode,
6592 mbedtls_x509_crt *chain,
6593 void *rs_ctx )
6594{
6595 int ret = 0;
6596 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6597 ssl->handshake->ciphersuite_info;
6598 int have_ca_chain = 0;
6599
6600 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6601 void *p_vrfy;
6602
6603 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6604 return( 0 );
6605
6606 if( ssl->f_vrfy != NULL )
6607 {
6608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6609 f_vrfy = ssl->f_vrfy;
6610 p_vrfy = ssl->p_vrfy;
6611 }
6612 else
6613 {
6614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6615 f_vrfy = ssl->conf->f_vrfy;
6616 p_vrfy = ssl->conf->p_vrfy;
6617 }
6618
6619 /*
6620 * Main check: verify certificate
6621 */
6622#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6623 if( ssl->conf->f_ca_cb != NULL )
6624 {
6625 ((void) rs_ctx);
6626 have_ca_chain = 1;
6627
6628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6629 ret = mbedtls_x509_crt_verify_with_ca_cb(
6630 chain,
6631 ssl->conf->f_ca_cb,
6632 ssl->conf->p_ca_cb,
6633 ssl->conf->cert_profile,
6634 ssl->hostname,
6635 &ssl->session_negotiate->verify_result,
6636 f_vrfy, p_vrfy );
6637 }
6638 else
6639#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6640 {
6641 mbedtls_x509_crt *ca_chain;
6642 mbedtls_x509_crl *ca_crl;
6643
6644#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6645 if( ssl->handshake->sni_ca_chain != NULL )
6646 {
6647 ca_chain = ssl->handshake->sni_ca_chain;
6648 ca_crl = ssl->handshake->sni_ca_crl;
6649 }
6650 else
6651#endif
6652 {
6653 ca_chain = ssl->conf->ca_chain;
6654 ca_crl = ssl->conf->ca_crl;
6655 }
6656
6657 if( ca_chain != NULL )
6658 have_ca_chain = 1;
6659
6660 ret = mbedtls_x509_crt_verify_restartable(
6661 chain,
6662 ca_chain, ca_crl,
6663 ssl->conf->cert_profile,
6664 ssl->hostname,
6665 &ssl->session_negotiate->verify_result,
6666 f_vrfy, p_vrfy, rs_ctx );
6667 }
6668
6669 if( ret != 0 )
6670 {
6671 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6672 }
6673
6674#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6675 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6676 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6677#endif
6678
6679 /*
6680 * Secondary checks: always done, but change 'ret' only if it was 0
6681 */
6682
6683#if defined(MBEDTLS_ECP_C)
6684 {
6685 const mbedtls_pk_context *pk = &chain->pk;
6686
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006687 /* If certificate uses an EC key, make sure the curve is OK.
6688 * This is a public key, so it can't be opaque, so can_do() is a good
6689 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006690 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006691 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006692 /* and in the unlikely case the above assumption no longer holds
6693 * we are making sure that pk_ec() here does not return a NULL
6694 */
6695 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6696 if( ec == NULL )
6697 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6700 }
Jerry Yud9526692022-02-17 14:23:47 +08006701
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006702 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6703 {
6704 ssl->session_negotiate->verify_result |=
6705 MBEDTLS_X509_BADCERT_BAD_KEY;
6706
6707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6708 if( ret == 0 )
6709 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6710 }
Jerry Yud9526692022-02-17 14:23:47 +08006711 }
6712 }
6713#endif /* MBEDTLS_ECP_C */
6714
6715 if( mbedtls_ssl_check_cert_usage( chain,
6716 ciphersuite_info,
6717 ! ssl->conf->endpoint,
6718 &ssl->session_negotiate->verify_result ) != 0 )
6719 {
6720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6721 if( ret == 0 )
6722 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6723 }
6724
6725 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6726 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6727 * with details encoded in the verification flags. All other kinds
6728 * of error codes, including those from the user provided f_vrfy
6729 * functions, are treated as fatal and lead to a failure of
6730 * ssl_parse_certificate even if verification was optional. */
6731 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6732 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6733 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6734 {
6735 ret = 0;
6736 }
6737
6738 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6739 {
6740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6741 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6742 }
6743
6744 if( ret != 0 )
6745 {
6746 uint8_t alert;
6747
6748 /* The certificate may have been rejected for several reasons.
6749 Pick one and send the corresponding alert. Which alert to send
6750 may be a subject of debate in some cases. */
6751 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6752 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6753 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6754 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6755 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6756 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6757 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6758 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6759 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6760 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6761 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6762 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6763 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6764 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6765 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6766 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6767 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6768 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6769 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6770 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6771 else
6772 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6774 alert );
6775 }
6776
6777#if defined(MBEDTLS_DEBUG_C)
6778 if( ssl->session_negotiate->verify_result != 0 )
6779 {
6780 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6781 (unsigned int) ssl->session_negotiate->verify_result ) );
6782 }
6783 else
6784 {
6785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6786 }
6787#endif /* MBEDTLS_DEBUG_C */
6788
6789 return( ret );
6790}
6791
6792#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006793MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006794static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6795 unsigned char *start, size_t len )
6796{
6797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6798 /* Remember digest of the peer's end-CRT. */
6799 ssl->session_negotiate->peer_cert_digest =
6800 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6801 if( ssl->session_negotiate->peer_cert_digest == NULL )
6802 {
6803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6804 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6805 mbedtls_ssl_send_alert_message( ssl,
6806 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6807 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6808
6809 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6810 }
6811
6812 ret = mbedtls_md( mbedtls_md_info_from_type(
6813 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6814 start, len,
6815 ssl->session_negotiate->peer_cert_digest );
6816
6817 ssl->session_negotiate->peer_cert_digest_type =
6818 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6819 ssl->session_negotiate->peer_cert_digest_len =
6820 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6821
6822 return( ret );
6823}
6824
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006825MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006826static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6827 unsigned char *start, size_t len )
6828{
6829 unsigned char *end = start + len;
6830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6831
6832 /* Make a copy of the peer's raw public key. */
6833 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6834 ret = mbedtls_pk_parse_subpubkey( &start, end,
6835 &ssl->handshake->peer_pubkey );
6836 if( ret != 0 )
6837 {
6838 /* We should have parsed the public key before. */
6839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6840 }
6841
6842 return( 0 );
6843}
6844#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6845
6846int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6847{
6848 int ret = 0;
6849 int crt_expected;
6850#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6851 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6852 ? ssl->handshake->sni_authmode
6853 : ssl->conf->authmode;
6854#else
6855 const int authmode = ssl->conf->authmode;
6856#endif
6857 void *rs_ctx = NULL;
6858 mbedtls_x509_crt *chain = NULL;
6859
6860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6861
6862 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6863 if( crt_expected == SSL_CERTIFICATE_SKIP )
6864 {
6865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6866 goto exit;
6867 }
6868
6869#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6870 if( ssl->handshake->ecrs_enabled &&
6871 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6872 {
6873 chain = ssl->handshake->ecrs_peer_cert;
6874 ssl->handshake->ecrs_peer_cert = NULL;
6875 goto crt_verify;
6876 }
6877#endif
6878
6879 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6880 {
6881 /* mbedtls_ssl_read_record may have sent an alert already. We
6882 let it decide whether to alert. */
6883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6884 goto exit;
6885 }
6886
6887#if defined(MBEDTLS_SSL_SRV_C)
6888 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6889 {
6890 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6891
6892 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6893 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6894
6895 goto exit;
6896 }
6897#endif /* MBEDTLS_SSL_SRV_C */
6898
6899 /* Clear existing peer CRT structure in case we tried to
6900 * reuse a session but it failed, and allocate a new one. */
6901 ssl_clear_peer_cert( ssl->session_negotiate );
6902
6903 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6904 if( chain == NULL )
6905 {
6906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6907 sizeof( mbedtls_x509_crt ) ) );
6908 mbedtls_ssl_send_alert_message( ssl,
6909 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6910 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6911
6912 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6913 goto exit;
6914 }
6915 mbedtls_x509_crt_init( chain );
6916
6917 ret = ssl_parse_certificate_chain( ssl, chain );
6918 if( ret != 0 )
6919 goto exit;
6920
6921#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6922 if( ssl->handshake->ecrs_enabled)
6923 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6924
6925crt_verify:
6926 if( ssl->handshake->ecrs_enabled)
6927 rs_ctx = &ssl->handshake->ecrs_ctx;
6928#endif
6929
6930 ret = ssl_parse_certificate_verify( ssl, authmode,
6931 chain, rs_ctx );
6932 if( ret != 0 )
6933 goto exit;
6934
6935#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6936 {
6937 unsigned char *crt_start, *pk_start;
6938 size_t crt_len, pk_len;
6939
6940 /* We parse the CRT chain without copying, so
6941 * these pointers point into the input buffer,
6942 * and are hence still valid after freeing the
6943 * CRT chain. */
6944
6945 crt_start = chain->raw.p;
6946 crt_len = chain->raw.len;
6947
6948 pk_start = chain->pk_raw.p;
6949 pk_len = chain->pk_raw.len;
6950
6951 /* Free the CRT structures before computing
6952 * digest and copying the peer's public key. */
6953 mbedtls_x509_crt_free( chain );
6954 mbedtls_free( chain );
6955 chain = NULL;
6956
6957 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6958 if( ret != 0 )
6959 goto exit;
6960
6961 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6962 if( ret != 0 )
6963 goto exit;
6964 }
6965#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6966 /* Pass ownership to session structure. */
6967 ssl->session_negotiate->peer_cert = chain;
6968 chain = NULL;
6969#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6970
6971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6972
6973exit:
6974
6975 if( ret == 0 )
6976 ssl->state++;
6977
6978#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6979 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6980 {
6981 ssl->handshake->ecrs_peer_cert = chain;
6982 chain = NULL;
6983 }
6984#endif
6985
6986 if( chain != NULL )
6987 {
6988 mbedtls_x509_crt_free( chain );
6989 mbedtls_free( chain );
6990 }
6991
6992 return( ret );
6993}
6994#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6995
Andrzej Kurek25f27152022-08-17 16:09:31 -04006996#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006997static void ssl_calc_finished_tls_sha256(
6998 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6999{
7000 int len = 12;
7001 const char *sender;
7002 unsigned char padbuf[32];
7003#if defined(MBEDTLS_USE_PSA_CRYPTO)
7004 size_t hash_size;
7005 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7006 psa_status_t status;
7007#else
7008 mbedtls_sha256_context sha256;
7009#endif
7010
7011 mbedtls_ssl_session *session = ssl->session_negotiate;
7012 if( !session )
7013 session = ssl->session;
7014
7015 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7016 ? "client finished"
7017 : "server finished";
7018
7019#if defined(MBEDTLS_USE_PSA_CRYPTO)
7020 sha256_psa = psa_hash_operation_init();
7021
7022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7023
7024 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7025 if( status != PSA_SUCCESS )
7026 {
7027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7028 return;
7029 }
7030
7031 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7032 if( status != PSA_SUCCESS )
7033 {
7034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7035 return;
7036 }
7037 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7038#else
7039
7040 mbedtls_sha256_init( &sha256 );
7041
7042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7043
7044 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7045
7046 /*
7047 * TLSv1.2:
7048 * hash = PRF( master, finished_label,
7049 * Hash( handshake ) )[0.11]
7050 */
7051
7052#if !defined(MBEDTLS_SHA256_ALT)
7053 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7054 sha256.state, sizeof( sha256.state ) );
7055#endif
7056
7057 mbedtls_sha256_finish( &sha256, padbuf );
7058 mbedtls_sha256_free( &sha256 );
7059#endif /* MBEDTLS_USE_PSA_CRYPTO */
7060
7061 ssl->handshake->tls_prf( session->master, 48, sender,
7062 padbuf, 32, buf, len );
7063
7064 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7065
7066 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7067
7068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7069}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007070#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007071
Jerry Yub7ba49e2022-02-17 14:25:53 +08007072
Andrzej Kurek25f27152022-08-17 16:09:31 -04007073#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007074static void ssl_calc_finished_tls_sha384(
7075 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7076{
7077 int len = 12;
7078 const char *sender;
7079 unsigned char padbuf[48];
7080#if defined(MBEDTLS_USE_PSA_CRYPTO)
7081 size_t hash_size;
7082 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7083 psa_status_t status;
7084#else
7085 mbedtls_sha512_context sha512;
7086#endif
7087
7088 mbedtls_ssl_session *session = ssl->session_negotiate;
7089 if( !session )
7090 session = ssl->session;
7091
7092 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7093 ? "client finished"
7094 : "server finished";
7095
7096#if defined(MBEDTLS_USE_PSA_CRYPTO)
7097 sha384_psa = psa_hash_operation_init();
7098
7099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7100
7101 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7102 if( status != PSA_SUCCESS )
7103 {
7104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7105 return;
7106 }
7107
7108 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7109 if( status != PSA_SUCCESS )
7110 {
7111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7112 return;
7113 }
7114 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7115#else
7116 mbedtls_sha512_init( &sha512 );
7117
7118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7119
Andrzej Kureka242e832022-08-11 10:03:14 -04007120 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007121
7122 /*
7123 * TLSv1.2:
7124 * hash = PRF( master, finished_label,
7125 * Hash( handshake ) )[0.11]
7126 */
7127
7128#if !defined(MBEDTLS_SHA512_ALT)
7129 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7130 sha512.state, sizeof( sha512.state ) );
7131#endif
7132 mbedtls_sha512_finish( &sha512, padbuf );
7133
7134 mbedtls_sha512_free( &sha512 );
7135#endif
7136
7137 ssl->handshake->tls_prf( session->master, 48, sender,
7138 padbuf, 48, buf, len );
7139
7140 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7141
7142 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7143
7144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7145}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007146#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007147
Jerry Yuaef00152022-02-17 14:27:31 +08007148void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7149{
7150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7151
7152 /*
7153 * Free our handshake params
7154 */
7155 mbedtls_ssl_handshake_free( ssl );
7156 mbedtls_free( ssl->handshake );
7157 ssl->handshake = NULL;
7158
7159 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007160 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007161 */
7162 if( ssl->transform )
7163 {
7164 mbedtls_ssl_transform_free( ssl->transform );
7165 mbedtls_free( ssl->transform );
7166 }
7167 ssl->transform = ssl->transform_negotiate;
7168 ssl->transform_negotiate = NULL;
7169
7170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7171}
7172
Jerry Yu2a9fff52022-02-17 14:28:51 +08007173void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7174{
7175 int resume = ssl->handshake->resume;
7176
7177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7178
7179#if defined(MBEDTLS_SSL_RENEGOTIATION)
7180 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7181 {
7182 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7183 ssl->renego_records_seen = 0;
7184 }
7185#endif
7186
7187 /*
7188 * Free the previous session and switch in the current one
7189 */
7190 if( ssl->session )
7191 {
7192#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7193 /* RFC 7366 3.1: keep the EtM state */
7194 ssl->session_negotiate->encrypt_then_mac =
7195 ssl->session->encrypt_then_mac;
7196#endif
7197
7198 mbedtls_ssl_session_free( ssl->session );
7199 mbedtls_free( ssl->session );
7200 }
7201 ssl->session = ssl->session_negotiate;
7202 ssl->session_negotiate = NULL;
7203
7204 /*
7205 * Add cache entry
7206 */
7207 if( ssl->conf->f_set_cache != NULL &&
7208 ssl->session->id_len != 0 &&
7209 resume == 0 )
7210 {
7211 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7212 ssl->session->id,
7213 ssl->session->id_len,
7214 ssl->session ) != 0 )
7215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7216 }
7217
7218#if defined(MBEDTLS_SSL_PROTO_DTLS)
7219 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7220 ssl->handshake->flight != NULL )
7221 {
7222 /* Cancel handshake timer */
7223 mbedtls_ssl_set_timer( ssl, 0 );
7224
7225 /* Keep last flight around in case we need to resend it:
7226 * we need the handshake and transform structures for that */
7227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7228 }
7229 else
7230#endif
7231 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7232
7233 ssl->state++;
7234
7235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7236}
7237
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007238int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7239{
7240 int ret, hash_len;
7241
7242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7243
7244 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7245
7246 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7247
7248 /*
7249 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7250 * may define some other value. Currently (early 2016), no defined
7251 * ciphersuite does this (and this is unlikely to change as activity has
7252 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7253 */
7254 hash_len = 12;
7255
7256#if defined(MBEDTLS_SSL_RENEGOTIATION)
7257 ssl->verify_data_len = hash_len;
7258 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7259#endif
7260
7261 ssl->out_msglen = 4 + hash_len;
7262 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7263 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7264
7265 /*
7266 * In case of session resuming, invert the client and server
7267 * ChangeCipherSpec messages order.
7268 */
7269 if( ssl->handshake->resume != 0 )
7270 {
7271#if defined(MBEDTLS_SSL_CLI_C)
7272 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7273 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7274#endif
7275#if defined(MBEDTLS_SSL_SRV_C)
7276 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7277 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7278#endif
7279 }
7280 else
7281 ssl->state++;
7282
7283 /*
7284 * Switch to our negotiated transform and session parameters for outbound
7285 * data.
7286 */
7287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7288
7289#if defined(MBEDTLS_SSL_PROTO_DTLS)
7290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7291 {
7292 unsigned char i;
7293
7294 /* Remember current epoch settings for resending */
7295 ssl->handshake->alt_transform_out = ssl->transform_out;
7296 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7297 sizeof( ssl->handshake->alt_out_ctr ) );
7298
7299 /* Set sequence_number to zero */
7300 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7301
7302
7303 /* Increment epoch */
7304 for( i = 2; i > 0; i-- )
7305 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7306 break;
7307
7308 /* The loop goes to its end iff the counter is wrapping */
7309 if( i == 0 )
7310 {
7311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7312 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7313 }
7314 }
7315 else
7316#endif /* MBEDTLS_SSL_PROTO_DTLS */
7317 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7318
7319 ssl->transform_out = ssl->transform_negotiate;
7320 ssl->session_out = ssl->session_negotiate;
7321
7322#if defined(MBEDTLS_SSL_PROTO_DTLS)
7323 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7324 mbedtls_ssl_send_flight_completed( ssl );
7325#endif
7326
7327 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7328 {
7329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7330 return( ret );
7331 }
7332
7333#if defined(MBEDTLS_SSL_PROTO_DTLS)
7334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7335 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7336 {
7337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7338 return( ret );
7339 }
7340#endif
7341
7342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7343
7344 return( 0 );
7345}
7346
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007347#define SSL_MAX_HASH_LEN 12
7348
7349int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7350{
7351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7352 unsigned int hash_len = 12;
7353 unsigned char buf[SSL_MAX_HASH_LEN];
7354
7355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7356
7357 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7358
7359 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7360 {
7361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7362 goto exit;
7363 }
7364
7365 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7366 {
7367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7368 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7369 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7370 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7371 goto exit;
7372 }
7373
7374 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7375 {
7376 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7377 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7378 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7379 goto exit;
7380 }
7381
7382 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7383 {
7384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7385 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7386 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7387 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7388 goto exit;
7389 }
7390
7391 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7392 buf, hash_len ) != 0 )
7393 {
7394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7396 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7397 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7398 goto exit;
7399 }
7400
7401#if defined(MBEDTLS_SSL_RENEGOTIATION)
7402 ssl->verify_data_len = hash_len;
7403 memcpy( ssl->peer_verify_data, buf, hash_len );
7404#endif
7405
7406 if( ssl->handshake->resume != 0 )
7407 {
7408#if defined(MBEDTLS_SSL_CLI_C)
7409 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7410 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7411#endif
7412#if defined(MBEDTLS_SSL_SRV_C)
7413 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7414 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7415#endif
7416 }
7417 else
7418 ssl->state++;
7419
7420#if defined(MBEDTLS_SSL_PROTO_DTLS)
7421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7422 mbedtls_ssl_recv_flight_completed( ssl );
7423#endif
7424
7425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7426
7427exit:
7428 mbedtls_platform_zeroize( buf, hash_len );
7429 return( ret );
7430}
7431
Jerry Yu392112c2022-02-17 14:34:10 +08007432#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7433/*
7434 * Helper to get TLS 1.2 PRF from ciphersuite
7435 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7436 */
7437static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7438{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007439#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007440 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7441 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7442
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007443 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007444 return( tls_prf_sha384 );
7445#else
7446 (void) ciphersuite_id;
7447#endif
7448 return( tls_prf_sha256 );
7449}
7450#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007451
7452static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7453{
7454 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007455#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007456 if( tls_prf == tls_prf_sha384 )
7457 {
7458 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7459 }
7460 else
7461#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007462#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007463 if( tls_prf == tls_prf_sha256 )
7464 {
7465 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7466 }
7467 else
7468#endif
7469 return( MBEDTLS_SSL_TLS_PRF_NONE );
7470}
7471
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007472/*
7473 * Populate a transform structure with session keys and all the other
7474 * necessary information.
7475 *
7476 * Parameters:
7477 * - [in/out]: transform: structure to populate
7478 * [in] must be just initialised with mbedtls_ssl_transform_init()
7479 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7480 * - [in] ciphersuite
7481 * - [in] master
7482 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007483 * - [in] tls_prf: pointer to PRF to use for key derivation
7484 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007485 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007486 * - [in] endpoint: client or server
7487 * - [in] ssl: used for:
7488 * - ssl->conf->{f,p}_export_keys
7489 * [in] optionally used for:
7490 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7491 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007492MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007493static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7494 int ciphersuite,
7495 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007496#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007497 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007498#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007499 ssl_tls_prf_t tls_prf,
7500 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007501 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007502 unsigned endpoint,
7503 const mbedtls_ssl_context *ssl )
7504{
7505 int ret = 0;
7506 unsigned char keyblk[256];
7507 unsigned char *key1;
7508 unsigned char *key2;
7509 unsigned char *mac_enc;
7510 unsigned char *mac_dec;
7511 size_t mac_key_len = 0;
7512 size_t iv_copy_len;
7513 size_t keylen;
7514 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007515 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007516#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007517 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007518 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007519#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007520
7521#if defined(MBEDTLS_USE_PSA_CRYPTO)
7522 psa_key_type_t key_type;
7523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7524 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007525 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007526 size_t key_bits;
7527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7528#endif
7529
7530#if !defined(MBEDTLS_DEBUG_C) && \
7531 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7532 if( ssl->f_export_keys == NULL )
7533 {
7534 ssl = NULL; /* make sure we don't use it except for these cases */
7535 (void) ssl;
7536 }
7537#endif
7538
7539 /*
7540 * Some data just needs copying into the structure
7541 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007542#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007543 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007544#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007545 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007546
7547#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7548 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7549#endif
7550
7551#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007552 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007553 {
7554 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7555 * generation separate. This should never happen. */
7556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7557 }
7558#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7559
7560 /*
7561 * Get various info structures
7562 */
7563 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7564 if( ciphersuite_info == NULL )
7565 {
7566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7567 ciphersuite ) );
7568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7569 }
7570
Neil Armstrongab555e02022-04-04 11:07:59 +02007571 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007572#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007573 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007574#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007575 ciphersuite_info );
7576
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007577 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7578 transform->taglen =
7579 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7580
7581#if defined(MBEDTLS_USE_PSA_CRYPTO)
7582 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7583 transform->taglen,
7584 &alg,
7585 &key_type,
7586 &key_bits ) ) != PSA_SUCCESS )
7587 {
7588 ret = psa_ssl_status_to_mbedtls( status );
7589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7590 goto end;
7591 }
7592#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007593 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7594 if( cipher_info == NULL )
7595 {
7596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7597 ciphersuite_info->cipher ) );
7598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7599 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007600#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007601
Neil Armstronge4512952022-03-08 09:08:22 +01007602#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007603 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007604 if( mac_alg == 0 )
7605 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007607 (unsigned) ciphersuite_info->mac ) );
7608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7609 }
7610#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007611 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7612 if( md_info == NULL )
7613 {
7614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7615 (unsigned) ciphersuite_info->mac ) );
7616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7617 }
Neil Armstronge4512952022-03-08 09:08:22 +01007618#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007619
7620#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7621 /* Copy own and peer's CID if the use of the CID
7622 * extension has been negotiated. */
7623 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7624 {
7625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7626
7627 transform->in_cid_len = ssl->own_cid_len;
7628 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7629 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7630 transform->in_cid_len );
7631
7632 transform->out_cid_len = ssl->handshake->peer_cid_len;
7633 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7634 ssl->handshake->peer_cid_len );
7635 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7636 transform->out_cid_len );
7637 }
7638#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7639
7640 /*
7641 * Compute key block using the PRF
7642 */
7643 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7644 if( ret != 0 )
7645 {
7646 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7647 return( ret );
7648 }
7649
7650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7651 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7652 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7653 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7654 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7655
7656 /*
7657 * Determine the appropriate key, IV and MAC length.
7658 */
7659
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007660#if defined(MBEDTLS_USE_PSA_CRYPTO)
7661 keylen = PSA_BITS_TO_BYTES(key_bits);
7662#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007663 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007664#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007665
7666#if defined(MBEDTLS_GCM_C) || \
7667 defined(MBEDTLS_CCM_C) || \
7668 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007669 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007670 {
7671 size_t explicit_ivlen;
7672
7673 transform->maclen = 0;
7674 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007675
7676 /* All modes haves 96-bit IVs, but the length of the static parts vary
7677 * with mode and version:
7678 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7679 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7680 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7681 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7682 * sequence number).
7683 */
7684 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007685#if defined(MBEDTLS_USE_PSA_CRYPTO)
7686 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7687#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007688 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007689#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007690 transform->fixed_ivlen = 12;
7691 else
7692 transform->fixed_ivlen = 4;
7693
7694 /* Minimum length of encrypted record */
7695 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7696 transform->minlen = explicit_ivlen + transform->taglen;
7697 }
7698 else
7699#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7700#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007701 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7702 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7703 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007704 {
Neil Armstronge4512952022-03-08 09:08:22 +01007705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007706 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007707#else
7708 size_t block_size = cipher_info->block_size;
7709#endif /* MBEDTLS_USE_PSA_CRYPTO */
7710
7711#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007712 /* Get MAC length */
7713 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7714#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007715 /* Initialize HMAC contexts */
7716 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7717 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7718 {
7719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7720 goto end;
7721 }
7722
7723 /* Get MAC length */
7724 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007725#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007726 transform->maclen = mac_key_len;
7727
7728 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007729#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007730 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007731#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007732 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007733#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007734
7735 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007736 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007737 transform->minlen = transform->maclen;
7738 else
7739 {
7740 /*
7741 * GenericBlockCipher:
7742 * 1. if EtM is in use: one block plus MAC
7743 * otherwise: * first multiple of blocklen greater than maclen
7744 * 2. IV
7745 */
7746#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007747 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007748 {
7749 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007750 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007751 }
7752 else
7753#endif
7754 {
7755 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007756 + block_size
7757 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007758 }
7759
Glenn Strauss07c64162022-03-14 12:34:51 -04007760 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007761 {
7762 transform->minlen += transform->ivlen;
7763 }
7764 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007765 {
7766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7767 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7768 goto end;
7769 }
7770 }
7771 }
7772 else
7773#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7774 {
7775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7776 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7777 }
7778
7779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7780 (unsigned) keylen,
7781 (unsigned) transform->minlen,
7782 (unsigned) transform->ivlen,
7783 (unsigned) transform->maclen ) );
7784
7785 /*
7786 * Finally setup the cipher contexts, IVs and MAC secrets.
7787 */
7788#if defined(MBEDTLS_SSL_CLI_C)
7789 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7790 {
7791 key1 = keyblk + mac_key_len * 2;
7792 key2 = keyblk + mac_key_len * 2 + keylen;
7793
7794 mac_enc = keyblk;
7795 mac_dec = keyblk + mac_key_len;
7796
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007797 iv_copy_len = ( transform->fixed_ivlen ) ?
7798 transform->fixed_ivlen : transform->ivlen;
7799 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7800 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7801 iv_copy_len );
7802 }
7803 else
7804#endif /* MBEDTLS_SSL_CLI_C */
7805#if defined(MBEDTLS_SSL_SRV_C)
7806 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7807 {
7808 key1 = keyblk + mac_key_len * 2 + keylen;
7809 key2 = keyblk + mac_key_len * 2;
7810
7811 mac_enc = keyblk + mac_key_len;
7812 mac_dec = keyblk;
7813
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007814 iv_copy_len = ( transform->fixed_ivlen ) ?
7815 transform->fixed_ivlen : transform->ivlen;
7816 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7817 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7818 iv_copy_len );
7819 }
7820 else
7821#endif /* MBEDTLS_SSL_SRV_C */
7822 {
7823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7824 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7825 goto end;
7826 }
7827
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007828 if( ssl != NULL && ssl->f_export_keys != NULL )
7829 {
7830 ssl->f_export_keys( ssl->p_export_keys,
7831 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7832 master, 48,
7833 randbytes + 32,
7834 randbytes,
7835 tls_prf_get_type( tls_prf ) );
7836 }
7837
7838#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007839 transform->psa_alg = alg;
7840
7841 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7842 {
7843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7844 psa_set_key_algorithm( &attributes, alg );
7845 psa_set_key_type( &attributes, key_type );
7846
7847 if( ( status = psa_import_key( &attributes,
7848 key1,
7849 PSA_BITS_TO_BYTES( key_bits ),
7850 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7851 {
7852 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7853 ret = psa_ssl_status_to_mbedtls( status );
7854 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7855 goto end;
7856 }
7857
7858 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7859
7860 if( ( status = psa_import_key( &attributes,
7861 key2,
7862 PSA_BITS_TO_BYTES( key_bits ),
7863 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7864 {
7865 ret = psa_ssl_status_to_mbedtls( status );
7866 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7867 goto end;
7868 }
7869 }
7870#else
7871 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7872 cipher_info ) ) != 0 )
7873 {
7874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7875 goto end;
7876 }
7877
7878 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7879 cipher_info ) ) != 0 )
7880 {
7881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7882 goto end;
7883 }
7884
7885 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7886 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7887 MBEDTLS_ENCRYPT ) ) != 0 )
7888 {
7889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7890 goto end;
7891 }
7892
7893 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7894 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7895 MBEDTLS_DECRYPT ) ) != 0 )
7896 {
7897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7898 goto end;
7899 }
7900
7901#if defined(MBEDTLS_CIPHER_MODE_CBC)
7902 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7903 {
7904 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7905 MBEDTLS_PADDING_NONE ) ) != 0 )
7906 {
7907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7908 goto end;
7909 }
7910
7911 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7912 MBEDTLS_PADDING_NONE ) ) != 0 )
7913 {
7914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7915 goto end;
7916 }
7917 }
7918#endif /* MBEDTLS_CIPHER_MODE_CBC */
7919#endif /* MBEDTLS_USE_PSA_CRYPTO */
7920
Neil Armstrong29c0c042022-03-17 17:47:28 +01007921#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7922 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7923 For AEAD-based ciphersuites, there is nothing to do here. */
7924 if( mac_key_len != 0 )
7925 {
7926#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007927 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007928
7929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007930 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007931 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7932
7933 if( ( status = psa_import_key( &attributes,
7934 mac_enc, mac_key_len,
7935 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7936 {
7937 ret = psa_ssl_status_to_mbedtls( status );
7938 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7939 goto end;
7940 }
7941
Ronald Cronfb39f152022-03-25 14:36:28 +01007942 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007943 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7944#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7945 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7946#endif
7947 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007948 /* mbedtls_ct_hmac() requires the key to be exportable */
7949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7950 PSA_KEY_USAGE_VERIFY_HASH );
7951 else
7952 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7953
7954 if( ( status = psa_import_key( &attributes,
7955 mac_dec, mac_key_len,
7956 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7957 {
7958 ret = psa_ssl_status_to_mbedtls( status );
7959 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7960 goto end;
7961 }
7962#else
7963 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7964 if( ret != 0 )
7965 goto end;
7966 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7967 if( ret != 0 )
7968 goto end;
7969#endif /* MBEDTLS_USE_PSA_CRYPTO */
7970 }
7971#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7972
7973 ((void) mac_dec);
7974 ((void) mac_enc);
7975
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007976end:
7977 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7978 return( ret );
7979}
7980
Jerry Yuee40f9d2022-02-17 14:55:16 +08007981#if defined(MBEDTLS_USE_PSA_CRYPTO)
7982int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7983 unsigned char *hash, size_t *hashlen,
7984 unsigned char *data, size_t data_len,
7985 mbedtls_md_type_t md_alg )
7986{
7987 psa_status_t status;
7988 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007989 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007990
7991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7992
7993 if( ( status = psa_hash_setup( &hash_operation,
7994 hash_alg ) ) != PSA_SUCCESS )
7995 {
7996 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7997 goto exit;
7998 }
7999
8000 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8001 64 ) ) != PSA_SUCCESS )
8002 {
8003 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8004 goto exit;
8005 }
8006
8007 if( ( status = psa_hash_update( &hash_operation,
8008 data, data_len ) ) != PSA_SUCCESS )
8009 {
8010 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8011 goto exit;
8012 }
8013
8014 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8015 hashlen ) ) != PSA_SUCCESS )
8016 {
8017 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8018 goto exit;
8019 }
8020
8021exit:
8022 if( status != PSA_SUCCESS )
8023 {
8024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8025 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8026 switch( status )
8027 {
8028 case PSA_ERROR_NOT_SUPPORTED:
8029 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8030 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8031 case PSA_ERROR_BUFFER_TOO_SMALL:
8032 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8033 case PSA_ERROR_INSUFFICIENT_MEMORY:
8034 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8035 default:
8036 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8037 }
8038 }
8039 return( 0 );
8040}
8041
8042#else
8043
8044int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8045 unsigned char *hash, size_t *hashlen,
8046 unsigned char *data, size_t data_len,
8047 mbedtls_md_type_t md_alg )
8048{
8049 int ret = 0;
8050 mbedtls_md_context_t ctx;
8051 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8052 *hashlen = mbedtls_md_get_size( md_info );
8053
8054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8055
8056 mbedtls_md_init( &ctx );
8057
8058 /*
8059 * digitally-signed struct {
8060 * opaque client_random[32];
8061 * opaque server_random[32];
8062 * ServerDHParams params;
8063 * };
8064 */
8065 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8066 {
8067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8068 goto exit;
8069 }
8070 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8071 {
8072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8073 goto exit;
8074 }
8075 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8076 {
8077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8078 goto exit;
8079 }
8080 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8081 {
8082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8083 goto exit;
8084 }
8085 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8086 {
8087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8088 goto exit;
8089 }
8090
8091exit:
8092 mbedtls_md_free( &ctx );
8093
8094 if( ret != 0 )
8095 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8096 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8097
8098 return( ret );
8099}
8100#endif /* MBEDTLS_USE_PSA_CRYPTO */
8101
Jerry Yud9d91da2022-02-17 14:57:06 +08008102#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8103
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008104/* Find the preferred hash for a given signature algorithm. */
8105unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8106 mbedtls_ssl_context *ssl,
8107 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008108{
Gabor Mezei078e8032022-04-27 21:17:56 +02008109 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008110 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008111
8112 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008113 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008114
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008115 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008116 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008117 unsigned int hash_alg_received =
8118 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8119 received_sig_algs[i] );
8120 unsigned int sig_alg_received =
8121 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8122 received_sig_algs[i] );
8123
8124 if( sig_alg == sig_alg_received )
8125 {
8126#if defined(MBEDTLS_USE_PSA_CRYPTO)
8127 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8128 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008129 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008130 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008131
Neil Armstrong96eceb82022-06-30 18:05:05 +02008132 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8133 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8134 PSA_ALG_ECDSA( psa_hash_alg ),
8135 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008136 continue;
8137
Neil Armstrong96eceb82022-06-30 18:05:05 +02008138 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008139 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8140 PSA_ALG_RSA_PKCS1V15_SIGN(
8141 psa_hash_alg ),
8142 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008143 continue;
8144 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008145#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008146
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008147 return( hash_alg_received );
8148 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008149 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008150
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008151 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008152}
8153
8154#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8155
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008156/* Serialization of TLS 1.2 sessions:
8157 *
8158 * struct {
8159 * uint64 start_time;
8160 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008161 * uint8 session_id_len; // at most 32
8162 * opaque session_id[32];
8163 * opaque master[48]; // fixed length in the standard
8164 * uint32 verify_result;
8165 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8166 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8167 * uint32 ticket_lifetime;
8168 * uint8 mfl_code; // up to 255 according to standard
8169 * uint8 encrypt_then_mac; // 0 or 1
8170 * } serialized_session_tls12;
8171 *
8172 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008173static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008174 unsigned char *buf,
8175 size_t buf_len )
8176{
8177 unsigned char *p = buf;
8178 size_t used = 0;
8179
8180#if defined(MBEDTLS_HAVE_TIME)
8181 uint64_t start;
8182#endif
8183#if defined(MBEDTLS_X509_CRT_PARSE_C)
8184#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8185 size_t cert_len;
8186#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8187#endif /* MBEDTLS_X509_CRT_PARSE_C */
8188
8189 /*
8190 * Time
8191 */
8192#if defined(MBEDTLS_HAVE_TIME)
8193 used += 8;
8194
8195 if( used <= buf_len )
8196 {
8197 start = (uint64_t) session->start;
8198
8199 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8200 p += 8;
8201 }
8202#endif /* MBEDTLS_HAVE_TIME */
8203
8204 /*
8205 * Basic mandatory fields
8206 */
8207 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008208 + 1 /* id_len */
8209 + sizeof( session->id )
8210 + sizeof( session->master )
8211 + 4; /* verify_result */
8212
8213 if( used <= buf_len )
8214 {
8215 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8216 p += 2;
8217
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008218 *p++ = MBEDTLS_BYTE_0( session->id_len );
8219 memcpy( p, session->id, 32 );
8220 p += 32;
8221
8222 memcpy( p, session->master, 48 );
8223 p += 48;
8224
8225 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8226 p += 4;
8227 }
8228
8229 /*
8230 * Peer's end-entity certificate
8231 */
8232#if defined(MBEDTLS_X509_CRT_PARSE_C)
8233#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8234 if( session->peer_cert == NULL )
8235 cert_len = 0;
8236 else
8237 cert_len = session->peer_cert->raw.len;
8238
8239 used += 3 + cert_len;
8240
8241 if( used <= buf_len )
8242 {
8243 *p++ = MBEDTLS_BYTE_2( cert_len );
8244 *p++ = MBEDTLS_BYTE_1( cert_len );
8245 *p++ = MBEDTLS_BYTE_0( cert_len );
8246
8247 if( session->peer_cert != NULL )
8248 {
8249 memcpy( p, session->peer_cert->raw.p, cert_len );
8250 p += cert_len;
8251 }
8252 }
8253#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8254 if( session->peer_cert_digest != NULL )
8255 {
8256 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8257 if( used <= buf_len )
8258 {
8259 *p++ = (unsigned char) session->peer_cert_digest_type;
8260 *p++ = (unsigned char) session->peer_cert_digest_len;
8261 memcpy( p, session->peer_cert_digest,
8262 session->peer_cert_digest_len );
8263 p += session->peer_cert_digest_len;
8264 }
8265 }
8266 else
8267 {
8268 used += 2;
8269 if( used <= buf_len )
8270 {
8271 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8272 *p++ = 0;
8273 }
8274 }
8275#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8276#endif /* MBEDTLS_X509_CRT_PARSE_C */
8277
8278 /*
8279 * Session ticket if any, plus associated data
8280 */
8281#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8282 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8283
8284 if( used <= buf_len )
8285 {
8286 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8287 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8288 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8289
8290 if( session->ticket != NULL )
8291 {
8292 memcpy( p, session->ticket, session->ticket_len );
8293 p += session->ticket_len;
8294 }
8295
8296 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8297 p += 4;
8298 }
8299#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8300
8301 /*
8302 * Misc extension-related info
8303 */
8304#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8305 used += 1;
8306
8307 if( used <= buf_len )
8308 *p++ = session->mfl_code;
8309#endif
8310
8311#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8312 used += 1;
8313
8314 if( used <= buf_len )
8315 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8316#endif
8317
8318 return( used );
8319}
8320
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008321MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008322static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008323 const unsigned char *buf,
8324 size_t len )
8325{
8326#if defined(MBEDTLS_HAVE_TIME)
8327 uint64_t start;
8328#endif
8329#if defined(MBEDTLS_X509_CRT_PARSE_C)
8330#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8331 size_t cert_len;
8332#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8333#endif /* MBEDTLS_X509_CRT_PARSE_C */
8334
8335 const unsigned char *p = buf;
8336 const unsigned char * const end = buf + len;
8337
8338 /*
8339 * Time
8340 */
8341#if defined(MBEDTLS_HAVE_TIME)
8342 if( 8 > (size_t)( end - p ) )
8343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8344
8345 start = ( (uint64_t) p[0] << 56 ) |
8346 ( (uint64_t) p[1] << 48 ) |
8347 ( (uint64_t) p[2] << 40 ) |
8348 ( (uint64_t) p[3] << 32 ) |
8349 ( (uint64_t) p[4] << 24 ) |
8350 ( (uint64_t) p[5] << 16 ) |
8351 ( (uint64_t) p[6] << 8 ) |
8352 ( (uint64_t) p[7] );
8353 p += 8;
8354
8355 session->start = (time_t) start;
8356#endif /* MBEDTLS_HAVE_TIME */
8357
8358 /*
8359 * Basic mandatory fields
8360 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008361 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8363
8364 session->ciphersuite = ( p[0] << 8 ) | p[1];
8365 p += 2;
8366
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008367 session->id_len = *p++;
8368 memcpy( session->id, p, 32 );
8369 p += 32;
8370
8371 memcpy( session->master, p, 48 );
8372 p += 48;
8373
8374 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8375 ( (uint32_t) p[1] << 16 ) |
8376 ( (uint32_t) p[2] << 8 ) |
8377 ( (uint32_t) p[3] );
8378 p += 4;
8379
8380 /* Immediately clear invalid pointer values that have been read, in case
8381 * we exit early before we replaced them with valid ones. */
8382#if defined(MBEDTLS_X509_CRT_PARSE_C)
8383#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8384 session->peer_cert = NULL;
8385#else
8386 session->peer_cert_digest = NULL;
8387#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8388#endif /* MBEDTLS_X509_CRT_PARSE_C */
8389#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8390 session->ticket = NULL;
8391#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8392
8393 /*
8394 * Peer certificate
8395 */
8396#if defined(MBEDTLS_X509_CRT_PARSE_C)
8397#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8398 /* Deserialize CRT from the end of the ticket. */
8399 if( 3 > (size_t)( end - p ) )
8400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8401
8402 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8403 p += 3;
8404
8405 if( cert_len != 0 )
8406 {
8407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8408
8409 if( cert_len > (size_t)( end - p ) )
8410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8411
8412 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8413
8414 if( session->peer_cert == NULL )
8415 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8416
8417 mbedtls_x509_crt_init( session->peer_cert );
8418
8419 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8420 p, cert_len ) ) != 0 )
8421 {
8422 mbedtls_x509_crt_free( session->peer_cert );
8423 mbedtls_free( session->peer_cert );
8424 session->peer_cert = NULL;
8425 return( ret );
8426 }
8427
8428 p += cert_len;
8429 }
8430#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8431 /* Deserialize CRT digest from the end of the ticket. */
8432 if( 2 > (size_t)( end - p ) )
8433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8434
8435 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8436 session->peer_cert_digest_len = (size_t) *p++;
8437
8438 if( session->peer_cert_digest_len != 0 )
8439 {
8440 const mbedtls_md_info_t *md_info =
8441 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8442 if( md_info == NULL )
8443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8444 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8446
8447 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8449
8450 session->peer_cert_digest =
8451 mbedtls_calloc( 1, session->peer_cert_digest_len );
8452 if( session->peer_cert_digest == NULL )
8453 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8454
8455 memcpy( session->peer_cert_digest, p,
8456 session->peer_cert_digest_len );
8457 p += session->peer_cert_digest_len;
8458 }
8459#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8460#endif /* MBEDTLS_X509_CRT_PARSE_C */
8461
8462 /*
8463 * Session ticket and associated data
8464 */
8465#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8466 if( 3 > (size_t)( end - p ) )
8467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8468
8469 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8470 p += 3;
8471
8472 if( session->ticket_len != 0 )
8473 {
8474 if( session->ticket_len > (size_t)( end - p ) )
8475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8476
8477 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8478 if( session->ticket == NULL )
8479 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8480
8481 memcpy( session->ticket, p, session->ticket_len );
8482 p += session->ticket_len;
8483 }
8484
8485 if( 4 > (size_t)( end - p ) )
8486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8487
8488 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8489 ( (uint32_t) p[1] << 16 ) |
8490 ( (uint32_t) p[2] << 8 ) |
8491 ( (uint32_t) p[3] );
8492 p += 4;
8493#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8494
8495 /*
8496 * Misc extension-related info
8497 */
8498#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8499 if( 1 > (size_t)( end - p ) )
8500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8501
8502 session->mfl_code = *p++;
8503#endif
8504
8505#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8506 if( 1 > (size_t)( end - p ) )
8507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8508
8509 session->encrypt_then_mac = *p++;
8510#endif
8511
8512 /* Done, should have consumed entire buffer */
8513 if( p != end )
8514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8515
8516 return( 0 );
8517}
Jerry Yudc7bd172022-02-17 13:44:15 +08008518#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8519
XiaokangQian75d40ef2022-04-20 11:05:24 +00008520int mbedtls_ssl_validate_ciphersuite(
8521 const mbedtls_ssl_context *ssl,
8522 const mbedtls_ssl_ciphersuite_t *suite_info,
8523 mbedtls_ssl_protocol_version min_tls_version,
8524 mbedtls_ssl_protocol_version max_tls_version )
8525{
8526 (void) ssl;
8527
8528 if( suite_info == NULL )
8529 return( -1 );
8530
8531 if( ( suite_info->min_tls_version > max_tls_version ) ||
8532 ( suite_info->max_tls_version < min_tls_version ) )
8533 {
8534 return( -1 );
8535 }
8536
XiaokangQian060d8672022-04-21 09:24:56 +00008537#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008538#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8539 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8540 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8541 {
8542 return( -1 );
8543 }
8544#endif
8545
8546 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8547#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8548 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8549 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8550 {
8551 return( -1 );
8552 }
8553#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8555
8556 return( 0 );
8557}
8558
XiaokangQianeaf36512022-04-24 09:07:44 +00008559#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8560/*
8561 * Function for writing a signature algorithm extension.
8562 *
8563 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8564 * value (TLS 1.3 RFC8446):
8565 * enum {
8566 * ....
8567 * ecdsa_secp256r1_sha256( 0x0403 ),
8568 * ecdsa_secp384r1_sha384( 0x0503 ),
8569 * ecdsa_secp521r1_sha512( 0x0603 ),
8570 * ....
8571 * } SignatureScheme;
8572 *
8573 * struct {
8574 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8575 * } SignatureSchemeList;
8576 *
8577 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8578 * value (TLS 1.2 RFC5246):
8579 * enum {
8580 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8581 * sha512(6), (255)
8582 * } HashAlgorithm;
8583 *
8584 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8585 * SignatureAlgorithm;
8586 *
8587 * struct {
8588 * HashAlgorithm hash;
8589 * SignatureAlgorithm signature;
8590 * } SignatureAndHashAlgorithm;
8591 *
8592 * SignatureAndHashAlgorithm
8593 * supported_signature_algorithms<2..2^16-2>;
8594 *
8595 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8596 * generalization of the TLS 1.2 signature algorithm extension.
8597 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8598 * `SignatureScheme` field of TLS 1.3
8599 *
8600 */
8601int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8602 const unsigned char *end, size_t *out_len )
8603{
8604 unsigned char *p = buf;
8605 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8606 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8607
8608 *out_len = 0;
8609
8610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8611
8612 /* Check if we have space for header and length field:
8613 * - extension_type (2 bytes)
8614 * - extension_data_length (2 bytes)
8615 * - supported_signature_algorithms_length (2 bytes)
8616 */
8617 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8618 p += 6;
8619
8620 /*
8621 * Write supported_signature_algorithms
8622 */
8623 supported_sig_alg = p;
8624 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8625 if( sig_alg == NULL )
8626 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8627
8628 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8629 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8631 *sig_alg,
8632 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008633 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8634 continue;
8635 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8636 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8637 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008639 *sig_alg,
8640 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008641 }
8642
8643 /* Length of supported_signature_algorithms */
8644 supported_sig_alg_len = p - supported_sig_alg;
8645 if( supported_sig_alg_len == 0 )
8646 {
8647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8648 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8649 }
8650
XiaokangQianeaf36512022-04-24 09:07:44 +00008651 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008652 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008653 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8654
XiaokangQianeaf36512022-04-24 09:07:44 +00008655 *out_len = p - buf;
8656
8657#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8658 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8659#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8660 return( 0 );
8661}
8662#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8663
XiaokangQian40a35232022-05-07 09:02:40 +00008664#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008665/*
8666 * mbedtls_ssl_parse_server_name_ext
8667 *
8668 * Structure of server_name extension:
8669 *
8670 * enum {
8671 * host_name(0), (255)
8672 * } NameType;
8673 * opaque HostName<1..2^16-1>;
8674 *
8675 * struct {
8676 * NameType name_type;
8677 * select (name_type) {
8678 * case host_name: HostName;
8679 * } name;
8680 * } ServerName;
8681 * struct {
8682 * ServerName server_name_list<1..2^16-1>
8683 * } ServerNameList;
8684 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008685MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008686int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8687 const unsigned char *buf,
8688 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008689{
8690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8691 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008692 size_t server_name_list_len, hostname_len;
8693 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008694
XiaokangQianf2a94202022-05-20 06:44:24 +00008695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008696
8697 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008698 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008699 p += 2;
8700
XiaokangQian9b2b7712022-05-17 02:57:00 +00008701 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8702 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008703 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008704 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008705 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008706 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008707 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8708 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008709
8710 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8711 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008712 /* sni_name is intended to be used only during the parsing of the
8713 * ClientHello message (it is reset to NULL before the end of
8714 * the message parsing). Thus it is ok to just point to the
8715 * reception buffer and not make a copy of it.
8716 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008717 ssl->handshake->sni_name = p + 3;
8718 ssl->handshake->sni_name_len = hostname_len;
8719 if( ssl->conf->f_sni == NULL )
8720 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008721 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008722 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008723 if( ret != 0 )
8724 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008725 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008726 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8727 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008728 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8729 }
8730 return( 0 );
8731 }
8732
8733 p += hostname_len + 3;
8734 }
8735
8736 return( 0 );
8737}
8738#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8739
XiaokangQianacb39922022-06-17 10:18:48 +00008740#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008741MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008742int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8743 const unsigned char *buf,
8744 const unsigned char *end )
8745{
8746 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008747 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008748 const unsigned char *protocol_name_list;
8749 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008750 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008751
8752 /* If ALPN not configured, just ignore the extension */
8753 if( ssl->conf->alpn_list == NULL )
8754 return( 0 );
8755
8756 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008757 * RFC7301, section 3.1
8758 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008759 *
XiaokangQianc7403452022-06-23 03:24:12 +00008760 * struct {
8761 * ProtocolName protocol_name_list<2..2^16-1>
8762 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008763 */
8764
XiaokangQianc7403452022-06-23 03:24:12 +00008765 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008766 * protocol_name_list_len 2 bytes
8767 * protocol_name_len 1 bytes
8768 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008769 */
XiaokangQianacb39922022-06-17 10:18:48 +00008770 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8771
XiaokangQianc7403452022-06-23 03:24:12 +00008772 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008773 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008774 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008775 protocol_name_list = p;
8776 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008777
8778 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008779 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008780 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008781 protocol_name_len = *p++;
8782 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8783 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008784 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008785 {
8786 MBEDTLS_SSL_PEND_FATAL_ALERT(
8787 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8788 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008789 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008790 }
8791
8792 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008793 }
8794
8795 /* Use our order of preference */
8796 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8797 {
8798 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008799 p = protocol_name_list;
8800 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008801 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008802 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008803 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008804 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008805 {
8806 ssl->alpn_chosen = *alpn;
8807 return( 0 );
8808 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008809
8810 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008811 }
8812 }
8813
XiaokangQian95d5f542022-06-24 02:29:26 +00008814 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008815 MBEDTLS_SSL_PEND_FATAL_ALERT(
8816 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8817 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8818 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8819}
8820
8821int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8822 unsigned char *buf,
8823 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008824 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008825{
8826 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008827 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008828 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008829
8830 if( ssl->alpn_chosen == NULL )
8831 {
8832 return( 0 );
8833 }
8834
XiaokangQian95d5f542022-06-24 02:29:26 +00008835 protocol_name_len = strlen( ssl->alpn_chosen );
8836 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008837
8838 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8839 /*
8840 * 0 . 1 ext identifier
8841 * 2 . 3 ext length
8842 * 4 . 5 protocol list length
8843 * 6 . 6 protocol name length
8844 * 7 . 7+n protocol name
8845 */
8846 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8847
XiaokangQian95d5f542022-06-24 02:29:26 +00008848 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008849
XiaokangQian95d5f542022-06-24 02:29:26 +00008850 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8851 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008852 /* Note: the length of the chosen protocol has been checked to be less
8853 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8854 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008855 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008856
XiaokangQian95d5f542022-06-24 02:29:26 +00008857 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008858 return ( 0 );
8859}
8860#endif /* MBEDTLS_SSL_ALPN */
8861
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008862#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008863 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008864 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008865 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008866int 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}
Xiaokang Qian03409292022-10-12 02:49:52 +00008907#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8908 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008909 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008910 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#endif /* MBEDTLS_SSL_TLS_C */