blob: a36f5b1f123753578a4b4d8cc5d93690f29e2ca2 [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)
Xiaokang Qian87306442022-10-12 09:47:38 +0000248 if( src->ticket != NULL )
249 {
250 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
251 if( dst->ticket == NULL )
252 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
253
254 memcpy( dst->ticket, src->ticket, src->ticket_len );
255 }
256
257#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
258 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
259 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
260 {
Xiaokang Qian997669a2022-10-12 14:30:27 +0000261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaokang Qian87306442022-10-12 09:47:38 +0000262 dst->hostname = NULL;
Xiaokang Qian997669a2022-10-12 14:30:27 +0000263 ret = mbedtls_ssl_session_set_hostname( dst,
264 src->hostname );
265 if( ret != 0)
266 return ret;
Xiaokang Qian87306442022-10-12 09:47:38 +0000267 }
Xiaokang Qianbaa47642022-10-12 10:21:27 +0000268#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Xiaokang Qian87306442022-10-12 09:47:38 +0000269#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272
273#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200274 if( src->peer_cert != NULL )
275 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200277
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200278 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200279 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200280 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200285 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288 dst->peer_cert = NULL;
289 return( ret );
290 }
291 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000292#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000293 if( src->peer_cert_digest != NULL )
294 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000295 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000296 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000297 if( dst->peer_cert_digest == NULL )
298 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
299
300 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
301 src->peer_cert_digest_len );
302 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000303 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000304 }
305#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200308
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200309 return( 0 );
310}
311
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200313MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
315{
316 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
317 if( resized_buffer == NULL )
318 return -1;
319
320 /* We want to copy len_new bytes when downsizing the buffer, and
321 * len_old bytes when upsizing, so we choose the smaller of two sizes,
322 * to fit one buffer into another. Size checks, ensuring that no data is
323 * lost, are done outside of this function. */
324 memcpy( resized_buffer, *buffer,
325 ( len_new < *len_old ) ? len_new : *len_old );
326 mbedtls_platform_zeroize( *buffer, *len_old );
327 mbedtls_free( *buffer );
328
329 *buffer = resized_buffer;
330 *len_old = len_new;
331
332 return 0;
333}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200334
335static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500336 size_t in_buf_new_len,
337 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200338{
339 int modified = 0;
340 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
341 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
342 if( ssl->in_buf != NULL )
343 {
344 written_in = ssl->in_msg - ssl->in_buf;
345 iv_offset_in = ssl->in_iv - ssl->in_buf;
346 len_offset_in = ssl->in_len - ssl->in_buf;
347 if( downsizing ?
348 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
349 ssl->in_buf_len < in_buf_new_len )
350 {
351 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
352 {
353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
354 }
355 else
356 {
Paul Elliottb7449902021-03-10 18:14:58 +0000357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
358 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200359 modified = 1;
360 }
361 }
362 }
363
364 if( ssl->out_buf != NULL )
365 {
366 written_out = ssl->out_msg - ssl->out_buf;
367 iv_offset_out = ssl->out_iv - ssl->out_buf;
368 len_offset_out = ssl->out_len - ssl->out_buf;
369 if( downsizing ?
370 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
371 ssl->out_buf_len < out_buf_new_len )
372 {
373 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
374 {
375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
376 }
377 else
378 {
Paul Elliottb7449902021-03-10 18:14:58 +0000379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
380 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200381 modified = 1;
382 }
383 }
384 }
385 if( modified )
386 {
387 /* Update pointers here to avoid doing it twice. */
388 mbedtls_ssl_reset_in_out_pointers( ssl );
389 /* Fields below might not be properly updated with record
390 * splitting or with CID, so they are manually updated here. */
391 ssl->out_msg = ssl->out_buf + written_out;
392 ssl->out_len = ssl->out_buf + len_offset_out;
393 ssl->out_iv = ssl->out_buf + iv_offset_out;
394
395 ssl->in_msg = ssl->in_buf + written_in;
396 ssl->in_len = ssl->in_buf + len_offset_in;
397 ssl->in_iv = ssl->in_buf + iv_offset_in;
398 }
399}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500400#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
401
Jerry Yudb8c48a2022-01-27 14:54:54 +0800402#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800403
404#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
405typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
406 const char *label,
407 const unsigned char *random, size_t rlen,
408 unsigned char *dstbuf, size_t dlen );
409
410static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
411
412#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
413
414/* Type for the TLS PRF */
415typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
416 const unsigned char *, size_t,
417 unsigned char *, size_t);
418
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200419MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800420static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
421 int ciphersuite,
422 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800424 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200425#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800426 ssl_tls_prf_t tls_prf,
427 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400428 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800429 unsigned endpoint,
430 const mbedtls_ssl_context *ssl );
431
Andrzej Kurek25f27152022-08-17 16:09:31 -0400432#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200433MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800434static int tls_prf_sha256( const unsigned char *secret, size_t slen,
435 const char *label,
436 const unsigned char *random, size_t rlen,
437 unsigned char *dstbuf, size_t dlen );
438static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
439static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
440
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400441#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800442
Andrzej Kurek25f27152022-08-17 16:09:31 -0400443#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200444MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800445static int tls_prf_sha384( const unsigned char *secret, size_t slen,
446 const char *label,
447 const unsigned char *random, size_t rlen,
448 unsigned char *dstbuf, size_t dlen );
449
450static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
451static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400452#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800453
Jerry Yu438ddd82022-07-07 06:55:50 +0000454static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800455 unsigned char *buf,
456 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800457
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200458MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000459static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800460 const unsigned char *buf,
461 size_t len );
462#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
463
Jerry Yu53d23e22022-02-09 16:25:09 +0800464static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
465
Andrzej Kurek25f27152022-08-17 16:09:31 -0400466#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400468#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469
Andrzej Kurek25f27152022-08-17 16:09:31 -0400470#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800471static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400472#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000473
Ron Eldor51d3ab52019-05-12 14:54:30 +0300474int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
475 const unsigned char *secret, size_t slen,
476 const char *label,
477 const unsigned char *random, size_t rlen,
478 unsigned char *dstbuf, size_t dlen )
479{
480 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
481
482 switch( prf )
483 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300484#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400485#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300486 case MBEDTLS_SSL_TLS_PRF_SHA384:
487 tls_prf = tls_prf_sha384;
488 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400489#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400490#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300491 case MBEDTLS_SSL_TLS_PRF_SHA256:
492 tls_prf = tls_prf_sha256;
493 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400494#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300495#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300496 default:
497 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
498 }
499
500 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
501}
502
Jerry Yuc73c6182022-02-08 20:29:25 +0800503#if defined(MBEDTLS_X509_CRT_PARSE_C)
504static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
505{
506#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
507 if( session->peer_cert != NULL )
508 {
509 mbedtls_x509_crt_free( session->peer_cert );
510 mbedtls_free( session->peer_cert );
511 session->peer_cert = NULL;
512 }
513#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
514 if( session->peer_cert_digest != NULL )
515 {
516 /* Zeroization is not necessary. */
517 mbedtls_free( session->peer_cert_digest );
518 session->peer_cert_digest = NULL;
519 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
520 session->peer_cert_digest_len = 0;
521 }
522#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
523}
524#endif /* MBEDTLS_X509_CRT_PARSE_C */
525
526void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
528{
529 ((void) ciphersuite_info);
530
Andrzej Kurek25f27152022-08-17 16:09:31 -0400531#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800532 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
533 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
534 else
535#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400536#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800537 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
538 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
539 else
540#endif
541 {
542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
543 return;
544 }
545}
546
XiaokangQianadab9a62022-07-18 07:41:26 +0000547void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
548 unsigned hs_type,
549 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100550{
551 unsigned char hs_hdr[4];
552
553 /* Build HS header for checksum update. */
554 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
555 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
556 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
557 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
558
559 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
560}
561
562void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
563 unsigned hs_type,
564 unsigned char const *msg,
565 size_t msg_len )
566{
567 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
568 ssl->handshake->update_checksum( ssl, msg, msg_len );
569}
570
Jerry Yuc73c6182022-02-08 20:29:25 +0800571void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
572{
573 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400574#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800575#if defined(MBEDTLS_USE_PSA_CRYPTO)
576 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
577 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
578#else
579 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
580#endif
581#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400582#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800583#if defined(MBEDTLS_USE_PSA_CRYPTO)
584 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
585 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
586#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400587 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800588#endif
589#endif
590}
591
592static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
593 const unsigned char *buf, size_t len )
594{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400595#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800596#if defined(MBEDTLS_USE_PSA_CRYPTO)
597 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
598#else
599 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
600#endif
601#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400602#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800603#if defined(MBEDTLS_USE_PSA_CRYPTO)
604 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
605#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400606 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800607#endif
608#endif
609}
610
Andrzej Kurek25f27152022-08-17 16:09:31 -0400611#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800612static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
613 const unsigned char *buf, size_t len )
614{
615#if defined(MBEDTLS_USE_PSA_CRYPTO)
616 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
617#else
618 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
619#endif
620}
621#endif
622
Andrzej Kurek25f27152022-08-17 16:09:31 -0400623#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800624static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
625 const unsigned char *buf, size_t len )
626{
627#if defined(MBEDTLS_USE_PSA_CRYPTO)
628 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
629#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400630 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800631#endif
632}
633#endif
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200638
Andrzej Kurek25f27152022-08-17 16:09:31 -0400639#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
641 handshake->fin_sha256_psa = psa_hash_operation_init();
642 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
643#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200645 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200646#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500647#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400648#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500649#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500650 handshake->fin_sha384_psa = psa_hash_operation_init();
651 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500652#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400653 mbedtls_sha512_init( &handshake->fin_sha384 );
654 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200655#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500656#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200657
658 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_DHM_C)
661 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200662#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200663#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200665#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200666#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200667 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200668#if defined(MBEDTLS_SSL_CLI_C)
669 handshake->ecjpake_cache = NULL;
670 handshake->ecjpake_cache_len = 0;
671#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200672#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200673
Gilles Peskineeccd8882020-03-10 12:19:08 +0100674#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200675 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200676#endif
677
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200678#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
679 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
680#endif
Hanno Becker75173122019-02-06 16:18:31 +0000681
682#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
683 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
684 mbedtls_pk_init( &handshake->peer_pubkey );
685#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200686}
687
Hanno Beckera18d1322018-01-03 14:27:32 +0000688void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200691
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100692#if defined(MBEDTLS_USE_PSA_CRYPTO)
693 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
694 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100695#else
696 mbedtls_cipher_init( &transform->cipher_ctx_enc );
697 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100698#endif
699
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000700#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100701#if defined(MBEDTLS_USE_PSA_CRYPTO)
702 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
703 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100704#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_md_init( &transform->md_ctx_enc );
706 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000707#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100708#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200709}
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200714}
715
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200716MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000718{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200719 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000720 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200722 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200724 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200725 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200726
727 /*
728 * Either the pointers are now NULL or cleared properly and can be freed.
729 * Now allocate missing structures.
730 */
731 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200732 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200733 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200734 }
Paul Bakker48916f92012-09-16 19:57:18 +0000735
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200736 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200737 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200738 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 }
Paul Bakker48916f92012-09-16 19:57:18 +0000740
Paul Bakker82788fb2014-10-20 13:59:19 +0200741 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200742 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200743 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200744 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500745#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
746 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400747
Andrzej Kurek4a063792020-10-21 15:08:44 +0200748 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
749 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500750#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000751
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200752 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000753 if( ssl->handshake == NULL ||
754 ssl->transform_negotiate == NULL ||
755 ssl->session_negotiate == NULL )
756 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_free( ssl->handshake );
760 mbedtls_free( ssl->transform_negotiate );
761 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200762
763 ssl->handshake = NULL;
764 ssl->transform_negotiate = NULL;
765 ssl->session_negotiate = NULL;
766
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200767 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000768 }
769
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200770 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000772 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200773 ssl_handshake_params_init( ssl->handshake );
774
Jerry Yud0766ec2022-09-22 10:46:57 +0800775#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
776 defined(MBEDTLS_SSL_SRV_C) && \
777 defined(MBEDTLS_SSL_SESSION_TICKETS)
778 ssl->handshake->new_session_tickets_count =
779 ssl->conf->new_session_tickets_count ;
780#endif
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
784 {
785 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200786
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200787 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
788 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
789 else
790 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200791
Hanno Becker0f57a652020-02-05 10:37:26 +0000792 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200793 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200794#endif
795
Brett Warrene0edc842021-08-17 09:53:13 +0100796/*
797 * curve_list is translated to IANA TLS group identifiers here because
798 * mbedtls_ssl_conf_curves returns void and so can't return
799 * any error codes.
800 */
801#if defined(MBEDTLS_ECP_C)
802#if !defined(MBEDTLS_DEPRECATED_REMOVED)
803 /* Heap allocate and translate curve_list from internal to IANA group ids */
804 if ( ssl->conf->curve_list != NULL )
805 {
806 size_t length;
807 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
808
809 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
810 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
811
812 /* Leave room for zero termination */
813 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
814 if ( group_list == NULL )
815 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
816
817 for( size_t i = 0; i < length; i++ )
818 {
819 const mbedtls_ecp_curve_info *info =
820 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
821 if ( info == NULL )
822 {
823 mbedtls_free( group_list );
824 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
825 }
826 group_list[i] = info->tls_id;
827 }
828
829 group_list[length] = 0;
830
831 ssl->handshake->group_list = group_list;
832 ssl->handshake->group_list_heap_allocated = 1;
833 }
834 else
835 {
836 ssl->handshake->group_list = ssl->conf->group_list;
837 ssl->handshake->group_list_heap_allocated = 0;
838 }
839#endif /* MBEDTLS_DEPRECATED_REMOVED */
840#endif /* MBEDTLS_ECP_C */
841
Jerry Yuf017ee42022-01-12 15:49:48 +0800842#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
843#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800844#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800845 /* Heap allocate and translate sig_hashes from internal hash identifiers to
846 signature algorithms IANA identifiers. */
847 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800848 ssl->conf->sig_hashes != NULL )
849 {
850 const int *md;
851 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800852 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800853 uint16_t *p;
854
Jerry Yub476a442022-01-21 18:14:45 +0800855#if defined(static_assert)
856 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
857 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
858 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800859#endif
860
Jerry Yuf017ee42022-01-12 15:49:48 +0800861 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
862 {
863 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
864 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800865#if defined(MBEDTLS_ECDSA_C)
866 sig_algs_len += sizeof( uint16_t );
867#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800868
Jerry Yub476a442022-01-21 18:14:45 +0800869#if defined(MBEDTLS_RSA_C)
870 sig_algs_len += sizeof( uint16_t );
871#endif
872 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
873 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800874 }
875
Jerry Yub476a442022-01-21 18:14:45 +0800876 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800877 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
878
Jerry Yub476a442022-01-21 18:14:45 +0800879 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
880 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800881 if( ssl->handshake->sig_algs == NULL )
882 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
883
884 p = (uint16_t *)ssl->handshake->sig_algs;
885 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
886 {
887 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
888 if( hash == MBEDTLS_SSL_HASH_NONE )
889 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800890#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800891 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
892 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800893#endif
894#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
896 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800897#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800898 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200899 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800900 ssl->handshake->sig_algs_heap_allocated = 1;
901 }
902 else
Jerry Yua69269a2022-01-17 21:06:01 +0800903#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800904 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800905 ssl->handshake->sig_algs_heap_allocated = 0;
906 }
Jerry Yucc539102022-06-27 16:27:35 +0800907#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800908#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000909 return( 0 );
910}
911
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200912#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200913/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200914MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200915static int ssl_cookie_write_dummy( void *ctx,
916 unsigned char **p, unsigned char *end,
917 const unsigned char *cli_id, size_t cli_id_len )
918{
919 ((void) ctx);
920 ((void) p);
921 ((void) end);
922 ((void) cli_id);
923 ((void) cli_id_len);
924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200926}
927
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200928MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200929static int ssl_cookie_check_dummy( void *ctx,
930 const unsigned char *cookie, size_t cookie_len,
931 const unsigned char *cli_id, size_t cli_id_len )
932{
933 ((void) ctx);
934 ((void) cookie);
935 ((void) cookie_len);
936 ((void) cli_id);
937 ((void) cli_id_len);
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200940}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200941#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200942
Paul Bakker5121ce52009-01-03 21:22:43 +0000943/*
944 * Initialize an SSL context
945 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200946void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
947{
948 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
949}
950
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200951MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800952static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
953{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100954 const mbedtls_ssl_config *conf = ssl->conf;
955
Ronald Cron6f135e12021-12-08 16:57:54 +0100956#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100957 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
958 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000959 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
960 {
961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
962 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
963 }
964
Jerry Yu60835a82021-08-04 10:13:52 +0800965 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
966 return( 0 );
967 }
968#endif
969
970#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100971 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800972 {
973 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
974 return( 0 );
975 }
976#endif
977
Ronald Cron6f135e12021-12-08 16:57:54 +0100978#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100979 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800980 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
984 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
985 }
986
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000987 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
988 {
989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
990 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
991 }
992
993
Ronald Crone1d3f062022-02-10 14:50:54 +0100994 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
995 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800996 }
997#endif
998
999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1000 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1001}
1002
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001003MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001004static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1005{
1006 int ret;
1007 ret = ssl_conf_version_check( ssl );
1008 if( ret != 0 )
1009 return( ret );
1010
1011 /* Space for further checks */
1012
1013 return( 0 );
1014}
1015
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001016/*
1017 * Setup an SSL context
1018 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001019
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001020int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001021 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001022{
Janos Follath865b3eb2019-12-16 11:46:15 +00001023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001024 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1025 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001027 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001028
Jerry Yu60835a82021-08-04 10:13:52 +08001029 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1030 return( ret );
1031
Paul Bakker62f2dee2012-09-28 07:31:51 +00001032 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001033 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001034 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001035
1036 /* Set to NULL in case of an error condition */
1037 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001038
Darryl Greenb33cc762019-11-28 14:29:44 +00001039#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1040 ssl->in_buf_len = in_buf_len;
1041#endif
1042 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001043 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001046 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001047 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001048 }
1049
Darryl Greenb33cc762019-11-28 14:29:44 +00001050#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1051 ssl->out_buf_len = out_buf_len;
1052#endif
1053 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001054 if( ssl->out_buf == NULL )
1055 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001057 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001058 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 }
1060
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001061 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001062
Johan Pascalb62bb512015-12-03 21:56:45 +01001063#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001064 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001065#endif
1066
Paul Bakker48916f92012-09-16 19:57:18 +00001067 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001068 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
1070 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001071
1072error:
1073 mbedtls_free( ssl->in_buf );
1074 mbedtls_free( ssl->out_buf );
1075
1076 ssl->conf = NULL;
1077
Darryl Greenb33cc762019-11-28 14:29:44 +00001078#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1079 ssl->in_buf_len = 0;
1080 ssl->out_buf_len = 0;
1081#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001082 ssl->in_buf = NULL;
1083 ssl->out_buf = NULL;
1084
1085 ssl->in_hdr = NULL;
1086 ssl->in_ctr = NULL;
1087 ssl->in_len = NULL;
1088 ssl->in_iv = NULL;
1089 ssl->in_msg = NULL;
1090
1091 ssl->out_hdr = NULL;
1092 ssl->out_ctr = NULL;
1093 ssl->out_len = NULL;
1094 ssl->out_iv = NULL;
1095 ssl->out_msg = NULL;
1096
k-stachowiak9f7798e2018-07-31 16:52:32 +02001097 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098}
1099
1100/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001101 * Reset an initialized and used SSL context for re-use while retaining
1102 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001103 *
1104 * If partial is non-zero, keep data in the input buffer and client ID.
1105 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001106 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001107void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1108 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001109{
Darryl Greenb33cc762019-11-28 14:29:44 +00001110#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1111 size_t in_buf_len = ssl->in_buf_len;
1112 size_t out_buf_len = ssl->out_buf_len;
1113#else
1114 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1115 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1116#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001117
Hanno Beckerb0302c42021-08-03 09:39:42 +01001118#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1119 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001120#endif
1121
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001122 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001123 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001124
Hanno Beckerb0302c42021-08-03 09:39:42 +01001125 mbedtls_ssl_reset_in_out_pointers( ssl );
1126
1127 /* Reset incoming message parsing */
1128 ssl->in_offt = NULL;
1129 ssl->nb_zero = 0;
1130 ssl->in_msgtype = 0;
1131 ssl->in_msglen = 0;
1132 ssl->in_hslen = 0;
1133 ssl->keep_current_message = 0;
1134 ssl->transform_in = NULL;
1135
1136#if defined(MBEDTLS_SSL_PROTO_DTLS)
1137 ssl->next_record_offset = 0;
1138 ssl->in_epoch = 0;
1139#endif
1140
1141 /* Keep current datagram if partial == 1 */
1142 if( partial == 0 )
1143 {
1144 ssl->in_left = 0;
1145 memset( ssl->in_buf, 0, in_buf_len );
1146 }
1147
Ronald Cronad8c17b2022-06-10 17:18:09 +02001148 ssl->send_alert = 0;
1149
Hanno Beckerb0302c42021-08-03 09:39:42 +01001150 /* Reset outgoing message writing */
1151 ssl->out_msgtype = 0;
1152 ssl->out_msglen = 0;
1153 ssl->out_left = 0;
1154 memset( ssl->out_buf, 0, out_buf_len );
1155 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1156 ssl->transform_out = NULL;
1157
1158#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1159 mbedtls_ssl_dtls_replay_reset( ssl );
1160#endif
1161
XiaokangQian2b01dc32022-01-21 02:53:13 +00001162#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001163 if( ssl->transform )
1164 {
1165 mbedtls_ssl_transform_free( ssl->transform );
1166 mbedtls_free( ssl->transform );
1167 ssl->transform = NULL;
1168 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001169#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1170
XiaokangQian2b01dc32022-01-21 02:53:13 +00001171#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1172 mbedtls_ssl_transform_free( ssl->transform_application );
1173 mbedtls_free( ssl->transform_application );
1174 ssl->transform_application = NULL;
1175
1176 if( ssl->handshake != NULL )
1177 {
1178 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1179 mbedtls_free( ssl->handshake->transform_earlydata );
1180 ssl->handshake->transform_earlydata = NULL;
1181
1182 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1183 mbedtls_free( ssl->handshake->transform_handshake );
1184 ssl->handshake->transform_handshake = NULL;
1185 }
1186
XiaokangQian2b01dc32022-01-21 02:53:13 +00001187#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001188}
1189
1190int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1191{
1192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1193
1194 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1195
XiaokangQian78b1fa72022-01-19 06:56:30 +00001196 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001197
1198 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_SSL_RENEGOTIATION)
1200 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001201 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001202
1203 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1205 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001208
Hanno Beckerb0302c42021-08-03 09:39:42 +01001209 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001210 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001211 if( ssl->session )
1212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 mbedtls_ssl_session_free( ssl->session );
1214 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001215 ssl->session = NULL;
1216 }
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001219 ssl->alpn_chosen = NULL;
1220#endif
1221
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001222#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001223#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001224 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001225#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001226 {
1227 mbedtls_free( ssl->cli_id );
1228 ssl->cli_id = NULL;
1229 ssl->cli_id_len = 0;
1230 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001231#endif
1232
Paul Bakker48916f92012-09-16 19:57:18 +00001233 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1234 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001235
1236 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001237}
1238
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001239/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001240 * Reset an initialized and used SSL context for re-use while retaining
1241 * all application-set variables, function pointers and data.
1242 */
1243int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1244{
Hanno Becker43aefe22020-02-05 10:44:56 +00001245 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001246}
1247
1248/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 * SSL set accessors
1250 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001251void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001252{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001253 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001254}
1255
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001256void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001257{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001258 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001259}
1260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001262void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001263{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001264 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001265}
1266#endif
1267
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001268void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001270 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001271}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001274
Hanno Becker1841b0a2018-08-24 11:13:57 +01001275void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1276 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001277{
1278 ssl->disable_datagram_packing = !allow_packing;
1279}
1280
1281void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1282 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001283{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001284 conf->hs_timeout_min = min;
1285 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001286}
1287#endif
1288
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001289void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001290{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001291 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001292}
1293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001295void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001296 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001297 void *p_vrfy )
1298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001299 conf->f_vrfy = f_vrfy;
1300 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001303
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001304void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001305 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 void *p_rng )
1307{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001308 conf->f_rng = f_rng;
1309 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310}
1311
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001312void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001313 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 void *p_dbg )
1315{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001316 conf->f_dbg = f_dbg;
1317 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001318}
1319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001321 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001322 mbedtls_ssl_send_t *f_send,
1323 mbedtls_ssl_recv_t *f_recv,
1324 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001325{
1326 ssl->p_bio = p_bio;
1327 ssl->f_send = f_send;
1328 ssl->f_recv = f_recv;
1329 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001330}
1331
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001332#if defined(MBEDTLS_SSL_PROTO_DTLS)
1333void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1334{
1335 ssl->mtu = mtu;
1336}
1337#endif
1338
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001339void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001340{
1341 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001342}
1343
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001344void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1345 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001346 mbedtls_ssl_set_timer_t *f_set_timer,
1347 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001348{
1349 ssl->p_timer = p_timer;
1350 ssl->f_set_timer = f_set_timer;
1351 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001352
1353 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001354 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001355}
1356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001358void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001359 void *p_cache,
1360 mbedtls_ssl_cache_get_t *f_get_cache,
1361 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001363 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001364 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001365 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#if defined(MBEDTLS_SSL_CLI_C)
1370int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371{
Janos Follath865b3eb2019-12-16 11:46:15 +00001372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001373
1374 if( ssl == NULL ||
1375 session == NULL ||
1376 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001377 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001380 }
1381
Hanno Beckere810bbc2021-05-14 16:01:05 +01001382 if( ssl->handshake->resume == 1 )
1383 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1384
Jerry Yu21092062022-10-10 21:21:31 +08001385#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1386 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001387 {
Jerry Yu21092062022-10-10 21:21:31 +08001388 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1389 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1390
1391 if( mbedtls_ssl_validate_ciphersuite(
1392 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1393 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1394 {
1395 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1396 session->ciphersuite ) );
1397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1398 }
Jerry Yu40afab62022-10-08 10:42:13 +08001399 }
Jerry Yu21092062022-10-10 21:21:31 +08001400#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001401
Hanno Becker52055ae2019-02-06 14:30:46 +00001402 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1403 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001404 return( ret );
1405
Paul Bakker0a597072012-09-25 21:55:46 +00001406 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001407
1408 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001412void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1413 const int *ciphersuites )
1414{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001415 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001416}
1417
Ronald Cron6f135e12021-12-08 16:57:54 +01001418#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001419void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001420 const int kex_modes )
1421{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001422 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001423}
Ronald Cron6f135e12021-12-08 16:57:54 +01001424#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001427void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001428 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001429{
1430 conf->cert_profile = profile;
1431}
1432
Glenn Strauss36872db2022-01-22 05:06:31 -05001433static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1434{
1435 mbedtls_ssl_key_cert *cur = key_cert, *next;
1436
1437 while( cur != NULL )
1438 {
1439 next = cur->next;
1440 mbedtls_free( cur );
1441 cur = next;
1442 }
1443}
1444
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001445/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001446MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001447static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1448 mbedtls_x509_crt *cert,
1449 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001450{
niisato8ee24222018-06-25 19:05:48 +09001451 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001452
Glenn Strauss36872db2022-01-22 05:06:31 -05001453 if( cert == NULL )
1454 {
1455 /* Free list if cert is null */
1456 ssl_key_cert_free( *head );
1457 *head = NULL;
1458 return( 0 );
1459 }
1460
niisato8ee24222018-06-25 19:05:48 +09001461 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1462 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001463 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001464
niisato8ee24222018-06-25 19:05:48 +09001465 new_cert->cert = cert;
1466 new_cert->key = key;
1467 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001468
Glenn Strauss36872db2022-01-22 05:06:31 -05001469 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001470 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001471 {
niisato8ee24222018-06-25 19:05:48 +09001472 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001473 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001474 else
1475 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001476 mbedtls_ssl_key_cert *cur = *head;
1477 while( cur->next != NULL )
1478 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001479 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001480 }
1481
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001482 return( 0 );
1483}
1484
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001485int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001486 mbedtls_x509_crt *own_cert,
1487 mbedtls_pk_context *pk_key )
1488{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001489 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001490}
1491
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001492void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001493 mbedtls_x509_crt *ca_chain,
1494 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001495{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001496 conf->ca_chain = ca_chain;
1497 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001498
1499#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1500 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1501 * cannot be used together. */
1502 conf->f_ca_cb = NULL;
1503 conf->p_ca_cb = NULL;
1504#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001505}
Hanno Becker5adaad92019-03-27 16:54:37 +00001506
1507#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1508void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001509 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001510 void *p_ca_cb )
1511{
1512 conf->f_ca_cb = f_ca_cb;
1513 conf->p_ca_cb = p_ca_cb;
1514
1515 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1516 * cannot be used together. */
1517 conf->ca_chain = NULL;
1518 conf->ca_crl = NULL;
1519}
1520#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001522
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001523#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001524const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1525 size_t *name_len )
1526{
1527 *name_len = ssl->handshake->sni_name_len;
1528 return( ssl->handshake->sni_name );
1529}
1530
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001531int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1532 mbedtls_x509_crt *own_cert,
1533 mbedtls_pk_context *pk_key )
1534{
1535 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1536 own_cert, pk_key ) );
1537}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001538
1539void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1540 mbedtls_x509_crt *ca_chain,
1541 mbedtls_x509_crl *ca_crl )
1542{
1543 ssl->handshake->sni_ca_chain = ca_chain;
1544 ssl->handshake->sni_ca_crl = ca_crl;
1545}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001546
Glenn Strauss999ef702022-03-11 01:37:23 -05001547#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1548void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1549 const mbedtls_x509_crt *crt)
1550{
1551 ssl->handshake->dn_hints = crt;
1552}
1553#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1554
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001555void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1556 int authmode )
1557{
1558 ssl->handshake->sni_authmode = authmode;
1559}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001560#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1561
Hanno Becker8927c832019-04-03 12:52:50 +01001562#if defined(MBEDTLS_X509_CRT_PARSE_C)
1563void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1564 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1565 void *p_vrfy )
1566{
1567 ssl->f_vrfy = f_vrfy;
1568 ssl->p_vrfy = p_vrfy;
1569}
1570#endif
1571
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001572#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001573/*
1574 * Set EC J-PAKE password for current handshake
1575 */
1576int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1577 const unsigned char *pw,
1578 size_t pw_len )
1579{
1580 mbedtls_ecjpake_role role;
1581
Janos Follath8eb64132016-06-03 15:40:57 +01001582 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1584
1585 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1586 role = MBEDTLS_ECJPAKE_SERVER;
1587 else
1588 role = MBEDTLS_ECJPAKE_CLIENT;
1589
1590 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1591 role,
1592 MBEDTLS_MD_SHA256,
1593 MBEDTLS_ECP_DP_SECP256R1,
1594 pw, pw_len ) );
1595}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001596#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001597
Gilles Peskineeccd8882020-03-10 12:19:08 +01001598#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001599
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001600MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001601static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1602{
1603#if defined(MBEDTLS_USE_PSA_CRYPTO)
1604 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1605 return( 1 );
1606#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001607 if( conf->psk != NULL )
1608 return( 1 );
1609
1610 return( 0 );
1611}
1612
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001613static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1614{
1615 /* Remove reference to existing PSK, if any. */
1616#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001617 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001618 {
1619 /* The maintenance of the PSK key slot is the
1620 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001621 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001622 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001623#endif /* MBEDTLS_USE_PSA_CRYPTO */
1624 if( conf->psk != NULL )
1625 {
1626 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1627
1628 mbedtls_free( conf->psk );
1629 conf->psk = NULL;
1630 conf->psk_len = 0;
1631 }
1632
1633 /* Remove reference to PSK identity, if any. */
1634 if( conf->psk_identity != NULL )
1635 {
1636 mbedtls_free( conf->psk_identity );
1637 conf->psk_identity = NULL;
1638 conf->psk_identity_len = 0;
1639 }
1640}
1641
Hanno Becker7390c712018-11-15 13:33:04 +00001642/* This function assumes that PSK identity in the SSL config is unset.
1643 * It checks that the provided identity is well-formed and attempts
1644 * to make a copy of it in the SSL config.
1645 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001646MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001647static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1648 unsigned char const *psk_identity,
1649 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001650{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001651 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001652 if( psk_identity == NULL ||
1653 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001654 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001655 {
1656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1657 }
1658
Hanno Becker7390c712018-11-15 13:33:04 +00001659 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1660 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001661 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001662
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001663 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001664 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001665
1666 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001667}
1668
Hanno Becker7390c712018-11-15 13:33:04 +00001669int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1670 const unsigned char *psk, size_t psk_len,
1671 const unsigned char *psk_identity, size_t psk_identity_len )
1672{
Janos Follath865b3eb2019-12-16 11:46:15 +00001673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001674
1675 /* We currently only support one PSK, raw or opaque. */
1676 if( ssl_conf_psk_is_configured( conf ) )
1677 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001678
1679 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001680 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001682 if( psk_len == 0 )
1683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1684 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1686
Hanno Becker7390c712018-11-15 13:33:04 +00001687 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1688 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1689 conf->psk_len = psk_len;
1690 memcpy( conf->psk, psk, conf->psk_len );
1691
1692 /* Check and set PSK Identity */
1693 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1694 if( ret != 0 )
1695 ssl_conf_remove_psk( conf );
1696
1697 return( ret );
1698}
1699
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001700static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1701{
1702#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001703 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001704 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001705 /* The maintenance of the external PSK key slot is the
1706 * user's responsibility. */
1707 if( ssl->handshake->psk_opaque_is_internal )
1708 {
1709 psa_destroy_key( ssl->handshake->psk_opaque );
1710 ssl->handshake->psk_opaque_is_internal = 0;
1711 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001712 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001713 }
Neil Armstronge952a302022-05-03 10:22:14 +02001714#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001715 if( ssl->handshake->psk != NULL )
1716 {
1717 mbedtls_platform_zeroize( ssl->handshake->psk,
1718 ssl->handshake->psk_len );
1719 mbedtls_free( ssl->handshake->psk );
1720 ssl->handshake->psk_len = 0;
1721 }
Neil Armstronge952a302022-05-03 10:22:14 +02001722#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001723}
1724
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001725int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1726 const unsigned char *psk, size_t psk_len )
1727{
Neil Armstrong501c9322022-05-03 09:35:09 +02001728#if defined(MBEDTLS_USE_PSA_CRYPTO)
1729 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001731 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001733#endif /* MBEDTLS_USE_PSA_CRYPTO */
1734
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001735 if( psk == NULL || ssl->handshake == NULL )
1736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1737
1738 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1740
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001741 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001742
Neil Armstrong501c9322022-05-03 09:35:09 +02001743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001744#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1745 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1746 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001747 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001748 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1749 else
1750 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1751 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1752 }
1753#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001754
Jerry Yu568ec252022-07-22 21:27:34 +08001755#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001756 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1757 {
1758 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1759 psa_set_key_usage_flags( &key_attributes,
1760 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1761 }
1762#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1763
Neil Armstrong501c9322022-05-03 09:35:09 +02001764 psa_set_key_algorithm( &key_attributes, alg );
1765 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1766
1767 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1768 if( status != PSA_SUCCESS )
1769 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1770
1771 /* Allow calling psa_destroy_key() on psk remove */
1772 ssl->handshake->psk_opaque_is_internal = 1;
1773 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1774#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001775 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001777
1778 ssl->handshake->psk_len = psk_len;
1779 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1780
1781 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001782#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001783}
1784
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001785#if defined(MBEDTLS_USE_PSA_CRYPTO)
1786int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001787 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001788 const unsigned char *psk_identity,
1789 size_t psk_identity_len )
1790{
Janos Follath865b3eb2019-12-16 11:46:15 +00001791 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001792
1793 /* We currently only support one PSK, raw or opaque. */
1794 if( ssl_conf_psk_is_configured( conf ) )
1795 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001796
Hanno Becker7390c712018-11-15 13:33:04 +00001797 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001798 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001800 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001801
1802 /* Check and set PSK Identity */
1803 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1804 psk_identity_len );
1805 if( ret != 0 )
1806 ssl_conf_remove_psk( conf );
1807
1808 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001809}
1810
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001811int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1812 mbedtls_svc_key_id_t psk )
1813{
1814 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1815 ( ssl->handshake == NULL ) )
1816 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1817
1818 ssl_remove_psk( ssl );
1819 ssl->handshake->psk_opaque = psk;
1820 return( 0 );
1821}
1822#endif /* MBEDTLS_USE_PSA_CRYPTO */
1823
Jerry Yu8897c072022-08-12 13:56:53 +08001824#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001825void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1826 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1827 size_t),
1828 void *p_psk )
1829{
1830 conf->f_psk = f_psk;
1831 conf->p_psk = p_psk;
1832}
Jerry Yu8897c072022-08-12 13:56:53 +08001833#endif /* MBEDTLS_SSL_SRV_C */
1834
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001835#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1836
1837#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001838static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1839 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001840{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001841#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001842 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001843 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001844#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001845 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001846 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001847 return( MBEDTLS_SSL_MODE_STREAM );
1848}
1849
1850#else /* MBEDTLS_USE_PSA_CRYPTO */
1851
1852static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1853 mbedtls_cipher_mode_t mode )
1854{
1855#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1856 if( mode == MBEDTLS_MODE_CBC )
1857 return( MBEDTLS_SSL_MODE_CBC );
1858#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1859
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001860#if defined(MBEDTLS_GCM_C) || \
1861 defined(MBEDTLS_CCM_C) || \
1862 defined(MBEDTLS_CHACHAPOLY_C)
1863 if( mode == MBEDTLS_MODE_GCM ||
1864 mode == MBEDTLS_MODE_CCM ||
1865 mode == MBEDTLS_MODE_CHACHAPOLY )
1866 return( MBEDTLS_SSL_MODE_AEAD );
1867#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001868
1869 return( MBEDTLS_SSL_MODE_STREAM );
1870}
Gilles Peskine301711e2022-04-26 16:57:05 +02001871#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001872
Gilles Peskinee108d982022-04-26 16:50:40 +02001873static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1874 mbedtls_ssl_mode_t base_mode,
1875 int encrypt_then_mac )
1876{
1877#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1878 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1879 base_mode == MBEDTLS_SSL_MODE_CBC )
1880 {
1881 return( MBEDTLS_SSL_MODE_CBC_ETM );
1882 }
1883#else
1884 (void) encrypt_then_mac;
1885#endif
1886 return( base_mode );
1887}
1888
Neil Armstrongab555e02022-04-04 11:07:59 +02001889mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001890 const mbedtls_ssl_transform *transform )
1891{
Gilles Peskinee108d982022-04-26 16:50:40 +02001892 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001893#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001894 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001895#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001896 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1897#endif
1898 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001899
Gilles Peskinee108d982022-04-26 16:50:40 +02001900 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001901#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001902 encrypt_then_mac = transform->encrypt_then_mac;
1903#endif
1904 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001905}
1906
Neil Armstrongab555e02022-04-04 11:07:59 +02001907mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001908#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001909 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001910#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001911 const mbedtls_ssl_ciphersuite_t *suite )
1912{
Gilles Peskinee108d982022-04-26 16:50:40 +02001913 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1914
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001915#if defined(MBEDTLS_USE_PSA_CRYPTO)
1916 psa_status_t status;
1917 psa_algorithm_t alg;
1918 psa_key_type_t type;
1919 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001920 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1921 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001922 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001923#else
1924 const mbedtls_cipher_info_t *cipher =
1925 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001926 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001927 {
1928 base_mode =
1929 mbedtls_ssl_get_base_mode(
1930 mbedtls_cipher_info_get_mode( cipher ) );
1931 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001932#endif /* MBEDTLS_USE_PSA_CRYPTO */
1933
Gilles Peskinee108d982022-04-26 16:50:40 +02001934#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1935 int encrypt_then_mac = 0;
1936#endif
1937 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001938}
1939
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001940#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001941
1942#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001943/* Serialization of TLS 1.3 sessions:
1944 *
1945 * struct {
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00001946 * opaque hostname<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001947 * uint64 ticket_received;
1948 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001949 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001950 * } ClientOnlyData;
1951 *
1952 * struct {
1953 * uint8 endpoint;
1954 * uint8 ciphersuite[2];
1955 * uint32 ticket_age_add;
1956 * uint8 ticket_flags;
1957 * opaque resumption_key<0..255>;
1958 * select ( endpoint ) {
1959 * case client: ClientOnlyData;
1960 * case server: uint64 start_time;
1961 * };
1962 * } serialized_session_tls13;
1963 *
1964 */
1965#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001966MBEDTLS_CHECK_RETURN_CRITICAL
1967static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1968 unsigned char *buf,
1969 size_t buf_len,
1970 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001971{
1972 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001973#if defined(MBEDTLS_SSL_CLI_C) && \
1974 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1975 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001976 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001977#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001978 size_t needed = 1 /* endpoint */
1979 + 2 /* ciphersuite */
1980 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001981 + 1 /* ticket_flags */
1982 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001983 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001984
1985 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1987 needed += session->resumption_key_len; /* resumption_key */
1988
Jerry Yu438ddd82022-07-07 06:55:50 +00001989#if defined(MBEDTLS_HAVE_TIME)
1990 needed += 8; /* start_time or ticket_received */
1991#endif
1992
1993#if defined(MBEDTLS_SSL_CLI_C)
1994 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1995 {
Xiaokang Qian03409292022-10-12 02:49:52 +00001996#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
1997 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00001998 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00001999 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002000#endif
2001
Jerry Yu438ddd82022-07-07 06:55:50 +00002002 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002003 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002004
2005 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002006 if( session->ticket_len > SIZE_MAX - needed )
2007 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002008
Jerry Yue28d9742022-08-18 15:44:03 +08002009 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002010 }
2011#endif /* MBEDTLS_SSL_CLI_C */
2012
Jerry Yue36fdd62022-08-17 21:31:36 +08002013 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002014 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002015 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002016
2017 p[0] = session->endpoint;
2018 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2019 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2020 p[7] = session->ticket_flags;
2021
2022 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002023 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002024 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002025 memcpy( p, session->resumption_key, session->resumption_key_len );
2026 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002027
2028#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2029 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2030 {
2031 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2032 p += 8;
2033 }
2034#endif /* MBEDTLS_HAVE_TIME */
2035
2036#if defined(MBEDTLS_SSL_CLI_C)
2037 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2038 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002039#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2040 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2041 p += 2;
2042 if ( hostname_len > 0 )
2043 {
2044 /* save host name */
2045 memcpy( p, session->hostname, hostname_len );
2046 p += hostname_len;
2047 }
2048#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2049
Jerry Yu438ddd82022-07-07 06:55:50 +00002050#if defined(MBEDTLS_HAVE_TIME)
2051 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2052 p += 8;
2053#endif
2054 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2055 p += 4;
2056
2057 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2058 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002059
2060 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002061 {
2062 memcpy( p, session->ticket, session->ticket_len );
2063 p += session->ticket_len;
2064 }
2065 }
2066#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002067 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002068}
2069
2070MBEDTLS_CHECK_RETURN_CRITICAL
2071static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2072 const unsigned char *buf,
2073 size_t len )
2074{
2075 const unsigned char *p = buf;
2076 const unsigned char *end = buf + len;
2077
2078 if( end - p < 9 )
2079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2080 session->endpoint = p[0];
2081 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2082 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2083 session->ticket_flags = p[7];
2084
2085 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002086 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002087 p += 9;
2088
Jerry Yubc7c1a42022-07-21 22:57:37 +08002089 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2091
Jerry Yubc7c1a42022-07-21 22:57:37 +08002092 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002094 memcpy( session->resumption_key, p, session->resumption_key_len );
2095 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002096
2097#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2098 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2099 {
2100 if( end - p < 8 )
2101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2102 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2103 p += 8;
2104 }
2105#endif /* MBEDTLS_HAVE_TIME */
2106
2107#if defined(MBEDTLS_SSL_CLI_C)
2108 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2109 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002110#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2111 defined(MBEDTLS_SSL_SESSION_TICKETS)
2112 size_t hostname_len;
2113 /* load host name */
2114 if( end - p < 2 )
2115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2116 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2117 p += 2;
2118
2119 if( end - p < ( long int )hostname_len )
2120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2121 if( hostname_len > 0 )
2122 {
2123 session->hostname = mbedtls_calloc( 1, hostname_len );
2124 if( session->hostname == NULL )
2125 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2126 memcpy( session->hostname, p, hostname_len );
2127 p += hostname_len;
2128 }
2129#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2130 MBEDTLS_SSL_SESSION_TICKETS */
2131
Jerry Yu438ddd82022-07-07 06:55:50 +00002132#if defined(MBEDTLS_HAVE_TIME)
2133 if( end - p < 8 )
2134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2135 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2136 p += 8;
2137#endif
2138 if( end - p < 4 )
2139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2140 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2141 p += 4;
2142
2143 if( end - p < 2 )
2144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2145 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2146 p += 2;
2147
2148 if( end - p < ( long int )session->ticket_len )
2149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2150 if( session->ticket_len > 0 )
2151 {
2152 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2153 if( session->ticket == NULL )
2154 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2155 memcpy( session->ticket, p, session->ticket_len );
2156 p += session->ticket_len;
2157 }
2158 }
2159#endif /* MBEDTLS_SSL_CLI_C */
2160
2161 return( 0 );
2162
2163}
2164#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002165MBEDTLS_CHECK_RETURN_CRITICAL
2166static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2167 unsigned char *buf,
2168 size_t buf_len,
2169 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002170{
2171 ((void) session);
2172 ((void) buf);
2173 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002174 *olen = 0;
2175 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002176}
Jerry Yu438ddd82022-07-07 06:55:50 +00002177
2178static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2179 unsigned char *buf,
2180 size_t buf_len )
2181{
2182 ((void) session);
2183 ((void) buf);
2184 ((void) buf_len);
2185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2186}
2187#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002188#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2189
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002190psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002191 size_t taglen,
2192 psa_algorithm_t *alg,
2193 psa_key_type_t *key_type,
2194 size_t *key_size )
2195{
2196 switch ( mbedtls_cipher_type )
2197 {
2198 case MBEDTLS_CIPHER_AES_128_CBC:
2199 *alg = PSA_ALG_CBC_NO_PADDING;
2200 *key_type = PSA_KEY_TYPE_AES;
2201 *key_size = 128;
2202 break;
2203 case MBEDTLS_CIPHER_AES_128_CCM:
2204 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2205 *key_type = PSA_KEY_TYPE_AES;
2206 *key_size = 128;
2207 break;
2208 case MBEDTLS_CIPHER_AES_128_GCM:
2209 *alg = PSA_ALG_GCM;
2210 *key_type = PSA_KEY_TYPE_AES;
2211 *key_size = 128;
2212 break;
2213 case MBEDTLS_CIPHER_AES_192_CCM:
2214 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2215 *key_type = PSA_KEY_TYPE_AES;
2216 *key_size = 192;
2217 break;
2218 case MBEDTLS_CIPHER_AES_192_GCM:
2219 *alg = PSA_ALG_GCM;
2220 *key_type = PSA_KEY_TYPE_AES;
2221 *key_size = 192;
2222 break;
2223 case MBEDTLS_CIPHER_AES_256_CBC:
2224 *alg = PSA_ALG_CBC_NO_PADDING;
2225 *key_type = PSA_KEY_TYPE_AES;
2226 *key_size = 256;
2227 break;
2228 case MBEDTLS_CIPHER_AES_256_CCM:
2229 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2230 *key_type = PSA_KEY_TYPE_AES;
2231 *key_size = 256;
2232 break;
2233 case MBEDTLS_CIPHER_AES_256_GCM:
2234 *alg = PSA_ALG_GCM;
2235 *key_type = PSA_KEY_TYPE_AES;
2236 *key_size = 256;
2237 break;
2238 case MBEDTLS_CIPHER_ARIA_128_CBC:
2239 *alg = PSA_ALG_CBC_NO_PADDING;
2240 *key_type = PSA_KEY_TYPE_ARIA;
2241 *key_size = 128;
2242 break;
2243 case MBEDTLS_CIPHER_ARIA_128_CCM:
2244 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2245 *key_type = PSA_KEY_TYPE_ARIA;
2246 *key_size = 128;
2247 break;
2248 case MBEDTLS_CIPHER_ARIA_128_GCM:
2249 *alg = PSA_ALG_GCM;
2250 *key_type = PSA_KEY_TYPE_ARIA;
2251 *key_size = 128;
2252 break;
2253 case MBEDTLS_CIPHER_ARIA_192_CCM:
2254 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2255 *key_type = PSA_KEY_TYPE_ARIA;
2256 *key_size = 192;
2257 break;
2258 case MBEDTLS_CIPHER_ARIA_192_GCM:
2259 *alg = PSA_ALG_GCM;
2260 *key_type = PSA_KEY_TYPE_ARIA;
2261 *key_size = 192;
2262 break;
2263 case MBEDTLS_CIPHER_ARIA_256_CBC:
2264 *alg = PSA_ALG_CBC_NO_PADDING;
2265 *key_type = PSA_KEY_TYPE_ARIA;
2266 *key_size = 256;
2267 break;
2268 case MBEDTLS_CIPHER_ARIA_256_CCM:
2269 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2270 *key_type = PSA_KEY_TYPE_ARIA;
2271 *key_size = 256;
2272 break;
2273 case MBEDTLS_CIPHER_ARIA_256_GCM:
2274 *alg = PSA_ALG_GCM;
2275 *key_type = PSA_KEY_TYPE_ARIA;
2276 *key_size = 256;
2277 break;
2278 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2279 *alg = PSA_ALG_CBC_NO_PADDING;
2280 *key_type = PSA_KEY_TYPE_CAMELLIA;
2281 *key_size = 128;
2282 break;
2283 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2284 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2285 *key_type = PSA_KEY_TYPE_CAMELLIA;
2286 *key_size = 128;
2287 break;
2288 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2289 *alg = PSA_ALG_GCM;
2290 *key_type = PSA_KEY_TYPE_CAMELLIA;
2291 *key_size = 128;
2292 break;
2293 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2294 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2295 *key_type = PSA_KEY_TYPE_CAMELLIA;
2296 *key_size = 192;
2297 break;
2298 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2299 *alg = PSA_ALG_GCM;
2300 *key_type = PSA_KEY_TYPE_CAMELLIA;
2301 *key_size = 192;
2302 break;
2303 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2304 *alg = PSA_ALG_CBC_NO_PADDING;
2305 *key_type = PSA_KEY_TYPE_CAMELLIA;
2306 *key_size = 256;
2307 break;
2308 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2309 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2310 *key_type = PSA_KEY_TYPE_CAMELLIA;
2311 *key_size = 256;
2312 break;
2313 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2314 *alg = PSA_ALG_GCM;
2315 *key_type = PSA_KEY_TYPE_CAMELLIA;
2316 *key_size = 256;
2317 break;
2318 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2319 *alg = PSA_ALG_CHACHA20_POLY1305;
2320 *key_type = PSA_KEY_TYPE_CHACHA20;
2321 *key_size = 256;
2322 break;
2323 case MBEDTLS_CIPHER_NULL:
2324 *alg = MBEDTLS_SSL_NULL_CIPHER;
2325 *key_type = 0;
2326 *key_size = 0;
2327 break;
2328 default:
2329 return PSA_ERROR_NOT_SUPPORTED;
2330 }
2331
2332 return PSA_SUCCESS;
2333}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002334#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002335
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002336#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002337int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2338 const unsigned char *dhm_P, size_t P_len,
2339 const unsigned char *dhm_G, size_t G_len )
2340{
Janos Follath865b3eb2019-12-16 11:46:15 +00002341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002342
Glenn Strausscee11292021-12-20 01:43:17 -05002343 mbedtls_mpi_free( &conf->dhm_P );
2344 mbedtls_mpi_free( &conf->dhm_G );
2345
Hanno Beckera90658f2017-10-04 15:29:08 +01002346 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2347 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2348 {
2349 mbedtls_mpi_free( &conf->dhm_P );
2350 mbedtls_mpi_free( &conf->dhm_G );
2351 return( ret );
2352 }
2353
2354 return( 0 );
2355}
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002357int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002358{
Janos Follath865b3eb2019-12-16 11:46:15 +00002359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002360
Glenn Strausscee11292021-12-20 01:43:17 -05002361 mbedtls_mpi_free( &conf->dhm_P );
2362 mbedtls_mpi_free( &conf->dhm_G );
2363
Gilles Peskinee5702482021-06-11 21:59:08 +02002364 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2365 &conf->dhm_P ) ) != 0 ||
2366 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2367 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002368 {
2369 mbedtls_mpi_free( &conf->dhm_P );
2370 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002371 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002372 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002373
2374 return( 0 );
2375}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002376#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002377
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002378#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2379/*
2380 * Set the minimum length for Diffie-Hellman parameters
2381 */
2382void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2383 unsigned int bitlen )
2384{
2385 conf->dhm_min_bitlen = bitlen;
2386}
2387#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2388
Gilles Peskineeccd8882020-03-10 12:19:08 +01002389#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002390#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002391/*
2392 * Set allowed/preferred hashes for handshake signatures
2393 */
2394void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2395 const int *hashes )
2396{
2397 conf->sig_hashes = hashes;
2398}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002399#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002400
Jerry Yuf017ee42022-01-12 15:49:48 +08002401/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002402void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2403 const uint16_t* sig_algs )
2404{
Jerry Yuf017ee42022-01-12 15:49:48 +08002405#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2406 conf->sig_hashes = NULL;
2407#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2408 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002409}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002410#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002411
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002412#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002413#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002414/*
2415 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002416 *
2417 * mbedtls_ssl_setup() takes the provided list
2418 * and translates it to a list of IANA TLS group identifiers,
2419 * stored in ssl->handshake->group_list.
2420 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002421 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002422void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002423 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002425 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002426 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002427}
Brett Warrene0edc842021-08-17 09:53:13 +01002428#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002429#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002430
Brett Warrene0edc842021-08-17 09:53:13 +01002431/*
2432 * Set the allowed groups
2433 */
2434void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2435 const uint16_t *group_list )
2436{
2437#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2438 conf->curve_list = NULL;
2439#endif
2440 conf->group_list = group_list;
2441}
2442
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002443#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445{
Hanno Becker947194e2017-04-07 13:25:49 +01002446 /* Initialize to suppress unnecessary compiler warning */
2447 size_t hostname_len = 0;
2448
2449 /* Check if new hostname is valid before
2450 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002451 if( hostname != NULL )
2452 {
2453 hostname_len = strlen( hostname );
2454
2455 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2457 }
2458
2459 /* Now it's clear that we will overwrite the old hostname,
2460 * so we can free it safely */
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002461
Hanno Becker947194e2017-04-07 13:25:49 +01002462 if( ssl->hostname != NULL )
2463 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002464 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002465 mbedtls_free( ssl->hostname );
2466 }
2467
2468 /* Passing NULL as hostname shall clear the old one */
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002469
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002471 {
2472 ssl->hostname = NULL;
2473 }
2474 else
2475 {
2476 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002477 if( ssl->hostname == NULL )
2478 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002479
Hanno Becker947194e2017-04-07 13:25:49 +01002480 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002481
Hanno Becker947194e2017-04-07 13:25:49 +01002482 ssl->hostname[hostname_len] = '\0';
2483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
2485 return( 0 );
2486}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002487#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002489#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002490void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002492 const unsigned char *, size_t),
2493 void *p_sni )
2494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002495 conf->f_sni = f_sni;
2496 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002501int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002502{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002503 size_t cur_len, tot_len;
2504 const char **p;
2505
2506 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002507 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2508 * MUST NOT be truncated."
2509 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002510 */
2511 tot_len = 0;
2512 for( p = protos; *p != NULL; p++ )
2513 {
2514 cur_len = strlen( *p );
2515 tot_len += cur_len;
2516
Ronald Cron8216dd32020-04-23 16:41:44 +02002517 if( ( cur_len == 0 ) ||
2518 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2519 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002521 }
2522
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002523 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002524
2525 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002526}
2527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002529{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002530 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002533
Johan Pascalb62bb512015-12-03 21:56:45 +01002534#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002535void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2536 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002537{
2538 conf->dtls_srtp_mki_support = support_mki_value;
2539}
2540
Ron Eldoref72faf2018-07-12 11:54:20 +03002541int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2542 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002543 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002544{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002545 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002546 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002548 }
Ron Eldor591f1622018-01-22 12:30:04 +02002549
2550 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002551 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002552 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002553 }
Ron Eldor591f1622018-01-22 12:30:04 +02002554
2555 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2556 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002557 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002558}
2559
Ron Eldoref72faf2018-07-12 11:54:20 +03002560int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002561 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002562{
Johan Pascal253d0262020-09-22 13:04:45 +02002563 const mbedtls_ssl_srtp_profile *p;
2564 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002565
Johan Pascal253d0262020-09-22 13:04:45 +02002566 /* check the profiles list: all entry must be valid,
2567 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002568 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2569 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2570 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002571 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002572 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002573 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002574 list_size++;
2575 }
2576 else
2577 {
2578 /* unsupported value, stop parsing and set the size to an error value */
2579 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002580 }
2581 }
2582
Johan Pascal5ef72d22020-10-28 17:05:47 +01002583 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002584 {
Johan Pascal253d0262020-09-22 13:04:45 +02002585 conf->dtls_srtp_profile_list = NULL;
2586 conf->dtls_srtp_profile_list_len = 0;
2587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2588 }
2589
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002590 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002591 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002592
2593 return( 0 );
2594}
2595
Johan Pascal5ef72d22020-10-28 17:05:47 +01002596void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2597 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002598{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002599 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2600 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002601 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002602 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002603 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002604 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002605 else
2606 {
2607 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002608 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2609 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002610 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002611}
Johan Pascalb62bb512015-12-03 21:56:45 +01002612#endif /* MBEDTLS_SSL_DTLS_SRTP */
2613
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302614#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002615void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002616{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002617 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002618}
2619
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002620void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002621{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002622 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002623}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302624#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002625
Janos Follath088ce432017-04-10 12:42:31 +01002626#if defined(MBEDTLS_SSL_SRV_C)
2627void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2628 char cert_req_ca_list )
2629{
2630 conf->cert_req_ca_list = cert_req_ca_list;
2631}
2632#endif
2633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002635void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002636{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002637 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002638}
2639#endif
2640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002642void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002643{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002644 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002645}
2646#endif
2647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002649int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002652 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002655 }
2656
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002657 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002658
2659 return( 0 );
2660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002662
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002663void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002665 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002666}
2667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002669void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002671 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002672}
2673
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002674void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002676 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002677}
2678
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002679void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002680 const unsigned char period[8] )
2681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002682 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002687#if defined(MBEDTLS_SSL_CLI_C)
2688void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002689{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002690 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002691}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002692#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002693
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002694#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002695
Jerry Yud0766ec2022-09-22 10:46:57 +08002696#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002697void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2698 uint16_t num_tickets )
2699{
Jerry Yud0766ec2022-09-22 10:46:57 +08002700 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002701}
2702#endif
2703
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002704void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2705 mbedtls_ssl_ticket_write_t *f_ticket_write,
2706 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2707 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002708{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002709 conf->f_ticket_write = f_ticket_write;
2710 conf->f_ticket_parse = f_ticket_parse;
2711 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002712}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002715
Hanno Becker7e6c1782021-06-08 09:24:55 +01002716void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2717 mbedtls_ssl_export_keys_t *f_export_keys,
2718 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002719{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002720 ssl->f_export_keys = f_export_keys;
2721 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002722}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002723
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002724#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002725void mbedtls_ssl_conf_async_private_cb(
2726 mbedtls_ssl_config *conf,
2727 mbedtls_ssl_async_sign_t *f_async_sign,
2728 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2729 mbedtls_ssl_async_resume_t *f_async_resume,
2730 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002731 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002732{
2733 conf->f_async_sign_start = f_async_sign;
2734 conf->f_async_decrypt_start = f_async_decrypt;
2735 conf->f_async_resume = f_async_resume;
2736 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002737 conf->p_async_config_data = async_config_data;
2738}
2739
Gilles Peskine8f97af72018-04-26 11:46:10 +02002740void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2741{
2742 return( conf->p_async_config_data );
2743}
2744
Gilles Peskine1febfef2018-04-30 11:54:39 +02002745void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002746{
2747 if( ssl->handshake == NULL )
2748 return( NULL );
2749 else
2750 return( ssl->handshake->user_async_ctx );
2751}
2752
Gilles Peskine1febfef2018-04-30 11:54:39 +02002753void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002754 void *ctx )
2755{
2756 if( ssl->handshake != NULL )
2757 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002758}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002759#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002760
Paul Bakker5121ce52009-01-03 21:22:43 +00002761/*
2762 * SSL get accessors
2763 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002764uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002765{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002766 if( ssl->session != NULL )
2767 return( ssl->session->verify_result );
2768
2769 if( ssl->session_negotiate != NULL )
2770 return( ssl->session_negotiate->verify_result );
2771
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002772 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773}
2774
Glenn Strauss8f526902022-01-13 00:04:49 -05002775int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2776{
2777 if( ssl == NULL || ssl->session == NULL )
2778 return( 0 );
2779
2780 return( ssl->session->ciphersuite );
2781}
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002784{
Paul Bakker926c8e42013-03-06 10:23:34 +01002785 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002786 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002789}
2790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002792{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002795 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002796 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002797 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002798 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002799 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002800 default:
2801 return( "unknown (DTLS)" );
2802 }
2803 }
2804#endif
2805
Glenn Strauss60bfe602022-03-14 19:04:24 -04002806 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002807 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002808 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002809 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002810 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002811 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002812 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002813 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002814 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002815}
2816
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002817#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002818size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2819{
David Horstmann95d516f2021-05-04 18:36:56 +01002820 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002821 size_t read_mfl;
2822
2823 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2824 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2825 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2826 {
2827 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2828 }
2829
2830 /* Check if a smaller max length was negotiated */
2831 if( ssl->session_out != NULL )
2832 {
2833 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2834 if( read_mfl < max_len )
2835 {
2836 max_len = read_mfl;
2837 }
2838 }
2839
2840 // During a handshake, use the value being negotiated
2841 if( ssl->session_negotiate != NULL )
2842 {
2843 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2844 if( read_mfl < max_len )
2845 {
2846 max_len = read_mfl;
2847 }
2848 }
2849
2850 return( max_len );
2851}
2852
2853size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002854{
2855 size_t max_len;
2856
2857 /*
2858 * Assume mfl_code is correct since it was checked when set
2859 */
Angus Grattond8213d02016-05-25 20:56:48 +10002860 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002861
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002862 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002863 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002864 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002865 {
Angus Grattond8213d02016-05-25 20:56:48 +10002866 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002867 }
2868
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002869 /* During a handshake, use the value being negotiated */
2870 if( ssl->session_negotiate != NULL &&
2871 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2872 {
2873 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2874 }
2875
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002876 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002877}
2878#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2879
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002881size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002882{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002883 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2885 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2886 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2887 return ( 0 );
2888
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002889 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2890 return( ssl->mtu );
2891
2892 if( ssl->mtu == 0 )
2893 return( ssl->handshake->mtu );
2894
2895 return( ssl->mtu < ssl->handshake->mtu ?
2896 ssl->mtu : ssl->handshake->mtu );
2897}
2898#endif /* MBEDTLS_SSL_PROTO_DTLS */
2899
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002900int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2901{
2902 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2903
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002904#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2905 !defined(MBEDTLS_SSL_PROTO_DTLS)
2906 (void) ssl;
2907#endif
2908
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002909#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002910 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002911
2912 if( max_len > mfl )
2913 max_len = mfl;
2914#endif
2915
2916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002917 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002918 {
Hanno Becker89490712020-02-05 10:50:12 +00002919 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002920 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2921 const size_t overhead = (size_t) ret;
2922
2923 if( ret < 0 )
2924 return( ret );
2925
2926 if( mtu <= overhead )
2927 {
2928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2930 }
2931
2932 if( max_len > mtu - overhead )
2933 max_len = mtu - overhead;
2934 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002935#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002936
Hanno Becker0defedb2018-08-10 12:35:02 +01002937#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2938 !defined(MBEDTLS_SSL_PROTO_DTLS)
2939 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002940#endif
2941
2942 return( (int) max_len );
2943}
2944
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002945int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2946{
2947 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2948
2949#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2950 (void) ssl;
2951#endif
2952
2953#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2954 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2955
2956 if( max_len > mfl )
2957 max_len = mfl;
2958#endif
2959
2960 return( (int) max_len );
2961}
2962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963#if defined(MBEDTLS_X509_CRT_PARSE_C)
2964const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002965{
2966 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002967 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002968
Hanno Beckere6824572019-02-07 13:18:46 +00002969#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002970 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002971#else
2972 return( NULL );
2973#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002978int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2979 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002980{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002981 int ret;
2982
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002983 if( ssl == NULL ||
2984 dst == NULL ||
2985 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002986 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002989 }
2990
Hanno Beckere810bbc2021-05-14 16:01:05 +01002991 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2992 * idempotent: Each session can only be exported once.
2993 *
2994 * (This is in preparation for TLS 1.3 support where we will
2995 * need the ability to export multiple sessions (aka tickets),
2996 * which will be achieved by calling mbedtls_ssl_get_session()
2997 * multiple times until it fails.)
2998 *
2999 * Check whether we have already exported the current session,
3000 * and fail if so.
3001 */
3002 if( ssl->session->exported == 1 )
3003 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3004
3005 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3006 if( ret != 0 )
3007 return( ret );
3008
3009 /* Remember that we've exported the session. */
3010 ssl->session->exported = 1;
3011 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003014
Paul Bakker5121ce52009-01-03 21:22:43 +00003015/*
Hanno Beckera835da52019-05-16 12:39:07 +01003016 * Define ticket header determining Mbed TLS version
3017 * and structure of the ticket.
3018 */
3019
Hanno Becker94ef3b32019-05-16 12:50:45 +01003020/*
Hanno Becker50b59662019-05-28 14:30:45 +01003021 * Define bitflag determining compile-time settings influencing
3022 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003023 */
3024
Hanno Becker50b59662019-05-28 14:30:45 +01003025#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003026#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003027#else
Hanno Becker3e088662019-05-29 11:10:18 +01003028#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003029#endif /* MBEDTLS_HAVE_TIME */
3030
3031#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003032#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003033#else
Hanno Becker3e088662019-05-29 11:10:18 +01003034#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003035#endif /* MBEDTLS_X509_CRT_PARSE_C */
3036
3037#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003038#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003039#else
Hanno Becker3e088662019-05-29 11:10:18 +01003040#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003041#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3042
3043#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003044#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003045#else
Hanno Becker3e088662019-05-29 11:10:18 +01003046#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003047#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3048
Hanno Becker94ef3b32019-05-16 12:50:45 +01003049#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003050#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003051#else
Hanno Becker3e088662019-05-29 11:10:18 +01003052#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003053#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3054
Hanno Becker94ef3b32019-05-16 12:50:45 +01003055#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3056#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3057#else
3058#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3059#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3060
Hanno Becker3e088662019-05-29 11:10:18 +01003061#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3062#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3063#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3064#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003065#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3066#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003067
Hanno Becker50b59662019-05-28 14:30:45 +01003068#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003069 ( (uint16_t) ( \
3070 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3071 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3072 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3073 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003074 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003075 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003076
Hanno Beckerf8787072019-05-16 12:41:07 +01003077static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003078 MBEDTLS_VERSION_MAJOR,
3079 MBEDTLS_VERSION_MINOR,
3080 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003081 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3082 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003083};
Hanno Beckera835da52019-05-16 12:39:07 +01003084
3085/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003086 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003087 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003088 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003089 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003090 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003091 * opaque mbedtls_version[3]; // library version: major, minor, patch
3092 * opaque session_format[2]; // library-version specific 16-bit field
3093 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003094 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003095 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003096 * Note: When updating the format, remember to keep
3097 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003098 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003099 * // In this version, `session_format` determines
3100 * // the setting of those compile-time
3101 * // configuration options which influence
3102 * // the structure of mbedtls_ssl_session.
3103 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003104 * uint8_t minor_ver; // Protocol minor version. Possible values:
3105 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003106 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003107 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003108 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003109 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003110 * serialized_session_tls12 data;
3111 *
3112 * };
3113 *
3114 * } serialized_session;
3115 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003116 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003117
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003118MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003119static int ssl_session_save( const mbedtls_ssl_session *session,
3120 unsigned char omit_header,
3121 unsigned char *buf,
3122 size_t buf_len,
3123 size_t *olen )
3124{
3125 unsigned char *p = buf;
3126 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003127 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003128#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3129 size_t out_len;
3130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3131#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003132 if( session == NULL )
3133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3134
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003135 if( !omit_header )
3136 {
3137 /*
3138 * Add Mbed TLS version identifier
3139 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003140 used += sizeof( ssl_serialized_session_header );
3141
3142 if( used <= buf_len )
3143 {
3144 memcpy( p, ssl_serialized_session_header,
3145 sizeof( ssl_serialized_session_header ) );
3146 p += sizeof( ssl_serialized_session_header );
3147 }
3148 }
3149
3150 /*
3151 * TLS version identifier
3152 */
3153 used += 1;
3154 if( used <= buf_len )
3155 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003156 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003157 }
3158
3159 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003160 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003161 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003162 {
3163#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003164 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003165 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003166 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3168
Jerry Yu251a12e2022-07-13 15:15:48 +08003169#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3170 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003171 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3172 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003173 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003174 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003175 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003176#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3177
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003178 default:
3179 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3180 }
3181
3182 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003183 if( used > buf_len )
3184 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003185
3186 return( 0 );
3187}
3188
3189/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003190 * Public wrapper for ssl_session_save()
3191 */
3192int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3193 unsigned char *buf,
3194 size_t buf_len,
3195 size_t *olen )
3196{
3197 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3198}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003199
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003200/*
3201 * Deserialize session, see mbedtls_ssl_session_save() for format.
3202 *
3203 * This internal version is wrapped by a public function that cleans up in
3204 * case of error, and has an extra option omit_header.
3205 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003206MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003207static int ssl_session_load( mbedtls_ssl_session *session,
3208 unsigned char omit_header,
3209 const unsigned char *buf,
3210 size_t len )
3211{
3212 const unsigned char *p = buf;
3213 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003214 size_t remaining_len;
3215
3216
3217 if( session == NULL )
3218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003219
3220 if( !omit_header )
3221 {
3222 /*
3223 * Check Mbed TLS version identifier
3224 */
3225
3226 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3228
3229 if( memcmp( p, ssl_serialized_session_header,
3230 sizeof( ssl_serialized_session_header ) ) != 0 )
3231 {
3232 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3233 }
3234 p += sizeof( ssl_serialized_session_header );
3235 }
3236
3237 /*
3238 * TLS version identifier
3239 */
3240 if( 1 > (size_t)( end - p ) )
3241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003242 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003243
3244 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003245 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003246 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003247 {
3248#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003249 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003250 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003251#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3252
Jerry Yu438ddd82022-07-07 06:55:50 +00003253#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3254 case MBEDTLS_SSL_VERSION_TLS1_3:
3255 return( ssl_tls13_session_load( session, p, remaining_len ) );
3256#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3257
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003258 default:
3259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3260 }
3261}
3262
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003263/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003264 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003265 */
3266int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3267 const unsigned char *buf,
3268 size_t len )
3269{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003270 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003271
3272 if( ret != 0 )
3273 mbedtls_ssl_session_free( session );
3274
3275 return( ret );
3276}
3277
3278/*
Paul Bakker1961b702013-01-25 14:49:24 +01003279 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003281MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003282static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3283{
3284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3285
Ronald Cron66dbf912022-02-02 15:33:46 +01003286 /*
3287 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003288 * that were written into the output buffer by the previous handshake step,
3289 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003290 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3291 * We proceed to the next handshake step only when all data from the
3292 * previous one have been sent to the peer, thus we make sure that this is
3293 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3294 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3295 * we have to wait before to go ahead.
3296 * In the case of TLS 1.3, handshake step handlers do not send data to the
3297 * peer. Data are only sent here and through
3298 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003299 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003300 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003301 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3302 return( ret );
3303
3304#if defined(MBEDTLS_SSL_PROTO_DTLS)
3305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3306 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3307 {
3308 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3309 return( ret );
3310 }
3311#endif /* MBEDTLS_SSL_PROTO_DTLS */
3312
3313 return( ret );
3314}
3315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003317{
Hanno Becker41934dd2021-08-07 19:13:43 +01003318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003319
Hanno Becker41934dd2021-08-07 19:13:43 +01003320 if( ssl == NULL ||
3321 ssl->conf == NULL ||
3322 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003323 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003324 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003326 }
3327
3328 ret = ssl_prepare_handshake_step( ssl );
3329 if( ret != 0 )
3330 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003331
Jerry Yue7047812021-09-13 19:26:39 +08003332 ret = mbedtls_ssl_handle_pending_alert( ssl );
3333 if( ret != 0 )
3334 goto cleanup;
3335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003337 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003338 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3340 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003341
Ronald Cron9f0fba32022-02-10 16:45:15 +01003342 switch( ssl->state )
3343 {
3344 case MBEDTLS_SSL_HELLO_REQUEST:
3345 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3346 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003347
Ronald Cron9f0fba32022-02-10 16:45:15 +01003348 case MBEDTLS_SSL_CLIENT_HELLO:
3349 ret = mbedtls_ssl_write_client_hello( ssl );
3350 break;
3351
3352 default:
3353#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003354 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003355 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3356 else
3357 ret = mbedtls_ssl_handshake_client_step( ssl );
3358#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3359 ret = mbedtls_ssl_handshake_client_step( ssl );
3360#else
3361 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3362#endif
3363 }
Jerry Yub9930e72021-08-06 17:11:51 +08003364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003368 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003369#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003370 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003371 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003372#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003373
3374#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3375 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3376 ret = mbedtls_ssl_handshake_server_step( ssl );
3377#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3378 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003379#endif
3380
Jerry Yue7047812021-09-13 19:26:39 +08003381 if( ret != 0 )
3382 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003383 /* handshake_step return error. And it is same
3384 * with alert_reason.
3385 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003386 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003387 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003388 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003389 goto cleanup;
3390 }
3391 }
3392
3393cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003394 return( ret );
3395}
3396
3397/*
3398 * Perform the SSL handshake
3399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003401{
3402 int ret = 0;
3403
Hanno Beckera817ea42020-10-20 15:20:23 +01003404 /* Sanity checks */
3405
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003406 if( ssl == NULL || ssl->conf == NULL )
3407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3408
Hanno Beckera817ea42020-10-20 15:20:23 +01003409#if defined(MBEDTLS_SSL_PROTO_DTLS)
3410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3411 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3412 {
3413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3414 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3415 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3416 }
3417#endif /* MBEDTLS_SSL_PROTO_DTLS */
3418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003420
Hanno Beckera817ea42020-10-20 15:20:23 +01003421 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003422 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003425
3426 if( ret != 0 )
3427 break;
3428 }
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003431
3432 return( ret );
3433}
3434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#if defined(MBEDTLS_SSL_RENEGOTIATION)
3436#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003437/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003438 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003439 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003440MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003442{
Janos Follath865b3eb2019-12-16 11:46:15 +00003443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003446
3447 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3449 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003450
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003451 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003452 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003454 return( ret );
3455 }
3456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003458
3459 return( 0 );
3460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003462
3463/*
3464 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 * - any side: calling mbedtls_ssl_renegotiate(),
3466 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3467 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003468 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003469 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003471 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003472int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003473{
Janos Follath865b3eb2019-12-16 11:46:15 +00003474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003477
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003478 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3479 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003480
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003481 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3482 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003484 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003486 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003487 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003488 ssl->handshake->out_msg_seq = 1;
3489 else
3490 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003491 }
3492#endif
3493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3495 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003500 return( ret );
3501 }
3502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003504
3505 return( 0 );
3506}
3507
3508/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003509 * Renegotiate current connection on client,
3510 * or request renegotiation on server
3511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003515
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003516 if( ssl == NULL || ssl->conf == NULL )
3517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003520 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003521 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003522 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003523 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003527
3528 /* Did we already try/start sending HelloRequest? */
3529 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003531
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003532 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003537 /*
3538 * On client, either start the renegotiation process or,
3539 * if already in progress, continue the handshake
3540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003542 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003543 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003545
Hanno Becker40cdaa12020-02-05 10:48:27 +00003546 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003547 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003549 return( ret );
3550 }
3551 }
3552 else
3553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003557 return( ret );
3558 }
3559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003561
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003562 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003563}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003565
Gilles Peskine9b562d52018-04-25 20:32:43 +02003566void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003567{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003568 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3569
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003570 if( handshake == NULL )
3571 return;
3572
Brett Warrene0edc842021-08-17 09:53:13 +01003573#if defined(MBEDTLS_ECP_C)
3574#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3575 if ( ssl->handshake->group_list_heap_allocated )
3576 mbedtls_free( (void*) handshake->group_list );
3577 handshake->group_list = NULL;
3578#endif /* MBEDTLS_DEPRECATED_REMOVED */
3579#endif /* MBEDTLS_ECP_C */
3580
Jerry Yuf017ee42022-01-12 15:49:48 +08003581#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3582#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3583 if ( ssl->handshake->sig_algs_heap_allocated )
3584 mbedtls_free( (void*) handshake->sig_algs );
3585 handshake->sig_algs = NULL;
3586#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003587#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3588 if( ssl->handshake->certificate_request_context )
3589 {
3590 mbedtls_free( (void*) handshake->certificate_request_context );
3591 }
3592#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003593#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3594
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003595#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3596 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3597 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003598 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003599 handshake->async_in_progress = 0;
3600 }
3601#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3602
Andrzej Kurek25f27152022-08-17 16:09:31 -04003603#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003604#if defined(MBEDTLS_USE_PSA_CRYPTO)
3605 psa_hash_abort( &handshake->fin_sha256_psa );
3606#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003607 mbedtls_sha256_free( &handshake->fin_sha256 );
3608#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003609#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003610#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003611#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003612 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003613#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003614 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003615#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003616#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618#if defined(MBEDTLS_DHM_C)
3619 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003620#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003621#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003623#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003624#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003625 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003626#if defined(MBEDTLS_SSL_CLI_C)
3627 mbedtls_free( handshake->ecjpake_cache );
3628 handshake->ecjpake_cache = NULL;
3629 handshake->ecjpake_cache_len = 0;
3630#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003631#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003632
Janos Follath4ae5c292016-02-10 11:27:43 +00003633#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3634 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003635 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003637#endif
3638
Gilles Peskineeccd8882020-03-10 12:19:08 +01003639#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003640#if defined(MBEDTLS_USE_PSA_CRYPTO)
3641 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3642 {
3643 /* The maintenance of the external PSK key slot is the
3644 * user's responsibility. */
3645 if( ssl->handshake->psk_opaque_is_internal )
3646 {
3647 psa_destroy_key( ssl->handshake->psk_opaque );
3648 ssl->handshake->psk_opaque_is_internal = 0;
3649 }
3650 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3651 }
Neil Armstronge952a302022-05-03 10:22:14 +02003652#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003653 if( handshake->psk != NULL )
3654 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003655 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003656 mbedtls_free( handshake->psk );
3657 }
Neil Armstronge952a302022-05-03 10:22:14 +02003658#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003659#endif
3660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3662 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003663 /*
3664 * Free only the linked list wrapper, not the keys themselves
3665 * since the belong to the SNI callback
3666 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003667 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003669
Gilles Peskineeccd8882020-03-10 12:19:08 +01003670#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003671 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003672 if( handshake->ecrs_peer_cert != NULL )
3673 {
3674 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3675 mbedtls_free( handshake->ecrs_peer_cert );
3676 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003677#endif
3678
Hanno Becker75173122019-02-06 16:18:31 +00003679#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3680 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3681 mbedtls_pk_free( &handshake->peer_pubkey );
3682#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3683
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003684#if defined(MBEDTLS_SSL_CLI_C) && \
3685 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3686 mbedtls_free( handshake->cookie );
3687#endif /* MBEDTLS_SSL_CLI_C &&
3688 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003689
3690#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003691 mbedtls_ssl_flight_free( handshake->flight );
3692 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003693#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003694
Ronald Cronf12b81d2022-03-15 10:42:41 +01003695#if defined(MBEDTLS_ECDH_C) && \
3696 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003697 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003698 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003699#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3700
Ronald Cron6f135e12021-12-08 16:57:54 +01003701#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003702 mbedtls_ssl_transform_free( handshake->transform_handshake );
3703 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003704 mbedtls_free( handshake->transform_earlydata );
3705 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003706#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003707
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003708
3709#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3710 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3711 * processes datagrams and the fact that a datagram is allowed to have
3712 * several records in it, it is possible that the I/O buffers are not
3713 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003714 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3715 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003716#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003717
Jerry Yuba9c7272021-10-30 11:54:10 +08003718 /* mbedtls_platform_zeroize MUST be last one in this function */
3719 mbedtls_platform_zeroize( handshake,
3720 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003721}
3722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003724{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003725 if( session == NULL )
3726 return;
3727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003729 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003730#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003731
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003732#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003733#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3734 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003735 mbedtls_free( session->hostname );
3736#endif
Xiaokang Qianed0620c2022-10-12 06:58:13 +00003737 mbedtls_free( session->ticket );
3738#endif
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003739
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003740 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003741}
3742
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003743#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003744
3745#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3746#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3747#else
3748#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3749#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3750
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003751#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003752
3753#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3754#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3755#else
3756#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3757#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3758
3759#if defined(MBEDTLS_SSL_ALPN)
3760#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3761#else
3762#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3763#endif /* MBEDTLS_SSL_ALPN */
3764
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3766#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3767#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3768#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3769
3770#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3771 ( (uint32_t) ( \
3772 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3773 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3774 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3775 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3776 0u ) )
3777
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003778static unsigned char ssl_serialized_context_header[] = {
3779 MBEDTLS_VERSION_MAJOR,
3780 MBEDTLS_VERSION_MINOR,
3781 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003782 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3783 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3784 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3785 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3786 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003787};
3788
Paul Bakker5121ce52009-01-03 21:22:43 +00003789/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003790 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003791 *
3792 * The format of the serialized data is:
3793 * (in the presentation language of TLS, RFC 8446 section 3)
3794 *
3795 * // header
3796 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003797 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003798 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003799 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003800 * Note: When updating the format, remember to keep these
3801 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003802 *
3803 * // session sub-structure
3804 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3805 * // transform sub-structure
3806 * uint8 random[64]; // ServerHello.random+ClientHello.random
3807 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3808 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3809 * // fields from ssl_context
3810 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3811 * uint64 in_window_top; // DTLS: last validated record seq_num
3812 * uint64 in_window; // DTLS: bitmask for replay protection
3813 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3814 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3815 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3816 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3817 *
3818 * Note that many fields of the ssl_context or sub-structures are not
3819 * serialized, as they fall in one of the following categories:
3820 *
3821 * 1. forced value (eg in_left must be 0)
3822 * 2. pointer to dynamically-allocated memory (eg session, transform)
3823 * 3. value can be re-derived from other data (eg session keys from MS)
3824 * 4. value was temporary (eg content of input buffer)
3825 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003826 */
3827int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3828 unsigned char *buf,
3829 size_t buf_len,
3830 size_t *olen )
3831{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003832 unsigned char *p = buf;
3833 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003834 size_t session_len;
3835 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003836
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003837 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003838 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3839 * this function's documentation.
3840 *
3841 * These are due to assumptions/limitations in the implementation. Some of
3842 * them are likely to stay (no handshake in progress) some might go away
3843 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003844 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003845 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003846 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003847 {
3848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003850 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003851 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003852 {
3853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003854 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003855 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003856 /* Double-check that sub-structures are indeed ready */
3857 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003858 {
3859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003861 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003862 /* There must be no pending incoming or outgoing data */
3863 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003864 {
3865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003867 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003868 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003869 {
3870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003871 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003872 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003873 /* Protocol must be DLTS, not TLS */
3874 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003875 {
3876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003878 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003879 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003880 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003881 {
3882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003884 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003885 /* We must be using an AEAD ciphersuite */
3886 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003887 {
3888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003890 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003891 /* Renegotiation must not be enabled */
3892#if defined(MBEDTLS_SSL_RENEGOTIATION)
3893 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003894 {
3895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
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#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003899
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003900 /*
3901 * Version and format identifier
3902 */
3903 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003904
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003905 if( used <= buf_len )
3906 {
3907 memcpy( p, ssl_serialized_context_header,
3908 sizeof( ssl_serialized_context_header ) );
3909 p += sizeof( ssl_serialized_context_header );
3910 }
3911
3912 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003913 * Session (length + data)
3914 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003915 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003916 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3917 return( ret );
3918
3919 used += 4 + session_len;
3920 if( used <= buf_len )
3921 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003922 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3923 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003924
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003925 ret = ssl_session_save( ssl->session, 1,
3926 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003927 if( ret != 0 )
3928 return( ret );
3929
3930 p += session_len;
3931 }
3932
3933 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003934 * Transform
3935 */
3936 used += sizeof( ssl->transform->randbytes );
3937 if( used <= buf_len )
3938 {
3939 memcpy( p, ssl->transform->randbytes,
3940 sizeof( ssl->transform->randbytes ) );
3941 p += sizeof( ssl->transform->randbytes );
3942 }
3943
3944#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3945 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3946 if( used <= buf_len )
3947 {
3948 *p++ = ssl->transform->in_cid_len;
3949 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3950 p += ssl->transform->in_cid_len;
3951
3952 *p++ = ssl->transform->out_cid_len;
3953 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3954 p += ssl->transform->out_cid_len;
3955 }
3956#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3957
3958 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003959 * Saved fields from top-level ssl_context structure
3960 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003961 used += 4;
3962 if( used <= buf_len )
3963 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003964 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3965 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003966 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003967
3968#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3969 used += 16;
3970 if( used <= buf_len )
3971 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003972 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3973 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003974
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003975 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3976 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003977 }
3978#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3979
3980#if defined(MBEDTLS_SSL_PROTO_DTLS)
3981 used += 1;
3982 if( used <= buf_len )
3983 {
3984 *p++ = ssl->disable_datagram_packing;
3985 }
3986#endif /* MBEDTLS_SSL_PROTO_DTLS */
3987
Jerry Yuae0b2e22021-10-08 15:21:19 +08003988 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003989 if( used <= buf_len )
3990 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003991 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3992 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003993 }
3994
3995#if defined(MBEDTLS_SSL_PROTO_DTLS)
3996 used += 2;
3997 if( used <= buf_len )
3998 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003999 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4000 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004001 }
4002#endif /* MBEDTLS_SSL_PROTO_DTLS */
4003
4004#if defined(MBEDTLS_SSL_ALPN)
4005 {
4006 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004007 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004008 : 0;
4009
4010 used += 1 + alpn_len;
4011 if( used <= buf_len )
4012 {
4013 *p++ = alpn_len;
4014
4015 if( ssl->alpn_chosen != NULL )
4016 {
4017 memcpy( p, ssl->alpn_chosen, alpn_len );
4018 p += alpn_len;
4019 }
4020 }
4021 }
4022#endif /* MBEDTLS_SSL_ALPN */
4023
4024 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004025 * Done
4026 */
4027 *olen = used;
4028
4029 if( used > buf_len )
4030 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004031
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004032 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4033
Hanno Becker43aefe22020-02-05 10:44:56 +00004034 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004035}
4036
4037/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004038 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004039 *
4040 * This internal version is wrapped by a public function that cleans up in
4041 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004042 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004043MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004044static int ssl_context_load( mbedtls_ssl_context *ssl,
4045 const unsigned char *buf,
4046 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004047{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004048 const unsigned char *p = buf;
4049 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004050 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004052
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004053 /*
4054 * The context should have been freshly setup or reset.
4055 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004056 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004057 * renegotiating, or if the user mistakenly loaded a session first.)
4058 */
4059 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4060 ssl->session != NULL )
4061 {
4062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4063 }
4064
4065 /*
4066 * We can't check that the config matches the initial one, but we can at
4067 * least check it matches the requirements for serializing.
4068 */
4069 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004070 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4071 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004072#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004073 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004074#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004075 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004076 {
4077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4078 }
4079
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004080 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4081
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004082 /*
4083 * Check version identifier
4084 */
4085 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4087
4088 if( memcmp( p, ssl_serialized_context_header,
4089 sizeof( ssl_serialized_context_header ) ) != 0 )
4090 {
4091 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4092 }
4093 p += sizeof( ssl_serialized_context_header );
4094
4095 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004096 * Session
4097 */
4098 if( (size_t)( end - p ) < 4 )
4099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4100
4101 session_len = ( (size_t) p[0] << 24 ) |
4102 ( (size_t) p[1] << 16 ) |
4103 ( (size_t) p[2] << 8 ) |
4104 ( (size_t) p[3] );
4105 p += 4;
4106
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004107 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004108 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004109 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004110 ssl->session_in = ssl->session;
4111 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004112 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004113
4114 if( (size_t)( end - p ) < session_len )
4115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4116
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004117 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004118 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004119 {
4120 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004121 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004122 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004123
4124 p += session_len;
4125
4126 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004127 * Transform
4128 */
4129
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004130 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004131 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004132 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004133 ssl->transform_in = ssl->transform;
4134 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004135 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004136
4137 /* Read random bytes and populate structure */
4138 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004140#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004141 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004142 ssl->session->ciphersuite,
4143 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004144#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004145 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004146#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004147 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4148 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004149 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004150 ssl->conf->endpoint,
4151 ssl );
4152 if( ret != 0 )
4153 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004154#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004155 p += sizeof( ssl->transform->randbytes );
4156
4157#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4158 /* Read connection IDs and store them */
4159 if( (size_t)( end - p ) < 1 )
4160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4161
4162 ssl->transform->in_cid_len = *p++;
4163
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004164 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4166
4167 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4168 p += ssl->transform->in_cid_len;
4169
4170 ssl->transform->out_cid_len = *p++;
4171
4172 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4174
4175 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4176 p += ssl->transform->out_cid_len;
4177#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4178
4179 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004180 * Saved fields from top-level ssl_context structure
4181 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004182 if( (size_t)( end - p ) < 4 )
4183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4184
4185 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4186 ( (uint32_t) p[1] << 16 ) |
4187 ( (uint32_t) p[2] << 8 ) |
4188 ( (uint32_t) p[3] );
4189 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004190
4191#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4192 if( (size_t)( end - p ) < 16 )
4193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4194
4195 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4196 ( (uint64_t) p[1] << 48 ) |
4197 ( (uint64_t) p[2] << 40 ) |
4198 ( (uint64_t) p[3] << 32 ) |
4199 ( (uint64_t) p[4] << 24 ) |
4200 ( (uint64_t) p[5] << 16 ) |
4201 ( (uint64_t) p[6] << 8 ) |
4202 ( (uint64_t) p[7] );
4203 p += 8;
4204
4205 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4206 ( (uint64_t) p[1] << 48 ) |
4207 ( (uint64_t) p[2] << 40 ) |
4208 ( (uint64_t) p[3] << 32 ) |
4209 ( (uint64_t) p[4] << 24 ) |
4210 ( (uint64_t) p[5] << 16 ) |
4211 ( (uint64_t) p[6] << 8 ) |
4212 ( (uint64_t) p[7] );
4213 p += 8;
4214#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4215
4216#if defined(MBEDTLS_SSL_PROTO_DTLS)
4217 if( (size_t)( end - p ) < 1 )
4218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4219
4220 ssl->disable_datagram_packing = *p++;
4221#endif /* MBEDTLS_SSL_PROTO_DTLS */
4222
Jerry Yud9a94fe2021-09-28 18:58:59 +08004223 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004225 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4226 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004227
4228#if defined(MBEDTLS_SSL_PROTO_DTLS)
4229 if( (size_t)( end - p ) < 2 )
4230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4231
4232 ssl->mtu = ( p[0] << 8 ) | p[1];
4233 p += 2;
4234#endif /* MBEDTLS_SSL_PROTO_DTLS */
4235
4236#if defined(MBEDTLS_SSL_ALPN)
4237 {
4238 uint8_t alpn_len;
4239 const char **cur;
4240
4241 if( (size_t)( end - p ) < 1 )
4242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4243
4244 alpn_len = *p++;
4245
4246 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4247 {
4248 /* alpn_chosen should point to an item in the configured list */
4249 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4250 {
4251 if( strlen( *cur ) == alpn_len &&
4252 memcmp( p, cur, alpn_len ) == 0 )
4253 {
4254 ssl->alpn_chosen = *cur;
4255 break;
4256 }
4257 }
4258 }
4259
4260 /* can only happen on conf mismatch */
4261 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4263
4264 p += alpn_len;
4265 }
4266#endif /* MBEDTLS_SSL_ALPN */
4267
4268 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004269 * Forced fields from top-level ssl_context structure
4270 *
4271 * Most of them already set to the correct value by mbedtls_ssl_init() and
4272 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4273 */
4274 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004275 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004276
Hanno Becker361b10d2019-08-30 10:42:49 +01004277 /* Adjust pointers for header fields of outgoing records to
4278 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004279 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004280
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004281#if defined(MBEDTLS_SSL_PROTO_DTLS)
4282 ssl->in_epoch = 1;
4283#endif
4284
4285 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4286 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004287 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4288 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004289 if( ssl->handshake != NULL )
4290 {
4291 mbedtls_ssl_handshake_free( ssl );
4292 mbedtls_free( ssl->handshake );
4293 ssl->handshake = NULL;
4294 }
4295
4296 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004297 * Done - should have consumed entire buffer
4298 */
4299 if( p != end )
4300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004301
4302 return( 0 );
4303}
4304
4305/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004306 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004307 */
4308int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4309 const unsigned char *buf,
4310 size_t len )
4311{
4312 int ret = ssl_context_load( context, buf, len );
4313
4314 if( ret != 0 )
4315 mbedtls_ssl_free( context );
4316
4317 return( ret );
4318}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004319#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004320
4321/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 * Free an SSL context
4323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004325{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004326 if( ssl == NULL )
4327 return;
4328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004330
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004331 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004332 {
sander-visserb8aa2072020-05-06 22:05:13 +02004333#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4334 size_t out_buf_len = ssl->out_buf_len;
4335#else
4336 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4337#endif
4338
Darryl Greenb33cc762019-11-28 14:29:44 +00004339 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004341 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 }
4343
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004344 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004345 {
sander-visserb8aa2072020-05-06 22:05:13 +02004346#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4347 size_t in_buf_len = ssl->in_buf_len;
4348#else
4349 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4350#endif
4351
Darryl Greenb33cc762019-11-28 14:29:44 +00004352 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004354 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 }
4356
Paul Bakker48916f92012-09-16 19:57:18 +00004357 if( ssl->transform )
4358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 mbedtls_ssl_transform_free( ssl->transform );
4360 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004361 }
4362
4363 if( ssl->handshake )
4364 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004365 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4367 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 mbedtls_free( ssl->handshake );
4370 mbedtls_free( ssl->transform_negotiate );
4371 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004372 }
4373
Ronald Cron6f135e12021-12-08 16:57:54 +01004374#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004375 mbedtls_ssl_transform_free( ssl->transform_application );
4376 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004377#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004378
Paul Bakkerc0463502013-02-14 11:19:38 +01004379 if( ssl->session )
4380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 mbedtls_ssl_session_free( ssl->session );
4382 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004383 }
4384
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004385#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004386 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004388 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004391#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004392
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004393#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004395#endif
4396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004398
Paul Bakker86f04f42013-02-14 11:20:09 +01004399 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004400 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004401}
4402
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004403/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004404 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004405 */
4406void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4407{
4408 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4409}
4410
Gilles Peskineae270bf2021-06-02 00:05:29 +02004411/* The selection should be the same as mbedtls_x509_crt_profile_default in
4412 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004413 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004414 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004415 * about this list.
4416 */
Brett Warrene0edc842021-08-17 09:53:13 +01004417static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004418#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004419 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004420#endif
4421#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004422 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004423#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004424#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004425 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004426#endif
4427#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004428 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004429#endif
4430#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004431 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004432#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004433#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004434 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004435#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004436#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004437 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004438#endif
4439#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004440 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004441#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004442 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004443};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004444
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004445static int ssl_preset_suiteb_ciphersuites[] = {
4446 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4447 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4448 0
4449};
4450
Gilles Peskineeccd8882020-03-10 12:19:08 +01004451#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004452
Jerry Yu909df7b2022-01-22 11:56:27 +08004453/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004454 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004455 * rules SHOULD be upheld.
4456 * - No duplicate entries.
4457 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004458 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004459 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004460 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004461static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004462
Andrzej Kurek25f27152022-08-17 16:09:31 -04004463#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 +08004464 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4465 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004466#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004467 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004468
Andrzej Kurek25f27152022-08-17 16:09:31 -04004469#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 +08004470 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4471 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004472#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004473 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4474
Andrzej Kurek25f27152022-08-17 16:09:31 -04004475#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 +08004476 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4477 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004478#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004479 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004480
Andrzej Kurek25f27152022-08-17 16:09:31 -04004481#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 +08004482 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004483#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 +08004484
Andrzej Kurek25f27152022-08-17 16:09:31 -04004485#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 +08004486 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004487#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 +08004488
Andrzej Kurek25f27152022-08-17 16:09:31 -04004489#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 +08004490 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004491#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 +08004492
Andrzej Kurek25f27152022-08-17 16:09:31 -04004493#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 +08004494 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4495#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4496
Andrzej Kurek25f27152022-08-17 16:09:31 -04004497#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 +08004498 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4499#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4500
Andrzej Kurek25f27152022-08-17 16:09:31 -04004501#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 +08004502 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4503#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4504
Gabor Mezei15b95a62022-05-09 16:37:58 +02004505 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004506};
4507
4508/* NOTICE: see above */
4509#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4510static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004511#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004512#if defined(MBEDTLS_ECDSA_C)
4513 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004514#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004515#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4516 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4517#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004518#if defined(MBEDTLS_RSA_C)
4519 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4520#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004521#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004522#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004523#if defined(MBEDTLS_ECDSA_C)
4524 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004525#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004526#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4527 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4528#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004529#if defined(MBEDTLS_RSA_C)
4530 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4531#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004532#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004533#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004534#if defined(MBEDTLS_ECDSA_C)
4535 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004536#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004537#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4538 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4539#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004540#if defined(MBEDTLS_RSA_C)
4541 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4542#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004543#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004544 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004545};
4546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4547/* NOTICE: see above */
4548static uint16_t ssl_preset_suiteb_sig_algs[] = {
4549
Andrzej Kurek25f27152022-08-17 16:09:31 -04004550#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 +08004551 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4552 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004553#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004554 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4555
Andrzej Kurek25f27152022-08-17 16:09:31 -04004556#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 +08004557 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4558 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004559#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004560 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004561
Andrzej Kurek25f27152022-08-17 16:09:31 -04004562#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 +08004563 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004564#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 +08004565
Andrzej Kurek25f27152022-08-17 16:09:31 -04004566#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 +08004567 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004568#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004569
Gabor Mezei15b95a62022-05-09 16:37:58 +02004570 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004571};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004572
Jerry Yu909df7b2022-01-22 11:56:27 +08004573/* NOTICE: see above */
4574#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4575static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004576#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004577#if defined(MBEDTLS_ECDSA_C)
4578 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004579#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004580#if defined(MBEDTLS_RSA_C)
4581 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4582#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004583#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004584#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004585#if defined(MBEDTLS_ECDSA_C)
4586 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004587#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004588#if defined(MBEDTLS_RSA_C)
4589 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4590#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004591#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004592 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004593};
Jerry Yu909df7b2022-01-22 11:56:27 +08004594#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4595
Jerry Yu1a8b4812022-01-20 17:56:50 +08004596#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004597
Brett Warrene0edc842021-08-17 09:53:13 +01004598static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004599#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004600 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004601#endif
4602#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004603 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004604#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004605 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004606};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004607
Jerry Yu1a8b4812022-01-20 17:56:50 +08004608#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004609/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004610 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004611MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004612static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004613{
4614 size_t i, j;
4615 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004616
Gabor Mezei15b95a62022-05-09 16:37:58 +02004617 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004618 {
Jerry Yuf377d642022-01-25 10:43:59 +08004619 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004620 {
Jerry Yuf377d642022-01-25 10:43:59 +08004621 if( sig_algs[i] != sig_algs[j] )
4622 continue;
4623 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4624 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4625 sig_algs[i], j, i );
4626 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004627 }
4628 }
4629 return( ret );
4630}
4631
4632#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4633
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004634/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004635 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004636 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004637int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004638 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004639{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004640#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004642#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004643
Jerry Yu1a8b4812022-01-20 17:56:50 +08004644#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004645 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004646 {
4647 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4648 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4649 }
4650
Jerry Yuf377d642022-01-25 10:43:59 +08004651 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004652 {
4653 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4654 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4655 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004656
4657#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004658 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004659 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004660 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004661 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4662 }
4663
Jerry Yuf377d642022-01-25 10:43:59 +08004664 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004665 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004666 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004667 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4668 }
4669#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004670#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4671
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004672 /* Use the functions here so that they are covered in tests,
4673 * but otherwise access member directly for efficiency */
4674 mbedtls_ssl_conf_endpoint( conf, endpoint );
4675 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004676
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004677 /*
4678 * Things that are common to all presets
4679 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004680#if defined(MBEDTLS_SSL_CLI_C)
4681 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4682 {
4683 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4684#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4685 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4686#endif
4687 }
4688#endif
4689
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004690#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4691 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4692#endif
4693
4694#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4695 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4696#endif
4697
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004698#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004699 conf->f_cookie_write = ssl_cookie_write_dummy;
4700 conf->f_cookie_check = ssl_cookie_check_dummy;
4701#endif
4702
4703#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4704 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4705#endif
4706
Janos Follath088ce432017-04-10 12:42:31 +01004707#if defined(MBEDTLS_SSL_SRV_C)
4708 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004709 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004710#endif
4711
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004712#if defined(MBEDTLS_SSL_PROTO_DTLS)
4713 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4714 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4715#endif
4716
4717#if defined(MBEDTLS_SSL_RENEGOTIATION)
4718 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004719 memset( conf->renego_period, 0x00, 2 );
4720 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004721#endif
4722
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004723#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004724 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4725 {
4726 const unsigned char dhm_p[] =
4727 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4728 const unsigned char dhm_g[] =
4729 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004730
Hanno Beckere2defad2021-07-24 05:59:17 +01004731 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4732 dhm_p, sizeof( dhm_p ),
4733 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4734 {
4735 return( ret );
4736 }
4737 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004738#endif
4739
Ronald Cron6f135e12021-12-08 16:57:54 +01004740#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004741#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004742 mbedtls_ssl_conf_new_session_tickets(
4743 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4744#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004745 /*
4746 * Allow all TLS 1.3 key exchange modes by default.
4747 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004748 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004749#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004750
XiaokangQian4d3a6042022-04-21 13:46:17 +00004751 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4752 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004753#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004754 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004755 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004756#else
XiaokangQian060d8672022-04-21 09:24:56 +00004757 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004758#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004759 }
4760 else
4761 {
4762#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4763 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4764 {
4765 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4766 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4767 }
4768 else
4769 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4770 {
4771 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4772 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4773 }
4774#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4775 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4776 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4777#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4778 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4779 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4780#else
4781 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4782#endif
4783 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004784
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004785 /*
4786 * Preset-specific defaults
4787 */
4788 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004789 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004790 /*
4791 * NSA Suite B
4792 */
4793 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004794
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004795 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004796
4797#if defined(MBEDTLS_X509_CRT_PARSE_C)
4798 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004799#endif
4800
Gilles Peskineeccd8882020-03-10 12:19:08 +01004801#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004802#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4803 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4804 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4805 else
4806#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4807 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004808#endif
4809
Brett Warrene0edc842021-08-17 09:53:13 +01004810#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4811 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004812#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004813 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004814 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004815
4816 /*
4817 * Default
4818 */
4819 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004820
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004821 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004822
4823#if defined(MBEDTLS_X509_CRT_PARSE_C)
4824 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4825#endif
4826
Gilles Peskineeccd8882020-03-10 12:19:08 +01004827#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004828#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4829 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4830 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4831 else
4832#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4833 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004834#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004835
Brett Warrene0edc842021-08-17 09:53:13 +01004836#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4837 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004838#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004839 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004840
4841#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4842 conf->dhm_min_bitlen = 1024;
4843#endif
4844 }
4845
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004846 return( 0 );
4847}
4848
4849/*
4850 * Free mbedtls_ssl_config
4851 */
4852void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4853{
4854#if defined(MBEDTLS_DHM_C)
4855 mbedtls_mpi_free( &conf->dhm_P );
4856 mbedtls_mpi_free( &conf->dhm_G );
4857#endif
4858
Gilles Peskineeccd8882020-03-10 12:19:08 +01004859#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004860#if defined(MBEDTLS_USE_PSA_CRYPTO)
4861 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4862 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004863 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4864 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004865#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004866 if( conf->psk != NULL )
4867 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004868 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004869 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004870 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004871 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004872 }
4873
4874 if( conf->psk_identity != NULL )
4875 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004876 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004877 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004878 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004879 conf->psk_identity_len = 0;
4880 }
4881#endif
4882
4883#if defined(MBEDTLS_X509_CRT_PARSE_C)
4884 ssl_key_cert_free( conf->key_cert );
4885#endif
4886
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004887 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004888}
4889
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004890#if defined(MBEDTLS_PK_C) && \
4891 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004892/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897#if defined(MBEDTLS_RSA_C)
4898 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4899 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901#if defined(MBEDTLS_ECDSA_C)
4902 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4903 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004906}
4907
Hanno Becker7e5437a2017-04-28 17:15:26 +01004908unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4909{
4910 switch( type ) {
4911 case MBEDTLS_PK_RSA:
4912 return( MBEDTLS_SSL_SIG_RSA );
4913 case MBEDTLS_PK_ECDSA:
4914 case MBEDTLS_PK_ECKEY:
4915 return( MBEDTLS_SSL_SIG_ECDSA );
4916 default:
4917 return( MBEDTLS_SSL_SIG_ANON );
4918 }
4919}
4920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004921mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004922{
4923 switch( sig )
4924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925#if defined(MBEDTLS_RSA_C)
4926 case MBEDTLS_SSL_SIG_RSA:
4927 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_ECDSA_C)
4930 case MBEDTLS_SSL_SIG_ECDSA:
4931 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004932#endif
4933 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004935 }
4936}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004937#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004938
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004939/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004940 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004943{
4944 switch( hash )
4945 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004946#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 case MBEDTLS_SSL_HASH_MD5:
4948 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004949#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004950#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951 case MBEDTLS_SSL_HASH_SHA1:
4952 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004953#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004954#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 case MBEDTLS_SSL_HASH_SHA224:
4956 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004957#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 case MBEDTLS_SSL_HASH_SHA256:
4960 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004961#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004962#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 case MBEDTLS_SSL_HASH_SHA384:
4964 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004965#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004966#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967 case MBEDTLS_SSL_HASH_SHA512:
4968 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004969#endif
4970 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004972 }
4973}
4974
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004975/*
4976 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4977 */
4978unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4979{
4980 switch( md )
4981 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004982#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004983 case MBEDTLS_MD_MD5:
4984 return( MBEDTLS_SSL_HASH_MD5 );
4985#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004986#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004987 case MBEDTLS_MD_SHA1:
4988 return( MBEDTLS_SSL_HASH_SHA1 );
4989#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004990#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004991 case MBEDTLS_MD_SHA224:
4992 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004993#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004994#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004995 case MBEDTLS_MD_SHA256:
4996 return( MBEDTLS_SSL_HASH_SHA256 );
4997#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004998#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004999 case MBEDTLS_MD_SHA384:
5000 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005001#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005002#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005003 case MBEDTLS_MD_SHA512:
5004 return( MBEDTLS_SSL_HASH_SHA512 );
5005#endif
5006 default:
5007 return( MBEDTLS_SSL_HASH_NONE );
5008 }
5009}
5010
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005011/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005012 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005013 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005014 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005015int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005016{
Brett Warrene0edc842021-08-17 09:53:13 +01005017 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005018
Brett Warrene0edc842021-08-17 09:53:13 +01005019 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005020 return( -1 );
5021
Brett Warrene0edc842021-08-17 09:53:13 +01005022 for( ; *group_list != 0; group_list++ )
5023 {
5024 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005025 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005026 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005027
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005028 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005029}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005030
5031#if defined(MBEDTLS_ECP_C)
5032/*
5033 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5034 */
5035int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5036{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005037 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005038 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005039
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005040 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005041 return -1;
5042
5043 uint16_t tls_id = grp_info->tls_id;
5044
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005045 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5046}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005047#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005049#if defined(MBEDTLS_X509_CRT_PARSE_C)
5050int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5051 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005052 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005053 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005054{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005055 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005056 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005057 const char *ext_oid;
5058 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005061 {
5062 /* Server part of the key exchange */
5063 switch( ciphersuite->key_exchange )
5064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 case MBEDTLS_KEY_EXCHANGE_RSA:
5066 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005067 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005068 break;
5069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5071 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5072 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5073 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005074 break;
5075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5077 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005078 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005079 break;
5080
5081 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 case MBEDTLS_KEY_EXCHANGE_NONE:
5083 case MBEDTLS_KEY_EXCHANGE_PSK:
5084 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5085 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005086 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005087 usage = 0;
5088 }
5089 }
5090 else
5091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5093 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005094 }
5095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005097 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005098 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005099 ret = -1;
5100 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5105 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005106 }
5107 else
5108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5110 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005111 }
5112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005114 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005115 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005116 ret = -1;
5117 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005118
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005119 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005122
Jerry Yu148165c2021-09-24 23:20:59 +08005123#if defined(MBEDTLS_USE_PSA_CRYPTO)
5124int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5125 const mbedtls_md_type_t md,
5126 unsigned char *dst,
5127 size_t dst_len,
5128 size_t *olen )
5129{
Ronald Cronf6893e12022-01-07 22:09:01 +01005130 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5131 psa_hash_operation_t *hash_operation_to_clone;
5132 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5133
Jerry Yu148165c2021-09-24 23:20:59 +08005134 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005135
5136 switch( md )
5137 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005138#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005139 case MBEDTLS_MD_SHA384:
5140 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5141 break;
5142#endif
5143
Andrzej Kurek25f27152022-08-17 16:09:31 -04005144#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005145 case MBEDTLS_MD_SHA256:
5146 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5147 break;
5148#endif
5149
5150 default:
5151 goto exit;
5152 }
5153
5154 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5155 if( status != PSA_SUCCESS )
5156 goto exit;
5157
5158 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5159 if( status != PSA_SUCCESS )
5160 goto exit;
5161
5162exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005163 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005164}
5165#else /* MBEDTLS_USE_PSA_CRYPTO */
5166
Andrzej Kurek25f27152022-08-17 16:09:31 -04005167#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005168MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005169static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5170 unsigned char *dst,
5171 size_t dst_len,
5172 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005173{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005174 int ret;
5175 mbedtls_sha512_context sha512;
5176
5177 if( dst_len < 48 )
5178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5179
5180 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005181 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005182
5183 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5184 {
5185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5186 goto exit;
5187 }
5188
5189 *olen = 48;
5190
5191exit:
5192
5193 mbedtls_sha512_free( &sha512 );
5194 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005195}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005196#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005197
Andrzej Kurek25f27152022-08-17 16:09:31 -04005198#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005199MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005200static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5201 unsigned char *dst,
5202 size_t dst_len,
5203 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005204{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005205 int ret;
5206 mbedtls_sha256_context sha256;
5207
5208 if( dst_len < 32 )
5209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5210
5211 mbedtls_sha256_init( &sha256 );
5212 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005213
Jerry Yu24c0ec32021-09-09 14:21:07 +08005214 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5215 {
5216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5217 goto exit;
5218 }
5219
5220 *olen = 32;
5221
5222exit:
5223
5224 mbedtls_sha256_free( &sha256 );
5225 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005226}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005227#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005228
Jerry Yu000f9762021-09-14 11:12:51 +08005229int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5230 const mbedtls_md_type_t md,
5231 unsigned char *dst,
5232 size_t dst_len,
5233 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005234{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005235 switch( md )
5236 {
5237
Andrzej Kurek25f27152022-08-17 16:09:31 -04005238#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005239 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005240 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005241#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005242
Andrzej Kurek25f27152022-08-17 16:09:31 -04005243#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005244 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005245 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005246#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005247
5248 default:
5249 break;
5250 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005252}
XiaokangQian647719a2021-12-07 09:16:29 +00005253
Jerry Yu148165c2021-09-24 23:20:59 +08005254#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005255
Gabor Mezei078e8032022-04-27 21:17:56 +02005256#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5257/* mbedtls_ssl_parse_sig_alg_ext()
5258 *
5259 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5260 * value (TLS 1.3 RFC8446):
5261 * enum {
5262 * ....
5263 * ecdsa_secp256r1_sha256( 0x0403 ),
5264 * ecdsa_secp384r1_sha384( 0x0503 ),
5265 * ecdsa_secp521r1_sha512( 0x0603 ),
5266 * ....
5267 * } SignatureScheme;
5268 *
5269 * struct {
5270 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5271 * } SignatureSchemeList;
5272 *
5273 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5274 * value (TLS 1.2 RFC5246):
5275 * enum {
5276 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5277 * sha512(6), (255)
5278 * } HashAlgorithm;
5279 *
5280 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5281 * SignatureAlgorithm;
5282 *
5283 * struct {
5284 * HashAlgorithm hash;
5285 * SignatureAlgorithm signature;
5286 * } SignatureAndHashAlgorithm;
5287 *
5288 * SignatureAndHashAlgorithm
5289 * supported_signature_algorithms<2..2^16-2>;
5290 *
5291 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5292 * generalization of the TLS 1.2 signature algorithm extension.
5293 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5294 * `SignatureScheme` field of TLS 1.3
5295 *
5296 */
5297int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5298 const unsigned char *buf,
5299 const unsigned char *end )
5300{
5301 const unsigned char *p = buf;
5302 size_t supported_sig_algs_len = 0;
5303 const unsigned char *supported_sig_algs_end;
5304 uint16_t sig_alg;
5305 uint32_t common_idx = 0;
5306
5307 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5308 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5309 p += 2;
5310
5311 memset( ssl->handshake->received_sig_algs, 0,
5312 sizeof(ssl->handshake->received_sig_algs) );
5313
5314 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5315 supported_sig_algs_end = p + supported_sig_algs_len;
5316 while( p < supported_sig_algs_end )
5317 {
5318 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5319 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5320 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005321 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5322 sig_alg,
5323 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005324#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005325 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005326 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5327 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5328 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005329 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005330 }
5331#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005332
Jerry Yuf3b46b52022-06-19 16:52:27 +08005333 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5334 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005335
5336 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5337 {
5338 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5339 common_idx += 1;
5340 }
5341 }
5342 /* Check that we consumed all the message. */
5343 if( p != end )
5344 {
5345 MBEDTLS_SSL_DEBUG_MSG( 1,
5346 ( "Signature algorithms extension length misaligned" ) );
5347 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5348 MBEDTLS_ERR_SSL_DECODE_ERROR );
5349 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5350 }
5351
5352 if( common_idx == 0 )
5353 {
5354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5355 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5356 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5357 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5358 }
5359
Gabor Mezei15b95a62022-05-09 16:37:58 +02005360 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005361 return( 0 );
5362}
5363
5364#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5365
Jerry Yudc7bd172022-02-17 13:44:15 +08005366#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5367
5368#if defined(MBEDTLS_USE_PSA_CRYPTO)
5369
5370static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5371 mbedtls_svc_key_id_t key,
5372 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005373 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005374 const unsigned char* seed, size_t seed_length,
5375 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005376 const unsigned char* other_secret,
5377 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005378 size_t capacity )
5379{
5380 psa_status_t status;
5381
5382 status = psa_key_derivation_setup( derivation, alg );
5383 if( status != PSA_SUCCESS )
5384 return( status );
5385
5386 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5387 {
5388 status = psa_key_derivation_input_bytes( derivation,
5389 PSA_KEY_DERIVATION_INPUT_SEED,
5390 seed, seed_length );
5391 if( status != PSA_SUCCESS )
5392 return( status );
5393
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005394 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005395 {
5396 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005397 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5398 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005399 if( status != PSA_SUCCESS )
5400 return( status );
5401 }
5402
Jerry Yudc7bd172022-02-17 13:44:15 +08005403 if( mbedtls_svc_key_id_is_null( key ) )
5404 {
5405 status = psa_key_derivation_input_bytes(
5406 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005407 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005408 }
5409 else
5410 {
5411 status = psa_key_derivation_input_key(
5412 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5413 }
5414 if( status != PSA_SUCCESS )
5415 return( status );
5416
5417 status = psa_key_derivation_input_bytes( derivation,
5418 PSA_KEY_DERIVATION_INPUT_LABEL,
5419 label, label_length );
5420 if( status != PSA_SUCCESS )
5421 return( status );
5422 }
5423 else
5424 {
5425 return( PSA_ERROR_NOT_SUPPORTED );
5426 }
5427
5428 status = psa_key_derivation_set_capacity( derivation, capacity );
5429 if( status != PSA_SUCCESS )
5430 return( status );
5431
5432 return( PSA_SUCCESS );
5433}
5434
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005435MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005436static int tls_prf_generic( mbedtls_md_type_t md_type,
5437 const unsigned char *secret, size_t slen,
5438 const char *label,
5439 const unsigned char *random, size_t rlen,
5440 unsigned char *dstbuf, size_t dlen )
5441{
5442 psa_status_t status;
5443 psa_algorithm_t alg;
5444 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5445 psa_key_derivation_operation_t derivation =
5446 PSA_KEY_DERIVATION_OPERATION_INIT;
5447
5448 if( md_type == MBEDTLS_MD_SHA384 )
5449 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5450 else
5451 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5452
5453 /* Normally a "secret" should be long enough to be impossible to
5454 * find by brute force, and in particular should not be empty. But
5455 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5456 * and for this use case it makes sense to have a 0-length "secret".
5457 * Since the key API doesn't allow importing a key of length 0,
5458 * keep master_key=0, which setup_psa_key_derivation() understands
5459 * to mean a 0-length "secret" input. */
5460 if( slen != 0 )
5461 {
5462 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5463 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5464 psa_set_key_algorithm( &key_attributes, alg );
5465 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5466
5467 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5468 if( status != PSA_SUCCESS )
5469 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5470 }
5471
5472 status = setup_psa_key_derivation( &derivation,
5473 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005474 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005475 random, rlen,
5476 (unsigned char const *) label,
5477 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005478 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005479 dlen );
5480 if( status != PSA_SUCCESS )
5481 {
5482 psa_key_derivation_abort( &derivation );
5483 psa_destroy_key( master_key );
5484 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5485 }
5486
5487 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5488 if( status != PSA_SUCCESS )
5489 {
5490 psa_key_derivation_abort( &derivation );
5491 psa_destroy_key( master_key );
5492 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5493 }
5494
5495 status = psa_key_derivation_abort( &derivation );
5496 if( status != PSA_SUCCESS )
5497 {
5498 psa_destroy_key( master_key );
5499 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5500 }
5501
5502 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5503 status = psa_destroy_key( master_key );
5504 if( status != PSA_SUCCESS )
5505 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5506
5507 return( 0 );
5508}
5509
5510#else /* MBEDTLS_USE_PSA_CRYPTO */
5511
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005512MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005513static int tls_prf_generic( mbedtls_md_type_t md_type,
5514 const unsigned char *secret, size_t slen,
5515 const char *label,
5516 const unsigned char *random, size_t rlen,
5517 unsigned char *dstbuf, size_t dlen )
5518{
5519 size_t nb;
5520 size_t i, j, k, md_len;
5521 unsigned char *tmp;
5522 size_t tmp_len = 0;
5523 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5524 const mbedtls_md_info_t *md_info;
5525 mbedtls_md_context_t md_ctx;
5526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5527
5528 mbedtls_md_init( &md_ctx );
5529
5530 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5532
5533 md_len = mbedtls_md_get_size( md_info );
5534
5535 tmp_len = md_len + strlen( label ) + rlen;
5536 tmp = mbedtls_calloc( 1, tmp_len );
5537 if( tmp == NULL )
5538 {
5539 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5540 goto exit;
5541 }
5542
5543 nb = strlen( label );
5544 memcpy( tmp + md_len, label, nb );
5545 memcpy( tmp + md_len + nb, random, rlen );
5546 nb += rlen;
5547
5548 /*
5549 * Compute P_<hash>(secret, label + random)[0..dlen]
5550 */
5551 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5552 goto exit;
5553
5554 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5555 if( ret != 0 )
5556 goto exit;
5557 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5558 if( ret != 0 )
5559 goto exit;
5560 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5561 if( ret != 0 )
5562 goto exit;
5563
5564 for( i = 0; i < dlen; i += md_len )
5565 {
5566 ret = mbedtls_md_hmac_reset ( &md_ctx );
5567 if( ret != 0 )
5568 goto exit;
5569 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5570 if( ret != 0 )
5571 goto exit;
5572 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5573 if( ret != 0 )
5574 goto exit;
5575
5576 ret = mbedtls_md_hmac_reset ( &md_ctx );
5577 if( ret != 0 )
5578 goto exit;
5579 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5580 if( ret != 0 )
5581 goto exit;
5582 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5583 if( ret != 0 )
5584 goto exit;
5585
5586 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5587
5588 for( j = 0; j < k; j++ )
5589 dstbuf[i + j] = h_i[j];
5590 }
5591
5592exit:
5593 mbedtls_md_free( &md_ctx );
5594
5595 mbedtls_platform_zeroize( tmp, tmp_len );
5596 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5597
5598 mbedtls_free( tmp );
5599
5600 return( ret );
5601}
5602#endif /* MBEDTLS_USE_PSA_CRYPTO */
5603
Andrzej Kurek25f27152022-08-17 16:09:31 -04005604#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005605MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005606static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5607 const char *label,
5608 const unsigned char *random, size_t rlen,
5609 unsigned char *dstbuf, size_t dlen )
5610{
5611 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5612 label, random, rlen, dstbuf, dlen ) );
5613}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005614#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005615
Andrzej Kurek25f27152022-08-17 16:09:31 -04005616#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005617MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005618static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5619 const char *label,
5620 const unsigned char *random, size_t rlen,
5621 unsigned char *dstbuf, size_t dlen )
5622{
5623 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5624 label, random, rlen, dstbuf, dlen ) );
5625}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005626#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005627
Jerry Yuf009d862022-02-17 14:01:37 +08005628/*
5629 * Set appropriate PRF function and other SSL / TLS1.2 functions
5630 *
5631 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005632 * - hash associated with the ciphersuite (only used by TLS 1.2)
5633 *
5634 * Outputs:
5635 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5636 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005637MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005638static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005639 mbedtls_md_type_t hash )
5640{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005641#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005642 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005643 {
5644 handshake->tls_prf = tls_prf_sha384;
5645 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5646 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5647 }
5648 else
5649#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005650#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005651 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005652 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005653 handshake->tls_prf = tls_prf_sha256;
5654 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5655 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5656 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005657#else
Jerry Yuf009d862022-02-17 14:01:37 +08005658 {
Jerry Yuf009d862022-02-17 14:01:37 +08005659 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005660 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5662 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005663#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005664
5665 return( 0 );
5666}
Jerry Yud6ab2352022-02-17 14:03:43 +08005667
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005668/*
5669 * Compute master secret if needed
5670 *
5671 * Parameters:
5672 * [in/out] handshake
5673 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5674 * (PSA-PSK) ciphersuite_info, psk_opaque
5675 * [out] premaster (cleared)
5676 * [out] master
5677 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5678 * debug: conf->f_dbg, conf->p_dbg
5679 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005680 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005681 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005682MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005683static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5684 unsigned char *master,
5685 const mbedtls_ssl_context *ssl )
5686{
5687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5688
5689 /* cf. RFC 5246, Section 8.1:
5690 * "The master secret is always exactly 48 bytes in length." */
5691 size_t const master_secret_len = 48;
5692
5693#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5694 unsigned char session_hash[48];
5695#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5696
5697 /* The label for the KDF used for key expansion.
5698 * This is either "master secret" or "extended master secret"
5699 * depending on whether the Extended Master Secret extension
5700 * is used. */
5701 char const *lbl = "master secret";
5702
Przemek Stekielae4ed302022-04-05 17:15:55 +02005703 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005704 * - If the Extended Master Secret extension is not used,
5705 * this is ClientHello.Random + ServerHello.Random
5706 * (see Sect. 8.1 in RFC 5246).
5707 * - If the Extended Master Secret extension is used,
5708 * this is the transcript of the handshake so far.
5709 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005710 unsigned char const *seed = handshake->randbytes;
5711 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005712
5713#if !defined(MBEDTLS_DEBUG_C) && \
5714 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5715 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5716 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5717 ssl = NULL; /* make sure we don't use it except for those cases */
5718 (void) ssl;
5719#endif
5720
5721 if( handshake->resume != 0 )
5722 {
5723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5724 return( 0 );
5725 }
5726
5727#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5728 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5729 {
5730 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005731 seed = session_hash;
5732 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005733
5734 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005735 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005736 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005737#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005738
Przemek Stekiel99114f32022-04-22 11:20:09 +02005739#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005740 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005741 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005742 {
5743 /* Perform PSK-to-MS expansion in a single step. */
5744 psa_status_t status;
5745 psa_algorithm_t alg;
5746 mbedtls_svc_key_id_t psk;
5747 psa_key_derivation_operation_t derivation =
5748 PSA_KEY_DERIVATION_OPERATION_INIT;
5749 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5750
5751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5752
5753 psk = mbedtls_ssl_get_opaque_psk( ssl );
5754
5755 if( hash_alg == MBEDTLS_MD_SHA384 )
5756 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5757 else
5758 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5759
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005760 size_t other_secret_len = 0;
5761 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005762
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005763 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005764 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005765 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005766 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005767 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005768 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005769 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005770 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005771 other_secret_len = 48;
5772 other_secret = handshake->premaster + 2;
5773 break;
5774 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005775 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005776 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5777 other_secret = handshake->premaster + 2;
5778 break;
5779 default:
5780 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005781 }
5782
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005783 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005784 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005785 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005786 (unsigned char const *) lbl,
5787 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005788 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005789 master_secret_len );
5790 if( status != PSA_SUCCESS )
5791 {
5792 psa_key_derivation_abort( &derivation );
5793 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5794 }
5795
5796 status = psa_key_derivation_output_bytes( &derivation,
5797 master,
5798 master_secret_len );
5799 if( status != PSA_SUCCESS )
5800 {
5801 psa_key_derivation_abort( &derivation );
5802 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5803 }
5804
5805 status = psa_key_derivation_abort( &derivation );
5806 if( status != PSA_SUCCESS )
5807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5808 }
5809 else
5810#endif
5811 {
5812 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005813 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005814 master,
5815 master_secret_len );
5816 if( ret != 0 )
5817 {
5818 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5819 return( ret );
5820 }
5821
5822 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5823 handshake->premaster,
5824 handshake->pmslen );
5825
5826 mbedtls_platform_zeroize( handshake->premaster,
5827 sizeof(handshake->premaster) );
5828 }
5829
5830 return( 0 );
5831}
5832
Jerry Yud62f87e2022-02-17 14:09:02 +08005833int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5834{
5835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5836 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5837 ssl->handshake->ciphersuite_info;
5838
5839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5840
5841 /* Set PRF, calc_verify and calc_finished function pointers */
5842 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005843 ciphersuite_info->mac );
5844 if( ret != 0 )
5845 {
5846 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5847 return( ret );
5848 }
5849
5850 /* Compute master secret if needed */
5851 ret = ssl_compute_master( ssl->handshake,
5852 ssl->session_negotiate->master,
5853 ssl );
5854 if( ret != 0 )
5855 {
5856 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5857 return( ret );
5858 }
5859
5860 /* Swap the client and server random values:
5861 * - MS derivation wanted client+server (RFC 5246 8.1)
5862 * - key derivation wants server+client (RFC 5246 6.3) */
5863 {
5864 unsigned char tmp[64];
5865 memcpy( tmp, ssl->handshake->randbytes, 64 );
5866 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5867 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5868 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5869 }
5870
5871 /* Populate transform structure */
5872 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5873 ssl->session_negotiate->ciphersuite,
5874 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005875#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005876 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005877#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005878 ssl->handshake->tls_prf,
5879 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005880 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005881 ssl->conf->endpoint,
5882 ssl );
5883 if( ret != 0 )
5884 {
5885 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5886 return( ret );
5887 }
5888
5889 /* We no longer need Server/ClientHello.random values */
5890 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5891 sizeof( ssl->handshake->randbytes ) );
5892
5893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5894
5895 return( 0 );
5896}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005897
Ronald Cron4dcbca92022-03-07 10:21:40 +01005898int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5899{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005900 switch( md )
5901 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005902#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005903 case MBEDTLS_SSL_HASH_SHA384:
5904 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5905 break;
5906#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005907#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005908 case MBEDTLS_SSL_HASH_SHA256:
5909 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5910 break;
5911#endif
5912 default:
5913 return( -1 );
5914 }
5915
Ronald Cronc2f13a02022-03-07 10:25:24 +01005916 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005917}
5918
Andrzej Kurek25f27152022-08-17 16:09:31 -04005919#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005920void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5921 unsigned char *hash,
5922 size_t *hlen )
5923{
5924#if defined(MBEDTLS_USE_PSA_CRYPTO)
5925 size_t hash_size;
5926 psa_status_t status;
5927 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5928
5929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5930 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5931 if( status != PSA_SUCCESS )
5932 {
5933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5934 return;
5935 }
5936
5937 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5938 if( status != PSA_SUCCESS )
5939 {
5940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5941 return;
5942 }
5943
5944 *hlen = 32;
5945 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5947#else
5948 mbedtls_sha256_context sha256;
5949
5950 mbedtls_sha256_init( &sha256 );
5951
5952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5953
5954 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5955 mbedtls_sha256_finish( &sha256, hash );
5956
5957 *hlen = 32;
5958
5959 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5961
5962 mbedtls_sha256_free( &sha256 );
5963#endif /* MBEDTLS_USE_PSA_CRYPTO */
5964 return;
5965}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005966#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005967
Andrzej Kurek25f27152022-08-17 16:09:31 -04005968#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005969void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5970 unsigned char *hash,
5971 size_t *hlen )
5972{
5973#if defined(MBEDTLS_USE_PSA_CRYPTO)
5974 size_t hash_size;
5975 psa_status_t status;
5976 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5977
5978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5979 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5980 if( status != PSA_SUCCESS )
5981 {
5982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5983 return;
5984 }
5985
5986 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5987 if( status != PSA_SUCCESS )
5988 {
5989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5990 return;
5991 }
5992
5993 *hlen = 48;
5994 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5996#else
5997 mbedtls_sha512_context sha512;
5998
5999 mbedtls_sha512_init( &sha512 );
6000
6001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6002
Andrzej Kureka242e832022-08-11 10:03:14 -04006003 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006004 mbedtls_sha512_finish( &sha512, hash );
6005
6006 *hlen = 48;
6007
6008 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6010
6011 mbedtls_sha512_free( &sha512 );
6012#endif /* MBEDTLS_USE_PSA_CRYPTO */
6013 return;
6014}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006015#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006016
Neil Armstrong80f6f322022-05-03 17:56:38 +02006017#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6018 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006019int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6020{
6021 unsigned char *p = ssl->handshake->premaster;
6022 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6023 const unsigned char *psk = NULL;
6024 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006025 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006026
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006027 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006028 {
6029 /*
6030 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006031 * checked before calling this function.
6032 *
6033 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006034 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006035 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006036 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6037 {
6038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6040 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006041 }
6042
6043 /*
6044 * PMS = struct {
6045 * opaque other_secret<0..2^16-1>;
6046 * opaque psk<0..2^16-1>;
6047 * };
6048 * with "other_secret" depending on the particular key exchange
6049 */
6050#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6051 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6052 {
6053 if( end - p < 2 )
6054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6055
6056 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6057 p += 2;
6058
6059 if( end < p || (size_t)( end - p ) < psk_len )
6060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6061
6062 memset( p, 0, psk_len );
6063 p += psk_len;
6064 }
6065 else
6066#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6067#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6068 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6069 {
6070 /*
6071 * other_secret already set by the ClientKeyExchange message,
6072 * and is 48 bytes long
6073 */
6074 if( end - p < 2 )
6075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6076
6077 *p++ = 0;
6078 *p++ = 48;
6079 p += 48;
6080 }
6081 else
6082#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6083#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6084 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6085 {
6086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6087 size_t len;
6088
6089 /* Write length only when we know the actual value */
6090 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6091 p + 2, end - ( p + 2 ), &len,
6092 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6093 {
6094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6095 return( ret );
6096 }
6097 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6098 p += 2 + len;
6099
6100 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6101 }
6102 else
6103#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006104#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006105 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6106 {
6107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6108 size_t zlen;
6109
6110 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6111 p + 2, end - ( p + 2 ),
6112 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6113 {
6114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6115 return( ret );
6116 }
6117
6118 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6119 p += 2 + zlen;
6120
6121 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6122 MBEDTLS_DEBUG_ECDH_Z );
6123 }
6124 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006125#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006126 {
6127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6129 }
6130
6131 /* opaque psk<0..2^16-1>; */
6132 if( end - p < 2 )
6133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6134
6135 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6136 p += 2;
6137
6138 if( end < p || (size_t)( end - p ) < psk_len )
6139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6140
6141 memcpy( p, psk, psk_len );
6142 p += psk_len;
6143
6144 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6145
6146 return( 0 );
6147}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006148#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006149
6150#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006151MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006152static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6153
6154#if defined(MBEDTLS_SSL_PROTO_DTLS)
6155int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6156{
6157 /* If renegotiation is not enforced, retransmit until we would reach max
6158 * timeout if we were using the usual handshake doubling scheme */
6159 if( ssl->conf->renego_max_records < 0 )
6160 {
6161 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6162 unsigned char doublings = 1;
6163
6164 while( ratio != 0 )
6165 {
6166 ++doublings;
6167 ratio >>= 1;
6168 }
6169
6170 if( ++ssl->renego_records_seen > doublings )
6171 {
6172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6173 return( 0 );
6174 }
6175 }
6176
6177 return( ssl_write_hello_request( ssl ) );
6178}
6179#endif
6180#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006181
Jerry Yud9526692022-02-17 14:23:47 +08006182/*
6183 * Handshake functions
6184 */
6185#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6186/* No certificate support -> dummy functions */
6187int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6188{
6189 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6190 ssl->handshake->ciphersuite_info;
6191
6192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6193
6194 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6195 {
6196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6197 ssl->state++;
6198 return( 0 );
6199 }
6200
6201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6203}
6204
6205int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6206{
6207 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6208 ssl->handshake->ciphersuite_info;
6209
6210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6211
6212 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6213 {
6214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6215 ssl->state++;
6216 return( 0 );
6217 }
6218
6219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6221}
6222
6223#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6224/* Some certificate support -> implement write and parse */
6225
6226int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6227{
6228 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6229 size_t i, n;
6230 const mbedtls_x509_crt *crt;
6231 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6232 ssl->handshake->ciphersuite_info;
6233
6234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6235
6236 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6237 {
6238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6239 ssl->state++;
6240 return( 0 );
6241 }
6242
6243#if defined(MBEDTLS_SSL_CLI_C)
6244 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6245 {
6246 if( ssl->handshake->client_auth == 0 )
6247 {
6248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6249 ssl->state++;
6250 return( 0 );
6251 }
6252 }
6253#endif /* MBEDTLS_SSL_CLI_C */
6254#if defined(MBEDTLS_SSL_SRV_C)
6255 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6256 {
6257 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6258 {
6259 /* Should never happen because we shouldn't have picked the
6260 * ciphersuite if we don't have a certificate. */
6261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6262 }
6263 }
6264#endif
6265
6266 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6267
6268 /*
6269 * 0 . 0 handshake type
6270 * 1 . 3 handshake length
6271 * 4 . 6 length of all certs
6272 * 7 . 9 length of cert. 1
6273 * 10 . n-1 peer certificate
6274 * n . n+2 length of cert. 2
6275 * n+3 . ... upper level cert, etc.
6276 */
6277 i = 7;
6278 crt = mbedtls_ssl_own_cert( ssl );
6279
6280 while( crt != NULL )
6281 {
6282 n = crt->raw.len;
6283 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6284 {
6285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6286 " > %" MBEDTLS_PRINTF_SIZET,
6287 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6288 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6289 }
6290
6291 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6292 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6293 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6294
6295 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6296 i += n; crt = crt->next;
6297 }
6298
6299 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6300 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6301 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6302
6303 ssl->out_msglen = i;
6304 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6305 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6306
6307 ssl->state++;
6308
6309 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6310 {
6311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6312 return( ret );
6313 }
6314
6315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6316
6317 return( ret );
6318}
6319
6320#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6321
6322#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006323MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006324static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6325 unsigned char *crt_buf,
6326 size_t crt_buf_len )
6327{
6328 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6329
6330 if( peer_crt == NULL )
6331 return( -1 );
6332
6333 if( peer_crt->raw.len != crt_buf_len )
6334 return( -1 );
6335
6336 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6337}
6338#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006339MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006340static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6341 unsigned char *crt_buf,
6342 size_t crt_buf_len )
6343{
6344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6345 unsigned char const * const peer_cert_digest =
6346 ssl->session->peer_cert_digest;
6347 mbedtls_md_type_t const peer_cert_digest_type =
6348 ssl->session->peer_cert_digest_type;
6349 mbedtls_md_info_t const * const digest_info =
6350 mbedtls_md_info_from_type( peer_cert_digest_type );
6351 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6352 size_t digest_len;
6353
6354 if( peer_cert_digest == NULL || digest_info == NULL )
6355 return( -1 );
6356
6357 digest_len = mbedtls_md_get_size( digest_info );
6358 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6359 return( -1 );
6360
6361 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6362 if( ret != 0 )
6363 return( -1 );
6364
6365 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6366}
6367#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6368#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6369
6370/*
6371 * Once the certificate message is read, parse it into a cert chain and
6372 * perform basic checks, but leave actual verification to the caller
6373 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006374MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006375static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6376 mbedtls_x509_crt *chain )
6377{
6378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6379#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6380 int crt_cnt=0;
6381#endif
6382 size_t i, n;
6383 uint8_t alert;
6384
6385 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6386 {
6387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6388 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6389 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6390 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6391 }
6392
6393 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6394 {
6395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6396 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6397 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6398 }
6399
6400 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6401 {
6402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6403 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6404 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6405 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6406 }
6407
6408 i = mbedtls_ssl_hs_hdr_len( ssl );
6409
6410 /*
6411 * Same message structure as in mbedtls_ssl_write_certificate()
6412 */
6413 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6414
6415 if( ssl->in_msg[i] != 0 ||
6416 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6417 {
6418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6419 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6420 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6421 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6422 }
6423
6424 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6425 i += 3;
6426
6427 /* Iterate through and parse the CRTs in the provided chain. */
6428 while( i < ssl->in_hslen )
6429 {
6430 /* Check that there's room for the next CRT's length fields. */
6431 if ( i + 3 > ssl->in_hslen ) {
6432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6433 mbedtls_ssl_send_alert_message( ssl,
6434 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6435 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6436 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6437 }
6438 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6439 * anything beyond 2**16 ~ 64K. */
6440 if( ssl->in_msg[i] != 0 )
6441 {
6442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6443 mbedtls_ssl_send_alert_message( ssl,
6444 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6445 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6446 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6447 }
6448
6449 /* Read length of the next CRT in the chain. */
6450 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6451 | (unsigned int) ssl->in_msg[i + 2];
6452 i += 3;
6453
6454 if( n < 128 || i + n > ssl->in_hslen )
6455 {
6456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6457 mbedtls_ssl_send_alert_message( ssl,
6458 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6460 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6461 }
6462
6463 /* Check if we're handling the first CRT in the chain. */
6464#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6465 if( crt_cnt++ == 0 &&
6466 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6467 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6468 {
6469 /* During client-side renegotiation, check that the server's
6470 * end-CRTs hasn't changed compared to the initial handshake,
6471 * mitigating the triple handshake attack. On success, reuse
6472 * the original end-CRT instead of parsing it again. */
6473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6474 if( ssl_check_peer_crt_unchanged( ssl,
6475 &ssl->in_msg[i],
6476 n ) != 0 )
6477 {
6478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6479 mbedtls_ssl_send_alert_message( ssl,
6480 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6481 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6482 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6483 }
6484
6485 /* Now we can safely free the original chain. */
6486 ssl_clear_peer_cert( ssl->session );
6487 }
6488#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6489
6490 /* Parse the next certificate in the chain. */
6491#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6492 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6493#else
6494 /* If we don't need to store the CRT chain permanently, parse
6495 * it in-place from the input buffer instead of making a copy. */
6496 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6497#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6498 switch( ret )
6499 {
6500 case 0: /*ok*/
6501 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6502 /* Ignore certificate with an unknown algorithm: maybe a
6503 prior certificate was already trusted. */
6504 break;
6505
6506 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6507 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6508 goto crt_parse_der_failed;
6509
6510 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6511 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6512 goto crt_parse_der_failed;
6513
6514 default:
6515 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6516 crt_parse_der_failed:
6517 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6518 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6519 return( ret );
6520 }
6521
6522 i += n;
6523 }
6524
6525 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6526 return( 0 );
6527}
6528
6529#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006530MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006531static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6532{
6533 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6534 return( -1 );
6535
6536 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6537 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6538 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6539 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6540 {
Ronald Cron19385882022-06-15 16:26:13 +02006541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006542 return( 0 );
6543 }
6544 return( -1 );
6545}
6546#endif /* MBEDTLS_SSL_SRV_C */
6547
6548/* Check if a certificate message is expected.
6549 * Return either
6550 * - SSL_CERTIFICATE_EXPECTED, or
6551 * - SSL_CERTIFICATE_SKIP
6552 * indicating whether a Certificate message is expected or not.
6553 */
6554#define SSL_CERTIFICATE_EXPECTED 0
6555#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006556MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006557static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6558 int authmode )
6559{
6560 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6561 ssl->handshake->ciphersuite_info;
6562
6563 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6564 return( SSL_CERTIFICATE_SKIP );
6565
6566#if defined(MBEDTLS_SSL_SRV_C)
6567 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6568 {
6569 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6570 return( SSL_CERTIFICATE_SKIP );
6571
6572 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6573 {
6574 ssl->session_negotiate->verify_result =
6575 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6576 return( SSL_CERTIFICATE_SKIP );
6577 }
6578 }
6579#else
6580 ((void) authmode);
6581#endif /* MBEDTLS_SSL_SRV_C */
6582
6583 return( SSL_CERTIFICATE_EXPECTED );
6584}
6585
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006586MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006587static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6588 int authmode,
6589 mbedtls_x509_crt *chain,
6590 void *rs_ctx )
6591{
6592 int ret = 0;
6593 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6594 ssl->handshake->ciphersuite_info;
6595 int have_ca_chain = 0;
6596
6597 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6598 void *p_vrfy;
6599
6600 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6601 return( 0 );
6602
6603 if( ssl->f_vrfy != NULL )
6604 {
6605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6606 f_vrfy = ssl->f_vrfy;
6607 p_vrfy = ssl->p_vrfy;
6608 }
6609 else
6610 {
6611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6612 f_vrfy = ssl->conf->f_vrfy;
6613 p_vrfy = ssl->conf->p_vrfy;
6614 }
6615
6616 /*
6617 * Main check: verify certificate
6618 */
6619#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6620 if( ssl->conf->f_ca_cb != NULL )
6621 {
6622 ((void) rs_ctx);
6623 have_ca_chain = 1;
6624
6625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6626 ret = mbedtls_x509_crt_verify_with_ca_cb(
6627 chain,
6628 ssl->conf->f_ca_cb,
6629 ssl->conf->p_ca_cb,
6630 ssl->conf->cert_profile,
6631 ssl->hostname,
6632 &ssl->session_negotiate->verify_result,
6633 f_vrfy, p_vrfy );
6634 }
6635 else
6636#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6637 {
6638 mbedtls_x509_crt *ca_chain;
6639 mbedtls_x509_crl *ca_crl;
6640
6641#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6642 if( ssl->handshake->sni_ca_chain != NULL )
6643 {
6644 ca_chain = ssl->handshake->sni_ca_chain;
6645 ca_crl = ssl->handshake->sni_ca_crl;
6646 }
6647 else
6648#endif
6649 {
6650 ca_chain = ssl->conf->ca_chain;
6651 ca_crl = ssl->conf->ca_crl;
6652 }
6653
6654 if( ca_chain != NULL )
6655 have_ca_chain = 1;
6656
6657 ret = mbedtls_x509_crt_verify_restartable(
6658 chain,
6659 ca_chain, ca_crl,
6660 ssl->conf->cert_profile,
6661 ssl->hostname,
6662 &ssl->session_negotiate->verify_result,
6663 f_vrfy, p_vrfy, rs_ctx );
6664 }
6665
6666 if( ret != 0 )
6667 {
6668 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6669 }
6670
6671#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6672 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6673 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6674#endif
6675
6676 /*
6677 * Secondary checks: always done, but change 'ret' only if it was 0
6678 */
6679
6680#if defined(MBEDTLS_ECP_C)
6681 {
6682 const mbedtls_pk_context *pk = &chain->pk;
6683
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006684 /* If certificate uses an EC key, make sure the curve is OK.
6685 * This is a public key, so it can't be opaque, so can_do() is a good
6686 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006687 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006688 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006689 /* and in the unlikely case the above assumption no longer holds
6690 * we are making sure that pk_ec() here does not return a NULL
6691 */
6692 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6693 if( ec == NULL )
6694 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6697 }
Jerry Yud9526692022-02-17 14:23:47 +08006698
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006699 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6700 {
6701 ssl->session_negotiate->verify_result |=
6702 MBEDTLS_X509_BADCERT_BAD_KEY;
6703
6704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6705 if( ret == 0 )
6706 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6707 }
Jerry Yud9526692022-02-17 14:23:47 +08006708 }
6709 }
6710#endif /* MBEDTLS_ECP_C */
6711
6712 if( mbedtls_ssl_check_cert_usage( chain,
6713 ciphersuite_info,
6714 ! ssl->conf->endpoint,
6715 &ssl->session_negotiate->verify_result ) != 0 )
6716 {
6717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6718 if( ret == 0 )
6719 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6720 }
6721
6722 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6723 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6724 * with details encoded in the verification flags. All other kinds
6725 * of error codes, including those from the user provided f_vrfy
6726 * functions, are treated as fatal and lead to a failure of
6727 * ssl_parse_certificate even if verification was optional. */
6728 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6729 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6730 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6731 {
6732 ret = 0;
6733 }
6734
6735 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6736 {
6737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6738 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6739 }
6740
6741 if( ret != 0 )
6742 {
6743 uint8_t alert;
6744
6745 /* The certificate may have been rejected for several reasons.
6746 Pick one and send the corresponding alert. Which alert to send
6747 may be a subject of debate in some cases. */
6748 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6749 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6750 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6751 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6752 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6753 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6754 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6755 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6756 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6757 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6758 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6759 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6760 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6761 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6762 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6763 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6764 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6765 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6766 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6767 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6768 else
6769 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6770 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6771 alert );
6772 }
6773
6774#if defined(MBEDTLS_DEBUG_C)
6775 if( ssl->session_negotiate->verify_result != 0 )
6776 {
6777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6778 (unsigned int) ssl->session_negotiate->verify_result ) );
6779 }
6780 else
6781 {
6782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6783 }
6784#endif /* MBEDTLS_DEBUG_C */
6785
6786 return( ret );
6787}
6788
6789#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006790MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006791static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6792 unsigned char *start, size_t len )
6793{
6794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6795 /* Remember digest of the peer's end-CRT. */
6796 ssl->session_negotiate->peer_cert_digest =
6797 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6798 if( ssl->session_negotiate->peer_cert_digest == NULL )
6799 {
6800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6801 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6802 mbedtls_ssl_send_alert_message( ssl,
6803 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6804 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6805
6806 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6807 }
6808
6809 ret = mbedtls_md( mbedtls_md_info_from_type(
6810 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6811 start, len,
6812 ssl->session_negotiate->peer_cert_digest );
6813
6814 ssl->session_negotiate->peer_cert_digest_type =
6815 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6816 ssl->session_negotiate->peer_cert_digest_len =
6817 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6818
6819 return( ret );
6820}
6821
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006822MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006823static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6824 unsigned char *start, size_t len )
6825{
6826 unsigned char *end = start + len;
6827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6828
6829 /* Make a copy of the peer's raw public key. */
6830 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6831 ret = mbedtls_pk_parse_subpubkey( &start, end,
6832 &ssl->handshake->peer_pubkey );
6833 if( ret != 0 )
6834 {
6835 /* We should have parsed the public key before. */
6836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6837 }
6838
6839 return( 0 );
6840}
6841#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6842
6843int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6844{
6845 int ret = 0;
6846 int crt_expected;
6847#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6848 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6849 ? ssl->handshake->sni_authmode
6850 : ssl->conf->authmode;
6851#else
6852 const int authmode = ssl->conf->authmode;
6853#endif
6854 void *rs_ctx = NULL;
6855 mbedtls_x509_crt *chain = NULL;
6856
6857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6858
6859 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6860 if( crt_expected == SSL_CERTIFICATE_SKIP )
6861 {
6862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6863 goto exit;
6864 }
6865
6866#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6867 if( ssl->handshake->ecrs_enabled &&
6868 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6869 {
6870 chain = ssl->handshake->ecrs_peer_cert;
6871 ssl->handshake->ecrs_peer_cert = NULL;
6872 goto crt_verify;
6873 }
6874#endif
6875
6876 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6877 {
6878 /* mbedtls_ssl_read_record may have sent an alert already. We
6879 let it decide whether to alert. */
6880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6881 goto exit;
6882 }
6883
6884#if defined(MBEDTLS_SSL_SRV_C)
6885 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6886 {
6887 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6888
6889 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6890 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6891
6892 goto exit;
6893 }
6894#endif /* MBEDTLS_SSL_SRV_C */
6895
6896 /* Clear existing peer CRT structure in case we tried to
6897 * reuse a session but it failed, and allocate a new one. */
6898 ssl_clear_peer_cert( ssl->session_negotiate );
6899
6900 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6901 if( chain == NULL )
6902 {
6903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6904 sizeof( mbedtls_x509_crt ) ) );
6905 mbedtls_ssl_send_alert_message( ssl,
6906 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6907 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6908
6909 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6910 goto exit;
6911 }
6912 mbedtls_x509_crt_init( chain );
6913
6914 ret = ssl_parse_certificate_chain( ssl, chain );
6915 if( ret != 0 )
6916 goto exit;
6917
6918#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6919 if( ssl->handshake->ecrs_enabled)
6920 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6921
6922crt_verify:
6923 if( ssl->handshake->ecrs_enabled)
6924 rs_ctx = &ssl->handshake->ecrs_ctx;
6925#endif
6926
6927 ret = ssl_parse_certificate_verify( ssl, authmode,
6928 chain, rs_ctx );
6929 if( ret != 0 )
6930 goto exit;
6931
6932#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6933 {
6934 unsigned char *crt_start, *pk_start;
6935 size_t crt_len, pk_len;
6936
6937 /* We parse the CRT chain without copying, so
6938 * these pointers point into the input buffer,
6939 * and are hence still valid after freeing the
6940 * CRT chain. */
6941
6942 crt_start = chain->raw.p;
6943 crt_len = chain->raw.len;
6944
6945 pk_start = chain->pk_raw.p;
6946 pk_len = chain->pk_raw.len;
6947
6948 /* Free the CRT structures before computing
6949 * digest and copying the peer's public key. */
6950 mbedtls_x509_crt_free( chain );
6951 mbedtls_free( chain );
6952 chain = NULL;
6953
6954 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6955 if( ret != 0 )
6956 goto exit;
6957
6958 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6959 if( ret != 0 )
6960 goto exit;
6961 }
6962#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6963 /* Pass ownership to session structure. */
6964 ssl->session_negotiate->peer_cert = chain;
6965 chain = NULL;
6966#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6967
6968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6969
6970exit:
6971
6972 if( ret == 0 )
6973 ssl->state++;
6974
6975#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6976 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6977 {
6978 ssl->handshake->ecrs_peer_cert = chain;
6979 chain = NULL;
6980 }
6981#endif
6982
6983 if( chain != NULL )
6984 {
6985 mbedtls_x509_crt_free( chain );
6986 mbedtls_free( chain );
6987 }
6988
6989 return( ret );
6990}
6991#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6992
Andrzej Kurek25f27152022-08-17 16:09:31 -04006993#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006994static void ssl_calc_finished_tls_sha256(
6995 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6996{
6997 int len = 12;
6998 const char *sender;
6999 unsigned char padbuf[32];
7000#if defined(MBEDTLS_USE_PSA_CRYPTO)
7001 size_t hash_size;
7002 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7003 psa_status_t status;
7004#else
7005 mbedtls_sha256_context sha256;
7006#endif
7007
7008 mbedtls_ssl_session *session = ssl->session_negotiate;
7009 if( !session )
7010 session = ssl->session;
7011
7012 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7013 ? "client finished"
7014 : "server finished";
7015
7016#if defined(MBEDTLS_USE_PSA_CRYPTO)
7017 sha256_psa = psa_hash_operation_init();
7018
7019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7020
7021 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7022 if( status != PSA_SUCCESS )
7023 {
7024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7025 return;
7026 }
7027
7028 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7029 if( status != PSA_SUCCESS )
7030 {
7031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7032 return;
7033 }
7034 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7035#else
7036
7037 mbedtls_sha256_init( &sha256 );
7038
7039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7040
7041 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7042
7043 /*
7044 * TLSv1.2:
7045 * hash = PRF( master, finished_label,
7046 * Hash( handshake ) )[0.11]
7047 */
7048
7049#if !defined(MBEDTLS_SHA256_ALT)
7050 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7051 sha256.state, sizeof( sha256.state ) );
7052#endif
7053
7054 mbedtls_sha256_finish( &sha256, padbuf );
7055 mbedtls_sha256_free( &sha256 );
7056#endif /* MBEDTLS_USE_PSA_CRYPTO */
7057
7058 ssl->handshake->tls_prf( session->master, 48, sender,
7059 padbuf, 32, buf, len );
7060
7061 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7062
7063 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7064
7065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7066}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007067#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007068
Jerry Yub7ba49e2022-02-17 14:25:53 +08007069
Andrzej Kurek25f27152022-08-17 16:09:31 -04007070#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007071static void ssl_calc_finished_tls_sha384(
7072 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7073{
7074 int len = 12;
7075 const char *sender;
7076 unsigned char padbuf[48];
7077#if defined(MBEDTLS_USE_PSA_CRYPTO)
7078 size_t hash_size;
7079 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7080 psa_status_t status;
7081#else
7082 mbedtls_sha512_context sha512;
7083#endif
7084
7085 mbedtls_ssl_session *session = ssl->session_negotiate;
7086 if( !session )
7087 session = ssl->session;
7088
7089 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7090 ? "client finished"
7091 : "server finished";
7092
7093#if defined(MBEDTLS_USE_PSA_CRYPTO)
7094 sha384_psa = psa_hash_operation_init();
7095
7096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7097
7098 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7099 if( status != PSA_SUCCESS )
7100 {
7101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7102 return;
7103 }
7104
7105 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7106 if( status != PSA_SUCCESS )
7107 {
7108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7109 return;
7110 }
7111 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7112#else
7113 mbedtls_sha512_init( &sha512 );
7114
7115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7116
Andrzej Kureka242e832022-08-11 10:03:14 -04007117 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007118
7119 /*
7120 * TLSv1.2:
7121 * hash = PRF( master, finished_label,
7122 * Hash( handshake ) )[0.11]
7123 */
7124
7125#if !defined(MBEDTLS_SHA512_ALT)
7126 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7127 sha512.state, sizeof( sha512.state ) );
7128#endif
7129 mbedtls_sha512_finish( &sha512, padbuf );
7130
7131 mbedtls_sha512_free( &sha512 );
7132#endif
7133
7134 ssl->handshake->tls_prf( session->master, 48, sender,
7135 padbuf, 48, buf, len );
7136
7137 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7138
7139 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7140
7141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7142}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007143#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007144
Jerry Yuaef00152022-02-17 14:27:31 +08007145void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7146{
7147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7148
7149 /*
7150 * Free our handshake params
7151 */
7152 mbedtls_ssl_handshake_free( ssl );
7153 mbedtls_free( ssl->handshake );
7154 ssl->handshake = NULL;
7155
7156 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007157 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007158 */
7159 if( ssl->transform )
7160 {
7161 mbedtls_ssl_transform_free( ssl->transform );
7162 mbedtls_free( ssl->transform );
7163 }
7164 ssl->transform = ssl->transform_negotiate;
7165 ssl->transform_negotiate = NULL;
7166
7167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7168}
7169
Jerry Yu2a9fff52022-02-17 14:28:51 +08007170void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7171{
7172 int resume = ssl->handshake->resume;
7173
7174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7175
7176#if defined(MBEDTLS_SSL_RENEGOTIATION)
7177 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7178 {
7179 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7180 ssl->renego_records_seen = 0;
7181 }
7182#endif
7183
7184 /*
7185 * Free the previous session and switch in the current one
7186 */
7187 if( ssl->session )
7188 {
7189#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7190 /* RFC 7366 3.1: keep the EtM state */
7191 ssl->session_negotiate->encrypt_then_mac =
7192 ssl->session->encrypt_then_mac;
7193#endif
7194
7195 mbedtls_ssl_session_free( ssl->session );
7196 mbedtls_free( ssl->session );
7197 }
7198 ssl->session = ssl->session_negotiate;
7199 ssl->session_negotiate = NULL;
7200
7201 /*
7202 * Add cache entry
7203 */
7204 if( ssl->conf->f_set_cache != NULL &&
7205 ssl->session->id_len != 0 &&
7206 resume == 0 )
7207 {
7208 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7209 ssl->session->id,
7210 ssl->session->id_len,
7211 ssl->session ) != 0 )
7212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7213 }
7214
7215#if defined(MBEDTLS_SSL_PROTO_DTLS)
7216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7217 ssl->handshake->flight != NULL )
7218 {
7219 /* Cancel handshake timer */
7220 mbedtls_ssl_set_timer( ssl, 0 );
7221
7222 /* Keep last flight around in case we need to resend it:
7223 * we need the handshake and transform structures for that */
7224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7225 }
7226 else
7227#endif
7228 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7229
7230 ssl->state++;
7231
7232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7233}
7234
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007235int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7236{
7237 int ret, hash_len;
7238
7239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7240
7241 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7242
7243 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7244
7245 /*
7246 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7247 * may define some other value. Currently (early 2016), no defined
7248 * ciphersuite does this (and this is unlikely to change as activity has
7249 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7250 */
7251 hash_len = 12;
7252
7253#if defined(MBEDTLS_SSL_RENEGOTIATION)
7254 ssl->verify_data_len = hash_len;
7255 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7256#endif
7257
7258 ssl->out_msglen = 4 + hash_len;
7259 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7260 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7261
7262 /*
7263 * In case of session resuming, invert the client and server
7264 * ChangeCipherSpec messages order.
7265 */
7266 if( ssl->handshake->resume != 0 )
7267 {
7268#if defined(MBEDTLS_SSL_CLI_C)
7269 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7270 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7271#endif
7272#if defined(MBEDTLS_SSL_SRV_C)
7273 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7274 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7275#endif
7276 }
7277 else
7278 ssl->state++;
7279
7280 /*
7281 * Switch to our negotiated transform and session parameters for outbound
7282 * data.
7283 */
7284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7285
7286#if defined(MBEDTLS_SSL_PROTO_DTLS)
7287 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7288 {
7289 unsigned char i;
7290
7291 /* Remember current epoch settings for resending */
7292 ssl->handshake->alt_transform_out = ssl->transform_out;
7293 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7294 sizeof( ssl->handshake->alt_out_ctr ) );
7295
7296 /* Set sequence_number to zero */
7297 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7298
7299
7300 /* Increment epoch */
7301 for( i = 2; i > 0; i-- )
7302 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7303 break;
7304
7305 /* The loop goes to its end iff the counter is wrapping */
7306 if( i == 0 )
7307 {
7308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7309 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7310 }
7311 }
7312 else
7313#endif /* MBEDTLS_SSL_PROTO_DTLS */
7314 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7315
7316 ssl->transform_out = ssl->transform_negotiate;
7317 ssl->session_out = ssl->session_negotiate;
7318
7319#if defined(MBEDTLS_SSL_PROTO_DTLS)
7320 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7321 mbedtls_ssl_send_flight_completed( ssl );
7322#endif
7323
7324 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7325 {
7326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7327 return( ret );
7328 }
7329
7330#if defined(MBEDTLS_SSL_PROTO_DTLS)
7331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7332 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7333 {
7334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7335 return( ret );
7336 }
7337#endif
7338
7339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7340
7341 return( 0 );
7342}
7343
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007344#define SSL_MAX_HASH_LEN 12
7345
7346int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7347{
7348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7349 unsigned int hash_len = 12;
7350 unsigned char buf[SSL_MAX_HASH_LEN];
7351
7352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7353
7354 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7355
7356 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7357 {
7358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7359 goto exit;
7360 }
7361
7362 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7363 {
7364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7365 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7366 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7367 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7368 goto exit;
7369 }
7370
7371 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7372 {
7373 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7374 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7375 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7376 goto exit;
7377 }
7378
7379 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7380 {
7381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7382 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7383 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7384 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7385 goto exit;
7386 }
7387
7388 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7389 buf, hash_len ) != 0 )
7390 {
7391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7392 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7393 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7394 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7395 goto exit;
7396 }
7397
7398#if defined(MBEDTLS_SSL_RENEGOTIATION)
7399 ssl->verify_data_len = hash_len;
7400 memcpy( ssl->peer_verify_data, buf, hash_len );
7401#endif
7402
7403 if( ssl->handshake->resume != 0 )
7404 {
7405#if defined(MBEDTLS_SSL_CLI_C)
7406 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7407 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7408#endif
7409#if defined(MBEDTLS_SSL_SRV_C)
7410 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7411 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7412#endif
7413 }
7414 else
7415 ssl->state++;
7416
7417#if defined(MBEDTLS_SSL_PROTO_DTLS)
7418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7419 mbedtls_ssl_recv_flight_completed( ssl );
7420#endif
7421
7422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7423
7424exit:
7425 mbedtls_platform_zeroize( buf, hash_len );
7426 return( ret );
7427}
7428
Jerry Yu392112c2022-02-17 14:34:10 +08007429#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7430/*
7431 * Helper to get TLS 1.2 PRF from ciphersuite
7432 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7433 */
7434static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7435{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007436#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007437 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7438 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7439
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007440 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007441 return( tls_prf_sha384 );
7442#else
7443 (void) ciphersuite_id;
7444#endif
7445 return( tls_prf_sha256 );
7446}
7447#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007448
7449static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7450{
7451 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007452#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007453 if( tls_prf == tls_prf_sha384 )
7454 {
7455 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7456 }
7457 else
7458#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007459#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007460 if( tls_prf == tls_prf_sha256 )
7461 {
7462 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7463 }
7464 else
7465#endif
7466 return( MBEDTLS_SSL_TLS_PRF_NONE );
7467}
7468
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007469/*
7470 * Populate a transform structure with session keys and all the other
7471 * necessary information.
7472 *
7473 * Parameters:
7474 * - [in/out]: transform: structure to populate
7475 * [in] must be just initialised with mbedtls_ssl_transform_init()
7476 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7477 * - [in] ciphersuite
7478 * - [in] master
7479 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007480 * - [in] tls_prf: pointer to PRF to use for key derivation
7481 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007482 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007483 * - [in] endpoint: client or server
7484 * - [in] ssl: used for:
7485 * - ssl->conf->{f,p}_export_keys
7486 * [in] optionally used for:
7487 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7488 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007489MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007490static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7491 int ciphersuite,
7492 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007493#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007494 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007495#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007496 ssl_tls_prf_t tls_prf,
7497 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007498 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007499 unsigned endpoint,
7500 const mbedtls_ssl_context *ssl )
7501{
7502 int ret = 0;
7503 unsigned char keyblk[256];
7504 unsigned char *key1;
7505 unsigned char *key2;
7506 unsigned char *mac_enc;
7507 unsigned char *mac_dec;
7508 size_t mac_key_len = 0;
7509 size_t iv_copy_len;
7510 size_t keylen;
7511 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007512 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007513#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007514 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007515 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007516#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007517
7518#if defined(MBEDTLS_USE_PSA_CRYPTO)
7519 psa_key_type_t key_type;
7520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7521 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007522 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007523 size_t key_bits;
7524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7525#endif
7526
7527#if !defined(MBEDTLS_DEBUG_C) && \
7528 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7529 if( ssl->f_export_keys == NULL )
7530 {
7531 ssl = NULL; /* make sure we don't use it except for these cases */
7532 (void) ssl;
7533 }
7534#endif
7535
7536 /*
7537 * Some data just needs copying into the structure
7538 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007539#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007540 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007541#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007542 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007543
7544#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7545 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7546#endif
7547
7548#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007549 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007550 {
7551 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7552 * generation separate. This should never happen. */
7553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7554 }
7555#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7556
7557 /*
7558 * Get various info structures
7559 */
7560 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7561 if( ciphersuite_info == NULL )
7562 {
7563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7564 ciphersuite ) );
7565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7566 }
7567
Neil Armstrongab555e02022-04-04 11:07:59 +02007568 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007569#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007570 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007571#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007572 ciphersuite_info );
7573
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007574 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7575 transform->taglen =
7576 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7577
7578#if defined(MBEDTLS_USE_PSA_CRYPTO)
7579 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7580 transform->taglen,
7581 &alg,
7582 &key_type,
7583 &key_bits ) ) != PSA_SUCCESS )
7584 {
7585 ret = psa_ssl_status_to_mbedtls( status );
7586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7587 goto end;
7588 }
7589#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007590 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7591 if( cipher_info == NULL )
7592 {
7593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7594 ciphersuite_info->cipher ) );
7595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7596 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007597#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007598
Neil Armstronge4512952022-03-08 09:08:22 +01007599#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007600 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007601 if( mac_alg == 0 )
7602 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007604 (unsigned) ciphersuite_info->mac ) );
7605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7606 }
7607#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007608 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7609 if( md_info == NULL )
7610 {
7611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7612 (unsigned) ciphersuite_info->mac ) );
7613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7614 }
Neil Armstronge4512952022-03-08 09:08:22 +01007615#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007616
7617#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7618 /* Copy own and peer's CID if the use of the CID
7619 * extension has been negotiated. */
7620 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7621 {
7622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7623
7624 transform->in_cid_len = ssl->own_cid_len;
7625 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7626 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7627 transform->in_cid_len );
7628
7629 transform->out_cid_len = ssl->handshake->peer_cid_len;
7630 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7631 ssl->handshake->peer_cid_len );
7632 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7633 transform->out_cid_len );
7634 }
7635#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7636
7637 /*
7638 * Compute key block using the PRF
7639 */
7640 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7641 if( ret != 0 )
7642 {
7643 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7644 return( ret );
7645 }
7646
7647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7648 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7649 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7650 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7651 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7652
7653 /*
7654 * Determine the appropriate key, IV and MAC length.
7655 */
7656
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007657#if defined(MBEDTLS_USE_PSA_CRYPTO)
7658 keylen = PSA_BITS_TO_BYTES(key_bits);
7659#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007660 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007661#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007662
7663#if defined(MBEDTLS_GCM_C) || \
7664 defined(MBEDTLS_CCM_C) || \
7665 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007666 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007667 {
7668 size_t explicit_ivlen;
7669
7670 transform->maclen = 0;
7671 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007672
7673 /* All modes haves 96-bit IVs, but the length of the static parts vary
7674 * with mode and version:
7675 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7676 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7677 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7678 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7679 * sequence number).
7680 */
7681 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007682#if defined(MBEDTLS_USE_PSA_CRYPTO)
7683 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7684#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007685 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007686#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007687 transform->fixed_ivlen = 12;
7688 else
7689 transform->fixed_ivlen = 4;
7690
7691 /* Minimum length of encrypted record */
7692 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7693 transform->minlen = explicit_ivlen + transform->taglen;
7694 }
7695 else
7696#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7697#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007698 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7699 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7700 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007701 {
Neil Armstronge4512952022-03-08 09:08:22 +01007702#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007703 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007704#else
7705 size_t block_size = cipher_info->block_size;
7706#endif /* MBEDTLS_USE_PSA_CRYPTO */
7707
7708#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007709 /* Get MAC length */
7710 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7711#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007712 /* Initialize HMAC contexts */
7713 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7714 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7715 {
7716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7717 goto end;
7718 }
7719
7720 /* Get MAC length */
7721 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007722#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007723 transform->maclen = mac_key_len;
7724
7725 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007726#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007727 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007728#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007729 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007730#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007731
7732 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007733 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007734 transform->minlen = transform->maclen;
7735 else
7736 {
7737 /*
7738 * GenericBlockCipher:
7739 * 1. if EtM is in use: one block plus MAC
7740 * otherwise: * first multiple of blocklen greater than maclen
7741 * 2. IV
7742 */
7743#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007744 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007745 {
7746 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007747 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007748 }
7749 else
7750#endif
7751 {
7752 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007753 + block_size
7754 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007755 }
7756
Glenn Strauss07c64162022-03-14 12:34:51 -04007757 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007758 {
7759 transform->minlen += transform->ivlen;
7760 }
7761 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007762 {
7763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7764 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7765 goto end;
7766 }
7767 }
7768 }
7769 else
7770#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7771 {
7772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7774 }
7775
7776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7777 (unsigned) keylen,
7778 (unsigned) transform->minlen,
7779 (unsigned) transform->ivlen,
7780 (unsigned) transform->maclen ) );
7781
7782 /*
7783 * Finally setup the cipher contexts, IVs and MAC secrets.
7784 */
7785#if defined(MBEDTLS_SSL_CLI_C)
7786 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7787 {
7788 key1 = keyblk + mac_key_len * 2;
7789 key2 = keyblk + mac_key_len * 2 + keylen;
7790
7791 mac_enc = keyblk;
7792 mac_dec = keyblk + mac_key_len;
7793
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007794 iv_copy_len = ( transform->fixed_ivlen ) ?
7795 transform->fixed_ivlen : transform->ivlen;
7796 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7797 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7798 iv_copy_len );
7799 }
7800 else
7801#endif /* MBEDTLS_SSL_CLI_C */
7802#if defined(MBEDTLS_SSL_SRV_C)
7803 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7804 {
7805 key1 = keyblk + mac_key_len * 2 + keylen;
7806 key2 = keyblk + mac_key_len * 2;
7807
7808 mac_enc = keyblk + mac_key_len;
7809 mac_dec = keyblk;
7810
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007811 iv_copy_len = ( transform->fixed_ivlen ) ?
7812 transform->fixed_ivlen : transform->ivlen;
7813 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7814 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7815 iv_copy_len );
7816 }
7817 else
7818#endif /* MBEDTLS_SSL_SRV_C */
7819 {
7820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7821 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7822 goto end;
7823 }
7824
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007825 if( ssl != NULL && ssl->f_export_keys != NULL )
7826 {
7827 ssl->f_export_keys( ssl->p_export_keys,
7828 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7829 master, 48,
7830 randbytes + 32,
7831 randbytes,
7832 tls_prf_get_type( tls_prf ) );
7833 }
7834
7835#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007836 transform->psa_alg = alg;
7837
7838 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7839 {
7840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7841 psa_set_key_algorithm( &attributes, alg );
7842 psa_set_key_type( &attributes, key_type );
7843
7844 if( ( status = psa_import_key( &attributes,
7845 key1,
7846 PSA_BITS_TO_BYTES( key_bits ),
7847 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7848 {
7849 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7850 ret = psa_ssl_status_to_mbedtls( status );
7851 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7852 goto end;
7853 }
7854
7855 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7856
7857 if( ( status = psa_import_key( &attributes,
7858 key2,
7859 PSA_BITS_TO_BYTES( key_bits ),
7860 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7861 {
7862 ret = psa_ssl_status_to_mbedtls( status );
7863 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7864 goto end;
7865 }
7866 }
7867#else
7868 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7869 cipher_info ) ) != 0 )
7870 {
7871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7872 goto end;
7873 }
7874
7875 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7876 cipher_info ) ) != 0 )
7877 {
7878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7879 goto end;
7880 }
7881
7882 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7883 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7884 MBEDTLS_ENCRYPT ) ) != 0 )
7885 {
7886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7887 goto end;
7888 }
7889
7890 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7891 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7892 MBEDTLS_DECRYPT ) ) != 0 )
7893 {
7894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7895 goto end;
7896 }
7897
7898#if defined(MBEDTLS_CIPHER_MODE_CBC)
7899 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7900 {
7901 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7902 MBEDTLS_PADDING_NONE ) ) != 0 )
7903 {
7904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7905 goto end;
7906 }
7907
7908 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7909 MBEDTLS_PADDING_NONE ) ) != 0 )
7910 {
7911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7912 goto end;
7913 }
7914 }
7915#endif /* MBEDTLS_CIPHER_MODE_CBC */
7916#endif /* MBEDTLS_USE_PSA_CRYPTO */
7917
Neil Armstrong29c0c042022-03-17 17:47:28 +01007918#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7919 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7920 For AEAD-based ciphersuites, there is nothing to do here. */
7921 if( mac_key_len != 0 )
7922 {
7923#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007924 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007925
7926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007927 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007928 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7929
7930 if( ( status = psa_import_key( &attributes,
7931 mac_enc, mac_key_len,
7932 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7933 {
7934 ret = psa_ssl_status_to_mbedtls( status );
7935 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7936 goto end;
7937 }
7938
Ronald Cronfb39f152022-03-25 14:36:28 +01007939 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007940 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7941#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7942 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7943#endif
7944 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007945 /* mbedtls_ct_hmac() requires the key to be exportable */
7946 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7947 PSA_KEY_USAGE_VERIFY_HASH );
7948 else
7949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7950
7951 if( ( status = psa_import_key( &attributes,
7952 mac_dec, mac_key_len,
7953 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7954 {
7955 ret = psa_ssl_status_to_mbedtls( status );
7956 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7957 goto end;
7958 }
7959#else
7960 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7961 if( ret != 0 )
7962 goto end;
7963 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7964 if( ret != 0 )
7965 goto end;
7966#endif /* MBEDTLS_USE_PSA_CRYPTO */
7967 }
7968#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7969
7970 ((void) mac_dec);
7971 ((void) mac_enc);
7972
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007973end:
7974 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7975 return( ret );
7976}
7977
Jerry Yuee40f9d2022-02-17 14:55:16 +08007978#if defined(MBEDTLS_USE_PSA_CRYPTO)
7979int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7980 unsigned char *hash, size_t *hashlen,
7981 unsigned char *data, size_t data_len,
7982 mbedtls_md_type_t md_alg )
7983{
7984 psa_status_t status;
7985 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007986 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007987
7988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7989
7990 if( ( status = psa_hash_setup( &hash_operation,
7991 hash_alg ) ) != PSA_SUCCESS )
7992 {
7993 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7994 goto exit;
7995 }
7996
7997 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7998 64 ) ) != PSA_SUCCESS )
7999 {
8000 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8001 goto exit;
8002 }
8003
8004 if( ( status = psa_hash_update( &hash_operation,
8005 data, data_len ) ) != PSA_SUCCESS )
8006 {
8007 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8008 goto exit;
8009 }
8010
8011 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8012 hashlen ) ) != PSA_SUCCESS )
8013 {
8014 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8015 goto exit;
8016 }
8017
8018exit:
8019 if( status != PSA_SUCCESS )
8020 {
8021 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8022 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8023 switch( status )
8024 {
8025 case PSA_ERROR_NOT_SUPPORTED:
8026 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8027 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8028 case PSA_ERROR_BUFFER_TOO_SMALL:
8029 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8030 case PSA_ERROR_INSUFFICIENT_MEMORY:
8031 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8032 default:
8033 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8034 }
8035 }
8036 return( 0 );
8037}
8038
8039#else
8040
8041int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8042 unsigned char *hash, size_t *hashlen,
8043 unsigned char *data, size_t data_len,
8044 mbedtls_md_type_t md_alg )
8045{
8046 int ret = 0;
8047 mbedtls_md_context_t ctx;
8048 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8049 *hashlen = mbedtls_md_get_size( md_info );
8050
8051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8052
8053 mbedtls_md_init( &ctx );
8054
8055 /*
8056 * digitally-signed struct {
8057 * opaque client_random[32];
8058 * opaque server_random[32];
8059 * ServerDHParams params;
8060 * };
8061 */
8062 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8063 {
8064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8065 goto exit;
8066 }
8067 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8068 {
8069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8070 goto exit;
8071 }
8072 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8073 {
8074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8075 goto exit;
8076 }
8077 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8078 {
8079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8080 goto exit;
8081 }
8082 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8083 {
8084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8085 goto exit;
8086 }
8087
8088exit:
8089 mbedtls_md_free( &ctx );
8090
8091 if( ret != 0 )
8092 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8093 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8094
8095 return( ret );
8096}
8097#endif /* MBEDTLS_USE_PSA_CRYPTO */
8098
Jerry Yud9d91da2022-02-17 14:57:06 +08008099#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8100
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008101/* Find the preferred hash for a given signature algorithm. */
8102unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8103 mbedtls_ssl_context *ssl,
8104 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008105{
Gabor Mezei078e8032022-04-27 21:17:56 +02008106 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008107 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008108
8109 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008110 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008111
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008112 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008113 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008114 unsigned int hash_alg_received =
8115 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8116 received_sig_algs[i] );
8117 unsigned int sig_alg_received =
8118 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8119 received_sig_algs[i] );
8120
8121 if( sig_alg == sig_alg_received )
8122 {
8123#if defined(MBEDTLS_USE_PSA_CRYPTO)
8124 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8125 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008126 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008127 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008128
Neil Armstrong96eceb82022-06-30 18:05:05 +02008129 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8130 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8131 PSA_ALG_ECDSA( psa_hash_alg ),
8132 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008133 continue;
8134
Neil Armstrong96eceb82022-06-30 18:05:05 +02008135 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008136 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8137 PSA_ALG_RSA_PKCS1V15_SIGN(
8138 psa_hash_alg ),
8139 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008140 continue;
8141 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008142#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008143
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008144 return( hash_alg_received );
8145 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008146 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008147
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008148 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008149}
8150
8151#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8152
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008153/* Serialization of TLS 1.2 sessions:
8154 *
8155 * struct {
8156 * uint64 start_time;
8157 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008158 * uint8 session_id_len; // at most 32
8159 * opaque session_id[32];
8160 * opaque master[48]; // fixed length in the standard
8161 * uint32 verify_result;
8162 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8163 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8164 * uint32 ticket_lifetime;
8165 * uint8 mfl_code; // up to 255 according to standard
8166 * uint8 encrypt_then_mac; // 0 or 1
8167 * } serialized_session_tls12;
8168 *
8169 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008170static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008171 unsigned char *buf,
8172 size_t buf_len )
8173{
8174 unsigned char *p = buf;
8175 size_t used = 0;
8176
8177#if defined(MBEDTLS_HAVE_TIME)
8178 uint64_t start;
8179#endif
8180#if defined(MBEDTLS_X509_CRT_PARSE_C)
8181#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8182 size_t cert_len;
8183#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8184#endif /* MBEDTLS_X509_CRT_PARSE_C */
8185
8186 /*
8187 * Time
8188 */
8189#if defined(MBEDTLS_HAVE_TIME)
8190 used += 8;
8191
8192 if( used <= buf_len )
8193 {
8194 start = (uint64_t) session->start;
8195
8196 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8197 p += 8;
8198 }
8199#endif /* MBEDTLS_HAVE_TIME */
8200
8201 /*
8202 * Basic mandatory fields
8203 */
8204 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008205 + 1 /* id_len */
8206 + sizeof( session->id )
8207 + sizeof( session->master )
8208 + 4; /* verify_result */
8209
8210 if( used <= buf_len )
8211 {
8212 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8213 p += 2;
8214
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008215 *p++ = MBEDTLS_BYTE_0( session->id_len );
8216 memcpy( p, session->id, 32 );
8217 p += 32;
8218
8219 memcpy( p, session->master, 48 );
8220 p += 48;
8221
8222 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8223 p += 4;
8224 }
8225
8226 /*
8227 * Peer's end-entity certificate
8228 */
8229#if defined(MBEDTLS_X509_CRT_PARSE_C)
8230#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8231 if( session->peer_cert == NULL )
8232 cert_len = 0;
8233 else
8234 cert_len = session->peer_cert->raw.len;
8235
8236 used += 3 + cert_len;
8237
8238 if( used <= buf_len )
8239 {
8240 *p++ = MBEDTLS_BYTE_2( cert_len );
8241 *p++ = MBEDTLS_BYTE_1( cert_len );
8242 *p++ = MBEDTLS_BYTE_0( cert_len );
8243
8244 if( session->peer_cert != NULL )
8245 {
8246 memcpy( p, session->peer_cert->raw.p, cert_len );
8247 p += cert_len;
8248 }
8249 }
8250#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8251 if( session->peer_cert_digest != NULL )
8252 {
8253 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8254 if( used <= buf_len )
8255 {
8256 *p++ = (unsigned char) session->peer_cert_digest_type;
8257 *p++ = (unsigned char) session->peer_cert_digest_len;
8258 memcpy( p, session->peer_cert_digest,
8259 session->peer_cert_digest_len );
8260 p += session->peer_cert_digest_len;
8261 }
8262 }
8263 else
8264 {
8265 used += 2;
8266 if( used <= buf_len )
8267 {
8268 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8269 *p++ = 0;
8270 }
8271 }
8272#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8273#endif /* MBEDTLS_X509_CRT_PARSE_C */
8274
8275 /*
8276 * Session ticket if any, plus associated data
8277 */
8278#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8279 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8280
8281 if( used <= buf_len )
8282 {
8283 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8284 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8285 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8286
8287 if( session->ticket != NULL )
8288 {
8289 memcpy( p, session->ticket, session->ticket_len );
8290 p += session->ticket_len;
8291 }
8292
8293 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8294 p += 4;
8295 }
8296#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8297
8298 /*
8299 * Misc extension-related info
8300 */
8301#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8302 used += 1;
8303
8304 if( used <= buf_len )
8305 *p++ = session->mfl_code;
8306#endif
8307
8308#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8309 used += 1;
8310
8311 if( used <= buf_len )
8312 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8313#endif
8314
8315 return( used );
8316}
8317
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008318MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008319static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008320 const unsigned char *buf,
8321 size_t len )
8322{
8323#if defined(MBEDTLS_HAVE_TIME)
8324 uint64_t start;
8325#endif
8326#if defined(MBEDTLS_X509_CRT_PARSE_C)
8327#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8328 size_t cert_len;
8329#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8330#endif /* MBEDTLS_X509_CRT_PARSE_C */
8331
8332 const unsigned char *p = buf;
8333 const unsigned char * const end = buf + len;
8334
8335 /*
8336 * Time
8337 */
8338#if defined(MBEDTLS_HAVE_TIME)
8339 if( 8 > (size_t)( end - p ) )
8340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8341
8342 start = ( (uint64_t) p[0] << 56 ) |
8343 ( (uint64_t) p[1] << 48 ) |
8344 ( (uint64_t) p[2] << 40 ) |
8345 ( (uint64_t) p[3] << 32 ) |
8346 ( (uint64_t) p[4] << 24 ) |
8347 ( (uint64_t) p[5] << 16 ) |
8348 ( (uint64_t) p[6] << 8 ) |
8349 ( (uint64_t) p[7] );
8350 p += 8;
8351
8352 session->start = (time_t) start;
8353#endif /* MBEDTLS_HAVE_TIME */
8354
8355 /*
8356 * Basic mandatory fields
8357 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008358 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8360
8361 session->ciphersuite = ( p[0] << 8 ) | p[1];
8362 p += 2;
8363
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008364 session->id_len = *p++;
8365 memcpy( session->id, p, 32 );
8366 p += 32;
8367
8368 memcpy( session->master, p, 48 );
8369 p += 48;
8370
8371 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8372 ( (uint32_t) p[1] << 16 ) |
8373 ( (uint32_t) p[2] << 8 ) |
8374 ( (uint32_t) p[3] );
8375 p += 4;
8376
8377 /* Immediately clear invalid pointer values that have been read, in case
8378 * we exit early before we replaced them with valid ones. */
8379#if defined(MBEDTLS_X509_CRT_PARSE_C)
8380#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8381 session->peer_cert = NULL;
8382#else
8383 session->peer_cert_digest = NULL;
8384#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8385#endif /* MBEDTLS_X509_CRT_PARSE_C */
8386#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8387 session->ticket = NULL;
8388#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8389
8390 /*
8391 * Peer certificate
8392 */
8393#if defined(MBEDTLS_X509_CRT_PARSE_C)
8394#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8395 /* Deserialize CRT from the end of the ticket. */
8396 if( 3 > (size_t)( end - p ) )
8397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8398
8399 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8400 p += 3;
8401
8402 if( cert_len != 0 )
8403 {
8404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8405
8406 if( cert_len > (size_t)( end - p ) )
8407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8408
8409 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8410
8411 if( session->peer_cert == NULL )
8412 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8413
8414 mbedtls_x509_crt_init( session->peer_cert );
8415
8416 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8417 p, cert_len ) ) != 0 )
8418 {
8419 mbedtls_x509_crt_free( session->peer_cert );
8420 mbedtls_free( session->peer_cert );
8421 session->peer_cert = NULL;
8422 return( ret );
8423 }
8424
8425 p += cert_len;
8426 }
8427#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8428 /* Deserialize CRT digest from the end of the ticket. */
8429 if( 2 > (size_t)( end - p ) )
8430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8431
8432 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8433 session->peer_cert_digest_len = (size_t) *p++;
8434
8435 if( session->peer_cert_digest_len != 0 )
8436 {
8437 const mbedtls_md_info_t *md_info =
8438 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8439 if( md_info == NULL )
8440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8441 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8443
8444 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8446
8447 session->peer_cert_digest =
8448 mbedtls_calloc( 1, session->peer_cert_digest_len );
8449 if( session->peer_cert_digest == NULL )
8450 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8451
8452 memcpy( session->peer_cert_digest, p,
8453 session->peer_cert_digest_len );
8454 p += session->peer_cert_digest_len;
8455 }
8456#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8457#endif /* MBEDTLS_X509_CRT_PARSE_C */
8458
8459 /*
8460 * Session ticket and associated data
8461 */
8462#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8463 if( 3 > (size_t)( end - p ) )
8464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8465
8466 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8467 p += 3;
8468
8469 if( session->ticket_len != 0 )
8470 {
8471 if( session->ticket_len > (size_t)( end - p ) )
8472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8473
8474 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8475 if( session->ticket == NULL )
8476 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8477
8478 memcpy( session->ticket, p, session->ticket_len );
8479 p += session->ticket_len;
8480 }
8481
8482 if( 4 > (size_t)( end - p ) )
8483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8484
8485 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8486 ( (uint32_t) p[1] << 16 ) |
8487 ( (uint32_t) p[2] << 8 ) |
8488 ( (uint32_t) p[3] );
8489 p += 4;
8490#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8491
8492 /*
8493 * Misc extension-related info
8494 */
8495#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8496 if( 1 > (size_t)( end - p ) )
8497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8498
8499 session->mfl_code = *p++;
8500#endif
8501
8502#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8503 if( 1 > (size_t)( end - p ) )
8504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8505
8506 session->encrypt_then_mac = *p++;
8507#endif
8508
8509 /* Done, should have consumed entire buffer */
8510 if( p != end )
8511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8512
8513 return( 0 );
8514}
Jerry Yudc7bd172022-02-17 13:44:15 +08008515#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8516
XiaokangQian75d40ef2022-04-20 11:05:24 +00008517int mbedtls_ssl_validate_ciphersuite(
8518 const mbedtls_ssl_context *ssl,
8519 const mbedtls_ssl_ciphersuite_t *suite_info,
8520 mbedtls_ssl_protocol_version min_tls_version,
8521 mbedtls_ssl_protocol_version max_tls_version )
8522{
8523 (void) ssl;
8524
8525 if( suite_info == NULL )
8526 return( -1 );
8527
8528 if( ( suite_info->min_tls_version > max_tls_version ) ||
8529 ( suite_info->max_tls_version < min_tls_version ) )
8530 {
8531 return( -1 );
8532 }
8533
XiaokangQian060d8672022-04-21 09:24:56 +00008534#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008535#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8536 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8537 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8538 {
8539 return( -1 );
8540 }
8541#endif
8542
8543 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8544#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8545 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8546 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8547 {
8548 return( -1 );
8549 }
8550#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8551#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8552
8553 return( 0 );
8554}
8555
XiaokangQianeaf36512022-04-24 09:07:44 +00008556#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8557/*
8558 * Function for writing a signature algorithm extension.
8559 *
8560 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8561 * value (TLS 1.3 RFC8446):
8562 * enum {
8563 * ....
8564 * ecdsa_secp256r1_sha256( 0x0403 ),
8565 * ecdsa_secp384r1_sha384( 0x0503 ),
8566 * ecdsa_secp521r1_sha512( 0x0603 ),
8567 * ....
8568 * } SignatureScheme;
8569 *
8570 * struct {
8571 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8572 * } SignatureSchemeList;
8573 *
8574 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8575 * value (TLS 1.2 RFC5246):
8576 * enum {
8577 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8578 * sha512(6), (255)
8579 * } HashAlgorithm;
8580 *
8581 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8582 * SignatureAlgorithm;
8583 *
8584 * struct {
8585 * HashAlgorithm hash;
8586 * SignatureAlgorithm signature;
8587 * } SignatureAndHashAlgorithm;
8588 *
8589 * SignatureAndHashAlgorithm
8590 * supported_signature_algorithms<2..2^16-2>;
8591 *
8592 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8593 * generalization of the TLS 1.2 signature algorithm extension.
8594 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8595 * `SignatureScheme` field of TLS 1.3
8596 *
8597 */
8598int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8599 const unsigned char *end, size_t *out_len )
8600{
8601 unsigned char *p = buf;
8602 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8603 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8604
8605 *out_len = 0;
8606
8607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8608
8609 /* Check if we have space for header and length field:
8610 * - extension_type (2 bytes)
8611 * - extension_data_length (2 bytes)
8612 * - supported_signature_algorithms_length (2 bytes)
8613 */
8614 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8615 p += 6;
8616
8617 /*
8618 * Write supported_signature_algorithms
8619 */
8620 supported_sig_alg = p;
8621 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8622 if( sig_alg == NULL )
8623 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8624
8625 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8626 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8628 *sig_alg,
8629 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008630 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8631 continue;
8632 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8633 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8634 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008636 *sig_alg,
8637 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008638 }
8639
8640 /* Length of supported_signature_algorithms */
8641 supported_sig_alg_len = p - supported_sig_alg;
8642 if( supported_sig_alg_len == 0 )
8643 {
8644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8645 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8646 }
8647
XiaokangQianeaf36512022-04-24 09:07:44 +00008648 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008649 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008650 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8651
XiaokangQianeaf36512022-04-24 09:07:44 +00008652 *out_len = p - buf;
8653
8654#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8655 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8656#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8657 return( 0 );
8658}
8659#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8660
XiaokangQian40a35232022-05-07 09:02:40 +00008661#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008662/*
8663 * mbedtls_ssl_parse_server_name_ext
8664 *
8665 * Structure of server_name extension:
8666 *
8667 * enum {
8668 * host_name(0), (255)
8669 * } NameType;
8670 * opaque HostName<1..2^16-1>;
8671 *
8672 * struct {
8673 * NameType name_type;
8674 * select (name_type) {
8675 * case host_name: HostName;
8676 * } name;
8677 * } ServerName;
8678 * struct {
8679 * ServerName server_name_list<1..2^16-1>
8680 * } ServerNameList;
8681 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008682MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008683int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8684 const unsigned char *buf,
8685 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008686{
8687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8688 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008689 size_t server_name_list_len, hostname_len;
8690 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008691
XiaokangQianf2a94202022-05-20 06:44:24 +00008692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008693
8694 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008695 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008696 p += 2;
8697
XiaokangQian9b2b7712022-05-17 02:57:00 +00008698 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8699 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008700 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008701 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008702 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008703 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008704 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8705 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008706
8707 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8708 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008709 /* sni_name is intended to be used only during the parsing of the
8710 * ClientHello message (it is reset to NULL before the end of
8711 * the message parsing). Thus it is ok to just point to the
8712 * reception buffer and not make a copy of it.
8713 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008714 ssl->handshake->sni_name = p + 3;
8715 ssl->handshake->sni_name_len = hostname_len;
8716 if( ssl->conf->f_sni == NULL )
8717 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008718 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008719 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008720 if( ret != 0 )
8721 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008722 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008723 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8724 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008725 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8726 }
8727 return( 0 );
8728 }
8729
8730 p += hostname_len + 3;
8731 }
8732
8733 return( 0 );
8734}
8735#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8736
XiaokangQianacb39922022-06-17 10:18:48 +00008737#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008738MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008739int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8740 const unsigned char *buf,
8741 const unsigned char *end )
8742{
8743 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008744 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008745 const unsigned char *protocol_name_list;
8746 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008747 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008748
8749 /* If ALPN not configured, just ignore the extension */
8750 if( ssl->conf->alpn_list == NULL )
8751 return( 0 );
8752
8753 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008754 * RFC7301, section 3.1
8755 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008756 *
XiaokangQianc7403452022-06-23 03:24:12 +00008757 * struct {
8758 * ProtocolName protocol_name_list<2..2^16-1>
8759 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008760 */
8761
XiaokangQianc7403452022-06-23 03:24:12 +00008762 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008763 * protocol_name_list_len 2 bytes
8764 * protocol_name_len 1 bytes
8765 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008766 */
XiaokangQianacb39922022-06-17 10:18:48 +00008767 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8768
XiaokangQianc7403452022-06-23 03:24:12 +00008769 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008770 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008771 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008772 protocol_name_list = p;
8773 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008774
8775 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008776 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008777 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008778 protocol_name_len = *p++;
8779 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8780 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008781 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008782 {
8783 MBEDTLS_SSL_PEND_FATAL_ALERT(
8784 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8785 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008786 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008787 }
8788
8789 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008790 }
8791
8792 /* Use our order of preference */
8793 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8794 {
8795 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008796 p = protocol_name_list;
8797 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008798 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008799 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008800 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008801 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008802 {
8803 ssl->alpn_chosen = *alpn;
8804 return( 0 );
8805 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008806
8807 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008808 }
8809 }
8810
XiaokangQian95d5f542022-06-24 02:29:26 +00008811 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008812 MBEDTLS_SSL_PEND_FATAL_ALERT(
8813 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8814 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8815 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8816}
8817
8818int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8819 unsigned char *buf,
8820 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008821 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008822{
8823 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008824 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008825 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008826
8827 if( ssl->alpn_chosen == NULL )
8828 {
8829 return( 0 );
8830 }
8831
XiaokangQian95d5f542022-06-24 02:29:26 +00008832 protocol_name_len = strlen( ssl->alpn_chosen );
8833 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008834
8835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8836 /*
8837 * 0 . 1 ext identifier
8838 * 2 . 3 ext length
8839 * 4 . 5 protocol list length
8840 * 6 . 6 protocol name length
8841 * 7 . 7+n protocol name
8842 */
8843 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8844
XiaokangQian95d5f542022-06-24 02:29:26 +00008845 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008846
XiaokangQian95d5f542022-06-24 02:29:26 +00008847 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8848 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008849 /* Note: the length of the chosen protocol has been checked to be less
8850 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8851 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008852 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008853
XiaokangQian95d5f542022-06-24 02:29:26 +00008854 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008855 return ( 0 );
8856}
8857#endif /* MBEDTLS_SSL_ALPN */
8858
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008859#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008860 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008861 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008862 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008863int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8864 const char *hostname )
8865{
8866 /* Initialize to suppress unnecessary compiler warning */
8867 size_t hostname_len = 0;
8868
8869 /* Check if new hostname is valid before
8870 * making any change to current one */
8871 if( hostname != NULL )
8872 {
8873 hostname_len = strlen( hostname );
8874
8875 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8877 }
8878
8879 /* Now it's clear that we will overwrite the old hostname,
8880 * so we can free it safely */
8881 if( session->hostname != NULL )
8882 {
8883 mbedtls_platform_zeroize( session->hostname,
8884 strlen( session->hostname ) );
8885 mbedtls_free( session->hostname );
8886 }
8887
8888 /* Passing NULL as hostname shall clear the old one */
8889 if( hostname == NULL )
8890 {
8891 session->hostname = NULL;
8892 }
8893 else
8894 {
8895 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8896 if( session->hostname == NULL )
8897 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8898
8899 memcpy( session->hostname, hostname, hostname_len );
8900 }
8901
8902 return( 0 );
8903}
Xiaokang Qian03409292022-10-12 02:49:52 +00008904#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8905 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008906 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008907 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909#endif /* MBEDTLS_SSL_TLS_C */