blob: 9238761639cf6a550a57944154086b60c2eaf4cc [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 Qian281fd1b2022-09-20 11:35:41 +0000300#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
301 if( src->hostname != NULL )
302 {
303 dst->hostname = mbedtls_calloc( 1, src->hostname_len + 1 );
304 if( dst->hostname == NULL )
305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
306
307 memcpy( dst->hostname, src->hostname, src->hostname_len );
308 dst->hostname[src->hostname_len] = '\0';
309 }
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 {
1949 * uint64 ticket_received;
1950 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001951 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001952 * } ClientOnlyData;
1953 *
1954 * struct {
1955 * uint8 endpoint;
1956 * uint8 ciphersuite[2];
1957 * uint32 ticket_age_add;
1958 * uint8 ticket_flags;
1959 * opaque resumption_key<0..255>;
1960 * select ( endpoint ) {
1961 * case client: ClientOnlyData;
1962 * case server: uint64 start_time;
1963 * };
1964 * } serialized_session_tls13;
1965 *
1966 */
1967#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001968MBEDTLS_CHECK_RETURN_CRITICAL
1969static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1970 unsigned char *buf,
1971 size_t buf_len,
1972 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001973{
1974 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001975 size_t needed = 1 /* endpoint */
1976 + 2 /* ciphersuite */
1977 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001978 + 1 /* ticket_flags */
1979 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001980 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001981
1982 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1984 needed += session->resumption_key_len; /* resumption_key */
1985
Jerry Yu438ddd82022-07-07 06:55:50 +00001986#if defined(MBEDTLS_HAVE_TIME)
1987 needed += 8; /* start_time or ticket_received */
1988#endif
1989
1990#if defined(MBEDTLS_SSL_CLI_C)
1991 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1992 {
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00001993#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1994 needed += 1 /* hostname_len */
1995 + session->hostname_len; /* hostname */
1996#endif
1997
Jerry Yu438ddd82022-07-07 06:55:50 +00001998 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001999 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002000
2001 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002002 if( session->ticket_len > SIZE_MAX - needed )
2003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002004
Jerry Yue28d9742022-08-18 15:44:03 +08002005 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002006 }
2007#endif /* MBEDTLS_SSL_CLI_C */
2008
Jerry Yue36fdd62022-08-17 21:31:36 +08002009 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002010 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002011 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002012
2013 p[0] = session->endpoint;
2014 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2015 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2016 p[7] = session->ticket_flags;
2017
2018 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002019 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002020 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002021 memcpy( p, session->resumption_key, session->resumption_key_len );
2022 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002023
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002024#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
2025 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2026 session->hostname_len != 0 &&
2027 session->hostname != NULL )
2028 {
2029 /* save host name */
2030 p[0] = session->hostname_len;
2031 p++;
2032 memcpy( p, session->hostname, session->hostname_len );
2033 p += session->hostname_len;
2034 }
2035#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2036
Jerry Yu438ddd82022-07-07 06:55:50 +00002037#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2038 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2039 {
2040 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2041 p += 8;
2042 }
2043#endif /* MBEDTLS_HAVE_TIME */
2044
2045#if defined(MBEDTLS_SSL_CLI_C)
2046 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2047 {
2048#if defined(MBEDTLS_HAVE_TIME)
2049 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2050 p += 8;
2051#endif
2052 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2053 p += 4;
2054
2055 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2056 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002057
2058 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002059 {
2060 memcpy( p, session->ticket, session->ticket_len );
2061 p += session->ticket_len;
2062 }
2063 }
2064#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002065 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002066}
2067
2068MBEDTLS_CHECK_RETURN_CRITICAL
2069static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2070 const unsigned char *buf,
2071 size_t len )
2072{
2073 const unsigned char *p = buf;
2074 const unsigned char *end = buf + len;
2075
2076 if( end - p < 9 )
2077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2078 session->endpoint = p[0];
2079 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2080 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2081 session->ticket_flags = p[7];
2082
2083 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002084 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002085 p += 9;
2086
Jerry Yubc7c1a42022-07-21 22:57:37 +08002087 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2089
Jerry Yubc7c1a42022-07-21 22:57:37 +08002090 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002092 memcpy( session->resumption_key, p, session->resumption_key_len );
2093 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002094
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
2096 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2097 {
2098 /* load host name */
2099 session->hostname_len = p[0];
2100 p += 1;
2101 if( end - p < session->hostname_len )
2102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2103 session->hostname = mbedtls_calloc( 1, session->hostname_len );
2104 if( session->hostname == NULL )
2105 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2106 memcpy( session->hostname, p, session->hostname_len );
2107 p += session->hostname_len;
2108 }
2109#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2110
Jerry Yu438ddd82022-07-07 06:55:50 +00002111#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2112 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2113 {
2114 if( end - p < 8 )
2115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2116 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2117 p += 8;
2118 }
2119#endif /* MBEDTLS_HAVE_TIME */
2120
2121#if defined(MBEDTLS_SSL_CLI_C)
2122 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2123 {
2124#if defined(MBEDTLS_HAVE_TIME)
2125 if( end - p < 8 )
2126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2127 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2128 p += 8;
2129#endif
2130 if( end - p < 4 )
2131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2132 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2133 p += 4;
2134
2135 if( end - p < 2 )
2136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2137 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2138 p += 2;
2139
2140 if( end - p < ( long int )session->ticket_len )
2141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2142 if( session->ticket_len > 0 )
2143 {
2144 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2145 if( session->ticket == NULL )
2146 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2147 memcpy( session->ticket, p, session->ticket_len );
2148 p += session->ticket_len;
2149 }
2150 }
2151#endif /* MBEDTLS_SSL_CLI_C */
2152
2153 return( 0 );
2154
2155}
2156#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002157MBEDTLS_CHECK_RETURN_CRITICAL
2158static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2159 unsigned char *buf,
2160 size_t buf_len,
2161 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002162{
2163 ((void) session);
2164 ((void) buf);
2165 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002166 *olen = 0;
2167 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002168}
Jerry Yu438ddd82022-07-07 06:55:50 +00002169
2170static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2171 unsigned char *buf,
2172 size_t buf_len )
2173{
2174 ((void) session);
2175 ((void) buf);
2176 ((void) buf_len);
2177 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2178}
2179#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002180#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2181
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002182psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002183 size_t taglen,
2184 psa_algorithm_t *alg,
2185 psa_key_type_t *key_type,
2186 size_t *key_size )
2187{
2188 switch ( mbedtls_cipher_type )
2189 {
2190 case MBEDTLS_CIPHER_AES_128_CBC:
2191 *alg = PSA_ALG_CBC_NO_PADDING;
2192 *key_type = PSA_KEY_TYPE_AES;
2193 *key_size = 128;
2194 break;
2195 case MBEDTLS_CIPHER_AES_128_CCM:
2196 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2197 *key_type = PSA_KEY_TYPE_AES;
2198 *key_size = 128;
2199 break;
2200 case MBEDTLS_CIPHER_AES_128_GCM:
2201 *alg = PSA_ALG_GCM;
2202 *key_type = PSA_KEY_TYPE_AES;
2203 *key_size = 128;
2204 break;
2205 case MBEDTLS_CIPHER_AES_192_CCM:
2206 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2207 *key_type = PSA_KEY_TYPE_AES;
2208 *key_size = 192;
2209 break;
2210 case MBEDTLS_CIPHER_AES_192_GCM:
2211 *alg = PSA_ALG_GCM;
2212 *key_type = PSA_KEY_TYPE_AES;
2213 *key_size = 192;
2214 break;
2215 case MBEDTLS_CIPHER_AES_256_CBC:
2216 *alg = PSA_ALG_CBC_NO_PADDING;
2217 *key_type = PSA_KEY_TYPE_AES;
2218 *key_size = 256;
2219 break;
2220 case MBEDTLS_CIPHER_AES_256_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 = 256;
2224 break;
2225 case MBEDTLS_CIPHER_AES_256_GCM:
2226 *alg = PSA_ALG_GCM;
2227 *key_type = PSA_KEY_TYPE_AES;
2228 *key_size = 256;
2229 break;
2230 case MBEDTLS_CIPHER_ARIA_128_CBC:
2231 *alg = PSA_ALG_CBC_NO_PADDING;
2232 *key_type = PSA_KEY_TYPE_ARIA;
2233 *key_size = 128;
2234 break;
2235 case MBEDTLS_CIPHER_ARIA_128_CCM:
2236 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2237 *key_type = PSA_KEY_TYPE_ARIA;
2238 *key_size = 128;
2239 break;
2240 case MBEDTLS_CIPHER_ARIA_128_GCM:
2241 *alg = PSA_ALG_GCM;
2242 *key_type = PSA_KEY_TYPE_ARIA;
2243 *key_size = 128;
2244 break;
2245 case MBEDTLS_CIPHER_ARIA_192_CCM:
2246 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2247 *key_type = PSA_KEY_TYPE_ARIA;
2248 *key_size = 192;
2249 break;
2250 case MBEDTLS_CIPHER_ARIA_192_GCM:
2251 *alg = PSA_ALG_GCM;
2252 *key_type = PSA_KEY_TYPE_ARIA;
2253 *key_size = 192;
2254 break;
2255 case MBEDTLS_CIPHER_ARIA_256_CBC:
2256 *alg = PSA_ALG_CBC_NO_PADDING;
2257 *key_type = PSA_KEY_TYPE_ARIA;
2258 *key_size = 256;
2259 break;
2260 case MBEDTLS_CIPHER_ARIA_256_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 = 256;
2264 break;
2265 case MBEDTLS_CIPHER_ARIA_256_GCM:
2266 *alg = PSA_ALG_GCM;
2267 *key_type = PSA_KEY_TYPE_ARIA;
2268 *key_size = 256;
2269 break;
2270 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2271 *alg = PSA_ALG_CBC_NO_PADDING;
2272 *key_type = PSA_KEY_TYPE_CAMELLIA;
2273 *key_size = 128;
2274 break;
2275 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2276 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2277 *key_type = PSA_KEY_TYPE_CAMELLIA;
2278 *key_size = 128;
2279 break;
2280 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2281 *alg = PSA_ALG_GCM;
2282 *key_type = PSA_KEY_TYPE_CAMELLIA;
2283 *key_size = 128;
2284 break;
2285 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2286 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2287 *key_type = PSA_KEY_TYPE_CAMELLIA;
2288 *key_size = 192;
2289 break;
2290 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2291 *alg = PSA_ALG_GCM;
2292 *key_type = PSA_KEY_TYPE_CAMELLIA;
2293 *key_size = 192;
2294 break;
2295 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2296 *alg = PSA_ALG_CBC_NO_PADDING;
2297 *key_type = PSA_KEY_TYPE_CAMELLIA;
2298 *key_size = 256;
2299 break;
2300 case MBEDTLS_CIPHER_CAMELLIA_256_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 = 256;
2304 break;
2305 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2306 *alg = PSA_ALG_GCM;
2307 *key_type = PSA_KEY_TYPE_CAMELLIA;
2308 *key_size = 256;
2309 break;
2310 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2311 *alg = PSA_ALG_CHACHA20_POLY1305;
2312 *key_type = PSA_KEY_TYPE_CHACHA20;
2313 *key_size = 256;
2314 break;
2315 case MBEDTLS_CIPHER_NULL:
2316 *alg = MBEDTLS_SSL_NULL_CIPHER;
2317 *key_type = 0;
2318 *key_size = 0;
2319 break;
2320 default:
2321 return PSA_ERROR_NOT_SUPPORTED;
2322 }
2323
2324 return PSA_SUCCESS;
2325}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002326#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002327
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002328#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002329int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2330 const unsigned char *dhm_P, size_t P_len,
2331 const unsigned char *dhm_G, size_t G_len )
2332{
Janos Follath865b3eb2019-12-16 11:46:15 +00002333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002334
Glenn Strausscee11292021-12-20 01:43:17 -05002335 mbedtls_mpi_free( &conf->dhm_P );
2336 mbedtls_mpi_free( &conf->dhm_G );
2337
Hanno Beckera90658f2017-10-04 15:29:08 +01002338 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2339 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2340 {
2341 mbedtls_mpi_free( &conf->dhm_P );
2342 mbedtls_mpi_free( &conf->dhm_G );
2343 return( ret );
2344 }
2345
2346 return( 0 );
2347}
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002349int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002350{
Janos Follath865b3eb2019-12-16 11:46:15 +00002351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002352
Glenn Strausscee11292021-12-20 01:43:17 -05002353 mbedtls_mpi_free( &conf->dhm_P );
2354 mbedtls_mpi_free( &conf->dhm_G );
2355
Gilles Peskinee5702482021-06-11 21:59:08 +02002356 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2357 &conf->dhm_P ) ) != 0 ||
2358 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2359 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002360 {
2361 mbedtls_mpi_free( &conf->dhm_P );
2362 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002363 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002364 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002365
2366 return( 0 );
2367}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002368#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002369
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002370#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2371/*
2372 * Set the minimum length for Diffie-Hellman parameters
2373 */
2374void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2375 unsigned int bitlen )
2376{
2377 conf->dhm_min_bitlen = bitlen;
2378}
2379#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2380
Gilles Peskineeccd8882020-03-10 12:19:08 +01002381#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002382#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002383/*
2384 * Set allowed/preferred hashes for handshake signatures
2385 */
2386void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2387 const int *hashes )
2388{
2389 conf->sig_hashes = hashes;
2390}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002391#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002392
Jerry Yuf017ee42022-01-12 15:49:48 +08002393/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002394void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2395 const uint16_t* sig_algs )
2396{
Jerry Yuf017ee42022-01-12 15:49:48 +08002397#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2398 conf->sig_hashes = NULL;
2399#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2400 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002401}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002402#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002403
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002404#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002405#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002406/*
2407 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002408 *
2409 * mbedtls_ssl_setup() takes the provided list
2410 * and translates it to a list of IANA TLS group identifiers,
2411 * stored in ssl->handshake->group_list.
2412 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002413 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002414void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002415 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002416{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002417 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002418 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002419}
Brett Warrene0edc842021-08-17 09:53:13 +01002420#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002421#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002422
Brett Warrene0edc842021-08-17 09:53:13 +01002423/*
2424 * Set the allowed groups
2425 */
2426void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2427 const uint16_t *group_list )
2428{
2429#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2430 conf->curve_list = NULL;
2431#endif
2432 conf->group_list = group_list;
2433}
2434
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002435#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437{
Hanno Becker947194e2017-04-07 13:25:49 +01002438 /* Initialize to suppress unnecessary compiler warning */
2439 size_t hostname_len = 0;
2440
2441 /* Check if new hostname is valid before
2442 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002443 if( hostname != NULL )
2444 {
2445 hostname_len = strlen( hostname );
2446
2447 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2449 }
2450
2451 /* Now it's clear that we will overwrite the old hostname,
2452 * so we can free it safely */
2453
2454 if( ssl->hostname != NULL )
2455 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002456 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002457 mbedtls_free( ssl->hostname );
2458 }
2459
2460 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002461
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002463 {
2464 ssl->hostname = NULL;
2465 }
2466 else
2467 {
2468 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002469 if( ssl->hostname == NULL )
2470 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002471
Hanno Becker947194e2017-04-07 13:25:49 +01002472 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002473
Hanno Becker947194e2017-04-07 13:25:49 +01002474 ssl->hostname[hostname_len] = '\0';
2475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
2477 return( 0 );
2478}
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002479
2480int mbedtls_ssl_reset_hostname( mbedtls_ssl_context *ssl,
2481 const char *hostname,
2482 const char *rec_hostname )
2483{
2484 /* Initialize to suppress unnecessary compiler warning */
2485 size_t rec_hostname_len = 0;
2486
2487 if( hostname == NULL || rec_hostname == NULL )
2488 return( 0 );
2489
2490 rec_hostname_len = strlen( rec_hostname );
2491 if( rec_hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2493
2494 if( rec_hostname_len == strlen( hostname ) &&
2495 memcmp( hostname, rec_hostname, rec_hostname_len ) == 0 )
2496 return( 0 );
2497
2498 if( ssl->hostname != NULL )
2499 {
2500 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
2501 mbedtls_free( ssl->hostname );
2502 ssl->hostname = NULL;
2503 ssl->hostname = mbedtls_calloc( 1, rec_hostname_len + 1 );
2504 if( ssl->hostname == NULL )
2505 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2506 memcpy( ssl->hostname, rec_hostname, rec_hostname_len );
2507 ssl->hostname[rec_hostname_len] = '\0';
2508 }
2509
2510 return( 0 );
2511}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002512#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002514#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002515void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002517 const unsigned char *, size_t),
2518 void *p_sni )
2519{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002520 conf->f_sni = f_sni;
2521 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002526int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002527{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002528 size_t cur_len, tot_len;
2529 const char **p;
2530
2531 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002532 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2533 * MUST NOT be truncated."
2534 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002535 */
2536 tot_len = 0;
2537 for( p = protos; *p != NULL; p++ )
2538 {
2539 cur_len = strlen( *p );
2540 tot_len += cur_len;
2541
Ronald Cron8216dd32020-04-23 16:41:44 +02002542 if( ( cur_len == 0 ) ||
2543 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2544 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002546 }
2547
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002548 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002549
2550 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002551}
2552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002554{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002555 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002558
Johan Pascalb62bb512015-12-03 21:56:45 +01002559#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002560void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2561 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002562{
2563 conf->dtls_srtp_mki_support = support_mki_value;
2564}
2565
Ron Eldoref72faf2018-07-12 11:54:20 +03002566int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2567 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002568 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002569{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002570 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002571 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002573 }
Ron Eldor591f1622018-01-22 12:30:04 +02002574
2575 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002576 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002577 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002578 }
Ron Eldor591f1622018-01-22 12:30:04 +02002579
2580 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2581 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002582 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002583}
2584
Ron Eldoref72faf2018-07-12 11:54:20 +03002585int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002586 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002587{
Johan Pascal253d0262020-09-22 13:04:45 +02002588 const mbedtls_ssl_srtp_profile *p;
2589 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002590
Johan Pascal253d0262020-09-22 13:04:45 +02002591 /* check the profiles list: all entry must be valid,
2592 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002593 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2594 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2595 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002596 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002597 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002598 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002599 list_size++;
2600 }
2601 else
2602 {
2603 /* unsupported value, stop parsing and set the size to an error value */
2604 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002605 }
2606 }
2607
Johan Pascal5ef72d22020-10-28 17:05:47 +01002608 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002609 {
Johan Pascal253d0262020-09-22 13:04:45 +02002610 conf->dtls_srtp_profile_list = NULL;
2611 conf->dtls_srtp_profile_list_len = 0;
2612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2613 }
2614
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002615 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002616 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002617
2618 return( 0 );
2619}
2620
Johan Pascal5ef72d22020-10-28 17:05:47 +01002621void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2622 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002623{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002624 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2625 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002626 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002627 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002628 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002629 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002630 else
2631 {
2632 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002633 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2634 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002635 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002636}
Johan Pascalb62bb512015-12-03 21:56:45 +01002637#endif /* MBEDTLS_SSL_DTLS_SRTP */
2638
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302639#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002640void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002641{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002642 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002643}
2644
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002645void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002646{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002647 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002648}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302649#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002650
Janos Follath088ce432017-04-10 12:42:31 +01002651#if defined(MBEDTLS_SSL_SRV_C)
2652void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2653 char cert_req_ca_list )
2654{
2655 conf->cert_req_ca_list = cert_req_ca_list;
2656}
2657#endif
2658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002660void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002662 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002663}
2664#endif
2665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002667void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002669 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002670}
2671#endif
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002674int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002677 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002680 }
2681
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002682 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002683
2684 return( 0 );
2685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002687
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002688void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002689{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002690 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002691}
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002694void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002695{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002696 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002697}
2698
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002699void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002700{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002701 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002702}
2703
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002704void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002705 const unsigned char period[8] )
2706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002707 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002712#if defined(MBEDTLS_SSL_CLI_C)
2713void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002714{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002715 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002716}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002717#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002718
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002719#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002720
Jerry Yud0766ec2022-09-22 10:46:57 +08002721#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002722void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2723 uint16_t num_tickets )
2724{
Jerry Yud0766ec2022-09-22 10:46:57 +08002725 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002726}
2727#endif
2728
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002729void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2730 mbedtls_ssl_ticket_write_t *f_ticket_write,
2731 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2732 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002733{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002734 conf->f_ticket_write = f_ticket_write;
2735 conf->f_ticket_parse = f_ticket_parse;
2736 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002737}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002740
Hanno Becker7e6c1782021-06-08 09:24:55 +01002741void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2742 mbedtls_ssl_export_keys_t *f_export_keys,
2743 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002744{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002745 ssl->f_export_keys = f_export_keys;
2746 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002747}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002748
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002749#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002750void mbedtls_ssl_conf_async_private_cb(
2751 mbedtls_ssl_config *conf,
2752 mbedtls_ssl_async_sign_t *f_async_sign,
2753 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2754 mbedtls_ssl_async_resume_t *f_async_resume,
2755 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002756 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002757{
2758 conf->f_async_sign_start = f_async_sign;
2759 conf->f_async_decrypt_start = f_async_decrypt;
2760 conf->f_async_resume = f_async_resume;
2761 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002762 conf->p_async_config_data = async_config_data;
2763}
2764
Gilles Peskine8f97af72018-04-26 11:46:10 +02002765void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2766{
2767 return( conf->p_async_config_data );
2768}
2769
Gilles Peskine1febfef2018-04-30 11:54:39 +02002770void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002771{
2772 if( ssl->handshake == NULL )
2773 return( NULL );
2774 else
2775 return( ssl->handshake->user_async_ctx );
2776}
2777
Gilles Peskine1febfef2018-04-30 11:54:39 +02002778void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002779 void *ctx )
2780{
2781 if( ssl->handshake != NULL )
2782 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002783}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002784#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002785
Paul Bakker5121ce52009-01-03 21:22:43 +00002786/*
2787 * SSL get accessors
2788 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002789uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002790{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002791 if( ssl->session != NULL )
2792 return( ssl->session->verify_result );
2793
2794 if( ssl->session_negotiate != NULL )
2795 return( ssl->session_negotiate->verify_result );
2796
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002797 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798}
2799
Glenn Strauss8f526902022-01-13 00:04:49 -05002800int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2801{
2802 if( ssl == NULL || ssl->session == NULL )
2803 return( 0 );
2804
2805 return( ssl->session->ciphersuite );
2806}
2807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002809{
Paul Bakker926c8e42013-03-06 10:23:34 +01002810 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002811 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002814}
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002817{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002819 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002820 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002821 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002822 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002823 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002824 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002825 default:
2826 return( "unknown (DTLS)" );
2827 }
2828 }
2829#endif
2830
Glenn Strauss60bfe602022-03-14 19:04:24 -04002831 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002832 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002833 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002835 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002836 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002837 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002838 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002839 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002840}
2841
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002842#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002843size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2844{
David Horstmann95d516f2021-05-04 18:36:56 +01002845 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002846 size_t read_mfl;
2847
2848 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2849 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2850 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2851 {
2852 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2853 }
2854
2855 /* Check if a smaller max length was negotiated */
2856 if( ssl->session_out != NULL )
2857 {
2858 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2859 if( read_mfl < max_len )
2860 {
2861 max_len = read_mfl;
2862 }
2863 }
2864
2865 // During a handshake, use the value being negotiated
2866 if( ssl->session_negotiate != NULL )
2867 {
2868 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2869 if( read_mfl < max_len )
2870 {
2871 max_len = read_mfl;
2872 }
2873 }
2874
2875 return( max_len );
2876}
2877
2878size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002879{
2880 size_t max_len;
2881
2882 /*
2883 * Assume mfl_code is correct since it was checked when set
2884 */
Angus Grattond8213d02016-05-25 20:56:48 +10002885 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002886
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002887 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002888 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002889 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002890 {
Angus Grattond8213d02016-05-25 20:56:48 +10002891 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002892 }
2893
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002894 /* During a handshake, use the value being negotiated */
2895 if( ssl->session_negotiate != NULL &&
2896 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2897 {
2898 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2899 }
2900
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002901 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002902}
2903#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2904
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002906size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002907{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002908 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2909 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2910 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2911 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2912 return ( 0 );
2913
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002914 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2915 return( ssl->mtu );
2916
2917 if( ssl->mtu == 0 )
2918 return( ssl->handshake->mtu );
2919
2920 return( ssl->mtu < ssl->handshake->mtu ?
2921 ssl->mtu : ssl->handshake->mtu );
2922}
2923#endif /* MBEDTLS_SSL_PROTO_DTLS */
2924
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002925int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2926{
2927 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2928
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002929#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2930 !defined(MBEDTLS_SSL_PROTO_DTLS)
2931 (void) ssl;
2932#endif
2933
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002934#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002935 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002936
2937 if( max_len > mfl )
2938 max_len = mfl;
2939#endif
2940
2941#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002942 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002943 {
Hanno Becker89490712020-02-05 10:50:12 +00002944 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002945 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2946 const size_t overhead = (size_t) ret;
2947
2948 if( ret < 0 )
2949 return( ret );
2950
2951 if( mtu <= overhead )
2952 {
2953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2954 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2955 }
2956
2957 if( max_len > mtu - overhead )
2958 max_len = mtu - overhead;
2959 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002960#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002961
Hanno Becker0defedb2018-08-10 12:35:02 +01002962#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2963 !defined(MBEDTLS_SSL_PROTO_DTLS)
2964 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002965#endif
2966
2967 return( (int) max_len );
2968}
2969
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002970int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2971{
2972 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2973
2974#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2975 (void) ssl;
2976#endif
2977
2978#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2979 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2980
2981 if( max_len > mfl )
2982 max_len = mfl;
2983#endif
2984
2985 return( (int) max_len );
2986}
2987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_X509_CRT_PARSE_C)
2989const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002990{
2991 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002992 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002993
Hanno Beckere6824572019-02-07 13:18:46 +00002994#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002995 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002996#else
2997 return( NULL );
2998#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003003int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3004 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003005{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003006 int ret;
3007
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003008 if( ssl == NULL ||
3009 dst == NULL ||
3010 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003011 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003014 }
3015
Hanno Beckere810bbc2021-05-14 16:01:05 +01003016 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3017 * idempotent: Each session can only be exported once.
3018 *
3019 * (This is in preparation for TLS 1.3 support where we will
3020 * need the ability to export multiple sessions (aka tickets),
3021 * which will be achieved by calling mbedtls_ssl_get_session()
3022 * multiple times until it fails.)
3023 *
3024 * Check whether we have already exported the current session,
3025 * and fail if so.
3026 */
3027 if( ssl->session->exported == 1 )
3028 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3029
3030 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3031 if( ret != 0 )
3032 return( ret );
3033
3034 /* Remember that we've exported the session. */
3035 ssl->session->exported = 1;
3036 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003039
Paul Bakker5121ce52009-01-03 21:22:43 +00003040/*
Hanno Beckera835da52019-05-16 12:39:07 +01003041 * Define ticket header determining Mbed TLS version
3042 * and structure of the ticket.
3043 */
3044
Hanno Becker94ef3b32019-05-16 12:50:45 +01003045/*
Hanno Becker50b59662019-05-28 14:30:45 +01003046 * Define bitflag determining compile-time settings influencing
3047 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048 */
3049
Hanno Becker50b59662019-05-28 14:30:45 +01003050#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003051#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003052#else
Hanno Becker3e088662019-05-29 11:10:18 +01003053#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003054#endif /* MBEDTLS_HAVE_TIME */
3055
3056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003057#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003058#else
Hanno Becker3e088662019-05-29 11:10:18 +01003059#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003060#endif /* MBEDTLS_X509_CRT_PARSE_C */
3061
3062#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003063#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003064#else
Hanno Becker3e088662019-05-29 11:10:18 +01003065#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003066#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3067
3068#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003069#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003070#else
Hanno Becker3e088662019-05-29 11:10:18 +01003071#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003072#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3073
Hanno Becker94ef3b32019-05-16 12:50:45 +01003074#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003075#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003076#else
Hanno Becker3e088662019-05-29 11:10:18 +01003077#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003078#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3079
Hanno Becker94ef3b32019-05-16 12:50:45 +01003080#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3081#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3082#else
3083#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3084#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3085
Hanno Becker3e088662019-05-29 11:10:18 +01003086#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3087#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3088#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3089#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003090#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3091#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003092
Hanno Becker50b59662019-05-28 14:30:45 +01003093#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003094 ( (uint16_t) ( \
3095 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3096 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3097 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3098 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003099 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003100 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003101
Hanno Beckerf8787072019-05-16 12:41:07 +01003102static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003103 MBEDTLS_VERSION_MAJOR,
3104 MBEDTLS_VERSION_MINOR,
3105 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003106 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3107 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003108};
Hanno Beckera835da52019-05-16 12:39:07 +01003109
3110/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003111 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003112 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003113 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003114 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003115 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003116 * opaque mbedtls_version[3]; // library version: major, minor, patch
3117 * opaque session_format[2]; // library-version specific 16-bit field
3118 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003119 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003120 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003121 * Note: When updating the format, remember to keep
3122 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003123 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003124 * // In this version, `session_format` determines
3125 * // the setting of those compile-time
3126 * // configuration options which influence
3127 * // the structure of mbedtls_ssl_session.
3128 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003129 * uint8_t minor_ver; // Protocol minor version. Possible values:
3130 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003131 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003132 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003133 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003134 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003135 * serialized_session_tls12 data;
3136 *
3137 * };
3138 *
3139 * } serialized_session;
3140 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003141 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003142
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003143MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003144static int ssl_session_save( const mbedtls_ssl_session *session,
3145 unsigned char omit_header,
3146 unsigned char *buf,
3147 size_t buf_len,
3148 size_t *olen )
3149{
3150 unsigned char *p = buf;
3151 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003152 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003153#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3154 size_t out_len;
3155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3156#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003157 if( session == NULL )
3158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3159
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003160 if( !omit_header )
3161 {
3162 /*
3163 * Add Mbed TLS version identifier
3164 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003165 used += sizeof( ssl_serialized_session_header );
3166
3167 if( used <= buf_len )
3168 {
3169 memcpy( p, ssl_serialized_session_header,
3170 sizeof( ssl_serialized_session_header ) );
3171 p += sizeof( ssl_serialized_session_header );
3172 }
3173 }
3174
3175 /*
3176 * TLS version identifier
3177 */
3178 used += 1;
3179 if( used <= buf_len )
3180 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003181 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003182 }
3183
3184 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003185 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003186 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003187 {
3188#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003189 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003190 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003191 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003192#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3193
Jerry Yu251a12e2022-07-13 15:15:48 +08003194#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3195 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003196 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3197 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003198 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003199 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003200 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003201#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3202
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003203 default:
3204 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3205 }
3206
3207 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003208 if( used > buf_len )
3209 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003210
3211 return( 0 );
3212}
3213
3214/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003215 * Public wrapper for ssl_session_save()
3216 */
3217int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3218 unsigned char *buf,
3219 size_t buf_len,
3220 size_t *olen )
3221{
3222 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3223}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003224
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003225/*
3226 * Deserialize session, see mbedtls_ssl_session_save() for format.
3227 *
3228 * This internal version is wrapped by a public function that cleans up in
3229 * case of error, and has an extra option omit_header.
3230 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003231MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003232static int ssl_session_load( mbedtls_ssl_session *session,
3233 unsigned char omit_header,
3234 const unsigned char *buf,
3235 size_t len )
3236{
3237 const unsigned char *p = buf;
3238 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003239 size_t remaining_len;
3240
3241
3242 if( session == NULL )
3243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003244
3245 if( !omit_header )
3246 {
3247 /*
3248 * Check Mbed TLS version identifier
3249 */
3250
3251 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3253
3254 if( memcmp( p, ssl_serialized_session_header,
3255 sizeof( ssl_serialized_session_header ) ) != 0 )
3256 {
3257 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3258 }
3259 p += sizeof( ssl_serialized_session_header );
3260 }
3261
3262 /*
3263 * TLS version identifier
3264 */
3265 if( 1 > (size_t)( end - p ) )
3266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003267 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003268
3269 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003270 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003271 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003272 {
3273#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003274 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003275 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003276#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3277
Jerry Yu438ddd82022-07-07 06:55:50 +00003278#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3279 case MBEDTLS_SSL_VERSION_TLS1_3:
3280 return( ssl_tls13_session_load( session, p, remaining_len ) );
3281#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3282
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003283 default:
3284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3285 }
3286}
3287
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003288/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003289 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003290 */
3291int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3292 const unsigned char *buf,
3293 size_t len )
3294{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003295 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003296
3297 if( ret != 0 )
3298 mbedtls_ssl_session_free( session );
3299
3300 return( ret );
3301}
3302
3303/*
Paul Bakker1961b702013-01-25 14:49:24 +01003304 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003306MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003307static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3308{
3309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3310
Ronald Cron66dbf912022-02-02 15:33:46 +01003311 /*
3312 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003313 * that were written into the output buffer by the previous handshake step,
3314 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003315 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3316 * We proceed to the next handshake step only when all data from the
3317 * previous one have been sent to the peer, thus we make sure that this is
3318 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3319 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3320 * we have to wait before to go ahead.
3321 * In the case of TLS 1.3, handshake step handlers do not send data to the
3322 * peer. Data are only sent here and through
3323 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003324 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003325 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003326 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3327 return( ret );
3328
3329#if defined(MBEDTLS_SSL_PROTO_DTLS)
3330 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3331 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3332 {
3333 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3334 return( ret );
3335 }
3336#endif /* MBEDTLS_SSL_PROTO_DTLS */
3337
3338 return( ret );
3339}
3340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003342{
Hanno Becker41934dd2021-08-07 19:13:43 +01003343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003344
Hanno Becker41934dd2021-08-07 19:13:43 +01003345 if( ssl == NULL ||
3346 ssl->conf == NULL ||
3347 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003348 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003349 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003351 }
3352
3353 ret = ssl_prepare_handshake_step( ssl );
3354 if( ret != 0 )
3355 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003356
Jerry Yue7047812021-09-13 19:26:39 +08003357 ret = mbedtls_ssl_handle_pending_alert( ssl );
3358 if( ret != 0 )
3359 goto cleanup;
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003362 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003363 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3365 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003366
Ronald Cron9f0fba32022-02-10 16:45:15 +01003367 switch( ssl->state )
3368 {
3369 case MBEDTLS_SSL_HELLO_REQUEST:
3370 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3371 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003372
Ronald Cron9f0fba32022-02-10 16:45:15 +01003373 case MBEDTLS_SSL_CLIENT_HELLO:
3374 ret = mbedtls_ssl_write_client_hello( ssl );
3375 break;
3376
3377 default:
3378#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003379 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003380 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3381 else
3382 ret = mbedtls_ssl_handshake_client_step( ssl );
3383#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3384 ret = mbedtls_ssl_handshake_client_step( ssl );
3385#else
3386 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3387#endif
3388 }
Jerry Yub9930e72021-08-06 17:11:51 +08003389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003392 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003393 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003394#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003395 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003396 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003397#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003398
3399#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3400 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3401 ret = mbedtls_ssl_handshake_server_step( ssl );
3402#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003404#endif
3405
Jerry Yue7047812021-09-13 19:26:39 +08003406 if( ret != 0 )
3407 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003408 /* handshake_step return error. And it is same
3409 * with alert_reason.
3410 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003411 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003412 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003413 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003414 goto cleanup;
3415 }
3416 }
3417
3418cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003419 return( ret );
3420}
3421
3422/*
3423 * Perform the SSL handshake
3424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003426{
3427 int ret = 0;
3428
Hanno Beckera817ea42020-10-20 15:20:23 +01003429 /* Sanity checks */
3430
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003431 if( ssl == NULL || ssl->conf == NULL )
3432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3433
Hanno Beckera817ea42020-10-20 15:20:23 +01003434#if defined(MBEDTLS_SSL_PROTO_DTLS)
3435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3436 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3437 {
3438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3439 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3441 }
3442#endif /* MBEDTLS_SSL_PROTO_DTLS */
3443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003445
Hanno Beckera817ea42020-10-20 15:20:23 +01003446 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003447 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003450
3451 if( ret != 0 )
3452 break;
3453 }
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003456
3457 return( ret );
3458}
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#if defined(MBEDTLS_SSL_RENEGOTIATION)
3461#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003462/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003463 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003464 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003465MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003467{
Janos Follath865b3eb2019-12-16 11:46:15 +00003468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003471
3472 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3474 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003475
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003476 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003477 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003479 return( ret );
3480 }
3481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003483
3484 return( 0 );
3485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003487
3488/*
3489 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 * - any side: calling mbedtls_ssl_renegotiate(),
3491 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3492 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003493 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003494 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003496 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003497int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003498{
Janos Follath865b3eb2019-12-16 11:46:15 +00003499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003502
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003503 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3504 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003505
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003506 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3507 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003509 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003511 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003512 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003513 ssl->handshake->out_msg_seq = 1;
3514 else
3515 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003516 }
3517#endif
3518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3520 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003525 return( ret );
3526 }
3527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003529
3530 return( 0 );
3531}
3532
3533/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003534 * Renegotiate current connection on client,
3535 * or request renegotiation on server
3536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003540
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003541 if( ssl == NULL || ssl->conf == NULL )
3542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003545 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003546 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003547 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003548 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003552
3553 /* Did we already try/start sending HelloRequest? */
3554 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003556
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003557 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003562 /*
3563 * On client, either start the renegotiation process or,
3564 * if already in progress, continue the handshake
3565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003567 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003568 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003570
Hanno Becker40cdaa12020-02-05 10:48:27 +00003571 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003572 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003574 return( ret );
3575 }
3576 }
3577 else
3578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003582 return( ret );
3583 }
3584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003586
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003587 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003590
Gilles Peskine9b562d52018-04-25 20:32:43 +02003591void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003592{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003593 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3594
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003595 if( handshake == NULL )
3596 return;
3597
Brett Warrene0edc842021-08-17 09:53:13 +01003598#if defined(MBEDTLS_ECP_C)
3599#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3600 if ( ssl->handshake->group_list_heap_allocated )
3601 mbedtls_free( (void*) handshake->group_list );
3602 handshake->group_list = NULL;
3603#endif /* MBEDTLS_DEPRECATED_REMOVED */
3604#endif /* MBEDTLS_ECP_C */
3605
Jerry Yuf017ee42022-01-12 15:49:48 +08003606#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3607#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3608 if ( ssl->handshake->sig_algs_heap_allocated )
3609 mbedtls_free( (void*) handshake->sig_algs );
3610 handshake->sig_algs = NULL;
3611#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003612#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3613 if( ssl->handshake->certificate_request_context )
3614 {
3615 mbedtls_free( (void*) handshake->certificate_request_context );
3616 }
3617#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003618#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3619
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003620#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3621 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3622 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003623 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003624 handshake->async_in_progress = 0;
3625 }
3626#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3627
Andrzej Kurek25f27152022-08-17 16:09:31 -04003628#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003629#if defined(MBEDTLS_USE_PSA_CRYPTO)
3630 psa_hash_abort( &handshake->fin_sha256_psa );
3631#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003632 mbedtls_sha256_free( &handshake->fin_sha256 );
3633#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003634#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003635#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003636#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003637 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003638#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003639 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003640#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003641#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643#if defined(MBEDTLS_DHM_C)
3644 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003645#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003646#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003648#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003649#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003650 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003651#if defined(MBEDTLS_SSL_CLI_C)
3652 mbedtls_free( handshake->ecjpake_cache );
3653 handshake->ecjpake_cache = NULL;
3654 handshake->ecjpake_cache_len = 0;
3655#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003656#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003657
Janos Follath4ae5c292016-02-10 11:27:43 +00003658#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3659 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003660 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003662#endif
3663
Gilles Peskineeccd8882020-03-10 12:19:08 +01003664#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003665#if defined(MBEDTLS_USE_PSA_CRYPTO)
3666 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3667 {
3668 /* The maintenance of the external PSK key slot is the
3669 * user's responsibility. */
3670 if( ssl->handshake->psk_opaque_is_internal )
3671 {
3672 psa_destroy_key( ssl->handshake->psk_opaque );
3673 ssl->handshake->psk_opaque_is_internal = 0;
3674 }
3675 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3676 }
Neil Armstronge952a302022-05-03 10:22:14 +02003677#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003678 if( handshake->psk != NULL )
3679 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003680 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003681 mbedtls_free( handshake->psk );
3682 }
Neil Armstronge952a302022-05-03 10:22:14 +02003683#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003684#endif
3685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3687 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003688 /*
3689 * Free only the linked list wrapper, not the keys themselves
3690 * since the belong to the SNI callback
3691 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003692 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003694
Gilles Peskineeccd8882020-03-10 12:19:08 +01003695#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003696 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003697 if( handshake->ecrs_peer_cert != NULL )
3698 {
3699 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3700 mbedtls_free( handshake->ecrs_peer_cert );
3701 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003702#endif
3703
Hanno Becker75173122019-02-06 16:18:31 +00003704#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3705 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3706 mbedtls_pk_free( &handshake->peer_pubkey );
3707#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3708
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003709#if defined(MBEDTLS_SSL_CLI_C) && \
3710 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3711 mbedtls_free( handshake->cookie );
3712#endif /* MBEDTLS_SSL_CLI_C &&
3713 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003714
3715#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003716 mbedtls_ssl_flight_free( handshake->flight );
3717 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003718#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003719
Ronald Cronf12b81d2022-03-15 10:42:41 +01003720#if defined(MBEDTLS_ECDH_C) && \
3721 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003722 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003723 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003724#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3725
Ronald Cron6f135e12021-12-08 16:57:54 +01003726#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003727 mbedtls_ssl_transform_free( handshake->transform_handshake );
3728 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003729 mbedtls_free( handshake->transform_earlydata );
3730 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003731#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003732
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003733
3734#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3735 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3736 * processes datagrams and the fact that a datagram is allowed to have
3737 * several records in it, it is possible that the I/O buffers are not
3738 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003739 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3740 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003741#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003742
Jerry Yuba9c7272021-10-30 11:54:10 +08003743 /* mbedtls_platform_zeroize MUST be last one in this function */
3744 mbedtls_platform_zeroize( handshake,
3745 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003746}
3747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003749{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003750 if( session == NULL )
3751 return;
3752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003754 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003755#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003756
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003757#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003759#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003760
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003761#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3762 mbedtls_free( session->hostname );
3763#endif
3764
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003765 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003766}
3767
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003768#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003769
3770#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3771#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3772#else
3773#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3774#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3775
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003776#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003777
3778#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3779#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3780#else
3781#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3782#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3783
3784#if defined(MBEDTLS_SSL_ALPN)
3785#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3786#else
3787#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3788#endif /* MBEDTLS_SSL_ALPN */
3789
3790#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3791#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3792#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3793#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3794
3795#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3796 ( (uint32_t) ( \
3797 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3798 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3799 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3800 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3801 0u ) )
3802
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003803static unsigned char ssl_serialized_context_header[] = {
3804 MBEDTLS_VERSION_MAJOR,
3805 MBEDTLS_VERSION_MINOR,
3806 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003807 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3808 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3809 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3810 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3811 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003812};
3813
Paul Bakker5121ce52009-01-03 21:22:43 +00003814/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003815 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003816 *
3817 * The format of the serialized data is:
3818 * (in the presentation language of TLS, RFC 8446 section 3)
3819 *
3820 * // header
3821 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003822 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003823 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003824 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003825 * Note: When updating the format, remember to keep these
3826 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003827 *
3828 * // session sub-structure
3829 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3830 * // transform sub-structure
3831 * uint8 random[64]; // ServerHello.random+ClientHello.random
3832 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3833 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3834 * // fields from ssl_context
3835 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3836 * uint64 in_window_top; // DTLS: last validated record seq_num
3837 * uint64 in_window; // DTLS: bitmask for replay protection
3838 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3839 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3840 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3841 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3842 *
3843 * Note that many fields of the ssl_context or sub-structures are not
3844 * serialized, as they fall in one of the following categories:
3845 *
3846 * 1. forced value (eg in_left must be 0)
3847 * 2. pointer to dynamically-allocated memory (eg session, transform)
3848 * 3. value can be re-derived from other data (eg session keys from MS)
3849 * 4. value was temporary (eg content of input buffer)
3850 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003851 */
3852int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3853 unsigned char *buf,
3854 size_t buf_len,
3855 size_t *olen )
3856{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003857 unsigned char *p = buf;
3858 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003859 size_t session_len;
3860 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003861
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003862 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003863 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3864 * this function's documentation.
3865 *
3866 * These are due to assumptions/limitations in the implementation. Some of
3867 * them are likely to stay (no handshake in progress) some might go away
3868 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003869 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003870 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003871 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003872 {
3873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003875 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003876 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003877 {
3878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003880 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003881 /* Double-check that sub-structures are indeed ready */
3882 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003883 {
3884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003886 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003887 /* There must be no pending incoming or outgoing data */
3888 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003889 {
3890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003892 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003893 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003894 {
3895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003897 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003898 /* Protocol must be DLTS, not TLS */
3899 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003900 {
3901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003903 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003904 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003905 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003906 {
3907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003909 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003910 /* We must be using an AEAD ciphersuite */
3911 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003912 {
3913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003915 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003916 /* Renegotiation must not be enabled */
3917#if defined(MBEDTLS_SSL_RENEGOTIATION)
3918 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003919 {
3920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003922 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003923#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003924
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003925 /*
3926 * Version and format identifier
3927 */
3928 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003929
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003930 if( used <= buf_len )
3931 {
3932 memcpy( p, ssl_serialized_context_header,
3933 sizeof( ssl_serialized_context_header ) );
3934 p += sizeof( ssl_serialized_context_header );
3935 }
3936
3937 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003938 * Session (length + data)
3939 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003940 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003941 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3942 return( ret );
3943
3944 used += 4 + session_len;
3945 if( used <= buf_len )
3946 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003947 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3948 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003949
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003950 ret = ssl_session_save( ssl->session, 1,
3951 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003952 if( ret != 0 )
3953 return( ret );
3954
3955 p += session_len;
3956 }
3957
3958 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003959 * Transform
3960 */
3961 used += sizeof( ssl->transform->randbytes );
3962 if( used <= buf_len )
3963 {
3964 memcpy( p, ssl->transform->randbytes,
3965 sizeof( ssl->transform->randbytes ) );
3966 p += sizeof( ssl->transform->randbytes );
3967 }
3968
3969#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3970 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3971 if( used <= buf_len )
3972 {
3973 *p++ = ssl->transform->in_cid_len;
3974 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3975 p += ssl->transform->in_cid_len;
3976
3977 *p++ = ssl->transform->out_cid_len;
3978 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3979 p += ssl->transform->out_cid_len;
3980 }
3981#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3982
3983 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003984 * Saved fields from top-level ssl_context structure
3985 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003986 used += 4;
3987 if( used <= buf_len )
3988 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003989 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3990 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003991 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003992
3993#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3994 used += 16;
3995 if( used <= buf_len )
3996 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003997 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3998 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003999
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004000 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4001 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004002 }
4003#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4004
4005#if defined(MBEDTLS_SSL_PROTO_DTLS)
4006 used += 1;
4007 if( used <= buf_len )
4008 {
4009 *p++ = ssl->disable_datagram_packing;
4010 }
4011#endif /* MBEDTLS_SSL_PROTO_DTLS */
4012
Jerry Yuae0b2e22021-10-08 15:21:19 +08004013 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004014 if( used <= buf_len )
4015 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004016 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4017 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004018 }
4019
4020#if defined(MBEDTLS_SSL_PROTO_DTLS)
4021 used += 2;
4022 if( used <= buf_len )
4023 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004024 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4025 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004026 }
4027#endif /* MBEDTLS_SSL_PROTO_DTLS */
4028
4029#if defined(MBEDTLS_SSL_ALPN)
4030 {
4031 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004032 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004033 : 0;
4034
4035 used += 1 + alpn_len;
4036 if( used <= buf_len )
4037 {
4038 *p++ = alpn_len;
4039
4040 if( ssl->alpn_chosen != NULL )
4041 {
4042 memcpy( p, ssl->alpn_chosen, alpn_len );
4043 p += alpn_len;
4044 }
4045 }
4046 }
4047#endif /* MBEDTLS_SSL_ALPN */
4048
4049 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004050 * Done
4051 */
4052 *olen = used;
4053
4054 if( used > buf_len )
4055 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004056
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004057 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4058
Hanno Becker43aefe22020-02-05 10:44:56 +00004059 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004060}
4061
4062/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004063 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004064 *
4065 * This internal version is wrapped by a public function that cleans up in
4066 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004067 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004068MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004069static int ssl_context_load( mbedtls_ssl_context *ssl,
4070 const unsigned char *buf,
4071 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004072{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004073 const unsigned char *p = buf;
4074 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004075 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004077
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004078 /*
4079 * The context should have been freshly setup or reset.
4080 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004081 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004082 * renegotiating, or if the user mistakenly loaded a session first.)
4083 */
4084 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4085 ssl->session != NULL )
4086 {
4087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4088 }
4089
4090 /*
4091 * We can't check that the config matches the initial one, but we can at
4092 * least check it matches the requirements for serializing.
4093 */
4094 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004095 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4096 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004097#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004098 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004099#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004100 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004101 {
4102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4103 }
4104
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004105 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4106
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004107 /*
4108 * Check version identifier
4109 */
4110 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4112
4113 if( memcmp( p, ssl_serialized_context_header,
4114 sizeof( ssl_serialized_context_header ) ) != 0 )
4115 {
4116 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4117 }
4118 p += sizeof( ssl_serialized_context_header );
4119
4120 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004121 * Session
4122 */
4123 if( (size_t)( end - p ) < 4 )
4124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4125
4126 session_len = ( (size_t) p[0] << 24 ) |
4127 ( (size_t) p[1] << 16 ) |
4128 ( (size_t) p[2] << 8 ) |
4129 ( (size_t) p[3] );
4130 p += 4;
4131
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004132 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004133 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004134 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004135 ssl->session_in = ssl->session;
4136 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004137 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004138
4139 if( (size_t)( end - p ) < session_len )
4140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4141
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004142 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004143 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004144 {
4145 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004146 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004147 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004148
4149 p += session_len;
4150
4151 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004152 * Transform
4153 */
4154
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004155 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004156 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004157 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004158 ssl->transform_in = ssl->transform;
4159 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004160 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004161
4162 /* Read random bytes and populate structure */
4163 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004165#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004166 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004167 ssl->session->ciphersuite,
4168 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004169#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004170 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004171#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004172 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4173 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004174 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004175 ssl->conf->endpoint,
4176 ssl );
4177 if( ret != 0 )
4178 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004179#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004180 p += sizeof( ssl->transform->randbytes );
4181
4182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4183 /* Read connection IDs and store them */
4184 if( (size_t)( end - p ) < 1 )
4185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4186
4187 ssl->transform->in_cid_len = *p++;
4188
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004189 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4191
4192 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4193 p += ssl->transform->in_cid_len;
4194
4195 ssl->transform->out_cid_len = *p++;
4196
4197 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4199
4200 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4201 p += ssl->transform->out_cid_len;
4202#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4203
4204 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004205 * Saved fields from top-level ssl_context structure
4206 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004207 if( (size_t)( end - p ) < 4 )
4208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4209
4210 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4211 ( (uint32_t) p[1] << 16 ) |
4212 ( (uint32_t) p[2] << 8 ) |
4213 ( (uint32_t) p[3] );
4214 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004215
4216#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4217 if( (size_t)( end - p ) < 16 )
4218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4219
4220 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4221 ( (uint64_t) p[1] << 48 ) |
4222 ( (uint64_t) p[2] << 40 ) |
4223 ( (uint64_t) p[3] << 32 ) |
4224 ( (uint64_t) p[4] << 24 ) |
4225 ( (uint64_t) p[5] << 16 ) |
4226 ( (uint64_t) p[6] << 8 ) |
4227 ( (uint64_t) p[7] );
4228 p += 8;
4229
4230 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4231 ( (uint64_t) p[1] << 48 ) |
4232 ( (uint64_t) p[2] << 40 ) |
4233 ( (uint64_t) p[3] << 32 ) |
4234 ( (uint64_t) p[4] << 24 ) |
4235 ( (uint64_t) p[5] << 16 ) |
4236 ( (uint64_t) p[6] << 8 ) |
4237 ( (uint64_t) p[7] );
4238 p += 8;
4239#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4240
4241#if defined(MBEDTLS_SSL_PROTO_DTLS)
4242 if( (size_t)( end - p ) < 1 )
4243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4244
4245 ssl->disable_datagram_packing = *p++;
4246#endif /* MBEDTLS_SSL_PROTO_DTLS */
4247
Jerry Yud9a94fe2021-09-28 18:58:59 +08004248 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004250 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4251 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004252
4253#if defined(MBEDTLS_SSL_PROTO_DTLS)
4254 if( (size_t)( end - p ) < 2 )
4255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4256
4257 ssl->mtu = ( p[0] << 8 ) | p[1];
4258 p += 2;
4259#endif /* MBEDTLS_SSL_PROTO_DTLS */
4260
4261#if defined(MBEDTLS_SSL_ALPN)
4262 {
4263 uint8_t alpn_len;
4264 const char **cur;
4265
4266 if( (size_t)( end - p ) < 1 )
4267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4268
4269 alpn_len = *p++;
4270
4271 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4272 {
4273 /* alpn_chosen should point to an item in the configured list */
4274 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4275 {
4276 if( strlen( *cur ) == alpn_len &&
4277 memcmp( p, cur, alpn_len ) == 0 )
4278 {
4279 ssl->alpn_chosen = *cur;
4280 break;
4281 }
4282 }
4283 }
4284
4285 /* can only happen on conf mismatch */
4286 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4288
4289 p += alpn_len;
4290 }
4291#endif /* MBEDTLS_SSL_ALPN */
4292
4293 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004294 * Forced fields from top-level ssl_context structure
4295 *
4296 * Most of them already set to the correct value by mbedtls_ssl_init() and
4297 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4298 */
4299 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004300 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004301
Hanno Becker361b10d2019-08-30 10:42:49 +01004302 /* Adjust pointers for header fields of outgoing records to
4303 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004304 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004305
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004306#if defined(MBEDTLS_SSL_PROTO_DTLS)
4307 ssl->in_epoch = 1;
4308#endif
4309
4310 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4311 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004312 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4313 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004314 if( ssl->handshake != NULL )
4315 {
4316 mbedtls_ssl_handshake_free( ssl );
4317 mbedtls_free( ssl->handshake );
4318 ssl->handshake = NULL;
4319 }
4320
4321 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004322 * Done - should have consumed entire buffer
4323 */
4324 if( p != end )
4325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004326
4327 return( 0 );
4328}
4329
4330/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004331 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004332 */
4333int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4334 const unsigned char *buf,
4335 size_t len )
4336{
4337 int ret = ssl_context_load( context, buf, len );
4338
4339 if( ret != 0 )
4340 mbedtls_ssl_free( context );
4341
4342 return( ret );
4343}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004344#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004345
4346/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 * Free an SSL context
4348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004350{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004351 if( ssl == NULL )
4352 return;
4353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004355
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004356 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004357 {
sander-visserb8aa2072020-05-06 22:05:13 +02004358#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4359 size_t out_buf_len = ssl->out_buf_len;
4360#else
4361 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4362#endif
4363
Darryl Greenb33cc762019-11-28 14:29:44 +00004364 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004366 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 }
4368
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004369 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 {
sander-visserb8aa2072020-05-06 22:05:13 +02004371#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4372 size_t in_buf_len = ssl->in_buf_len;
4373#else
4374 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4375#endif
4376
Darryl Greenb33cc762019-11-28 14:29:44 +00004377 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004379 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004380 }
4381
Paul Bakker48916f92012-09-16 19:57:18 +00004382 if( ssl->transform )
4383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 mbedtls_ssl_transform_free( ssl->transform );
4385 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004386 }
4387
4388 if( ssl->handshake )
4389 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004390 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4392 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 mbedtls_free( ssl->handshake );
4395 mbedtls_free( ssl->transform_negotiate );
4396 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004397 }
4398
Ronald Cron6f135e12021-12-08 16:57:54 +01004399#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004400 mbedtls_ssl_transform_free( ssl->transform_application );
4401 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004402#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004403
Paul Bakkerc0463502013-02-14 11:19:38 +01004404 if( ssl->session )
4405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 mbedtls_ssl_session_free( ssl->session );
4407 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004408 }
4409
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004410#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004411 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004412 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004413 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004416#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004417
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004418#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004420#endif
4421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004423
Paul Bakker86f04f42013-02-14 11:20:09 +01004424 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004425 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004426}
4427
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004428/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004429 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004430 */
4431void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4432{
4433 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4434}
4435
Gilles Peskineae270bf2021-06-02 00:05:29 +02004436/* The selection should be the same as mbedtls_x509_crt_profile_default in
4437 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004438 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004439 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004440 * about this list.
4441 */
Brett Warrene0edc842021-08-17 09:53:13 +01004442static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004443#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004444 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004445#endif
4446#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004447 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004448#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004449#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004450 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004451#endif
4452#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004453 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004454#endif
4455#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004456 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004457#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004458#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004459 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004460#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004461#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004462 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004463#endif
4464#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004465 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004466#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004467 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004468};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004469
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004470static int ssl_preset_suiteb_ciphersuites[] = {
4471 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4472 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4473 0
4474};
4475
Gilles Peskineeccd8882020-03-10 12:19:08 +01004476#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004477
Jerry Yu909df7b2022-01-22 11:56:27 +08004478/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004479 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004480 * rules SHOULD be upheld.
4481 * - No duplicate entries.
4482 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004483 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004484 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004485 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004486static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004487
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#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 +08004489 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4490 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004491#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004492 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004493
Andrzej Kurek25f27152022-08-17 16:09:31 -04004494#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 +08004495 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4496 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004497#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004498 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4499
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#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 +08004501 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4502 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004503#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004504 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004505
Andrzej Kurek25f27152022-08-17 16:09:31 -04004506#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 +08004507 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#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 +08004509
Andrzej Kurek25f27152022-08-17 16:09:31 -04004510#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 +08004511 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004512#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 +08004513
Andrzej Kurek25f27152022-08-17 16:09:31 -04004514#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 +08004515 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004516#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 +08004517
Andrzej Kurek25f27152022-08-17 16:09:31 -04004518#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 +08004519 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4520#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4521
Andrzej Kurek25f27152022-08-17 16:09:31 -04004522#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 +08004523 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4524#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4525
Andrzej Kurek25f27152022-08-17 16:09:31 -04004526#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 +08004527 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4528#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4529
Gabor Mezei15b95a62022-05-09 16:37:58 +02004530 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004531};
4532
4533/* NOTICE: see above */
4534#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4535static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004536#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004537#if defined(MBEDTLS_ECDSA_C)
4538 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004539#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004540#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4541 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4542#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004543#if defined(MBEDTLS_RSA_C)
4544 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4545#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004546#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004547#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004548#if defined(MBEDTLS_ECDSA_C)
4549 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004550#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004551#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4552 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4553#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004554#if defined(MBEDTLS_RSA_C)
4555 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4556#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004557#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004558#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004559#if defined(MBEDTLS_ECDSA_C)
4560 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004561#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004562#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4563 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4564#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004565#if defined(MBEDTLS_RSA_C)
4566 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4567#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004568#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004569 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004570};
4571#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4572/* NOTICE: see above */
4573static uint16_t ssl_preset_suiteb_sig_algs[] = {
4574
Andrzej Kurek25f27152022-08-17 16:09:31 -04004575#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 +08004576 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4577 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004578#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004579 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4580
Andrzej Kurek25f27152022-08-17 16:09:31 -04004581#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 +08004582 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4583 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004584#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004585 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004586
Andrzej Kurek25f27152022-08-17 16:09:31 -04004587#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 +08004588 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004589#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 +08004590
Andrzej Kurek25f27152022-08-17 16:09:31 -04004591#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 +08004592 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004593#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004594
Gabor Mezei15b95a62022-05-09 16:37:58 +02004595 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004596};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004597
Jerry Yu909df7b2022-01-22 11:56:27 +08004598/* NOTICE: see above */
4599#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4600static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004601#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004602#if defined(MBEDTLS_ECDSA_C)
4603 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004604#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004605#if defined(MBEDTLS_RSA_C)
4606 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4607#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004608#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004609#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004610#if defined(MBEDTLS_ECDSA_C)
4611 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004612#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004613#if defined(MBEDTLS_RSA_C)
4614 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4615#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004616#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004617 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004618};
Jerry Yu909df7b2022-01-22 11:56:27 +08004619#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4620
Jerry Yu1a8b4812022-01-20 17:56:50 +08004621#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004622
Brett Warrene0edc842021-08-17 09:53:13 +01004623static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004624#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004625 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004626#endif
4627#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004628 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004629#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004630 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004631};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004632
Jerry Yu1a8b4812022-01-20 17:56:50 +08004633#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004634/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004635 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004636MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004637static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004638{
4639 size_t i, j;
4640 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004641
Gabor Mezei15b95a62022-05-09 16:37:58 +02004642 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004643 {
Jerry Yuf377d642022-01-25 10:43:59 +08004644 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004645 {
Jerry Yuf377d642022-01-25 10:43:59 +08004646 if( sig_algs[i] != sig_algs[j] )
4647 continue;
4648 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4649 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4650 sig_algs[i], j, i );
4651 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004652 }
4653 }
4654 return( ret );
4655}
4656
4657#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4658
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004659/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004660 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004661 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004662int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004663 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004664{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004665#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004667#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004668
Jerry Yu1a8b4812022-01-20 17:56:50 +08004669#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004670 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004671 {
4672 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4673 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4674 }
4675
Jerry Yuf377d642022-01-25 10:43:59 +08004676 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004677 {
4678 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4679 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4680 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004681
4682#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004683 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004684 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004685 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004686 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4687 }
4688
Jerry Yuf377d642022-01-25 10:43:59 +08004689 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004690 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004691 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004692 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4693 }
4694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004695#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4696
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004697 /* Use the functions here so that they are covered in tests,
4698 * but otherwise access member directly for efficiency */
4699 mbedtls_ssl_conf_endpoint( conf, endpoint );
4700 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004701
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004702 /*
4703 * Things that are common to all presets
4704 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004705#if defined(MBEDTLS_SSL_CLI_C)
4706 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4707 {
4708 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4709#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4710 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4711#endif
4712 }
4713#endif
4714
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004715#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4716 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4717#endif
4718
4719#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4720 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4721#endif
4722
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004723#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004724 conf->f_cookie_write = ssl_cookie_write_dummy;
4725 conf->f_cookie_check = ssl_cookie_check_dummy;
4726#endif
4727
4728#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4729 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4730#endif
4731
Janos Follath088ce432017-04-10 12:42:31 +01004732#if defined(MBEDTLS_SSL_SRV_C)
4733 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004734 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004735#endif
4736
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004737#if defined(MBEDTLS_SSL_PROTO_DTLS)
4738 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4739 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4740#endif
4741
4742#if defined(MBEDTLS_SSL_RENEGOTIATION)
4743 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004744 memset( conf->renego_period, 0x00, 2 );
4745 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004746#endif
4747
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004748#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004749 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4750 {
4751 const unsigned char dhm_p[] =
4752 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4753 const unsigned char dhm_g[] =
4754 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004755
Hanno Beckere2defad2021-07-24 05:59:17 +01004756 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4757 dhm_p, sizeof( dhm_p ),
4758 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4759 {
4760 return( ret );
4761 }
4762 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004763#endif
4764
Ronald Cron6f135e12021-12-08 16:57:54 +01004765#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004766#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004767 mbedtls_ssl_conf_new_session_tickets(
4768 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4769#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004770 /*
4771 * Allow all TLS 1.3 key exchange modes by default.
4772 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004773 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004774#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004775
XiaokangQian4d3a6042022-04-21 13:46:17 +00004776 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4777 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004778#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004779 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004780 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004781#else
XiaokangQian060d8672022-04-21 09:24:56 +00004782 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004783#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004784 }
4785 else
4786 {
4787#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4788 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4789 {
4790 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4791 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4792 }
4793 else
4794 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4795 {
4796 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4797 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4798 }
4799#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4800 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4801 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4802#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4803 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4804 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4805#else
4806 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4807#endif
4808 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004809
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004810 /*
4811 * Preset-specific defaults
4812 */
4813 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004814 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004815 /*
4816 * NSA Suite B
4817 */
4818 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004819
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004820 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004821
4822#if defined(MBEDTLS_X509_CRT_PARSE_C)
4823 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004824#endif
4825
Gilles Peskineeccd8882020-03-10 12:19:08 +01004826#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004827#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4828 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4829 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4830 else
4831#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4832 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004833#endif
4834
Brett Warrene0edc842021-08-17 09:53:13 +01004835#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4836 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004837#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004838 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004839 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004840
4841 /*
4842 * Default
4843 */
4844 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004845
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004846 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004847
4848#if defined(MBEDTLS_X509_CRT_PARSE_C)
4849 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4850#endif
4851
Gilles Peskineeccd8882020-03-10 12:19:08 +01004852#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004853#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4854 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4855 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4856 else
4857#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4858 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004859#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004860
Brett Warrene0edc842021-08-17 09:53:13 +01004861#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4862 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004863#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004864 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004865
4866#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4867 conf->dhm_min_bitlen = 1024;
4868#endif
4869 }
4870
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004871 return( 0 );
4872}
4873
4874/*
4875 * Free mbedtls_ssl_config
4876 */
4877void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4878{
4879#if defined(MBEDTLS_DHM_C)
4880 mbedtls_mpi_free( &conf->dhm_P );
4881 mbedtls_mpi_free( &conf->dhm_G );
4882#endif
4883
Gilles Peskineeccd8882020-03-10 12:19:08 +01004884#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004885#if defined(MBEDTLS_USE_PSA_CRYPTO)
4886 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4887 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004888 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4889 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004890#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004891 if( conf->psk != NULL )
4892 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004893 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004894 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004895 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004896 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004897 }
4898
4899 if( conf->psk_identity != NULL )
4900 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004901 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004902 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004903 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004904 conf->psk_identity_len = 0;
4905 }
4906#endif
4907
4908#if defined(MBEDTLS_X509_CRT_PARSE_C)
4909 ssl_key_cert_free( conf->key_cert );
4910#endif
4911
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004912 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004913}
4914
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004915#if defined(MBEDTLS_PK_C) && \
4916 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004917/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922#if defined(MBEDTLS_RSA_C)
4923 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4924 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926#if defined(MBEDTLS_ECDSA_C)
4927 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4928 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004931}
4932
Hanno Becker7e5437a2017-04-28 17:15:26 +01004933unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4934{
4935 switch( type ) {
4936 case MBEDTLS_PK_RSA:
4937 return( MBEDTLS_SSL_SIG_RSA );
4938 case MBEDTLS_PK_ECDSA:
4939 case MBEDTLS_PK_ECKEY:
4940 return( MBEDTLS_SSL_SIG_ECDSA );
4941 default:
4942 return( MBEDTLS_SSL_SIG_ANON );
4943 }
4944}
4945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004947{
4948 switch( sig )
4949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950#if defined(MBEDTLS_RSA_C)
4951 case MBEDTLS_SSL_SIG_RSA:
4952 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#if defined(MBEDTLS_ECDSA_C)
4955 case MBEDTLS_SSL_SIG_ECDSA:
4956 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004957#endif
4958 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004960 }
4961}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004962#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004963
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004964/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004965 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004968{
4969 switch( hash )
4970 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004971#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 case MBEDTLS_SSL_HASH_MD5:
4973 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004974#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004975#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 case MBEDTLS_SSL_HASH_SHA1:
4977 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004978#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004979#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 case MBEDTLS_SSL_HASH_SHA224:
4981 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004982#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004983#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 case MBEDTLS_SSL_HASH_SHA256:
4985 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004986#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004987#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 case MBEDTLS_SSL_HASH_SHA384:
4989 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004990#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004991#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992 case MBEDTLS_SSL_HASH_SHA512:
4993 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004994#endif
4995 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004997 }
4998}
4999
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005000/*
5001 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5002 */
5003unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5004{
5005 switch( md )
5006 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005007#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005008 case MBEDTLS_MD_MD5:
5009 return( MBEDTLS_SSL_HASH_MD5 );
5010#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005011#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005012 case MBEDTLS_MD_SHA1:
5013 return( MBEDTLS_SSL_HASH_SHA1 );
5014#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005015#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005016 case MBEDTLS_MD_SHA224:
5017 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005018#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005019#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005020 case MBEDTLS_MD_SHA256:
5021 return( MBEDTLS_SSL_HASH_SHA256 );
5022#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005023#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005024 case MBEDTLS_MD_SHA384:
5025 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005026#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005027#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005028 case MBEDTLS_MD_SHA512:
5029 return( MBEDTLS_SSL_HASH_SHA512 );
5030#endif
5031 default:
5032 return( MBEDTLS_SSL_HASH_NONE );
5033 }
5034}
5035
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005036/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005037 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005038 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005039 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005040int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005041{
Brett Warrene0edc842021-08-17 09:53:13 +01005042 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005043
Brett Warrene0edc842021-08-17 09:53:13 +01005044 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005045 return( -1 );
5046
Brett Warrene0edc842021-08-17 09:53:13 +01005047 for( ; *group_list != 0; group_list++ )
5048 {
5049 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005050 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005051 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005052
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005053 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005054}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005055
5056#if defined(MBEDTLS_ECP_C)
5057/*
5058 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5059 */
5060int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5061{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005062 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005063 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005064
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005065 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005066 return -1;
5067
5068 uint16_t tls_id = grp_info->tls_id;
5069
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005070 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5071}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005072#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074#if defined(MBEDTLS_X509_CRT_PARSE_C)
5075int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5076 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005077 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005078 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005079{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005080 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005081 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005082 const char *ext_oid;
5083 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005086 {
5087 /* Server part of the key exchange */
5088 switch( ciphersuite->key_exchange )
5089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 case MBEDTLS_KEY_EXCHANGE_RSA:
5091 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005092 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005093 break;
5094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5096 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5097 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5098 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005099 break;
5100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5102 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005103 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005104 break;
5105
5106 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 case MBEDTLS_KEY_EXCHANGE_NONE:
5108 case MBEDTLS_KEY_EXCHANGE_PSK:
5109 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5110 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005111 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005112 usage = 0;
5113 }
5114 }
5115 else
5116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5118 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005119 }
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005122 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005123 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005124 ret = -1;
5125 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5130 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005131 }
5132 else
5133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5135 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005136 }
5137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005139 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005140 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005141 ret = -1;
5142 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005143
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005144 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005147
Jerry Yu148165c2021-09-24 23:20:59 +08005148#if defined(MBEDTLS_USE_PSA_CRYPTO)
5149int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5150 const mbedtls_md_type_t md,
5151 unsigned char *dst,
5152 size_t dst_len,
5153 size_t *olen )
5154{
Ronald Cronf6893e12022-01-07 22:09:01 +01005155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5156 psa_hash_operation_t *hash_operation_to_clone;
5157 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5158
Jerry Yu148165c2021-09-24 23:20:59 +08005159 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005160
5161 switch( md )
5162 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005163#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005164 case MBEDTLS_MD_SHA384:
5165 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5166 break;
5167#endif
5168
Andrzej Kurek25f27152022-08-17 16:09:31 -04005169#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005170 case MBEDTLS_MD_SHA256:
5171 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5172 break;
5173#endif
5174
5175 default:
5176 goto exit;
5177 }
5178
5179 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5180 if( status != PSA_SUCCESS )
5181 goto exit;
5182
5183 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5184 if( status != PSA_SUCCESS )
5185 goto exit;
5186
5187exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005188 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005189}
5190#else /* MBEDTLS_USE_PSA_CRYPTO */
5191
Andrzej Kurek25f27152022-08-17 16:09:31 -04005192#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005193MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005194static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5195 unsigned char *dst,
5196 size_t dst_len,
5197 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005198{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005199 int ret;
5200 mbedtls_sha512_context sha512;
5201
5202 if( dst_len < 48 )
5203 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5204
5205 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005206 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005207
5208 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5209 {
5210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5211 goto exit;
5212 }
5213
5214 *olen = 48;
5215
5216exit:
5217
5218 mbedtls_sha512_free( &sha512 );
5219 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005220}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005221#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005222
Andrzej Kurek25f27152022-08-17 16:09:31 -04005223#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005224MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005225static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5226 unsigned char *dst,
5227 size_t dst_len,
5228 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005229{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005230 int ret;
5231 mbedtls_sha256_context sha256;
5232
5233 if( dst_len < 32 )
5234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5235
5236 mbedtls_sha256_init( &sha256 );
5237 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005238
Jerry Yu24c0ec32021-09-09 14:21:07 +08005239 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5240 {
5241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5242 goto exit;
5243 }
5244
5245 *olen = 32;
5246
5247exit:
5248
5249 mbedtls_sha256_free( &sha256 );
5250 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005251}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005252#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005253
Jerry Yu000f9762021-09-14 11:12:51 +08005254int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5255 const mbedtls_md_type_t md,
5256 unsigned char *dst,
5257 size_t dst_len,
5258 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005259{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005260 switch( md )
5261 {
5262
Andrzej Kurek25f27152022-08-17 16:09:31 -04005263#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005264 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005265 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005266#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005267
Andrzej Kurek25f27152022-08-17 16:09:31 -04005268#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005269 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005270 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005271#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005272
5273 default:
5274 break;
5275 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005277}
XiaokangQian647719a2021-12-07 09:16:29 +00005278
Jerry Yu148165c2021-09-24 23:20:59 +08005279#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005280
Gabor Mezei078e8032022-04-27 21:17:56 +02005281#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5282/* mbedtls_ssl_parse_sig_alg_ext()
5283 *
5284 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5285 * value (TLS 1.3 RFC8446):
5286 * enum {
5287 * ....
5288 * ecdsa_secp256r1_sha256( 0x0403 ),
5289 * ecdsa_secp384r1_sha384( 0x0503 ),
5290 * ecdsa_secp521r1_sha512( 0x0603 ),
5291 * ....
5292 * } SignatureScheme;
5293 *
5294 * struct {
5295 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5296 * } SignatureSchemeList;
5297 *
5298 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5299 * value (TLS 1.2 RFC5246):
5300 * enum {
5301 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5302 * sha512(6), (255)
5303 * } HashAlgorithm;
5304 *
5305 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5306 * SignatureAlgorithm;
5307 *
5308 * struct {
5309 * HashAlgorithm hash;
5310 * SignatureAlgorithm signature;
5311 * } SignatureAndHashAlgorithm;
5312 *
5313 * SignatureAndHashAlgorithm
5314 * supported_signature_algorithms<2..2^16-2>;
5315 *
5316 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5317 * generalization of the TLS 1.2 signature algorithm extension.
5318 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5319 * `SignatureScheme` field of TLS 1.3
5320 *
5321 */
5322int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5323 const unsigned char *buf,
5324 const unsigned char *end )
5325{
5326 const unsigned char *p = buf;
5327 size_t supported_sig_algs_len = 0;
5328 const unsigned char *supported_sig_algs_end;
5329 uint16_t sig_alg;
5330 uint32_t common_idx = 0;
5331
5332 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5333 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5334 p += 2;
5335
5336 memset( ssl->handshake->received_sig_algs, 0,
5337 sizeof(ssl->handshake->received_sig_algs) );
5338
5339 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5340 supported_sig_algs_end = p + supported_sig_algs_len;
5341 while( p < supported_sig_algs_end )
5342 {
5343 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5344 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5345 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005346 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5347 sig_alg,
5348 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005349#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005350 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005351 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5352 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5353 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005354 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005355 }
5356#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005357
Jerry Yuf3b46b52022-06-19 16:52:27 +08005358 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5359 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005360
5361 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5362 {
5363 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5364 common_idx += 1;
5365 }
5366 }
5367 /* Check that we consumed all the message. */
5368 if( p != end )
5369 {
5370 MBEDTLS_SSL_DEBUG_MSG( 1,
5371 ( "Signature algorithms extension length misaligned" ) );
5372 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5373 MBEDTLS_ERR_SSL_DECODE_ERROR );
5374 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5375 }
5376
5377 if( common_idx == 0 )
5378 {
5379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5380 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5381 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5382 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5383 }
5384
Gabor Mezei15b95a62022-05-09 16:37:58 +02005385 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005386 return( 0 );
5387}
5388
5389#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5390
Jerry Yudc7bd172022-02-17 13:44:15 +08005391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5392
5393#if defined(MBEDTLS_USE_PSA_CRYPTO)
5394
5395static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5396 mbedtls_svc_key_id_t key,
5397 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005398 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005399 const unsigned char* seed, size_t seed_length,
5400 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005401 const unsigned char* other_secret,
5402 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005403 size_t capacity )
5404{
5405 psa_status_t status;
5406
5407 status = psa_key_derivation_setup( derivation, alg );
5408 if( status != PSA_SUCCESS )
5409 return( status );
5410
5411 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5412 {
5413 status = psa_key_derivation_input_bytes( derivation,
5414 PSA_KEY_DERIVATION_INPUT_SEED,
5415 seed, seed_length );
5416 if( status != PSA_SUCCESS )
5417 return( status );
5418
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005419 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005420 {
5421 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005422 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5423 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005424 if( status != PSA_SUCCESS )
5425 return( status );
5426 }
5427
Jerry Yudc7bd172022-02-17 13:44:15 +08005428 if( mbedtls_svc_key_id_is_null( key ) )
5429 {
5430 status = psa_key_derivation_input_bytes(
5431 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005432 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005433 }
5434 else
5435 {
5436 status = psa_key_derivation_input_key(
5437 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5438 }
5439 if( status != PSA_SUCCESS )
5440 return( status );
5441
5442 status = psa_key_derivation_input_bytes( derivation,
5443 PSA_KEY_DERIVATION_INPUT_LABEL,
5444 label, label_length );
5445 if( status != PSA_SUCCESS )
5446 return( status );
5447 }
5448 else
5449 {
5450 return( PSA_ERROR_NOT_SUPPORTED );
5451 }
5452
5453 status = psa_key_derivation_set_capacity( derivation, capacity );
5454 if( status != PSA_SUCCESS )
5455 return( status );
5456
5457 return( PSA_SUCCESS );
5458}
5459
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005460MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005461static int tls_prf_generic( mbedtls_md_type_t md_type,
5462 const unsigned char *secret, size_t slen,
5463 const char *label,
5464 const unsigned char *random, size_t rlen,
5465 unsigned char *dstbuf, size_t dlen )
5466{
5467 psa_status_t status;
5468 psa_algorithm_t alg;
5469 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5470 psa_key_derivation_operation_t derivation =
5471 PSA_KEY_DERIVATION_OPERATION_INIT;
5472
5473 if( md_type == MBEDTLS_MD_SHA384 )
5474 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5475 else
5476 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5477
5478 /* Normally a "secret" should be long enough to be impossible to
5479 * find by brute force, and in particular should not be empty. But
5480 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5481 * and for this use case it makes sense to have a 0-length "secret".
5482 * Since the key API doesn't allow importing a key of length 0,
5483 * keep master_key=0, which setup_psa_key_derivation() understands
5484 * to mean a 0-length "secret" input. */
5485 if( slen != 0 )
5486 {
5487 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5488 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5489 psa_set_key_algorithm( &key_attributes, alg );
5490 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5491
5492 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5493 if( status != PSA_SUCCESS )
5494 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5495 }
5496
5497 status = setup_psa_key_derivation( &derivation,
5498 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005499 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005500 random, rlen,
5501 (unsigned char const *) label,
5502 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005503 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005504 dlen );
5505 if( status != PSA_SUCCESS )
5506 {
5507 psa_key_derivation_abort( &derivation );
5508 psa_destroy_key( master_key );
5509 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5510 }
5511
5512 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5513 if( status != PSA_SUCCESS )
5514 {
5515 psa_key_derivation_abort( &derivation );
5516 psa_destroy_key( master_key );
5517 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5518 }
5519
5520 status = psa_key_derivation_abort( &derivation );
5521 if( status != PSA_SUCCESS )
5522 {
5523 psa_destroy_key( master_key );
5524 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5525 }
5526
5527 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5528 status = psa_destroy_key( master_key );
5529 if( status != PSA_SUCCESS )
5530 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5531
5532 return( 0 );
5533}
5534
5535#else /* MBEDTLS_USE_PSA_CRYPTO */
5536
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005537MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005538static int tls_prf_generic( mbedtls_md_type_t md_type,
5539 const unsigned char *secret, size_t slen,
5540 const char *label,
5541 const unsigned char *random, size_t rlen,
5542 unsigned char *dstbuf, size_t dlen )
5543{
5544 size_t nb;
5545 size_t i, j, k, md_len;
5546 unsigned char *tmp;
5547 size_t tmp_len = 0;
5548 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5549 const mbedtls_md_info_t *md_info;
5550 mbedtls_md_context_t md_ctx;
5551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5552
5553 mbedtls_md_init( &md_ctx );
5554
5555 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5557
5558 md_len = mbedtls_md_get_size( md_info );
5559
5560 tmp_len = md_len + strlen( label ) + rlen;
5561 tmp = mbedtls_calloc( 1, tmp_len );
5562 if( tmp == NULL )
5563 {
5564 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5565 goto exit;
5566 }
5567
5568 nb = strlen( label );
5569 memcpy( tmp + md_len, label, nb );
5570 memcpy( tmp + md_len + nb, random, rlen );
5571 nb += rlen;
5572
5573 /*
5574 * Compute P_<hash>(secret, label + random)[0..dlen]
5575 */
5576 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5577 goto exit;
5578
5579 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5580 if( ret != 0 )
5581 goto exit;
5582 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5583 if( ret != 0 )
5584 goto exit;
5585 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5586 if( ret != 0 )
5587 goto exit;
5588
5589 for( i = 0; i < dlen; i += md_len )
5590 {
5591 ret = mbedtls_md_hmac_reset ( &md_ctx );
5592 if( ret != 0 )
5593 goto exit;
5594 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5595 if( ret != 0 )
5596 goto exit;
5597 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5598 if( ret != 0 )
5599 goto exit;
5600
5601 ret = mbedtls_md_hmac_reset ( &md_ctx );
5602 if( ret != 0 )
5603 goto exit;
5604 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5605 if( ret != 0 )
5606 goto exit;
5607 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5608 if( ret != 0 )
5609 goto exit;
5610
5611 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5612
5613 for( j = 0; j < k; j++ )
5614 dstbuf[i + j] = h_i[j];
5615 }
5616
5617exit:
5618 mbedtls_md_free( &md_ctx );
5619
5620 mbedtls_platform_zeroize( tmp, tmp_len );
5621 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5622
5623 mbedtls_free( tmp );
5624
5625 return( ret );
5626}
5627#endif /* MBEDTLS_USE_PSA_CRYPTO */
5628
Andrzej Kurek25f27152022-08-17 16:09:31 -04005629#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005630MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005631static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5632 const char *label,
5633 const unsigned char *random, size_t rlen,
5634 unsigned char *dstbuf, size_t dlen )
5635{
5636 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5637 label, random, rlen, dstbuf, dlen ) );
5638}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005639#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005640
Andrzej Kurek25f27152022-08-17 16:09:31 -04005641#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005642MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005643static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5644 const char *label,
5645 const unsigned char *random, size_t rlen,
5646 unsigned char *dstbuf, size_t dlen )
5647{
5648 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5649 label, random, rlen, dstbuf, dlen ) );
5650}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005651#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005652
Jerry Yuf009d862022-02-17 14:01:37 +08005653/*
5654 * Set appropriate PRF function and other SSL / TLS1.2 functions
5655 *
5656 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005657 * - hash associated with the ciphersuite (only used by TLS 1.2)
5658 *
5659 * Outputs:
5660 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5661 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005662MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005663static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005664 mbedtls_md_type_t hash )
5665{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005666#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005667 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005668 {
5669 handshake->tls_prf = tls_prf_sha384;
5670 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5671 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5672 }
5673 else
5674#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005675#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005676 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005677 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005678 handshake->tls_prf = tls_prf_sha256;
5679 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5680 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5681 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005682#else
Jerry Yuf009d862022-02-17 14:01:37 +08005683 {
Jerry Yuf009d862022-02-17 14:01:37 +08005684 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005685 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005686 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5687 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005688#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005689
5690 return( 0 );
5691}
Jerry Yud6ab2352022-02-17 14:03:43 +08005692
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005693/*
5694 * Compute master secret if needed
5695 *
5696 * Parameters:
5697 * [in/out] handshake
5698 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5699 * (PSA-PSK) ciphersuite_info, psk_opaque
5700 * [out] premaster (cleared)
5701 * [out] master
5702 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5703 * debug: conf->f_dbg, conf->p_dbg
5704 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005705 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005706 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005707MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005708static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5709 unsigned char *master,
5710 const mbedtls_ssl_context *ssl )
5711{
5712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5713
5714 /* cf. RFC 5246, Section 8.1:
5715 * "The master secret is always exactly 48 bytes in length." */
5716 size_t const master_secret_len = 48;
5717
5718#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5719 unsigned char session_hash[48];
5720#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5721
5722 /* The label for the KDF used for key expansion.
5723 * This is either "master secret" or "extended master secret"
5724 * depending on whether the Extended Master Secret extension
5725 * is used. */
5726 char const *lbl = "master secret";
5727
Przemek Stekielae4ed302022-04-05 17:15:55 +02005728 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005729 * - If the Extended Master Secret extension is not used,
5730 * this is ClientHello.Random + ServerHello.Random
5731 * (see Sect. 8.1 in RFC 5246).
5732 * - If the Extended Master Secret extension is used,
5733 * this is the transcript of the handshake so far.
5734 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005735 unsigned char const *seed = handshake->randbytes;
5736 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005737
5738#if !defined(MBEDTLS_DEBUG_C) && \
5739 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5740 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5741 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5742 ssl = NULL; /* make sure we don't use it except for those cases */
5743 (void) ssl;
5744#endif
5745
5746 if( handshake->resume != 0 )
5747 {
5748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5749 return( 0 );
5750 }
5751
5752#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5753 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5754 {
5755 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005756 seed = session_hash;
5757 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005758
5759 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005760 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005761 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005762#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005763
Przemek Stekiel99114f32022-04-22 11:20:09 +02005764#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005765 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005766 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005767 {
5768 /* Perform PSK-to-MS expansion in a single step. */
5769 psa_status_t status;
5770 psa_algorithm_t alg;
5771 mbedtls_svc_key_id_t psk;
5772 psa_key_derivation_operation_t derivation =
5773 PSA_KEY_DERIVATION_OPERATION_INIT;
5774 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5775
5776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5777
5778 psk = mbedtls_ssl_get_opaque_psk( ssl );
5779
5780 if( hash_alg == MBEDTLS_MD_SHA384 )
5781 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5782 else
5783 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5784
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005785 size_t other_secret_len = 0;
5786 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005787
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005788 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005789 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005790 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005791 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005792 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005793 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005794 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005795 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005796 other_secret_len = 48;
5797 other_secret = handshake->premaster + 2;
5798 break;
5799 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005800 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005801 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5802 other_secret = handshake->premaster + 2;
5803 break;
5804 default:
5805 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005806 }
5807
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005808 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005809 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005810 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005811 (unsigned char const *) lbl,
5812 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005813 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005814 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_output_bytes( &derivation,
5822 master,
5823 master_secret_len );
5824 if( status != PSA_SUCCESS )
5825 {
5826 psa_key_derivation_abort( &derivation );
5827 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5828 }
5829
5830 status = psa_key_derivation_abort( &derivation );
5831 if( status != PSA_SUCCESS )
5832 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5833 }
5834 else
5835#endif
5836 {
5837 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005838 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005839 master,
5840 master_secret_len );
5841 if( ret != 0 )
5842 {
5843 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5844 return( ret );
5845 }
5846
5847 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5848 handshake->premaster,
5849 handshake->pmslen );
5850
5851 mbedtls_platform_zeroize( handshake->premaster,
5852 sizeof(handshake->premaster) );
5853 }
5854
5855 return( 0 );
5856}
5857
Jerry Yud62f87e2022-02-17 14:09:02 +08005858int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5859{
5860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5861 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5862 ssl->handshake->ciphersuite_info;
5863
5864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5865
5866 /* Set PRF, calc_verify and calc_finished function pointers */
5867 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005868 ciphersuite_info->mac );
5869 if( ret != 0 )
5870 {
5871 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5872 return( ret );
5873 }
5874
5875 /* Compute master secret if needed */
5876 ret = ssl_compute_master( ssl->handshake,
5877 ssl->session_negotiate->master,
5878 ssl );
5879 if( ret != 0 )
5880 {
5881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5882 return( ret );
5883 }
5884
5885 /* Swap the client and server random values:
5886 * - MS derivation wanted client+server (RFC 5246 8.1)
5887 * - key derivation wants server+client (RFC 5246 6.3) */
5888 {
5889 unsigned char tmp[64];
5890 memcpy( tmp, ssl->handshake->randbytes, 64 );
5891 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5892 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5893 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5894 }
5895
5896 /* Populate transform structure */
5897 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5898 ssl->session_negotiate->ciphersuite,
5899 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005900#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005901 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005902#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005903 ssl->handshake->tls_prf,
5904 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005905 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005906 ssl->conf->endpoint,
5907 ssl );
5908 if( ret != 0 )
5909 {
5910 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5911 return( ret );
5912 }
5913
5914 /* We no longer need Server/ClientHello.random values */
5915 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5916 sizeof( ssl->handshake->randbytes ) );
5917
5918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5919
5920 return( 0 );
5921}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005922
Ronald Cron4dcbca92022-03-07 10:21:40 +01005923int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5924{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005925 switch( md )
5926 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005927#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005928 case MBEDTLS_SSL_HASH_SHA384:
5929 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5930 break;
5931#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005932#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005933 case MBEDTLS_SSL_HASH_SHA256:
5934 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5935 break;
5936#endif
5937 default:
5938 return( -1 );
5939 }
5940
Ronald Cronc2f13a02022-03-07 10:25:24 +01005941 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005942}
5943
Andrzej Kurek25f27152022-08-17 16:09:31 -04005944#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005945void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5946 unsigned char *hash,
5947 size_t *hlen )
5948{
5949#if defined(MBEDTLS_USE_PSA_CRYPTO)
5950 size_t hash_size;
5951 psa_status_t status;
5952 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5953
5954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5955 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5956 if( status != PSA_SUCCESS )
5957 {
5958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5959 return;
5960 }
5961
5962 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5963 if( status != PSA_SUCCESS )
5964 {
5965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5966 return;
5967 }
5968
5969 *hlen = 32;
5970 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5972#else
5973 mbedtls_sha256_context sha256;
5974
5975 mbedtls_sha256_init( &sha256 );
5976
5977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5978
5979 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5980 mbedtls_sha256_finish( &sha256, hash );
5981
5982 *hlen = 32;
5983
5984 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5986
5987 mbedtls_sha256_free( &sha256 );
5988#endif /* MBEDTLS_USE_PSA_CRYPTO */
5989 return;
5990}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005991#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005992
Andrzej Kurek25f27152022-08-17 16:09:31 -04005993#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005994void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5995 unsigned char *hash,
5996 size_t *hlen )
5997{
5998#if defined(MBEDTLS_USE_PSA_CRYPTO)
5999 size_t hash_size;
6000 psa_status_t status;
6001 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6002
6003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6004 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6005 if( status != PSA_SUCCESS )
6006 {
6007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6008 return;
6009 }
6010
6011 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6012 if( status != PSA_SUCCESS )
6013 {
6014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6015 return;
6016 }
6017
6018 *hlen = 48;
6019 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6021#else
6022 mbedtls_sha512_context sha512;
6023
6024 mbedtls_sha512_init( &sha512 );
6025
6026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6027
Andrzej Kureka242e832022-08-11 10:03:14 -04006028 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006029 mbedtls_sha512_finish( &sha512, hash );
6030
6031 *hlen = 48;
6032
6033 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6035
6036 mbedtls_sha512_free( &sha512 );
6037#endif /* MBEDTLS_USE_PSA_CRYPTO */
6038 return;
6039}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006040#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006041
Neil Armstrong80f6f322022-05-03 17:56:38 +02006042#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6043 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006044int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6045{
6046 unsigned char *p = ssl->handshake->premaster;
6047 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6048 const unsigned char *psk = NULL;
6049 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006050 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006051
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006052 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006053 {
6054 /*
6055 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006056 * checked before calling this function.
6057 *
6058 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006059 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006060 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006061 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6062 {
6063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6065 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006066 }
6067
6068 /*
6069 * PMS = struct {
6070 * opaque other_secret<0..2^16-1>;
6071 * opaque psk<0..2^16-1>;
6072 * };
6073 * with "other_secret" depending on the particular key exchange
6074 */
6075#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6076 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6077 {
6078 if( end - p < 2 )
6079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6080
6081 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6082 p += 2;
6083
6084 if( end < p || (size_t)( end - p ) < psk_len )
6085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6086
6087 memset( p, 0, psk_len );
6088 p += psk_len;
6089 }
6090 else
6091#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6092#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6093 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6094 {
6095 /*
6096 * other_secret already set by the ClientKeyExchange message,
6097 * and is 48 bytes long
6098 */
6099 if( end - p < 2 )
6100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6101
6102 *p++ = 0;
6103 *p++ = 48;
6104 p += 48;
6105 }
6106 else
6107#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6108#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6109 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6110 {
6111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6112 size_t len;
6113
6114 /* Write length only when we know the actual value */
6115 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6116 p + 2, end - ( p + 2 ), &len,
6117 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6118 {
6119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6120 return( ret );
6121 }
6122 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6123 p += 2 + len;
6124
6125 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6126 }
6127 else
6128#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006129#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006130 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6131 {
6132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6133 size_t zlen;
6134
6135 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6136 p + 2, end - ( p + 2 ),
6137 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6138 {
6139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6140 return( ret );
6141 }
6142
6143 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6144 p += 2 + zlen;
6145
6146 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6147 MBEDTLS_DEBUG_ECDH_Z );
6148 }
6149 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006150#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006151 {
6152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6154 }
6155
6156 /* opaque psk<0..2^16-1>; */
6157 if( end - p < 2 )
6158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6159
6160 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6161 p += 2;
6162
6163 if( end < p || (size_t)( end - p ) < psk_len )
6164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6165
6166 memcpy( p, psk, psk_len );
6167 p += psk_len;
6168
6169 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6170
6171 return( 0 );
6172}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006173#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006174
6175#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006176MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006177static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6178
6179#if defined(MBEDTLS_SSL_PROTO_DTLS)
6180int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6181{
6182 /* If renegotiation is not enforced, retransmit until we would reach max
6183 * timeout if we were using the usual handshake doubling scheme */
6184 if( ssl->conf->renego_max_records < 0 )
6185 {
6186 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6187 unsigned char doublings = 1;
6188
6189 while( ratio != 0 )
6190 {
6191 ++doublings;
6192 ratio >>= 1;
6193 }
6194
6195 if( ++ssl->renego_records_seen > doublings )
6196 {
6197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6198 return( 0 );
6199 }
6200 }
6201
6202 return( ssl_write_hello_request( ssl ) );
6203}
6204#endif
6205#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006206
Jerry Yud9526692022-02-17 14:23:47 +08006207/*
6208 * Handshake functions
6209 */
6210#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6211/* No certificate support -> dummy functions */
6212int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6213{
6214 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6215 ssl->handshake->ciphersuite_info;
6216
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6218
6219 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6220 {
6221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6222 ssl->state++;
6223 return( 0 );
6224 }
6225
6226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6228}
6229
6230int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6231{
6232 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6233 ssl->handshake->ciphersuite_info;
6234
6235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6236
6237 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6238 {
6239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6240 ssl->state++;
6241 return( 0 );
6242 }
6243
6244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6246}
6247
6248#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6249/* Some certificate support -> implement write and parse */
6250
6251int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6252{
6253 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6254 size_t i, n;
6255 const mbedtls_x509_crt *crt;
6256 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6257 ssl->handshake->ciphersuite_info;
6258
6259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6260
6261 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6262 {
6263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6264 ssl->state++;
6265 return( 0 );
6266 }
6267
6268#if defined(MBEDTLS_SSL_CLI_C)
6269 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6270 {
6271 if( ssl->handshake->client_auth == 0 )
6272 {
6273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6274 ssl->state++;
6275 return( 0 );
6276 }
6277 }
6278#endif /* MBEDTLS_SSL_CLI_C */
6279#if defined(MBEDTLS_SSL_SRV_C)
6280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6281 {
6282 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6283 {
6284 /* Should never happen because we shouldn't have picked the
6285 * ciphersuite if we don't have a certificate. */
6286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6287 }
6288 }
6289#endif
6290
6291 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6292
6293 /*
6294 * 0 . 0 handshake type
6295 * 1 . 3 handshake length
6296 * 4 . 6 length of all certs
6297 * 7 . 9 length of cert. 1
6298 * 10 . n-1 peer certificate
6299 * n . n+2 length of cert. 2
6300 * n+3 . ... upper level cert, etc.
6301 */
6302 i = 7;
6303 crt = mbedtls_ssl_own_cert( ssl );
6304
6305 while( crt != NULL )
6306 {
6307 n = crt->raw.len;
6308 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6309 {
6310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6311 " > %" MBEDTLS_PRINTF_SIZET,
6312 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6313 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6314 }
6315
6316 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6317 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6318 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6319
6320 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6321 i += n; crt = crt->next;
6322 }
6323
6324 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6325 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6326 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6327
6328 ssl->out_msglen = i;
6329 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6330 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6331
6332 ssl->state++;
6333
6334 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6335 {
6336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6337 return( ret );
6338 }
6339
6340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6341
6342 return( ret );
6343}
6344
6345#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6346
6347#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006348MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006349static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6350 unsigned char *crt_buf,
6351 size_t crt_buf_len )
6352{
6353 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6354
6355 if( peer_crt == NULL )
6356 return( -1 );
6357
6358 if( peer_crt->raw.len != crt_buf_len )
6359 return( -1 );
6360
6361 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6362}
6363#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006364MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006365static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6366 unsigned char *crt_buf,
6367 size_t crt_buf_len )
6368{
6369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6370 unsigned char const * const peer_cert_digest =
6371 ssl->session->peer_cert_digest;
6372 mbedtls_md_type_t const peer_cert_digest_type =
6373 ssl->session->peer_cert_digest_type;
6374 mbedtls_md_info_t const * const digest_info =
6375 mbedtls_md_info_from_type( peer_cert_digest_type );
6376 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6377 size_t digest_len;
6378
6379 if( peer_cert_digest == NULL || digest_info == NULL )
6380 return( -1 );
6381
6382 digest_len = mbedtls_md_get_size( digest_info );
6383 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6384 return( -1 );
6385
6386 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6387 if( ret != 0 )
6388 return( -1 );
6389
6390 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6391}
6392#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6393#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6394
6395/*
6396 * Once the certificate message is read, parse it into a cert chain and
6397 * perform basic checks, but leave actual verification to the caller
6398 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006399MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006400static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6401 mbedtls_x509_crt *chain )
6402{
6403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6404#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6405 int crt_cnt=0;
6406#endif
6407 size_t i, n;
6408 uint8_t alert;
6409
6410 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6411 {
6412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6413 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6414 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6415 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6416 }
6417
6418 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6419 {
6420 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6421 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6422 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6423 }
6424
6425 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6426 {
6427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6428 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6429 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6430 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6431 }
6432
6433 i = mbedtls_ssl_hs_hdr_len( ssl );
6434
6435 /*
6436 * Same message structure as in mbedtls_ssl_write_certificate()
6437 */
6438 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6439
6440 if( ssl->in_msg[i] != 0 ||
6441 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6442 {
6443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6444 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6445 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6446 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6447 }
6448
6449 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6450 i += 3;
6451
6452 /* Iterate through and parse the CRTs in the provided chain. */
6453 while( i < ssl->in_hslen )
6454 {
6455 /* Check that there's room for the next CRT's length fields. */
6456 if ( i + 3 > ssl->in_hslen ) {
6457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6458 mbedtls_ssl_send_alert_message( ssl,
6459 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6460 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6461 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6462 }
6463 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6464 * anything beyond 2**16 ~ 64K. */
6465 if( ssl->in_msg[i] != 0 )
6466 {
6467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6468 mbedtls_ssl_send_alert_message( ssl,
6469 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6470 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6471 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6472 }
6473
6474 /* Read length of the next CRT in the chain. */
6475 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6476 | (unsigned int) ssl->in_msg[i + 2];
6477 i += 3;
6478
6479 if( n < 128 || i + n > ssl->in_hslen )
6480 {
6481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6482 mbedtls_ssl_send_alert_message( ssl,
6483 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6484 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6485 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6486 }
6487
6488 /* Check if we're handling the first CRT in the chain. */
6489#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6490 if( crt_cnt++ == 0 &&
6491 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6492 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6493 {
6494 /* During client-side renegotiation, check that the server's
6495 * end-CRTs hasn't changed compared to the initial handshake,
6496 * mitigating the triple handshake attack. On success, reuse
6497 * the original end-CRT instead of parsing it again. */
6498 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6499 if( ssl_check_peer_crt_unchanged( ssl,
6500 &ssl->in_msg[i],
6501 n ) != 0 )
6502 {
6503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6504 mbedtls_ssl_send_alert_message( ssl,
6505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6506 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6507 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6508 }
6509
6510 /* Now we can safely free the original chain. */
6511 ssl_clear_peer_cert( ssl->session );
6512 }
6513#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6514
6515 /* Parse the next certificate in the chain. */
6516#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6517 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6518#else
6519 /* If we don't need to store the CRT chain permanently, parse
6520 * it in-place from the input buffer instead of making a copy. */
6521 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6522#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6523 switch( ret )
6524 {
6525 case 0: /*ok*/
6526 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6527 /* Ignore certificate with an unknown algorithm: maybe a
6528 prior certificate was already trusted. */
6529 break;
6530
6531 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6532 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6533 goto crt_parse_der_failed;
6534
6535 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6536 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6537 goto crt_parse_der_failed;
6538
6539 default:
6540 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6541 crt_parse_der_failed:
6542 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6543 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6544 return( ret );
6545 }
6546
6547 i += n;
6548 }
6549
6550 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6551 return( 0 );
6552}
6553
6554#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006555MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006556static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6557{
6558 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6559 return( -1 );
6560
6561 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6562 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6563 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6564 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6565 {
Ronald Cron19385882022-06-15 16:26:13 +02006566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006567 return( 0 );
6568 }
6569 return( -1 );
6570}
6571#endif /* MBEDTLS_SSL_SRV_C */
6572
6573/* Check if a certificate message is expected.
6574 * Return either
6575 * - SSL_CERTIFICATE_EXPECTED, or
6576 * - SSL_CERTIFICATE_SKIP
6577 * indicating whether a Certificate message is expected or not.
6578 */
6579#define SSL_CERTIFICATE_EXPECTED 0
6580#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006581MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006582static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6583 int authmode )
6584{
6585 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6586 ssl->handshake->ciphersuite_info;
6587
6588 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6589 return( SSL_CERTIFICATE_SKIP );
6590
6591#if defined(MBEDTLS_SSL_SRV_C)
6592 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6593 {
6594 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6595 return( SSL_CERTIFICATE_SKIP );
6596
6597 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6598 {
6599 ssl->session_negotiate->verify_result =
6600 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6601 return( SSL_CERTIFICATE_SKIP );
6602 }
6603 }
6604#else
6605 ((void) authmode);
6606#endif /* MBEDTLS_SSL_SRV_C */
6607
6608 return( SSL_CERTIFICATE_EXPECTED );
6609}
6610
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006611MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006612static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6613 int authmode,
6614 mbedtls_x509_crt *chain,
6615 void *rs_ctx )
6616{
6617 int ret = 0;
6618 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6619 ssl->handshake->ciphersuite_info;
6620 int have_ca_chain = 0;
6621
6622 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6623 void *p_vrfy;
6624
6625 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6626 return( 0 );
6627
6628 if( ssl->f_vrfy != NULL )
6629 {
6630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6631 f_vrfy = ssl->f_vrfy;
6632 p_vrfy = ssl->p_vrfy;
6633 }
6634 else
6635 {
6636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6637 f_vrfy = ssl->conf->f_vrfy;
6638 p_vrfy = ssl->conf->p_vrfy;
6639 }
6640
6641 /*
6642 * Main check: verify certificate
6643 */
6644#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6645 if( ssl->conf->f_ca_cb != NULL )
6646 {
6647 ((void) rs_ctx);
6648 have_ca_chain = 1;
6649
6650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6651 ret = mbedtls_x509_crt_verify_with_ca_cb(
6652 chain,
6653 ssl->conf->f_ca_cb,
6654 ssl->conf->p_ca_cb,
6655 ssl->conf->cert_profile,
6656 ssl->hostname,
6657 &ssl->session_negotiate->verify_result,
6658 f_vrfy, p_vrfy );
6659 }
6660 else
6661#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6662 {
6663 mbedtls_x509_crt *ca_chain;
6664 mbedtls_x509_crl *ca_crl;
6665
6666#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6667 if( ssl->handshake->sni_ca_chain != NULL )
6668 {
6669 ca_chain = ssl->handshake->sni_ca_chain;
6670 ca_crl = ssl->handshake->sni_ca_crl;
6671 }
6672 else
6673#endif
6674 {
6675 ca_chain = ssl->conf->ca_chain;
6676 ca_crl = ssl->conf->ca_crl;
6677 }
6678
6679 if( ca_chain != NULL )
6680 have_ca_chain = 1;
6681
6682 ret = mbedtls_x509_crt_verify_restartable(
6683 chain,
6684 ca_chain, ca_crl,
6685 ssl->conf->cert_profile,
6686 ssl->hostname,
6687 &ssl->session_negotiate->verify_result,
6688 f_vrfy, p_vrfy, rs_ctx );
6689 }
6690
6691 if( ret != 0 )
6692 {
6693 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6694 }
6695
6696#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6697 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6698 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6699#endif
6700
6701 /*
6702 * Secondary checks: always done, but change 'ret' only if it was 0
6703 */
6704
6705#if defined(MBEDTLS_ECP_C)
6706 {
6707 const mbedtls_pk_context *pk = &chain->pk;
6708
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006709 /* If certificate uses an EC key, make sure the curve is OK.
6710 * This is a public key, so it can't be opaque, so can_do() is a good
6711 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006712 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006713 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006714 /* and in the unlikely case the above assumption no longer holds
6715 * we are making sure that pk_ec() here does not return a NULL
6716 */
6717 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6718 if( ec == NULL )
6719 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6722 }
Jerry Yud9526692022-02-17 14:23:47 +08006723
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006724 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6725 {
6726 ssl->session_negotiate->verify_result |=
6727 MBEDTLS_X509_BADCERT_BAD_KEY;
6728
6729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6730 if( ret == 0 )
6731 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6732 }
Jerry Yud9526692022-02-17 14:23:47 +08006733 }
6734 }
6735#endif /* MBEDTLS_ECP_C */
6736
6737 if( mbedtls_ssl_check_cert_usage( chain,
6738 ciphersuite_info,
6739 ! ssl->conf->endpoint,
6740 &ssl->session_negotiate->verify_result ) != 0 )
6741 {
6742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6743 if( ret == 0 )
6744 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6745 }
6746
6747 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6748 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6749 * with details encoded in the verification flags. All other kinds
6750 * of error codes, including those from the user provided f_vrfy
6751 * functions, are treated as fatal and lead to a failure of
6752 * ssl_parse_certificate even if verification was optional. */
6753 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6754 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6755 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6756 {
6757 ret = 0;
6758 }
6759
6760 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6761 {
6762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6763 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6764 }
6765
6766 if( ret != 0 )
6767 {
6768 uint8_t alert;
6769
6770 /* The certificate may have been rejected for several reasons.
6771 Pick one and send the corresponding alert. Which alert to send
6772 may be a subject of debate in some cases. */
6773 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6774 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6775 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6776 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6777 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6778 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6779 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6780 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6781 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6782 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6783 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6784 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6785 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6786 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6787 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6788 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6789 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6790 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6791 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6792 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6793 else
6794 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6795 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6796 alert );
6797 }
6798
6799#if defined(MBEDTLS_DEBUG_C)
6800 if( ssl->session_negotiate->verify_result != 0 )
6801 {
6802 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6803 (unsigned int) ssl->session_negotiate->verify_result ) );
6804 }
6805 else
6806 {
6807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6808 }
6809#endif /* MBEDTLS_DEBUG_C */
6810
6811 return( ret );
6812}
6813
6814#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006815MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006816static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6817 unsigned char *start, size_t len )
6818{
6819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6820 /* Remember digest of the peer's end-CRT. */
6821 ssl->session_negotiate->peer_cert_digest =
6822 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6823 if( ssl->session_negotiate->peer_cert_digest == NULL )
6824 {
6825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6826 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6827 mbedtls_ssl_send_alert_message( ssl,
6828 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6829 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6830
6831 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6832 }
6833
6834 ret = mbedtls_md( mbedtls_md_info_from_type(
6835 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6836 start, len,
6837 ssl->session_negotiate->peer_cert_digest );
6838
6839 ssl->session_negotiate->peer_cert_digest_type =
6840 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6841 ssl->session_negotiate->peer_cert_digest_len =
6842 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6843
6844 return( ret );
6845}
6846
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006847MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006848static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6849 unsigned char *start, size_t len )
6850{
6851 unsigned char *end = start + len;
6852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6853
6854 /* Make a copy of the peer's raw public key. */
6855 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6856 ret = mbedtls_pk_parse_subpubkey( &start, end,
6857 &ssl->handshake->peer_pubkey );
6858 if( ret != 0 )
6859 {
6860 /* We should have parsed the public key before. */
6861 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6862 }
6863
6864 return( 0 );
6865}
6866#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6867
6868int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6869{
6870 int ret = 0;
6871 int crt_expected;
6872#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6873 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6874 ? ssl->handshake->sni_authmode
6875 : ssl->conf->authmode;
6876#else
6877 const int authmode = ssl->conf->authmode;
6878#endif
6879 void *rs_ctx = NULL;
6880 mbedtls_x509_crt *chain = NULL;
6881
6882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6883
6884 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6885 if( crt_expected == SSL_CERTIFICATE_SKIP )
6886 {
6887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6888 goto exit;
6889 }
6890
6891#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6892 if( ssl->handshake->ecrs_enabled &&
6893 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6894 {
6895 chain = ssl->handshake->ecrs_peer_cert;
6896 ssl->handshake->ecrs_peer_cert = NULL;
6897 goto crt_verify;
6898 }
6899#endif
6900
6901 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6902 {
6903 /* mbedtls_ssl_read_record may have sent an alert already. We
6904 let it decide whether to alert. */
6905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6906 goto exit;
6907 }
6908
6909#if defined(MBEDTLS_SSL_SRV_C)
6910 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6911 {
6912 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6913
6914 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6915 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6916
6917 goto exit;
6918 }
6919#endif /* MBEDTLS_SSL_SRV_C */
6920
6921 /* Clear existing peer CRT structure in case we tried to
6922 * reuse a session but it failed, and allocate a new one. */
6923 ssl_clear_peer_cert( ssl->session_negotiate );
6924
6925 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6926 if( chain == NULL )
6927 {
6928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6929 sizeof( mbedtls_x509_crt ) ) );
6930 mbedtls_ssl_send_alert_message( ssl,
6931 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6932 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6933
6934 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6935 goto exit;
6936 }
6937 mbedtls_x509_crt_init( chain );
6938
6939 ret = ssl_parse_certificate_chain( ssl, chain );
6940 if( ret != 0 )
6941 goto exit;
6942
6943#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6944 if( ssl->handshake->ecrs_enabled)
6945 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6946
6947crt_verify:
6948 if( ssl->handshake->ecrs_enabled)
6949 rs_ctx = &ssl->handshake->ecrs_ctx;
6950#endif
6951
6952 ret = ssl_parse_certificate_verify( ssl, authmode,
6953 chain, rs_ctx );
6954 if( ret != 0 )
6955 goto exit;
6956
6957#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6958 {
6959 unsigned char *crt_start, *pk_start;
6960 size_t crt_len, pk_len;
6961
6962 /* We parse the CRT chain without copying, so
6963 * these pointers point into the input buffer,
6964 * and are hence still valid after freeing the
6965 * CRT chain. */
6966
6967 crt_start = chain->raw.p;
6968 crt_len = chain->raw.len;
6969
6970 pk_start = chain->pk_raw.p;
6971 pk_len = chain->pk_raw.len;
6972
6973 /* Free the CRT structures before computing
6974 * digest and copying the peer's public key. */
6975 mbedtls_x509_crt_free( chain );
6976 mbedtls_free( chain );
6977 chain = NULL;
6978
6979 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6980 if( ret != 0 )
6981 goto exit;
6982
6983 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6984 if( ret != 0 )
6985 goto exit;
6986 }
6987#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6988 /* Pass ownership to session structure. */
6989 ssl->session_negotiate->peer_cert = chain;
6990 chain = NULL;
6991#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6992
6993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6994
6995exit:
6996
6997 if( ret == 0 )
6998 ssl->state++;
6999
7000#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7001 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7002 {
7003 ssl->handshake->ecrs_peer_cert = chain;
7004 chain = NULL;
7005 }
7006#endif
7007
7008 if( chain != NULL )
7009 {
7010 mbedtls_x509_crt_free( chain );
7011 mbedtls_free( chain );
7012 }
7013
7014 return( ret );
7015}
7016#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7017
Andrzej Kurek25f27152022-08-17 16:09:31 -04007018#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007019static void ssl_calc_finished_tls_sha256(
7020 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7021{
7022 int len = 12;
7023 const char *sender;
7024 unsigned char padbuf[32];
7025#if defined(MBEDTLS_USE_PSA_CRYPTO)
7026 size_t hash_size;
7027 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7028 psa_status_t status;
7029#else
7030 mbedtls_sha256_context sha256;
7031#endif
7032
7033 mbedtls_ssl_session *session = ssl->session_negotiate;
7034 if( !session )
7035 session = ssl->session;
7036
7037 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7038 ? "client finished"
7039 : "server finished";
7040
7041#if defined(MBEDTLS_USE_PSA_CRYPTO)
7042 sha256_psa = psa_hash_operation_init();
7043
7044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7045
7046 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7047 if( status != PSA_SUCCESS )
7048 {
7049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7050 return;
7051 }
7052
7053 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7054 if( status != PSA_SUCCESS )
7055 {
7056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7057 return;
7058 }
7059 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7060#else
7061
7062 mbedtls_sha256_init( &sha256 );
7063
7064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7065
7066 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7067
7068 /*
7069 * TLSv1.2:
7070 * hash = PRF( master, finished_label,
7071 * Hash( handshake ) )[0.11]
7072 */
7073
7074#if !defined(MBEDTLS_SHA256_ALT)
7075 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7076 sha256.state, sizeof( sha256.state ) );
7077#endif
7078
7079 mbedtls_sha256_finish( &sha256, padbuf );
7080 mbedtls_sha256_free( &sha256 );
7081#endif /* MBEDTLS_USE_PSA_CRYPTO */
7082
7083 ssl->handshake->tls_prf( session->master, 48, sender,
7084 padbuf, 32, buf, len );
7085
7086 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7087
7088 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7089
7090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7091}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007092#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007093
Jerry Yub7ba49e2022-02-17 14:25:53 +08007094
Andrzej Kurek25f27152022-08-17 16:09:31 -04007095#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007096static void ssl_calc_finished_tls_sha384(
7097 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7098{
7099 int len = 12;
7100 const char *sender;
7101 unsigned char padbuf[48];
7102#if defined(MBEDTLS_USE_PSA_CRYPTO)
7103 size_t hash_size;
7104 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7105 psa_status_t status;
7106#else
7107 mbedtls_sha512_context sha512;
7108#endif
7109
7110 mbedtls_ssl_session *session = ssl->session_negotiate;
7111 if( !session )
7112 session = ssl->session;
7113
7114 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7115 ? "client finished"
7116 : "server finished";
7117
7118#if defined(MBEDTLS_USE_PSA_CRYPTO)
7119 sha384_psa = psa_hash_operation_init();
7120
7121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7122
7123 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7124 if( status != PSA_SUCCESS )
7125 {
7126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7127 return;
7128 }
7129
7130 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7131 if( status != PSA_SUCCESS )
7132 {
7133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7134 return;
7135 }
7136 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7137#else
7138 mbedtls_sha512_init( &sha512 );
7139
7140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7141
Andrzej Kureka242e832022-08-11 10:03:14 -04007142 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007143
7144 /*
7145 * TLSv1.2:
7146 * hash = PRF( master, finished_label,
7147 * Hash( handshake ) )[0.11]
7148 */
7149
7150#if !defined(MBEDTLS_SHA512_ALT)
7151 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7152 sha512.state, sizeof( sha512.state ) );
7153#endif
7154 mbedtls_sha512_finish( &sha512, padbuf );
7155
7156 mbedtls_sha512_free( &sha512 );
7157#endif
7158
7159 ssl->handshake->tls_prf( session->master, 48, sender,
7160 padbuf, 48, buf, len );
7161
7162 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7163
7164 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7165
7166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7167}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007168#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007169
Jerry Yuaef00152022-02-17 14:27:31 +08007170void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7171{
7172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7173
7174 /*
7175 * Free our handshake params
7176 */
7177 mbedtls_ssl_handshake_free( ssl );
7178 mbedtls_free( ssl->handshake );
7179 ssl->handshake = NULL;
7180
7181 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007182 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007183 */
7184 if( ssl->transform )
7185 {
7186 mbedtls_ssl_transform_free( ssl->transform );
7187 mbedtls_free( ssl->transform );
7188 }
7189 ssl->transform = ssl->transform_negotiate;
7190 ssl->transform_negotiate = NULL;
7191
7192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7193}
7194
Jerry Yu2a9fff52022-02-17 14:28:51 +08007195void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7196{
7197 int resume = ssl->handshake->resume;
7198
7199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7200
7201#if defined(MBEDTLS_SSL_RENEGOTIATION)
7202 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7203 {
7204 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7205 ssl->renego_records_seen = 0;
7206 }
7207#endif
7208
7209 /*
7210 * Free the previous session and switch in the current one
7211 */
7212 if( ssl->session )
7213 {
7214#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7215 /* RFC 7366 3.1: keep the EtM state */
7216 ssl->session_negotiate->encrypt_then_mac =
7217 ssl->session->encrypt_then_mac;
7218#endif
7219
7220 mbedtls_ssl_session_free( ssl->session );
7221 mbedtls_free( ssl->session );
7222 }
7223 ssl->session = ssl->session_negotiate;
7224 ssl->session_negotiate = NULL;
7225
7226 /*
7227 * Add cache entry
7228 */
7229 if( ssl->conf->f_set_cache != NULL &&
7230 ssl->session->id_len != 0 &&
7231 resume == 0 )
7232 {
7233 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7234 ssl->session->id,
7235 ssl->session->id_len,
7236 ssl->session ) != 0 )
7237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7238 }
7239
7240#if defined(MBEDTLS_SSL_PROTO_DTLS)
7241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7242 ssl->handshake->flight != NULL )
7243 {
7244 /* Cancel handshake timer */
7245 mbedtls_ssl_set_timer( ssl, 0 );
7246
7247 /* Keep last flight around in case we need to resend it:
7248 * we need the handshake and transform structures for that */
7249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7250 }
7251 else
7252#endif
7253 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7254
7255 ssl->state++;
7256
7257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7258}
7259
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007260int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7261{
7262 int ret, hash_len;
7263
7264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7265
7266 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7267
7268 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7269
7270 /*
7271 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7272 * may define some other value. Currently (early 2016), no defined
7273 * ciphersuite does this (and this is unlikely to change as activity has
7274 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7275 */
7276 hash_len = 12;
7277
7278#if defined(MBEDTLS_SSL_RENEGOTIATION)
7279 ssl->verify_data_len = hash_len;
7280 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7281#endif
7282
7283 ssl->out_msglen = 4 + hash_len;
7284 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7285 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7286
7287 /*
7288 * In case of session resuming, invert the client and server
7289 * ChangeCipherSpec messages order.
7290 */
7291 if( ssl->handshake->resume != 0 )
7292 {
7293#if defined(MBEDTLS_SSL_CLI_C)
7294 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7295 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7296#endif
7297#if defined(MBEDTLS_SSL_SRV_C)
7298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7299 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7300#endif
7301 }
7302 else
7303 ssl->state++;
7304
7305 /*
7306 * Switch to our negotiated transform and session parameters for outbound
7307 * data.
7308 */
7309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7310
7311#if defined(MBEDTLS_SSL_PROTO_DTLS)
7312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7313 {
7314 unsigned char i;
7315
7316 /* Remember current epoch settings for resending */
7317 ssl->handshake->alt_transform_out = ssl->transform_out;
7318 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7319 sizeof( ssl->handshake->alt_out_ctr ) );
7320
7321 /* Set sequence_number to zero */
7322 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7323
7324
7325 /* Increment epoch */
7326 for( i = 2; i > 0; i-- )
7327 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7328 break;
7329
7330 /* The loop goes to its end iff the counter is wrapping */
7331 if( i == 0 )
7332 {
7333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7334 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7335 }
7336 }
7337 else
7338#endif /* MBEDTLS_SSL_PROTO_DTLS */
7339 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7340
7341 ssl->transform_out = ssl->transform_negotiate;
7342 ssl->session_out = ssl->session_negotiate;
7343
7344#if defined(MBEDTLS_SSL_PROTO_DTLS)
7345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7346 mbedtls_ssl_send_flight_completed( ssl );
7347#endif
7348
7349 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7350 {
7351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7352 return( ret );
7353 }
7354
7355#if defined(MBEDTLS_SSL_PROTO_DTLS)
7356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7357 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7358 {
7359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7360 return( ret );
7361 }
7362#endif
7363
7364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7365
7366 return( 0 );
7367}
7368
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007369#define SSL_MAX_HASH_LEN 12
7370
7371int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7372{
7373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7374 unsigned int hash_len = 12;
7375 unsigned char buf[SSL_MAX_HASH_LEN];
7376
7377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7378
7379 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7380
7381 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7382 {
7383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7384 goto exit;
7385 }
7386
7387 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7388 {
7389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7390 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7391 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7392 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7393 goto exit;
7394 }
7395
7396 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7397 {
7398 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7399 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7400 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7401 goto exit;
7402 }
7403
7404 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7405 {
7406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7407 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7408 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7409 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7410 goto exit;
7411 }
7412
7413 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7414 buf, hash_len ) != 0 )
7415 {
7416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7417 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7418 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7419 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7420 goto exit;
7421 }
7422
7423#if defined(MBEDTLS_SSL_RENEGOTIATION)
7424 ssl->verify_data_len = hash_len;
7425 memcpy( ssl->peer_verify_data, buf, hash_len );
7426#endif
7427
7428 if( ssl->handshake->resume != 0 )
7429 {
7430#if defined(MBEDTLS_SSL_CLI_C)
7431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7432 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7433#endif
7434#if defined(MBEDTLS_SSL_SRV_C)
7435 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7436 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7437#endif
7438 }
7439 else
7440 ssl->state++;
7441
7442#if defined(MBEDTLS_SSL_PROTO_DTLS)
7443 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7444 mbedtls_ssl_recv_flight_completed( ssl );
7445#endif
7446
7447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7448
7449exit:
7450 mbedtls_platform_zeroize( buf, hash_len );
7451 return( ret );
7452}
7453
Jerry Yu392112c2022-02-17 14:34:10 +08007454#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7455/*
7456 * Helper to get TLS 1.2 PRF from ciphersuite
7457 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7458 */
7459static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7460{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007461#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007462 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7463 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7464
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007465 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007466 return( tls_prf_sha384 );
7467#else
7468 (void) ciphersuite_id;
7469#endif
7470 return( tls_prf_sha256 );
7471}
7472#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007473
7474static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7475{
7476 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007477#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007478 if( tls_prf == tls_prf_sha384 )
7479 {
7480 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7481 }
7482 else
7483#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007484#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007485 if( tls_prf == tls_prf_sha256 )
7486 {
7487 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7488 }
7489 else
7490#endif
7491 return( MBEDTLS_SSL_TLS_PRF_NONE );
7492}
7493
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007494/*
7495 * Populate a transform structure with session keys and all the other
7496 * necessary information.
7497 *
7498 * Parameters:
7499 * - [in/out]: transform: structure to populate
7500 * [in] must be just initialised with mbedtls_ssl_transform_init()
7501 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7502 * - [in] ciphersuite
7503 * - [in] master
7504 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007505 * - [in] tls_prf: pointer to PRF to use for key derivation
7506 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007507 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007508 * - [in] endpoint: client or server
7509 * - [in] ssl: used for:
7510 * - ssl->conf->{f,p}_export_keys
7511 * [in] optionally used for:
7512 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7513 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007514MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007515static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7516 int ciphersuite,
7517 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007518#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007519 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007520#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007521 ssl_tls_prf_t tls_prf,
7522 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007523 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007524 unsigned endpoint,
7525 const mbedtls_ssl_context *ssl )
7526{
7527 int ret = 0;
7528 unsigned char keyblk[256];
7529 unsigned char *key1;
7530 unsigned char *key2;
7531 unsigned char *mac_enc;
7532 unsigned char *mac_dec;
7533 size_t mac_key_len = 0;
7534 size_t iv_copy_len;
7535 size_t keylen;
7536 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007537 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007538#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007539 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007540 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007541#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007542
7543#if defined(MBEDTLS_USE_PSA_CRYPTO)
7544 psa_key_type_t key_type;
7545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7546 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007547 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007548 size_t key_bits;
7549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7550#endif
7551
7552#if !defined(MBEDTLS_DEBUG_C) && \
7553 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7554 if( ssl->f_export_keys == NULL )
7555 {
7556 ssl = NULL; /* make sure we don't use it except for these cases */
7557 (void) ssl;
7558 }
7559#endif
7560
7561 /*
7562 * Some data just needs copying into the structure
7563 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007564#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007565 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007566#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007567 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007568
7569#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7570 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7571#endif
7572
7573#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007574 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007575 {
7576 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7577 * generation separate. This should never happen. */
7578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7579 }
7580#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7581
7582 /*
7583 * Get various info structures
7584 */
7585 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7586 if( ciphersuite_info == NULL )
7587 {
7588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7589 ciphersuite ) );
7590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7591 }
7592
Neil Armstrongab555e02022-04-04 11:07:59 +02007593 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007594#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007595 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007596#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007597 ciphersuite_info );
7598
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007599 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7600 transform->taglen =
7601 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7602
7603#if defined(MBEDTLS_USE_PSA_CRYPTO)
7604 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7605 transform->taglen,
7606 &alg,
7607 &key_type,
7608 &key_bits ) ) != PSA_SUCCESS )
7609 {
7610 ret = psa_ssl_status_to_mbedtls( status );
7611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7612 goto end;
7613 }
7614#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007615 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7616 if( cipher_info == NULL )
7617 {
7618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7619 ciphersuite_info->cipher ) );
7620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7621 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007622#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623
Neil Armstronge4512952022-03-08 09:08:22 +01007624#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007625 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007626 if( mac_alg == 0 )
7627 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007629 (unsigned) ciphersuite_info->mac ) );
7630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7631 }
7632#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007633 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7634 if( md_info == NULL )
7635 {
7636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7637 (unsigned) ciphersuite_info->mac ) );
7638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7639 }
Neil Armstronge4512952022-03-08 09:08:22 +01007640#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007641
7642#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7643 /* Copy own and peer's CID if the use of the CID
7644 * extension has been negotiated. */
7645 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7646 {
7647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7648
7649 transform->in_cid_len = ssl->own_cid_len;
7650 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7651 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7652 transform->in_cid_len );
7653
7654 transform->out_cid_len = ssl->handshake->peer_cid_len;
7655 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7656 ssl->handshake->peer_cid_len );
7657 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7658 transform->out_cid_len );
7659 }
7660#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7661
7662 /*
7663 * Compute key block using the PRF
7664 */
7665 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7666 if( ret != 0 )
7667 {
7668 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7669 return( ret );
7670 }
7671
7672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7673 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7674 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7675 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7676 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7677
7678 /*
7679 * Determine the appropriate key, IV and MAC length.
7680 */
7681
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007682#if defined(MBEDTLS_USE_PSA_CRYPTO)
7683 keylen = PSA_BITS_TO_BYTES(key_bits);
7684#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007685 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007686#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007687
7688#if defined(MBEDTLS_GCM_C) || \
7689 defined(MBEDTLS_CCM_C) || \
7690 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007691 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007692 {
7693 size_t explicit_ivlen;
7694
7695 transform->maclen = 0;
7696 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007697
7698 /* All modes haves 96-bit IVs, but the length of the static parts vary
7699 * with mode and version:
7700 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7701 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7702 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7703 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7704 * sequence number).
7705 */
7706 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007707#if defined(MBEDTLS_USE_PSA_CRYPTO)
7708 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7709#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007710 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007711#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007712 transform->fixed_ivlen = 12;
7713 else
7714 transform->fixed_ivlen = 4;
7715
7716 /* Minimum length of encrypted record */
7717 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7718 transform->minlen = explicit_ivlen + transform->taglen;
7719 }
7720 else
7721#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7722#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007723 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7724 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7725 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007726 {
Neil Armstronge4512952022-03-08 09:08:22 +01007727#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007728 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007729#else
7730 size_t block_size = cipher_info->block_size;
7731#endif /* MBEDTLS_USE_PSA_CRYPTO */
7732
7733#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007734 /* Get MAC length */
7735 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7736#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007737 /* Initialize HMAC contexts */
7738 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7739 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7740 {
7741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7742 goto end;
7743 }
7744
7745 /* Get MAC length */
7746 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007747#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007748 transform->maclen = mac_key_len;
7749
7750 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007751#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007752 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007753#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007754 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007755#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007756
7757 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007758 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007759 transform->minlen = transform->maclen;
7760 else
7761 {
7762 /*
7763 * GenericBlockCipher:
7764 * 1. if EtM is in use: one block plus MAC
7765 * otherwise: * first multiple of blocklen greater than maclen
7766 * 2. IV
7767 */
7768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007769 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007770 {
7771 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007772 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007773 }
7774 else
7775#endif
7776 {
7777 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007778 + block_size
7779 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007780 }
7781
Glenn Strauss07c64162022-03-14 12:34:51 -04007782 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007783 {
7784 transform->minlen += transform->ivlen;
7785 }
7786 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007787 {
7788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7789 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7790 goto end;
7791 }
7792 }
7793 }
7794 else
7795#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7796 {
7797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7799 }
7800
7801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7802 (unsigned) keylen,
7803 (unsigned) transform->minlen,
7804 (unsigned) transform->ivlen,
7805 (unsigned) transform->maclen ) );
7806
7807 /*
7808 * Finally setup the cipher contexts, IVs and MAC secrets.
7809 */
7810#if defined(MBEDTLS_SSL_CLI_C)
7811 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7812 {
7813 key1 = keyblk + mac_key_len * 2;
7814 key2 = keyblk + mac_key_len * 2 + keylen;
7815
7816 mac_enc = keyblk;
7817 mac_dec = keyblk + mac_key_len;
7818
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007819 iv_copy_len = ( transform->fixed_ivlen ) ?
7820 transform->fixed_ivlen : transform->ivlen;
7821 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7822 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7823 iv_copy_len );
7824 }
7825 else
7826#endif /* MBEDTLS_SSL_CLI_C */
7827#if defined(MBEDTLS_SSL_SRV_C)
7828 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7829 {
7830 key1 = keyblk + mac_key_len * 2 + keylen;
7831 key2 = keyblk + mac_key_len * 2;
7832
7833 mac_enc = keyblk + mac_key_len;
7834 mac_dec = keyblk;
7835
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007836 iv_copy_len = ( transform->fixed_ivlen ) ?
7837 transform->fixed_ivlen : transform->ivlen;
7838 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7839 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7840 iv_copy_len );
7841 }
7842 else
7843#endif /* MBEDTLS_SSL_SRV_C */
7844 {
7845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7846 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7847 goto end;
7848 }
7849
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007850 if( ssl != NULL && ssl->f_export_keys != NULL )
7851 {
7852 ssl->f_export_keys( ssl->p_export_keys,
7853 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7854 master, 48,
7855 randbytes + 32,
7856 randbytes,
7857 tls_prf_get_type( tls_prf ) );
7858 }
7859
7860#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007861 transform->psa_alg = alg;
7862
7863 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7864 {
7865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7866 psa_set_key_algorithm( &attributes, alg );
7867 psa_set_key_type( &attributes, key_type );
7868
7869 if( ( status = psa_import_key( &attributes,
7870 key1,
7871 PSA_BITS_TO_BYTES( key_bits ),
7872 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7873 {
7874 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7875 ret = psa_ssl_status_to_mbedtls( status );
7876 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7877 goto end;
7878 }
7879
7880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7881
7882 if( ( status = psa_import_key( &attributes,
7883 key2,
7884 PSA_BITS_TO_BYTES( key_bits ),
7885 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7886 {
7887 ret = psa_ssl_status_to_mbedtls( status );
7888 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7889 goto end;
7890 }
7891 }
7892#else
7893 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7894 cipher_info ) ) != 0 )
7895 {
7896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7897 goto end;
7898 }
7899
7900 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7901 cipher_info ) ) != 0 )
7902 {
7903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7904 goto end;
7905 }
7906
7907 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7908 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7909 MBEDTLS_ENCRYPT ) ) != 0 )
7910 {
7911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7912 goto end;
7913 }
7914
7915 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7916 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7917 MBEDTLS_DECRYPT ) ) != 0 )
7918 {
7919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7920 goto end;
7921 }
7922
7923#if defined(MBEDTLS_CIPHER_MODE_CBC)
7924 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7925 {
7926 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7927 MBEDTLS_PADDING_NONE ) ) != 0 )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7930 goto end;
7931 }
7932
7933 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7934 MBEDTLS_PADDING_NONE ) ) != 0 )
7935 {
7936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7937 goto end;
7938 }
7939 }
7940#endif /* MBEDTLS_CIPHER_MODE_CBC */
7941#endif /* MBEDTLS_USE_PSA_CRYPTO */
7942
Neil Armstrong29c0c042022-03-17 17:47:28 +01007943#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7944 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7945 For AEAD-based ciphersuites, there is nothing to do here. */
7946 if( mac_key_len != 0 )
7947 {
7948#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007949 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007950
7951 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007952 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007953 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7954
7955 if( ( status = psa_import_key( &attributes,
7956 mac_enc, mac_key_len,
7957 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7958 {
7959 ret = psa_ssl_status_to_mbedtls( status );
7960 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7961 goto end;
7962 }
7963
Ronald Cronfb39f152022-03-25 14:36:28 +01007964 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007965 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7966#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7967 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7968#endif
7969 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007970 /* mbedtls_ct_hmac() requires the key to be exportable */
7971 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7972 PSA_KEY_USAGE_VERIFY_HASH );
7973 else
7974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7975
7976 if( ( status = psa_import_key( &attributes,
7977 mac_dec, mac_key_len,
7978 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7979 {
7980 ret = psa_ssl_status_to_mbedtls( status );
7981 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7982 goto end;
7983 }
7984#else
7985 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7986 if( ret != 0 )
7987 goto end;
7988 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7989 if( ret != 0 )
7990 goto end;
7991#endif /* MBEDTLS_USE_PSA_CRYPTO */
7992 }
7993#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7994
7995 ((void) mac_dec);
7996 ((void) mac_enc);
7997
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007998end:
7999 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8000 return( ret );
8001}
8002
Jerry Yuee40f9d2022-02-17 14:55:16 +08008003#if defined(MBEDTLS_USE_PSA_CRYPTO)
8004int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8005 unsigned char *hash, size_t *hashlen,
8006 unsigned char *data, size_t data_len,
8007 mbedtls_md_type_t md_alg )
8008{
8009 psa_status_t status;
8010 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008011 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008012
8013 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8014
8015 if( ( status = psa_hash_setup( &hash_operation,
8016 hash_alg ) ) != PSA_SUCCESS )
8017 {
8018 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8019 goto exit;
8020 }
8021
8022 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8023 64 ) ) != PSA_SUCCESS )
8024 {
8025 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8026 goto exit;
8027 }
8028
8029 if( ( status = psa_hash_update( &hash_operation,
8030 data, data_len ) ) != PSA_SUCCESS )
8031 {
8032 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8033 goto exit;
8034 }
8035
8036 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8037 hashlen ) ) != PSA_SUCCESS )
8038 {
8039 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8040 goto exit;
8041 }
8042
8043exit:
8044 if( status != PSA_SUCCESS )
8045 {
8046 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8047 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8048 switch( status )
8049 {
8050 case PSA_ERROR_NOT_SUPPORTED:
8051 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8052 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8053 case PSA_ERROR_BUFFER_TOO_SMALL:
8054 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8055 case PSA_ERROR_INSUFFICIENT_MEMORY:
8056 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8057 default:
8058 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8059 }
8060 }
8061 return( 0 );
8062}
8063
8064#else
8065
8066int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8067 unsigned char *hash, size_t *hashlen,
8068 unsigned char *data, size_t data_len,
8069 mbedtls_md_type_t md_alg )
8070{
8071 int ret = 0;
8072 mbedtls_md_context_t ctx;
8073 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8074 *hashlen = mbedtls_md_get_size( md_info );
8075
8076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8077
8078 mbedtls_md_init( &ctx );
8079
8080 /*
8081 * digitally-signed struct {
8082 * opaque client_random[32];
8083 * opaque server_random[32];
8084 * ServerDHParams params;
8085 * };
8086 */
8087 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8088 {
8089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8090 goto exit;
8091 }
8092 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8093 {
8094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8095 goto exit;
8096 }
8097 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8098 {
8099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8100 goto exit;
8101 }
8102 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8103 {
8104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8105 goto exit;
8106 }
8107 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8108 {
8109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8110 goto exit;
8111 }
8112
8113exit:
8114 mbedtls_md_free( &ctx );
8115
8116 if( ret != 0 )
8117 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8118 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8119
8120 return( ret );
8121}
8122#endif /* MBEDTLS_USE_PSA_CRYPTO */
8123
Jerry Yud9d91da2022-02-17 14:57:06 +08008124#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8125
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008126/* Find the preferred hash for a given signature algorithm. */
8127unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8128 mbedtls_ssl_context *ssl,
8129 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008130{
Gabor Mezei078e8032022-04-27 21:17:56 +02008131 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008132 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008133
8134 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008135 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008136
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008137 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008138 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008139 unsigned int hash_alg_received =
8140 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8141 received_sig_algs[i] );
8142 unsigned int sig_alg_received =
8143 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8144 received_sig_algs[i] );
8145
8146 if( sig_alg == sig_alg_received )
8147 {
8148#if defined(MBEDTLS_USE_PSA_CRYPTO)
8149 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8150 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008151 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008152 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008153
Neil Armstrong96eceb82022-06-30 18:05:05 +02008154 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8155 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8156 PSA_ALG_ECDSA( psa_hash_alg ),
8157 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008158 continue;
8159
Neil Armstrong96eceb82022-06-30 18:05:05 +02008160 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008161 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8162 PSA_ALG_RSA_PKCS1V15_SIGN(
8163 psa_hash_alg ),
8164 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008165 continue;
8166 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008167#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008168
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008169 return( hash_alg_received );
8170 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008171 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008172
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008173 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008174}
8175
8176#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8177
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008178/* Serialization of TLS 1.2 sessions:
8179 *
8180 * struct {
8181 * uint64 start_time;
8182 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008183 * uint8 session_id_len; // at most 32
8184 * opaque session_id[32];
8185 * opaque master[48]; // fixed length in the standard
8186 * uint32 verify_result;
8187 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8188 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8189 * uint32 ticket_lifetime;
8190 * uint8 mfl_code; // up to 255 according to standard
8191 * uint8 encrypt_then_mac; // 0 or 1
8192 * } serialized_session_tls12;
8193 *
8194 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008195static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008196 unsigned char *buf,
8197 size_t buf_len )
8198{
8199 unsigned char *p = buf;
8200 size_t used = 0;
8201
8202#if defined(MBEDTLS_HAVE_TIME)
8203 uint64_t start;
8204#endif
8205#if defined(MBEDTLS_X509_CRT_PARSE_C)
8206#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8207 size_t cert_len;
8208#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8209#endif /* MBEDTLS_X509_CRT_PARSE_C */
8210
8211 /*
8212 * Time
8213 */
8214#if defined(MBEDTLS_HAVE_TIME)
8215 used += 8;
8216
8217 if( used <= buf_len )
8218 {
8219 start = (uint64_t) session->start;
8220
8221 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8222 p += 8;
8223 }
8224#endif /* MBEDTLS_HAVE_TIME */
8225
8226 /*
8227 * Basic mandatory fields
8228 */
8229 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008230 + 1 /* id_len */
8231 + sizeof( session->id )
8232 + sizeof( session->master )
8233 + 4; /* verify_result */
8234
8235 if( used <= buf_len )
8236 {
8237 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8238 p += 2;
8239
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008240 *p++ = MBEDTLS_BYTE_0( session->id_len );
8241 memcpy( p, session->id, 32 );
8242 p += 32;
8243
8244 memcpy( p, session->master, 48 );
8245 p += 48;
8246
8247 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8248 p += 4;
8249 }
8250
8251 /*
8252 * Peer's end-entity certificate
8253 */
8254#if defined(MBEDTLS_X509_CRT_PARSE_C)
8255#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8256 if( session->peer_cert == NULL )
8257 cert_len = 0;
8258 else
8259 cert_len = session->peer_cert->raw.len;
8260
8261 used += 3 + cert_len;
8262
8263 if( used <= buf_len )
8264 {
8265 *p++ = MBEDTLS_BYTE_2( cert_len );
8266 *p++ = MBEDTLS_BYTE_1( cert_len );
8267 *p++ = MBEDTLS_BYTE_0( cert_len );
8268
8269 if( session->peer_cert != NULL )
8270 {
8271 memcpy( p, session->peer_cert->raw.p, cert_len );
8272 p += cert_len;
8273 }
8274 }
8275#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8276 if( session->peer_cert_digest != NULL )
8277 {
8278 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8279 if( used <= buf_len )
8280 {
8281 *p++ = (unsigned char) session->peer_cert_digest_type;
8282 *p++ = (unsigned char) session->peer_cert_digest_len;
8283 memcpy( p, session->peer_cert_digest,
8284 session->peer_cert_digest_len );
8285 p += session->peer_cert_digest_len;
8286 }
8287 }
8288 else
8289 {
8290 used += 2;
8291 if( used <= buf_len )
8292 {
8293 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8294 *p++ = 0;
8295 }
8296 }
8297#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8298#endif /* MBEDTLS_X509_CRT_PARSE_C */
8299
8300 /*
8301 * Session ticket if any, plus associated data
8302 */
8303#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8304 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8305
8306 if( used <= buf_len )
8307 {
8308 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8309 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8310 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8311
8312 if( session->ticket != NULL )
8313 {
8314 memcpy( p, session->ticket, session->ticket_len );
8315 p += session->ticket_len;
8316 }
8317
8318 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8319 p += 4;
8320 }
8321#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8322
8323 /*
8324 * Misc extension-related info
8325 */
8326#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8327 used += 1;
8328
8329 if( used <= buf_len )
8330 *p++ = session->mfl_code;
8331#endif
8332
8333#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8334 used += 1;
8335
8336 if( used <= buf_len )
8337 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8338#endif
8339
8340 return( used );
8341}
8342
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008343MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008344static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008345 const unsigned char *buf,
8346 size_t len )
8347{
8348#if defined(MBEDTLS_HAVE_TIME)
8349 uint64_t start;
8350#endif
8351#if defined(MBEDTLS_X509_CRT_PARSE_C)
8352#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8353 size_t cert_len;
8354#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8355#endif /* MBEDTLS_X509_CRT_PARSE_C */
8356
8357 const unsigned char *p = buf;
8358 const unsigned char * const end = buf + len;
8359
8360 /*
8361 * Time
8362 */
8363#if defined(MBEDTLS_HAVE_TIME)
8364 if( 8 > (size_t)( end - p ) )
8365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8366
8367 start = ( (uint64_t) p[0] << 56 ) |
8368 ( (uint64_t) p[1] << 48 ) |
8369 ( (uint64_t) p[2] << 40 ) |
8370 ( (uint64_t) p[3] << 32 ) |
8371 ( (uint64_t) p[4] << 24 ) |
8372 ( (uint64_t) p[5] << 16 ) |
8373 ( (uint64_t) p[6] << 8 ) |
8374 ( (uint64_t) p[7] );
8375 p += 8;
8376
8377 session->start = (time_t) start;
8378#endif /* MBEDTLS_HAVE_TIME */
8379
8380 /*
8381 * Basic mandatory fields
8382 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008383 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8385
8386 session->ciphersuite = ( p[0] << 8 ) | p[1];
8387 p += 2;
8388
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008389 session->id_len = *p++;
8390 memcpy( session->id, p, 32 );
8391 p += 32;
8392
8393 memcpy( session->master, p, 48 );
8394 p += 48;
8395
8396 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8397 ( (uint32_t) p[1] << 16 ) |
8398 ( (uint32_t) p[2] << 8 ) |
8399 ( (uint32_t) p[3] );
8400 p += 4;
8401
8402 /* Immediately clear invalid pointer values that have been read, in case
8403 * we exit early before we replaced them with valid ones. */
8404#if defined(MBEDTLS_X509_CRT_PARSE_C)
8405#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8406 session->peer_cert = NULL;
8407#else
8408 session->peer_cert_digest = NULL;
8409#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8410#endif /* MBEDTLS_X509_CRT_PARSE_C */
8411#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8412 session->ticket = NULL;
8413#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8414
8415 /*
8416 * Peer certificate
8417 */
8418#if defined(MBEDTLS_X509_CRT_PARSE_C)
8419#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8420 /* Deserialize CRT from the end of the ticket. */
8421 if( 3 > (size_t)( end - p ) )
8422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8423
8424 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8425 p += 3;
8426
8427 if( cert_len != 0 )
8428 {
8429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8430
8431 if( cert_len > (size_t)( end - p ) )
8432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8433
8434 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8435
8436 if( session->peer_cert == NULL )
8437 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8438
8439 mbedtls_x509_crt_init( session->peer_cert );
8440
8441 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8442 p, cert_len ) ) != 0 )
8443 {
8444 mbedtls_x509_crt_free( session->peer_cert );
8445 mbedtls_free( session->peer_cert );
8446 session->peer_cert = NULL;
8447 return( ret );
8448 }
8449
8450 p += cert_len;
8451 }
8452#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8453 /* Deserialize CRT digest from the end of the ticket. */
8454 if( 2 > (size_t)( end - p ) )
8455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8456
8457 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8458 session->peer_cert_digest_len = (size_t) *p++;
8459
8460 if( session->peer_cert_digest_len != 0 )
8461 {
8462 const mbedtls_md_info_t *md_info =
8463 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8464 if( md_info == NULL )
8465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8466 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8468
8469 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8470 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8471
8472 session->peer_cert_digest =
8473 mbedtls_calloc( 1, session->peer_cert_digest_len );
8474 if( session->peer_cert_digest == NULL )
8475 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8476
8477 memcpy( session->peer_cert_digest, p,
8478 session->peer_cert_digest_len );
8479 p += session->peer_cert_digest_len;
8480 }
8481#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8482#endif /* MBEDTLS_X509_CRT_PARSE_C */
8483
8484 /*
8485 * Session ticket and associated data
8486 */
8487#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8488 if( 3 > (size_t)( end - p ) )
8489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8490
8491 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8492 p += 3;
8493
8494 if( session->ticket_len != 0 )
8495 {
8496 if( session->ticket_len > (size_t)( end - p ) )
8497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8498
8499 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8500 if( session->ticket == NULL )
8501 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8502
8503 memcpy( session->ticket, p, session->ticket_len );
8504 p += session->ticket_len;
8505 }
8506
8507 if( 4 > (size_t)( end - p ) )
8508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8509
8510 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8511 ( (uint32_t) p[1] << 16 ) |
8512 ( (uint32_t) p[2] << 8 ) |
8513 ( (uint32_t) p[3] );
8514 p += 4;
8515#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8516
8517 /*
8518 * Misc extension-related info
8519 */
8520#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8521 if( 1 > (size_t)( end - p ) )
8522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8523
8524 session->mfl_code = *p++;
8525#endif
8526
8527#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8528 if( 1 > (size_t)( end - p ) )
8529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8530
8531 session->encrypt_then_mac = *p++;
8532#endif
8533
8534 /* Done, should have consumed entire buffer */
8535 if( p != end )
8536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8537
8538 return( 0 );
8539}
Jerry Yudc7bd172022-02-17 13:44:15 +08008540#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8541
XiaokangQian75d40ef2022-04-20 11:05:24 +00008542int mbedtls_ssl_validate_ciphersuite(
8543 const mbedtls_ssl_context *ssl,
8544 const mbedtls_ssl_ciphersuite_t *suite_info,
8545 mbedtls_ssl_protocol_version min_tls_version,
8546 mbedtls_ssl_protocol_version max_tls_version )
8547{
8548 (void) ssl;
8549
8550 if( suite_info == NULL )
8551 return( -1 );
8552
8553 if( ( suite_info->min_tls_version > max_tls_version ) ||
8554 ( suite_info->max_tls_version < min_tls_version ) )
8555 {
8556 return( -1 );
8557 }
8558
XiaokangQian060d8672022-04-21 09:24:56 +00008559#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008560#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8561 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8562 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8563 {
8564 return( -1 );
8565 }
8566#endif
8567
8568 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8569#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8570 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8571 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8572 {
8573 return( -1 );
8574 }
8575#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8576#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8577
8578 return( 0 );
8579}
8580
XiaokangQianeaf36512022-04-24 09:07:44 +00008581#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8582/*
8583 * Function for writing a signature algorithm extension.
8584 *
8585 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8586 * value (TLS 1.3 RFC8446):
8587 * enum {
8588 * ....
8589 * ecdsa_secp256r1_sha256( 0x0403 ),
8590 * ecdsa_secp384r1_sha384( 0x0503 ),
8591 * ecdsa_secp521r1_sha512( 0x0603 ),
8592 * ....
8593 * } SignatureScheme;
8594 *
8595 * struct {
8596 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8597 * } SignatureSchemeList;
8598 *
8599 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8600 * value (TLS 1.2 RFC5246):
8601 * enum {
8602 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8603 * sha512(6), (255)
8604 * } HashAlgorithm;
8605 *
8606 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8607 * SignatureAlgorithm;
8608 *
8609 * struct {
8610 * HashAlgorithm hash;
8611 * SignatureAlgorithm signature;
8612 * } SignatureAndHashAlgorithm;
8613 *
8614 * SignatureAndHashAlgorithm
8615 * supported_signature_algorithms<2..2^16-2>;
8616 *
8617 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8618 * generalization of the TLS 1.2 signature algorithm extension.
8619 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8620 * `SignatureScheme` field of TLS 1.3
8621 *
8622 */
8623int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8624 const unsigned char *end, size_t *out_len )
8625{
8626 unsigned char *p = buf;
8627 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8628 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8629
8630 *out_len = 0;
8631
8632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8633
8634 /* Check if we have space for header and length field:
8635 * - extension_type (2 bytes)
8636 * - extension_data_length (2 bytes)
8637 * - supported_signature_algorithms_length (2 bytes)
8638 */
8639 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8640 p += 6;
8641
8642 /*
8643 * Write supported_signature_algorithms
8644 */
8645 supported_sig_alg = p;
8646 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8647 if( sig_alg == NULL )
8648 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8649
8650 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8651 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8653 *sig_alg,
8654 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008655 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8656 continue;
8657 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8658 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8659 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008661 *sig_alg,
8662 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008663 }
8664
8665 /* Length of supported_signature_algorithms */
8666 supported_sig_alg_len = p - supported_sig_alg;
8667 if( supported_sig_alg_len == 0 )
8668 {
8669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8671 }
8672
XiaokangQianeaf36512022-04-24 09:07:44 +00008673 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008674 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008675 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8676
XiaokangQianeaf36512022-04-24 09:07:44 +00008677 *out_len = p - buf;
8678
8679#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8680 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8681#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8682 return( 0 );
8683}
8684#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8685
XiaokangQian40a35232022-05-07 09:02:40 +00008686#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008687/*
8688 * mbedtls_ssl_parse_server_name_ext
8689 *
8690 * Structure of server_name extension:
8691 *
8692 * enum {
8693 * host_name(0), (255)
8694 * } NameType;
8695 * opaque HostName<1..2^16-1>;
8696 *
8697 * struct {
8698 * NameType name_type;
8699 * select (name_type) {
8700 * case host_name: HostName;
8701 * } name;
8702 * } ServerName;
8703 * struct {
8704 * ServerName server_name_list<1..2^16-1>
8705 * } ServerNameList;
8706 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008707MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008708int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8709 const unsigned char *buf,
8710 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008711{
8712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8713 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008714 size_t server_name_list_len, hostname_len;
8715 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008716
XiaokangQianf2a94202022-05-20 06:44:24 +00008717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008718
8719 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008720 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008721 p += 2;
8722
XiaokangQian9b2b7712022-05-17 02:57:00 +00008723 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8724 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008725 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008726 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008728 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008729 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8730 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008731
8732 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8733 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008734 /* sni_name is intended to be used only during the parsing of the
8735 * ClientHello message (it is reset to NULL before the end of
8736 * the message parsing). Thus it is ok to just point to the
8737 * reception buffer and not make a copy of it.
8738 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008739 ssl->handshake->sni_name = p + 3;
8740 ssl->handshake->sni_name_len = hostname_len;
8741 if( ssl->conf->f_sni == NULL )
8742 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008743 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008744 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008745 if( ret != 0 )
8746 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008747 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008748 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8749 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008750 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8751 }
8752 return( 0 );
8753 }
8754
8755 p += hostname_len + 3;
8756 }
8757
8758 return( 0 );
8759}
8760#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8761
XiaokangQianacb39922022-06-17 10:18:48 +00008762#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008763MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008764int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8765 const unsigned char *buf,
8766 const unsigned char *end )
8767{
8768 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008769 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008770 const unsigned char *protocol_name_list;
8771 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008772 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008773
8774 /* If ALPN not configured, just ignore the extension */
8775 if( ssl->conf->alpn_list == NULL )
8776 return( 0 );
8777
8778 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008779 * RFC7301, section 3.1
8780 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008781 *
XiaokangQianc7403452022-06-23 03:24:12 +00008782 * struct {
8783 * ProtocolName protocol_name_list<2..2^16-1>
8784 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008785 */
8786
XiaokangQianc7403452022-06-23 03:24:12 +00008787 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008788 * protocol_name_list_len 2 bytes
8789 * protocol_name_len 1 bytes
8790 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008791 */
XiaokangQianacb39922022-06-17 10:18:48 +00008792 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8793
XiaokangQianc7403452022-06-23 03:24:12 +00008794 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008795 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008796 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008797 protocol_name_list = p;
8798 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008799
8800 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008801 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008802 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008803 protocol_name_len = *p++;
8804 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8805 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008806 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008807 {
8808 MBEDTLS_SSL_PEND_FATAL_ALERT(
8809 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8810 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008811 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008812 }
8813
8814 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008815 }
8816
8817 /* Use our order of preference */
8818 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8819 {
8820 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008821 p = protocol_name_list;
8822 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008823 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008824 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008825 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008826 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008827 {
8828 ssl->alpn_chosen = *alpn;
8829 return( 0 );
8830 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008831
8832 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008833 }
8834 }
8835
XiaokangQian95d5f542022-06-24 02:29:26 +00008836 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008837 MBEDTLS_SSL_PEND_FATAL_ALERT(
8838 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8839 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8840 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8841}
8842
8843int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8844 unsigned char *buf,
8845 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008846 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008847{
8848 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008849 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008850 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008851
8852 if( ssl->alpn_chosen == NULL )
8853 {
8854 return( 0 );
8855 }
8856
XiaokangQian95d5f542022-06-24 02:29:26 +00008857 protocol_name_len = strlen( ssl->alpn_chosen );
8858 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008859
8860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8861 /*
8862 * 0 . 1 ext identifier
8863 * 2 . 3 ext length
8864 * 4 . 5 protocol list length
8865 * 6 . 6 protocol name length
8866 * 7 . 7+n protocol name
8867 */
8868 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8869
XiaokangQian95d5f542022-06-24 02:29:26 +00008870 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008871
XiaokangQian95d5f542022-06-24 02:29:26 +00008872 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8873 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008874 /* Note: the length of the chosen protocol has been checked to be less
8875 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8876 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008877 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008878
XiaokangQian95d5f542022-06-24 02:29:26 +00008879 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008880 return ( 0 );
8881}
8882#endif /* MBEDTLS_SSL_ALPN */
8883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008884#endif /* MBEDTLS_SSL_TLS_C */