blob: 6563b907c5cbd4d0395a1465a26f585772711711 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020057#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254 if( src->peer_cert != NULL )
255 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200257
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200258 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200265 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 dst->peer_cert = NULL;
269 return( ret );
270 }
271 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 if( src->peer_cert_digest != NULL )
274 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 if( dst->peer_cert_digest == NULL )
278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
279
280 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
281 src->peer_cert_digest_len );
282 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000283 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 }
285#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200289#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290 if( src->ticket != NULL )
291 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200293 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 memcpy( dst->ticket, src->ticket, src->ticket_len );
297 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000300#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000301 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000302 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
303 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000304 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000305 {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000306 dst->hostname = NULL;
Xiaokang Qian03409292022-10-12 02:49:52 +0000307 return mbedtls_ssl_session_set_hostname( dst,
308 src->hostname );
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>;
1961 * select ( endpoint ) {
1962 * case client: ClientOnlyData;
1963 * case server: uint64 start_time;
1964 * };
1965 * } serialized_session_tls13;
1966 *
1967 */
1968#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001969MBEDTLS_CHECK_RETURN_CRITICAL
1970static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1971 unsigned char *buf,
1972 size_t buf_len,
1973 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001974{
1975 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001976#if defined(MBEDTLS_SSL_CLI_C) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00001977 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001978 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1979 size_t hostname_len = ( session->hostname == NULL ) ?
1980 0 : strlen( session->hostname );
1981#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001982 size_t needed = 1 /* endpoint */
1983 + 2 /* ciphersuite */
1984 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001985 + 1 /* ticket_flags */
1986 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001987 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001988
1989 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1991 needed += session->resumption_key_len; /* resumption_key */
1992
Jerry Yu438ddd82022-07-07 06:55:50 +00001993#if defined(MBEDTLS_HAVE_TIME)
1994 needed += 8; /* start_time or ticket_received */
1995#endif
1996
1997#if defined(MBEDTLS_SSL_CLI_C)
1998 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1999 {
Xiaokang Qian03409292022-10-12 02:49:52 +00002000#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2001 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002002 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002003 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002004#endif
2005
Jerry Yu438ddd82022-07-07 06:55:50 +00002006 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002007 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002008
2009 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002010 if( session->ticket_len > SIZE_MAX - needed )
2011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002012
Jerry Yue28d9742022-08-18 15:44:03 +08002013 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002014 }
2015#endif /* MBEDTLS_SSL_CLI_C */
2016
Jerry Yue36fdd62022-08-17 21:31:36 +08002017 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002018 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002019 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002020
2021 p[0] = session->endpoint;
2022 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2023 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2024 p[7] = session->ticket_flags;
2025
2026 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002027 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002028 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002029 memcpy( p, session->resumption_key, session->resumption_key_len );
2030 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002031
Xiaokang Qian03409292022-10-12 02:49:52 +00002032#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2033 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2034 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002035 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002036 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002037 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002038 p += 2;
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002039 if ( hostname_len > 0 &&
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002040 session->hostname != NULL )
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002041 {
2042 /* save host name */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002043 memcpy( p, session->hostname, hostname_len );
2044 p += hostname_len;
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002045 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002046 }
Xiaokang Qian03409292022-10-12 02:49:52 +00002047#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2048 MBEDTLS_SSL_SESSION_TICKETS &&
2049 MBEDTLS_SSL_CLI_C */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002050
Jerry Yu438ddd82022-07-07 06:55:50 +00002051#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2052 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2053 {
2054 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2055 p += 8;
2056 }
2057#endif /* MBEDTLS_HAVE_TIME */
2058
2059#if defined(MBEDTLS_SSL_CLI_C)
2060 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2061 {
2062#if defined(MBEDTLS_HAVE_TIME)
2063 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2064 p += 8;
2065#endif
2066 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2067 p += 4;
2068
2069 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2070 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002071
2072 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002073 {
2074 memcpy( p, session->ticket, session->ticket_len );
2075 p += session->ticket_len;
2076 }
2077 }
2078#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002079 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002080}
2081
2082MBEDTLS_CHECK_RETURN_CRITICAL
2083static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2084 const unsigned char *buf,
2085 size_t len )
2086{
2087 const unsigned char *p = buf;
2088 const unsigned char *end = buf + len;
2089
2090 if( end - p < 9 )
2091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2092 session->endpoint = p[0];
2093 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2094 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2095 session->ticket_flags = p[7];
2096
2097 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002098 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002099 p += 9;
2100
Jerry Yubc7c1a42022-07-21 22:57:37 +08002101 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2103
Jerry Yubc7c1a42022-07-21 22:57:37 +08002104 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002106 memcpy( session->resumption_key, p, session->resumption_key_len );
2107 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002108
Xiaokang Qian03409292022-10-12 02:49:52 +00002109#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2110 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2111 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002112 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2113 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002114 size_t hostname_len;
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002115 /* load host name */
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002116 if( end - p < 2 )
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002118 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002119 p += 2;
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002120
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002121 if( end - p < ( long int )hostname_len )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002123 if( hostname_len > 0 )
Xiaokang Qianecd75282022-09-28 07:11:02 +00002124 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002125 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Xiaokang Qianecd75282022-09-28 07:11:02 +00002126 if( session->hostname == NULL )
2127 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002128 memcpy( session->hostname, p, hostname_len );
2129 p += hostname_len;
Xiaokang Qianecd75282022-09-28 07:11:02 +00002130 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002131 }
Xiaokang Qian03409292022-10-12 02:49:52 +00002132#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2133 MBEDTLS_SSL_SESSION_TICKETS &&
2134 MBEDTLS_SSL_CLI_C */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002135
Jerry Yu438ddd82022-07-07 06:55:50 +00002136#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2137 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2138 {
2139 if( end - p < 8 )
2140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2141 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2142 p += 8;
2143 }
2144#endif /* MBEDTLS_HAVE_TIME */
2145
2146#if defined(MBEDTLS_SSL_CLI_C)
2147 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2148 {
2149#if defined(MBEDTLS_HAVE_TIME)
2150 if( end - p < 8 )
2151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2152 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2153 p += 8;
2154#endif
2155 if( end - p < 4 )
2156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2157 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2158 p += 4;
2159
2160 if( end - p < 2 )
2161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2162 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2163 p += 2;
2164
2165 if( end - p < ( long int )session->ticket_len )
2166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2167 if( session->ticket_len > 0 )
2168 {
2169 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2170 if( session->ticket == NULL )
2171 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2172 memcpy( session->ticket, p, session->ticket_len );
2173 p += session->ticket_len;
2174 }
2175 }
2176#endif /* MBEDTLS_SSL_CLI_C */
2177
2178 return( 0 );
2179
2180}
2181#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002182MBEDTLS_CHECK_RETURN_CRITICAL
2183static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2184 unsigned char *buf,
2185 size_t buf_len,
2186 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002187{
2188 ((void) session);
2189 ((void) buf);
2190 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002191 *olen = 0;
2192 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002193}
Jerry Yu438ddd82022-07-07 06:55:50 +00002194
2195static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2196 unsigned char *buf,
2197 size_t buf_len )
2198{
2199 ((void) session);
2200 ((void) buf);
2201 ((void) buf_len);
2202 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2203}
2204#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002205#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2206
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002207psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002208 size_t taglen,
2209 psa_algorithm_t *alg,
2210 psa_key_type_t *key_type,
2211 size_t *key_size )
2212{
2213 switch ( mbedtls_cipher_type )
2214 {
2215 case MBEDTLS_CIPHER_AES_128_CBC:
2216 *alg = PSA_ALG_CBC_NO_PADDING;
2217 *key_type = PSA_KEY_TYPE_AES;
2218 *key_size = 128;
2219 break;
2220 case MBEDTLS_CIPHER_AES_128_CCM:
2221 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2222 *key_type = PSA_KEY_TYPE_AES;
2223 *key_size = 128;
2224 break;
2225 case MBEDTLS_CIPHER_AES_128_GCM:
2226 *alg = PSA_ALG_GCM;
2227 *key_type = PSA_KEY_TYPE_AES;
2228 *key_size = 128;
2229 break;
2230 case MBEDTLS_CIPHER_AES_192_CCM:
2231 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2232 *key_type = PSA_KEY_TYPE_AES;
2233 *key_size = 192;
2234 break;
2235 case MBEDTLS_CIPHER_AES_192_GCM:
2236 *alg = PSA_ALG_GCM;
2237 *key_type = PSA_KEY_TYPE_AES;
2238 *key_size = 192;
2239 break;
2240 case MBEDTLS_CIPHER_AES_256_CBC:
2241 *alg = PSA_ALG_CBC_NO_PADDING;
2242 *key_type = PSA_KEY_TYPE_AES;
2243 *key_size = 256;
2244 break;
2245 case MBEDTLS_CIPHER_AES_256_CCM:
2246 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2247 *key_type = PSA_KEY_TYPE_AES;
2248 *key_size = 256;
2249 break;
2250 case MBEDTLS_CIPHER_AES_256_GCM:
2251 *alg = PSA_ALG_GCM;
2252 *key_type = PSA_KEY_TYPE_AES;
2253 *key_size = 256;
2254 break;
2255 case MBEDTLS_CIPHER_ARIA_128_CBC:
2256 *alg = PSA_ALG_CBC_NO_PADDING;
2257 *key_type = PSA_KEY_TYPE_ARIA;
2258 *key_size = 128;
2259 break;
2260 case MBEDTLS_CIPHER_ARIA_128_CCM:
2261 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2262 *key_type = PSA_KEY_TYPE_ARIA;
2263 *key_size = 128;
2264 break;
2265 case MBEDTLS_CIPHER_ARIA_128_GCM:
2266 *alg = PSA_ALG_GCM;
2267 *key_type = PSA_KEY_TYPE_ARIA;
2268 *key_size = 128;
2269 break;
2270 case MBEDTLS_CIPHER_ARIA_192_CCM:
2271 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2272 *key_type = PSA_KEY_TYPE_ARIA;
2273 *key_size = 192;
2274 break;
2275 case MBEDTLS_CIPHER_ARIA_192_GCM:
2276 *alg = PSA_ALG_GCM;
2277 *key_type = PSA_KEY_TYPE_ARIA;
2278 *key_size = 192;
2279 break;
2280 case MBEDTLS_CIPHER_ARIA_256_CBC:
2281 *alg = PSA_ALG_CBC_NO_PADDING;
2282 *key_type = PSA_KEY_TYPE_ARIA;
2283 *key_size = 256;
2284 break;
2285 case MBEDTLS_CIPHER_ARIA_256_CCM:
2286 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2287 *key_type = PSA_KEY_TYPE_ARIA;
2288 *key_size = 256;
2289 break;
2290 case MBEDTLS_CIPHER_ARIA_256_GCM:
2291 *alg = PSA_ALG_GCM;
2292 *key_type = PSA_KEY_TYPE_ARIA;
2293 *key_size = 256;
2294 break;
2295 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2296 *alg = PSA_ALG_CBC_NO_PADDING;
2297 *key_type = PSA_KEY_TYPE_CAMELLIA;
2298 *key_size = 128;
2299 break;
2300 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2301 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2302 *key_type = PSA_KEY_TYPE_CAMELLIA;
2303 *key_size = 128;
2304 break;
2305 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2306 *alg = PSA_ALG_GCM;
2307 *key_type = PSA_KEY_TYPE_CAMELLIA;
2308 *key_size = 128;
2309 break;
2310 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2311 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2312 *key_type = PSA_KEY_TYPE_CAMELLIA;
2313 *key_size = 192;
2314 break;
2315 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2316 *alg = PSA_ALG_GCM;
2317 *key_type = PSA_KEY_TYPE_CAMELLIA;
2318 *key_size = 192;
2319 break;
2320 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2321 *alg = PSA_ALG_CBC_NO_PADDING;
2322 *key_type = PSA_KEY_TYPE_CAMELLIA;
2323 *key_size = 256;
2324 break;
2325 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2326 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2327 *key_type = PSA_KEY_TYPE_CAMELLIA;
2328 *key_size = 256;
2329 break;
2330 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2331 *alg = PSA_ALG_GCM;
2332 *key_type = PSA_KEY_TYPE_CAMELLIA;
2333 *key_size = 256;
2334 break;
2335 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2336 *alg = PSA_ALG_CHACHA20_POLY1305;
2337 *key_type = PSA_KEY_TYPE_CHACHA20;
2338 *key_size = 256;
2339 break;
2340 case MBEDTLS_CIPHER_NULL:
2341 *alg = MBEDTLS_SSL_NULL_CIPHER;
2342 *key_type = 0;
2343 *key_size = 0;
2344 break;
2345 default:
2346 return PSA_ERROR_NOT_SUPPORTED;
2347 }
2348
2349 return PSA_SUCCESS;
2350}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002351#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002352
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002353#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002354int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2355 const unsigned char *dhm_P, size_t P_len,
2356 const unsigned char *dhm_G, size_t G_len )
2357{
Janos Follath865b3eb2019-12-16 11:46:15 +00002358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002359
Glenn Strausscee11292021-12-20 01:43:17 -05002360 mbedtls_mpi_free( &conf->dhm_P );
2361 mbedtls_mpi_free( &conf->dhm_G );
2362
Hanno Beckera90658f2017-10-04 15:29:08 +01002363 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2364 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2365 {
2366 mbedtls_mpi_free( &conf->dhm_P );
2367 mbedtls_mpi_free( &conf->dhm_G );
2368 return( ret );
2369 }
2370
2371 return( 0 );
2372}
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002374int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002375{
Janos Follath865b3eb2019-12-16 11:46:15 +00002376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002377
Glenn Strausscee11292021-12-20 01:43:17 -05002378 mbedtls_mpi_free( &conf->dhm_P );
2379 mbedtls_mpi_free( &conf->dhm_G );
2380
Gilles Peskinee5702482021-06-11 21:59:08 +02002381 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2382 &conf->dhm_P ) ) != 0 ||
2383 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2384 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002385 {
2386 mbedtls_mpi_free( &conf->dhm_P );
2387 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002388 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002389 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002390
2391 return( 0 );
2392}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002393#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002394
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002395#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2396/*
2397 * Set the minimum length for Diffie-Hellman parameters
2398 */
2399void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2400 unsigned int bitlen )
2401{
2402 conf->dhm_min_bitlen = bitlen;
2403}
2404#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2405
Gilles Peskineeccd8882020-03-10 12:19:08 +01002406#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002407#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002408/*
2409 * Set allowed/preferred hashes for handshake signatures
2410 */
2411void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2412 const int *hashes )
2413{
2414 conf->sig_hashes = hashes;
2415}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002416#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002417
Jerry Yuf017ee42022-01-12 15:49:48 +08002418/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002419void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2420 const uint16_t* sig_algs )
2421{
Jerry Yuf017ee42022-01-12 15:49:48 +08002422#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2423 conf->sig_hashes = NULL;
2424#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2425 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002426}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002427#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002428
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002429#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002430#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002431/*
2432 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002433 *
2434 * mbedtls_ssl_setup() takes the provided list
2435 * and translates it to a list of IANA TLS group identifiers,
2436 * stored in ssl->handshake->group_list.
2437 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002438 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002439void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002440 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002442 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002443 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002444}
Brett Warrene0edc842021-08-17 09:53:13 +01002445#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002446#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002447
Brett Warrene0edc842021-08-17 09:53:13 +01002448/*
2449 * Set the allowed groups
2450 */
2451void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2452 const uint16_t *group_list )
2453{
2454#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2455 conf->curve_list = NULL;
2456#endif
2457 conf->group_list = group_list;
2458}
2459
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002460#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462{
Hanno Becker947194e2017-04-07 13:25:49 +01002463 /* Initialize to suppress unnecessary compiler warning */
2464 size_t hostname_len = 0;
2465
2466 /* Check if new hostname is valid before
2467 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002468 if( hostname != NULL )
2469 {
2470 hostname_len = strlen( hostname );
2471
2472 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2474 }
2475
2476 /* Now it's clear that we will overwrite the old hostname,
2477 * so we can free it safely */
Hanno Becker947194e2017-04-07 13:25:49 +01002478 if( ssl->hostname != NULL )
2479 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002480 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002481 mbedtls_free( ssl->hostname );
2482 }
2483
2484 /* Passing NULL as hostname shall clear the old one */
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002486 {
2487 ssl->hostname = NULL;
2488 }
2489 else
2490 {
2491 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002492 if( ssl->hostname == NULL )
2493 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002494
Hanno Becker947194e2017-04-07 13:25:49 +01002495 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002496
Hanno Becker947194e2017-04-07 13:25:49 +01002497 ssl->hostname[hostname_len] = '\0';
2498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
2500 return( 0 );
2501}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002502#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002504#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002505void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002507 const unsigned char *, size_t),
2508 void *p_sni )
2509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002510 conf->f_sni = f_sni;
2511 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002516int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002517{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002518 size_t cur_len, tot_len;
2519 const char **p;
2520
2521 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002522 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2523 * MUST NOT be truncated."
2524 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002525 */
2526 tot_len = 0;
2527 for( p = protos; *p != NULL; p++ )
2528 {
2529 cur_len = strlen( *p );
2530 tot_len += cur_len;
2531
Ronald Cron8216dd32020-04-23 16:41:44 +02002532 if( ( cur_len == 0 ) ||
2533 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2534 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002536 }
2537
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002538 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002539
2540 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002541}
2542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002544{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002545 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002548
Johan Pascalb62bb512015-12-03 21:56:45 +01002549#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002550void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2551 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002552{
2553 conf->dtls_srtp_mki_support = support_mki_value;
2554}
2555
Ron Eldoref72faf2018-07-12 11:54:20 +03002556int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2557 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002558 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002559{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002560 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002561 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002563 }
Ron Eldor591f1622018-01-22 12:30:04 +02002564
2565 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002566 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002567 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002568 }
Ron Eldor591f1622018-01-22 12:30:04 +02002569
2570 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2571 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002572 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002573}
2574
Ron Eldoref72faf2018-07-12 11:54:20 +03002575int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002576 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002577{
Johan Pascal253d0262020-09-22 13:04:45 +02002578 const mbedtls_ssl_srtp_profile *p;
2579 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002580
Johan Pascal253d0262020-09-22 13:04:45 +02002581 /* check the profiles list: all entry must be valid,
2582 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002583 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2584 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2585 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002586 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002587 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002588 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002589 list_size++;
2590 }
2591 else
2592 {
2593 /* unsupported value, stop parsing and set the size to an error value */
2594 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002595 }
2596 }
2597
Johan Pascal5ef72d22020-10-28 17:05:47 +01002598 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002599 {
Johan Pascal253d0262020-09-22 13:04:45 +02002600 conf->dtls_srtp_profile_list = NULL;
2601 conf->dtls_srtp_profile_list_len = 0;
2602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2603 }
2604
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002605 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002606 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002607
2608 return( 0 );
2609}
2610
Johan Pascal5ef72d22020-10-28 17:05:47 +01002611void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2612 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002613{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002614 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2615 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002616 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002617 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002618 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002619 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002620 else
2621 {
2622 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002623 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2624 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002625 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002626}
Johan Pascalb62bb512015-12-03 21:56:45 +01002627#endif /* MBEDTLS_SSL_DTLS_SRTP */
2628
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302629#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002630void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002631{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002632 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002633}
2634
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002635void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002636{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002637 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002638}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302639#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002640
Janos Follath088ce432017-04-10 12:42:31 +01002641#if defined(MBEDTLS_SSL_SRV_C)
2642void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2643 char cert_req_ca_list )
2644{
2645 conf->cert_req_ca_list = cert_req_ca_list;
2646}
2647#endif
2648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002650void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002651{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002652 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002653}
2654#endif
2655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002657void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002659 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002660}
2661#endif
2662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002664int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002667 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002670 }
2671
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002672 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002673
2674 return( 0 );
2675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002677
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002678void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002680 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002681}
2682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002684void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002686 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002687}
2688
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002689void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002690{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002691 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002692}
2693
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002694void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002695 const unsigned char period[8] )
2696{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002697 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002702#if defined(MBEDTLS_SSL_CLI_C)
2703void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002704{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002705 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002706}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002707#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002708
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002709#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002710
Jerry Yud0766ec2022-09-22 10:46:57 +08002711#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002712void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2713 uint16_t num_tickets )
2714{
Jerry Yud0766ec2022-09-22 10:46:57 +08002715 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002716}
2717#endif
2718
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002719void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2720 mbedtls_ssl_ticket_write_t *f_ticket_write,
2721 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2722 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002723{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002724 conf->f_ticket_write = f_ticket_write;
2725 conf->f_ticket_parse = f_ticket_parse;
2726 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002727}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002730
Hanno Becker7e6c1782021-06-08 09:24:55 +01002731void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2732 mbedtls_ssl_export_keys_t *f_export_keys,
2733 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002734{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002735 ssl->f_export_keys = f_export_keys;
2736 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002737}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002738
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002739#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002740void mbedtls_ssl_conf_async_private_cb(
2741 mbedtls_ssl_config *conf,
2742 mbedtls_ssl_async_sign_t *f_async_sign,
2743 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2744 mbedtls_ssl_async_resume_t *f_async_resume,
2745 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002746 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002747{
2748 conf->f_async_sign_start = f_async_sign;
2749 conf->f_async_decrypt_start = f_async_decrypt;
2750 conf->f_async_resume = f_async_resume;
2751 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002752 conf->p_async_config_data = async_config_data;
2753}
2754
Gilles Peskine8f97af72018-04-26 11:46:10 +02002755void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2756{
2757 return( conf->p_async_config_data );
2758}
2759
Gilles Peskine1febfef2018-04-30 11:54:39 +02002760void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002761{
2762 if( ssl->handshake == NULL )
2763 return( NULL );
2764 else
2765 return( ssl->handshake->user_async_ctx );
2766}
2767
Gilles Peskine1febfef2018-04-30 11:54:39 +02002768void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002769 void *ctx )
2770{
2771 if( ssl->handshake != NULL )
2772 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002773}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002774#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002775
Paul Bakker5121ce52009-01-03 21:22:43 +00002776/*
2777 * SSL get accessors
2778 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002779uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002780{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002781 if( ssl->session != NULL )
2782 return( ssl->session->verify_result );
2783
2784 if( ssl->session_negotiate != NULL )
2785 return( ssl->session_negotiate->verify_result );
2786
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002787 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788}
2789
Glenn Strauss8f526902022-01-13 00:04:49 -05002790int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2791{
2792 if( ssl == NULL || ssl->session == NULL )
2793 return( 0 );
2794
2795 return( ssl->session->ciphersuite );
2796}
2797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002799{
Paul Bakker926c8e42013-03-06 10:23:34 +01002800 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002801 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002804}
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002807{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002809 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002810 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002811 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002812 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002813 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002814 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002815 default:
2816 return( "unknown (DTLS)" );
2817 }
2818 }
2819#endif
2820
Glenn Strauss60bfe602022-03-14 19:04:24 -04002821 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002822 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002823 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002825 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002826 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002827 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002828 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002829 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002830}
2831
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002832#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002833size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2834{
David Horstmann95d516f2021-05-04 18:36:56 +01002835 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002836 size_t read_mfl;
2837
2838 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2839 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2840 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2841 {
2842 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2843 }
2844
2845 /* Check if a smaller max length was negotiated */
2846 if( ssl->session_out != NULL )
2847 {
2848 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2849 if( read_mfl < max_len )
2850 {
2851 max_len = read_mfl;
2852 }
2853 }
2854
2855 // During a handshake, use the value being negotiated
2856 if( ssl->session_negotiate != NULL )
2857 {
2858 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2859 if( read_mfl < max_len )
2860 {
2861 max_len = read_mfl;
2862 }
2863 }
2864
2865 return( max_len );
2866}
2867
2868size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002869{
2870 size_t max_len;
2871
2872 /*
2873 * Assume mfl_code is correct since it was checked when set
2874 */
Angus Grattond8213d02016-05-25 20:56:48 +10002875 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002876
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002877 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002878 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002879 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002880 {
Angus Grattond8213d02016-05-25 20:56:48 +10002881 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002882 }
2883
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002884 /* During a handshake, use the value being negotiated */
2885 if( ssl->session_negotiate != NULL &&
2886 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2887 {
2888 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2889 }
2890
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002891 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002892}
2893#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2894
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002896size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002897{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002898 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2899 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2900 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2901 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2902 return ( 0 );
2903
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002904 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2905 return( ssl->mtu );
2906
2907 if( ssl->mtu == 0 )
2908 return( ssl->handshake->mtu );
2909
2910 return( ssl->mtu < ssl->handshake->mtu ?
2911 ssl->mtu : ssl->handshake->mtu );
2912}
2913#endif /* MBEDTLS_SSL_PROTO_DTLS */
2914
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002915int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2916{
2917 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2918
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002919#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2920 !defined(MBEDTLS_SSL_PROTO_DTLS)
2921 (void) ssl;
2922#endif
2923
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002924#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002925 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002926
2927 if( max_len > mfl )
2928 max_len = mfl;
2929#endif
2930
2931#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002932 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002933 {
Hanno Becker89490712020-02-05 10:50:12 +00002934 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002935 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2936 const size_t overhead = (size_t) ret;
2937
2938 if( ret < 0 )
2939 return( ret );
2940
2941 if( mtu <= overhead )
2942 {
2943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2944 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2945 }
2946
2947 if( max_len > mtu - overhead )
2948 max_len = mtu - overhead;
2949 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002950#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002951
Hanno Becker0defedb2018-08-10 12:35:02 +01002952#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2953 !defined(MBEDTLS_SSL_PROTO_DTLS)
2954 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002955#endif
2956
2957 return( (int) max_len );
2958}
2959
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002960int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2961{
2962 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2963
2964#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2965 (void) ssl;
2966#endif
2967
2968#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2969 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2970
2971 if( max_len > mfl )
2972 max_len = mfl;
2973#endif
2974
2975 return( (int) max_len );
2976}
2977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#if defined(MBEDTLS_X509_CRT_PARSE_C)
2979const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002980{
2981 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002982 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002983
Hanno Beckere6824572019-02-07 13:18:46 +00002984#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002985 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002986#else
2987 return( NULL );
2988#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002993int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2994 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002995{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002996 int ret;
2997
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002998 if( ssl == NULL ||
2999 dst == NULL ||
3000 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003001 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003004 }
3005
Hanno Beckere810bbc2021-05-14 16:01:05 +01003006 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3007 * idempotent: Each session can only be exported once.
3008 *
3009 * (This is in preparation for TLS 1.3 support where we will
3010 * need the ability to export multiple sessions (aka tickets),
3011 * which will be achieved by calling mbedtls_ssl_get_session()
3012 * multiple times until it fails.)
3013 *
3014 * Check whether we have already exported the current session,
3015 * and fail if so.
3016 */
3017 if( ssl->session->exported == 1 )
3018 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3019
3020 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3021 if( ret != 0 )
3022 return( ret );
3023
3024 /* Remember that we've exported the session. */
3025 ssl->session->exported = 1;
3026 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003027}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003029
Paul Bakker5121ce52009-01-03 21:22:43 +00003030/*
Hanno Beckera835da52019-05-16 12:39:07 +01003031 * Define ticket header determining Mbed TLS version
3032 * and structure of the ticket.
3033 */
3034
Hanno Becker94ef3b32019-05-16 12:50:45 +01003035/*
Hanno Becker50b59662019-05-28 14:30:45 +01003036 * Define bitflag determining compile-time settings influencing
3037 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003038 */
3039
Hanno Becker50b59662019-05-28 14:30:45 +01003040#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003041#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003042#else
Hanno Becker3e088662019-05-29 11:10:18 +01003043#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003044#endif /* MBEDTLS_HAVE_TIME */
3045
3046#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003047#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048#else
Hanno Becker3e088662019-05-29 11:10:18 +01003049#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003050#endif /* MBEDTLS_X509_CRT_PARSE_C */
3051
3052#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003053#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003054#else
Hanno Becker3e088662019-05-29 11:10:18 +01003055#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003056#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3057
3058#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003059#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003060#else
Hanno Becker3e088662019-05-29 11:10:18 +01003061#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003062#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3063
Hanno Becker94ef3b32019-05-16 12:50:45 +01003064#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003065#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003066#else
Hanno Becker3e088662019-05-29 11:10:18 +01003067#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003068#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3069
Hanno Becker94ef3b32019-05-16 12:50:45 +01003070#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3071#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3072#else
3073#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3074#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3075
Hanno Becker3e088662019-05-29 11:10:18 +01003076#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3077#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3078#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3079#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003080#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3081#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003082
Hanno Becker50b59662019-05-28 14:30:45 +01003083#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003084 ( (uint16_t) ( \
3085 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3086 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3087 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3088 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003089 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003090 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003091
Hanno Beckerf8787072019-05-16 12:41:07 +01003092static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003093 MBEDTLS_VERSION_MAJOR,
3094 MBEDTLS_VERSION_MINOR,
3095 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003096 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3097 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003098};
Hanno Beckera835da52019-05-16 12:39:07 +01003099
3100/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003101 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003102 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003103 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003104 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003105 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003106 * opaque mbedtls_version[3]; // library version: major, minor, patch
3107 * opaque session_format[2]; // library-version specific 16-bit field
3108 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003109 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003110 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003111 * Note: When updating the format, remember to keep
3112 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003113 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003114 * // In this version, `session_format` determines
3115 * // the setting of those compile-time
3116 * // configuration options which influence
3117 * // the structure of mbedtls_ssl_session.
3118 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003119 * uint8_t minor_ver; // Protocol minor version. Possible values:
3120 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003121 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003122 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003123 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003124 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003125 * serialized_session_tls12 data;
3126 *
3127 * };
3128 *
3129 * } serialized_session;
3130 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003131 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003132
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003133MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003134static int ssl_session_save( const mbedtls_ssl_session *session,
3135 unsigned char omit_header,
3136 unsigned char *buf,
3137 size_t buf_len,
3138 size_t *olen )
3139{
3140 unsigned char *p = buf;
3141 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003142 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003143#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3144 size_t out_len;
3145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3146#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003147 if( session == NULL )
3148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3149
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003150 if( !omit_header )
3151 {
3152 /*
3153 * Add Mbed TLS version identifier
3154 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003155 used += sizeof( ssl_serialized_session_header );
3156
3157 if( used <= buf_len )
3158 {
3159 memcpy( p, ssl_serialized_session_header,
3160 sizeof( ssl_serialized_session_header ) );
3161 p += sizeof( ssl_serialized_session_header );
3162 }
3163 }
3164
3165 /*
3166 * TLS version identifier
3167 */
3168 used += 1;
3169 if( used <= buf_len )
3170 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003171 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003172 }
3173
3174 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003175 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003176 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003177 {
3178#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003179 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003180 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003181 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3183
Jerry Yu251a12e2022-07-13 15:15:48 +08003184#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3185 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003186 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3187 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003188 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003189 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003190 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003191#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3192
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003193 default:
3194 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3195 }
3196
3197 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003198 if( used > buf_len )
3199 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003200
3201 return( 0 );
3202}
3203
3204/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003205 * Public wrapper for ssl_session_save()
3206 */
3207int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3208 unsigned char *buf,
3209 size_t buf_len,
3210 size_t *olen )
3211{
3212 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3213}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003214
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003215/*
3216 * Deserialize session, see mbedtls_ssl_session_save() for format.
3217 *
3218 * This internal version is wrapped by a public function that cleans up in
3219 * case of error, and has an extra option omit_header.
3220 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003221MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003222static int ssl_session_load( mbedtls_ssl_session *session,
3223 unsigned char omit_header,
3224 const unsigned char *buf,
3225 size_t len )
3226{
3227 const unsigned char *p = buf;
3228 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003229 size_t remaining_len;
3230
3231
3232 if( session == NULL )
3233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003234
3235 if( !omit_header )
3236 {
3237 /*
3238 * Check Mbed TLS version identifier
3239 */
3240
3241 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3243
3244 if( memcmp( p, ssl_serialized_session_header,
3245 sizeof( ssl_serialized_session_header ) ) != 0 )
3246 {
3247 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3248 }
3249 p += sizeof( ssl_serialized_session_header );
3250 }
3251
3252 /*
3253 * TLS version identifier
3254 */
3255 if( 1 > (size_t)( end - p ) )
3256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003257 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003258
3259 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003260 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003261 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003262 {
3263#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003264 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003265 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3267
Jerry Yu438ddd82022-07-07 06:55:50 +00003268#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3269 case MBEDTLS_SSL_VERSION_TLS1_3:
3270 return( ssl_tls13_session_load( session, p, remaining_len ) );
3271#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3272
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003273 default:
3274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3275 }
3276}
3277
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003278/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003279 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003280 */
3281int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3282 const unsigned char *buf,
3283 size_t len )
3284{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003285 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003286
3287 if( ret != 0 )
3288 mbedtls_ssl_session_free( session );
3289
3290 return( ret );
3291}
3292
3293/*
Paul Bakker1961b702013-01-25 14:49:24 +01003294 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003296MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003297static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3298{
3299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3300
Ronald Cron66dbf912022-02-02 15:33:46 +01003301 /*
3302 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003303 * that were written into the output buffer by the previous handshake step,
3304 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003305 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3306 * We proceed to the next handshake step only when all data from the
3307 * previous one have been sent to the peer, thus we make sure that this is
3308 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3309 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3310 * we have to wait before to go ahead.
3311 * In the case of TLS 1.3, handshake step handlers do not send data to the
3312 * peer. Data are only sent here and through
3313 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003314 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003315 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003316 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3317 return( ret );
3318
3319#if defined(MBEDTLS_SSL_PROTO_DTLS)
3320 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3321 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3322 {
3323 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3324 return( ret );
3325 }
3326#endif /* MBEDTLS_SSL_PROTO_DTLS */
3327
3328 return( ret );
3329}
3330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003332{
Hanno Becker41934dd2021-08-07 19:13:43 +01003333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Hanno Becker41934dd2021-08-07 19:13:43 +01003335 if( ssl == NULL ||
3336 ssl->conf == NULL ||
3337 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003338 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003339 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003341 }
3342
3343 ret = ssl_prepare_handshake_step( ssl );
3344 if( ret != 0 )
3345 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003346
Jerry Yue7047812021-09-13 19:26:39 +08003347 ret = mbedtls_ssl_handle_pending_alert( ssl );
3348 if( ret != 0 )
3349 goto cleanup;
3350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003353 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3355 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003356
Ronald Cron9f0fba32022-02-10 16:45:15 +01003357 switch( ssl->state )
3358 {
3359 case MBEDTLS_SSL_HELLO_REQUEST:
3360 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3361 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003362
Ronald Cron9f0fba32022-02-10 16:45:15 +01003363 case MBEDTLS_SSL_CLIENT_HELLO:
3364 ret = mbedtls_ssl_write_client_hello( ssl );
3365 break;
3366
3367 default:
3368#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003369 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003370 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3371 else
3372 ret = mbedtls_ssl_handshake_client_step( ssl );
3373#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3374 ret = mbedtls_ssl_handshake_client_step( ssl );
3375#else
3376 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3377#endif
3378 }
Jerry Yub9930e72021-08-06 17:11:51 +08003379 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003383 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003384#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003385 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003386 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003387#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003388
3389#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3390 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3391 ret = mbedtls_ssl_handshake_server_step( ssl );
3392#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3393 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003394#endif
3395
Jerry Yue7047812021-09-13 19:26:39 +08003396 if( ret != 0 )
3397 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003398 /* handshake_step return error. And it is same
3399 * with alert_reason.
3400 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003401 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003402 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003403 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003404 goto cleanup;
3405 }
3406 }
3407
3408cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003409 return( ret );
3410}
3411
3412/*
3413 * Perform the SSL handshake
3414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003416{
3417 int ret = 0;
3418
Hanno Beckera817ea42020-10-20 15:20:23 +01003419 /* Sanity checks */
3420
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003421 if( ssl == NULL || ssl->conf == NULL )
3422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3423
Hanno Beckera817ea42020-10-20 15:20:23 +01003424#if defined(MBEDTLS_SSL_PROTO_DTLS)
3425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3426 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3427 {
3428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3429 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3431 }
3432#endif /* MBEDTLS_SSL_PROTO_DTLS */
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003435
Hanno Beckera817ea42020-10-20 15:20:23 +01003436 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003437 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003440
3441 if( ret != 0 )
3442 break;
3443 }
3444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
3447 return( ret );
3448}
3449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#if defined(MBEDTLS_SSL_RENEGOTIATION)
3451#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003452/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003453 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003454 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003455MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003457{
Janos Follath865b3eb2019-12-16 11:46:15 +00003458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003461
3462 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3464 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003465
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003466 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003467 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003469 return( ret );
3470 }
3471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003473
3474 return( 0 );
3475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003477
3478/*
3479 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 * - any side: calling mbedtls_ssl_renegotiate(),
3481 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3482 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003483 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003484 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003486 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003487int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003488{
Janos Follath865b3eb2019-12-16 11:46:15 +00003489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003492
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003493 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3494 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003495
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003496 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3497 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003499 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003501 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003502 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003503 ssl->handshake->out_msg_seq = 1;
3504 else
3505 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003506 }
3507#endif
3508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3510 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003515 return( ret );
3516 }
3517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003519
3520 return( 0 );
3521}
3522
3523/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003524 * Renegotiate current connection on client,
3525 * or request renegotiation on server
3526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003530
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003531 if( ssl == NULL || ssl->conf == NULL )
3532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003535 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003536 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003537 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003538 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003542
3543 /* Did we already try/start sending HelloRequest? */
3544 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003546
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003547 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003552 /*
3553 * On client, either start the renegotiation process or,
3554 * if already in progress, continue the handshake
3555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003557 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003558 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003560
Hanno Becker40cdaa12020-02-05 10:48:27 +00003561 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003562 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003564 return( ret );
3565 }
3566 }
3567 else
3568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003572 return( ret );
3573 }
3574 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003576
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003577 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003580
Gilles Peskine9b562d52018-04-25 20:32:43 +02003581void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003582{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003583 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3584
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003585 if( handshake == NULL )
3586 return;
3587
Brett Warrene0edc842021-08-17 09:53:13 +01003588#if defined(MBEDTLS_ECP_C)
3589#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3590 if ( ssl->handshake->group_list_heap_allocated )
3591 mbedtls_free( (void*) handshake->group_list );
3592 handshake->group_list = NULL;
3593#endif /* MBEDTLS_DEPRECATED_REMOVED */
3594#endif /* MBEDTLS_ECP_C */
3595
Jerry Yuf017ee42022-01-12 15:49:48 +08003596#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3597#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3598 if ( ssl->handshake->sig_algs_heap_allocated )
3599 mbedtls_free( (void*) handshake->sig_algs );
3600 handshake->sig_algs = NULL;
3601#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003602#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3603 if( ssl->handshake->certificate_request_context )
3604 {
3605 mbedtls_free( (void*) handshake->certificate_request_context );
3606 }
3607#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003608#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3609
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003610#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3611 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3612 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003613 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003614 handshake->async_in_progress = 0;
3615 }
3616#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3617
Andrzej Kurek25f27152022-08-17 16:09:31 -04003618#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003619#if defined(MBEDTLS_USE_PSA_CRYPTO)
3620 psa_hash_abort( &handshake->fin_sha256_psa );
3621#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003622 mbedtls_sha256_free( &handshake->fin_sha256 );
3623#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003624#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003625#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003626#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003627 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003628#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003629 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003630#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003631#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_DHM_C)
3634 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003635#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003636#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003638#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003639#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003640 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003641#if defined(MBEDTLS_SSL_CLI_C)
3642 mbedtls_free( handshake->ecjpake_cache );
3643 handshake->ecjpake_cache = NULL;
3644 handshake->ecjpake_cache_len = 0;
3645#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003646#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003647
Janos Follath4ae5c292016-02-10 11:27:43 +00003648#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3649 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003650 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003652#endif
3653
Gilles Peskineeccd8882020-03-10 12:19:08 +01003654#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003655#if defined(MBEDTLS_USE_PSA_CRYPTO)
3656 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3657 {
3658 /* The maintenance of the external PSK key slot is the
3659 * user's responsibility. */
3660 if( ssl->handshake->psk_opaque_is_internal )
3661 {
3662 psa_destroy_key( ssl->handshake->psk_opaque );
3663 ssl->handshake->psk_opaque_is_internal = 0;
3664 }
3665 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3666 }
Neil Armstronge952a302022-05-03 10:22:14 +02003667#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003668 if( handshake->psk != NULL )
3669 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003670 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003671 mbedtls_free( handshake->psk );
3672 }
Neil Armstronge952a302022-05-03 10:22:14 +02003673#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003674#endif
3675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3677 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003678 /*
3679 * Free only the linked list wrapper, not the keys themselves
3680 * since the belong to the SNI callback
3681 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003682 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003684
Gilles Peskineeccd8882020-03-10 12:19:08 +01003685#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003686 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003687 if( handshake->ecrs_peer_cert != NULL )
3688 {
3689 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3690 mbedtls_free( handshake->ecrs_peer_cert );
3691 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003692#endif
3693
Hanno Becker75173122019-02-06 16:18:31 +00003694#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3695 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3696 mbedtls_pk_free( &handshake->peer_pubkey );
3697#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3698
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003699#if defined(MBEDTLS_SSL_CLI_C) && \
3700 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3701 mbedtls_free( handshake->cookie );
3702#endif /* MBEDTLS_SSL_CLI_C &&
3703 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003704
3705#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003706 mbedtls_ssl_flight_free( handshake->flight );
3707 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003708#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003709
Ronald Cronf12b81d2022-03-15 10:42:41 +01003710#if defined(MBEDTLS_ECDH_C) && \
3711 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003712 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003713 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003714#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3715
Ronald Cron6f135e12021-12-08 16:57:54 +01003716#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003717 mbedtls_ssl_transform_free( handshake->transform_handshake );
3718 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003719 mbedtls_free( handshake->transform_earlydata );
3720 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003721#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003722
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003723
3724#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3725 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3726 * processes datagrams and the fact that a datagram is allowed to have
3727 * several records in it, it is possible that the I/O buffers are not
3728 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003729 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3730 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003731#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003732
Jerry Yuba9c7272021-10-30 11:54:10 +08003733 /* mbedtls_platform_zeroize MUST be last one in this function */
3734 mbedtls_platform_zeroize( handshake,
3735 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003736}
3737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003739{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003740 if( session == NULL )
3741 return;
3742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003744 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003745#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003746
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003747#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003749#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003750
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003751#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3752 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003753 mbedtls_free( session->hostname );
3754#endif
3755
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003756 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003757}
3758
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003759#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003760
3761#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3762#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3763#else
3764#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3765#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3766
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003767#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003768
3769#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3770#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3771#else
3772#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3773#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3774
3775#if defined(MBEDTLS_SSL_ALPN)
3776#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3777#else
3778#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3779#endif /* MBEDTLS_SSL_ALPN */
3780
3781#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3782#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3783#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3784#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3785
3786#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3787 ( (uint32_t) ( \
3788 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3789 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3790 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3791 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3792 0u ) )
3793
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003794static unsigned char ssl_serialized_context_header[] = {
3795 MBEDTLS_VERSION_MAJOR,
3796 MBEDTLS_VERSION_MINOR,
3797 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003798 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3799 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3800 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3801 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3802 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003803};
3804
Paul Bakker5121ce52009-01-03 21:22:43 +00003805/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003806 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003807 *
3808 * The format of the serialized data is:
3809 * (in the presentation language of TLS, RFC 8446 section 3)
3810 *
3811 * // header
3812 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003813 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003814 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003815 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003816 * Note: When updating the format, remember to keep these
3817 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003818 *
3819 * // session sub-structure
3820 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3821 * // transform sub-structure
3822 * uint8 random[64]; // ServerHello.random+ClientHello.random
3823 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3824 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3825 * // fields from ssl_context
3826 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3827 * uint64 in_window_top; // DTLS: last validated record seq_num
3828 * uint64 in_window; // DTLS: bitmask for replay protection
3829 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3830 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3831 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3832 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3833 *
3834 * Note that many fields of the ssl_context or sub-structures are not
3835 * serialized, as they fall in one of the following categories:
3836 *
3837 * 1. forced value (eg in_left must be 0)
3838 * 2. pointer to dynamically-allocated memory (eg session, transform)
3839 * 3. value can be re-derived from other data (eg session keys from MS)
3840 * 4. value was temporary (eg content of input buffer)
3841 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003842 */
3843int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3844 unsigned char *buf,
3845 size_t buf_len,
3846 size_t *olen )
3847{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003848 unsigned char *p = buf;
3849 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003850 size_t session_len;
3851 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003852
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003853 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003854 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3855 * this function's documentation.
3856 *
3857 * These are due to assumptions/limitations in the implementation. Some of
3858 * them are likely to stay (no handshake in progress) some might go away
3859 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003860 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003861 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003862 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003863 {
3864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003866 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003867 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003868 {
3869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003871 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003872 /* Double-check that sub-structures are indeed ready */
3873 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003874 {
3875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003877 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003878 /* There must be no pending incoming or outgoing data */
3879 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003883 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003884 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003885 {
3886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
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 /* Protocol must be DLTS, not TLS */
3890 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003891 {
3892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
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 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003896 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003897 {
3898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
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 /* We must be using an AEAD ciphersuite */
3902 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003903 {
3904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003906 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003907 /* Renegotiation must not be enabled */
3908#if defined(MBEDTLS_SSL_RENEGOTIATION)
3909 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003910 {
3911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003913 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003914#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003915
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003916 /*
3917 * Version and format identifier
3918 */
3919 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003920
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003921 if( used <= buf_len )
3922 {
3923 memcpy( p, ssl_serialized_context_header,
3924 sizeof( ssl_serialized_context_header ) );
3925 p += sizeof( ssl_serialized_context_header );
3926 }
3927
3928 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003929 * Session (length + data)
3930 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003931 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003932 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3933 return( ret );
3934
3935 used += 4 + session_len;
3936 if( used <= buf_len )
3937 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003938 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3939 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003940
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003941 ret = ssl_session_save( ssl->session, 1,
3942 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003943 if( ret != 0 )
3944 return( ret );
3945
3946 p += session_len;
3947 }
3948
3949 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003950 * Transform
3951 */
3952 used += sizeof( ssl->transform->randbytes );
3953 if( used <= buf_len )
3954 {
3955 memcpy( p, ssl->transform->randbytes,
3956 sizeof( ssl->transform->randbytes ) );
3957 p += sizeof( ssl->transform->randbytes );
3958 }
3959
3960#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3961 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3962 if( used <= buf_len )
3963 {
3964 *p++ = ssl->transform->in_cid_len;
3965 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3966 p += ssl->transform->in_cid_len;
3967
3968 *p++ = ssl->transform->out_cid_len;
3969 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3970 p += ssl->transform->out_cid_len;
3971 }
3972#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3973
3974 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003975 * Saved fields from top-level ssl_context structure
3976 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003977 used += 4;
3978 if( used <= buf_len )
3979 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003980 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3981 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003982 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003983
3984#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3985 used += 16;
3986 if( used <= buf_len )
3987 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003988 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3989 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003990
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003991 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3992 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003993 }
3994#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3995
3996#if defined(MBEDTLS_SSL_PROTO_DTLS)
3997 used += 1;
3998 if( used <= buf_len )
3999 {
4000 *p++ = ssl->disable_datagram_packing;
4001 }
4002#endif /* MBEDTLS_SSL_PROTO_DTLS */
4003
Jerry Yuae0b2e22021-10-08 15:21:19 +08004004 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004005 if( used <= buf_len )
4006 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004007 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4008 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004009 }
4010
4011#if defined(MBEDTLS_SSL_PROTO_DTLS)
4012 used += 2;
4013 if( used <= buf_len )
4014 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004015 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4016 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004017 }
4018#endif /* MBEDTLS_SSL_PROTO_DTLS */
4019
4020#if defined(MBEDTLS_SSL_ALPN)
4021 {
4022 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004023 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004024 : 0;
4025
4026 used += 1 + alpn_len;
4027 if( used <= buf_len )
4028 {
4029 *p++ = alpn_len;
4030
4031 if( ssl->alpn_chosen != NULL )
4032 {
4033 memcpy( p, ssl->alpn_chosen, alpn_len );
4034 p += alpn_len;
4035 }
4036 }
4037 }
4038#endif /* MBEDTLS_SSL_ALPN */
4039
4040 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004041 * Done
4042 */
4043 *olen = used;
4044
4045 if( used > buf_len )
4046 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004047
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004048 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4049
Hanno Becker43aefe22020-02-05 10:44:56 +00004050 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004051}
4052
4053/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004054 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004055 *
4056 * This internal version is wrapped by a public function that cleans up in
4057 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004058 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004059MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004060static int ssl_context_load( mbedtls_ssl_context *ssl,
4061 const unsigned char *buf,
4062 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004063{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004064 const unsigned char *p = buf;
4065 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004066 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004068
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004069 /*
4070 * The context should have been freshly setup or reset.
4071 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004072 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004073 * renegotiating, or if the user mistakenly loaded a session first.)
4074 */
4075 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4076 ssl->session != NULL )
4077 {
4078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4079 }
4080
4081 /*
4082 * We can't check that the config matches the initial one, but we can at
4083 * least check it matches the requirements for serializing.
4084 */
4085 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004086 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4087 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004088#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004089 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004090#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004091 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004092 {
4093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4094 }
4095
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004096 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4097
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004098 /*
4099 * Check version identifier
4100 */
4101 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4103
4104 if( memcmp( p, ssl_serialized_context_header,
4105 sizeof( ssl_serialized_context_header ) ) != 0 )
4106 {
4107 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4108 }
4109 p += sizeof( ssl_serialized_context_header );
4110
4111 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004112 * Session
4113 */
4114 if( (size_t)( end - p ) < 4 )
4115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4116
4117 session_len = ( (size_t) p[0] << 24 ) |
4118 ( (size_t) p[1] << 16 ) |
4119 ( (size_t) p[2] << 8 ) |
4120 ( (size_t) p[3] );
4121 p += 4;
4122
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004123 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004124 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004125 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004126 ssl->session_in = ssl->session;
4127 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004128 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004129
4130 if( (size_t)( end - p ) < session_len )
4131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4132
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004133 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004134 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004135 {
4136 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004137 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004138 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004139
4140 p += session_len;
4141
4142 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004143 * Transform
4144 */
4145
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004146 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004147 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004148 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004149 ssl->transform_in = ssl->transform;
4150 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004151 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004152
4153 /* Read random bytes and populate structure */
4154 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004156#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004157 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004158 ssl->session->ciphersuite,
4159 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004160#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004161 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004162#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004163 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4164 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004165 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004166 ssl->conf->endpoint,
4167 ssl );
4168 if( ret != 0 )
4169 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004171 p += sizeof( ssl->transform->randbytes );
4172
4173#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4174 /* Read connection IDs and store them */
4175 if( (size_t)( end - p ) < 1 )
4176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4177
4178 ssl->transform->in_cid_len = *p++;
4179
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004180 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4182
4183 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4184 p += ssl->transform->in_cid_len;
4185
4186 ssl->transform->out_cid_len = *p++;
4187
4188 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4190
4191 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4192 p += ssl->transform->out_cid_len;
4193#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4194
4195 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004196 * Saved fields from top-level ssl_context structure
4197 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004198 if( (size_t)( end - p ) < 4 )
4199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4200
4201 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4202 ( (uint32_t) p[1] << 16 ) |
4203 ( (uint32_t) p[2] << 8 ) |
4204 ( (uint32_t) p[3] );
4205 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004206
4207#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4208 if( (size_t)( end - p ) < 16 )
4209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4210
4211 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4212 ( (uint64_t) p[1] << 48 ) |
4213 ( (uint64_t) p[2] << 40 ) |
4214 ( (uint64_t) p[3] << 32 ) |
4215 ( (uint64_t) p[4] << 24 ) |
4216 ( (uint64_t) p[5] << 16 ) |
4217 ( (uint64_t) p[6] << 8 ) |
4218 ( (uint64_t) p[7] );
4219 p += 8;
4220
4221 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4222 ( (uint64_t) p[1] << 48 ) |
4223 ( (uint64_t) p[2] << 40 ) |
4224 ( (uint64_t) p[3] << 32 ) |
4225 ( (uint64_t) p[4] << 24 ) |
4226 ( (uint64_t) p[5] << 16 ) |
4227 ( (uint64_t) p[6] << 8 ) |
4228 ( (uint64_t) p[7] );
4229 p += 8;
4230#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4231
4232#if defined(MBEDTLS_SSL_PROTO_DTLS)
4233 if( (size_t)( end - p ) < 1 )
4234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4235
4236 ssl->disable_datagram_packing = *p++;
4237#endif /* MBEDTLS_SSL_PROTO_DTLS */
4238
Jerry Yud9a94fe2021-09-28 18:58:59 +08004239 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004241 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4242 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004243
4244#if defined(MBEDTLS_SSL_PROTO_DTLS)
4245 if( (size_t)( end - p ) < 2 )
4246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4247
4248 ssl->mtu = ( p[0] << 8 ) | p[1];
4249 p += 2;
4250#endif /* MBEDTLS_SSL_PROTO_DTLS */
4251
4252#if defined(MBEDTLS_SSL_ALPN)
4253 {
4254 uint8_t alpn_len;
4255 const char **cur;
4256
4257 if( (size_t)( end - p ) < 1 )
4258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4259
4260 alpn_len = *p++;
4261
4262 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4263 {
4264 /* alpn_chosen should point to an item in the configured list */
4265 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4266 {
4267 if( strlen( *cur ) == alpn_len &&
4268 memcmp( p, cur, alpn_len ) == 0 )
4269 {
4270 ssl->alpn_chosen = *cur;
4271 break;
4272 }
4273 }
4274 }
4275
4276 /* can only happen on conf mismatch */
4277 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4279
4280 p += alpn_len;
4281 }
4282#endif /* MBEDTLS_SSL_ALPN */
4283
4284 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004285 * Forced fields from top-level ssl_context structure
4286 *
4287 * Most of them already set to the correct value by mbedtls_ssl_init() and
4288 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4289 */
4290 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004291 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004292
Hanno Becker361b10d2019-08-30 10:42:49 +01004293 /* Adjust pointers for header fields of outgoing records to
4294 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004295 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004296
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004297#if defined(MBEDTLS_SSL_PROTO_DTLS)
4298 ssl->in_epoch = 1;
4299#endif
4300
4301 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4302 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004303 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4304 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004305 if( ssl->handshake != NULL )
4306 {
4307 mbedtls_ssl_handshake_free( ssl );
4308 mbedtls_free( ssl->handshake );
4309 ssl->handshake = NULL;
4310 }
4311
4312 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004313 * Done - should have consumed entire buffer
4314 */
4315 if( p != end )
4316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004317
4318 return( 0 );
4319}
4320
4321/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004322 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004323 */
4324int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4325 const unsigned char *buf,
4326 size_t len )
4327{
4328 int ret = ssl_context_load( context, buf, len );
4329
4330 if( ret != 0 )
4331 mbedtls_ssl_free( context );
4332
4333 return( ret );
4334}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004335#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004336
4337/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 * Free an SSL context
4339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004341{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004342 if( ssl == NULL )
4343 return;
4344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004346
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004347 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 {
sander-visserb8aa2072020-05-06 22:05:13 +02004349#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4350 size_t out_buf_len = ssl->out_buf_len;
4351#else
4352 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4353#endif
4354
Darryl Greenb33cc762019-11-28 14:29:44 +00004355 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004357 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 }
4359
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004360 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 {
sander-visserb8aa2072020-05-06 22:05:13 +02004362#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4363 size_t in_buf_len = ssl->in_buf_len;
4364#else
4365 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4366#endif
4367
Darryl Greenb33cc762019-11-28 14:29:44 +00004368 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004370 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004371 }
4372
Paul Bakker48916f92012-09-16 19:57:18 +00004373 if( ssl->transform )
4374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 mbedtls_ssl_transform_free( ssl->transform );
4376 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004377 }
4378
4379 if( ssl->handshake )
4380 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004381 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4383 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 mbedtls_free( ssl->handshake );
4386 mbedtls_free( ssl->transform_negotiate );
4387 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004388 }
4389
Ronald Cron6f135e12021-12-08 16:57:54 +01004390#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004391 mbedtls_ssl_transform_free( ssl->transform_application );
4392 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004393#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004394
Paul Bakkerc0463502013-02-14 11:19:38 +01004395 if( ssl->session )
4396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 mbedtls_ssl_session_free( ssl->session );
4398 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004399 }
4400
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004401#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004402 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004403 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004404 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004406 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004407#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004408
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004409#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004411#endif
4412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004414
Paul Bakker86f04f42013-02-14 11:20:09 +01004415 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004416 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004417}
4418
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004419/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004420 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004421 */
4422void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4423{
4424 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4425}
4426
Gilles Peskineae270bf2021-06-02 00:05:29 +02004427/* The selection should be the same as mbedtls_x509_crt_profile_default in
4428 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004429 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004430 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004431 * about this list.
4432 */
Brett Warrene0edc842021-08-17 09:53:13 +01004433static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004434#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004435 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004436#endif
4437#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004438 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004439#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004440#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004441 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004442#endif
4443#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004444 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004445#endif
4446#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004447 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004448#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004449#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004450 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004451#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004452#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004453 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004454#endif
4455#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004456 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004457#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004458 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004459};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004460
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004461static int ssl_preset_suiteb_ciphersuites[] = {
4462 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4463 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4464 0
4465};
4466
Gilles Peskineeccd8882020-03-10 12:19:08 +01004467#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004468
Jerry Yu909df7b2022-01-22 11:56:27 +08004469/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004470 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004471 * rules SHOULD be upheld.
4472 * - No duplicate entries.
4473 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004474 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004475 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004476 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004477static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004478
Andrzej Kurek25f27152022-08-17 16:09:31 -04004479#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 +08004480 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4481 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004482#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004483 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004484
Andrzej Kurek25f27152022-08-17 16:09:31 -04004485#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 +08004486 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4487 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004488#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004489 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4490
Andrzej Kurek25f27152022-08-17 16:09:31 -04004491#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 +08004492 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4493 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004494#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004495 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004496
Andrzej Kurek25f27152022-08-17 16:09:31 -04004497#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 +08004498 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004499#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 +08004500
Andrzej Kurek25f27152022-08-17 16:09:31 -04004501#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 +08004502 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004503#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 +08004504
Andrzej Kurek25f27152022-08-17 16:09:31 -04004505#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 +08004506 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004507#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 +08004508
Andrzej Kurek25f27152022-08-17 16:09:31 -04004509#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 +08004510 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4511#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4512
Andrzej Kurek25f27152022-08-17 16:09:31 -04004513#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 +08004514 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4515#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4516
Andrzej Kurek25f27152022-08-17 16:09:31 -04004517#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 +08004518 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4519#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4520
Gabor Mezei15b95a62022-05-09 16:37:58 +02004521 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004522};
4523
4524/* NOTICE: see above */
4525#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4526static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004527#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004528#if defined(MBEDTLS_ECDSA_C)
4529 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004530#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004531#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4532 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4533#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004534#if defined(MBEDTLS_RSA_C)
4535 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4536#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004537#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004538#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004539#if defined(MBEDTLS_ECDSA_C)
4540 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004541#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004542#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4543 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4544#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004545#if defined(MBEDTLS_RSA_C)
4546 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4547#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004548#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004549#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004550#if defined(MBEDTLS_ECDSA_C)
4551 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004552#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004553#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4554 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4555#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004556#if defined(MBEDTLS_RSA_C)
4557 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4558#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004559#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004560 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004561};
4562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4563/* NOTICE: see above */
4564static uint16_t ssl_preset_suiteb_sig_algs[] = {
4565
Andrzej Kurek25f27152022-08-17 16:09:31 -04004566#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 +08004567 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4568 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004569#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004570 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4571
Andrzej Kurek25f27152022-08-17 16:09:31 -04004572#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 +08004573 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4574 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004575#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004576 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004577
Andrzej Kurek25f27152022-08-17 16:09:31 -04004578#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 +08004579 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004580#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 +08004581
Andrzej Kurek25f27152022-08-17 16:09:31 -04004582#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 +08004583 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004584#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004585
Gabor Mezei15b95a62022-05-09 16:37:58 +02004586 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004587};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004588
Jerry Yu909df7b2022-01-22 11:56:27 +08004589/* NOTICE: see above */
4590#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4591static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004592#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004593#if defined(MBEDTLS_ECDSA_C)
4594 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004595#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004596#if defined(MBEDTLS_RSA_C)
4597 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4598#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004599#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004600#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004601#if defined(MBEDTLS_ECDSA_C)
4602 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004603#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004604#if defined(MBEDTLS_RSA_C)
4605 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4606#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004607#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004608 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004609};
Jerry Yu909df7b2022-01-22 11:56:27 +08004610#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4611
Jerry Yu1a8b4812022-01-20 17:56:50 +08004612#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004613
Brett Warrene0edc842021-08-17 09:53:13 +01004614static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004615#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004616 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004617#endif
4618#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004619 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004620#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004621 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004622};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004623
Jerry Yu1a8b4812022-01-20 17:56:50 +08004624#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004625/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004626 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004627MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004628static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004629{
4630 size_t i, j;
4631 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004632
Gabor Mezei15b95a62022-05-09 16:37:58 +02004633 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004634 {
Jerry Yuf377d642022-01-25 10:43:59 +08004635 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004636 {
Jerry Yuf377d642022-01-25 10:43:59 +08004637 if( sig_algs[i] != sig_algs[j] )
4638 continue;
4639 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4640 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4641 sig_algs[i], j, i );
4642 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004643 }
4644 }
4645 return( ret );
4646}
4647
4648#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4649
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004650/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004651 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004652 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004653int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004654 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004655{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004656#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004657 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004658#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004659
Jerry Yu1a8b4812022-01-20 17:56:50 +08004660#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004661 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004662 {
4663 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4664 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4665 }
4666
Jerry Yuf377d642022-01-25 10:43:59 +08004667 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004668 {
4669 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4670 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4671 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004672
4673#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004674 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004675 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004676 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004677 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4678 }
4679
Jerry Yuf377d642022-01-25 10:43:59 +08004680 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004681 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004682 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004683 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4684 }
4685#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004686#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4687
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004688 /* Use the functions here so that they are covered in tests,
4689 * but otherwise access member directly for efficiency */
4690 mbedtls_ssl_conf_endpoint( conf, endpoint );
4691 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004692
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004693 /*
4694 * Things that are common to all presets
4695 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004696#if defined(MBEDTLS_SSL_CLI_C)
4697 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4698 {
4699 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4700#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4701 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4702#endif
4703 }
4704#endif
4705
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004706#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4707 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4708#endif
4709
4710#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4711 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4712#endif
4713
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004714#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004715 conf->f_cookie_write = ssl_cookie_write_dummy;
4716 conf->f_cookie_check = ssl_cookie_check_dummy;
4717#endif
4718
4719#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4720 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4721#endif
4722
Janos Follath088ce432017-04-10 12:42:31 +01004723#if defined(MBEDTLS_SSL_SRV_C)
4724 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004725 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004726#endif
4727
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004728#if defined(MBEDTLS_SSL_PROTO_DTLS)
4729 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4730 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4731#endif
4732
4733#if defined(MBEDTLS_SSL_RENEGOTIATION)
4734 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004735 memset( conf->renego_period, 0x00, 2 );
4736 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004737#endif
4738
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004739#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004740 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4741 {
4742 const unsigned char dhm_p[] =
4743 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4744 const unsigned char dhm_g[] =
4745 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004746
Hanno Beckere2defad2021-07-24 05:59:17 +01004747 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4748 dhm_p, sizeof( dhm_p ),
4749 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4750 {
4751 return( ret );
4752 }
4753 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004754#endif
4755
Ronald Cron6f135e12021-12-08 16:57:54 +01004756#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004757#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004758 mbedtls_ssl_conf_new_session_tickets(
4759 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4760#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004761 /*
4762 * Allow all TLS 1.3 key exchange modes by default.
4763 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004764 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004765#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004766
XiaokangQian4d3a6042022-04-21 13:46:17 +00004767 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4768 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004769#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004770 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004771 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004772#else
XiaokangQian060d8672022-04-21 09:24:56 +00004773 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004774#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004775 }
4776 else
4777 {
4778#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4779 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4780 {
4781 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4782 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4783 }
4784 else
4785 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4786 {
4787 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4788 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4789 }
4790#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4791 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4792 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4793#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4794 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4795 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4796#else
4797 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4798#endif
4799 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004800
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004801 /*
4802 * Preset-specific defaults
4803 */
4804 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004805 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004806 /*
4807 * NSA Suite B
4808 */
4809 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004810
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004811 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004812
4813#if defined(MBEDTLS_X509_CRT_PARSE_C)
4814 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004815#endif
4816
Gilles Peskineeccd8882020-03-10 12:19:08 +01004817#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004818#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4819 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4820 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4821 else
4822#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4823 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004824#endif
4825
Brett Warrene0edc842021-08-17 09:53:13 +01004826#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4827 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004828#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004829 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004830 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004831
4832 /*
4833 * Default
4834 */
4835 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004836
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004837 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004838
4839#if defined(MBEDTLS_X509_CRT_PARSE_C)
4840 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4841#endif
4842
Gilles Peskineeccd8882020-03-10 12:19:08 +01004843#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004844#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4845 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4846 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4847 else
4848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4849 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004850#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004851
Brett Warrene0edc842021-08-17 09:53:13 +01004852#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4853 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004854#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004855 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004856
4857#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4858 conf->dhm_min_bitlen = 1024;
4859#endif
4860 }
4861
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004862 return( 0 );
4863}
4864
4865/*
4866 * Free mbedtls_ssl_config
4867 */
4868void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4869{
4870#if defined(MBEDTLS_DHM_C)
4871 mbedtls_mpi_free( &conf->dhm_P );
4872 mbedtls_mpi_free( &conf->dhm_G );
4873#endif
4874
Gilles Peskineeccd8882020-03-10 12:19:08 +01004875#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004876#if defined(MBEDTLS_USE_PSA_CRYPTO)
4877 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4878 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004879 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4880 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004881#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004882 if( conf->psk != NULL )
4883 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004884 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004885 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004886 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004887 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004888 }
4889
4890 if( conf->psk_identity != NULL )
4891 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004892 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004893 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004894 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004895 conf->psk_identity_len = 0;
4896 }
4897#endif
4898
4899#if defined(MBEDTLS_X509_CRT_PARSE_C)
4900 ssl_key_cert_free( conf->key_cert );
4901#endif
4902
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004903 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004904}
4905
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004906#if defined(MBEDTLS_PK_C) && \
4907 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004908/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913#if defined(MBEDTLS_RSA_C)
4914 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4915 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917#if defined(MBEDTLS_ECDSA_C)
4918 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4919 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004921 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004922}
4923
Hanno Becker7e5437a2017-04-28 17:15:26 +01004924unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4925{
4926 switch( type ) {
4927 case MBEDTLS_PK_RSA:
4928 return( MBEDTLS_SSL_SIG_RSA );
4929 case MBEDTLS_PK_ECDSA:
4930 case MBEDTLS_PK_ECKEY:
4931 return( MBEDTLS_SSL_SIG_ECDSA );
4932 default:
4933 return( MBEDTLS_SSL_SIG_ANON );
4934 }
4935}
4936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004938{
4939 switch( sig )
4940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#if defined(MBEDTLS_RSA_C)
4942 case MBEDTLS_SSL_SIG_RSA:
4943 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945#if defined(MBEDTLS_ECDSA_C)
4946 case MBEDTLS_SSL_SIG_ECDSA:
4947 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004948#endif
4949 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004951 }
4952}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004953#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004954
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004955/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004956 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004959{
4960 switch( hash )
4961 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004962#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 case MBEDTLS_SSL_HASH_MD5:
4964 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004965#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004966#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967 case MBEDTLS_SSL_HASH_SHA1:
4968 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004969#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004970#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 case MBEDTLS_SSL_HASH_SHA224:
4972 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004973#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004974#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975 case MBEDTLS_SSL_HASH_SHA256:
4976 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004977#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004978#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 case MBEDTLS_SSL_HASH_SHA384:
4980 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004981#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004982#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 case MBEDTLS_SSL_HASH_SHA512:
4984 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004985#endif
4986 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004988 }
4989}
4990
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004991/*
4992 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4993 */
4994unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4995{
4996 switch( md )
4997 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004998#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004999 case MBEDTLS_MD_MD5:
5000 return( MBEDTLS_SSL_HASH_MD5 );
5001#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005002#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005003 case MBEDTLS_MD_SHA1:
5004 return( MBEDTLS_SSL_HASH_SHA1 );
5005#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005006#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005007 case MBEDTLS_MD_SHA224:
5008 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005009#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005010#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005011 case MBEDTLS_MD_SHA256:
5012 return( MBEDTLS_SSL_HASH_SHA256 );
5013#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005014#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005015 case MBEDTLS_MD_SHA384:
5016 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005017#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005018#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005019 case MBEDTLS_MD_SHA512:
5020 return( MBEDTLS_SSL_HASH_SHA512 );
5021#endif
5022 default:
5023 return( MBEDTLS_SSL_HASH_NONE );
5024 }
5025}
5026
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005027/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005028 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005029 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005030 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005031int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005032{
Brett Warrene0edc842021-08-17 09:53:13 +01005033 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005034
Brett Warrene0edc842021-08-17 09:53:13 +01005035 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005036 return( -1 );
5037
Brett Warrene0edc842021-08-17 09:53:13 +01005038 for( ; *group_list != 0; group_list++ )
5039 {
5040 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005041 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005042 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005043
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005044 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005045}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005046
5047#if defined(MBEDTLS_ECP_C)
5048/*
5049 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5050 */
5051int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5052{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005053 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005054 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005055
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005056 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005057 return -1;
5058
5059 uint16_t tls_id = grp_info->tls_id;
5060
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005061 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5062}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005063#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065#if defined(MBEDTLS_X509_CRT_PARSE_C)
5066int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5067 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005068 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005069 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005070{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005071 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005072 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005073 const char *ext_oid;
5074 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005077 {
5078 /* Server part of the key exchange */
5079 switch( ciphersuite->key_exchange )
5080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 case MBEDTLS_KEY_EXCHANGE_RSA:
5082 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005083 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005084 break;
5085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5087 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5088 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5089 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005090 break;
5091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5093 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005094 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005095 break;
5096
5097 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 case MBEDTLS_KEY_EXCHANGE_NONE:
5099 case MBEDTLS_KEY_EXCHANGE_PSK:
5100 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5101 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005102 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005103 usage = 0;
5104 }
5105 }
5106 else
5107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5109 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005110 }
5111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005113 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005114 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005115 ret = -1;
5116 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5121 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005122 }
5123 else
5124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5126 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005127 }
5128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 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_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005132 ret = -1;
5133 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005134
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005135 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005138
Jerry Yu148165c2021-09-24 23:20:59 +08005139#if defined(MBEDTLS_USE_PSA_CRYPTO)
5140int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5141 const mbedtls_md_type_t md,
5142 unsigned char *dst,
5143 size_t dst_len,
5144 size_t *olen )
5145{
Ronald Cronf6893e12022-01-07 22:09:01 +01005146 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5147 psa_hash_operation_t *hash_operation_to_clone;
5148 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5149
Jerry Yu148165c2021-09-24 23:20:59 +08005150 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005151
5152 switch( md )
5153 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005154#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005155 case MBEDTLS_MD_SHA384:
5156 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5157 break;
5158#endif
5159
Andrzej Kurek25f27152022-08-17 16:09:31 -04005160#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005161 case MBEDTLS_MD_SHA256:
5162 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5163 break;
5164#endif
5165
5166 default:
5167 goto exit;
5168 }
5169
5170 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5171 if( status != PSA_SUCCESS )
5172 goto exit;
5173
5174 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5175 if( status != PSA_SUCCESS )
5176 goto exit;
5177
5178exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005179 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005180}
5181#else /* MBEDTLS_USE_PSA_CRYPTO */
5182
Andrzej Kurek25f27152022-08-17 16:09:31 -04005183#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005184MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005185static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5186 unsigned char *dst,
5187 size_t dst_len,
5188 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005189{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005190 int ret;
5191 mbedtls_sha512_context sha512;
5192
5193 if( dst_len < 48 )
5194 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5195
5196 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005197 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005198
5199 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5200 {
5201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5202 goto exit;
5203 }
5204
5205 *olen = 48;
5206
5207exit:
5208
5209 mbedtls_sha512_free( &sha512 );
5210 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005211}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005212#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005213
Andrzej Kurek25f27152022-08-17 16:09:31 -04005214#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005215MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005216static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5217 unsigned char *dst,
5218 size_t dst_len,
5219 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005220{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005221 int ret;
5222 mbedtls_sha256_context sha256;
5223
5224 if( dst_len < 32 )
5225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5226
5227 mbedtls_sha256_init( &sha256 );
5228 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005229
Jerry Yu24c0ec32021-09-09 14:21:07 +08005230 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5231 {
5232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5233 goto exit;
5234 }
5235
5236 *olen = 32;
5237
5238exit:
5239
5240 mbedtls_sha256_free( &sha256 );
5241 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005242}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005243#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005244
Jerry Yu000f9762021-09-14 11:12:51 +08005245int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5246 const mbedtls_md_type_t md,
5247 unsigned char *dst,
5248 size_t dst_len,
5249 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005250{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005251 switch( md )
5252 {
5253
Andrzej Kurek25f27152022-08-17 16:09:31 -04005254#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005255 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005256 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005257#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005258
Andrzej Kurek25f27152022-08-17 16:09:31 -04005259#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005260 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005261 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005262#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005263
5264 default:
5265 break;
5266 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005268}
XiaokangQian647719a2021-12-07 09:16:29 +00005269
Jerry Yu148165c2021-09-24 23:20:59 +08005270#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005271
Gabor Mezei078e8032022-04-27 21:17:56 +02005272#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5273/* mbedtls_ssl_parse_sig_alg_ext()
5274 *
5275 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5276 * value (TLS 1.3 RFC8446):
5277 * enum {
5278 * ....
5279 * ecdsa_secp256r1_sha256( 0x0403 ),
5280 * ecdsa_secp384r1_sha384( 0x0503 ),
5281 * ecdsa_secp521r1_sha512( 0x0603 ),
5282 * ....
5283 * } SignatureScheme;
5284 *
5285 * struct {
5286 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5287 * } SignatureSchemeList;
5288 *
5289 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5290 * value (TLS 1.2 RFC5246):
5291 * enum {
5292 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5293 * sha512(6), (255)
5294 * } HashAlgorithm;
5295 *
5296 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5297 * SignatureAlgorithm;
5298 *
5299 * struct {
5300 * HashAlgorithm hash;
5301 * SignatureAlgorithm signature;
5302 * } SignatureAndHashAlgorithm;
5303 *
5304 * SignatureAndHashAlgorithm
5305 * supported_signature_algorithms<2..2^16-2>;
5306 *
5307 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5308 * generalization of the TLS 1.2 signature algorithm extension.
5309 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5310 * `SignatureScheme` field of TLS 1.3
5311 *
5312 */
5313int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5314 const unsigned char *buf,
5315 const unsigned char *end )
5316{
5317 const unsigned char *p = buf;
5318 size_t supported_sig_algs_len = 0;
5319 const unsigned char *supported_sig_algs_end;
5320 uint16_t sig_alg;
5321 uint32_t common_idx = 0;
5322
5323 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5324 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5325 p += 2;
5326
5327 memset( ssl->handshake->received_sig_algs, 0,
5328 sizeof(ssl->handshake->received_sig_algs) );
5329
5330 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5331 supported_sig_algs_end = p + supported_sig_algs_len;
5332 while( p < supported_sig_algs_end )
5333 {
5334 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5335 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5336 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005337 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5338 sig_alg,
5339 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005340#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005341 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005342 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5343 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5344 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005345 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005346 }
5347#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005348
Jerry Yuf3b46b52022-06-19 16:52:27 +08005349 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5350 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005351
5352 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5353 {
5354 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5355 common_idx += 1;
5356 }
5357 }
5358 /* Check that we consumed all the message. */
5359 if( p != end )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 1,
5362 ( "Signature algorithms extension length misaligned" ) );
5363 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5364 MBEDTLS_ERR_SSL_DECODE_ERROR );
5365 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5366 }
5367
5368 if( common_idx == 0 )
5369 {
5370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5371 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5372 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5373 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5374 }
5375
Gabor Mezei15b95a62022-05-09 16:37:58 +02005376 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005377 return( 0 );
5378}
5379
5380#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5381
Jerry Yudc7bd172022-02-17 13:44:15 +08005382#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5383
5384#if defined(MBEDTLS_USE_PSA_CRYPTO)
5385
5386static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5387 mbedtls_svc_key_id_t key,
5388 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005389 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005390 const unsigned char* seed, size_t seed_length,
5391 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005392 const unsigned char* other_secret,
5393 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005394 size_t capacity )
5395{
5396 psa_status_t status;
5397
5398 status = psa_key_derivation_setup( derivation, alg );
5399 if( status != PSA_SUCCESS )
5400 return( status );
5401
5402 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5403 {
5404 status = psa_key_derivation_input_bytes( derivation,
5405 PSA_KEY_DERIVATION_INPUT_SEED,
5406 seed, seed_length );
5407 if( status != PSA_SUCCESS )
5408 return( status );
5409
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005410 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005411 {
5412 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005413 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5414 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005415 if( status != PSA_SUCCESS )
5416 return( status );
5417 }
5418
Jerry Yudc7bd172022-02-17 13:44:15 +08005419 if( mbedtls_svc_key_id_is_null( key ) )
5420 {
5421 status = psa_key_derivation_input_bytes(
5422 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005423 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005424 }
5425 else
5426 {
5427 status = psa_key_derivation_input_key(
5428 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5429 }
5430 if( status != PSA_SUCCESS )
5431 return( status );
5432
5433 status = psa_key_derivation_input_bytes( derivation,
5434 PSA_KEY_DERIVATION_INPUT_LABEL,
5435 label, label_length );
5436 if( status != PSA_SUCCESS )
5437 return( status );
5438 }
5439 else
5440 {
5441 return( PSA_ERROR_NOT_SUPPORTED );
5442 }
5443
5444 status = psa_key_derivation_set_capacity( derivation, capacity );
5445 if( status != PSA_SUCCESS )
5446 return( status );
5447
5448 return( PSA_SUCCESS );
5449}
5450
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005451MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005452static int tls_prf_generic( mbedtls_md_type_t md_type,
5453 const unsigned char *secret, size_t slen,
5454 const char *label,
5455 const unsigned char *random, size_t rlen,
5456 unsigned char *dstbuf, size_t dlen )
5457{
5458 psa_status_t status;
5459 psa_algorithm_t alg;
5460 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5461 psa_key_derivation_operation_t derivation =
5462 PSA_KEY_DERIVATION_OPERATION_INIT;
5463
5464 if( md_type == MBEDTLS_MD_SHA384 )
5465 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5466 else
5467 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5468
5469 /* Normally a "secret" should be long enough to be impossible to
5470 * find by brute force, and in particular should not be empty. But
5471 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5472 * and for this use case it makes sense to have a 0-length "secret".
5473 * Since the key API doesn't allow importing a key of length 0,
5474 * keep master_key=0, which setup_psa_key_derivation() understands
5475 * to mean a 0-length "secret" input. */
5476 if( slen != 0 )
5477 {
5478 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5479 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5480 psa_set_key_algorithm( &key_attributes, alg );
5481 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5482
5483 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5484 if( status != PSA_SUCCESS )
5485 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5486 }
5487
5488 status = setup_psa_key_derivation( &derivation,
5489 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005490 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005491 random, rlen,
5492 (unsigned char const *) label,
5493 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005494 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005495 dlen );
5496 if( status != PSA_SUCCESS )
5497 {
5498 psa_key_derivation_abort( &derivation );
5499 psa_destroy_key( master_key );
5500 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5501 }
5502
5503 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5504 if( status != PSA_SUCCESS )
5505 {
5506 psa_key_derivation_abort( &derivation );
5507 psa_destroy_key( master_key );
5508 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5509 }
5510
5511 status = psa_key_derivation_abort( &derivation );
5512 if( status != PSA_SUCCESS )
5513 {
5514 psa_destroy_key( master_key );
5515 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5516 }
5517
5518 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5519 status = psa_destroy_key( master_key );
5520 if( status != PSA_SUCCESS )
5521 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5522
5523 return( 0 );
5524}
5525
5526#else /* MBEDTLS_USE_PSA_CRYPTO */
5527
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005528MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005529static int tls_prf_generic( mbedtls_md_type_t md_type,
5530 const unsigned char *secret, size_t slen,
5531 const char *label,
5532 const unsigned char *random, size_t rlen,
5533 unsigned char *dstbuf, size_t dlen )
5534{
5535 size_t nb;
5536 size_t i, j, k, md_len;
5537 unsigned char *tmp;
5538 size_t tmp_len = 0;
5539 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5540 const mbedtls_md_info_t *md_info;
5541 mbedtls_md_context_t md_ctx;
5542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5543
5544 mbedtls_md_init( &md_ctx );
5545
5546 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5548
5549 md_len = mbedtls_md_get_size( md_info );
5550
5551 tmp_len = md_len + strlen( label ) + rlen;
5552 tmp = mbedtls_calloc( 1, tmp_len );
5553 if( tmp == NULL )
5554 {
5555 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5556 goto exit;
5557 }
5558
5559 nb = strlen( label );
5560 memcpy( tmp + md_len, label, nb );
5561 memcpy( tmp + md_len + nb, random, rlen );
5562 nb += rlen;
5563
5564 /*
5565 * Compute P_<hash>(secret, label + random)[0..dlen]
5566 */
5567 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5568 goto exit;
5569
5570 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5571 if( ret != 0 )
5572 goto exit;
5573 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5574 if( ret != 0 )
5575 goto exit;
5576 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5577 if( ret != 0 )
5578 goto exit;
5579
5580 for( i = 0; i < dlen; i += md_len )
5581 {
5582 ret = mbedtls_md_hmac_reset ( &md_ctx );
5583 if( ret != 0 )
5584 goto exit;
5585 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5586 if( ret != 0 )
5587 goto exit;
5588 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5589 if( ret != 0 )
5590 goto exit;
5591
5592 ret = mbedtls_md_hmac_reset ( &md_ctx );
5593 if( ret != 0 )
5594 goto exit;
5595 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5596 if( ret != 0 )
5597 goto exit;
5598 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5599 if( ret != 0 )
5600 goto exit;
5601
5602 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5603
5604 for( j = 0; j < k; j++ )
5605 dstbuf[i + j] = h_i[j];
5606 }
5607
5608exit:
5609 mbedtls_md_free( &md_ctx );
5610
5611 mbedtls_platform_zeroize( tmp, tmp_len );
5612 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5613
5614 mbedtls_free( tmp );
5615
5616 return( ret );
5617}
5618#endif /* MBEDTLS_USE_PSA_CRYPTO */
5619
Andrzej Kurek25f27152022-08-17 16:09:31 -04005620#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005621MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005622static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5623 const char *label,
5624 const unsigned char *random, size_t rlen,
5625 unsigned char *dstbuf, size_t dlen )
5626{
5627 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5628 label, random, rlen, dstbuf, dlen ) );
5629}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005630#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005631
Andrzej Kurek25f27152022-08-17 16:09:31 -04005632#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005633MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005634static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5635 const char *label,
5636 const unsigned char *random, size_t rlen,
5637 unsigned char *dstbuf, size_t dlen )
5638{
5639 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5640 label, random, rlen, dstbuf, dlen ) );
5641}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005642#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005643
Jerry Yuf009d862022-02-17 14:01:37 +08005644/*
5645 * Set appropriate PRF function and other SSL / TLS1.2 functions
5646 *
5647 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005648 * - hash associated with the ciphersuite (only used by TLS 1.2)
5649 *
5650 * Outputs:
5651 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5652 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005653MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005654static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005655 mbedtls_md_type_t hash )
5656{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005657#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005658 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005659 {
5660 handshake->tls_prf = tls_prf_sha384;
5661 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5662 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5663 }
5664 else
5665#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005666#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005667 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005668 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005669 handshake->tls_prf = tls_prf_sha256;
5670 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5671 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5672 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005673#else
Jerry Yuf009d862022-02-17 14:01:37 +08005674 {
Jerry Yuf009d862022-02-17 14:01:37 +08005675 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005676 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5678 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005679#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005680
5681 return( 0 );
5682}
Jerry Yud6ab2352022-02-17 14:03:43 +08005683
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005684/*
5685 * Compute master secret if needed
5686 *
5687 * Parameters:
5688 * [in/out] handshake
5689 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5690 * (PSA-PSK) ciphersuite_info, psk_opaque
5691 * [out] premaster (cleared)
5692 * [out] master
5693 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5694 * debug: conf->f_dbg, conf->p_dbg
5695 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005696 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005697 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005698MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005699static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5700 unsigned char *master,
5701 const mbedtls_ssl_context *ssl )
5702{
5703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5704
5705 /* cf. RFC 5246, Section 8.1:
5706 * "The master secret is always exactly 48 bytes in length." */
5707 size_t const master_secret_len = 48;
5708
5709#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5710 unsigned char session_hash[48];
5711#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5712
5713 /* The label for the KDF used for key expansion.
5714 * This is either "master secret" or "extended master secret"
5715 * depending on whether the Extended Master Secret extension
5716 * is used. */
5717 char const *lbl = "master secret";
5718
Przemek Stekielae4ed302022-04-05 17:15:55 +02005719 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005720 * - If the Extended Master Secret extension is not used,
5721 * this is ClientHello.Random + ServerHello.Random
5722 * (see Sect. 8.1 in RFC 5246).
5723 * - If the Extended Master Secret extension is used,
5724 * this is the transcript of the handshake so far.
5725 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005726 unsigned char const *seed = handshake->randbytes;
5727 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005728
5729#if !defined(MBEDTLS_DEBUG_C) && \
5730 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5731 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5732 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5733 ssl = NULL; /* make sure we don't use it except for those cases */
5734 (void) ssl;
5735#endif
5736
5737 if( handshake->resume != 0 )
5738 {
5739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5740 return( 0 );
5741 }
5742
5743#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5744 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5745 {
5746 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005747 seed = session_hash;
5748 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005749
5750 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005751 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005752 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005753#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005754
Przemek Stekiel99114f32022-04-22 11:20:09 +02005755#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005756 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005757 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005758 {
5759 /* Perform PSK-to-MS expansion in a single step. */
5760 psa_status_t status;
5761 psa_algorithm_t alg;
5762 mbedtls_svc_key_id_t psk;
5763 psa_key_derivation_operation_t derivation =
5764 PSA_KEY_DERIVATION_OPERATION_INIT;
5765 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5766
5767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5768
5769 psk = mbedtls_ssl_get_opaque_psk( ssl );
5770
5771 if( hash_alg == MBEDTLS_MD_SHA384 )
5772 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5773 else
5774 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5775
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005776 size_t other_secret_len = 0;
5777 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005778
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005779 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005780 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005781 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005782 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005783 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005784 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005785 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005786 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005787 other_secret_len = 48;
5788 other_secret = handshake->premaster + 2;
5789 break;
5790 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005791 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005792 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5793 other_secret = handshake->premaster + 2;
5794 break;
5795 default:
5796 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005797 }
5798
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005799 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005800 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005801 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005802 (unsigned char const *) lbl,
5803 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005804 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005805 master_secret_len );
5806 if( status != PSA_SUCCESS )
5807 {
5808 psa_key_derivation_abort( &derivation );
5809 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5810 }
5811
5812 status = psa_key_derivation_output_bytes( &derivation,
5813 master,
5814 master_secret_len );
5815 if( status != PSA_SUCCESS )
5816 {
5817 psa_key_derivation_abort( &derivation );
5818 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5819 }
5820
5821 status = psa_key_derivation_abort( &derivation );
5822 if( status != PSA_SUCCESS )
5823 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5824 }
5825 else
5826#endif
5827 {
5828 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005829 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005830 master,
5831 master_secret_len );
5832 if( ret != 0 )
5833 {
5834 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5835 return( ret );
5836 }
5837
5838 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5839 handshake->premaster,
5840 handshake->pmslen );
5841
5842 mbedtls_platform_zeroize( handshake->premaster,
5843 sizeof(handshake->premaster) );
5844 }
5845
5846 return( 0 );
5847}
5848
Jerry Yud62f87e2022-02-17 14:09:02 +08005849int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5850{
5851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5852 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5853 ssl->handshake->ciphersuite_info;
5854
5855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5856
5857 /* Set PRF, calc_verify and calc_finished function pointers */
5858 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005859 ciphersuite_info->mac );
5860 if( ret != 0 )
5861 {
5862 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5863 return( ret );
5864 }
5865
5866 /* Compute master secret if needed */
5867 ret = ssl_compute_master( ssl->handshake,
5868 ssl->session_negotiate->master,
5869 ssl );
5870 if( ret != 0 )
5871 {
5872 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5873 return( ret );
5874 }
5875
5876 /* Swap the client and server random values:
5877 * - MS derivation wanted client+server (RFC 5246 8.1)
5878 * - key derivation wants server+client (RFC 5246 6.3) */
5879 {
5880 unsigned char tmp[64];
5881 memcpy( tmp, ssl->handshake->randbytes, 64 );
5882 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5883 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5884 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5885 }
5886
5887 /* Populate transform structure */
5888 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5889 ssl->session_negotiate->ciphersuite,
5890 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005891#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005892 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005893#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005894 ssl->handshake->tls_prf,
5895 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005896 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005897 ssl->conf->endpoint,
5898 ssl );
5899 if( ret != 0 )
5900 {
5901 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5902 return( ret );
5903 }
5904
5905 /* We no longer need Server/ClientHello.random values */
5906 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5907 sizeof( ssl->handshake->randbytes ) );
5908
5909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5910
5911 return( 0 );
5912}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005913
Ronald Cron4dcbca92022-03-07 10:21:40 +01005914int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5915{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005916 switch( md )
5917 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005918#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005919 case MBEDTLS_SSL_HASH_SHA384:
5920 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5921 break;
5922#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005923#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005924 case MBEDTLS_SSL_HASH_SHA256:
5925 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5926 break;
5927#endif
5928 default:
5929 return( -1 );
5930 }
5931
Ronald Cronc2f13a02022-03-07 10:25:24 +01005932 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005933}
5934
Andrzej Kurek25f27152022-08-17 16:09:31 -04005935#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005936void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5937 unsigned char *hash,
5938 size_t *hlen )
5939{
5940#if defined(MBEDTLS_USE_PSA_CRYPTO)
5941 size_t hash_size;
5942 psa_status_t status;
5943 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5944
5945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5946 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5947 if( status != PSA_SUCCESS )
5948 {
5949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5950 return;
5951 }
5952
5953 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5954 if( status != PSA_SUCCESS )
5955 {
5956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5957 return;
5958 }
5959
5960 *hlen = 32;
5961 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5963#else
5964 mbedtls_sha256_context sha256;
5965
5966 mbedtls_sha256_init( &sha256 );
5967
5968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5969
5970 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5971 mbedtls_sha256_finish( &sha256, hash );
5972
5973 *hlen = 32;
5974
5975 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5977
5978 mbedtls_sha256_free( &sha256 );
5979#endif /* MBEDTLS_USE_PSA_CRYPTO */
5980 return;
5981}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005982#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005983
Andrzej Kurek25f27152022-08-17 16:09:31 -04005984#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005985void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5986 unsigned char *hash,
5987 size_t *hlen )
5988{
5989#if defined(MBEDTLS_USE_PSA_CRYPTO)
5990 size_t hash_size;
5991 psa_status_t status;
5992 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5993
5994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5995 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5996 if( status != PSA_SUCCESS )
5997 {
5998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5999 return;
6000 }
6001
6002 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6003 if( status != PSA_SUCCESS )
6004 {
6005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6006 return;
6007 }
6008
6009 *hlen = 48;
6010 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6012#else
6013 mbedtls_sha512_context sha512;
6014
6015 mbedtls_sha512_init( &sha512 );
6016
6017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6018
Andrzej Kureka242e832022-08-11 10:03:14 -04006019 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006020 mbedtls_sha512_finish( &sha512, hash );
6021
6022 *hlen = 48;
6023
6024 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6026
6027 mbedtls_sha512_free( &sha512 );
6028#endif /* MBEDTLS_USE_PSA_CRYPTO */
6029 return;
6030}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006031#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006032
Neil Armstrong80f6f322022-05-03 17:56:38 +02006033#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6034 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006035int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6036{
6037 unsigned char *p = ssl->handshake->premaster;
6038 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6039 const unsigned char *psk = NULL;
6040 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006041 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006042
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006043 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006044 {
6045 /*
6046 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006047 * checked before calling this function.
6048 *
6049 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006050 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006051 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006052 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6053 {
6054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6056 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006057 }
6058
6059 /*
6060 * PMS = struct {
6061 * opaque other_secret<0..2^16-1>;
6062 * opaque psk<0..2^16-1>;
6063 * };
6064 * with "other_secret" depending on the particular key exchange
6065 */
6066#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6067 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6068 {
6069 if( end - p < 2 )
6070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6071
6072 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6073 p += 2;
6074
6075 if( end < p || (size_t)( end - p ) < psk_len )
6076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6077
6078 memset( p, 0, psk_len );
6079 p += psk_len;
6080 }
6081 else
6082#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6083#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6084 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6085 {
6086 /*
6087 * other_secret already set by the ClientKeyExchange message,
6088 * and is 48 bytes long
6089 */
6090 if( end - p < 2 )
6091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6092
6093 *p++ = 0;
6094 *p++ = 48;
6095 p += 48;
6096 }
6097 else
6098#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6099#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6100 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6101 {
6102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6103 size_t len;
6104
6105 /* Write length only when we know the actual value */
6106 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6107 p + 2, end - ( p + 2 ), &len,
6108 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6109 {
6110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6111 return( ret );
6112 }
6113 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6114 p += 2 + len;
6115
6116 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6117 }
6118 else
6119#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006120#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006121 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6122 {
6123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6124 size_t zlen;
6125
6126 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6127 p + 2, end - ( p + 2 ),
6128 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6129 {
6130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6131 return( ret );
6132 }
6133
6134 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6135 p += 2 + zlen;
6136
6137 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6138 MBEDTLS_DEBUG_ECDH_Z );
6139 }
6140 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006141#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006142 {
6143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6145 }
6146
6147 /* opaque psk<0..2^16-1>; */
6148 if( end - p < 2 )
6149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6150
6151 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6152 p += 2;
6153
6154 if( end < p || (size_t)( end - p ) < psk_len )
6155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6156
6157 memcpy( p, psk, psk_len );
6158 p += psk_len;
6159
6160 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6161
6162 return( 0 );
6163}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006164#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006165
6166#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006167MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006168static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6169
6170#if defined(MBEDTLS_SSL_PROTO_DTLS)
6171int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6172{
6173 /* If renegotiation is not enforced, retransmit until we would reach max
6174 * timeout if we were using the usual handshake doubling scheme */
6175 if( ssl->conf->renego_max_records < 0 )
6176 {
6177 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6178 unsigned char doublings = 1;
6179
6180 while( ratio != 0 )
6181 {
6182 ++doublings;
6183 ratio >>= 1;
6184 }
6185
6186 if( ++ssl->renego_records_seen > doublings )
6187 {
6188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6189 return( 0 );
6190 }
6191 }
6192
6193 return( ssl_write_hello_request( ssl ) );
6194}
6195#endif
6196#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006197
Jerry Yud9526692022-02-17 14:23:47 +08006198/*
6199 * Handshake functions
6200 */
6201#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6202/* No certificate support -> dummy functions */
6203int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6204{
6205 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6206 ssl->handshake->ciphersuite_info;
6207
6208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6209
6210 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6211 {
6212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6213 ssl->state++;
6214 return( 0 );
6215 }
6216
6217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6219}
6220
6221int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6222{
6223 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6224 ssl->handshake->ciphersuite_info;
6225
6226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6227
6228 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6229 {
6230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6231 ssl->state++;
6232 return( 0 );
6233 }
6234
6235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6237}
6238
6239#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6240/* Some certificate support -> implement write and parse */
6241
6242int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6243{
6244 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6245 size_t i, n;
6246 const mbedtls_x509_crt *crt;
6247 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6248 ssl->handshake->ciphersuite_info;
6249
6250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6251
6252 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6253 {
6254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6255 ssl->state++;
6256 return( 0 );
6257 }
6258
6259#if defined(MBEDTLS_SSL_CLI_C)
6260 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6261 {
6262 if( ssl->handshake->client_auth == 0 )
6263 {
6264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6265 ssl->state++;
6266 return( 0 );
6267 }
6268 }
6269#endif /* MBEDTLS_SSL_CLI_C */
6270#if defined(MBEDTLS_SSL_SRV_C)
6271 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6272 {
6273 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6274 {
6275 /* Should never happen because we shouldn't have picked the
6276 * ciphersuite if we don't have a certificate. */
6277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6278 }
6279 }
6280#endif
6281
6282 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6283
6284 /*
6285 * 0 . 0 handshake type
6286 * 1 . 3 handshake length
6287 * 4 . 6 length of all certs
6288 * 7 . 9 length of cert. 1
6289 * 10 . n-1 peer certificate
6290 * n . n+2 length of cert. 2
6291 * n+3 . ... upper level cert, etc.
6292 */
6293 i = 7;
6294 crt = mbedtls_ssl_own_cert( ssl );
6295
6296 while( crt != NULL )
6297 {
6298 n = crt->raw.len;
6299 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6300 {
6301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6302 " > %" MBEDTLS_PRINTF_SIZET,
6303 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6304 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6305 }
6306
6307 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6308 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6309 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6310
6311 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6312 i += n; crt = crt->next;
6313 }
6314
6315 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6316 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6317 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6318
6319 ssl->out_msglen = i;
6320 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6321 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6322
6323 ssl->state++;
6324
6325 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6326 {
6327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6328 return( ret );
6329 }
6330
6331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6332
6333 return( ret );
6334}
6335
6336#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6337
6338#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006339MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006340static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6341 unsigned char *crt_buf,
6342 size_t crt_buf_len )
6343{
6344 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6345
6346 if( peer_crt == NULL )
6347 return( -1 );
6348
6349 if( peer_crt->raw.len != crt_buf_len )
6350 return( -1 );
6351
6352 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6353}
6354#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006355MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006356static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6357 unsigned char *crt_buf,
6358 size_t crt_buf_len )
6359{
6360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6361 unsigned char const * const peer_cert_digest =
6362 ssl->session->peer_cert_digest;
6363 mbedtls_md_type_t const peer_cert_digest_type =
6364 ssl->session->peer_cert_digest_type;
6365 mbedtls_md_info_t const * const digest_info =
6366 mbedtls_md_info_from_type( peer_cert_digest_type );
6367 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6368 size_t digest_len;
6369
6370 if( peer_cert_digest == NULL || digest_info == NULL )
6371 return( -1 );
6372
6373 digest_len = mbedtls_md_get_size( digest_info );
6374 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6375 return( -1 );
6376
6377 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6378 if( ret != 0 )
6379 return( -1 );
6380
6381 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6382}
6383#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6384#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6385
6386/*
6387 * Once the certificate message is read, parse it into a cert chain and
6388 * perform basic checks, but leave actual verification to the caller
6389 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006390MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006391static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6392 mbedtls_x509_crt *chain )
6393{
6394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6395#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6396 int crt_cnt=0;
6397#endif
6398 size_t i, n;
6399 uint8_t alert;
6400
6401 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6402 {
6403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6404 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6405 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6406 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6407 }
6408
6409 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6410 {
6411 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6412 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6413 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6414 }
6415
6416 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6417 {
6418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6419 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6420 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6421 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6422 }
6423
6424 i = mbedtls_ssl_hs_hdr_len( ssl );
6425
6426 /*
6427 * Same message structure as in mbedtls_ssl_write_certificate()
6428 */
6429 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6430
6431 if( ssl->in_msg[i] != 0 ||
6432 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6433 {
6434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6435 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6436 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6437 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6438 }
6439
6440 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6441 i += 3;
6442
6443 /* Iterate through and parse the CRTs in the provided chain. */
6444 while( i < ssl->in_hslen )
6445 {
6446 /* Check that there's room for the next CRT's length fields. */
6447 if ( i + 3 > ssl->in_hslen ) {
6448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6449 mbedtls_ssl_send_alert_message( ssl,
6450 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6451 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6452 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6453 }
6454 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6455 * anything beyond 2**16 ~ 64K. */
6456 if( ssl->in_msg[i] != 0 )
6457 {
6458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6459 mbedtls_ssl_send_alert_message( ssl,
6460 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6461 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6462 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6463 }
6464
6465 /* Read length of the next CRT in the chain. */
6466 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6467 | (unsigned int) ssl->in_msg[i + 2];
6468 i += 3;
6469
6470 if( n < 128 || i + n > ssl->in_hslen )
6471 {
6472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6473 mbedtls_ssl_send_alert_message( ssl,
6474 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6475 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6476 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6477 }
6478
6479 /* Check if we're handling the first CRT in the chain. */
6480#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6481 if( crt_cnt++ == 0 &&
6482 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6483 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6484 {
6485 /* During client-side renegotiation, check that the server's
6486 * end-CRTs hasn't changed compared to the initial handshake,
6487 * mitigating the triple handshake attack. On success, reuse
6488 * the original end-CRT instead of parsing it again. */
6489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6490 if( ssl_check_peer_crt_unchanged( ssl,
6491 &ssl->in_msg[i],
6492 n ) != 0 )
6493 {
6494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6495 mbedtls_ssl_send_alert_message( ssl,
6496 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6497 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6498 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6499 }
6500
6501 /* Now we can safely free the original chain. */
6502 ssl_clear_peer_cert( ssl->session );
6503 }
6504#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6505
6506 /* Parse the next certificate in the chain. */
6507#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6508 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6509#else
6510 /* If we don't need to store the CRT chain permanently, parse
6511 * it in-place from the input buffer instead of making a copy. */
6512 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6513#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6514 switch( ret )
6515 {
6516 case 0: /*ok*/
6517 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6518 /* Ignore certificate with an unknown algorithm: maybe a
6519 prior certificate was already trusted. */
6520 break;
6521
6522 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6523 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6524 goto crt_parse_der_failed;
6525
6526 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6527 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6528 goto crt_parse_der_failed;
6529
6530 default:
6531 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6532 crt_parse_der_failed:
6533 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6534 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6535 return( ret );
6536 }
6537
6538 i += n;
6539 }
6540
6541 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6542 return( 0 );
6543}
6544
6545#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006546MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006547static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6548{
6549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6550 return( -1 );
6551
6552 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6553 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6554 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6555 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6556 {
Ronald Cron19385882022-06-15 16:26:13 +02006557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006558 return( 0 );
6559 }
6560 return( -1 );
6561}
6562#endif /* MBEDTLS_SSL_SRV_C */
6563
6564/* Check if a certificate message is expected.
6565 * Return either
6566 * - SSL_CERTIFICATE_EXPECTED, or
6567 * - SSL_CERTIFICATE_SKIP
6568 * indicating whether a Certificate message is expected or not.
6569 */
6570#define SSL_CERTIFICATE_EXPECTED 0
6571#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006572MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006573static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6574 int authmode )
6575{
6576 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6577 ssl->handshake->ciphersuite_info;
6578
6579 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6580 return( SSL_CERTIFICATE_SKIP );
6581
6582#if defined(MBEDTLS_SSL_SRV_C)
6583 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6584 {
6585 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6586 return( SSL_CERTIFICATE_SKIP );
6587
6588 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6589 {
6590 ssl->session_negotiate->verify_result =
6591 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6592 return( SSL_CERTIFICATE_SKIP );
6593 }
6594 }
6595#else
6596 ((void) authmode);
6597#endif /* MBEDTLS_SSL_SRV_C */
6598
6599 return( SSL_CERTIFICATE_EXPECTED );
6600}
6601
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006602MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006603static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6604 int authmode,
6605 mbedtls_x509_crt *chain,
6606 void *rs_ctx )
6607{
6608 int ret = 0;
6609 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6610 ssl->handshake->ciphersuite_info;
6611 int have_ca_chain = 0;
6612
6613 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6614 void *p_vrfy;
6615
6616 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6617 return( 0 );
6618
6619 if( ssl->f_vrfy != NULL )
6620 {
6621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6622 f_vrfy = ssl->f_vrfy;
6623 p_vrfy = ssl->p_vrfy;
6624 }
6625 else
6626 {
6627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6628 f_vrfy = ssl->conf->f_vrfy;
6629 p_vrfy = ssl->conf->p_vrfy;
6630 }
6631
6632 /*
6633 * Main check: verify certificate
6634 */
6635#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6636 if( ssl->conf->f_ca_cb != NULL )
6637 {
6638 ((void) rs_ctx);
6639 have_ca_chain = 1;
6640
6641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6642 ret = mbedtls_x509_crt_verify_with_ca_cb(
6643 chain,
6644 ssl->conf->f_ca_cb,
6645 ssl->conf->p_ca_cb,
6646 ssl->conf->cert_profile,
6647 ssl->hostname,
6648 &ssl->session_negotiate->verify_result,
6649 f_vrfy, p_vrfy );
6650 }
6651 else
6652#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6653 {
6654 mbedtls_x509_crt *ca_chain;
6655 mbedtls_x509_crl *ca_crl;
6656
6657#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6658 if( ssl->handshake->sni_ca_chain != NULL )
6659 {
6660 ca_chain = ssl->handshake->sni_ca_chain;
6661 ca_crl = ssl->handshake->sni_ca_crl;
6662 }
6663 else
6664#endif
6665 {
6666 ca_chain = ssl->conf->ca_chain;
6667 ca_crl = ssl->conf->ca_crl;
6668 }
6669
6670 if( ca_chain != NULL )
6671 have_ca_chain = 1;
6672
6673 ret = mbedtls_x509_crt_verify_restartable(
6674 chain,
6675 ca_chain, ca_crl,
6676 ssl->conf->cert_profile,
6677 ssl->hostname,
6678 &ssl->session_negotiate->verify_result,
6679 f_vrfy, p_vrfy, rs_ctx );
6680 }
6681
6682 if( ret != 0 )
6683 {
6684 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6685 }
6686
6687#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6688 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6689 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6690#endif
6691
6692 /*
6693 * Secondary checks: always done, but change 'ret' only if it was 0
6694 */
6695
6696#if defined(MBEDTLS_ECP_C)
6697 {
6698 const mbedtls_pk_context *pk = &chain->pk;
6699
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006700 /* If certificate uses an EC key, make sure the curve is OK.
6701 * This is a public key, so it can't be opaque, so can_do() is a good
6702 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006703 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006704 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006705 /* and in the unlikely case the above assumption no longer holds
6706 * we are making sure that pk_ec() here does not return a NULL
6707 */
6708 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6709 if( ec == NULL )
6710 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6713 }
Jerry Yud9526692022-02-17 14:23:47 +08006714
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006715 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6716 {
6717 ssl->session_negotiate->verify_result |=
6718 MBEDTLS_X509_BADCERT_BAD_KEY;
6719
6720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6721 if( ret == 0 )
6722 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6723 }
Jerry Yud9526692022-02-17 14:23:47 +08006724 }
6725 }
6726#endif /* MBEDTLS_ECP_C */
6727
6728 if( mbedtls_ssl_check_cert_usage( chain,
6729 ciphersuite_info,
6730 ! ssl->conf->endpoint,
6731 &ssl->session_negotiate->verify_result ) != 0 )
6732 {
6733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6734 if( ret == 0 )
6735 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6736 }
6737
6738 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6739 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6740 * with details encoded in the verification flags. All other kinds
6741 * of error codes, including those from the user provided f_vrfy
6742 * functions, are treated as fatal and lead to a failure of
6743 * ssl_parse_certificate even if verification was optional. */
6744 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6745 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6746 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6747 {
6748 ret = 0;
6749 }
6750
6751 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6752 {
6753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6754 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6755 }
6756
6757 if( ret != 0 )
6758 {
6759 uint8_t alert;
6760
6761 /* The certificate may have been rejected for several reasons.
6762 Pick one and send the corresponding alert. Which alert to send
6763 may be a subject of debate in some cases. */
6764 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6765 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6766 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6767 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6768 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6769 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6770 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6771 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6772 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6773 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6774 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6775 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6776 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6777 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6778 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6779 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6780 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6781 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6782 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6783 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6784 else
6785 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6786 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6787 alert );
6788 }
6789
6790#if defined(MBEDTLS_DEBUG_C)
6791 if( ssl->session_negotiate->verify_result != 0 )
6792 {
6793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6794 (unsigned int) ssl->session_negotiate->verify_result ) );
6795 }
6796 else
6797 {
6798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6799 }
6800#endif /* MBEDTLS_DEBUG_C */
6801
6802 return( ret );
6803}
6804
6805#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006806MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006807static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6808 unsigned char *start, size_t len )
6809{
6810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6811 /* Remember digest of the peer's end-CRT. */
6812 ssl->session_negotiate->peer_cert_digest =
6813 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6814 if( ssl->session_negotiate->peer_cert_digest == NULL )
6815 {
6816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6817 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6818 mbedtls_ssl_send_alert_message( ssl,
6819 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6820 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6821
6822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6823 }
6824
6825 ret = mbedtls_md( mbedtls_md_info_from_type(
6826 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6827 start, len,
6828 ssl->session_negotiate->peer_cert_digest );
6829
6830 ssl->session_negotiate->peer_cert_digest_type =
6831 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6832 ssl->session_negotiate->peer_cert_digest_len =
6833 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6834
6835 return( ret );
6836}
6837
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006838MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006839static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6840 unsigned char *start, size_t len )
6841{
6842 unsigned char *end = start + len;
6843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6844
6845 /* Make a copy of the peer's raw public key. */
6846 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6847 ret = mbedtls_pk_parse_subpubkey( &start, end,
6848 &ssl->handshake->peer_pubkey );
6849 if( ret != 0 )
6850 {
6851 /* We should have parsed the public key before. */
6852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6853 }
6854
6855 return( 0 );
6856}
6857#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6858
6859int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6860{
6861 int ret = 0;
6862 int crt_expected;
6863#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6864 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6865 ? ssl->handshake->sni_authmode
6866 : ssl->conf->authmode;
6867#else
6868 const int authmode = ssl->conf->authmode;
6869#endif
6870 void *rs_ctx = NULL;
6871 mbedtls_x509_crt *chain = NULL;
6872
6873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6874
6875 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6876 if( crt_expected == SSL_CERTIFICATE_SKIP )
6877 {
6878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6879 goto exit;
6880 }
6881
6882#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6883 if( ssl->handshake->ecrs_enabled &&
6884 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6885 {
6886 chain = ssl->handshake->ecrs_peer_cert;
6887 ssl->handshake->ecrs_peer_cert = NULL;
6888 goto crt_verify;
6889 }
6890#endif
6891
6892 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6893 {
6894 /* mbedtls_ssl_read_record may have sent an alert already. We
6895 let it decide whether to alert. */
6896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6897 goto exit;
6898 }
6899
6900#if defined(MBEDTLS_SSL_SRV_C)
6901 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6902 {
6903 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6904
6905 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6906 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6907
6908 goto exit;
6909 }
6910#endif /* MBEDTLS_SSL_SRV_C */
6911
6912 /* Clear existing peer CRT structure in case we tried to
6913 * reuse a session but it failed, and allocate a new one. */
6914 ssl_clear_peer_cert( ssl->session_negotiate );
6915
6916 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6917 if( chain == NULL )
6918 {
6919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6920 sizeof( mbedtls_x509_crt ) ) );
6921 mbedtls_ssl_send_alert_message( ssl,
6922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6923 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6924
6925 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6926 goto exit;
6927 }
6928 mbedtls_x509_crt_init( chain );
6929
6930 ret = ssl_parse_certificate_chain( ssl, chain );
6931 if( ret != 0 )
6932 goto exit;
6933
6934#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6935 if( ssl->handshake->ecrs_enabled)
6936 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6937
6938crt_verify:
6939 if( ssl->handshake->ecrs_enabled)
6940 rs_ctx = &ssl->handshake->ecrs_ctx;
6941#endif
6942
6943 ret = ssl_parse_certificate_verify( ssl, authmode,
6944 chain, rs_ctx );
6945 if( ret != 0 )
6946 goto exit;
6947
6948#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6949 {
6950 unsigned char *crt_start, *pk_start;
6951 size_t crt_len, pk_len;
6952
6953 /* We parse the CRT chain without copying, so
6954 * these pointers point into the input buffer,
6955 * and are hence still valid after freeing the
6956 * CRT chain. */
6957
6958 crt_start = chain->raw.p;
6959 crt_len = chain->raw.len;
6960
6961 pk_start = chain->pk_raw.p;
6962 pk_len = chain->pk_raw.len;
6963
6964 /* Free the CRT structures before computing
6965 * digest and copying the peer's public key. */
6966 mbedtls_x509_crt_free( chain );
6967 mbedtls_free( chain );
6968 chain = NULL;
6969
6970 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6971 if( ret != 0 )
6972 goto exit;
6973
6974 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6975 if( ret != 0 )
6976 goto exit;
6977 }
6978#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6979 /* Pass ownership to session structure. */
6980 ssl->session_negotiate->peer_cert = chain;
6981 chain = NULL;
6982#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6983
6984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6985
6986exit:
6987
6988 if( ret == 0 )
6989 ssl->state++;
6990
6991#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6992 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6993 {
6994 ssl->handshake->ecrs_peer_cert = chain;
6995 chain = NULL;
6996 }
6997#endif
6998
6999 if( chain != NULL )
7000 {
7001 mbedtls_x509_crt_free( chain );
7002 mbedtls_free( chain );
7003 }
7004
7005 return( ret );
7006}
7007#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7008
Andrzej Kurek25f27152022-08-17 16:09:31 -04007009#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007010static void ssl_calc_finished_tls_sha256(
7011 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7012{
7013 int len = 12;
7014 const char *sender;
7015 unsigned char padbuf[32];
7016#if defined(MBEDTLS_USE_PSA_CRYPTO)
7017 size_t hash_size;
7018 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7019 psa_status_t status;
7020#else
7021 mbedtls_sha256_context sha256;
7022#endif
7023
7024 mbedtls_ssl_session *session = ssl->session_negotiate;
7025 if( !session )
7026 session = ssl->session;
7027
7028 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7029 ? "client finished"
7030 : "server finished";
7031
7032#if defined(MBEDTLS_USE_PSA_CRYPTO)
7033 sha256_psa = psa_hash_operation_init();
7034
7035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7036
7037 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7038 if( status != PSA_SUCCESS )
7039 {
7040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7041 return;
7042 }
7043
7044 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7045 if( status != PSA_SUCCESS )
7046 {
7047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7048 return;
7049 }
7050 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7051#else
7052
7053 mbedtls_sha256_init( &sha256 );
7054
7055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7056
7057 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7058
7059 /*
7060 * TLSv1.2:
7061 * hash = PRF( master, finished_label,
7062 * Hash( handshake ) )[0.11]
7063 */
7064
7065#if !defined(MBEDTLS_SHA256_ALT)
7066 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7067 sha256.state, sizeof( sha256.state ) );
7068#endif
7069
7070 mbedtls_sha256_finish( &sha256, padbuf );
7071 mbedtls_sha256_free( &sha256 );
7072#endif /* MBEDTLS_USE_PSA_CRYPTO */
7073
7074 ssl->handshake->tls_prf( session->master, 48, sender,
7075 padbuf, 32, buf, len );
7076
7077 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7078
7079 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7080
7081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7082}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007083#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007084
Jerry Yub7ba49e2022-02-17 14:25:53 +08007085
Andrzej Kurek25f27152022-08-17 16:09:31 -04007086#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007087static void ssl_calc_finished_tls_sha384(
7088 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7089{
7090 int len = 12;
7091 const char *sender;
7092 unsigned char padbuf[48];
7093#if defined(MBEDTLS_USE_PSA_CRYPTO)
7094 size_t hash_size;
7095 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7096 psa_status_t status;
7097#else
7098 mbedtls_sha512_context sha512;
7099#endif
7100
7101 mbedtls_ssl_session *session = ssl->session_negotiate;
7102 if( !session )
7103 session = ssl->session;
7104
7105 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7106 ? "client finished"
7107 : "server finished";
7108
7109#if defined(MBEDTLS_USE_PSA_CRYPTO)
7110 sha384_psa = psa_hash_operation_init();
7111
7112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7113
7114 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7115 if( status != PSA_SUCCESS )
7116 {
7117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7118 return;
7119 }
7120
7121 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7122 if( status != PSA_SUCCESS )
7123 {
7124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7125 return;
7126 }
7127 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7128#else
7129 mbedtls_sha512_init( &sha512 );
7130
7131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7132
Andrzej Kureka242e832022-08-11 10:03:14 -04007133 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007134
7135 /*
7136 * TLSv1.2:
7137 * hash = PRF( master, finished_label,
7138 * Hash( handshake ) )[0.11]
7139 */
7140
7141#if !defined(MBEDTLS_SHA512_ALT)
7142 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7143 sha512.state, sizeof( sha512.state ) );
7144#endif
7145 mbedtls_sha512_finish( &sha512, padbuf );
7146
7147 mbedtls_sha512_free( &sha512 );
7148#endif
7149
7150 ssl->handshake->tls_prf( session->master, 48, sender,
7151 padbuf, 48, buf, len );
7152
7153 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7154
7155 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7156
7157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7158}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007159#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007160
Jerry Yuaef00152022-02-17 14:27:31 +08007161void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7162{
7163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7164
7165 /*
7166 * Free our handshake params
7167 */
7168 mbedtls_ssl_handshake_free( ssl );
7169 mbedtls_free( ssl->handshake );
7170 ssl->handshake = NULL;
7171
7172 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007173 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007174 */
7175 if( ssl->transform )
7176 {
7177 mbedtls_ssl_transform_free( ssl->transform );
7178 mbedtls_free( ssl->transform );
7179 }
7180 ssl->transform = ssl->transform_negotiate;
7181 ssl->transform_negotiate = NULL;
7182
7183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7184}
7185
Jerry Yu2a9fff52022-02-17 14:28:51 +08007186void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7187{
7188 int resume = ssl->handshake->resume;
7189
7190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7191
7192#if defined(MBEDTLS_SSL_RENEGOTIATION)
7193 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7194 {
7195 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7196 ssl->renego_records_seen = 0;
7197 }
7198#endif
7199
7200 /*
7201 * Free the previous session and switch in the current one
7202 */
7203 if( ssl->session )
7204 {
7205#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7206 /* RFC 7366 3.1: keep the EtM state */
7207 ssl->session_negotiate->encrypt_then_mac =
7208 ssl->session->encrypt_then_mac;
7209#endif
7210
7211 mbedtls_ssl_session_free( ssl->session );
7212 mbedtls_free( ssl->session );
7213 }
7214 ssl->session = ssl->session_negotiate;
7215 ssl->session_negotiate = NULL;
7216
7217 /*
7218 * Add cache entry
7219 */
7220 if( ssl->conf->f_set_cache != NULL &&
7221 ssl->session->id_len != 0 &&
7222 resume == 0 )
7223 {
7224 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7225 ssl->session->id,
7226 ssl->session->id_len,
7227 ssl->session ) != 0 )
7228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7229 }
7230
7231#if defined(MBEDTLS_SSL_PROTO_DTLS)
7232 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7233 ssl->handshake->flight != NULL )
7234 {
7235 /* Cancel handshake timer */
7236 mbedtls_ssl_set_timer( ssl, 0 );
7237
7238 /* Keep last flight around in case we need to resend it:
7239 * we need the handshake and transform structures for that */
7240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7241 }
7242 else
7243#endif
7244 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7245
7246 ssl->state++;
7247
7248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7249}
7250
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007251int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7252{
7253 int ret, hash_len;
7254
7255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7256
7257 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7258
7259 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7260
7261 /*
7262 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7263 * may define some other value. Currently (early 2016), no defined
7264 * ciphersuite does this (and this is unlikely to change as activity has
7265 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7266 */
7267 hash_len = 12;
7268
7269#if defined(MBEDTLS_SSL_RENEGOTIATION)
7270 ssl->verify_data_len = hash_len;
7271 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7272#endif
7273
7274 ssl->out_msglen = 4 + hash_len;
7275 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7276 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7277
7278 /*
7279 * In case of session resuming, invert the client and server
7280 * ChangeCipherSpec messages order.
7281 */
7282 if( ssl->handshake->resume != 0 )
7283 {
7284#if defined(MBEDTLS_SSL_CLI_C)
7285 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7286 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7287#endif
7288#if defined(MBEDTLS_SSL_SRV_C)
7289 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7290 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7291#endif
7292 }
7293 else
7294 ssl->state++;
7295
7296 /*
7297 * Switch to our negotiated transform and session parameters for outbound
7298 * data.
7299 */
7300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7301
7302#if defined(MBEDTLS_SSL_PROTO_DTLS)
7303 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7304 {
7305 unsigned char i;
7306
7307 /* Remember current epoch settings for resending */
7308 ssl->handshake->alt_transform_out = ssl->transform_out;
7309 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7310 sizeof( ssl->handshake->alt_out_ctr ) );
7311
7312 /* Set sequence_number to zero */
7313 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7314
7315
7316 /* Increment epoch */
7317 for( i = 2; i > 0; i-- )
7318 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7319 break;
7320
7321 /* The loop goes to its end iff the counter is wrapping */
7322 if( i == 0 )
7323 {
7324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7325 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7326 }
7327 }
7328 else
7329#endif /* MBEDTLS_SSL_PROTO_DTLS */
7330 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7331
7332 ssl->transform_out = ssl->transform_negotiate;
7333 ssl->session_out = ssl->session_negotiate;
7334
7335#if defined(MBEDTLS_SSL_PROTO_DTLS)
7336 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7337 mbedtls_ssl_send_flight_completed( ssl );
7338#endif
7339
7340 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7341 {
7342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7343 return( ret );
7344 }
7345
7346#if defined(MBEDTLS_SSL_PROTO_DTLS)
7347 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7348 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7349 {
7350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7351 return( ret );
7352 }
7353#endif
7354
7355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7356
7357 return( 0 );
7358}
7359
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007360#define SSL_MAX_HASH_LEN 12
7361
7362int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7363{
7364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7365 unsigned int hash_len = 12;
7366 unsigned char buf[SSL_MAX_HASH_LEN];
7367
7368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7369
7370 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7371
7372 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7373 {
7374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7375 goto exit;
7376 }
7377
7378 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7379 {
7380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7381 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7382 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7383 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7384 goto exit;
7385 }
7386
7387 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7388 {
7389 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7390 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7391 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7392 goto exit;
7393 }
7394
7395 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
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_DECODE_ERROR );
7400 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7401 goto exit;
7402 }
7403
7404 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7405 buf, hash_len ) != 0 )
7406 {
7407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7408 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7409 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7410 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7411 goto exit;
7412 }
7413
7414#if defined(MBEDTLS_SSL_RENEGOTIATION)
7415 ssl->verify_data_len = hash_len;
7416 memcpy( ssl->peer_verify_data, buf, hash_len );
7417#endif
7418
7419 if( ssl->handshake->resume != 0 )
7420 {
7421#if defined(MBEDTLS_SSL_CLI_C)
7422 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7423 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7424#endif
7425#if defined(MBEDTLS_SSL_SRV_C)
7426 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7427 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7428#endif
7429 }
7430 else
7431 ssl->state++;
7432
7433#if defined(MBEDTLS_SSL_PROTO_DTLS)
7434 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7435 mbedtls_ssl_recv_flight_completed( ssl );
7436#endif
7437
7438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7439
7440exit:
7441 mbedtls_platform_zeroize( buf, hash_len );
7442 return( ret );
7443}
7444
Jerry Yu392112c2022-02-17 14:34:10 +08007445#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7446/*
7447 * Helper to get TLS 1.2 PRF from ciphersuite
7448 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7449 */
7450static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7451{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007452#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007453 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7454 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7455
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007456 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007457 return( tls_prf_sha384 );
7458#else
7459 (void) ciphersuite_id;
7460#endif
7461 return( tls_prf_sha256 );
7462}
7463#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007464
7465static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7466{
7467 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007469 if( tls_prf == tls_prf_sha384 )
7470 {
7471 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7472 }
7473 else
7474#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007475#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007476 if( tls_prf == tls_prf_sha256 )
7477 {
7478 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7479 }
7480 else
7481#endif
7482 return( MBEDTLS_SSL_TLS_PRF_NONE );
7483}
7484
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007485/*
7486 * Populate a transform structure with session keys and all the other
7487 * necessary information.
7488 *
7489 * Parameters:
7490 * - [in/out]: transform: structure to populate
7491 * [in] must be just initialised with mbedtls_ssl_transform_init()
7492 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7493 * - [in] ciphersuite
7494 * - [in] master
7495 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007496 * - [in] tls_prf: pointer to PRF to use for key derivation
7497 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007498 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007499 * - [in] endpoint: client or server
7500 * - [in] ssl: used for:
7501 * - ssl->conf->{f,p}_export_keys
7502 * [in] optionally used for:
7503 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7504 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007505MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007506static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7507 int ciphersuite,
7508 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007509#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007510 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007511#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007512 ssl_tls_prf_t tls_prf,
7513 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007514 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007515 unsigned endpoint,
7516 const mbedtls_ssl_context *ssl )
7517{
7518 int ret = 0;
7519 unsigned char keyblk[256];
7520 unsigned char *key1;
7521 unsigned char *key2;
7522 unsigned char *mac_enc;
7523 unsigned char *mac_dec;
7524 size_t mac_key_len = 0;
7525 size_t iv_copy_len;
7526 size_t keylen;
7527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007528 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007529#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007530 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007531 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007532#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007533
7534#if defined(MBEDTLS_USE_PSA_CRYPTO)
7535 psa_key_type_t key_type;
7536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7537 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007538 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007539 size_t key_bits;
7540 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7541#endif
7542
7543#if !defined(MBEDTLS_DEBUG_C) && \
7544 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7545 if( ssl->f_export_keys == NULL )
7546 {
7547 ssl = NULL; /* make sure we don't use it except for these cases */
7548 (void) ssl;
7549 }
7550#endif
7551
7552 /*
7553 * Some data just needs copying into the structure
7554 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007555#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007556 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007557#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007558 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007559
7560#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7561 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7562#endif
7563
7564#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007565 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007566 {
7567 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7568 * generation separate. This should never happen. */
7569 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7570 }
7571#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7572
7573 /*
7574 * Get various info structures
7575 */
7576 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7577 if( ciphersuite_info == NULL )
7578 {
7579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7580 ciphersuite ) );
7581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7582 }
7583
Neil Armstrongab555e02022-04-04 11:07:59 +02007584 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007585#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007586 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007587#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007588 ciphersuite_info );
7589
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007590 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7591 transform->taglen =
7592 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7593
7594#if defined(MBEDTLS_USE_PSA_CRYPTO)
7595 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7596 transform->taglen,
7597 &alg,
7598 &key_type,
7599 &key_bits ) ) != PSA_SUCCESS )
7600 {
7601 ret = psa_ssl_status_to_mbedtls( status );
7602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7603 goto end;
7604 }
7605#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007606 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7607 if( cipher_info == NULL )
7608 {
7609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7610 ciphersuite_info->cipher ) );
7611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7612 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007613#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007614
Neil Armstronge4512952022-03-08 09:08:22 +01007615#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007616 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007617 if( mac_alg == 0 )
7618 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007620 (unsigned) ciphersuite_info->mac ) );
7621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7622 }
7623#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007624 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7625 if( md_info == NULL )
7626 {
7627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7628 (unsigned) ciphersuite_info->mac ) );
7629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7630 }
Neil Armstronge4512952022-03-08 09:08:22 +01007631#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007632
7633#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7634 /* Copy own and peer's CID if the use of the CID
7635 * extension has been negotiated. */
7636 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7637 {
7638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7639
7640 transform->in_cid_len = ssl->own_cid_len;
7641 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7642 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7643 transform->in_cid_len );
7644
7645 transform->out_cid_len = ssl->handshake->peer_cid_len;
7646 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7647 ssl->handshake->peer_cid_len );
7648 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7649 transform->out_cid_len );
7650 }
7651#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7652
7653 /*
7654 * Compute key block using the PRF
7655 */
7656 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7657 if( ret != 0 )
7658 {
7659 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7660 return( ret );
7661 }
7662
7663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7664 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7665 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7666 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7667 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7668
7669 /*
7670 * Determine the appropriate key, IV and MAC length.
7671 */
7672
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007673#if defined(MBEDTLS_USE_PSA_CRYPTO)
7674 keylen = PSA_BITS_TO_BYTES(key_bits);
7675#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007676 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007677#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007678
7679#if defined(MBEDTLS_GCM_C) || \
7680 defined(MBEDTLS_CCM_C) || \
7681 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007682 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007683 {
7684 size_t explicit_ivlen;
7685
7686 transform->maclen = 0;
7687 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007688
7689 /* All modes haves 96-bit IVs, but the length of the static parts vary
7690 * with mode and version:
7691 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7692 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7693 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7694 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7695 * sequence number).
7696 */
7697 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007698#if defined(MBEDTLS_USE_PSA_CRYPTO)
7699 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7700#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007701 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007702#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007703 transform->fixed_ivlen = 12;
7704 else
7705 transform->fixed_ivlen = 4;
7706
7707 /* Minimum length of encrypted record */
7708 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7709 transform->minlen = explicit_ivlen + transform->taglen;
7710 }
7711 else
7712#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7713#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007714 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7715 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7716 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007717 {
Neil Armstronge4512952022-03-08 09:08:22 +01007718#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007719 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007720#else
7721 size_t block_size = cipher_info->block_size;
7722#endif /* MBEDTLS_USE_PSA_CRYPTO */
7723
7724#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007725 /* Get MAC length */
7726 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7727#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007728 /* Initialize HMAC contexts */
7729 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7730 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7731 {
7732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7733 goto end;
7734 }
7735
7736 /* Get MAC length */
7737 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007738#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007739 transform->maclen = mac_key_len;
7740
7741 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007742#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007743 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007744#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007745 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007746#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007747
7748 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007749 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007750 transform->minlen = transform->maclen;
7751 else
7752 {
7753 /*
7754 * GenericBlockCipher:
7755 * 1. if EtM is in use: one block plus MAC
7756 * otherwise: * first multiple of blocklen greater than maclen
7757 * 2. IV
7758 */
7759#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007760 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007761 {
7762 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007763 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007764 }
7765 else
7766#endif
7767 {
7768 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007769 + block_size
7770 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007771 }
7772
Glenn Strauss07c64162022-03-14 12:34:51 -04007773 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007774 {
7775 transform->minlen += transform->ivlen;
7776 }
7777 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007778 {
7779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7780 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7781 goto end;
7782 }
7783 }
7784 }
7785 else
7786#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7787 {
7788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7789 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7790 }
7791
7792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7793 (unsigned) keylen,
7794 (unsigned) transform->minlen,
7795 (unsigned) transform->ivlen,
7796 (unsigned) transform->maclen ) );
7797
7798 /*
7799 * Finally setup the cipher contexts, IVs and MAC secrets.
7800 */
7801#if defined(MBEDTLS_SSL_CLI_C)
7802 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7803 {
7804 key1 = keyblk + mac_key_len * 2;
7805 key2 = keyblk + mac_key_len * 2 + keylen;
7806
7807 mac_enc = keyblk;
7808 mac_dec = keyblk + mac_key_len;
7809
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007810 iv_copy_len = ( transform->fixed_ivlen ) ?
7811 transform->fixed_ivlen : transform->ivlen;
7812 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7813 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7814 iv_copy_len );
7815 }
7816 else
7817#endif /* MBEDTLS_SSL_CLI_C */
7818#if defined(MBEDTLS_SSL_SRV_C)
7819 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7820 {
7821 key1 = keyblk + mac_key_len * 2 + keylen;
7822 key2 = keyblk + mac_key_len * 2;
7823
7824 mac_enc = keyblk + mac_key_len;
7825 mac_dec = keyblk;
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_dec, key1 + keylen, iv_copy_len );
7830 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7831 iv_copy_len );
7832 }
7833 else
7834#endif /* MBEDTLS_SSL_SRV_C */
7835 {
7836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7837 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7838 goto end;
7839 }
7840
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007841 if( ssl != NULL && ssl->f_export_keys != NULL )
7842 {
7843 ssl->f_export_keys( ssl->p_export_keys,
7844 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7845 master, 48,
7846 randbytes + 32,
7847 randbytes,
7848 tls_prf_get_type( tls_prf ) );
7849 }
7850
7851#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007852 transform->psa_alg = alg;
7853
7854 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7855 {
7856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7857 psa_set_key_algorithm( &attributes, alg );
7858 psa_set_key_type( &attributes, key_type );
7859
7860 if( ( status = psa_import_key( &attributes,
7861 key1,
7862 PSA_BITS_TO_BYTES( key_bits ),
7863 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7864 {
7865 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7866 ret = psa_ssl_status_to_mbedtls( status );
7867 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7868 goto end;
7869 }
7870
7871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7872
7873 if( ( status = psa_import_key( &attributes,
7874 key2,
7875 PSA_BITS_TO_BYTES( key_bits ),
7876 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7877 {
7878 ret = psa_ssl_status_to_mbedtls( status );
7879 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7880 goto end;
7881 }
7882 }
7883#else
7884 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7885 cipher_info ) ) != 0 )
7886 {
7887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7888 goto end;
7889 }
7890
7891 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7892 cipher_info ) ) != 0 )
7893 {
7894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7895 goto end;
7896 }
7897
7898 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7899 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7900 MBEDTLS_ENCRYPT ) ) != 0 )
7901 {
7902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7903 goto end;
7904 }
7905
7906 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7907 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7908 MBEDTLS_DECRYPT ) ) != 0 )
7909 {
7910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7911 goto end;
7912 }
7913
7914#if defined(MBEDTLS_CIPHER_MODE_CBC)
7915 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7916 {
7917 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7918 MBEDTLS_PADDING_NONE ) ) != 0 )
7919 {
7920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7921 goto end;
7922 }
7923
7924 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7925 MBEDTLS_PADDING_NONE ) ) != 0 )
7926 {
7927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7928 goto end;
7929 }
7930 }
7931#endif /* MBEDTLS_CIPHER_MODE_CBC */
7932#endif /* MBEDTLS_USE_PSA_CRYPTO */
7933
Neil Armstrong29c0c042022-03-17 17:47:28 +01007934#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7935 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7936 For AEAD-based ciphersuites, there is nothing to do here. */
7937 if( mac_key_len != 0 )
7938 {
7939#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007940 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007941
7942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007943 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007944 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7945
7946 if( ( status = psa_import_key( &attributes,
7947 mac_enc, mac_key_len,
7948 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7949 {
7950 ret = psa_ssl_status_to_mbedtls( status );
7951 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7952 goto end;
7953 }
7954
Ronald Cronfb39f152022-03-25 14:36:28 +01007955 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007956 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7957#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7958 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7959#endif
7960 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007961 /* mbedtls_ct_hmac() requires the key to be exportable */
7962 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7963 PSA_KEY_USAGE_VERIFY_HASH );
7964 else
7965 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7966
7967 if( ( status = psa_import_key( &attributes,
7968 mac_dec, mac_key_len,
7969 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7970 {
7971 ret = psa_ssl_status_to_mbedtls( status );
7972 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7973 goto end;
7974 }
7975#else
7976 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7977 if( ret != 0 )
7978 goto end;
7979 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7980 if( ret != 0 )
7981 goto end;
7982#endif /* MBEDTLS_USE_PSA_CRYPTO */
7983 }
7984#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7985
7986 ((void) mac_dec);
7987 ((void) mac_enc);
7988
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007989end:
7990 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7991 return( ret );
7992}
7993
Jerry Yuee40f9d2022-02-17 14:55:16 +08007994#if defined(MBEDTLS_USE_PSA_CRYPTO)
7995int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7996 unsigned char *hash, size_t *hashlen,
7997 unsigned char *data, size_t data_len,
7998 mbedtls_md_type_t md_alg )
7999{
8000 psa_status_t status;
8001 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008002 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008003
8004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8005
8006 if( ( status = psa_hash_setup( &hash_operation,
8007 hash_alg ) ) != PSA_SUCCESS )
8008 {
8009 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8010 goto exit;
8011 }
8012
8013 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8014 64 ) ) != PSA_SUCCESS )
8015 {
8016 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8017 goto exit;
8018 }
8019
8020 if( ( status = psa_hash_update( &hash_operation,
8021 data, data_len ) ) != PSA_SUCCESS )
8022 {
8023 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8024 goto exit;
8025 }
8026
8027 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8028 hashlen ) ) != PSA_SUCCESS )
8029 {
8030 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8031 goto exit;
8032 }
8033
8034exit:
8035 if( status != PSA_SUCCESS )
8036 {
8037 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8038 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8039 switch( status )
8040 {
8041 case PSA_ERROR_NOT_SUPPORTED:
8042 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8043 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8044 case PSA_ERROR_BUFFER_TOO_SMALL:
8045 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8046 case PSA_ERROR_INSUFFICIENT_MEMORY:
8047 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8048 default:
8049 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8050 }
8051 }
8052 return( 0 );
8053}
8054
8055#else
8056
8057int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8058 unsigned char *hash, size_t *hashlen,
8059 unsigned char *data, size_t data_len,
8060 mbedtls_md_type_t md_alg )
8061{
8062 int ret = 0;
8063 mbedtls_md_context_t ctx;
8064 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8065 *hashlen = mbedtls_md_get_size( md_info );
8066
8067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8068
8069 mbedtls_md_init( &ctx );
8070
8071 /*
8072 * digitally-signed struct {
8073 * opaque client_random[32];
8074 * opaque server_random[32];
8075 * ServerDHParams params;
8076 * };
8077 */
8078 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8079 {
8080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8081 goto exit;
8082 }
8083 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8084 {
8085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8086 goto exit;
8087 }
8088 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8089 {
8090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8091 goto exit;
8092 }
8093 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8094 {
8095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8096 goto exit;
8097 }
8098 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8099 {
8100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8101 goto exit;
8102 }
8103
8104exit:
8105 mbedtls_md_free( &ctx );
8106
8107 if( ret != 0 )
8108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8109 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8110
8111 return( ret );
8112}
8113#endif /* MBEDTLS_USE_PSA_CRYPTO */
8114
Jerry Yud9d91da2022-02-17 14:57:06 +08008115#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8116
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008117/* Find the preferred hash for a given signature algorithm. */
8118unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8119 mbedtls_ssl_context *ssl,
8120 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008121{
Gabor Mezei078e8032022-04-27 21:17:56 +02008122 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008123 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008124
8125 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008126 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008127
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008128 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008129 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008130 unsigned int hash_alg_received =
8131 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8132 received_sig_algs[i] );
8133 unsigned int sig_alg_received =
8134 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8135 received_sig_algs[i] );
8136
8137 if( sig_alg == sig_alg_received )
8138 {
8139#if defined(MBEDTLS_USE_PSA_CRYPTO)
8140 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8141 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008142 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008143 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008144
Neil Armstrong96eceb82022-06-30 18:05:05 +02008145 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8146 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8147 PSA_ALG_ECDSA( psa_hash_alg ),
8148 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008149 continue;
8150
Neil Armstrong96eceb82022-06-30 18:05:05 +02008151 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008152 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8153 PSA_ALG_RSA_PKCS1V15_SIGN(
8154 psa_hash_alg ),
8155 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008156 continue;
8157 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008158#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008159
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008160 return( hash_alg_received );
8161 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008162 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008163
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008164 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008165}
8166
8167#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8168
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008169/* Serialization of TLS 1.2 sessions:
8170 *
8171 * struct {
8172 * uint64 start_time;
8173 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008174 * uint8 session_id_len; // at most 32
8175 * opaque session_id[32];
8176 * opaque master[48]; // fixed length in the standard
8177 * uint32 verify_result;
8178 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8179 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8180 * uint32 ticket_lifetime;
8181 * uint8 mfl_code; // up to 255 according to standard
8182 * uint8 encrypt_then_mac; // 0 or 1
8183 * } serialized_session_tls12;
8184 *
8185 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008186static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008187 unsigned char *buf,
8188 size_t buf_len )
8189{
8190 unsigned char *p = buf;
8191 size_t used = 0;
8192
8193#if defined(MBEDTLS_HAVE_TIME)
8194 uint64_t start;
8195#endif
8196#if defined(MBEDTLS_X509_CRT_PARSE_C)
8197#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8198 size_t cert_len;
8199#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8200#endif /* MBEDTLS_X509_CRT_PARSE_C */
8201
8202 /*
8203 * Time
8204 */
8205#if defined(MBEDTLS_HAVE_TIME)
8206 used += 8;
8207
8208 if( used <= buf_len )
8209 {
8210 start = (uint64_t) session->start;
8211
8212 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8213 p += 8;
8214 }
8215#endif /* MBEDTLS_HAVE_TIME */
8216
8217 /*
8218 * Basic mandatory fields
8219 */
8220 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008221 + 1 /* id_len */
8222 + sizeof( session->id )
8223 + sizeof( session->master )
8224 + 4; /* verify_result */
8225
8226 if( used <= buf_len )
8227 {
8228 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8229 p += 2;
8230
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008231 *p++ = MBEDTLS_BYTE_0( session->id_len );
8232 memcpy( p, session->id, 32 );
8233 p += 32;
8234
8235 memcpy( p, session->master, 48 );
8236 p += 48;
8237
8238 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8239 p += 4;
8240 }
8241
8242 /*
8243 * Peer's end-entity certificate
8244 */
8245#if defined(MBEDTLS_X509_CRT_PARSE_C)
8246#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8247 if( session->peer_cert == NULL )
8248 cert_len = 0;
8249 else
8250 cert_len = session->peer_cert->raw.len;
8251
8252 used += 3 + cert_len;
8253
8254 if( used <= buf_len )
8255 {
8256 *p++ = MBEDTLS_BYTE_2( cert_len );
8257 *p++ = MBEDTLS_BYTE_1( cert_len );
8258 *p++ = MBEDTLS_BYTE_0( cert_len );
8259
8260 if( session->peer_cert != NULL )
8261 {
8262 memcpy( p, session->peer_cert->raw.p, cert_len );
8263 p += cert_len;
8264 }
8265 }
8266#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8267 if( session->peer_cert_digest != NULL )
8268 {
8269 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8270 if( used <= buf_len )
8271 {
8272 *p++ = (unsigned char) session->peer_cert_digest_type;
8273 *p++ = (unsigned char) session->peer_cert_digest_len;
8274 memcpy( p, session->peer_cert_digest,
8275 session->peer_cert_digest_len );
8276 p += session->peer_cert_digest_len;
8277 }
8278 }
8279 else
8280 {
8281 used += 2;
8282 if( used <= buf_len )
8283 {
8284 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8285 *p++ = 0;
8286 }
8287 }
8288#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8289#endif /* MBEDTLS_X509_CRT_PARSE_C */
8290
8291 /*
8292 * Session ticket if any, plus associated data
8293 */
8294#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8295 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8296
8297 if( used <= buf_len )
8298 {
8299 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8300 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8301 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8302
8303 if( session->ticket != NULL )
8304 {
8305 memcpy( p, session->ticket, session->ticket_len );
8306 p += session->ticket_len;
8307 }
8308
8309 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8310 p += 4;
8311 }
8312#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8313
8314 /*
8315 * Misc extension-related info
8316 */
8317#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8318 used += 1;
8319
8320 if( used <= buf_len )
8321 *p++ = session->mfl_code;
8322#endif
8323
8324#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8325 used += 1;
8326
8327 if( used <= buf_len )
8328 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8329#endif
8330
8331 return( used );
8332}
8333
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008334MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008335static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008336 const unsigned char *buf,
8337 size_t len )
8338{
8339#if defined(MBEDTLS_HAVE_TIME)
8340 uint64_t start;
8341#endif
8342#if defined(MBEDTLS_X509_CRT_PARSE_C)
8343#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8344 size_t cert_len;
8345#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8346#endif /* MBEDTLS_X509_CRT_PARSE_C */
8347
8348 const unsigned char *p = buf;
8349 const unsigned char * const end = buf + len;
8350
8351 /*
8352 * Time
8353 */
8354#if defined(MBEDTLS_HAVE_TIME)
8355 if( 8 > (size_t)( end - p ) )
8356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8357
8358 start = ( (uint64_t) p[0] << 56 ) |
8359 ( (uint64_t) p[1] << 48 ) |
8360 ( (uint64_t) p[2] << 40 ) |
8361 ( (uint64_t) p[3] << 32 ) |
8362 ( (uint64_t) p[4] << 24 ) |
8363 ( (uint64_t) p[5] << 16 ) |
8364 ( (uint64_t) p[6] << 8 ) |
8365 ( (uint64_t) p[7] );
8366 p += 8;
8367
8368 session->start = (time_t) start;
8369#endif /* MBEDTLS_HAVE_TIME */
8370
8371 /*
8372 * Basic mandatory fields
8373 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008374 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8376
8377 session->ciphersuite = ( p[0] << 8 ) | p[1];
8378 p += 2;
8379
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008380 session->id_len = *p++;
8381 memcpy( session->id, p, 32 );
8382 p += 32;
8383
8384 memcpy( session->master, p, 48 );
8385 p += 48;
8386
8387 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8388 ( (uint32_t) p[1] << 16 ) |
8389 ( (uint32_t) p[2] << 8 ) |
8390 ( (uint32_t) p[3] );
8391 p += 4;
8392
8393 /* Immediately clear invalid pointer values that have been read, in case
8394 * we exit early before we replaced them with valid ones. */
8395#if defined(MBEDTLS_X509_CRT_PARSE_C)
8396#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8397 session->peer_cert = NULL;
8398#else
8399 session->peer_cert_digest = NULL;
8400#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8401#endif /* MBEDTLS_X509_CRT_PARSE_C */
8402#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8403 session->ticket = NULL;
8404#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8405
8406 /*
8407 * Peer certificate
8408 */
8409#if defined(MBEDTLS_X509_CRT_PARSE_C)
8410#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8411 /* Deserialize CRT from the end of the ticket. */
8412 if( 3 > (size_t)( end - p ) )
8413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8414
8415 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8416 p += 3;
8417
8418 if( cert_len != 0 )
8419 {
8420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8421
8422 if( cert_len > (size_t)( end - p ) )
8423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8424
8425 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8426
8427 if( session->peer_cert == NULL )
8428 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8429
8430 mbedtls_x509_crt_init( session->peer_cert );
8431
8432 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8433 p, cert_len ) ) != 0 )
8434 {
8435 mbedtls_x509_crt_free( session->peer_cert );
8436 mbedtls_free( session->peer_cert );
8437 session->peer_cert = NULL;
8438 return( ret );
8439 }
8440
8441 p += cert_len;
8442 }
8443#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8444 /* Deserialize CRT digest from the end of the ticket. */
8445 if( 2 > (size_t)( end - p ) )
8446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8447
8448 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8449 session->peer_cert_digest_len = (size_t) *p++;
8450
8451 if( session->peer_cert_digest_len != 0 )
8452 {
8453 const mbedtls_md_info_t *md_info =
8454 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8455 if( md_info == NULL )
8456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8457 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8459
8460 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8462
8463 session->peer_cert_digest =
8464 mbedtls_calloc( 1, session->peer_cert_digest_len );
8465 if( session->peer_cert_digest == NULL )
8466 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8467
8468 memcpy( session->peer_cert_digest, p,
8469 session->peer_cert_digest_len );
8470 p += session->peer_cert_digest_len;
8471 }
8472#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8473#endif /* MBEDTLS_X509_CRT_PARSE_C */
8474
8475 /*
8476 * Session ticket and associated data
8477 */
8478#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8479 if( 3 > (size_t)( end - p ) )
8480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8481
8482 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8483 p += 3;
8484
8485 if( session->ticket_len != 0 )
8486 {
8487 if( session->ticket_len > (size_t)( end - p ) )
8488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8489
8490 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8491 if( session->ticket == NULL )
8492 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8493
8494 memcpy( session->ticket, p, session->ticket_len );
8495 p += session->ticket_len;
8496 }
8497
8498 if( 4 > (size_t)( end - p ) )
8499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8500
8501 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8502 ( (uint32_t) p[1] << 16 ) |
8503 ( (uint32_t) p[2] << 8 ) |
8504 ( (uint32_t) p[3] );
8505 p += 4;
8506#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8507
8508 /*
8509 * Misc extension-related info
8510 */
8511#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8512 if( 1 > (size_t)( end - p ) )
8513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8514
8515 session->mfl_code = *p++;
8516#endif
8517
8518#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8519 if( 1 > (size_t)( end - p ) )
8520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8521
8522 session->encrypt_then_mac = *p++;
8523#endif
8524
8525 /* Done, should have consumed entire buffer */
8526 if( p != end )
8527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8528
8529 return( 0 );
8530}
Jerry Yudc7bd172022-02-17 13:44:15 +08008531#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8532
XiaokangQian75d40ef2022-04-20 11:05:24 +00008533int mbedtls_ssl_validate_ciphersuite(
8534 const mbedtls_ssl_context *ssl,
8535 const mbedtls_ssl_ciphersuite_t *suite_info,
8536 mbedtls_ssl_protocol_version min_tls_version,
8537 mbedtls_ssl_protocol_version max_tls_version )
8538{
8539 (void) ssl;
8540
8541 if( suite_info == NULL )
8542 return( -1 );
8543
8544 if( ( suite_info->min_tls_version > max_tls_version ) ||
8545 ( suite_info->max_tls_version < min_tls_version ) )
8546 {
8547 return( -1 );
8548 }
8549
XiaokangQian060d8672022-04-21 09:24:56 +00008550#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008551#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8552 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8553 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8554 {
8555 return( -1 );
8556 }
8557#endif
8558
8559 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8560#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8561 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8562 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8563 {
8564 return( -1 );
8565 }
8566#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8567#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8568
8569 return( 0 );
8570}
8571
XiaokangQianeaf36512022-04-24 09:07:44 +00008572#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8573/*
8574 * Function for writing a signature algorithm extension.
8575 *
8576 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8577 * value (TLS 1.3 RFC8446):
8578 * enum {
8579 * ....
8580 * ecdsa_secp256r1_sha256( 0x0403 ),
8581 * ecdsa_secp384r1_sha384( 0x0503 ),
8582 * ecdsa_secp521r1_sha512( 0x0603 ),
8583 * ....
8584 * } SignatureScheme;
8585 *
8586 * struct {
8587 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8588 * } SignatureSchemeList;
8589 *
8590 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8591 * value (TLS 1.2 RFC5246):
8592 * enum {
8593 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8594 * sha512(6), (255)
8595 * } HashAlgorithm;
8596 *
8597 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8598 * SignatureAlgorithm;
8599 *
8600 * struct {
8601 * HashAlgorithm hash;
8602 * SignatureAlgorithm signature;
8603 * } SignatureAndHashAlgorithm;
8604 *
8605 * SignatureAndHashAlgorithm
8606 * supported_signature_algorithms<2..2^16-2>;
8607 *
8608 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8609 * generalization of the TLS 1.2 signature algorithm extension.
8610 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8611 * `SignatureScheme` field of TLS 1.3
8612 *
8613 */
8614int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8615 const unsigned char *end, size_t *out_len )
8616{
8617 unsigned char *p = buf;
8618 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8619 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8620
8621 *out_len = 0;
8622
8623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8624
8625 /* Check if we have space for header and length field:
8626 * - extension_type (2 bytes)
8627 * - extension_data_length (2 bytes)
8628 * - supported_signature_algorithms_length (2 bytes)
8629 */
8630 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8631 p += 6;
8632
8633 /*
8634 * Write supported_signature_algorithms
8635 */
8636 supported_sig_alg = p;
8637 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8638 if( sig_alg == NULL )
8639 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8640
8641 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8642 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8644 *sig_alg,
8645 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008646 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8647 continue;
8648 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8649 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8650 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008652 *sig_alg,
8653 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008654 }
8655
8656 /* Length of supported_signature_algorithms */
8657 supported_sig_alg_len = p - supported_sig_alg;
8658 if( supported_sig_alg_len == 0 )
8659 {
8660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8662 }
8663
XiaokangQianeaf36512022-04-24 09:07:44 +00008664 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008665 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008666 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8667
XiaokangQianeaf36512022-04-24 09:07:44 +00008668 *out_len = p - buf;
8669
8670#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8671 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8672#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8673 return( 0 );
8674}
8675#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8676
XiaokangQian40a35232022-05-07 09:02:40 +00008677#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008678/*
8679 * mbedtls_ssl_parse_server_name_ext
8680 *
8681 * Structure of server_name extension:
8682 *
8683 * enum {
8684 * host_name(0), (255)
8685 * } NameType;
8686 * opaque HostName<1..2^16-1>;
8687 *
8688 * struct {
8689 * NameType name_type;
8690 * select (name_type) {
8691 * case host_name: HostName;
8692 * } name;
8693 * } ServerName;
8694 * struct {
8695 * ServerName server_name_list<1..2^16-1>
8696 * } ServerNameList;
8697 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008698MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008699int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8700 const unsigned char *buf,
8701 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008702{
8703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8704 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008705 size_t server_name_list_len, hostname_len;
8706 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008707
XiaokangQianf2a94202022-05-20 06:44:24 +00008708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008709
8710 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008711 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008712 p += 2;
8713
XiaokangQian9b2b7712022-05-17 02:57:00 +00008714 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8715 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008716 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008717 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008718 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008719 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008720 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8721 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008722
8723 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8724 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008725 /* sni_name is intended to be used only during the parsing of the
8726 * ClientHello message (it is reset to NULL before the end of
8727 * the message parsing). Thus it is ok to just point to the
8728 * reception buffer and not make a copy of it.
8729 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008730 ssl->handshake->sni_name = p + 3;
8731 ssl->handshake->sni_name_len = hostname_len;
8732 if( ssl->conf->f_sni == NULL )
8733 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008734 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008735 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008736 if( ret != 0 )
8737 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008739 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8740 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008741 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8742 }
8743 return( 0 );
8744 }
8745
8746 p += hostname_len + 3;
8747 }
8748
8749 return( 0 );
8750}
8751#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8752
XiaokangQianacb39922022-06-17 10:18:48 +00008753#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008754MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008755int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8756 const unsigned char *buf,
8757 const unsigned char *end )
8758{
8759 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008760 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008761 const unsigned char *protocol_name_list;
8762 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008763 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008764
8765 /* If ALPN not configured, just ignore the extension */
8766 if( ssl->conf->alpn_list == NULL )
8767 return( 0 );
8768
8769 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008770 * RFC7301, section 3.1
8771 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008772 *
XiaokangQianc7403452022-06-23 03:24:12 +00008773 * struct {
8774 * ProtocolName protocol_name_list<2..2^16-1>
8775 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008776 */
8777
XiaokangQianc7403452022-06-23 03:24:12 +00008778 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008779 * protocol_name_list_len 2 bytes
8780 * protocol_name_len 1 bytes
8781 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008782 */
XiaokangQianacb39922022-06-17 10:18:48 +00008783 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8784
XiaokangQianc7403452022-06-23 03:24:12 +00008785 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008786 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008787 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008788 protocol_name_list = p;
8789 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008790
8791 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008792 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008793 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008794 protocol_name_len = *p++;
8795 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8796 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008797 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008798 {
8799 MBEDTLS_SSL_PEND_FATAL_ALERT(
8800 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8801 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008802 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008803 }
8804
8805 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008806 }
8807
8808 /* Use our order of preference */
8809 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8810 {
8811 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008812 p = protocol_name_list;
8813 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008814 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008815 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008816 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008817 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008818 {
8819 ssl->alpn_chosen = *alpn;
8820 return( 0 );
8821 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008822
8823 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008824 }
8825 }
8826
XiaokangQian95d5f542022-06-24 02:29:26 +00008827 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008828 MBEDTLS_SSL_PEND_FATAL_ALERT(
8829 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8830 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8831 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8832}
8833
8834int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8835 unsigned char *buf,
8836 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008837 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008838{
8839 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008840 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008841 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008842
8843 if( ssl->alpn_chosen == NULL )
8844 {
8845 return( 0 );
8846 }
8847
XiaokangQian95d5f542022-06-24 02:29:26 +00008848 protocol_name_len = strlen( ssl->alpn_chosen );
8849 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008850
8851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8852 /*
8853 * 0 . 1 ext identifier
8854 * 2 . 3 ext length
8855 * 4 . 5 protocol list length
8856 * 6 . 6 protocol name length
8857 * 7 . 7+n protocol name
8858 */
8859 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8860
XiaokangQian95d5f542022-06-24 02:29:26 +00008861 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008862
XiaokangQian95d5f542022-06-24 02:29:26 +00008863 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8864 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008865 /* Note: the length of the chosen protocol has been checked to be less
8866 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8867 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008868 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008869
XiaokangQian95d5f542022-06-24 02:29:26 +00008870 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008871 return ( 0 );
8872}
8873#endif /* MBEDTLS_SSL_ALPN */
8874
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008875#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008876 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008877 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8878int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8879 const char *hostname )
8880{
8881 /* Initialize to suppress unnecessary compiler warning */
8882 size_t hostname_len = 0;
8883
8884 /* Check if new hostname is valid before
8885 * making any change to current one */
8886 if( hostname != NULL )
8887 {
8888 hostname_len = strlen( hostname );
8889
8890 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8892 }
8893
8894 /* Now it's clear that we will overwrite the old hostname,
8895 * so we can free it safely */
8896 if( session->hostname != NULL )
8897 {
8898 mbedtls_platform_zeroize( session->hostname,
8899 strlen( session->hostname ) );
8900 mbedtls_free( session->hostname );
8901 }
8902
8903 /* Passing NULL as hostname shall clear the old one */
8904 if( hostname == NULL )
8905 {
8906 session->hostname = NULL;
8907 }
8908 else
8909 {
8910 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8911 if( session->hostname == NULL )
8912 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8913
8914 memcpy( session->hostname, hostname, hostname_len );
8915 }
8916
8917 return( 0 );
8918}
Xiaokang Qian03409292022-10-12 02:49:52 +00008919#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8920 MBEDTLS_SSL_SESSION_TICKETS &&
8921 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923#endif /* MBEDTLS_SSL_TLS_C */