blob: 6a0a67800c074f9c625534b166999c8802dd1142 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020057#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254 if( src->peer_cert != NULL )
255 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200257
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200258 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200265 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 dst->peer_cert = NULL;
269 return( ret );
270 }
271 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 if( src->peer_cert_digest != NULL )
274 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 if( dst->peer_cert_digest == NULL )
278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
279
280 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
281 src->peer_cert_digest_len );
282 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000283 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 }
285#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200289#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290 if( src->ticket != NULL )
291 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200293 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 memcpy( dst->ticket, src->ticket, src->ticket_len );
297 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +0000300#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
301 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT && src->hostname != NULL )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000302 {
Xiaokang Qianecd75282022-09-28 07:11:02 +0000303 dst->hostname = mbedtls_calloc( 1, src->hostname_len );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000304 if( dst->hostname == NULL )
305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
306
307 memcpy( dst->hostname, src->hostname, src->hostname_len );
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +0000308 dst->hostname_len = src->hostname_len;
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000309 }
310#endif
311
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200312 return( 0 );
313}
314
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500315#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200316MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500317static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
318{
319 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
320 if( resized_buffer == NULL )
321 return -1;
322
323 /* We want to copy len_new bytes when downsizing the buffer, and
324 * len_old bytes when upsizing, so we choose the smaller of two sizes,
325 * to fit one buffer into another. Size checks, ensuring that no data is
326 * lost, are done outside of this function. */
327 memcpy( resized_buffer, *buffer,
328 ( len_new < *len_old ) ? len_new : *len_old );
329 mbedtls_platform_zeroize( *buffer, *len_old );
330 mbedtls_free( *buffer );
331
332 *buffer = resized_buffer;
333 *len_old = len_new;
334
335 return 0;
336}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337
338static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500339 size_t in_buf_new_len,
340 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341{
342 int modified = 0;
343 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
344 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
345 if( ssl->in_buf != NULL )
346 {
347 written_in = ssl->in_msg - ssl->in_buf;
348 iv_offset_in = ssl->in_iv - ssl->in_buf;
349 len_offset_in = ssl->in_len - ssl->in_buf;
350 if( downsizing ?
351 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
352 ssl->in_buf_len < in_buf_new_len )
353 {
354 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
355 {
356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
357 }
358 else
359 {
Paul Elliottb7449902021-03-10 18:14:58 +0000360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
361 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200362 modified = 1;
363 }
364 }
365 }
366
367 if( ssl->out_buf != NULL )
368 {
369 written_out = ssl->out_msg - ssl->out_buf;
370 iv_offset_out = ssl->out_iv - ssl->out_buf;
371 len_offset_out = ssl->out_len - ssl->out_buf;
372 if( downsizing ?
373 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
374 ssl->out_buf_len < out_buf_new_len )
375 {
376 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
377 {
378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
379 }
380 else
381 {
Paul Elliottb7449902021-03-10 18:14:58 +0000382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
383 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200384 modified = 1;
385 }
386 }
387 }
388 if( modified )
389 {
390 /* Update pointers here to avoid doing it twice. */
391 mbedtls_ssl_reset_in_out_pointers( ssl );
392 /* Fields below might not be properly updated with record
393 * splitting or with CID, so they are manually updated here. */
394 ssl->out_msg = ssl->out_buf + written_out;
395 ssl->out_len = ssl->out_buf + len_offset_out;
396 ssl->out_iv = ssl->out_buf + iv_offset_out;
397
398 ssl->in_msg = ssl->in_buf + written_in;
399 ssl->in_len = ssl->in_buf + len_offset_in;
400 ssl->in_iv = ssl->in_buf + iv_offset_in;
401 }
402}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500403#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
404
Jerry Yudb8c48a2022-01-27 14:54:54 +0800405#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800406
407#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
408typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
409 const char *label,
410 const unsigned char *random, size_t rlen,
411 unsigned char *dstbuf, size_t dlen );
412
413static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
414
415#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
416
417/* Type for the TLS PRF */
418typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
419 const unsigned char *, size_t,
420 unsigned char *, size_t);
421
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200422MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800423static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
424 int ciphersuite,
425 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200426#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800427 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200428#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800429 ssl_tls_prf_t tls_prf,
430 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400431 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800432 unsigned endpoint,
433 const mbedtls_ssl_context *ssl );
434
Andrzej Kurek25f27152022-08-17 16:09:31 -0400435#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200436MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800437static int tls_prf_sha256( const unsigned char *secret, size_t slen,
438 const char *label,
439 const unsigned char *random, size_t rlen,
440 unsigned char *dstbuf, size_t dlen );
441static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
442static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
443
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400444#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800445
Andrzej Kurek25f27152022-08-17 16:09:31 -0400446#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200447MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800448static int tls_prf_sha384( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
451 unsigned char *dstbuf, size_t dlen );
452
453static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
454static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800456
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 unsigned char *buf,
459 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800460
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200461MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000462static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800463 const unsigned char *buf,
464 size_t len );
465#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
466
Jerry Yu53d23e22022-02-09 16:25:09 +0800467static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
468
Andrzej Kurek25f27152022-08-17 16:09:31 -0400469#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800470static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800472
Andrzej Kurek25f27152022-08-17 16:09:31 -0400473#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800474static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400475#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000476
Ron Eldor51d3ab52019-05-12 14:54:30 +0300477int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
478 const unsigned char *secret, size_t slen,
479 const char *label,
480 const unsigned char *random, size_t rlen,
481 unsigned char *dstbuf, size_t dlen )
482{
483 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
484
485 switch( prf )
486 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300487#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA384:
490 tls_prf = tls_prf_sha384;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400493#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 case MBEDTLS_SSL_TLS_PRF_SHA256:
495 tls_prf = tls_prf_sha256;
496 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400497#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300498#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300499 default:
500 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
501 }
502
503 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
504}
505
Jerry Yuc73c6182022-02-08 20:29:25 +0800506#if defined(MBEDTLS_X509_CRT_PARSE_C)
507static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
508{
509#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
510 if( session->peer_cert != NULL )
511 {
512 mbedtls_x509_crt_free( session->peer_cert );
513 mbedtls_free( session->peer_cert );
514 session->peer_cert = NULL;
515 }
516#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
517 if( session->peer_cert_digest != NULL )
518 {
519 /* Zeroization is not necessary. */
520 mbedtls_free( session->peer_cert_digest );
521 session->peer_cert_digest = NULL;
522 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
523 session->peer_cert_digest_len = 0;
524 }
525#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
526}
527#endif /* MBEDTLS_X509_CRT_PARSE_C */
528
529void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
530 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
531{
532 ((void) ciphersuite_info);
533
Andrzej Kurek25f27152022-08-17 16:09:31 -0400534#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800535 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
536 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
537 else
538#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400539#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800540 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
541 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
542 else
543#endif
544 {
545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
546 return;
547 }
548}
549
XiaokangQianadab9a62022-07-18 07:41:26 +0000550void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
551 unsigned hs_type,
552 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100553{
554 unsigned char hs_hdr[4];
555
556 /* Build HS header for checksum update. */
557 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
558 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
559 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
560 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
561
562 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
563}
564
565void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
566 unsigned hs_type,
567 unsigned char const *msg,
568 size_t msg_len )
569{
570 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
571 ssl->handshake->update_checksum( ssl, msg, msg_len );
572}
573
Jerry Yuc73c6182022-02-08 20:29:25 +0800574void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
575{
576 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400577#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800578#if defined(MBEDTLS_USE_PSA_CRYPTO)
579 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
580 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
581#else
582 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
583#endif
584#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400585#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800586#if defined(MBEDTLS_USE_PSA_CRYPTO)
587 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
588 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
589#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400590 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800591#endif
592#endif
593}
594
595static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
596 const unsigned char *buf, size_t len )
597{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400598#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800599#if defined(MBEDTLS_USE_PSA_CRYPTO)
600 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
601#else
602 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
603#endif
604#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400605#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800606#if defined(MBEDTLS_USE_PSA_CRYPTO)
607 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
608#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400609 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800610#endif
611#endif
612}
613
Andrzej Kurek25f27152022-08-17 16:09:31 -0400614#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800615static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
616 const unsigned char *buf, size_t len )
617{
618#if defined(MBEDTLS_USE_PSA_CRYPTO)
619 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
620#else
621 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
622#endif
623}
624#endif
625
Andrzej Kurek25f27152022-08-17 16:09:31 -0400626#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800627static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
628 const unsigned char *buf, size_t len )
629{
630#if defined(MBEDTLS_USE_PSA_CRYPTO)
631 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
632#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400633 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800634#endif
635}
636#endif
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200641
Andrzej Kurek25f27152022-08-17 16:09:31 -0400642#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500643#if defined(MBEDTLS_USE_PSA_CRYPTO)
644 handshake->fin_sha256_psa = psa_hash_operation_init();
645 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
646#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200648 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200649#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500650#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400651#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500652#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500653 handshake->fin_sha384_psa = psa_hash_operation_init();
654 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500655#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400656 mbedtls_sha512_init( &handshake->fin_sha384 );
657 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200658#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500659#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200660
661 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_DHM_C)
664 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200665#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200666#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200668#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200669#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200670 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200671#if defined(MBEDTLS_SSL_CLI_C)
672 handshake->ecjpake_cache = NULL;
673 handshake->ecjpake_cache_len = 0;
674#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200675#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200676
Gilles Peskineeccd8882020-03-10 12:19:08 +0100677#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200678 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200679#endif
680
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200681#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
682 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
683#endif
Hanno Becker75173122019-02-06 16:18:31 +0000684
685#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
686 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
687 mbedtls_pk_init( &handshake->peer_pubkey );
688#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200689}
690
Hanno Beckera18d1322018-01-03 14:27:32 +0000691void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200694
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100695#if defined(MBEDTLS_USE_PSA_CRYPTO)
696 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
697 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100698#else
699 mbedtls_cipher_init( &transform->cipher_ctx_enc );
700 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100701#endif
702
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000703#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100704#if defined(MBEDTLS_USE_PSA_CRYPTO)
705 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
706 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100707#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_md_init( &transform->md_ctx_enc );
709 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000710#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100711#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200712}
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200715{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717}
718
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200719MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000721{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200722 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000723 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200725 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200728 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200729
730 /*
731 * Either the pointers are now NULL or cleared properly and can be freed.
732 * Now allocate missing structures.
733 */
734 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200735 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200736 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200737 }
Paul Bakker48916f92012-09-16 19:57:18 +0000738
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200739 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200740 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200741 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200742 }
Paul Bakker48916f92012-09-16 19:57:18 +0000743
Paul Bakker82788fb2014-10-20 13:59:19 +0200744 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200745 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200746 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200747 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500748#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
749 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400750
Andrzej Kurek4a063792020-10-21 15:08:44 +0200751 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
752 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500753#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000754
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200755 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000756 if( ssl->handshake == NULL ||
757 ssl->transform_negotiate == NULL ||
758 ssl->session_negotiate == NULL )
759 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_free( ssl->handshake );
763 mbedtls_free( ssl->transform_negotiate );
764 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200765
766 ssl->handshake = NULL;
767 ssl->transform_negotiate = NULL;
768 ssl->session_negotiate = NULL;
769
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200770 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000771 }
772
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200773 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000775 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200776 ssl_handshake_params_init( ssl->handshake );
777
Jerry Yud0766ec2022-09-22 10:46:57 +0800778#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
779 defined(MBEDTLS_SSL_SRV_C) && \
780 defined(MBEDTLS_SSL_SESSION_TICKETS)
781 ssl->handshake->new_session_tickets_count =
782 ssl->conf->new_session_tickets_count ;
783#endif
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200786 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
787 {
788 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200789
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200790 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
791 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
792 else
793 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200794
Hanno Becker0f57a652020-02-05 10:37:26 +0000795 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200796 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200797#endif
798
Brett Warrene0edc842021-08-17 09:53:13 +0100799/*
800 * curve_list is translated to IANA TLS group identifiers here because
801 * mbedtls_ssl_conf_curves returns void and so can't return
802 * any error codes.
803 */
804#if defined(MBEDTLS_ECP_C)
805#if !defined(MBEDTLS_DEPRECATED_REMOVED)
806 /* Heap allocate and translate curve_list from internal to IANA group ids */
807 if ( ssl->conf->curve_list != NULL )
808 {
809 size_t length;
810 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
811
812 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
813 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
814
815 /* Leave room for zero termination */
816 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
817 if ( group_list == NULL )
818 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
819
820 for( size_t i = 0; i < length; i++ )
821 {
822 const mbedtls_ecp_curve_info *info =
823 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
824 if ( info == NULL )
825 {
826 mbedtls_free( group_list );
827 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
828 }
829 group_list[i] = info->tls_id;
830 }
831
832 group_list[length] = 0;
833
834 ssl->handshake->group_list = group_list;
835 ssl->handshake->group_list_heap_allocated = 1;
836 }
837 else
838 {
839 ssl->handshake->group_list = ssl->conf->group_list;
840 ssl->handshake->group_list_heap_allocated = 0;
841 }
842#endif /* MBEDTLS_DEPRECATED_REMOVED */
843#endif /* MBEDTLS_ECP_C */
844
Jerry Yuf017ee42022-01-12 15:49:48 +0800845#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
846#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800848 /* Heap allocate and translate sig_hashes from internal hash identifiers to
849 signature algorithms IANA identifiers. */
850 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800851 ssl->conf->sig_hashes != NULL )
852 {
853 const int *md;
854 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800855 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800856 uint16_t *p;
857
Jerry Yub476a442022-01-21 18:14:45 +0800858#if defined(static_assert)
859 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
860 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
861 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800862#endif
863
Jerry Yuf017ee42022-01-12 15:49:48 +0800864 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
865 {
866 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
867 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800868#if defined(MBEDTLS_ECDSA_C)
869 sig_algs_len += sizeof( uint16_t );
870#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800871
Jerry Yub476a442022-01-21 18:14:45 +0800872#if defined(MBEDTLS_RSA_C)
873 sig_algs_len += sizeof( uint16_t );
874#endif
875 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
876 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800877 }
878
Jerry Yub476a442022-01-21 18:14:45 +0800879 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800880 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
881
Jerry Yub476a442022-01-21 18:14:45 +0800882 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
883 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800884 if( ssl->handshake->sig_algs == NULL )
885 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
886
887 p = (uint16_t *)ssl->handshake->sig_algs;
888 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
889 {
890 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
891 if( hash == MBEDTLS_SSL_HASH_NONE )
892 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800893#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800894 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
895 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800896#endif
897#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800898 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
899 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800900#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800901 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200902 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800903 ssl->handshake->sig_algs_heap_allocated = 1;
904 }
905 else
Jerry Yua69269a2022-01-17 21:06:01 +0800906#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800907 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800908 ssl->handshake->sig_algs_heap_allocated = 0;
909 }
Jerry Yucc539102022-06-27 16:27:35 +0800910#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800911#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000912 return( 0 );
913}
914
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200915#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200916/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200917MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200918static int ssl_cookie_write_dummy( void *ctx,
919 unsigned char **p, unsigned char *end,
920 const unsigned char *cli_id, size_t cli_id_len )
921{
922 ((void) ctx);
923 ((void) p);
924 ((void) end);
925 ((void) cli_id);
926 ((void) cli_id_len);
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200929}
930
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200931MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200932static int ssl_cookie_check_dummy( void *ctx,
933 const unsigned char *cookie, size_t cookie_len,
934 const unsigned char *cli_id, size_t cli_id_len )
935{
936 ((void) ctx);
937 ((void) cookie);
938 ((void) cookie_len);
939 ((void) cli_id);
940 ((void) cli_id_len);
941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200943}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200944#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200945
Paul Bakker5121ce52009-01-03 21:22:43 +0000946/*
947 * Initialize an SSL context
948 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200949void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
950{
951 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
952}
953
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200954MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800955static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
956{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100957 const mbedtls_ssl_config *conf = ssl->conf;
958
Ronald Cron6f135e12021-12-08 16:57:54 +0100959#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100960 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
961 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000962 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
965 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
966 }
967
Jerry Yu60835a82021-08-04 10:13:52 +0800968 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
969 return( 0 );
970 }
971#endif
972
973#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100974 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800975 {
976 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
977 return( 0 );
978 }
979#endif
980
Ronald Cron6f135e12021-12-08 16:57:54 +0100981#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100982 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800983 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000984 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
985 {
986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
987 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
988 }
989
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000990 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
991 {
992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
993 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
994 }
995
996
Ronald Crone1d3f062022-02-10 14:50:54 +0100997 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
998 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800999 }
1000#endif
1001
1002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1003 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1004}
1005
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001006MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001007static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1008{
1009 int ret;
1010 ret = ssl_conf_version_check( ssl );
1011 if( ret != 0 )
1012 return( ret );
1013
1014 /* Space for further checks */
1015
1016 return( 0 );
1017}
1018
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001019/*
1020 * Setup an SSL context
1021 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001022
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001023int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001024 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001025{
Janos Follath865b3eb2019-12-16 11:46:15 +00001026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001027 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1028 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001030 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001031
Jerry Yu60835a82021-08-04 10:13:52 +08001032 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1033 return( ret );
1034
Paul Bakker62f2dee2012-09-28 07:31:51 +00001035 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001036 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001037 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001038
1039 /* Set to NULL in case of an error condition */
1040 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001041
Darryl Greenb33cc762019-11-28 14:29:44 +00001042#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1043 ssl->in_buf_len = in_buf_len;
1044#endif
1045 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001046 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001049 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001050 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001051 }
1052
Darryl Greenb33cc762019-11-28 14:29:44 +00001053#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1054 ssl->out_buf_len = out_buf_len;
1055#endif
1056 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001057 if( ssl->out_buf == NULL )
1058 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001060 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001061 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 }
1063
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001064 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001065
Johan Pascalb62bb512015-12-03 21:56:45 +01001066#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001067 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001068#endif
1069
Paul Bakker48916f92012-09-16 19:57:18 +00001070 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001071 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
1073 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001074
1075error:
1076 mbedtls_free( ssl->in_buf );
1077 mbedtls_free( ssl->out_buf );
1078
1079 ssl->conf = NULL;
1080
Darryl Greenb33cc762019-11-28 14:29:44 +00001081#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1082 ssl->in_buf_len = 0;
1083 ssl->out_buf_len = 0;
1084#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001085 ssl->in_buf = NULL;
1086 ssl->out_buf = NULL;
1087
1088 ssl->in_hdr = NULL;
1089 ssl->in_ctr = NULL;
1090 ssl->in_len = NULL;
1091 ssl->in_iv = NULL;
1092 ssl->in_msg = NULL;
1093
1094 ssl->out_hdr = NULL;
1095 ssl->out_ctr = NULL;
1096 ssl->out_len = NULL;
1097 ssl->out_iv = NULL;
1098 ssl->out_msg = NULL;
1099
k-stachowiak9f7798e2018-07-31 16:52:32 +02001100 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001101}
1102
1103/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001104 * Reset an initialized and used SSL context for re-use while retaining
1105 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001106 *
1107 * If partial is non-zero, keep data in the input buffer and client ID.
1108 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001109 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001110void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1111 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001112{
Darryl Greenb33cc762019-11-28 14:29:44 +00001113#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1114 size_t in_buf_len = ssl->in_buf_len;
1115 size_t out_buf_len = ssl->out_buf_len;
1116#else
1117 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1118 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1119#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001120
Hanno Beckerb0302c42021-08-03 09:39:42 +01001121#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1122 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001123#endif
1124
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001125 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001126 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001127
Hanno Beckerb0302c42021-08-03 09:39:42 +01001128 mbedtls_ssl_reset_in_out_pointers( ssl );
1129
1130 /* Reset incoming message parsing */
1131 ssl->in_offt = NULL;
1132 ssl->nb_zero = 0;
1133 ssl->in_msgtype = 0;
1134 ssl->in_msglen = 0;
1135 ssl->in_hslen = 0;
1136 ssl->keep_current_message = 0;
1137 ssl->transform_in = NULL;
1138
1139#if defined(MBEDTLS_SSL_PROTO_DTLS)
1140 ssl->next_record_offset = 0;
1141 ssl->in_epoch = 0;
1142#endif
1143
1144 /* Keep current datagram if partial == 1 */
1145 if( partial == 0 )
1146 {
1147 ssl->in_left = 0;
1148 memset( ssl->in_buf, 0, in_buf_len );
1149 }
1150
Ronald Cronad8c17b2022-06-10 17:18:09 +02001151 ssl->send_alert = 0;
1152
Hanno Beckerb0302c42021-08-03 09:39:42 +01001153 /* Reset outgoing message writing */
1154 ssl->out_msgtype = 0;
1155 ssl->out_msglen = 0;
1156 ssl->out_left = 0;
1157 memset( ssl->out_buf, 0, out_buf_len );
1158 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1159 ssl->transform_out = NULL;
1160
1161#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1162 mbedtls_ssl_dtls_replay_reset( ssl );
1163#endif
1164
XiaokangQian2b01dc32022-01-21 02:53:13 +00001165#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001166 if( ssl->transform )
1167 {
1168 mbedtls_ssl_transform_free( ssl->transform );
1169 mbedtls_free( ssl->transform );
1170 ssl->transform = NULL;
1171 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001172#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1173
XiaokangQian2b01dc32022-01-21 02:53:13 +00001174#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1175 mbedtls_ssl_transform_free( ssl->transform_application );
1176 mbedtls_free( ssl->transform_application );
1177 ssl->transform_application = NULL;
1178
1179 if( ssl->handshake != NULL )
1180 {
1181 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1182 mbedtls_free( ssl->handshake->transform_earlydata );
1183 ssl->handshake->transform_earlydata = NULL;
1184
1185 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1186 mbedtls_free( ssl->handshake->transform_handshake );
1187 ssl->handshake->transform_handshake = NULL;
1188 }
1189
XiaokangQian2b01dc32022-01-21 02:53:13 +00001190#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001191}
1192
1193int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1194{
1195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1196
1197 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1198
XiaokangQian78b1fa72022-01-19 06:56:30 +00001199 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001200
1201 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_SSL_RENEGOTIATION)
1203 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001204 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001205
1206 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1208 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001209#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001211
Hanno Beckerb0302c42021-08-03 09:39:42 +01001212 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001213 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001214 if( ssl->session )
1215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_ssl_session_free( ssl->session );
1217 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001218 ssl->session = NULL;
1219 }
1220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001222 ssl->alpn_chosen = NULL;
1223#endif
1224
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001225#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001226#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001227 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001228#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001229 {
1230 mbedtls_free( ssl->cli_id );
1231 ssl->cli_id = NULL;
1232 ssl->cli_id_len = 0;
1233 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001234#endif
1235
Paul Bakker48916f92012-09-16 19:57:18 +00001236 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1237 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001238
1239 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001240}
1241
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001242/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001243 * Reset an initialized and used SSL context for re-use while retaining
1244 * all application-set variables, function pointers and data.
1245 */
1246int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1247{
Hanno Becker43aefe22020-02-05 10:44:56 +00001248 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001249}
1250
1251/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 * SSL set accessors
1253 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001254void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001256 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257}
1258
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001259void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001261 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001262}
1263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001265void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001266{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001267 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001268}
1269#endif
1270
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001271void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001272{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001273 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001274}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001277
Hanno Becker1841b0a2018-08-24 11:13:57 +01001278void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1279 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001280{
1281 ssl->disable_datagram_packing = !allow_packing;
1282}
1283
1284void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1285 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001286{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001287 conf->hs_timeout_min = min;
1288 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001289}
1290#endif
1291
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001292void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001293{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001294 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001295}
1296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001298void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001299 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001300 void *p_vrfy )
1301{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001302 conf->f_vrfy = f_vrfy;
1303 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001306
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001307void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001308 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 void *p_rng )
1310{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001311 conf->f_rng = f_rng;
1312 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001313}
1314
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001315void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001316 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 void *p_dbg )
1318{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001319 conf->f_dbg = f_dbg;
1320 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001321}
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001324 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001325 mbedtls_ssl_send_t *f_send,
1326 mbedtls_ssl_recv_t *f_recv,
1327 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001328{
1329 ssl->p_bio = p_bio;
1330 ssl->f_send = f_send;
1331 ssl->f_recv = f_recv;
1332 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001333}
1334
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001335#if defined(MBEDTLS_SSL_PROTO_DTLS)
1336void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1337{
1338 ssl->mtu = mtu;
1339}
1340#endif
1341
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001342void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001343{
1344 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001345}
1346
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001347void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1348 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001349 mbedtls_ssl_set_timer_t *f_set_timer,
1350 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001351{
1352 ssl->p_timer = p_timer;
1353 ssl->f_set_timer = f_set_timer;
1354 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001355
1356 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001357 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001358}
1359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001361void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001362 void *p_cache,
1363 mbedtls_ssl_cache_get_t *f_get_cache,
1364 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001366 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001367 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001368 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_SSL_CLI_C)
1373int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001374{
Janos Follath865b3eb2019-12-16 11:46:15 +00001375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001376
1377 if( ssl == NULL ||
1378 session == NULL ||
1379 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001380 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001383 }
1384
Hanno Beckere810bbc2021-05-14 16:01:05 +01001385 if( ssl->handshake->resume == 1 )
1386 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1387
Jerry Yu21092062022-10-10 21:21:31 +08001388#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1389 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001390 {
Jerry Yu21092062022-10-10 21:21:31 +08001391 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1392 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1393
1394 if( mbedtls_ssl_validate_ciphersuite(
1395 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1396 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1397 {
1398 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1399 session->ciphersuite ) );
1400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1401 }
Jerry Yu40afab62022-10-08 10:42:13 +08001402 }
Jerry Yu21092062022-10-10 21:21:31 +08001403#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001404
Hanno Becker52055ae2019-02-06 14:30:46 +00001405 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1406 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001407 return( ret );
1408
Paul Bakker0a597072012-09-25 21:55:46 +00001409 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001410
1411 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001415void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1416 const int *ciphersuites )
1417{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001418 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001419}
1420
Ronald Cron6f135e12021-12-08 16:57:54 +01001421#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001422void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001423 const int kex_modes )
1424{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001425 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001426}
Ronald Cron6f135e12021-12-08 16:57:54 +01001427#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001430void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001431 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001432{
1433 conf->cert_profile = profile;
1434}
1435
Glenn Strauss36872db2022-01-22 05:06:31 -05001436static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1437{
1438 mbedtls_ssl_key_cert *cur = key_cert, *next;
1439
1440 while( cur != NULL )
1441 {
1442 next = cur->next;
1443 mbedtls_free( cur );
1444 cur = next;
1445 }
1446}
1447
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001448/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001449MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001450static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1451 mbedtls_x509_crt *cert,
1452 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001453{
niisato8ee24222018-06-25 19:05:48 +09001454 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001455
Glenn Strauss36872db2022-01-22 05:06:31 -05001456 if( cert == NULL )
1457 {
1458 /* Free list if cert is null */
1459 ssl_key_cert_free( *head );
1460 *head = NULL;
1461 return( 0 );
1462 }
1463
niisato8ee24222018-06-25 19:05:48 +09001464 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1465 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001466 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001467
niisato8ee24222018-06-25 19:05:48 +09001468 new_cert->cert = cert;
1469 new_cert->key = key;
1470 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001471
Glenn Strauss36872db2022-01-22 05:06:31 -05001472 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001473 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001474 {
niisato8ee24222018-06-25 19:05:48 +09001475 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001476 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001477 else
1478 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001479 mbedtls_ssl_key_cert *cur = *head;
1480 while( cur->next != NULL )
1481 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001482 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001483 }
1484
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001485 return( 0 );
1486}
1487
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001488int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001489 mbedtls_x509_crt *own_cert,
1490 mbedtls_pk_context *pk_key )
1491{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001492 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001493}
1494
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001495void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001496 mbedtls_x509_crt *ca_chain,
1497 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001498{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001499 conf->ca_chain = ca_chain;
1500 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001501
1502#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1503 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1504 * cannot be used together. */
1505 conf->f_ca_cb = NULL;
1506 conf->p_ca_cb = NULL;
1507#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001508}
Hanno Becker5adaad92019-03-27 16:54:37 +00001509
1510#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1511void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001512 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001513 void *p_ca_cb )
1514{
1515 conf->f_ca_cb = f_ca_cb;
1516 conf->p_ca_cb = p_ca_cb;
1517
1518 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1519 * cannot be used together. */
1520 conf->ca_chain = NULL;
1521 conf->ca_crl = NULL;
1522}
1523#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001525
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001526#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001527const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1528 size_t *name_len )
1529{
1530 *name_len = ssl->handshake->sni_name_len;
1531 return( ssl->handshake->sni_name );
1532}
1533
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001534int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1535 mbedtls_x509_crt *own_cert,
1536 mbedtls_pk_context *pk_key )
1537{
1538 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1539 own_cert, pk_key ) );
1540}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001541
1542void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1543 mbedtls_x509_crt *ca_chain,
1544 mbedtls_x509_crl *ca_crl )
1545{
1546 ssl->handshake->sni_ca_chain = ca_chain;
1547 ssl->handshake->sni_ca_crl = ca_crl;
1548}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001549
Glenn Strauss999ef702022-03-11 01:37:23 -05001550#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1551void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1552 const mbedtls_x509_crt *crt)
1553{
1554 ssl->handshake->dn_hints = crt;
1555}
1556#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1557
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001558void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1559 int authmode )
1560{
1561 ssl->handshake->sni_authmode = authmode;
1562}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001563#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1564
Hanno Becker8927c832019-04-03 12:52:50 +01001565#if defined(MBEDTLS_X509_CRT_PARSE_C)
1566void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1567 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1568 void *p_vrfy )
1569{
1570 ssl->f_vrfy = f_vrfy;
1571 ssl->p_vrfy = p_vrfy;
1572}
1573#endif
1574
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001575#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001576/*
1577 * Set EC J-PAKE password for current handshake
1578 */
1579int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1580 const unsigned char *pw,
1581 size_t pw_len )
1582{
1583 mbedtls_ecjpake_role role;
1584
Janos Follath8eb64132016-06-03 15:40:57 +01001585 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1587
1588 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1589 role = MBEDTLS_ECJPAKE_SERVER;
1590 else
1591 role = MBEDTLS_ECJPAKE_CLIENT;
1592
1593 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1594 role,
1595 MBEDTLS_MD_SHA256,
1596 MBEDTLS_ECP_DP_SECP256R1,
1597 pw, pw_len ) );
1598}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001599#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001600
Gilles Peskineeccd8882020-03-10 12:19:08 +01001601#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001602
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001603MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001604static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1605{
1606#if defined(MBEDTLS_USE_PSA_CRYPTO)
1607 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1608 return( 1 );
1609#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001610 if( conf->psk != NULL )
1611 return( 1 );
1612
1613 return( 0 );
1614}
1615
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001616static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1617{
1618 /* Remove reference to existing PSK, if any. */
1619#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001620 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001621 {
1622 /* The maintenance of the PSK key slot is the
1623 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001624 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001625 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001626#endif /* MBEDTLS_USE_PSA_CRYPTO */
1627 if( conf->psk != NULL )
1628 {
1629 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1630
1631 mbedtls_free( conf->psk );
1632 conf->psk = NULL;
1633 conf->psk_len = 0;
1634 }
1635
1636 /* Remove reference to PSK identity, if any. */
1637 if( conf->psk_identity != NULL )
1638 {
1639 mbedtls_free( conf->psk_identity );
1640 conf->psk_identity = NULL;
1641 conf->psk_identity_len = 0;
1642 }
1643}
1644
Hanno Becker7390c712018-11-15 13:33:04 +00001645/* This function assumes that PSK identity in the SSL config is unset.
1646 * It checks that the provided identity is well-formed and attempts
1647 * to make a copy of it in the SSL config.
1648 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001649MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001650static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1651 unsigned char const *psk_identity,
1652 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001653{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001654 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001655 if( psk_identity == NULL ||
1656 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001657 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001658 {
1659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1660 }
1661
Hanno Becker7390c712018-11-15 13:33:04 +00001662 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1663 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001664 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001665
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001666 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001667 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001668
1669 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001670}
1671
Hanno Becker7390c712018-11-15 13:33:04 +00001672int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1673 const unsigned char *psk, size_t psk_len,
1674 const unsigned char *psk_identity, size_t psk_identity_len )
1675{
Janos Follath865b3eb2019-12-16 11:46:15 +00001676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001677
1678 /* We currently only support one PSK, raw or opaque. */
1679 if( ssl_conf_psk_is_configured( conf ) )
1680 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001681
1682 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001683 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001685 if( psk_len == 0 )
1686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1687 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1689
Hanno Becker7390c712018-11-15 13:33:04 +00001690 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1691 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1692 conf->psk_len = psk_len;
1693 memcpy( conf->psk, psk, conf->psk_len );
1694
1695 /* Check and set PSK Identity */
1696 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1697 if( ret != 0 )
1698 ssl_conf_remove_psk( conf );
1699
1700 return( ret );
1701}
1702
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001703static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1704{
1705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001706 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001707 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001708 /* The maintenance of the external PSK key slot is the
1709 * user's responsibility. */
1710 if( ssl->handshake->psk_opaque_is_internal )
1711 {
1712 psa_destroy_key( ssl->handshake->psk_opaque );
1713 ssl->handshake->psk_opaque_is_internal = 0;
1714 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001715 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001716 }
Neil Armstronge952a302022-05-03 10:22:14 +02001717#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001718 if( ssl->handshake->psk != NULL )
1719 {
1720 mbedtls_platform_zeroize( ssl->handshake->psk,
1721 ssl->handshake->psk_len );
1722 mbedtls_free( ssl->handshake->psk );
1723 ssl->handshake->psk_len = 0;
1724 }
Neil Armstronge952a302022-05-03 10:22:14 +02001725#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001726}
1727
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001728int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1729 const unsigned char *psk, size_t psk_len )
1730{
Neil Armstrong501c9322022-05-03 09:35:09 +02001731#if defined(MBEDTLS_USE_PSA_CRYPTO)
1732 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001733 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001734 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001736#endif /* MBEDTLS_USE_PSA_CRYPTO */
1737
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001738 if( psk == NULL || ssl->handshake == NULL )
1739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1740
1741 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1743
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001744 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001745
Neil Armstrong501c9322022-05-03 09:35:09 +02001746#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001747#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1748 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1749 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001750 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001751 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1752 else
1753 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1754 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1755 }
1756#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001757
Jerry Yu568ec252022-07-22 21:27:34 +08001758#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001759 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1760 {
1761 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1762 psa_set_key_usage_flags( &key_attributes,
1763 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1764 }
1765#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1766
Neil Armstrong501c9322022-05-03 09:35:09 +02001767 psa_set_key_algorithm( &key_attributes, alg );
1768 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1769
1770 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1771 if( status != PSA_SUCCESS )
1772 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1773
1774 /* Allow calling psa_destroy_key() on psk remove */
1775 ssl->handshake->psk_opaque_is_internal = 1;
1776 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1777#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001778 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001779 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001780
1781 ssl->handshake->psk_len = psk_len;
1782 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1783
1784 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001785#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001786}
1787
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001788#if defined(MBEDTLS_USE_PSA_CRYPTO)
1789int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001790 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001791 const unsigned char *psk_identity,
1792 size_t psk_identity_len )
1793{
Janos Follath865b3eb2019-12-16 11:46:15 +00001794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001795
1796 /* We currently only support one PSK, raw or opaque. */
1797 if( ssl_conf_psk_is_configured( conf ) )
1798 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001799
Hanno Becker7390c712018-11-15 13:33:04 +00001800 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001801 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001803 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001804
1805 /* Check and set PSK Identity */
1806 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1807 psk_identity_len );
1808 if( ret != 0 )
1809 ssl_conf_remove_psk( conf );
1810
1811 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001812}
1813
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001814int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1815 mbedtls_svc_key_id_t psk )
1816{
1817 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1818 ( ssl->handshake == NULL ) )
1819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1820
1821 ssl_remove_psk( ssl );
1822 ssl->handshake->psk_opaque = psk;
1823 return( 0 );
1824}
1825#endif /* MBEDTLS_USE_PSA_CRYPTO */
1826
Jerry Yu8897c072022-08-12 13:56:53 +08001827#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001828void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1829 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1830 size_t),
1831 void *p_psk )
1832{
1833 conf->f_psk = f_psk;
1834 conf->p_psk = p_psk;
1835}
Jerry Yu8897c072022-08-12 13:56:53 +08001836#endif /* MBEDTLS_SSL_SRV_C */
1837
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001838#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1839
1840#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001841static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1842 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001843{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001844#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001845 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001846 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001847#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001848 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001849 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001850 return( MBEDTLS_SSL_MODE_STREAM );
1851}
1852
1853#else /* MBEDTLS_USE_PSA_CRYPTO */
1854
1855static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1856 mbedtls_cipher_mode_t mode )
1857{
1858#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1859 if( mode == MBEDTLS_MODE_CBC )
1860 return( MBEDTLS_SSL_MODE_CBC );
1861#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1862
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001863#if defined(MBEDTLS_GCM_C) || \
1864 defined(MBEDTLS_CCM_C) || \
1865 defined(MBEDTLS_CHACHAPOLY_C)
1866 if( mode == MBEDTLS_MODE_GCM ||
1867 mode == MBEDTLS_MODE_CCM ||
1868 mode == MBEDTLS_MODE_CHACHAPOLY )
1869 return( MBEDTLS_SSL_MODE_AEAD );
1870#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001871
1872 return( MBEDTLS_SSL_MODE_STREAM );
1873}
Gilles Peskine301711e2022-04-26 16:57:05 +02001874#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001875
Gilles Peskinee108d982022-04-26 16:50:40 +02001876static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1877 mbedtls_ssl_mode_t base_mode,
1878 int encrypt_then_mac )
1879{
1880#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1881 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1882 base_mode == MBEDTLS_SSL_MODE_CBC )
1883 {
1884 return( MBEDTLS_SSL_MODE_CBC_ETM );
1885 }
1886#else
1887 (void) encrypt_then_mac;
1888#endif
1889 return( base_mode );
1890}
1891
Neil Armstrongab555e02022-04-04 11:07:59 +02001892mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001893 const mbedtls_ssl_transform *transform )
1894{
Gilles Peskinee108d982022-04-26 16:50:40 +02001895 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001896#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001897 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001898#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001899 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1900#endif
1901 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001902
Gilles Peskinee108d982022-04-26 16:50:40 +02001903 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001904#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001905 encrypt_then_mac = transform->encrypt_then_mac;
1906#endif
1907 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001908}
1909
Neil Armstrongab555e02022-04-04 11:07:59 +02001910mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001911#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001912 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001913#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001914 const mbedtls_ssl_ciphersuite_t *suite )
1915{
Gilles Peskinee108d982022-04-26 16:50:40 +02001916 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1917
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001918#if defined(MBEDTLS_USE_PSA_CRYPTO)
1919 psa_status_t status;
1920 psa_algorithm_t alg;
1921 psa_key_type_t type;
1922 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001923 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1924 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001925 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001926#else
1927 const mbedtls_cipher_info_t *cipher =
1928 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001929 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001930 {
1931 base_mode =
1932 mbedtls_ssl_get_base_mode(
1933 mbedtls_cipher_info_get_mode( cipher ) );
1934 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001935#endif /* MBEDTLS_USE_PSA_CRYPTO */
1936
Gilles Peskinee108d982022-04-26 16:50:40 +02001937#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1938 int encrypt_then_mac = 0;
1939#endif
1940 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001941}
1942
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001943#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001944
1945#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001946/* Serialization of TLS 1.3 sessions:
1947 *
1948 * struct {
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00001949 * opaque hostname<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001950 * uint64 ticket_received;
1951 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001952 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001953 * } ClientOnlyData;
1954 *
1955 * struct {
1956 * uint8 endpoint;
1957 * uint8 ciphersuite[2];
1958 * uint32 ticket_age_add;
1959 * uint8 ticket_flags;
1960 * opaque resumption_key<0..255>;
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00001961 *
Jerry Yu438ddd82022-07-07 06:55:50 +00001962 * select ( endpoint ) {
1963 * case client: ClientOnlyData;
1964 * case server: uint64 start_time;
1965 * };
1966 * } serialized_session_tls13;
1967 *
1968 */
1969#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001970MBEDTLS_CHECK_RETURN_CRITICAL
1971static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1972 unsigned char *buf,
1973 size_t buf_len,
1974 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001975{
1976 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001977 size_t needed = 1 /* endpoint */
1978 + 2 /* ciphersuite */
1979 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001980 + 1 /* ticket_flags */
1981 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001982 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001983
1984 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1986 needed += session->resumption_key_len; /* resumption_key */
1987
Jerry Yu438ddd82022-07-07 06:55:50 +00001988#if defined(MBEDTLS_HAVE_TIME)
1989 needed += 8; /* start_time or ticket_received */
1990#endif
1991
1992#if defined(MBEDTLS_SSL_CLI_C)
1993 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1994 {
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00001995#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1996 needed += 1 /* hostname_len */
1997 + session->hostname_len; /* hostname */
1998#endif
1999
Jerry Yu438ddd82022-07-07 06:55:50 +00002000 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002001 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002002
2003 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002004 if( session->ticket_len > SIZE_MAX - needed )
2005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002006
Jerry Yue28d9742022-08-18 15:44:03 +08002007 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002008 }
2009#endif /* MBEDTLS_SSL_CLI_C */
2010
Jerry Yue36fdd62022-08-17 21:31:36 +08002011 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002012 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002013 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002014
2015 p[0] = session->endpoint;
2016 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2017 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2018 p[7] = session->ticket_flags;
2019
2020 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002021 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002022 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002023 memcpy( p, session->resumption_key, session->resumption_key_len );
2024 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002025
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002026#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002027 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002028 {
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002029 p[0] = session->hostname_len;
2030 p++;
2031 if ( session->hostname_len > 0 &&
2032 session->hostname != NULL )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002033 /* save host name */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002034 memcpy( p, session->hostname, session->hostname_len );
2035 p += session->hostname_len;
2036 }
2037#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2038
Jerry Yu438ddd82022-07-07 06:55:50 +00002039#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2040 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2041 {
2042 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2043 p += 8;
2044 }
2045#endif /* MBEDTLS_HAVE_TIME */
2046
2047#if defined(MBEDTLS_SSL_CLI_C)
2048 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2049 {
2050#if defined(MBEDTLS_HAVE_TIME)
2051 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2052 p += 8;
2053#endif
2054 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2055 p += 4;
2056
2057 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2058 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002059
2060 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002061 {
2062 memcpy( p, session->ticket, session->ticket_len );
2063 p += session->ticket_len;
2064 }
2065 }
2066#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002067 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002068}
2069
2070MBEDTLS_CHECK_RETURN_CRITICAL
2071static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2072 const unsigned char *buf,
2073 size_t len )
2074{
2075 const unsigned char *p = buf;
2076 const unsigned char *end = buf + len;
2077
2078 if( end - p < 9 )
2079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2080 session->endpoint = p[0];
2081 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2082 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2083 session->ticket_flags = p[7];
2084
2085 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002086 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002087 p += 9;
2088
Jerry Yubc7c1a42022-07-21 22:57:37 +08002089 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2091
Jerry Yubc7c1a42022-07-21 22:57:37 +08002092 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002094 memcpy( session->resumption_key, p, session->resumption_key_len );
2095 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002096
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002097#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
2098 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2099 {
2100 /* load host name */
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002101 if( end - p < 1 )
2102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002103 session->hostname_len = p[0];
2104 p += 1;
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002105
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002106 if( end - p < session->hostname_len )
2107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianecd75282022-09-28 07:11:02 +00002108 if( session->hostname_len > 0 )
2109 {
2110 session->hostname = mbedtls_calloc( 1, session->hostname_len );
2111 if( session->hostname == NULL )
2112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2113 memcpy( session->hostname, p, session->hostname_len );
2114 p += session->hostname_len;
2115 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002116 }
2117#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2118
Jerry Yu438ddd82022-07-07 06:55:50 +00002119#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2120 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2121 {
2122 if( end - p < 8 )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2124 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2125 p += 8;
2126 }
2127#endif /* MBEDTLS_HAVE_TIME */
2128
2129#if defined(MBEDTLS_SSL_CLI_C)
2130 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2131 {
2132#if defined(MBEDTLS_HAVE_TIME)
2133 if( end - p < 8 )
2134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2135 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2136 p += 8;
2137#endif
2138 if( end - p < 4 )
2139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2140 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2141 p += 4;
2142
2143 if( end - p < 2 )
2144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2145 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2146 p += 2;
2147
2148 if( end - p < ( long int )session->ticket_len )
2149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2150 if( session->ticket_len > 0 )
2151 {
2152 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2153 if( session->ticket == NULL )
2154 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2155 memcpy( session->ticket, p, session->ticket_len );
2156 p += session->ticket_len;
2157 }
2158 }
2159#endif /* MBEDTLS_SSL_CLI_C */
2160
2161 return( 0 );
2162
2163}
2164#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002165MBEDTLS_CHECK_RETURN_CRITICAL
2166static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2167 unsigned char *buf,
2168 size_t buf_len,
2169 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002170{
2171 ((void) session);
2172 ((void) buf);
2173 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002174 *olen = 0;
2175 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002176}
Jerry Yu438ddd82022-07-07 06:55:50 +00002177
2178static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2179 unsigned char *buf,
2180 size_t buf_len )
2181{
2182 ((void) session);
2183 ((void) buf);
2184 ((void) buf_len);
2185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2186}
2187#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002188#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2189
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002190psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002191 size_t taglen,
2192 psa_algorithm_t *alg,
2193 psa_key_type_t *key_type,
2194 size_t *key_size )
2195{
2196 switch ( mbedtls_cipher_type )
2197 {
2198 case MBEDTLS_CIPHER_AES_128_CBC:
2199 *alg = PSA_ALG_CBC_NO_PADDING;
2200 *key_type = PSA_KEY_TYPE_AES;
2201 *key_size = 128;
2202 break;
2203 case MBEDTLS_CIPHER_AES_128_CCM:
2204 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2205 *key_type = PSA_KEY_TYPE_AES;
2206 *key_size = 128;
2207 break;
2208 case MBEDTLS_CIPHER_AES_128_GCM:
2209 *alg = PSA_ALG_GCM;
2210 *key_type = PSA_KEY_TYPE_AES;
2211 *key_size = 128;
2212 break;
2213 case MBEDTLS_CIPHER_AES_192_CCM:
2214 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2215 *key_type = PSA_KEY_TYPE_AES;
2216 *key_size = 192;
2217 break;
2218 case MBEDTLS_CIPHER_AES_192_GCM:
2219 *alg = PSA_ALG_GCM;
2220 *key_type = PSA_KEY_TYPE_AES;
2221 *key_size = 192;
2222 break;
2223 case MBEDTLS_CIPHER_AES_256_CBC:
2224 *alg = PSA_ALG_CBC_NO_PADDING;
2225 *key_type = PSA_KEY_TYPE_AES;
2226 *key_size = 256;
2227 break;
2228 case MBEDTLS_CIPHER_AES_256_CCM:
2229 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2230 *key_type = PSA_KEY_TYPE_AES;
2231 *key_size = 256;
2232 break;
2233 case MBEDTLS_CIPHER_AES_256_GCM:
2234 *alg = PSA_ALG_GCM;
2235 *key_type = PSA_KEY_TYPE_AES;
2236 *key_size = 256;
2237 break;
2238 case MBEDTLS_CIPHER_ARIA_128_CBC:
2239 *alg = PSA_ALG_CBC_NO_PADDING;
2240 *key_type = PSA_KEY_TYPE_ARIA;
2241 *key_size = 128;
2242 break;
2243 case MBEDTLS_CIPHER_ARIA_128_CCM:
2244 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2245 *key_type = PSA_KEY_TYPE_ARIA;
2246 *key_size = 128;
2247 break;
2248 case MBEDTLS_CIPHER_ARIA_128_GCM:
2249 *alg = PSA_ALG_GCM;
2250 *key_type = PSA_KEY_TYPE_ARIA;
2251 *key_size = 128;
2252 break;
2253 case MBEDTLS_CIPHER_ARIA_192_CCM:
2254 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2255 *key_type = PSA_KEY_TYPE_ARIA;
2256 *key_size = 192;
2257 break;
2258 case MBEDTLS_CIPHER_ARIA_192_GCM:
2259 *alg = PSA_ALG_GCM;
2260 *key_type = PSA_KEY_TYPE_ARIA;
2261 *key_size = 192;
2262 break;
2263 case MBEDTLS_CIPHER_ARIA_256_CBC:
2264 *alg = PSA_ALG_CBC_NO_PADDING;
2265 *key_type = PSA_KEY_TYPE_ARIA;
2266 *key_size = 256;
2267 break;
2268 case MBEDTLS_CIPHER_ARIA_256_CCM:
2269 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2270 *key_type = PSA_KEY_TYPE_ARIA;
2271 *key_size = 256;
2272 break;
2273 case MBEDTLS_CIPHER_ARIA_256_GCM:
2274 *alg = PSA_ALG_GCM;
2275 *key_type = PSA_KEY_TYPE_ARIA;
2276 *key_size = 256;
2277 break;
2278 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2279 *alg = PSA_ALG_CBC_NO_PADDING;
2280 *key_type = PSA_KEY_TYPE_CAMELLIA;
2281 *key_size = 128;
2282 break;
2283 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2284 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2285 *key_type = PSA_KEY_TYPE_CAMELLIA;
2286 *key_size = 128;
2287 break;
2288 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2289 *alg = PSA_ALG_GCM;
2290 *key_type = PSA_KEY_TYPE_CAMELLIA;
2291 *key_size = 128;
2292 break;
2293 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2294 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2295 *key_type = PSA_KEY_TYPE_CAMELLIA;
2296 *key_size = 192;
2297 break;
2298 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2299 *alg = PSA_ALG_GCM;
2300 *key_type = PSA_KEY_TYPE_CAMELLIA;
2301 *key_size = 192;
2302 break;
2303 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2304 *alg = PSA_ALG_CBC_NO_PADDING;
2305 *key_type = PSA_KEY_TYPE_CAMELLIA;
2306 *key_size = 256;
2307 break;
2308 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2309 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2310 *key_type = PSA_KEY_TYPE_CAMELLIA;
2311 *key_size = 256;
2312 break;
2313 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2314 *alg = PSA_ALG_GCM;
2315 *key_type = PSA_KEY_TYPE_CAMELLIA;
2316 *key_size = 256;
2317 break;
2318 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2319 *alg = PSA_ALG_CHACHA20_POLY1305;
2320 *key_type = PSA_KEY_TYPE_CHACHA20;
2321 *key_size = 256;
2322 break;
2323 case MBEDTLS_CIPHER_NULL:
2324 *alg = MBEDTLS_SSL_NULL_CIPHER;
2325 *key_type = 0;
2326 *key_size = 0;
2327 break;
2328 default:
2329 return PSA_ERROR_NOT_SUPPORTED;
2330 }
2331
2332 return PSA_SUCCESS;
2333}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002334#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002335
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002336#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002337int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2338 const unsigned char *dhm_P, size_t P_len,
2339 const unsigned char *dhm_G, size_t G_len )
2340{
Janos Follath865b3eb2019-12-16 11:46:15 +00002341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002342
Glenn Strausscee11292021-12-20 01:43:17 -05002343 mbedtls_mpi_free( &conf->dhm_P );
2344 mbedtls_mpi_free( &conf->dhm_G );
2345
Hanno Beckera90658f2017-10-04 15:29:08 +01002346 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2347 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2348 {
2349 mbedtls_mpi_free( &conf->dhm_P );
2350 mbedtls_mpi_free( &conf->dhm_G );
2351 return( ret );
2352 }
2353
2354 return( 0 );
2355}
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002357int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002358{
Janos Follath865b3eb2019-12-16 11:46:15 +00002359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002360
Glenn Strausscee11292021-12-20 01:43:17 -05002361 mbedtls_mpi_free( &conf->dhm_P );
2362 mbedtls_mpi_free( &conf->dhm_G );
2363
Gilles Peskinee5702482021-06-11 21:59:08 +02002364 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2365 &conf->dhm_P ) ) != 0 ||
2366 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2367 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002368 {
2369 mbedtls_mpi_free( &conf->dhm_P );
2370 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002371 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002372 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002373
2374 return( 0 );
2375}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002376#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002377
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002378#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2379/*
2380 * Set the minimum length for Diffie-Hellman parameters
2381 */
2382void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2383 unsigned int bitlen )
2384{
2385 conf->dhm_min_bitlen = bitlen;
2386}
2387#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2388
Gilles Peskineeccd8882020-03-10 12:19:08 +01002389#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002390#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002391/*
2392 * Set allowed/preferred hashes for handshake signatures
2393 */
2394void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2395 const int *hashes )
2396{
2397 conf->sig_hashes = hashes;
2398}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002399#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002400
Jerry Yuf017ee42022-01-12 15:49:48 +08002401/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002402void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2403 const uint16_t* sig_algs )
2404{
Jerry Yuf017ee42022-01-12 15:49:48 +08002405#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2406 conf->sig_hashes = NULL;
2407#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2408 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002409}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002410#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002411
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002412#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002413#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002414/*
2415 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002416 *
2417 * mbedtls_ssl_setup() takes the provided list
2418 * and translates it to a list of IANA TLS group identifiers,
2419 * stored in ssl->handshake->group_list.
2420 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002421 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002422void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002423 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002425 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002426 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002427}
Brett Warrene0edc842021-08-17 09:53:13 +01002428#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002429#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002430
Brett Warrene0edc842021-08-17 09:53:13 +01002431/*
2432 * Set the allowed groups
2433 */
2434void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2435 const uint16_t *group_list )
2436{
2437#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2438 conf->curve_list = NULL;
2439#endif
2440 conf->group_list = group_list;
2441}
2442
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002443#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445{
Hanno Becker947194e2017-04-07 13:25:49 +01002446 /* Initialize to suppress unnecessary compiler warning */
2447 size_t hostname_len = 0;
2448
2449 /* Check if new hostname is valid before
2450 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002451 if( hostname != NULL )
2452 {
2453 hostname_len = strlen( hostname );
2454
2455 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2457 }
2458
2459 /* Now it's clear that we will overwrite the old hostname,
2460 * so we can free it safely */
2461
2462 if( ssl->hostname != NULL )
2463 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002464 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002465 mbedtls_free( ssl->hostname );
2466 }
2467
2468 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002469
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002471 {
2472 ssl->hostname = NULL;
2473 }
2474 else
2475 {
2476 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002477 if( ssl->hostname == NULL )
2478 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002479
Hanno Becker947194e2017-04-07 13:25:49 +01002480 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002481
Hanno Becker947194e2017-04-07 13:25:49 +01002482 ssl->hostname[hostname_len] = '\0';
2483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
2485 return( 0 );
2486}
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002487
2488int mbedtls_ssl_reset_hostname( mbedtls_ssl_context *ssl,
2489 const char *hostname,
2490 const char *rec_hostname )
2491{
2492 /* Initialize to suppress unnecessary compiler warning */
2493 size_t rec_hostname_len = 0;
2494
2495 if( hostname == NULL || rec_hostname == NULL )
2496 return( 0 );
2497
2498 rec_hostname_len = strlen( rec_hostname );
2499 if( rec_hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2501
2502 if( rec_hostname_len == strlen( hostname ) &&
2503 memcmp( hostname, rec_hostname, rec_hostname_len ) == 0 )
2504 return( 0 );
2505
2506 if( ssl->hostname != NULL )
2507 {
2508 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
2509 mbedtls_free( ssl->hostname );
2510 ssl->hostname = NULL;
2511 ssl->hostname = mbedtls_calloc( 1, rec_hostname_len + 1 );
2512 if( ssl->hostname == NULL )
2513 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2514 memcpy( ssl->hostname, rec_hostname, rec_hostname_len );
2515 ssl->hostname[rec_hostname_len] = '\0';
2516 }
2517
2518 return( 0 );
2519}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002520#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002522#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002523void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002525 const unsigned char *, size_t),
2526 void *p_sni )
2527{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002528 conf->f_sni = f_sni;
2529 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002534int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002535{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002536 size_t cur_len, tot_len;
2537 const char **p;
2538
2539 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002540 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2541 * MUST NOT be truncated."
2542 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002543 */
2544 tot_len = 0;
2545 for( p = protos; *p != NULL; p++ )
2546 {
2547 cur_len = strlen( *p );
2548 tot_len += cur_len;
2549
Ronald Cron8216dd32020-04-23 16:41:44 +02002550 if( ( cur_len == 0 ) ||
2551 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2552 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002554 }
2555
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002556 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002557
2558 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002559}
2560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002562{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002563 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002566
Johan Pascalb62bb512015-12-03 21:56:45 +01002567#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002568void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2569 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002570{
2571 conf->dtls_srtp_mki_support = support_mki_value;
2572}
2573
Ron Eldoref72faf2018-07-12 11:54:20 +03002574int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2575 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002576 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002577{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002578 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002579 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002581 }
Ron Eldor591f1622018-01-22 12:30:04 +02002582
2583 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002584 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002585 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002586 }
Ron Eldor591f1622018-01-22 12:30:04 +02002587
2588 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2589 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002590 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002591}
2592
Ron Eldoref72faf2018-07-12 11:54:20 +03002593int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002594 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002595{
Johan Pascal253d0262020-09-22 13:04:45 +02002596 const mbedtls_ssl_srtp_profile *p;
2597 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002598
Johan Pascal253d0262020-09-22 13:04:45 +02002599 /* check the profiles list: all entry must be valid,
2600 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002601 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2602 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2603 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002604 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002605 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002606 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002607 list_size++;
2608 }
2609 else
2610 {
2611 /* unsupported value, stop parsing and set the size to an error value */
2612 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002613 }
2614 }
2615
Johan Pascal5ef72d22020-10-28 17:05:47 +01002616 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002617 {
Johan Pascal253d0262020-09-22 13:04:45 +02002618 conf->dtls_srtp_profile_list = NULL;
2619 conf->dtls_srtp_profile_list_len = 0;
2620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2621 }
2622
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002623 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002624 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002625
2626 return( 0 );
2627}
2628
Johan Pascal5ef72d22020-10-28 17:05:47 +01002629void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2630 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002631{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002632 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2633 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002634 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002635 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002636 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002637 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002638 else
2639 {
2640 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002641 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2642 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002643 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002644}
Johan Pascalb62bb512015-12-03 21:56:45 +01002645#endif /* MBEDTLS_SSL_DTLS_SRTP */
2646
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302647#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002648void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002649{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002650 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002651}
2652
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002653void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002654{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002655 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002656}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302657#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002658
Janos Follath088ce432017-04-10 12:42:31 +01002659#if defined(MBEDTLS_SSL_SRV_C)
2660void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2661 char cert_req_ca_list )
2662{
2663 conf->cert_req_ca_list = cert_req_ca_list;
2664}
2665#endif
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002668void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002670 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002671}
2672#endif
2673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002675void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002676{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002677 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002678}
2679#endif
2680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002682int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002685 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002688 }
2689
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002690 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002691
2692 return( 0 );
2693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002695
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002696void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002697{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002698 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002699}
2700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002702void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002704 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002705}
2706
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002707void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002708{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002709 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002710}
2711
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002712void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002713 const unsigned char period[8] )
2714{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002715 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002720#if defined(MBEDTLS_SSL_CLI_C)
2721void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002722{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002723 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002724}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002725#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002726
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002727#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002728
Jerry Yud0766ec2022-09-22 10:46:57 +08002729#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002730void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2731 uint16_t num_tickets )
2732{
Jerry Yud0766ec2022-09-22 10:46:57 +08002733 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002734}
2735#endif
2736
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002737void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2738 mbedtls_ssl_ticket_write_t *f_ticket_write,
2739 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2740 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002741{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002742 conf->f_ticket_write = f_ticket_write;
2743 conf->f_ticket_parse = f_ticket_parse;
2744 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002745}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002748
Hanno Becker7e6c1782021-06-08 09:24:55 +01002749void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2750 mbedtls_ssl_export_keys_t *f_export_keys,
2751 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002752{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002753 ssl->f_export_keys = f_export_keys;
2754 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002755}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002756
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002757#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002758void mbedtls_ssl_conf_async_private_cb(
2759 mbedtls_ssl_config *conf,
2760 mbedtls_ssl_async_sign_t *f_async_sign,
2761 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2762 mbedtls_ssl_async_resume_t *f_async_resume,
2763 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002764 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002765{
2766 conf->f_async_sign_start = f_async_sign;
2767 conf->f_async_decrypt_start = f_async_decrypt;
2768 conf->f_async_resume = f_async_resume;
2769 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002770 conf->p_async_config_data = async_config_data;
2771}
2772
Gilles Peskine8f97af72018-04-26 11:46:10 +02002773void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2774{
2775 return( conf->p_async_config_data );
2776}
2777
Gilles Peskine1febfef2018-04-30 11:54:39 +02002778void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002779{
2780 if( ssl->handshake == NULL )
2781 return( NULL );
2782 else
2783 return( ssl->handshake->user_async_ctx );
2784}
2785
Gilles Peskine1febfef2018-04-30 11:54:39 +02002786void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002787 void *ctx )
2788{
2789 if( ssl->handshake != NULL )
2790 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002791}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002792#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002793
Paul Bakker5121ce52009-01-03 21:22:43 +00002794/*
2795 * SSL get accessors
2796 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002797uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002798{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002799 if( ssl->session != NULL )
2800 return( ssl->session->verify_result );
2801
2802 if( ssl->session_negotiate != NULL )
2803 return( ssl->session_negotiate->verify_result );
2804
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002805 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806}
2807
Glenn Strauss8f526902022-01-13 00:04:49 -05002808int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2809{
2810 if( ssl == NULL || ssl->session == NULL )
2811 return( 0 );
2812
2813 return( ssl->session->ciphersuite );
2814}
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002817{
Paul Bakker926c8e42013-03-06 10:23:34 +01002818 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002819 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002822}
2823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002828 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002829 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002830 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002831 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002832 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002833 default:
2834 return( "unknown (DTLS)" );
2835 }
2836 }
2837#endif
2838
Glenn Strauss60bfe602022-03-14 19:04:24 -04002839 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002840 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002841 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002843 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002844 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002845 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002846 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002847 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002848}
2849
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002850#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002851size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2852{
David Horstmann95d516f2021-05-04 18:36:56 +01002853 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002854 size_t read_mfl;
2855
2856 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2857 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2858 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2859 {
2860 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2861 }
2862
2863 /* Check if a smaller max length was negotiated */
2864 if( ssl->session_out != NULL )
2865 {
2866 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2867 if( read_mfl < max_len )
2868 {
2869 max_len = read_mfl;
2870 }
2871 }
2872
2873 // During a handshake, use the value being negotiated
2874 if( ssl->session_negotiate != NULL )
2875 {
2876 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2877 if( read_mfl < max_len )
2878 {
2879 max_len = read_mfl;
2880 }
2881 }
2882
2883 return( max_len );
2884}
2885
2886size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002887{
2888 size_t max_len;
2889
2890 /*
2891 * Assume mfl_code is correct since it was checked when set
2892 */
Angus Grattond8213d02016-05-25 20:56:48 +10002893 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002894
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002895 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002896 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002897 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002898 {
Angus Grattond8213d02016-05-25 20:56:48 +10002899 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002900 }
2901
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002902 /* During a handshake, use the value being negotiated */
2903 if( ssl->session_negotiate != NULL &&
2904 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2905 {
2906 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2907 }
2908
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002909 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002910}
2911#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2912
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002914size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002915{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002916 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2917 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2918 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2919 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2920 return ( 0 );
2921
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002922 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2923 return( ssl->mtu );
2924
2925 if( ssl->mtu == 0 )
2926 return( ssl->handshake->mtu );
2927
2928 return( ssl->mtu < ssl->handshake->mtu ?
2929 ssl->mtu : ssl->handshake->mtu );
2930}
2931#endif /* MBEDTLS_SSL_PROTO_DTLS */
2932
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002933int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2934{
2935 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2936
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002937#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2938 !defined(MBEDTLS_SSL_PROTO_DTLS)
2939 (void) ssl;
2940#endif
2941
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002942#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002943 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002944
2945 if( max_len > mfl )
2946 max_len = mfl;
2947#endif
2948
2949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002950 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002951 {
Hanno Becker89490712020-02-05 10:50:12 +00002952 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002953 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2954 const size_t overhead = (size_t) ret;
2955
2956 if( ret < 0 )
2957 return( ret );
2958
2959 if( mtu <= overhead )
2960 {
2961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2962 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2963 }
2964
2965 if( max_len > mtu - overhead )
2966 max_len = mtu - overhead;
2967 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002968#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002969
Hanno Becker0defedb2018-08-10 12:35:02 +01002970#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2971 !defined(MBEDTLS_SSL_PROTO_DTLS)
2972 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002973#endif
2974
2975 return( (int) max_len );
2976}
2977
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002978int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2979{
2980 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2981
2982#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2983 (void) ssl;
2984#endif
2985
2986#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2987 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2988
2989 if( max_len > mfl )
2990 max_len = mfl;
2991#endif
2992
2993 return( (int) max_len );
2994}
2995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996#if defined(MBEDTLS_X509_CRT_PARSE_C)
2997const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002998{
2999 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003000 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003001
Hanno Beckere6824572019-02-07 13:18:46 +00003002#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003003 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003004#else
3005 return( NULL );
3006#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003011int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3012 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003013{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003014 int ret;
3015
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003016 if( ssl == NULL ||
3017 dst == NULL ||
3018 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003019 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003022 }
3023
Hanno Beckere810bbc2021-05-14 16:01:05 +01003024 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3025 * idempotent: Each session can only be exported once.
3026 *
3027 * (This is in preparation for TLS 1.3 support where we will
3028 * need the ability to export multiple sessions (aka tickets),
3029 * which will be achieved by calling mbedtls_ssl_get_session()
3030 * multiple times until it fails.)
3031 *
3032 * Check whether we have already exported the current session,
3033 * and fail if so.
3034 */
3035 if( ssl->session->exported == 1 )
3036 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3037
3038 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3039 if( ret != 0 )
3040 return( ret );
3041
3042 /* Remember that we've exported the session. */
3043 ssl->session->exported = 1;
3044 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003047
Paul Bakker5121ce52009-01-03 21:22:43 +00003048/*
Hanno Beckera835da52019-05-16 12:39:07 +01003049 * Define ticket header determining Mbed TLS version
3050 * and structure of the ticket.
3051 */
3052
Hanno Becker94ef3b32019-05-16 12:50:45 +01003053/*
Hanno Becker50b59662019-05-28 14:30:45 +01003054 * Define bitflag determining compile-time settings influencing
3055 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003056 */
3057
Hanno Becker50b59662019-05-28 14:30:45 +01003058#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003059#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003060#else
Hanno Becker3e088662019-05-29 11:10:18 +01003061#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003062#endif /* MBEDTLS_HAVE_TIME */
3063
3064#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003065#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003066#else
Hanno Becker3e088662019-05-29 11:10:18 +01003067#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003068#endif /* MBEDTLS_X509_CRT_PARSE_C */
3069
3070#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003071#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003072#else
Hanno Becker3e088662019-05-29 11:10:18 +01003073#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003074#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3075
3076#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003077#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003078#else
Hanno Becker3e088662019-05-29 11:10:18 +01003079#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003080#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3081
Hanno Becker94ef3b32019-05-16 12:50:45 +01003082#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003083#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003084#else
Hanno Becker3e088662019-05-29 11:10:18 +01003085#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003086#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3087
Hanno Becker94ef3b32019-05-16 12:50:45 +01003088#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3089#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3090#else
3091#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3092#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3093
Hanno Becker3e088662019-05-29 11:10:18 +01003094#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3095#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3096#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3097#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003098#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3099#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003100
Hanno Becker50b59662019-05-28 14:30:45 +01003101#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003102 ( (uint16_t) ( \
3103 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3104 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3105 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3106 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003107 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003108 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003109
Hanno Beckerf8787072019-05-16 12:41:07 +01003110static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003111 MBEDTLS_VERSION_MAJOR,
3112 MBEDTLS_VERSION_MINOR,
3113 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003114 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3115 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003116};
Hanno Beckera835da52019-05-16 12:39:07 +01003117
3118/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003119 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003120 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003121 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003122 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003123 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003124 * opaque mbedtls_version[3]; // library version: major, minor, patch
3125 * opaque session_format[2]; // library-version specific 16-bit field
3126 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003127 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003128 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003129 * Note: When updating the format, remember to keep
3130 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003131 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003132 * // In this version, `session_format` determines
3133 * // the setting of those compile-time
3134 * // configuration options which influence
3135 * // the structure of mbedtls_ssl_session.
3136 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003137 * uint8_t minor_ver; // Protocol minor version. Possible values:
3138 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003139 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003140 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003141 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003142 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003143 * serialized_session_tls12 data;
3144 *
3145 * };
3146 *
3147 * } serialized_session;
3148 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003149 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003150
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003151MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003152static int ssl_session_save( const mbedtls_ssl_session *session,
3153 unsigned char omit_header,
3154 unsigned char *buf,
3155 size_t buf_len,
3156 size_t *olen )
3157{
3158 unsigned char *p = buf;
3159 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003160 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003161#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3162 size_t out_len;
3163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3164#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003165 if( session == NULL )
3166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3167
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003168 if( !omit_header )
3169 {
3170 /*
3171 * Add Mbed TLS version identifier
3172 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003173 used += sizeof( ssl_serialized_session_header );
3174
3175 if( used <= buf_len )
3176 {
3177 memcpy( p, ssl_serialized_session_header,
3178 sizeof( ssl_serialized_session_header ) );
3179 p += sizeof( ssl_serialized_session_header );
3180 }
3181 }
3182
3183 /*
3184 * TLS version identifier
3185 */
3186 used += 1;
3187 if( used <= buf_len )
3188 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003189 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003190 }
3191
3192 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003193 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003194 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003195 {
3196#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003197 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003198 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003199 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003200#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3201
Jerry Yu251a12e2022-07-13 15:15:48 +08003202#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3203 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003204 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3205 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003206 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003207 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003208 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003209#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3210
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003211 default:
3212 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3213 }
3214
3215 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003216 if( used > buf_len )
3217 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003218
3219 return( 0 );
3220}
3221
3222/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003223 * Public wrapper for ssl_session_save()
3224 */
3225int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3226 unsigned char *buf,
3227 size_t buf_len,
3228 size_t *olen )
3229{
3230 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3231}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003232
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003233/*
3234 * Deserialize session, see mbedtls_ssl_session_save() for format.
3235 *
3236 * This internal version is wrapped by a public function that cleans up in
3237 * case of error, and has an extra option omit_header.
3238 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003239MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003240static int ssl_session_load( mbedtls_ssl_session *session,
3241 unsigned char omit_header,
3242 const unsigned char *buf,
3243 size_t len )
3244{
3245 const unsigned char *p = buf;
3246 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003247 size_t remaining_len;
3248
3249
3250 if( session == NULL )
3251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003252
3253 if( !omit_header )
3254 {
3255 /*
3256 * Check Mbed TLS version identifier
3257 */
3258
3259 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3261
3262 if( memcmp( p, ssl_serialized_session_header,
3263 sizeof( ssl_serialized_session_header ) ) != 0 )
3264 {
3265 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3266 }
3267 p += sizeof( ssl_serialized_session_header );
3268 }
3269
3270 /*
3271 * TLS version identifier
3272 */
3273 if( 1 > (size_t)( end - p ) )
3274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003275 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003276
3277 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003278 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003279 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003280 {
3281#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003282 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003283 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003284#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3285
Jerry Yu438ddd82022-07-07 06:55:50 +00003286#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3287 case MBEDTLS_SSL_VERSION_TLS1_3:
3288 return( ssl_tls13_session_load( session, p, remaining_len ) );
3289#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3290
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003291 default:
3292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3293 }
3294}
3295
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003296/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003297 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003298 */
3299int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3300 const unsigned char *buf,
3301 size_t len )
3302{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003303 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003304
3305 if( ret != 0 )
3306 mbedtls_ssl_session_free( session );
3307
3308 return( ret );
3309}
3310
3311/*
Paul Bakker1961b702013-01-25 14:49:24 +01003312 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003314MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003315static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3316{
3317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3318
Ronald Cron66dbf912022-02-02 15:33:46 +01003319 /*
3320 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003321 * that were written into the output buffer by the previous handshake step,
3322 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003323 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3324 * We proceed to the next handshake step only when all data from the
3325 * previous one have been sent to the peer, thus we make sure that this is
3326 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3327 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3328 * we have to wait before to go ahead.
3329 * In the case of TLS 1.3, handshake step handlers do not send data to the
3330 * peer. Data are only sent here and through
3331 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003332 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003333 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003334 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3335 return( ret );
3336
3337#if defined(MBEDTLS_SSL_PROTO_DTLS)
3338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3339 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3340 {
3341 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3342 return( ret );
3343 }
3344#endif /* MBEDTLS_SSL_PROTO_DTLS */
3345
3346 return( ret );
3347}
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003350{
Hanno Becker41934dd2021-08-07 19:13:43 +01003351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003352
Hanno Becker41934dd2021-08-07 19:13:43 +01003353 if( ssl == NULL ||
3354 ssl->conf == NULL ||
3355 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003356 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003357 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003359 }
3360
3361 ret = ssl_prepare_handshake_step( ssl );
3362 if( ret != 0 )
3363 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003364
Jerry Yue7047812021-09-13 19:26:39 +08003365 ret = mbedtls_ssl_handle_pending_alert( ssl );
3366 if( ret != 0 )
3367 goto cleanup;
3368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003370 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003371 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3373 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003374
Ronald Cron9f0fba32022-02-10 16:45:15 +01003375 switch( ssl->state )
3376 {
3377 case MBEDTLS_SSL_HELLO_REQUEST:
3378 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3379 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003380
Ronald Cron9f0fba32022-02-10 16:45:15 +01003381 case MBEDTLS_SSL_CLIENT_HELLO:
3382 ret = mbedtls_ssl_write_client_hello( ssl );
3383 break;
3384
3385 default:
3386#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003387 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003388 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3389 else
3390 ret = mbedtls_ssl_handshake_client_step( ssl );
3391#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3392 ret = mbedtls_ssl_handshake_client_step( ssl );
3393#else
3394 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3395#endif
3396 }
Jerry Yub9930e72021-08-06 17:11:51 +08003397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003400 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003401 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003402#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003403 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003404 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003405#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003406
3407#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3408 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3409 ret = mbedtls_ssl_handshake_server_step( ssl );
3410#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003412#endif
3413
Jerry Yue7047812021-09-13 19:26:39 +08003414 if( ret != 0 )
3415 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003416 /* handshake_step return error. And it is same
3417 * with alert_reason.
3418 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003419 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003420 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003421 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003422 goto cleanup;
3423 }
3424 }
3425
3426cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003427 return( ret );
3428}
3429
3430/*
3431 * Perform the SSL handshake
3432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003434{
3435 int ret = 0;
3436
Hanno Beckera817ea42020-10-20 15:20:23 +01003437 /* Sanity checks */
3438
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003439 if( ssl == NULL || ssl->conf == NULL )
3440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3441
Hanno Beckera817ea42020-10-20 15:20:23 +01003442#if defined(MBEDTLS_SSL_PROTO_DTLS)
3443 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3444 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3445 {
3446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3447 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3449 }
3450#endif /* MBEDTLS_SSL_PROTO_DTLS */
3451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003453
Hanno Beckera817ea42020-10-20 15:20:23 +01003454 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003455 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003458
3459 if( ret != 0 )
3460 break;
3461 }
3462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464
3465 return( ret );
3466}
3467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#if defined(MBEDTLS_SSL_RENEGOTIATION)
3469#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003470/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003471 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003472 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003473MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003475{
Janos Follath865b3eb2019-12-16 11:46:15 +00003476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003479
3480 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3482 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003483
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003484 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003485 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003487 return( ret );
3488 }
3489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003491
3492 return( 0 );
3493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003495
3496/*
3497 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 * - any side: calling mbedtls_ssl_renegotiate(),
3499 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3500 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003501 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003502 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003504 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003505int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003506{
Janos Follath865b3eb2019-12-16 11:46:15 +00003507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003510
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003511 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3512 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003513
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003514 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3515 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003519 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003520 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003521 ssl->handshake->out_msg_seq = 1;
3522 else
3523 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003524 }
3525#endif
3526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3528 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003533 return( ret );
3534 }
3535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003537
3538 return( 0 );
3539}
3540
3541/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003542 * Renegotiate current connection on client,
3543 * or request renegotiation on server
3544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003546{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003549 if( ssl == NULL || ssl->conf == NULL )
3550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003553 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003554 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003555 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003556 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003560
3561 /* Did we already try/start sending HelloRequest? */
3562 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003564
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003565 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003570 /*
3571 * On client, either start the renegotiation process or,
3572 * if already in progress, continue the handshake
3573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003575 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003576 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003578
Hanno Becker40cdaa12020-02-05 10:48:27 +00003579 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003580 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003582 return( ret );
3583 }
3584 }
3585 else
3586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003590 return( ret );
3591 }
3592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003594
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003595 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003596}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003598
Gilles Peskine9b562d52018-04-25 20:32:43 +02003599void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003600{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003601 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3602
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003603 if( handshake == NULL )
3604 return;
3605
Brett Warrene0edc842021-08-17 09:53:13 +01003606#if defined(MBEDTLS_ECP_C)
3607#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3608 if ( ssl->handshake->group_list_heap_allocated )
3609 mbedtls_free( (void*) handshake->group_list );
3610 handshake->group_list = NULL;
3611#endif /* MBEDTLS_DEPRECATED_REMOVED */
3612#endif /* MBEDTLS_ECP_C */
3613
Jerry Yuf017ee42022-01-12 15:49:48 +08003614#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3615#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3616 if ( ssl->handshake->sig_algs_heap_allocated )
3617 mbedtls_free( (void*) handshake->sig_algs );
3618 handshake->sig_algs = NULL;
3619#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003620#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3621 if( ssl->handshake->certificate_request_context )
3622 {
3623 mbedtls_free( (void*) handshake->certificate_request_context );
3624 }
3625#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003626#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3627
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003628#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3629 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3630 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003631 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003632 handshake->async_in_progress = 0;
3633 }
3634#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3635
Andrzej Kurek25f27152022-08-17 16:09:31 -04003636#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003637#if defined(MBEDTLS_USE_PSA_CRYPTO)
3638 psa_hash_abort( &handshake->fin_sha256_psa );
3639#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003640 mbedtls_sha256_free( &handshake->fin_sha256 );
3641#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003642#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003643#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003644#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003645 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003646#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003647 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003648#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003649#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651#if defined(MBEDTLS_DHM_C)
3652 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003653#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003654#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003656#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003657#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003658 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003659#if defined(MBEDTLS_SSL_CLI_C)
3660 mbedtls_free( handshake->ecjpake_cache );
3661 handshake->ecjpake_cache = NULL;
3662 handshake->ecjpake_cache_len = 0;
3663#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003664#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003665
Janos Follath4ae5c292016-02-10 11:27:43 +00003666#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3667 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003668 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003670#endif
3671
Gilles Peskineeccd8882020-03-10 12:19:08 +01003672#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003673#if defined(MBEDTLS_USE_PSA_CRYPTO)
3674 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3675 {
3676 /* The maintenance of the external PSK key slot is the
3677 * user's responsibility. */
3678 if( ssl->handshake->psk_opaque_is_internal )
3679 {
3680 psa_destroy_key( ssl->handshake->psk_opaque );
3681 ssl->handshake->psk_opaque_is_internal = 0;
3682 }
3683 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3684 }
Neil Armstronge952a302022-05-03 10:22:14 +02003685#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003686 if( handshake->psk != NULL )
3687 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003688 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003689 mbedtls_free( handshake->psk );
3690 }
Neil Armstronge952a302022-05-03 10:22:14 +02003691#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003692#endif
3693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3695 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003696 /*
3697 * Free only the linked list wrapper, not the keys themselves
3698 * since the belong to the SNI callback
3699 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003700 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003702
Gilles Peskineeccd8882020-03-10 12:19:08 +01003703#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003704 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003705 if( handshake->ecrs_peer_cert != NULL )
3706 {
3707 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3708 mbedtls_free( handshake->ecrs_peer_cert );
3709 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003710#endif
3711
Hanno Becker75173122019-02-06 16:18:31 +00003712#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3713 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3714 mbedtls_pk_free( &handshake->peer_pubkey );
3715#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3716
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003717#if defined(MBEDTLS_SSL_CLI_C) && \
3718 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3719 mbedtls_free( handshake->cookie );
3720#endif /* MBEDTLS_SSL_CLI_C &&
3721 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003722
3723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003724 mbedtls_ssl_flight_free( handshake->flight );
3725 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003726#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003727
Ronald Cronf12b81d2022-03-15 10:42:41 +01003728#if defined(MBEDTLS_ECDH_C) && \
3729 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003730 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003731 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003732#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3733
Ronald Cron6f135e12021-12-08 16:57:54 +01003734#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003735 mbedtls_ssl_transform_free( handshake->transform_handshake );
3736 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003737 mbedtls_free( handshake->transform_earlydata );
3738 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003739#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003740
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003741
3742#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3743 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3744 * processes datagrams and the fact that a datagram is allowed to have
3745 * several records in it, it is possible that the I/O buffers are not
3746 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003747 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3748 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003749#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003750
Jerry Yuba9c7272021-10-30 11:54:10 +08003751 /* mbedtls_platform_zeroize MUST be last one in this function */
3752 mbedtls_platform_zeroize( handshake,
3753 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003754}
3755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003757{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003758 if( session == NULL )
3759 return;
3760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003762 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003763#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003764
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003765#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003767#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003768
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003769#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3770 mbedtls_free( session->hostname );
3771#endif
3772
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003773 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003774}
3775
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003776#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003777
3778#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3779#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3780#else
3781#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3782#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3783
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003784#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003785
3786#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3787#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3788#else
3789#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3790#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3791
3792#if defined(MBEDTLS_SSL_ALPN)
3793#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3794#else
3795#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3796#endif /* MBEDTLS_SSL_ALPN */
3797
3798#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3799#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3800#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3801#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3802
3803#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3804 ( (uint32_t) ( \
3805 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3806 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3807 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3808 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3809 0u ) )
3810
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003811static unsigned char ssl_serialized_context_header[] = {
3812 MBEDTLS_VERSION_MAJOR,
3813 MBEDTLS_VERSION_MINOR,
3814 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003815 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3816 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3817 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3818 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3819 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003820};
3821
Paul Bakker5121ce52009-01-03 21:22:43 +00003822/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003823 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003824 *
3825 * The format of the serialized data is:
3826 * (in the presentation language of TLS, RFC 8446 section 3)
3827 *
3828 * // header
3829 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003830 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003831 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003832 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003833 * Note: When updating the format, remember to keep these
3834 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003835 *
3836 * // session sub-structure
3837 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3838 * // transform sub-structure
3839 * uint8 random[64]; // ServerHello.random+ClientHello.random
3840 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3841 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3842 * // fields from ssl_context
3843 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3844 * uint64 in_window_top; // DTLS: last validated record seq_num
3845 * uint64 in_window; // DTLS: bitmask for replay protection
3846 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3847 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3848 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3849 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3850 *
3851 * Note that many fields of the ssl_context or sub-structures are not
3852 * serialized, as they fall in one of the following categories:
3853 *
3854 * 1. forced value (eg in_left must be 0)
3855 * 2. pointer to dynamically-allocated memory (eg session, transform)
3856 * 3. value can be re-derived from other data (eg session keys from MS)
3857 * 4. value was temporary (eg content of input buffer)
3858 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003859 */
3860int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3861 unsigned char *buf,
3862 size_t buf_len,
3863 size_t *olen )
3864{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003865 unsigned char *p = buf;
3866 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003867 size_t session_len;
3868 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003869
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003870 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003871 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3872 * this function's documentation.
3873 *
3874 * These are due to assumptions/limitations in the implementation. Some of
3875 * them are likely to stay (no handshake in progress) some might go away
3876 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003877 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003878 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003879 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003883 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003884 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003885 {
3886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003888 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003889 /* Double-check that sub-structures are indeed ready */
3890 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003891 {
3892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003894 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003895 /* There must be no pending incoming or outgoing data */
3896 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003897 {
3898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
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 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003902 {
3903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003905 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003906 /* Protocol must be DLTS, not TLS */
3907 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003908 {
3909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003911 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003912 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003913 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003914 {
3915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003917 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003918 /* We must be using an AEAD ciphersuite */
3919 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003920 {
3921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003923 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003924 /* Renegotiation must not be enabled */
3925#if defined(MBEDTLS_SSL_RENEGOTIATION)
3926 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003927 {
3928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003929 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003930 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003931#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003932
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003933 /*
3934 * Version and format identifier
3935 */
3936 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003937
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003938 if( used <= buf_len )
3939 {
3940 memcpy( p, ssl_serialized_context_header,
3941 sizeof( ssl_serialized_context_header ) );
3942 p += sizeof( ssl_serialized_context_header );
3943 }
3944
3945 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003946 * Session (length + data)
3947 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003948 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003949 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3950 return( ret );
3951
3952 used += 4 + session_len;
3953 if( used <= buf_len )
3954 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003955 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3956 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003957
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003958 ret = ssl_session_save( ssl->session, 1,
3959 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003960 if( ret != 0 )
3961 return( ret );
3962
3963 p += session_len;
3964 }
3965
3966 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003967 * Transform
3968 */
3969 used += sizeof( ssl->transform->randbytes );
3970 if( used <= buf_len )
3971 {
3972 memcpy( p, ssl->transform->randbytes,
3973 sizeof( ssl->transform->randbytes ) );
3974 p += sizeof( ssl->transform->randbytes );
3975 }
3976
3977#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3978 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3979 if( used <= buf_len )
3980 {
3981 *p++ = ssl->transform->in_cid_len;
3982 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3983 p += ssl->transform->in_cid_len;
3984
3985 *p++ = ssl->transform->out_cid_len;
3986 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3987 p += ssl->transform->out_cid_len;
3988 }
3989#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3990
3991 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003992 * Saved fields from top-level ssl_context structure
3993 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003994 used += 4;
3995 if( used <= buf_len )
3996 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003997 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3998 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003999 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004000
4001#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4002 used += 16;
4003 if( used <= buf_len )
4004 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004005 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4006 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004007
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004008 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4009 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004010 }
4011#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4012
4013#if defined(MBEDTLS_SSL_PROTO_DTLS)
4014 used += 1;
4015 if( used <= buf_len )
4016 {
4017 *p++ = ssl->disable_datagram_packing;
4018 }
4019#endif /* MBEDTLS_SSL_PROTO_DTLS */
4020
Jerry Yuae0b2e22021-10-08 15:21:19 +08004021 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004022 if( used <= buf_len )
4023 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004024 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4025 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004026 }
4027
4028#if defined(MBEDTLS_SSL_PROTO_DTLS)
4029 used += 2;
4030 if( used <= buf_len )
4031 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004032 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4033 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004034 }
4035#endif /* MBEDTLS_SSL_PROTO_DTLS */
4036
4037#if defined(MBEDTLS_SSL_ALPN)
4038 {
4039 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004040 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004041 : 0;
4042
4043 used += 1 + alpn_len;
4044 if( used <= buf_len )
4045 {
4046 *p++ = alpn_len;
4047
4048 if( ssl->alpn_chosen != NULL )
4049 {
4050 memcpy( p, ssl->alpn_chosen, alpn_len );
4051 p += alpn_len;
4052 }
4053 }
4054 }
4055#endif /* MBEDTLS_SSL_ALPN */
4056
4057 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004058 * Done
4059 */
4060 *olen = used;
4061
4062 if( used > buf_len )
4063 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004064
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004065 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4066
Hanno Becker43aefe22020-02-05 10:44:56 +00004067 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004068}
4069
4070/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004071 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004072 *
4073 * This internal version is wrapped by a public function that cleans up in
4074 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004075 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004076MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004077static int ssl_context_load( mbedtls_ssl_context *ssl,
4078 const unsigned char *buf,
4079 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004080{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004081 const unsigned char *p = buf;
4082 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004083 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004085
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004086 /*
4087 * The context should have been freshly setup or reset.
4088 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004089 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004090 * renegotiating, or if the user mistakenly loaded a session first.)
4091 */
4092 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4093 ssl->session != NULL )
4094 {
4095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4096 }
4097
4098 /*
4099 * We can't check that the config matches the initial one, but we can at
4100 * least check it matches the requirements for serializing.
4101 */
4102 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004103 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4104 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004105#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004106 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004107#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004108 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004109 {
4110 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4111 }
4112
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004113 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4114
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004115 /*
4116 * Check version identifier
4117 */
4118 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4120
4121 if( memcmp( p, ssl_serialized_context_header,
4122 sizeof( ssl_serialized_context_header ) ) != 0 )
4123 {
4124 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4125 }
4126 p += sizeof( ssl_serialized_context_header );
4127
4128 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004129 * Session
4130 */
4131 if( (size_t)( end - p ) < 4 )
4132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4133
4134 session_len = ( (size_t) p[0] << 24 ) |
4135 ( (size_t) p[1] << 16 ) |
4136 ( (size_t) p[2] << 8 ) |
4137 ( (size_t) p[3] );
4138 p += 4;
4139
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004140 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004141 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004142 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004143 ssl->session_in = ssl->session;
4144 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004145 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004146
4147 if( (size_t)( end - p ) < session_len )
4148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4149
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004150 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004151 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004152 {
4153 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004154 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004155 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004156
4157 p += session_len;
4158
4159 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004160 * Transform
4161 */
4162
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004163 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004164 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004165 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004166 ssl->transform_in = ssl->transform;
4167 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004168 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004169
4170 /* Read random bytes and populate structure */
4171 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004173#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004174 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004175 ssl->session->ciphersuite,
4176 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004177#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004178 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004179#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004180 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4181 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004182 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004183 ssl->conf->endpoint,
4184 ssl );
4185 if( ret != 0 )
4186 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004187#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004188 p += sizeof( ssl->transform->randbytes );
4189
4190#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4191 /* Read connection IDs and store them */
4192 if( (size_t)( end - p ) < 1 )
4193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4194
4195 ssl->transform->in_cid_len = *p++;
4196
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004197 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4199
4200 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4201 p += ssl->transform->in_cid_len;
4202
4203 ssl->transform->out_cid_len = *p++;
4204
4205 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4207
4208 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4209 p += ssl->transform->out_cid_len;
4210#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4211
4212 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004213 * Saved fields from top-level ssl_context structure
4214 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004215 if( (size_t)( end - p ) < 4 )
4216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4217
4218 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4219 ( (uint32_t) p[1] << 16 ) |
4220 ( (uint32_t) p[2] << 8 ) |
4221 ( (uint32_t) p[3] );
4222 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004223
4224#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4225 if( (size_t)( end - p ) < 16 )
4226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4227
4228 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4229 ( (uint64_t) p[1] << 48 ) |
4230 ( (uint64_t) p[2] << 40 ) |
4231 ( (uint64_t) p[3] << 32 ) |
4232 ( (uint64_t) p[4] << 24 ) |
4233 ( (uint64_t) p[5] << 16 ) |
4234 ( (uint64_t) p[6] << 8 ) |
4235 ( (uint64_t) p[7] );
4236 p += 8;
4237
4238 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4239 ( (uint64_t) p[1] << 48 ) |
4240 ( (uint64_t) p[2] << 40 ) |
4241 ( (uint64_t) p[3] << 32 ) |
4242 ( (uint64_t) p[4] << 24 ) |
4243 ( (uint64_t) p[5] << 16 ) |
4244 ( (uint64_t) p[6] << 8 ) |
4245 ( (uint64_t) p[7] );
4246 p += 8;
4247#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4248
4249#if defined(MBEDTLS_SSL_PROTO_DTLS)
4250 if( (size_t)( end - p ) < 1 )
4251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4252
4253 ssl->disable_datagram_packing = *p++;
4254#endif /* MBEDTLS_SSL_PROTO_DTLS */
4255
Jerry Yud9a94fe2021-09-28 18:58:59 +08004256 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004258 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4259 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004260
4261#if defined(MBEDTLS_SSL_PROTO_DTLS)
4262 if( (size_t)( end - p ) < 2 )
4263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4264
4265 ssl->mtu = ( p[0] << 8 ) | p[1];
4266 p += 2;
4267#endif /* MBEDTLS_SSL_PROTO_DTLS */
4268
4269#if defined(MBEDTLS_SSL_ALPN)
4270 {
4271 uint8_t alpn_len;
4272 const char **cur;
4273
4274 if( (size_t)( end - p ) < 1 )
4275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4276
4277 alpn_len = *p++;
4278
4279 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4280 {
4281 /* alpn_chosen should point to an item in the configured list */
4282 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4283 {
4284 if( strlen( *cur ) == alpn_len &&
4285 memcmp( p, cur, alpn_len ) == 0 )
4286 {
4287 ssl->alpn_chosen = *cur;
4288 break;
4289 }
4290 }
4291 }
4292
4293 /* can only happen on conf mismatch */
4294 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4296
4297 p += alpn_len;
4298 }
4299#endif /* MBEDTLS_SSL_ALPN */
4300
4301 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004302 * Forced fields from top-level ssl_context structure
4303 *
4304 * Most of them already set to the correct value by mbedtls_ssl_init() and
4305 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4306 */
4307 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004308 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004309
Hanno Becker361b10d2019-08-30 10:42:49 +01004310 /* Adjust pointers for header fields of outgoing records to
4311 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004312 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004313
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004314#if defined(MBEDTLS_SSL_PROTO_DTLS)
4315 ssl->in_epoch = 1;
4316#endif
4317
4318 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4319 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004320 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4321 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004322 if( ssl->handshake != NULL )
4323 {
4324 mbedtls_ssl_handshake_free( ssl );
4325 mbedtls_free( ssl->handshake );
4326 ssl->handshake = NULL;
4327 }
4328
4329 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004330 * Done - should have consumed entire buffer
4331 */
4332 if( p != end )
4333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004334
4335 return( 0 );
4336}
4337
4338/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004339 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004340 */
4341int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4342 const unsigned char *buf,
4343 size_t len )
4344{
4345 int ret = ssl_context_load( context, buf, len );
4346
4347 if( ret != 0 )
4348 mbedtls_ssl_free( context );
4349
4350 return( ret );
4351}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004352#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004353
4354/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 * Free an SSL context
4356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004358{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004359 if( ssl == NULL )
4360 return;
4361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004363
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004364 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 {
sander-visserb8aa2072020-05-06 22:05:13 +02004366#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4367 size_t out_buf_len = ssl->out_buf_len;
4368#else
4369 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4370#endif
4371
Darryl Greenb33cc762019-11-28 14:29:44 +00004372 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004374 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 }
4376
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004377 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004378 {
sander-visserb8aa2072020-05-06 22:05:13 +02004379#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4380 size_t in_buf_len = ssl->in_buf_len;
4381#else
4382 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4383#endif
4384
Darryl Greenb33cc762019-11-28 14:29:44 +00004385 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004387 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004388 }
4389
Paul Bakker48916f92012-09-16 19:57:18 +00004390 if( ssl->transform )
4391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 mbedtls_ssl_transform_free( ssl->transform );
4393 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004394 }
4395
4396 if( ssl->handshake )
4397 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004398 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4400 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 mbedtls_free( ssl->handshake );
4403 mbedtls_free( ssl->transform_negotiate );
4404 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004405 }
4406
Ronald Cron6f135e12021-12-08 16:57:54 +01004407#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004408 mbedtls_ssl_transform_free( ssl->transform_application );
4409 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004410#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004411
Paul Bakkerc0463502013-02-14 11:19:38 +01004412 if( ssl->session )
4413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 mbedtls_ssl_session_free( ssl->session );
4415 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004416 }
4417
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004418#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004419 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004420 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004421 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004424#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004425
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004426#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004428#endif
4429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004431
Paul Bakker86f04f42013-02-14 11:20:09 +01004432 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004433 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434}
4435
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004436/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004437 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004438 */
4439void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4440{
4441 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4442}
4443
Gilles Peskineae270bf2021-06-02 00:05:29 +02004444/* The selection should be the same as mbedtls_x509_crt_profile_default in
4445 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004446 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004447 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004448 * about this list.
4449 */
Brett Warrene0edc842021-08-17 09:53:13 +01004450static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004451#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004452 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004453#endif
4454#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004455 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004456#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004457#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004458 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004459#endif
4460#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004461 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004462#endif
4463#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004464 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004465#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004466#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004467 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004468#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004469#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004470 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004471#endif
4472#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004473 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004474#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004475 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004476};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004477
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004478static int ssl_preset_suiteb_ciphersuites[] = {
4479 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4480 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4481 0
4482};
4483
Gilles Peskineeccd8882020-03-10 12:19:08 +01004484#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004485
Jerry Yu909df7b2022-01-22 11:56:27 +08004486/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004487 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004488 * rules SHOULD be upheld.
4489 * - No duplicate entries.
4490 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004491 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004492 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004493 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004494static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004495
Andrzej Kurek25f27152022-08-17 16:09:31 -04004496#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 +08004497 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4498 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004499#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004500 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004501
Andrzej Kurek25f27152022-08-17 16:09:31 -04004502#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 +08004503 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4504 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004505#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004506 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4507
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#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 +08004509 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4510 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004511#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004512 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004513
Andrzej Kurek25f27152022-08-17 16:09:31 -04004514#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 +08004515 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004516#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 +08004517
Andrzej Kurek25f27152022-08-17 16:09:31 -04004518#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 +08004519 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004520#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 +08004521
Andrzej Kurek25f27152022-08-17 16:09:31 -04004522#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 +08004523 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004524#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 +08004525
Andrzej Kurek25f27152022-08-17 16:09:31 -04004526#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 +08004527 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4528#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4529
Andrzej Kurek25f27152022-08-17 16:09:31 -04004530#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 +08004531 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4532#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4533
Andrzej Kurek25f27152022-08-17 16:09:31 -04004534#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 +08004535 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4536#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4537
Gabor Mezei15b95a62022-05-09 16:37:58 +02004538 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004539};
4540
4541/* NOTICE: see above */
4542#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4543static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004544#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004545#if defined(MBEDTLS_ECDSA_C)
4546 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004547#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004548#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4549 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4550#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004551#if defined(MBEDTLS_RSA_C)
4552 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4553#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004554#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004555#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004556#if defined(MBEDTLS_ECDSA_C)
4557 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004558#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004559#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4560 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4561#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004562#if defined(MBEDTLS_RSA_C)
4563 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4564#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004565#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004566#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004567#if defined(MBEDTLS_ECDSA_C)
4568 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004569#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004570#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4571 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4572#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004573#if defined(MBEDTLS_RSA_C)
4574 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4575#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004576#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004577 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004578};
4579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4580/* NOTICE: see above */
4581static uint16_t ssl_preset_suiteb_sig_algs[] = {
4582
Andrzej Kurek25f27152022-08-17 16:09:31 -04004583#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 +08004584 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4585 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004586#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004587 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4588
Andrzej Kurek25f27152022-08-17 16:09:31 -04004589#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 +08004590 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4591 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004592#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004593 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004594
Andrzej Kurek25f27152022-08-17 16:09:31 -04004595#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 +08004596 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004597#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 +08004598
Andrzej Kurek25f27152022-08-17 16:09:31 -04004599#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 +08004600 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004601#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004602
Gabor Mezei15b95a62022-05-09 16:37:58 +02004603 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004604};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004605
Jerry Yu909df7b2022-01-22 11:56:27 +08004606/* NOTICE: see above */
4607#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4608static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004609#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004610#if defined(MBEDTLS_ECDSA_C)
4611 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004612#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004613#if defined(MBEDTLS_RSA_C)
4614 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4615#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004616#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004617#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004618#if defined(MBEDTLS_ECDSA_C)
4619 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004620#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004621#if defined(MBEDTLS_RSA_C)
4622 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4623#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004624#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004625 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004626};
Jerry Yu909df7b2022-01-22 11:56:27 +08004627#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4628
Jerry Yu1a8b4812022-01-20 17:56:50 +08004629#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004630
Brett Warrene0edc842021-08-17 09:53:13 +01004631static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004632#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004633 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004634#endif
4635#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004636 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004637#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004638 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004639};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004640
Jerry Yu1a8b4812022-01-20 17:56:50 +08004641#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004642/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004643 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004644MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004645static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004646{
4647 size_t i, j;
4648 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004649
Gabor Mezei15b95a62022-05-09 16:37:58 +02004650 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004651 {
Jerry Yuf377d642022-01-25 10:43:59 +08004652 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004653 {
Jerry Yuf377d642022-01-25 10:43:59 +08004654 if( sig_algs[i] != sig_algs[j] )
4655 continue;
4656 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4657 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4658 sig_algs[i], j, i );
4659 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004660 }
4661 }
4662 return( ret );
4663}
4664
4665#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4666
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004667/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004668 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004669 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004670int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004671 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004672{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004673#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004675#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004676
Jerry Yu1a8b4812022-01-20 17:56:50 +08004677#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004678 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004679 {
4680 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4681 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4682 }
4683
Jerry Yuf377d642022-01-25 10:43:59 +08004684 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004685 {
4686 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4687 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4688 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004689
4690#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004691 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004692 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004693 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004694 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4695 }
4696
Jerry Yuf377d642022-01-25 10:43:59 +08004697 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004698 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004699 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004700 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4701 }
4702#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004703#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4704
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004705 /* Use the functions here so that they are covered in tests,
4706 * but otherwise access member directly for efficiency */
4707 mbedtls_ssl_conf_endpoint( conf, endpoint );
4708 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004709
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004710 /*
4711 * Things that are common to all presets
4712 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004713#if defined(MBEDTLS_SSL_CLI_C)
4714 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4715 {
4716 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4717#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4718 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4719#endif
4720 }
4721#endif
4722
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004723#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4724 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4725#endif
4726
4727#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4728 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4729#endif
4730
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004731#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004732 conf->f_cookie_write = ssl_cookie_write_dummy;
4733 conf->f_cookie_check = ssl_cookie_check_dummy;
4734#endif
4735
4736#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4737 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4738#endif
4739
Janos Follath088ce432017-04-10 12:42:31 +01004740#if defined(MBEDTLS_SSL_SRV_C)
4741 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004742 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004743#endif
4744
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004745#if defined(MBEDTLS_SSL_PROTO_DTLS)
4746 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4747 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4748#endif
4749
4750#if defined(MBEDTLS_SSL_RENEGOTIATION)
4751 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004752 memset( conf->renego_period, 0x00, 2 );
4753 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004754#endif
4755
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004756#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004757 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4758 {
4759 const unsigned char dhm_p[] =
4760 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4761 const unsigned char dhm_g[] =
4762 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004763
Hanno Beckere2defad2021-07-24 05:59:17 +01004764 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4765 dhm_p, sizeof( dhm_p ),
4766 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4767 {
4768 return( ret );
4769 }
4770 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004771#endif
4772
Ronald Cron6f135e12021-12-08 16:57:54 +01004773#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004774#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004775 mbedtls_ssl_conf_new_session_tickets(
4776 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4777#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004778 /*
4779 * Allow all TLS 1.3 key exchange modes by default.
4780 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004781 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004782#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004783
XiaokangQian4d3a6042022-04-21 13:46:17 +00004784 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4785 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004786#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004787 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004788 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004789#else
XiaokangQian060d8672022-04-21 09:24:56 +00004790 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004791#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004792 }
4793 else
4794 {
4795#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4796 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4797 {
4798 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4799 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4800 }
4801 else
4802 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4803 {
4804 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4805 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4806 }
4807#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4808 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4809 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4810#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4811 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4812 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4813#else
4814 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4815#endif
4816 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004817
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004818 /*
4819 * Preset-specific defaults
4820 */
4821 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004822 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004823 /*
4824 * NSA Suite B
4825 */
4826 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004827
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004828 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004829
4830#if defined(MBEDTLS_X509_CRT_PARSE_C)
4831 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004832#endif
4833
Gilles Peskineeccd8882020-03-10 12:19:08 +01004834#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004835#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4836 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4837 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4838 else
4839#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4840 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004841#endif
4842
Brett Warrene0edc842021-08-17 09:53:13 +01004843#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4844 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004845#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004846 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004847 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004848
4849 /*
4850 * Default
4851 */
4852 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004853
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004854 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004855
4856#if defined(MBEDTLS_X509_CRT_PARSE_C)
4857 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4858#endif
4859
Gilles Peskineeccd8882020-03-10 12:19:08 +01004860#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004861#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4862 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4863 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4864 else
4865#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4866 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004867#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004868
Brett Warrene0edc842021-08-17 09:53:13 +01004869#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4870 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004871#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004872 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004873
4874#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4875 conf->dhm_min_bitlen = 1024;
4876#endif
4877 }
4878
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004879 return( 0 );
4880}
4881
4882/*
4883 * Free mbedtls_ssl_config
4884 */
4885void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4886{
4887#if defined(MBEDTLS_DHM_C)
4888 mbedtls_mpi_free( &conf->dhm_P );
4889 mbedtls_mpi_free( &conf->dhm_G );
4890#endif
4891
Gilles Peskineeccd8882020-03-10 12:19:08 +01004892#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004893#if defined(MBEDTLS_USE_PSA_CRYPTO)
4894 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4895 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004896 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4897 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004898#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004899 if( conf->psk != NULL )
4900 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004901 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004902 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004903 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004904 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004905 }
4906
4907 if( conf->psk_identity != NULL )
4908 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004909 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004910 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004911 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004912 conf->psk_identity_len = 0;
4913 }
4914#endif
4915
4916#if defined(MBEDTLS_X509_CRT_PARSE_C)
4917 ssl_key_cert_free( conf->key_cert );
4918#endif
4919
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004920 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004921}
4922
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004923#if defined(MBEDTLS_PK_C) && \
4924 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004925/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930#if defined(MBEDTLS_RSA_C)
4931 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4932 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if defined(MBEDTLS_ECDSA_C)
4935 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4936 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004939}
4940
Hanno Becker7e5437a2017-04-28 17:15:26 +01004941unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4942{
4943 switch( type ) {
4944 case MBEDTLS_PK_RSA:
4945 return( MBEDTLS_SSL_SIG_RSA );
4946 case MBEDTLS_PK_ECDSA:
4947 case MBEDTLS_PK_ECKEY:
4948 return( MBEDTLS_SSL_SIG_ECDSA );
4949 default:
4950 return( MBEDTLS_SSL_SIG_ANON );
4951 }
4952}
4953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004955{
4956 switch( sig )
4957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958#if defined(MBEDTLS_RSA_C)
4959 case MBEDTLS_SSL_SIG_RSA:
4960 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962#if defined(MBEDTLS_ECDSA_C)
4963 case MBEDTLS_SSL_SIG_ECDSA:
4964 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004965#endif
4966 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004968 }
4969}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004970#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004971
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004972/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004973 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004976{
4977 switch( hash )
4978 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004979#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 case MBEDTLS_SSL_HASH_MD5:
4981 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004982#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004983#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 case MBEDTLS_SSL_HASH_SHA1:
4985 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004986#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004987#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 case MBEDTLS_SSL_HASH_SHA224:
4989 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004990#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004991#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992 case MBEDTLS_SSL_HASH_SHA256:
4993 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004994#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004995#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 case MBEDTLS_SSL_HASH_SHA384:
4997 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004998#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004999#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 case MBEDTLS_SSL_HASH_SHA512:
5001 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005002#endif
5003 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005005 }
5006}
5007
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005008/*
5009 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5010 */
5011unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5012{
5013 switch( md )
5014 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005015#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005016 case MBEDTLS_MD_MD5:
5017 return( MBEDTLS_SSL_HASH_MD5 );
5018#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005019#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005020 case MBEDTLS_MD_SHA1:
5021 return( MBEDTLS_SSL_HASH_SHA1 );
5022#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005023#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005024 case MBEDTLS_MD_SHA224:
5025 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005026#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005027#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005028 case MBEDTLS_MD_SHA256:
5029 return( MBEDTLS_SSL_HASH_SHA256 );
5030#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005031#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005032 case MBEDTLS_MD_SHA384:
5033 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005034#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005035#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005036 case MBEDTLS_MD_SHA512:
5037 return( MBEDTLS_SSL_HASH_SHA512 );
5038#endif
5039 default:
5040 return( MBEDTLS_SSL_HASH_NONE );
5041 }
5042}
5043
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005044/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005045 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005046 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005047 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005048int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005049{
Brett Warrene0edc842021-08-17 09:53:13 +01005050 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005051
Brett Warrene0edc842021-08-17 09:53:13 +01005052 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005053 return( -1 );
5054
Brett Warrene0edc842021-08-17 09:53:13 +01005055 for( ; *group_list != 0; group_list++ )
5056 {
5057 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005058 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005059 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005060
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005061 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005062}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005063
5064#if defined(MBEDTLS_ECP_C)
5065/*
5066 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5067 */
5068int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5069{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005070 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005071 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005072
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005073 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005074 return -1;
5075
5076 uint16_t tls_id = grp_info->tls_id;
5077
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005078 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5079}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005080#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082#if defined(MBEDTLS_X509_CRT_PARSE_C)
5083int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5084 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005085 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005086 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005087{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005088 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005089 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005090 const char *ext_oid;
5091 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005094 {
5095 /* Server part of the key exchange */
5096 switch( ciphersuite->key_exchange )
5097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 case MBEDTLS_KEY_EXCHANGE_RSA:
5099 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005100 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005101 break;
5102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5104 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5105 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5106 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005107 break;
5108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5110 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005111 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005112 break;
5113
5114 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 case MBEDTLS_KEY_EXCHANGE_NONE:
5116 case MBEDTLS_KEY_EXCHANGE_PSK:
5117 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5118 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005119 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005120 usage = 0;
5121 }
5122 }
5123 else
5124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5126 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005127 }
5128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005130 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005131 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005132 ret = -1;
5133 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5138 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005139 }
5140 else
5141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5143 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005144 }
5145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005147 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005148 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005149 ret = -1;
5150 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005151
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005152 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005155
Jerry Yu148165c2021-09-24 23:20:59 +08005156#if defined(MBEDTLS_USE_PSA_CRYPTO)
5157int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5158 const mbedtls_md_type_t md,
5159 unsigned char *dst,
5160 size_t dst_len,
5161 size_t *olen )
5162{
Ronald Cronf6893e12022-01-07 22:09:01 +01005163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5164 psa_hash_operation_t *hash_operation_to_clone;
5165 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5166
Jerry Yu148165c2021-09-24 23:20:59 +08005167 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005168
5169 switch( md )
5170 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005171#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005172 case MBEDTLS_MD_SHA384:
5173 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5174 break;
5175#endif
5176
Andrzej Kurek25f27152022-08-17 16:09:31 -04005177#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005178 case MBEDTLS_MD_SHA256:
5179 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5180 break;
5181#endif
5182
5183 default:
5184 goto exit;
5185 }
5186
5187 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5188 if( status != PSA_SUCCESS )
5189 goto exit;
5190
5191 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5192 if( status != PSA_SUCCESS )
5193 goto exit;
5194
5195exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005196 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005197}
5198#else /* MBEDTLS_USE_PSA_CRYPTO */
5199
Andrzej Kurek25f27152022-08-17 16:09:31 -04005200#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005201MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005202static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5203 unsigned char *dst,
5204 size_t dst_len,
5205 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005206{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005207 int ret;
5208 mbedtls_sha512_context sha512;
5209
5210 if( dst_len < 48 )
5211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5212
5213 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005214 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005215
5216 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5217 {
5218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5219 goto exit;
5220 }
5221
5222 *olen = 48;
5223
5224exit:
5225
5226 mbedtls_sha512_free( &sha512 );
5227 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005228}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005229#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005230
Andrzej Kurek25f27152022-08-17 16:09:31 -04005231#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005232MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005233static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5234 unsigned char *dst,
5235 size_t dst_len,
5236 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005237{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005238 int ret;
5239 mbedtls_sha256_context sha256;
5240
5241 if( dst_len < 32 )
5242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5243
5244 mbedtls_sha256_init( &sha256 );
5245 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005246
Jerry Yu24c0ec32021-09-09 14:21:07 +08005247 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5248 {
5249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5250 goto exit;
5251 }
5252
5253 *olen = 32;
5254
5255exit:
5256
5257 mbedtls_sha256_free( &sha256 );
5258 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005259}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005260#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005261
Jerry Yu000f9762021-09-14 11:12:51 +08005262int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5263 const mbedtls_md_type_t md,
5264 unsigned char *dst,
5265 size_t dst_len,
5266 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005267{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005268 switch( md )
5269 {
5270
Andrzej Kurek25f27152022-08-17 16:09:31 -04005271#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005272 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005273 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005274#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005275
Andrzej Kurek25f27152022-08-17 16:09:31 -04005276#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005277 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005278 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005279#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005280
5281 default:
5282 break;
5283 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005285}
XiaokangQian647719a2021-12-07 09:16:29 +00005286
Jerry Yu148165c2021-09-24 23:20:59 +08005287#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005288
Gabor Mezei078e8032022-04-27 21:17:56 +02005289#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5290/* mbedtls_ssl_parse_sig_alg_ext()
5291 *
5292 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5293 * value (TLS 1.3 RFC8446):
5294 * enum {
5295 * ....
5296 * ecdsa_secp256r1_sha256( 0x0403 ),
5297 * ecdsa_secp384r1_sha384( 0x0503 ),
5298 * ecdsa_secp521r1_sha512( 0x0603 ),
5299 * ....
5300 * } SignatureScheme;
5301 *
5302 * struct {
5303 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5304 * } SignatureSchemeList;
5305 *
5306 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5307 * value (TLS 1.2 RFC5246):
5308 * enum {
5309 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5310 * sha512(6), (255)
5311 * } HashAlgorithm;
5312 *
5313 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5314 * SignatureAlgorithm;
5315 *
5316 * struct {
5317 * HashAlgorithm hash;
5318 * SignatureAlgorithm signature;
5319 * } SignatureAndHashAlgorithm;
5320 *
5321 * SignatureAndHashAlgorithm
5322 * supported_signature_algorithms<2..2^16-2>;
5323 *
5324 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5325 * generalization of the TLS 1.2 signature algorithm extension.
5326 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5327 * `SignatureScheme` field of TLS 1.3
5328 *
5329 */
5330int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5331 const unsigned char *buf,
5332 const unsigned char *end )
5333{
5334 const unsigned char *p = buf;
5335 size_t supported_sig_algs_len = 0;
5336 const unsigned char *supported_sig_algs_end;
5337 uint16_t sig_alg;
5338 uint32_t common_idx = 0;
5339
5340 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5341 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5342 p += 2;
5343
5344 memset( ssl->handshake->received_sig_algs, 0,
5345 sizeof(ssl->handshake->received_sig_algs) );
5346
5347 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5348 supported_sig_algs_end = p + supported_sig_algs_len;
5349 while( p < supported_sig_algs_end )
5350 {
5351 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5352 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5353 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005354 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5355 sig_alg,
5356 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005357#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005358 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005359 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5360 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5361 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005362 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005363 }
5364#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005365
Jerry Yuf3b46b52022-06-19 16:52:27 +08005366 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5367 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005368
5369 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5370 {
5371 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5372 common_idx += 1;
5373 }
5374 }
5375 /* Check that we consumed all the message. */
5376 if( p != end )
5377 {
5378 MBEDTLS_SSL_DEBUG_MSG( 1,
5379 ( "Signature algorithms extension length misaligned" ) );
5380 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5381 MBEDTLS_ERR_SSL_DECODE_ERROR );
5382 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5383 }
5384
5385 if( common_idx == 0 )
5386 {
5387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5388 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5389 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5390 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5391 }
5392
Gabor Mezei15b95a62022-05-09 16:37:58 +02005393 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005394 return( 0 );
5395}
5396
5397#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5398
Jerry Yudc7bd172022-02-17 13:44:15 +08005399#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5400
5401#if defined(MBEDTLS_USE_PSA_CRYPTO)
5402
5403static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5404 mbedtls_svc_key_id_t key,
5405 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005406 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005407 const unsigned char* seed, size_t seed_length,
5408 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005409 const unsigned char* other_secret,
5410 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005411 size_t capacity )
5412{
5413 psa_status_t status;
5414
5415 status = psa_key_derivation_setup( derivation, alg );
5416 if( status != PSA_SUCCESS )
5417 return( status );
5418
5419 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5420 {
5421 status = psa_key_derivation_input_bytes( derivation,
5422 PSA_KEY_DERIVATION_INPUT_SEED,
5423 seed, seed_length );
5424 if( status != PSA_SUCCESS )
5425 return( status );
5426
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005427 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005428 {
5429 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005430 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5431 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005432 if( status != PSA_SUCCESS )
5433 return( status );
5434 }
5435
Jerry Yudc7bd172022-02-17 13:44:15 +08005436 if( mbedtls_svc_key_id_is_null( key ) )
5437 {
5438 status = psa_key_derivation_input_bytes(
5439 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005440 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005441 }
5442 else
5443 {
5444 status = psa_key_derivation_input_key(
5445 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5446 }
5447 if( status != PSA_SUCCESS )
5448 return( status );
5449
5450 status = psa_key_derivation_input_bytes( derivation,
5451 PSA_KEY_DERIVATION_INPUT_LABEL,
5452 label, label_length );
5453 if( status != PSA_SUCCESS )
5454 return( status );
5455 }
5456 else
5457 {
5458 return( PSA_ERROR_NOT_SUPPORTED );
5459 }
5460
5461 status = psa_key_derivation_set_capacity( derivation, capacity );
5462 if( status != PSA_SUCCESS )
5463 return( status );
5464
5465 return( PSA_SUCCESS );
5466}
5467
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005468MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005469static int tls_prf_generic( mbedtls_md_type_t md_type,
5470 const unsigned char *secret, size_t slen,
5471 const char *label,
5472 const unsigned char *random, size_t rlen,
5473 unsigned char *dstbuf, size_t dlen )
5474{
5475 psa_status_t status;
5476 psa_algorithm_t alg;
5477 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5478 psa_key_derivation_operation_t derivation =
5479 PSA_KEY_DERIVATION_OPERATION_INIT;
5480
5481 if( md_type == MBEDTLS_MD_SHA384 )
5482 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5483 else
5484 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5485
5486 /* Normally a "secret" should be long enough to be impossible to
5487 * find by brute force, and in particular should not be empty. But
5488 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5489 * and for this use case it makes sense to have a 0-length "secret".
5490 * Since the key API doesn't allow importing a key of length 0,
5491 * keep master_key=0, which setup_psa_key_derivation() understands
5492 * to mean a 0-length "secret" input. */
5493 if( slen != 0 )
5494 {
5495 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5496 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5497 psa_set_key_algorithm( &key_attributes, alg );
5498 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5499
5500 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5501 if( status != PSA_SUCCESS )
5502 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5503 }
5504
5505 status = setup_psa_key_derivation( &derivation,
5506 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005507 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005508 random, rlen,
5509 (unsigned char const *) label,
5510 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005511 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005512 dlen );
5513 if( status != PSA_SUCCESS )
5514 {
5515 psa_key_derivation_abort( &derivation );
5516 psa_destroy_key( master_key );
5517 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5518 }
5519
5520 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5521 if( status != PSA_SUCCESS )
5522 {
5523 psa_key_derivation_abort( &derivation );
5524 psa_destroy_key( master_key );
5525 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5526 }
5527
5528 status = psa_key_derivation_abort( &derivation );
5529 if( status != PSA_SUCCESS )
5530 {
5531 psa_destroy_key( master_key );
5532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5533 }
5534
5535 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5536 status = psa_destroy_key( master_key );
5537 if( status != PSA_SUCCESS )
5538 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5539
5540 return( 0 );
5541}
5542
5543#else /* MBEDTLS_USE_PSA_CRYPTO */
5544
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005545MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005546static int tls_prf_generic( mbedtls_md_type_t md_type,
5547 const unsigned char *secret, size_t slen,
5548 const char *label,
5549 const unsigned char *random, size_t rlen,
5550 unsigned char *dstbuf, size_t dlen )
5551{
5552 size_t nb;
5553 size_t i, j, k, md_len;
5554 unsigned char *tmp;
5555 size_t tmp_len = 0;
5556 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5557 const mbedtls_md_info_t *md_info;
5558 mbedtls_md_context_t md_ctx;
5559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5560
5561 mbedtls_md_init( &md_ctx );
5562
5563 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5565
5566 md_len = mbedtls_md_get_size( md_info );
5567
5568 tmp_len = md_len + strlen( label ) + rlen;
5569 tmp = mbedtls_calloc( 1, tmp_len );
5570 if( tmp == NULL )
5571 {
5572 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5573 goto exit;
5574 }
5575
5576 nb = strlen( label );
5577 memcpy( tmp + md_len, label, nb );
5578 memcpy( tmp + md_len + nb, random, rlen );
5579 nb += rlen;
5580
5581 /*
5582 * Compute P_<hash>(secret, label + random)[0..dlen]
5583 */
5584 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5585 goto exit;
5586
5587 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5588 if( ret != 0 )
5589 goto exit;
5590 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5591 if( ret != 0 )
5592 goto exit;
5593 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5594 if( ret != 0 )
5595 goto exit;
5596
5597 for( i = 0; i < dlen; i += md_len )
5598 {
5599 ret = mbedtls_md_hmac_reset ( &md_ctx );
5600 if( ret != 0 )
5601 goto exit;
5602 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5603 if( ret != 0 )
5604 goto exit;
5605 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5606 if( ret != 0 )
5607 goto exit;
5608
5609 ret = mbedtls_md_hmac_reset ( &md_ctx );
5610 if( ret != 0 )
5611 goto exit;
5612 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5613 if( ret != 0 )
5614 goto exit;
5615 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5616 if( ret != 0 )
5617 goto exit;
5618
5619 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5620
5621 for( j = 0; j < k; j++ )
5622 dstbuf[i + j] = h_i[j];
5623 }
5624
5625exit:
5626 mbedtls_md_free( &md_ctx );
5627
5628 mbedtls_platform_zeroize( tmp, tmp_len );
5629 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5630
5631 mbedtls_free( tmp );
5632
5633 return( ret );
5634}
5635#endif /* MBEDTLS_USE_PSA_CRYPTO */
5636
Andrzej Kurek25f27152022-08-17 16:09:31 -04005637#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005638MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005639static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5640 const char *label,
5641 const unsigned char *random, size_t rlen,
5642 unsigned char *dstbuf, size_t dlen )
5643{
5644 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5645 label, random, rlen, dstbuf, dlen ) );
5646}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005647#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005648
Andrzej Kurek25f27152022-08-17 16:09:31 -04005649#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005650MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005651static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5652 const char *label,
5653 const unsigned char *random, size_t rlen,
5654 unsigned char *dstbuf, size_t dlen )
5655{
5656 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5657 label, random, rlen, dstbuf, dlen ) );
5658}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005659#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005660
Jerry Yuf009d862022-02-17 14:01:37 +08005661/*
5662 * Set appropriate PRF function and other SSL / TLS1.2 functions
5663 *
5664 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005665 * - hash associated with the ciphersuite (only used by TLS 1.2)
5666 *
5667 * Outputs:
5668 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5669 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005670MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005671static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005672 mbedtls_md_type_t hash )
5673{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005674#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005675 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005676 {
5677 handshake->tls_prf = tls_prf_sha384;
5678 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5679 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5680 }
5681 else
5682#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005683#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005684 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005685 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005686 handshake->tls_prf = tls_prf_sha256;
5687 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5688 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5689 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005690#else
Jerry Yuf009d862022-02-17 14:01:37 +08005691 {
Jerry Yuf009d862022-02-17 14:01:37 +08005692 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005693 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5695 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005696#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005697
5698 return( 0 );
5699}
Jerry Yud6ab2352022-02-17 14:03:43 +08005700
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005701/*
5702 * Compute master secret if needed
5703 *
5704 * Parameters:
5705 * [in/out] handshake
5706 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5707 * (PSA-PSK) ciphersuite_info, psk_opaque
5708 * [out] premaster (cleared)
5709 * [out] master
5710 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5711 * debug: conf->f_dbg, conf->p_dbg
5712 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005713 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005714 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005715MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005716static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5717 unsigned char *master,
5718 const mbedtls_ssl_context *ssl )
5719{
5720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5721
5722 /* cf. RFC 5246, Section 8.1:
5723 * "The master secret is always exactly 48 bytes in length." */
5724 size_t const master_secret_len = 48;
5725
5726#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5727 unsigned char session_hash[48];
5728#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5729
5730 /* The label for the KDF used for key expansion.
5731 * This is either "master secret" or "extended master secret"
5732 * depending on whether the Extended Master Secret extension
5733 * is used. */
5734 char const *lbl = "master secret";
5735
Przemek Stekielae4ed302022-04-05 17:15:55 +02005736 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005737 * - If the Extended Master Secret extension is not used,
5738 * this is ClientHello.Random + ServerHello.Random
5739 * (see Sect. 8.1 in RFC 5246).
5740 * - If the Extended Master Secret extension is used,
5741 * this is the transcript of the handshake so far.
5742 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005743 unsigned char const *seed = handshake->randbytes;
5744 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005745
5746#if !defined(MBEDTLS_DEBUG_C) && \
5747 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5748 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5749 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5750 ssl = NULL; /* make sure we don't use it except for those cases */
5751 (void) ssl;
5752#endif
5753
5754 if( handshake->resume != 0 )
5755 {
5756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5757 return( 0 );
5758 }
5759
5760#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5761 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5762 {
5763 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005764 seed = session_hash;
5765 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005766
5767 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005768 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005769 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005770#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005771
Przemek Stekiel99114f32022-04-22 11:20:09 +02005772#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005773 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005774 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005775 {
5776 /* Perform PSK-to-MS expansion in a single step. */
5777 psa_status_t status;
5778 psa_algorithm_t alg;
5779 mbedtls_svc_key_id_t psk;
5780 psa_key_derivation_operation_t derivation =
5781 PSA_KEY_DERIVATION_OPERATION_INIT;
5782 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5783
5784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5785
5786 psk = mbedtls_ssl_get_opaque_psk( ssl );
5787
5788 if( hash_alg == MBEDTLS_MD_SHA384 )
5789 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5790 else
5791 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5792
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005793 size_t other_secret_len = 0;
5794 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005795
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005796 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005797 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005798 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005799 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005800 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005801 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005802 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005803 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005804 other_secret_len = 48;
5805 other_secret = handshake->premaster + 2;
5806 break;
5807 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005808 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005809 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5810 other_secret = handshake->premaster + 2;
5811 break;
5812 default:
5813 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005814 }
5815
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005816 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005817 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005818 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005819 (unsigned char const *) lbl,
5820 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005821 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005822 master_secret_len );
5823 if( status != PSA_SUCCESS )
5824 {
5825 psa_key_derivation_abort( &derivation );
5826 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5827 }
5828
5829 status = psa_key_derivation_output_bytes( &derivation,
5830 master,
5831 master_secret_len );
5832 if( status != PSA_SUCCESS )
5833 {
5834 psa_key_derivation_abort( &derivation );
5835 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5836 }
5837
5838 status = psa_key_derivation_abort( &derivation );
5839 if( status != PSA_SUCCESS )
5840 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5841 }
5842 else
5843#endif
5844 {
5845 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005846 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005847 master,
5848 master_secret_len );
5849 if( ret != 0 )
5850 {
5851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5852 return( ret );
5853 }
5854
5855 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5856 handshake->premaster,
5857 handshake->pmslen );
5858
5859 mbedtls_platform_zeroize( handshake->premaster,
5860 sizeof(handshake->premaster) );
5861 }
5862
5863 return( 0 );
5864}
5865
Jerry Yud62f87e2022-02-17 14:09:02 +08005866int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5867{
5868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5869 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5870 ssl->handshake->ciphersuite_info;
5871
5872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5873
5874 /* Set PRF, calc_verify and calc_finished function pointers */
5875 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005876 ciphersuite_info->mac );
5877 if( ret != 0 )
5878 {
5879 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5880 return( ret );
5881 }
5882
5883 /* Compute master secret if needed */
5884 ret = ssl_compute_master( ssl->handshake,
5885 ssl->session_negotiate->master,
5886 ssl );
5887 if( ret != 0 )
5888 {
5889 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5890 return( ret );
5891 }
5892
5893 /* Swap the client and server random values:
5894 * - MS derivation wanted client+server (RFC 5246 8.1)
5895 * - key derivation wants server+client (RFC 5246 6.3) */
5896 {
5897 unsigned char tmp[64];
5898 memcpy( tmp, ssl->handshake->randbytes, 64 );
5899 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5900 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5901 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5902 }
5903
5904 /* Populate transform structure */
5905 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5906 ssl->session_negotiate->ciphersuite,
5907 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005908#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005909 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005910#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005911 ssl->handshake->tls_prf,
5912 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005913 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005914 ssl->conf->endpoint,
5915 ssl );
5916 if( ret != 0 )
5917 {
5918 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5919 return( ret );
5920 }
5921
5922 /* We no longer need Server/ClientHello.random values */
5923 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5924 sizeof( ssl->handshake->randbytes ) );
5925
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5927
5928 return( 0 );
5929}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005930
Ronald Cron4dcbca92022-03-07 10:21:40 +01005931int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5932{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005933 switch( md )
5934 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005935#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005936 case MBEDTLS_SSL_HASH_SHA384:
5937 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5938 break;
5939#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005940#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005941 case MBEDTLS_SSL_HASH_SHA256:
5942 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5943 break;
5944#endif
5945 default:
5946 return( -1 );
5947 }
5948
Ronald Cronc2f13a02022-03-07 10:25:24 +01005949 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005950}
5951
Andrzej Kurek25f27152022-08-17 16:09:31 -04005952#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005953void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5954 unsigned char *hash,
5955 size_t *hlen )
5956{
5957#if defined(MBEDTLS_USE_PSA_CRYPTO)
5958 size_t hash_size;
5959 psa_status_t status;
5960 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5961
5962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5963 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5964 if( status != PSA_SUCCESS )
5965 {
5966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5967 return;
5968 }
5969
5970 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5971 if( status != PSA_SUCCESS )
5972 {
5973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5974 return;
5975 }
5976
5977 *hlen = 32;
5978 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5980#else
5981 mbedtls_sha256_context sha256;
5982
5983 mbedtls_sha256_init( &sha256 );
5984
5985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5986
5987 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5988 mbedtls_sha256_finish( &sha256, hash );
5989
5990 *hlen = 32;
5991
5992 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5994
5995 mbedtls_sha256_free( &sha256 );
5996#endif /* MBEDTLS_USE_PSA_CRYPTO */
5997 return;
5998}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005999#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006000
Andrzej Kurek25f27152022-08-17 16:09:31 -04006001#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006002void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6003 unsigned char *hash,
6004 size_t *hlen )
6005{
6006#if defined(MBEDTLS_USE_PSA_CRYPTO)
6007 size_t hash_size;
6008 psa_status_t status;
6009 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6010
6011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6012 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6013 if( status != PSA_SUCCESS )
6014 {
6015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6016 return;
6017 }
6018
6019 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6020 if( status != PSA_SUCCESS )
6021 {
6022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6023 return;
6024 }
6025
6026 *hlen = 48;
6027 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6029#else
6030 mbedtls_sha512_context sha512;
6031
6032 mbedtls_sha512_init( &sha512 );
6033
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6035
Andrzej Kureka242e832022-08-11 10:03:14 -04006036 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006037 mbedtls_sha512_finish( &sha512, hash );
6038
6039 *hlen = 48;
6040
6041 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6043
6044 mbedtls_sha512_free( &sha512 );
6045#endif /* MBEDTLS_USE_PSA_CRYPTO */
6046 return;
6047}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006048#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006049
Neil Armstrong80f6f322022-05-03 17:56:38 +02006050#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6051 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006052int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6053{
6054 unsigned char *p = ssl->handshake->premaster;
6055 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6056 const unsigned char *psk = NULL;
6057 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006058 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006059
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006060 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006061 {
6062 /*
6063 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006064 * checked before calling this function.
6065 *
6066 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006067 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006068 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006069 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6070 {
6071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6073 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006074 }
6075
6076 /*
6077 * PMS = struct {
6078 * opaque other_secret<0..2^16-1>;
6079 * opaque psk<0..2^16-1>;
6080 * };
6081 * with "other_secret" depending on the particular key exchange
6082 */
6083#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6084 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6085 {
6086 if( end - p < 2 )
6087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6088
6089 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6090 p += 2;
6091
6092 if( end < p || (size_t)( end - p ) < psk_len )
6093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6094
6095 memset( p, 0, psk_len );
6096 p += psk_len;
6097 }
6098 else
6099#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6100#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6101 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6102 {
6103 /*
6104 * other_secret already set by the ClientKeyExchange message,
6105 * and is 48 bytes long
6106 */
6107 if( end - p < 2 )
6108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6109
6110 *p++ = 0;
6111 *p++ = 48;
6112 p += 48;
6113 }
6114 else
6115#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6116#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6117 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6118 {
6119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6120 size_t len;
6121
6122 /* Write length only when we know the actual value */
6123 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6124 p + 2, end - ( p + 2 ), &len,
6125 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6126 {
6127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6128 return( ret );
6129 }
6130 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6131 p += 2 + len;
6132
6133 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6134 }
6135 else
6136#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006137#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006138 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6139 {
6140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6141 size_t zlen;
6142
6143 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6144 p + 2, end - ( p + 2 ),
6145 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6146 {
6147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6148 return( ret );
6149 }
6150
6151 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6152 p += 2 + zlen;
6153
6154 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6155 MBEDTLS_DEBUG_ECDH_Z );
6156 }
6157 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006158#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6162 }
6163
6164 /* opaque psk<0..2^16-1>; */
6165 if( end - p < 2 )
6166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6167
6168 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6169 p += 2;
6170
6171 if( end < p || (size_t)( end - p ) < psk_len )
6172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6173
6174 memcpy( p, psk, psk_len );
6175 p += psk_len;
6176
6177 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6178
6179 return( 0 );
6180}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006181#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006182
6183#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006184MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006185static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6186
6187#if defined(MBEDTLS_SSL_PROTO_DTLS)
6188int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6189{
6190 /* If renegotiation is not enforced, retransmit until we would reach max
6191 * timeout if we were using the usual handshake doubling scheme */
6192 if( ssl->conf->renego_max_records < 0 )
6193 {
6194 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6195 unsigned char doublings = 1;
6196
6197 while( ratio != 0 )
6198 {
6199 ++doublings;
6200 ratio >>= 1;
6201 }
6202
6203 if( ++ssl->renego_records_seen > doublings )
6204 {
6205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6206 return( 0 );
6207 }
6208 }
6209
6210 return( ssl_write_hello_request( ssl ) );
6211}
6212#endif
6213#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006214
Jerry Yud9526692022-02-17 14:23:47 +08006215/*
6216 * Handshake functions
6217 */
6218#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6219/* No certificate support -> dummy functions */
6220int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6221{
6222 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6223 ssl->handshake->ciphersuite_info;
6224
6225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6226
6227 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6228 {
6229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6230 ssl->state++;
6231 return( 0 );
6232 }
6233
6234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6236}
6237
6238int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6239{
6240 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6241 ssl->handshake->ciphersuite_info;
6242
6243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6244
6245 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6246 {
6247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6248 ssl->state++;
6249 return( 0 );
6250 }
6251
6252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6254}
6255
6256#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6257/* Some certificate support -> implement write and parse */
6258
6259int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6260{
6261 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6262 size_t i, n;
6263 const mbedtls_x509_crt *crt;
6264 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6265 ssl->handshake->ciphersuite_info;
6266
6267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6268
6269 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6270 {
6271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6272 ssl->state++;
6273 return( 0 );
6274 }
6275
6276#if defined(MBEDTLS_SSL_CLI_C)
6277 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6278 {
6279 if( ssl->handshake->client_auth == 0 )
6280 {
6281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6282 ssl->state++;
6283 return( 0 );
6284 }
6285 }
6286#endif /* MBEDTLS_SSL_CLI_C */
6287#if defined(MBEDTLS_SSL_SRV_C)
6288 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6289 {
6290 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6291 {
6292 /* Should never happen because we shouldn't have picked the
6293 * ciphersuite if we don't have a certificate. */
6294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6295 }
6296 }
6297#endif
6298
6299 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6300
6301 /*
6302 * 0 . 0 handshake type
6303 * 1 . 3 handshake length
6304 * 4 . 6 length of all certs
6305 * 7 . 9 length of cert. 1
6306 * 10 . n-1 peer certificate
6307 * n . n+2 length of cert. 2
6308 * n+3 . ... upper level cert, etc.
6309 */
6310 i = 7;
6311 crt = mbedtls_ssl_own_cert( ssl );
6312
6313 while( crt != NULL )
6314 {
6315 n = crt->raw.len;
6316 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6317 {
6318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6319 " > %" MBEDTLS_PRINTF_SIZET,
6320 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6321 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6322 }
6323
6324 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6325 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6326 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6327
6328 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6329 i += n; crt = crt->next;
6330 }
6331
6332 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6333 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6334 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6335
6336 ssl->out_msglen = i;
6337 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6338 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6339
6340 ssl->state++;
6341
6342 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6343 {
6344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6345 return( ret );
6346 }
6347
6348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6349
6350 return( ret );
6351}
6352
6353#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6354
6355#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006356MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006357static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6358 unsigned char *crt_buf,
6359 size_t crt_buf_len )
6360{
6361 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6362
6363 if( peer_crt == NULL )
6364 return( -1 );
6365
6366 if( peer_crt->raw.len != crt_buf_len )
6367 return( -1 );
6368
6369 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6370}
6371#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006372MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006373static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6374 unsigned char *crt_buf,
6375 size_t crt_buf_len )
6376{
6377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6378 unsigned char const * const peer_cert_digest =
6379 ssl->session->peer_cert_digest;
6380 mbedtls_md_type_t const peer_cert_digest_type =
6381 ssl->session->peer_cert_digest_type;
6382 mbedtls_md_info_t const * const digest_info =
6383 mbedtls_md_info_from_type( peer_cert_digest_type );
6384 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6385 size_t digest_len;
6386
6387 if( peer_cert_digest == NULL || digest_info == NULL )
6388 return( -1 );
6389
6390 digest_len = mbedtls_md_get_size( digest_info );
6391 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6392 return( -1 );
6393
6394 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6395 if( ret != 0 )
6396 return( -1 );
6397
6398 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6399}
6400#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6401#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6402
6403/*
6404 * Once the certificate message is read, parse it into a cert chain and
6405 * perform basic checks, but leave actual verification to the caller
6406 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006407MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006408static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6409 mbedtls_x509_crt *chain )
6410{
6411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6412#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6413 int crt_cnt=0;
6414#endif
6415 size_t i, n;
6416 uint8_t alert;
6417
6418 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6419 {
6420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6421 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6422 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6423 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6424 }
6425
6426 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6427 {
6428 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6429 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6430 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6431 }
6432
6433 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6434 {
6435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6436 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6437 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6438 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6439 }
6440
6441 i = mbedtls_ssl_hs_hdr_len( ssl );
6442
6443 /*
6444 * Same message structure as in mbedtls_ssl_write_certificate()
6445 */
6446 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6447
6448 if( ssl->in_msg[i] != 0 ||
6449 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6450 {
6451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6452 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6453 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6454 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6455 }
6456
6457 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6458 i += 3;
6459
6460 /* Iterate through and parse the CRTs in the provided chain. */
6461 while( i < ssl->in_hslen )
6462 {
6463 /* Check that there's room for the next CRT's length fields. */
6464 if ( i + 3 > ssl->in_hslen ) {
6465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6466 mbedtls_ssl_send_alert_message( ssl,
6467 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6468 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6469 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6470 }
6471 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6472 * anything beyond 2**16 ~ 64K. */
6473 if( ssl->in_msg[i] != 0 )
6474 {
6475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6476 mbedtls_ssl_send_alert_message( ssl,
6477 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6478 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6479 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6480 }
6481
6482 /* Read length of the next CRT in the chain. */
6483 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6484 | (unsigned int) ssl->in_msg[i + 2];
6485 i += 3;
6486
6487 if( n < 128 || i + n > ssl->in_hslen )
6488 {
6489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6490 mbedtls_ssl_send_alert_message( ssl,
6491 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6492 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6493 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6494 }
6495
6496 /* Check if we're handling the first CRT in the chain. */
6497#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6498 if( crt_cnt++ == 0 &&
6499 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6500 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6501 {
6502 /* During client-side renegotiation, check that the server's
6503 * end-CRTs hasn't changed compared to the initial handshake,
6504 * mitigating the triple handshake attack. On success, reuse
6505 * the original end-CRT instead of parsing it again. */
6506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6507 if( ssl_check_peer_crt_unchanged( ssl,
6508 &ssl->in_msg[i],
6509 n ) != 0 )
6510 {
6511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6512 mbedtls_ssl_send_alert_message( ssl,
6513 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6514 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6515 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6516 }
6517
6518 /* Now we can safely free the original chain. */
6519 ssl_clear_peer_cert( ssl->session );
6520 }
6521#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6522
6523 /* Parse the next certificate in the chain. */
6524#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6525 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6526#else
6527 /* If we don't need to store the CRT chain permanently, parse
6528 * it in-place from the input buffer instead of making a copy. */
6529 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6530#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6531 switch( ret )
6532 {
6533 case 0: /*ok*/
6534 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6535 /* Ignore certificate with an unknown algorithm: maybe a
6536 prior certificate was already trusted. */
6537 break;
6538
6539 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6540 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6541 goto crt_parse_der_failed;
6542
6543 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6544 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6545 goto crt_parse_der_failed;
6546
6547 default:
6548 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6549 crt_parse_der_failed:
6550 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6551 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6552 return( ret );
6553 }
6554
6555 i += n;
6556 }
6557
6558 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6559 return( 0 );
6560}
6561
6562#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006563MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006564static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6565{
6566 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6567 return( -1 );
6568
6569 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6570 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6571 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6572 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6573 {
Ronald Cron19385882022-06-15 16:26:13 +02006574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006575 return( 0 );
6576 }
6577 return( -1 );
6578}
6579#endif /* MBEDTLS_SSL_SRV_C */
6580
6581/* Check if a certificate message is expected.
6582 * Return either
6583 * - SSL_CERTIFICATE_EXPECTED, or
6584 * - SSL_CERTIFICATE_SKIP
6585 * indicating whether a Certificate message is expected or not.
6586 */
6587#define SSL_CERTIFICATE_EXPECTED 0
6588#define SSL_CERTIFICATE_SKIP 1
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_coordinate( mbedtls_ssl_context *ssl,
6591 int authmode )
6592{
6593 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6594 ssl->handshake->ciphersuite_info;
6595
6596 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6597 return( SSL_CERTIFICATE_SKIP );
6598
6599#if defined(MBEDTLS_SSL_SRV_C)
6600 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6601 {
6602 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6603 return( SSL_CERTIFICATE_SKIP );
6604
6605 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6606 {
6607 ssl->session_negotiate->verify_result =
6608 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6609 return( SSL_CERTIFICATE_SKIP );
6610 }
6611 }
6612#else
6613 ((void) authmode);
6614#endif /* MBEDTLS_SSL_SRV_C */
6615
6616 return( SSL_CERTIFICATE_EXPECTED );
6617}
6618
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006619MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006620static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6621 int authmode,
6622 mbedtls_x509_crt *chain,
6623 void *rs_ctx )
6624{
6625 int ret = 0;
6626 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6627 ssl->handshake->ciphersuite_info;
6628 int have_ca_chain = 0;
6629
6630 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6631 void *p_vrfy;
6632
6633 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6634 return( 0 );
6635
6636 if( ssl->f_vrfy != NULL )
6637 {
6638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6639 f_vrfy = ssl->f_vrfy;
6640 p_vrfy = ssl->p_vrfy;
6641 }
6642 else
6643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6645 f_vrfy = ssl->conf->f_vrfy;
6646 p_vrfy = ssl->conf->p_vrfy;
6647 }
6648
6649 /*
6650 * Main check: verify certificate
6651 */
6652#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6653 if( ssl->conf->f_ca_cb != NULL )
6654 {
6655 ((void) rs_ctx);
6656 have_ca_chain = 1;
6657
6658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6659 ret = mbedtls_x509_crt_verify_with_ca_cb(
6660 chain,
6661 ssl->conf->f_ca_cb,
6662 ssl->conf->p_ca_cb,
6663 ssl->conf->cert_profile,
6664 ssl->hostname,
6665 &ssl->session_negotiate->verify_result,
6666 f_vrfy, p_vrfy );
6667 }
6668 else
6669#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6670 {
6671 mbedtls_x509_crt *ca_chain;
6672 mbedtls_x509_crl *ca_crl;
6673
6674#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6675 if( ssl->handshake->sni_ca_chain != NULL )
6676 {
6677 ca_chain = ssl->handshake->sni_ca_chain;
6678 ca_crl = ssl->handshake->sni_ca_crl;
6679 }
6680 else
6681#endif
6682 {
6683 ca_chain = ssl->conf->ca_chain;
6684 ca_crl = ssl->conf->ca_crl;
6685 }
6686
6687 if( ca_chain != NULL )
6688 have_ca_chain = 1;
6689
6690 ret = mbedtls_x509_crt_verify_restartable(
6691 chain,
6692 ca_chain, ca_crl,
6693 ssl->conf->cert_profile,
6694 ssl->hostname,
6695 &ssl->session_negotiate->verify_result,
6696 f_vrfy, p_vrfy, rs_ctx );
6697 }
6698
6699 if( ret != 0 )
6700 {
6701 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6702 }
6703
6704#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6705 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6706 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6707#endif
6708
6709 /*
6710 * Secondary checks: always done, but change 'ret' only if it was 0
6711 */
6712
6713#if defined(MBEDTLS_ECP_C)
6714 {
6715 const mbedtls_pk_context *pk = &chain->pk;
6716
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006717 /* If certificate uses an EC key, make sure the curve is OK.
6718 * This is a public key, so it can't be opaque, so can_do() is a good
6719 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006720 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006721 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006722 /* and in the unlikely case the above assumption no longer holds
6723 * we are making sure that pk_ec() here does not return a NULL
6724 */
6725 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6726 if( ec == NULL )
6727 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6730 }
Jerry Yud9526692022-02-17 14:23:47 +08006731
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006732 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6733 {
6734 ssl->session_negotiate->verify_result |=
6735 MBEDTLS_X509_BADCERT_BAD_KEY;
6736
6737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6738 if( ret == 0 )
6739 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6740 }
Jerry Yud9526692022-02-17 14:23:47 +08006741 }
6742 }
6743#endif /* MBEDTLS_ECP_C */
6744
6745 if( mbedtls_ssl_check_cert_usage( chain,
6746 ciphersuite_info,
6747 ! ssl->conf->endpoint,
6748 &ssl->session_negotiate->verify_result ) != 0 )
6749 {
6750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6751 if( ret == 0 )
6752 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6753 }
6754
6755 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6756 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6757 * with details encoded in the verification flags. All other kinds
6758 * of error codes, including those from the user provided f_vrfy
6759 * functions, are treated as fatal and lead to a failure of
6760 * ssl_parse_certificate even if verification was optional. */
6761 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6762 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6763 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6764 {
6765 ret = 0;
6766 }
6767
6768 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6769 {
6770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6771 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6772 }
6773
6774 if( ret != 0 )
6775 {
6776 uint8_t alert;
6777
6778 /* The certificate may have been rejected for several reasons.
6779 Pick one and send the corresponding alert. Which alert to send
6780 may be a subject of debate in some cases. */
6781 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6782 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6783 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6784 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6785 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6786 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6787 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6788 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6789 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6790 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6791 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6792 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6793 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6794 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6795 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6796 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6797 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6798 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6799 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6800 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6801 else
6802 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6803 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6804 alert );
6805 }
6806
6807#if defined(MBEDTLS_DEBUG_C)
6808 if( ssl->session_negotiate->verify_result != 0 )
6809 {
6810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6811 (unsigned int) ssl->session_negotiate->verify_result ) );
6812 }
6813 else
6814 {
6815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6816 }
6817#endif /* MBEDTLS_DEBUG_C */
6818
6819 return( ret );
6820}
6821
6822#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006823MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006824static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6825 unsigned char *start, size_t len )
6826{
6827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6828 /* Remember digest of the peer's end-CRT. */
6829 ssl->session_negotiate->peer_cert_digest =
6830 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6831 if( ssl->session_negotiate->peer_cert_digest == NULL )
6832 {
6833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6834 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6835 mbedtls_ssl_send_alert_message( ssl,
6836 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6837 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6838
6839 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6840 }
6841
6842 ret = mbedtls_md( mbedtls_md_info_from_type(
6843 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6844 start, len,
6845 ssl->session_negotiate->peer_cert_digest );
6846
6847 ssl->session_negotiate->peer_cert_digest_type =
6848 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6849 ssl->session_negotiate->peer_cert_digest_len =
6850 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6851
6852 return( ret );
6853}
6854
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006855MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006856static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6857 unsigned char *start, size_t len )
6858{
6859 unsigned char *end = start + len;
6860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6861
6862 /* Make a copy of the peer's raw public key. */
6863 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6864 ret = mbedtls_pk_parse_subpubkey( &start, end,
6865 &ssl->handshake->peer_pubkey );
6866 if( ret != 0 )
6867 {
6868 /* We should have parsed the public key before. */
6869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6870 }
6871
6872 return( 0 );
6873}
6874#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6875
6876int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6877{
6878 int ret = 0;
6879 int crt_expected;
6880#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6881 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6882 ? ssl->handshake->sni_authmode
6883 : ssl->conf->authmode;
6884#else
6885 const int authmode = ssl->conf->authmode;
6886#endif
6887 void *rs_ctx = NULL;
6888 mbedtls_x509_crt *chain = NULL;
6889
6890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6891
6892 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6893 if( crt_expected == SSL_CERTIFICATE_SKIP )
6894 {
6895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6896 goto exit;
6897 }
6898
6899#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6900 if( ssl->handshake->ecrs_enabled &&
6901 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6902 {
6903 chain = ssl->handshake->ecrs_peer_cert;
6904 ssl->handshake->ecrs_peer_cert = NULL;
6905 goto crt_verify;
6906 }
6907#endif
6908
6909 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6910 {
6911 /* mbedtls_ssl_read_record may have sent an alert already. We
6912 let it decide whether to alert. */
6913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6914 goto exit;
6915 }
6916
6917#if defined(MBEDTLS_SSL_SRV_C)
6918 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6919 {
6920 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6921
6922 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6923 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6924
6925 goto exit;
6926 }
6927#endif /* MBEDTLS_SSL_SRV_C */
6928
6929 /* Clear existing peer CRT structure in case we tried to
6930 * reuse a session but it failed, and allocate a new one. */
6931 ssl_clear_peer_cert( ssl->session_negotiate );
6932
6933 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6934 if( chain == NULL )
6935 {
6936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6937 sizeof( mbedtls_x509_crt ) ) );
6938 mbedtls_ssl_send_alert_message( ssl,
6939 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6940 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6941
6942 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6943 goto exit;
6944 }
6945 mbedtls_x509_crt_init( chain );
6946
6947 ret = ssl_parse_certificate_chain( ssl, chain );
6948 if( ret != 0 )
6949 goto exit;
6950
6951#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6952 if( ssl->handshake->ecrs_enabled)
6953 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6954
6955crt_verify:
6956 if( ssl->handshake->ecrs_enabled)
6957 rs_ctx = &ssl->handshake->ecrs_ctx;
6958#endif
6959
6960 ret = ssl_parse_certificate_verify( ssl, authmode,
6961 chain, rs_ctx );
6962 if( ret != 0 )
6963 goto exit;
6964
6965#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6966 {
6967 unsigned char *crt_start, *pk_start;
6968 size_t crt_len, pk_len;
6969
6970 /* We parse the CRT chain without copying, so
6971 * these pointers point into the input buffer,
6972 * and are hence still valid after freeing the
6973 * CRT chain. */
6974
6975 crt_start = chain->raw.p;
6976 crt_len = chain->raw.len;
6977
6978 pk_start = chain->pk_raw.p;
6979 pk_len = chain->pk_raw.len;
6980
6981 /* Free the CRT structures before computing
6982 * digest and copying the peer's public key. */
6983 mbedtls_x509_crt_free( chain );
6984 mbedtls_free( chain );
6985 chain = NULL;
6986
6987 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6988 if( ret != 0 )
6989 goto exit;
6990
6991 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6992 if( ret != 0 )
6993 goto exit;
6994 }
6995#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6996 /* Pass ownership to session structure. */
6997 ssl->session_negotiate->peer_cert = chain;
6998 chain = NULL;
6999#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7000
7001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7002
7003exit:
7004
7005 if( ret == 0 )
7006 ssl->state++;
7007
7008#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7009 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7010 {
7011 ssl->handshake->ecrs_peer_cert = chain;
7012 chain = NULL;
7013 }
7014#endif
7015
7016 if( chain != NULL )
7017 {
7018 mbedtls_x509_crt_free( chain );
7019 mbedtls_free( chain );
7020 }
7021
7022 return( ret );
7023}
7024#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7025
Andrzej Kurek25f27152022-08-17 16:09:31 -04007026#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007027static void ssl_calc_finished_tls_sha256(
7028 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7029{
7030 int len = 12;
7031 const char *sender;
7032 unsigned char padbuf[32];
7033#if defined(MBEDTLS_USE_PSA_CRYPTO)
7034 size_t hash_size;
7035 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7036 psa_status_t status;
7037#else
7038 mbedtls_sha256_context sha256;
7039#endif
7040
7041 mbedtls_ssl_session *session = ssl->session_negotiate;
7042 if( !session )
7043 session = ssl->session;
7044
7045 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7046 ? "client finished"
7047 : "server finished";
7048
7049#if defined(MBEDTLS_USE_PSA_CRYPTO)
7050 sha256_psa = psa_hash_operation_init();
7051
7052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7053
7054 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7055 if( status != PSA_SUCCESS )
7056 {
7057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7058 return;
7059 }
7060
7061 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7062 if( status != PSA_SUCCESS )
7063 {
7064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7065 return;
7066 }
7067 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7068#else
7069
7070 mbedtls_sha256_init( &sha256 );
7071
7072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7073
7074 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7075
7076 /*
7077 * TLSv1.2:
7078 * hash = PRF( master, finished_label,
7079 * Hash( handshake ) )[0.11]
7080 */
7081
7082#if !defined(MBEDTLS_SHA256_ALT)
7083 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7084 sha256.state, sizeof( sha256.state ) );
7085#endif
7086
7087 mbedtls_sha256_finish( &sha256, padbuf );
7088 mbedtls_sha256_free( &sha256 );
7089#endif /* MBEDTLS_USE_PSA_CRYPTO */
7090
7091 ssl->handshake->tls_prf( session->master, 48, sender,
7092 padbuf, 32, buf, len );
7093
7094 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7095
7096 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7097
7098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7099}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007100#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007101
Jerry Yub7ba49e2022-02-17 14:25:53 +08007102
Andrzej Kurek25f27152022-08-17 16:09:31 -04007103#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007104static void ssl_calc_finished_tls_sha384(
7105 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7106{
7107 int len = 12;
7108 const char *sender;
7109 unsigned char padbuf[48];
7110#if defined(MBEDTLS_USE_PSA_CRYPTO)
7111 size_t hash_size;
7112 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7113 psa_status_t status;
7114#else
7115 mbedtls_sha512_context sha512;
7116#endif
7117
7118 mbedtls_ssl_session *session = ssl->session_negotiate;
7119 if( !session )
7120 session = ssl->session;
7121
7122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7123 ? "client finished"
7124 : "server finished";
7125
7126#if defined(MBEDTLS_USE_PSA_CRYPTO)
7127 sha384_psa = psa_hash_operation_init();
7128
7129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7130
7131 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7132 if( status != PSA_SUCCESS )
7133 {
7134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7135 return;
7136 }
7137
7138 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7139 if( status != PSA_SUCCESS )
7140 {
7141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7142 return;
7143 }
7144 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7145#else
7146 mbedtls_sha512_init( &sha512 );
7147
7148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7149
Andrzej Kureka242e832022-08-11 10:03:14 -04007150 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007151
7152 /*
7153 * TLSv1.2:
7154 * hash = PRF( master, finished_label,
7155 * Hash( handshake ) )[0.11]
7156 */
7157
7158#if !defined(MBEDTLS_SHA512_ALT)
7159 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7160 sha512.state, sizeof( sha512.state ) );
7161#endif
7162 mbedtls_sha512_finish( &sha512, padbuf );
7163
7164 mbedtls_sha512_free( &sha512 );
7165#endif
7166
7167 ssl->handshake->tls_prf( session->master, 48, sender,
7168 padbuf, 48, buf, len );
7169
7170 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7171
7172 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7173
7174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7175}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007176#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007177
Jerry Yuaef00152022-02-17 14:27:31 +08007178void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7179{
7180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7181
7182 /*
7183 * Free our handshake params
7184 */
7185 mbedtls_ssl_handshake_free( ssl );
7186 mbedtls_free( ssl->handshake );
7187 ssl->handshake = NULL;
7188
7189 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007190 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007191 */
7192 if( ssl->transform )
7193 {
7194 mbedtls_ssl_transform_free( ssl->transform );
7195 mbedtls_free( ssl->transform );
7196 }
7197 ssl->transform = ssl->transform_negotiate;
7198 ssl->transform_negotiate = NULL;
7199
7200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7201}
7202
Jerry Yu2a9fff52022-02-17 14:28:51 +08007203void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7204{
7205 int resume = ssl->handshake->resume;
7206
7207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7208
7209#if defined(MBEDTLS_SSL_RENEGOTIATION)
7210 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7211 {
7212 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7213 ssl->renego_records_seen = 0;
7214 }
7215#endif
7216
7217 /*
7218 * Free the previous session and switch in the current one
7219 */
7220 if( ssl->session )
7221 {
7222#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7223 /* RFC 7366 3.1: keep the EtM state */
7224 ssl->session_negotiate->encrypt_then_mac =
7225 ssl->session->encrypt_then_mac;
7226#endif
7227
7228 mbedtls_ssl_session_free( ssl->session );
7229 mbedtls_free( ssl->session );
7230 }
7231 ssl->session = ssl->session_negotiate;
7232 ssl->session_negotiate = NULL;
7233
7234 /*
7235 * Add cache entry
7236 */
7237 if( ssl->conf->f_set_cache != NULL &&
7238 ssl->session->id_len != 0 &&
7239 resume == 0 )
7240 {
7241 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7242 ssl->session->id,
7243 ssl->session->id_len,
7244 ssl->session ) != 0 )
7245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7246 }
7247
7248#if defined(MBEDTLS_SSL_PROTO_DTLS)
7249 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7250 ssl->handshake->flight != NULL )
7251 {
7252 /* Cancel handshake timer */
7253 mbedtls_ssl_set_timer( ssl, 0 );
7254
7255 /* Keep last flight around in case we need to resend it:
7256 * we need the handshake and transform structures for that */
7257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7258 }
7259 else
7260#endif
7261 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7262
7263 ssl->state++;
7264
7265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7266}
7267
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007268int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7269{
7270 int ret, hash_len;
7271
7272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7273
7274 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7275
7276 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7277
7278 /*
7279 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7280 * may define some other value. Currently (early 2016), no defined
7281 * ciphersuite does this (and this is unlikely to change as activity has
7282 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7283 */
7284 hash_len = 12;
7285
7286#if defined(MBEDTLS_SSL_RENEGOTIATION)
7287 ssl->verify_data_len = hash_len;
7288 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7289#endif
7290
7291 ssl->out_msglen = 4 + hash_len;
7292 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7293 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7294
7295 /*
7296 * In case of session resuming, invert the client and server
7297 * ChangeCipherSpec messages order.
7298 */
7299 if( ssl->handshake->resume != 0 )
7300 {
7301#if defined(MBEDTLS_SSL_CLI_C)
7302 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7303 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7304#endif
7305#if defined(MBEDTLS_SSL_SRV_C)
7306 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7307 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7308#endif
7309 }
7310 else
7311 ssl->state++;
7312
7313 /*
7314 * Switch to our negotiated transform and session parameters for outbound
7315 * data.
7316 */
7317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7318
7319#if defined(MBEDTLS_SSL_PROTO_DTLS)
7320 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7321 {
7322 unsigned char i;
7323
7324 /* Remember current epoch settings for resending */
7325 ssl->handshake->alt_transform_out = ssl->transform_out;
7326 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7327 sizeof( ssl->handshake->alt_out_ctr ) );
7328
7329 /* Set sequence_number to zero */
7330 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7331
7332
7333 /* Increment epoch */
7334 for( i = 2; i > 0; i-- )
7335 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7336 break;
7337
7338 /* The loop goes to its end iff the counter is wrapping */
7339 if( i == 0 )
7340 {
7341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7342 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7343 }
7344 }
7345 else
7346#endif /* MBEDTLS_SSL_PROTO_DTLS */
7347 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7348
7349 ssl->transform_out = ssl->transform_negotiate;
7350 ssl->session_out = ssl->session_negotiate;
7351
7352#if defined(MBEDTLS_SSL_PROTO_DTLS)
7353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7354 mbedtls_ssl_send_flight_completed( ssl );
7355#endif
7356
7357 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7358 {
7359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7360 return( ret );
7361 }
7362
7363#if defined(MBEDTLS_SSL_PROTO_DTLS)
7364 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7365 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7366 {
7367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7368 return( ret );
7369 }
7370#endif
7371
7372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7373
7374 return( 0 );
7375}
7376
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007377#define SSL_MAX_HASH_LEN 12
7378
7379int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7380{
7381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7382 unsigned int hash_len = 12;
7383 unsigned char buf[SSL_MAX_HASH_LEN];
7384
7385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7386
7387 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7388
7389 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7390 {
7391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7392 goto exit;
7393 }
7394
7395 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7396 {
7397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7398 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7399 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7400 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7401 goto exit;
7402 }
7403
7404 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7405 {
7406 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7407 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7408 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7409 goto exit;
7410 }
7411
7412 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7413 {
7414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7415 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7416 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7417 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7418 goto exit;
7419 }
7420
7421 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7422 buf, hash_len ) != 0 )
7423 {
7424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7425 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7426 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7427 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7428 goto exit;
7429 }
7430
7431#if defined(MBEDTLS_SSL_RENEGOTIATION)
7432 ssl->verify_data_len = hash_len;
7433 memcpy( ssl->peer_verify_data, buf, hash_len );
7434#endif
7435
7436 if( ssl->handshake->resume != 0 )
7437 {
7438#if defined(MBEDTLS_SSL_CLI_C)
7439 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7440 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7441#endif
7442#if defined(MBEDTLS_SSL_SRV_C)
7443 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7444 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7445#endif
7446 }
7447 else
7448 ssl->state++;
7449
7450#if defined(MBEDTLS_SSL_PROTO_DTLS)
7451 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7452 mbedtls_ssl_recv_flight_completed( ssl );
7453#endif
7454
7455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7456
7457exit:
7458 mbedtls_platform_zeroize( buf, hash_len );
7459 return( ret );
7460}
7461
Jerry Yu392112c2022-02-17 14:34:10 +08007462#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7463/*
7464 * Helper to get TLS 1.2 PRF from ciphersuite
7465 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7466 */
7467static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7468{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007469#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007470 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7471 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7472
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007473 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007474 return( tls_prf_sha384 );
7475#else
7476 (void) ciphersuite_id;
7477#endif
7478 return( tls_prf_sha256 );
7479}
7480#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007481
7482static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7483{
7484 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007485#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007486 if( tls_prf == tls_prf_sha384 )
7487 {
7488 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7489 }
7490 else
7491#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007492#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007493 if( tls_prf == tls_prf_sha256 )
7494 {
7495 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7496 }
7497 else
7498#endif
7499 return( MBEDTLS_SSL_TLS_PRF_NONE );
7500}
7501
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007502/*
7503 * Populate a transform structure with session keys and all the other
7504 * necessary information.
7505 *
7506 * Parameters:
7507 * - [in/out]: transform: structure to populate
7508 * [in] must be just initialised with mbedtls_ssl_transform_init()
7509 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7510 * - [in] ciphersuite
7511 * - [in] master
7512 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007513 * - [in] tls_prf: pointer to PRF to use for key derivation
7514 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007515 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007516 * - [in] endpoint: client or server
7517 * - [in] ssl: used for:
7518 * - ssl->conf->{f,p}_export_keys
7519 * [in] optionally used for:
7520 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7521 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007522MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007523static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7524 int ciphersuite,
7525 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007526#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007527 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007528#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007529 ssl_tls_prf_t tls_prf,
7530 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007531 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007532 unsigned endpoint,
7533 const mbedtls_ssl_context *ssl )
7534{
7535 int ret = 0;
7536 unsigned char keyblk[256];
7537 unsigned char *key1;
7538 unsigned char *key2;
7539 unsigned char *mac_enc;
7540 unsigned char *mac_dec;
7541 size_t mac_key_len = 0;
7542 size_t iv_copy_len;
7543 size_t keylen;
7544 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007545 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007546#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007547 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007548 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007549#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007550
7551#if defined(MBEDTLS_USE_PSA_CRYPTO)
7552 psa_key_type_t key_type;
7553 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7554 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007555 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007556 size_t key_bits;
7557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7558#endif
7559
7560#if !defined(MBEDTLS_DEBUG_C) && \
7561 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7562 if( ssl->f_export_keys == NULL )
7563 {
7564 ssl = NULL; /* make sure we don't use it except for these cases */
7565 (void) ssl;
7566 }
7567#endif
7568
7569 /*
7570 * Some data just needs copying into the structure
7571 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007572#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007573 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007574#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007575 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007576
7577#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7578 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7579#endif
7580
7581#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007582 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007583 {
7584 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7585 * generation separate. This should never happen. */
7586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7587 }
7588#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7589
7590 /*
7591 * Get various info structures
7592 */
7593 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7594 if( ciphersuite_info == NULL )
7595 {
7596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7597 ciphersuite ) );
7598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7599 }
7600
Neil Armstrongab555e02022-04-04 11:07:59 +02007601 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007602#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007603 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007604#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007605 ciphersuite_info );
7606
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007607 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7608 transform->taglen =
7609 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7610
7611#if defined(MBEDTLS_USE_PSA_CRYPTO)
7612 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7613 transform->taglen,
7614 &alg,
7615 &key_type,
7616 &key_bits ) ) != PSA_SUCCESS )
7617 {
7618 ret = psa_ssl_status_to_mbedtls( status );
7619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7620 goto end;
7621 }
7622#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7624 if( cipher_info == NULL )
7625 {
7626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7627 ciphersuite_info->cipher ) );
7628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7629 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007630#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007631
Neil Armstronge4512952022-03-08 09:08:22 +01007632#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007633 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007634 if( mac_alg == 0 )
7635 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007637 (unsigned) ciphersuite_info->mac ) );
7638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7639 }
7640#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007641 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7642 if( md_info == NULL )
7643 {
7644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7645 (unsigned) ciphersuite_info->mac ) );
7646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7647 }
Neil Armstronge4512952022-03-08 09:08:22 +01007648#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007649
7650#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7651 /* Copy own and peer's CID if the use of the CID
7652 * extension has been negotiated. */
7653 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7654 {
7655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7656
7657 transform->in_cid_len = ssl->own_cid_len;
7658 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7659 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7660 transform->in_cid_len );
7661
7662 transform->out_cid_len = ssl->handshake->peer_cid_len;
7663 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7664 ssl->handshake->peer_cid_len );
7665 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7666 transform->out_cid_len );
7667 }
7668#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7669
7670 /*
7671 * Compute key block using the PRF
7672 */
7673 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7674 if( ret != 0 )
7675 {
7676 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7677 return( ret );
7678 }
7679
7680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7681 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7682 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7683 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7684 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7685
7686 /*
7687 * Determine the appropriate key, IV and MAC length.
7688 */
7689
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007690#if defined(MBEDTLS_USE_PSA_CRYPTO)
7691 keylen = PSA_BITS_TO_BYTES(key_bits);
7692#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007693 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007694#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007695
7696#if defined(MBEDTLS_GCM_C) || \
7697 defined(MBEDTLS_CCM_C) || \
7698 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007699 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007700 {
7701 size_t explicit_ivlen;
7702
7703 transform->maclen = 0;
7704 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007705
7706 /* All modes haves 96-bit IVs, but the length of the static parts vary
7707 * with mode and version:
7708 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7709 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7710 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7711 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7712 * sequence number).
7713 */
7714 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007715#if defined(MBEDTLS_USE_PSA_CRYPTO)
7716 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7717#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007718 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007719#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007720 transform->fixed_ivlen = 12;
7721 else
7722 transform->fixed_ivlen = 4;
7723
7724 /* Minimum length of encrypted record */
7725 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7726 transform->minlen = explicit_ivlen + transform->taglen;
7727 }
7728 else
7729#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7730#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007731 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7732 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7733 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007734 {
Neil Armstronge4512952022-03-08 09:08:22 +01007735#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007736 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007737#else
7738 size_t block_size = cipher_info->block_size;
7739#endif /* MBEDTLS_USE_PSA_CRYPTO */
7740
7741#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007742 /* Get MAC length */
7743 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7744#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007745 /* Initialize HMAC contexts */
7746 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7747 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7748 {
7749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7750 goto end;
7751 }
7752
7753 /* Get MAC length */
7754 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007755#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007756 transform->maclen = mac_key_len;
7757
7758 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007759#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007760 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007761#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007762 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007764
7765 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007766 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007767 transform->minlen = transform->maclen;
7768 else
7769 {
7770 /*
7771 * GenericBlockCipher:
7772 * 1. if EtM is in use: one block plus MAC
7773 * otherwise: * first multiple of blocklen greater than maclen
7774 * 2. IV
7775 */
7776#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007777 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007778 {
7779 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007780 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007781 }
7782 else
7783#endif
7784 {
7785 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007786 + block_size
7787 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007788 }
7789
Glenn Strauss07c64162022-03-14 12:34:51 -04007790 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007791 {
7792 transform->minlen += transform->ivlen;
7793 }
7794 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007795 {
7796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7797 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7798 goto end;
7799 }
7800 }
7801 }
7802 else
7803#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7804 {
7805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7807 }
7808
7809 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7810 (unsigned) keylen,
7811 (unsigned) transform->minlen,
7812 (unsigned) transform->ivlen,
7813 (unsigned) transform->maclen ) );
7814
7815 /*
7816 * Finally setup the cipher contexts, IVs and MAC secrets.
7817 */
7818#if defined(MBEDTLS_SSL_CLI_C)
7819 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7820 {
7821 key1 = keyblk + mac_key_len * 2;
7822 key2 = keyblk + mac_key_len * 2 + keylen;
7823
7824 mac_enc = keyblk;
7825 mac_dec = keyblk + mac_key_len;
7826
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007827 iv_copy_len = ( transform->fixed_ivlen ) ?
7828 transform->fixed_ivlen : transform->ivlen;
7829 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7830 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7831 iv_copy_len );
7832 }
7833 else
7834#endif /* MBEDTLS_SSL_CLI_C */
7835#if defined(MBEDTLS_SSL_SRV_C)
7836 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7837 {
7838 key1 = keyblk + mac_key_len * 2 + keylen;
7839 key2 = keyblk + mac_key_len * 2;
7840
7841 mac_enc = keyblk + mac_key_len;
7842 mac_dec = keyblk;
7843
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007844 iv_copy_len = ( transform->fixed_ivlen ) ?
7845 transform->fixed_ivlen : transform->ivlen;
7846 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7847 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7848 iv_copy_len );
7849 }
7850 else
7851#endif /* MBEDTLS_SSL_SRV_C */
7852 {
7853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7854 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7855 goto end;
7856 }
7857
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007858 if( ssl != NULL && ssl->f_export_keys != NULL )
7859 {
7860 ssl->f_export_keys( ssl->p_export_keys,
7861 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7862 master, 48,
7863 randbytes + 32,
7864 randbytes,
7865 tls_prf_get_type( tls_prf ) );
7866 }
7867
7868#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007869 transform->psa_alg = alg;
7870
7871 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7872 {
7873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7874 psa_set_key_algorithm( &attributes, alg );
7875 psa_set_key_type( &attributes, key_type );
7876
7877 if( ( status = psa_import_key( &attributes,
7878 key1,
7879 PSA_BITS_TO_BYTES( key_bits ),
7880 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7881 {
7882 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7883 ret = psa_ssl_status_to_mbedtls( status );
7884 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7885 goto end;
7886 }
7887
7888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7889
7890 if( ( status = psa_import_key( &attributes,
7891 key2,
7892 PSA_BITS_TO_BYTES( key_bits ),
7893 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7894 {
7895 ret = psa_ssl_status_to_mbedtls( status );
7896 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7897 goto end;
7898 }
7899 }
7900#else
7901 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7902 cipher_info ) ) != 0 )
7903 {
7904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7905 goto end;
7906 }
7907
7908 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7909 cipher_info ) ) != 0 )
7910 {
7911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7912 goto end;
7913 }
7914
7915 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7916 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7917 MBEDTLS_ENCRYPT ) ) != 0 )
7918 {
7919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7920 goto end;
7921 }
7922
7923 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7924 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7925 MBEDTLS_DECRYPT ) ) != 0 )
7926 {
7927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7928 goto end;
7929 }
7930
7931#if defined(MBEDTLS_CIPHER_MODE_CBC)
7932 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7933 {
7934 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7935 MBEDTLS_PADDING_NONE ) ) != 0 )
7936 {
7937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7938 goto end;
7939 }
7940
7941 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7942 MBEDTLS_PADDING_NONE ) ) != 0 )
7943 {
7944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7945 goto end;
7946 }
7947 }
7948#endif /* MBEDTLS_CIPHER_MODE_CBC */
7949#endif /* MBEDTLS_USE_PSA_CRYPTO */
7950
Neil Armstrong29c0c042022-03-17 17:47:28 +01007951#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7952 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7953 For AEAD-based ciphersuites, there is nothing to do here. */
7954 if( mac_key_len != 0 )
7955 {
7956#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007957 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007958
7959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007960 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007961 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7962
7963 if( ( status = psa_import_key( &attributes,
7964 mac_enc, mac_key_len,
7965 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7966 {
7967 ret = psa_ssl_status_to_mbedtls( status );
7968 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7969 goto end;
7970 }
7971
Ronald Cronfb39f152022-03-25 14:36:28 +01007972 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007973 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7974#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7975 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7976#endif
7977 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007978 /* mbedtls_ct_hmac() requires the key to be exportable */
7979 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7980 PSA_KEY_USAGE_VERIFY_HASH );
7981 else
7982 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7983
7984 if( ( status = psa_import_key( &attributes,
7985 mac_dec, mac_key_len,
7986 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7987 {
7988 ret = psa_ssl_status_to_mbedtls( status );
7989 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7990 goto end;
7991 }
7992#else
7993 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7994 if( ret != 0 )
7995 goto end;
7996 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7997 if( ret != 0 )
7998 goto end;
7999#endif /* MBEDTLS_USE_PSA_CRYPTO */
8000 }
8001#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8002
8003 ((void) mac_dec);
8004 ((void) mac_enc);
8005
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008006end:
8007 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8008 return( ret );
8009}
8010
Jerry Yuee40f9d2022-02-17 14:55:16 +08008011#if defined(MBEDTLS_USE_PSA_CRYPTO)
8012int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8013 unsigned char *hash, size_t *hashlen,
8014 unsigned char *data, size_t data_len,
8015 mbedtls_md_type_t md_alg )
8016{
8017 psa_status_t status;
8018 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008019 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008020
8021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8022
8023 if( ( status = psa_hash_setup( &hash_operation,
8024 hash_alg ) ) != PSA_SUCCESS )
8025 {
8026 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8027 goto exit;
8028 }
8029
8030 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8031 64 ) ) != PSA_SUCCESS )
8032 {
8033 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8034 goto exit;
8035 }
8036
8037 if( ( status = psa_hash_update( &hash_operation,
8038 data, data_len ) ) != PSA_SUCCESS )
8039 {
8040 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8041 goto exit;
8042 }
8043
8044 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8045 hashlen ) ) != PSA_SUCCESS )
8046 {
8047 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8048 goto exit;
8049 }
8050
8051exit:
8052 if( status != PSA_SUCCESS )
8053 {
8054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8055 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8056 switch( status )
8057 {
8058 case PSA_ERROR_NOT_SUPPORTED:
8059 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8060 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8061 case PSA_ERROR_BUFFER_TOO_SMALL:
8062 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8063 case PSA_ERROR_INSUFFICIENT_MEMORY:
8064 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8065 default:
8066 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8067 }
8068 }
8069 return( 0 );
8070}
8071
8072#else
8073
8074int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8075 unsigned char *hash, size_t *hashlen,
8076 unsigned char *data, size_t data_len,
8077 mbedtls_md_type_t md_alg )
8078{
8079 int ret = 0;
8080 mbedtls_md_context_t ctx;
8081 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8082 *hashlen = mbedtls_md_get_size( md_info );
8083
8084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8085
8086 mbedtls_md_init( &ctx );
8087
8088 /*
8089 * digitally-signed struct {
8090 * opaque client_random[32];
8091 * opaque server_random[32];
8092 * ServerDHParams params;
8093 * };
8094 */
8095 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8096 {
8097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8098 goto exit;
8099 }
8100 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8101 {
8102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8103 goto exit;
8104 }
8105 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8106 {
8107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8108 goto exit;
8109 }
8110 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8111 {
8112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8113 goto exit;
8114 }
8115 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8116 {
8117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8118 goto exit;
8119 }
8120
8121exit:
8122 mbedtls_md_free( &ctx );
8123
8124 if( ret != 0 )
8125 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8126 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8127
8128 return( ret );
8129}
8130#endif /* MBEDTLS_USE_PSA_CRYPTO */
8131
Jerry Yud9d91da2022-02-17 14:57:06 +08008132#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8133
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008134/* Find the preferred hash for a given signature algorithm. */
8135unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8136 mbedtls_ssl_context *ssl,
8137 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008138{
Gabor Mezei078e8032022-04-27 21:17:56 +02008139 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008140 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008141
8142 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008143 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008144
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008145 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008146 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008147 unsigned int hash_alg_received =
8148 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8149 received_sig_algs[i] );
8150 unsigned int sig_alg_received =
8151 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8152 received_sig_algs[i] );
8153
8154 if( sig_alg == sig_alg_received )
8155 {
8156#if defined(MBEDTLS_USE_PSA_CRYPTO)
8157 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8158 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008159 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008160 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008161
Neil Armstrong96eceb82022-06-30 18:05:05 +02008162 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8163 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8164 PSA_ALG_ECDSA( psa_hash_alg ),
8165 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008166 continue;
8167
Neil Armstrong96eceb82022-06-30 18:05:05 +02008168 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008169 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8170 PSA_ALG_RSA_PKCS1V15_SIGN(
8171 psa_hash_alg ),
8172 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008173 continue;
8174 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008175#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008176
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008177 return( hash_alg_received );
8178 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008179 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008180
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008181 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008182}
8183
8184#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8185
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008186/* Serialization of TLS 1.2 sessions:
8187 *
8188 * struct {
8189 * uint64 start_time;
8190 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008191 * uint8 session_id_len; // at most 32
8192 * opaque session_id[32];
8193 * opaque master[48]; // fixed length in the standard
8194 * uint32 verify_result;
8195 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8196 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8197 * uint32 ticket_lifetime;
8198 * uint8 mfl_code; // up to 255 according to standard
8199 * uint8 encrypt_then_mac; // 0 or 1
8200 * } serialized_session_tls12;
8201 *
8202 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008203static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008204 unsigned char *buf,
8205 size_t buf_len )
8206{
8207 unsigned char *p = buf;
8208 size_t used = 0;
8209
8210#if defined(MBEDTLS_HAVE_TIME)
8211 uint64_t start;
8212#endif
8213#if defined(MBEDTLS_X509_CRT_PARSE_C)
8214#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8215 size_t cert_len;
8216#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8217#endif /* MBEDTLS_X509_CRT_PARSE_C */
8218
8219 /*
8220 * Time
8221 */
8222#if defined(MBEDTLS_HAVE_TIME)
8223 used += 8;
8224
8225 if( used <= buf_len )
8226 {
8227 start = (uint64_t) session->start;
8228
8229 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8230 p += 8;
8231 }
8232#endif /* MBEDTLS_HAVE_TIME */
8233
8234 /*
8235 * Basic mandatory fields
8236 */
8237 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008238 + 1 /* id_len */
8239 + sizeof( session->id )
8240 + sizeof( session->master )
8241 + 4; /* verify_result */
8242
8243 if( used <= buf_len )
8244 {
8245 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8246 p += 2;
8247
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008248 *p++ = MBEDTLS_BYTE_0( session->id_len );
8249 memcpy( p, session->id, 32 );
8250 p += 32;
8251
8252 memcpy( p, session->master, 48 );
8253 p += 48;
8254
8255 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8256 p += 4;
8257 }
8258
8259 /*
8260 * Peer's end-entity certificate
8261 */
8262#if defined(MBEDTLS_X509_CRT_PARSE_C)
8263#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8264 if( session->peer_cert == NULL )
8265 cert_len = 0;
8266 else
8267 cert_len = session->peer_cert->raw.len;
8268
8269 used += 3 + cert_len;
8270
8271 if( used <= buf_len )
8272 {
8273 *p++ = MBEDTLS_BYTE_2( cert_len );
8274 *p++ = MBEDTLS_BYTE_1( cert_len );
8275 *p++ = MBEDTLS_BYTE_0( cert_len );
8276
8277 if( session->peer_cert != NULL )
8278 {
8279 memcpy( p, session->peer_cert->raw.p, cert_len );
8280 p += cert_len;
8281 }
8282 }
8283#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8284 if( session->peer_cert_digest != NULL )
8285 {
8286 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8287 if( used <= buf_len )
8288 {
8289 *p++ = (unsigned char) session->peer_cert_digest_type;
8290 *p++ = (unsigned char) session->peer_cert_digest_len;
8291 memcpy( p, session->peer_cert_digest,
8292 session->peer_cert_digest_len );
8293 p += session->peer_cert_digest_len;
8294 }
8295 }
8296 else
8297 {
8298 used += 2;
8299 if( used <= buf_len )
8300 {
8301 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8302 *p++ = 0;
8303 }
8304 }
8305#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8306#endif /* MBEDTLS_X509_CRT_PARSE_C */
8307
8308 /*
8309 * Session ticket if any, plus associated data
8310 */
8311#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8312 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8313
8314 if( used <= buf_len )
8315 {
8316 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8317 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8318 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8319
8320 if( session->ticket != NULL )
8321 {
8322 memcpy( p, session->ticket, session->ticket_len );
8323 p += session->ticket_len;
8324 }
8325
8326 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8327 p += 4;
8328 }
8329#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8330
8331 /*
8332 * Misc extension-related info
8333 */
8334#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8335 used += 1;
8336
8337 if( used <= buf_len )
8338 *p++ = session->mfl_code;
8339#endif
8340
8341#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8342 used += 1;
8343
8344 if( used <= buf_len )
8345 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8346#endif
8347
8348 return( used );
8349}
8350
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008351MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008352static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008353 const unsigned char *buf,
8354 size_t len )
8355{
8356#if defined(MBEDTLS_HAVE_TIME)
8357 uint64_t start;
8358#endif
8359#if defined(MBEDTLS_X509_CRT_PARSE_C)
8360#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8361 size_t cert_len;
8362#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8363#endif /* MBEDTLS_X509_CRT_PARSE_C */
8364
8365 const unsigned char *p = buf;
8366 const unsigned char * const end = buf + len;
8367
8368 /*
8369 * Time
8370 */
8371#if defined(MBEDTLS_HAVE_TIME)
8372 if( 8 > (size_t)( end - p ) )
8373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8374
8375 start = ( (uint64_t) p[0] << 56 ) |
8376 ( (uint64_t) p[1] << 48 ) |
8377 ( (uint64_t) p[2] << 40 ) |
8378 ( (uint64_t) p[3] << 32 ) |
8379 ( (uint64_t) p[4] << 24 ) |
8380 ( (uint64_t) p[5] << 16 ) |
8381 ( (uint64_t) p[6] << 8 ) |
8382 ( (uint64_t) p[7] );
8383 p += 8;
8384
8385 session->start = (time_t) start;
8386#endif /* MBEDTLS_HAVE_TIME */
8387
8388 /*
8389 * Basic mandatory fields
8390 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008391 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8393
8394 session->ciphersuite = ( p[0] << 8 ) | p[1];
8395 p += 2;
8396
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008397 session->id_len = *p++;
8398 memcpy( session->id, p, 32 );
8399 p += 32;
8400
8401 memcpy( session->master, p, 48 );
8402 p += 48;
8403
8404 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8405 ( (uint32_t) p[1] << 16 ) |
8406 ( (uint32_t) p[2] << 8 ) |
8407 ( (uint32_t) p[3] );
8408 p += 4;
8409
8410 /* Immediately clear invalid pointer values that have been read, in case
8411 * we exit early before we replaced them with valid ones. */
8412#if defined(MBEDTLS_X509_CRT_PARSE_C)
8413#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8414 session->peer_cert = NULL;
8415#else
8416 session->peer_cert_digest = NULL;
8417#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8418#endif /* MBEDTLS_X509_CRT_PARSE_C */
8419#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8420 session->ticket = NULL;
8421#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8422
8423 /*
8424 * Peer certificate
8425 */
8426#if defined(MBEDTLS_X509_CRT_PARSE_C)
8427#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8428 /* Deserialize CRT from the end of the ticket. */
8429 if( 3 > (size_t)( end - p ) )
8430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8431
8432 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8433 p += 3;
8434
8435 if( cert_len != 0 )
8436 {
8437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8438
8439 if( cert_len > (size_t)( end - p ) )
8440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8441
8442 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8443
8444 if( session->peer_cert == NULL )
8445 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8446
8447 mbedtls_x509_crt_init( session->peer_cert );
8448
8449 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8450 p, cert_len ) ) != 0 )
8451 {
8452 mbedtls_x509_crt_free( session->peer_cert );
8453 mbedtls_free( session->peer_cert );
8454 session->peer_cert = NULL;
8455 return( ret );
8456 }
8457
8458 p += cert_len;
8459 }
8460#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8461 /* Deserialize CRT digest from the end of the ticket. */
8462 if( 2 > (size_t)( end - p ) )
8463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8464
8465 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8466 session->peer_cert_digest_len = (size_t) *p++;
8467
8468 if( session->peer_cert_digest_len != 0 )
8469 {
8470 const mbedtls_md_info_t *md_info =
8471 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8472 if( md_info == NULL )
8473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8474 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8476
8477 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8479
8480 session->peer_cert_digest =
8481 mbedtls_calloc( 1, session->peer_cert_digest_len );
8482 if( session->peer_cert_digest == NULL )
8483 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8484
8485 memcpy( session->peer_cert_digest, p,
8486 session->peer_cert_digest_len );
8487 p += session->peer_cert_digest_len;
8488 }
8489#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8490#endif /* MBEDTLS_X509_CRT_PARSE_C */
8491
8492 /*
8493 * Session ticket and associated data
8494 */
8495#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8496 if( 3 > (size_t)( end - p ) )
8497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8498
8499 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8500 p += 3;
8501
8502 if( session->ticket_len != 0 )
8503 {
8504 if( session->ticket_len > (size_t)( end - p ) )
8505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8506
8507 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8508 if( session->ticket == NULL )
8509 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8510
8511 memcpy( session->ticket, p, session->ticket_len );
8512 p += session->ticket_len;
8513 }
8514
8515 if( 4 > (size_t)( end - p ) )
8516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8517
8518 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8519 ( (uint32_t) p[1] << 16 ) |
8520 ( (uint32_t) p[2] << 8 ) |
8521 ( (uint32_t) p[3] );
8522 p += 4;
8523#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8524
8525 /*
8526 * Misc extension-related info
8527 */
8528#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8529 if( 1 > (size_t)( end - p ) )
8530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8531
8532 session->mfl_code = *p++;
8533#endif
8534
8535#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8536 if( 1 > (size_t)( end - p ) )
8537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8538
8539 session->encrypt_then_mac = *p++;
8540#endif
8541
8542 /* Done, should have consumed entire buffer */
8543 if( p != end )
8544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8545
8546 return( 0 );
8547}
Jerry Yudc7bd172022-02-17 13:44:15 +08008548#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8549
XiaokangQian75d40ef2022-04-20 11:05:24 +00008550int mbedtls_ssl_validate_ciphersuite(
8551 const mbedtls_ssl_context *ssl,
8552 const mbedtls_ssl_ciphersuite_t *suite_info,
8553 mbedtls_ssl_protocol_version min_tls_version,
8554 mbedtls_ssl_protocol_version max_tls_version )
8555{
8556 (void) ssl;
8557
8558 if( suite_info == NULL )
8559 return( -1 );
8560
8561 if( ( suite_info->min_tls_version > max_tls_version ) ||
8562 ( suite_info->max_tls_version < min_tls_version ) )
8563 {
8564 return( -1 );
8565 }
8566
XiaokangQian060d8672022-04-21 09:24:56 +00008567#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008568#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8569 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8570 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8571 {
8572 return( -1 );
8573 }
8574#endif
8575
8576 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8577#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8578 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8579 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8580 {
8581 return( -1 );
8582 }
8583#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8585
8586 return( 0 );
8587}
8588
XiaokangQianeaf36512022-04-24 09:07:44 +00008589#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8590/*
8591 * Function for writing a signature algorithm extension.
8592 *
8593 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8594 * value (TLS 1.3 RFC8446):
8595 * enum {
8596 * ....
8597 * ecdsa_secp256r1_sha256( 0x0403 ),
8598 * ecdsa_secp384r1_sha384( 0x0503 ),
8599 * ecdsa_secp521r1_sha512( 0x0603 ),
8600 * ....
8601 * } SignatureScheme;
8602 *
8603 * struct {
8604 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8605 * } SignatureSchemeList;
8606 *
8607 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8608 * value (TLS 1.2 RFC5246):
8609 * enum {
8610 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8611 * sha512(6), (255)
8612 * } HashAlgorithm;
8613 *
8614 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8615 * SignatureAlgorithm;
8616 *
8617 * struct {
8618 * HashAlgorithm hash;
8619 * SignatureAlgorithm signature;
8620 * } SignatureAndHashAlgorithm;
8621 *
8622 * SignatureAndHashAlgorithm
8623 * supported_signature_algorithms<2..2^16-2>;
8624 *
8625 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8626 * generalization of the TLS 1.2 signature algorithm extension.
8627 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8628 * `SignatureScheme` field of TLS 1.3
8629 *
8630 */
8631int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8632 const unsigned char *end, size_t *out_len )
8633{
8634 unsigned char *p = buf;
8635 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8636 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8637
8638 *out_len = 0;
8639
8640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8641
8642 /* Check if we have space for header and length field:
8643 * - extension_type (2 bytes)
8644 * - extension_data_length (2 bytes)
8645 * - supported_signature_algorithms_length (2 bytes)
8646 */
8647 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8648 p += 6;
8649
8650 /*
8651 * Write supported_signature_algorithms
8652 */
8653 supported_sig_alg = p;
8654 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8655 if( sig_alg == NULL )
8656 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8657
8658 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8659 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8661 *sig_alg,
8662 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008663 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8664 continue;
8665 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8666 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8667 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008669 *sig_alg,
8670 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008671 }
8672
8673 /* Length of supported_signature_algorithms */
8674 supported_sig_alg_len = p - supported_sig_alg;
8675 if( supported_sig_alg_len == 0 )
8676 {
8677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8679 }
8680
XiaokangQianeaf36512022-04-24 09:07:44 +00008681 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008682 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008683 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8684
XiaokangQianeaf36512022-04-24 09:07:44 +00008685 *out_len = p - buf;
8686
8687#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8688 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8689#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8690 return( 0 );
8691}
8692#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8693
XiaokangQian40a35232022-05-07 09:02:40 +00008694#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008695/*
8696 * mbedtls_ssl_parse_server_name_ext
8697 *
8698 * Structure of server_name extension:
8699 *
8700 * enum {
8701 * host_name(0), (255)
8702 * } NameType;
8703 * opaque HostName<1..2^16-1>;
8704 *
8705 * struct {
8706 * NameType name_type;
8707 * select (name_type) {
8708 * case host_name: HostName;
8709 * } name;
8710 * } ServerName;
8711 * struct {
8712 * ServerName server_name_list<1..2^16-1>
8713 * } ServerNameList;
8714 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008715MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008716int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8717 const unsigned char *buf,
8718 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008719{
8720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8721 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008722 size_t server_name_list_len, hostname_len;
8723 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008724
XiaokangQianf2a94202022-05-20 06:44:24 +00008725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008726
8727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008728 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008729 p += 2;
8730
XiaokangQian9b2b7712022-05-17 02:57:00 +00008731 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8732 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008733 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008734 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008735 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008736 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008737 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8738 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008739
8740 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8741 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008742 /* sni_name is intended to be used only during the parsing of the
8743 * ClientHello message (it is reset to NULL before the end of
8744 * the message parsing). Thus it is ok to just point to the
8745 * reception buffer and not make a copy of it.
8746 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008747 ssl->handshake->sni_name = p + 3;
8748 ssl->handshake->sni_name_len = hostname_len;
8749 if( ssl->conf->f_sni == NULL )
8750 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008751 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008752 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008753 if( ret != 0 )
8754 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008755 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008756 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8757 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008758 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8759 }
8760 return( 0 );
8761 }
8762
8763 p += hostname_len + 3;
8764 }
8765
8766 return( 0 );
8767}
8768#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8769
XiaokangQianacb39922022-06-17 10:18:48 +00008770#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008771MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008772int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8773 const unsigned char *buf,
8774 const unsigned char *end )
8775{
8776 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008777 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008778 const unsigned char *protocol_name_list;
8779 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008780 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008781
8782 /* If ALPN not configured, just ignore the extension */
8783 if( ssl->conf->alpn_list == NULL )
8784 return( 0 );
8785
8786 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008787 * RFC7301, section 3.1
8788 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008789 *
XiaokangQianc7403452022-06-23 03:24:12 +00008790 * struct {
8791 * ProtocolName protocol_name_list<2..2^16-1>
8792 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008793 */
8794
XiaokangQianc7403452022-06-23 03:24:12 +00008795 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008796 * protocol_name_list_len 2 bytes
8797 * protocol_name_len 1 bytes
8798 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008799 */
XiaokangQianacb39922022-06-17 10:18:48 +00008800 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8801
XiaokangQianc7403452022-06-23 03:24:12 +00008802 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008803 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008804 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008805 protocol_name_list = p;
8806 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008807
8808 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008809 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008810 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008811 protocol_name_len = *p++;
8812 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8813 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008814 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008815 {
8816 MBEDTLS_SSL_PEND_FATAL_ALERT(
8817 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8818 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008819 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008820 }
8821
8822 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008823 }
8824
8825 /* Use our order of preference */
8826 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8827 {
8828 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008829 p = protocol_name_list;
8830 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008831 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008832 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008833 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008834 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008835 {
8836 ssl->alpn_chosen = *alpn;
8837 return( 0 );
8838 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008839
8840 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008841 }
8842 }
8843
XiaokangQian95d5f542022-06-24 02:29:26 +00008844 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008845 MBEDTLS_SSL_PEND_FATAL_ALERT(
8846 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8847 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8848 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8849}
8850
8851int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8852 unsigned char *buf,
8853 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008854 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008855{
8856 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008857 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008858 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008859
8860 if( ssl->alpn_chosen == NULL )
8861 {
8862 return( 0 );
8863 }
8864
XiaokangQian95d5f542022-06-24 02:29:26 +00008865 protocol_name_len = strlen( ssl->alpn_chosen );
8866 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008867
8868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8869 /*
8870 * 0 . 1 ext identifier
8871 * 2 . 3 ext length
8872 * 4 . 5 protocol list length
8873 * 6 . 6 protocol name length
8874 * 7 . 7+n protocol name
8875 */
8876 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8877
XiaokangQian95d5f542022-06-24 02:29:26 +00008878 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008879
XiaokangQian95d5f542022-06-24 02:29:26 +00008880 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8881 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008882 /* Note: the length of the chosen protocol has been checked to be less
8883 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8884 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008885 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008886
XiaokangQian95d5f542022-06-24 02:29:26 +00008887 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008888 return ( 0 );
8889}
8890#endif /* MBEDTLS_SSL_ALPN */
8891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#endif /* MBEDTLS_SSL_TLS_C */