blob: 20648a16673c1afa85cb711252e6872b84b00753 [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#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yud25cab02022-10-31 12:48:30 +0800619#if defined(MBEDTLS_DEBUG_C)
620static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800621 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800622 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
623 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
624 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
625 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
626 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
627 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
628 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
629 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
630 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
631 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
632 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
633 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
634 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
635 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
636 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
637 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
638 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
639 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
640 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
641 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
642 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
643 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
644 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
646 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
647 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
648 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
649};
650
Jerry Yuea52ed92022-11-08 21:01:17 +0800651static unsigned int extension_type_table[]={
Jerry Yud25cab02022-10-31 12:48:30 +0800652 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
653 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
654 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
655 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
657 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
658 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
659 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
660 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
661 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
662 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
664 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
665 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
666 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
668 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
669 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
670 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
671 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
672 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
673 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
674 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
675 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
676 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
677 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
678 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
679 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
680};
681
682const char *mbedtls_ssl_get_extension_name( unsigned int extension_type )
683{
684 return( extension_name_table[
685 mbedtls_ssl_get_extension_id( extension_type ) ] );
686}
687
688static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type )
689{
690 switch( hs_msg_type )
691 {
692 case MBEDTLS_SSL_HS_CLIENT_HELLO:
693 return( "ClientHello" );
694 case MBEDTLS_SSL_HS_SERVER_HELLO:
695 return( "ServerHello" );
696 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
697 return( "HelloRetryRequest" );
698 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
699 return( "NewSessionTicket" );
700 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
701 return( "EncryptedExtensions" );
702 case MBEDTLS_SSL_HS_CERTIFICATE:
703 return( "Certificate" );
704 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
705 return( "CertificateRequest" );
706 }
Jerry Yu79aa7212022-11-08 21:30:21 +0800707 return( "Unknown" );
Jerry Yud25cab02022-10-31 12:48:30 +0800708}
709
Jerry Yu79aa7212022-11-08 21:30:21 +0800710void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type, unsigned int extension_type,
713 const char *extra_msg0, const char *extra_msg1 )
Jerry Yud25cab02022-10-31 12:48:30 +0800714{
715 const char *extra_msg;
716 if( extra_msg0 && extra_msg1 )
717 {
718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
720 "%s: %s(%u) extension %s %s.",
721 ssl_tls13_get_hs_msg_name( hs_msg_type ),
722 mbedtls_ssl_get_extension_name( extension_type ),
723 extension_type,
724 extra_msg0, extra_msg1 );
725 return;
726 }
727
728 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
729 if( extra_msg )
730 {
731 mbedtls_debug_print_msg(
732 ssl, level, file, line,
733 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
734 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
735 extra_msg );
736 return;
737 }
738
739 mbedtls_debug_print_msg(
740 ssl, level, file, line,
741 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
742 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
743}
744
745void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
746 int level, const char *file, int line,
747 int hs_msg_type, uint32_t extensions_mask,
748 const char *extra )
749{
750
751 for( unsigned i = 0;
752 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
753 i++ )
754 {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yu97be6a92022-11-09 22:43:31 +0800757 extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra );
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
761#endif /* MBEDTLS_DEBUG_C */
762
Jerry Yuc73c6182022-02-08 20:29:25 +0800763void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
765{
766 ((void) ciphersuite_info);
767
Andrzej Kurek25f27152022-08-17 16:09:31 -0400768#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800769 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
770 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
771 else
772#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400773#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
775 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
776 else
777#endif
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
780 return;
781 }
782}
783
XiaokangQianadab9a62022-07-18 07:41:26 +0000784void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
785 unsigned hs_type,
786 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100787{
788 unsigned char hs_hdr[4];
789
790 /* Build HS header for checksum update. */
791 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
792 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
793 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
794 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
795
796 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
797}
798
799void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
800 unsigned hs_type,
801 unsigned char const *msg,
802 size_t msg_len )
803{
804 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
805 ssl->handshake->update_checksum( ssl, msg, msg_len );
806}
807
Jerry Yuc73c6182022-02-08 20:29:25 +0800808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
809{
810 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#if defined(MBEDTLS_USE_PSA_CRYPTO)
813 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
814 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
815#else
816 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
817#endif
818#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400819#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
822 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
823#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400824 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#endif
826#endif
827}
828
829static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
830 const unsigned char *buf, size_t len )
831{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
834 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
835#else
836 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
837#endif
838#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
842#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400843 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400846#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
847 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
848 (void) ssl;
849 (void) buf;
850 (void) len;
851#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800852}
853
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
856 const unsigned char *buf, size_t len )
857{
858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
860#else
861 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
862#endif
863}
864#endif
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
868 const unsigned char *buf, size_t len )
869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
871 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
872#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400873 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875}
876#endif
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881
Andrzej Kurek25f27152022-08-17 16:09:31 -0400882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884 handshake->fin_sha256_psa = psa_hash_operation_init();
885 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200888 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500893 handshake->fin_sha384_psa = psa_hash_operation_init();
894 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400896 mbedtls_sha512_init( &handshake->fin_sha384 );
897 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900
901 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_DHM_C)
904 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200906#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200910 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200911#if defined(MBEDTLS_SSL_CLI_C)
912 handshake->ecjpake_cache = NULL;
913 handshake->ecjpake_cache_len = 0;
914#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200915#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200916
Gilles Peskineeccd8882020-03-10 12:19:08 +0100917#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200918 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200919#endif
920
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200921#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
922 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
923#endif
Hanno Becker75173122019-02-06 16:18:31 +0000924
925#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
926 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
927 mbedtls_pk_init( &handshake->peer_pubkey );
928#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200929}
930
Hanno Beckera18d1322018-01-03 14:27:32 +0000931void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200934
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100935#if defined(MBEDTLS_USE_PSA_CRYPTO)
936 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
937 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100938#else
939 mbedtls_cipher_init( &transform->cipher_ctx_enc );
940 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100941#endif
942
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000943#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100944#if defined(MBEDTLS_USE_PSA_CRYPTO)
945 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
946 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100947#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_md_init( &transform->md_ctx_enc );
949 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000950#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100951#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200952}
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957}
958
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200959MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000961{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000963 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200968 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969
970 /*
971 * Either the pointers are now NULL or cleared properly and can be freed.
972 * Now allocate missing structures.
973 */
974 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200975 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200976 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200977 }
Paul Bakker48916f92012-09-16 19:57:18 +0000978
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200981 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakker82788fb2014-10-20 13:59:19 +0200984 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500988#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
989 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400990
Andrzej Kurek4a063792020-10-21 15:08:44 +0200991 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
992 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500993#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000994
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200995 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000996 if( ssl->handshake == NULL ||
997 ssl->transform_negotiate == NULL ||
998 ssl->session_negotiate == NULL )
999 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_free( ssl->handshake );
1003 mbedtls_free( ssl->transform_negotiate );
1004 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001005
1006 ssl->handshake = NULL;
1007 ssl->transform_negotiate = NULL;
1008 ssl->session_negotiate = NULL;
1009
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001010 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001011 }
1012
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001013 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001015 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001016 ssl_handshake_params_init( ssl->handshake );
1017
Jerry Yud0766ec2022-09-22 10:46:57 +08001018#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1019 defined(MBEDTLS_SSL_SRV_C) && \
1020 defined(MBEDTLS_SSL_SESSION_TICKETS)
1021 ssl->handshake->new_session_tickets_count =
1022 ssl->conf->new_session_tickets_count ;
1023#endif
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1027 {
1028 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001029
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001030 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1031 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1032 else
1033 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001034
Hanno Becker0f57a652020-02-05 10:37:26 +00001035 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001036 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001037#endif
1038
Brett Warrene0edc842021-08-17 09:53:13 +01001039/*
1040 * curve_list is translated to IANA TLS group identifiers here because
1041 * mbedtls_ssl_conf_curves returns void and so can't return
1042 * any error codes.
1043 */
1044#if defined(MBEDTLS_ECP_C)
1045#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1046 /* Heap allocate and translate curve_list from internal to IANA group ids */
1047 if ( ssl->conf->curve_list != NULL )
1048 {
1049 size_t length;
1050 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1051
1052 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1053 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1054
1055 /* Leave room for zero termination */
1056 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1057 if ( group_list == NULL )
1058 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1059
1060 for( size_t i = 0; i < length; i++ )
1061 {
1062 const mbedtls_ecp_curve_info *info =
1063 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1064 if ( info == NULL )
1065 {
1066 mbedtls_free( group_list );
1067 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1068 }
1069 group_list[i] = info->tls_id;
1070 }
1071
1072 group_list[length] = 0;
1073
1074 ssl->handshake->group_list = group_list;
1075 ssl->handshake->group_list_heap_allocated = 1;
1076 }
1077 else
1078 {
1079 ssl->handshake->group_list = ssl->conf->group_list;
1080 ssl->handshake->group_list_heap_allocated = 0;
1081 }
1082#endif /* MBEDTLS_DEPRECATED_REMOVED */
1083#endif /* MBEDTLS_ECP_C */
1084
Ronald Crone68ab4f2022-10-05 12:46:29 +02001085#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001086#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001087#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001088 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1089 signature algorithms IANA identifiers. */
1090 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001091 ssl->conf->sig_hashes != NULL )
1092 {
1093 const int *md;
1094 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001095 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001096 uint16_t *p;
1097
Jerry Yub476a442022-01-21 18:14:45 +08001098#if defined(static_assert)
1099 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1100 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1101 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001102#endif
1103
Jerry Yuf017ee42022-01-12 15:49:48 +08001104 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1105 {
1106 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1107 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001108#if defined(MBEDTLS_ECDSA_C)
1109 sig_algs_len += sizeof( uint16_t );
1110#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001111
Jerry Yub476a442022-01-21 18:14:45 +08001112#if defined(MBEDTLS_RSA_C)
1113 sig_algs_len += sizeof( uint16_t );
1114#endif
1115 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1116 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001117 }
1118
Jerry Yub476a442022-01-21 18:14:45 +08001119 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001120 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1121
Jerry Yub476a442022-01-21 18:14:45 +08001122 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1123 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001124 if( ssl->handshake->sig_algs == NULL )
1125 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1126
1127 p = (uint16_t *)ssl->handshake->sig_algs;
1128 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1129 {
1130 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1131 if( hash == MBEDTLS_SSL_HASH_NONE )
1132 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001133#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001134 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1135 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001136#endif
1137#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001138 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1139 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001140#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001141 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001142 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 ssl->handshake->sig_algs_heap_allocated = 1;
1144 }
1145 else
Jerry Yua69269a2022-01-17 21:06:01 +08001146#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001147 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001148 ssl->handshake->sig_algs_heap_allocated = 0;
1149 }
Jerry Yucc539102022-06-27 16:27:35 +08001150#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001151#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001152 return( 0 );
1153}
1154
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001155#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001156/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001157MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001158static int ssl_cookie_write_dummy( void *ctx,
1159 unsigned char **p, unsigned char *end,
1160 const unsigned char *cli_id, size_t cli_id_len )
1161{
1162 ((void) ctx);
1163 ((void) p);
1164 ((void) end);
1165 ((void) cli_id);
1166 ((void) cli_id_len);
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001169}
1170
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001171MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001172static int ssl_cookie_check_dummy( void *ctx,
1173 const unsigned char *cookie, size_t cookie_len,
1174 const unsigned char *cli_id, size_t cli_id_len )
1175{
1176 ((void) ctx);
1177 ((void) cookie);
1178 ((void) cookie_len);
1179 ((void) cli_id);
1180 ((void) cli_id_len);
1181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001183}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001184#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001185
Paul Bakker5121ce52009-01-03 21:22:43 +00001186/*
1187 * Initialize an SSL context
1188 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001189void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1190{
1191 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1192}
1193
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001194MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001195static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1196{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001197 const mbedtls_ssl_config *conf = ssl->conf;
1198
Ronald Cron6f135e12021-12-08 16:57:54 +01001199#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001200 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1201 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001202 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1203 {
1204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1205 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1206 }
1207
Jerry Yu60835a82021-08-04 10:13:52 +08001208 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1209 return( 0 );
1210 }
1211#endif
1212
1213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001214 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001215 {
1216 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1217 return( 0 );
1218 }
1219#endif
1220
Ronald Cron6f135e12021-12-08 16:57:54 +01001221#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001222 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001223 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1225 {
1226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1227 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1228 }
1229
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001230 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1231 {
1232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1233 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1234 }
1235
1236
Ronald Crone1d3f062022-02-10 14:50:54 +01001237 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1238 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001239 }
1240#endif
1241
1242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1243 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1244}
1245
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001246MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001247static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1248{
1249 int ret;
1250 ret = ssl_conf_version_check( ssl );
1251 if( ret != 0 )
1252 return( ret );
1253
1254 /* Space for further checks */
1255
1256 return( 0 );
1257}
1258
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001259/*
1260 * Setup an SSL context
1261 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001262
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001263int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001264 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001265{
Janos Follath865b3eb2019-12-16 11:46:15 +00001266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001267 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1268 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001270 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001271
Jerry Yu60835a82021-08-04 10:13:52 +08001272 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1273 return( ret );
1274
Paul Bakker62f2dee2012-09-28 07:31:51 +00001275 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001276 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001277 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001278
1279 /* Set to NULL in case of an error condition */
1280 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001281
Darryl Greenb33cc762019-11-28 14:29:44 +00001282#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1283 ssl->in_buf_len = in_buf_len;
1284#endif
1285 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001286 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001289 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001290 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001291 }
1292
Darryl Greenb33cc762019-11-28 14:29:44 +00001293#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1294 ssl->out_buf_len = out_buf_len;
1295#endif
1296 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001297 if( ssl->out_buf == NULL )
1298 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001300 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001301 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 }
1303
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001304 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001305
Johan Pascalb62bb512015-12-03 21:56:45 +01001306#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001307 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001308#endif
1309
Paul Bakker48916f92012-09-16 19:57:18 +00001310 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001311 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
1313 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001314
1315error:
1316 mbedtls_free( ssl->in_buf );
1317 mbedtls_free( ssl->out_buf );
1318
1319 ssl->conf = NULL;
1320
Darryl Greenb33cc762019-11-28 14:29:44 +00001321#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1322 ssl->in_buf_len = 0;
1323 ssl->out_buf_len = 0;
1324#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001325 ssl->in_buf = NULL;
1326 ssl->out_buf = NULL;
1327
1328 ssl->in_hdr = NULL;
1329 ssl->in_ctr = NULL;
1330 ssl->in_len = NULL;
1331 ssl->in_iv = NULL;
1332 ssl->in_msg = NULL;
1333
1334 ssl->out_hdr = NULL;
1335 ssl->out_ctr = NULL;
1336 ssl->out_len = NULL;
1337 ssl->out_iv = NULL;
1338 ssl->out_msg = NULL;
1339
k-stachowiak9f7798e2018-07-31 16:52:32 +02001340 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341}
1342
1343/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001344 * Reset an initialized and used SSL context for re-use while retaining
1345 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001346 *
1347 * If partial is non-zero, keep data in the input buffer and client ID.
1348 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001349 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001350void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1351 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001352{
Darryl Greenb33cc762019-11-28 14:29:44 +00001353#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1354 size_t in_buf_len = ssl->in_buf_len;
1355 size_t out_buf_len = ssl->out_buf_len;
1356#else
1357 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1358 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1359#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001360
Hanno Beckerb0302c42021-08-03 09:39:42 +01001361#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1362 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001363#endif
1364
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001365 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001366 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001367
Hanno Beckerb0302c42021-08-03 09:39:42 +01001368 mbedtls_ssl_reset_in_out_pointers( ssl );
1369
1370 /* Reset incoming message parsing */
1371 ssl->in_offt = NULL;
1372 ssl->nb_zero = 0;
1373 ssl->in_msgtype = 0;
1374 ssl->in_msglen = 0;
1375 ssl->in_hslen = 0;
1376 ssl->keep_current_message = 0;
1377 ssl->transform_in = NULL;
1378
1379#if defined(MBEDTLS_SSL_PROTO_DTLS)
1380 ssl->next_record_offset = 0;
1381 ssl->in_epoch = 0;
1382#endif
1383
1384 /* Keep current datagram if partial == 1 */
1385 if( partial == 0 )
1386 {
1387 ssl->in_left = 0;
1388 memset( ssl->in_buf, 0, in_buf_len );
1389 }
1390
Ronald Cronad8c17b2022-06-10 17:18:09 +02001391 ssl->send_alert = 0;
1392
Hanno Beckerb0302c42021-08-03 09:39:42 +01001393 /* Reset outgoing message writing */
1394 ssl->out_msgtype = 0;
1395 ssl->out_msglen = 0;
1396 ssl->out_left = 0;
1397 memset( ssl->out_buf, 0, out_buf_len );
1398 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1399 ssl->transform_out = NULL;
1400
1401#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1402 mbedtls_ssl_dtls_replay_reset( ssl );
1403#endif
1404
XiaokangQian2b01dc32022-01-21 02:53:13 +00001405#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001406 if( ssl->transform )
1407 {
1408 mbedtls_ssl_transform_free( ssl->transform );
1409 mbedtls_free( ssl->transform );
1410 ssl->transform = NULL;
1411 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001412#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1413
XiaokangQian2b01dc32022-01-21 02:53:13 +00001414#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1415 mbedtls_ssl_transform_free( ssl->transform_application );
1416 mbedtls_free( ssl->transform_application );
1417 ssl->transform_application = NULL;
1418
1419 if( ssl->handshake != NULL )
1420 {
1421 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1422 mbedtls_free( ssl->handshake->transform_earlydata );
1423 ssl->handshake->transform_earlydata = NULL;
1424
1425 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1426 mbedtls_free( ssl->handshake->transform_handshake );
1427 ssl->handshake->transform_handshake = NULL;
1428 }
1429
XiaokangQian2b01dc32022-01-21 02:53:13 +00001430#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001431}
1432
1433int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1434{
1435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1436
1437 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1438
XiaokangQian78b1fa72022-01-19 06:56:30 +00001439 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001440
1441 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_SSL_RENEGOTIATION)
1443 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001444 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001445
1446 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1448 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001449#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001451
Hanno Beckerb0302c42021-08-03 09:39:42 +01001452 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001453 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001454 if( ssl->session )
1455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_ssl_session_free( ssl->session );
1457 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001458 ssl->session = NULL;
1459 }
1460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001462 ssl->alpn_chosen = NULL;
1463#endif
1464
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001465#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001466 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001467#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001468 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001469#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001470 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001471 {
1472 mbedtls_free( ssl->cli_id );
1473 ssl->cli_id = NULL;
1474 ssl->cli_id_len = 0;
1475 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001476#endif
1477
Paul Bakker48916f92012-09-16 19:57:18 +00001478 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1479 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001480
1481 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001482}
1483
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001484/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001485 * Reset an initialized and used SSL context for re-use while retaining
1486 * all application-set variables, function pointers and data.
1487 */
1488int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1489{
Hanno Becker43aefe22020-02-05 10:44:56 +00001490 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001491}
1492
1493/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 * SSL set accessors
1495 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001496void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001498 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001499}
1500
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001501void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001502{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001503 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001504}
1505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001507void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001508{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001509 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001510}
1511#endif
1512
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001513void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001514{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001515 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001516}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001519
Hanno Becker1841b0a2018-08-24 11:13:57 +01001520void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1521 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001522{
1523 ssl->disable_datagram_packing = !allow_packing;
1524}
1525
1526void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1527 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001529 conf->hs_timeout_min = min;
1530 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001531}
1532#endif
1533
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001534void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001535{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001536 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001537}
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001540void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001541 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001542 void *p_vrfy )
1543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001544 conf->f_vrfy = f_vrfy;
1545 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001548
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001549void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001550 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 void *p_rng )
1552{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001553 conf->f_rng = f_rng;
1554 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001555}
1556
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001557void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001558 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 void *p_dbg )
1560{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001561 conf->f_dbg = f_dbg;
1562 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001563}
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001566 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001567 mbedtls_ssl_send_t *f_send,
1568 mbedtls_ssl_recv_t *f_recv,
1569 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001570{
1571 ssl->p_bio = p_bio;
1572 ssl->f_send = f_send;
1573 ssl->f_recv = f_recv;
1574 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001575}
1576
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001577#if defined(MBEDTLS_SSL_PROTO_DTLS)
1578void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1579{
1580 ssl->mtu = mtu;
1581}
1582#endif
1583
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001584void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001585{
1586 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001587}
1588
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001589void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1590 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001591 mbedtls_ssl_set_timer_t *f_set_timer,
1592 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001593{
1594 ssl->p_timer = p_timer;
1595 ssl->f_set_timer = f_set_timer;
1596 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001597
1598 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001599 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001600}
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001603void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001604 void *p_cache,
1605 mbedtls_ssl_cache_get_t *f_get_cache,
1606 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001607{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001608 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001609 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001610 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#if defined(MBEDTLS_SSL_CLI_C)
1615int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616{
Janos Follath865b3eb2019-12-16 11:46:15 +00001617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001618
1619 if( ssl == NULL ||
1620 session == NULL ||
1621 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001622 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001625 }
1626
Hanno Beckere810bbc2021-05-14 16:01:05 +01001627 if( ssl->handshake->resume == 1 )
1628 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1629
Jerry Yu21092062022-10-10 21:21:31 +08001630#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1631 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001632 {
Jerry Yu21092062022-10-10 21:21:31 +08001633 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1634 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1635
1636 if( mbedtls_ssl_validate_ciphersuite(
1637 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1638 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1639 {
1640 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1641 session->ciphersuite ) );
1642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1643 }
Jerry Yu40afab62022-10-08 10:42:13 +08001644 }
Jerry Yu21092062022-10-10 21:21:31 +08001645#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001646
Hanno Becker52055ae2019-02-06 14:30:46 +00001647 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1648 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001649 return( ret );
1650
Paul Bakker0a597072012-09-25 21:55:46 +00001651 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001652
1653 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001657void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1658 const int *ciphersuites )
1659{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001660 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661}
1662
Ronald Cron6f135e12021-12-08 16:57:54 +01001663#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001664void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001665 const int kex_modes )
1666{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001667 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001668}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001669
1670#if defined(MBEDTLS_SSL_EARLY_DATA)
1671void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001672 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001673{
1674 conf->early_data_enabled = early_data_enabled;
1675}
1676#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001677#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001680void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001681 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001682{
1683 conf->cert_profile = profile;
1684}
1685
Glenn Strauss36872db2022-01-22 05:06:31 -05001686static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1687{
1688 mbedtls_ssl_key_cert *cur = key_cert, *next;
1689
1690 while( cur != NULL )
1691 {
1692 next = cur->next;
1693 mbedtls_free( cur );
1694 cur = next;
1695 }
1696}
1697
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001698/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001699MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001700static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1701 mbedtls_x509_crt *cert,
1702 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001703{
niisato8ee24222018-06-25 19:05:48 +09001704 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001705
Glenn Strauss36872db2022-01-22 05:06:31 -05001706 if( cert == NULL )
1707 {
1708 /* Free list if cert is null */
1709 ssl_key_cert_free( *head );
1710 *head = NULL;
1711 return( 0 );
1712 }
1713
niisato8ee24222018-06-25 19:05:48 +09001714 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1715 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001716 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001717
niisato8ee24222018-06-25 19:05:48 +09001718 new_cert->cert = cert;
1719 new_cert->key = key;
1720 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001721
Glenn Strauss36872db2022-01-22 05:06:31 -05001722 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001723 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001724 {
niisato8ee24222018-06-25 19:05:48 +09001725 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001726 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001727 else
1728 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001729 mbedtls_ssl_key_cert *cur = *head;
1730 while( cur->next != NULL )
1731 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001732 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001733 }
1734
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001735 return( 0 );
1736}
1737
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001738int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001739 mbedtls_x509_crt *own_cert,
1740 mbedtls_pk_context *pk_key )
1741{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001742 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001743}
1744
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001745void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001746 mbedtls_x509_crt *ca_chain,
1747 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001748{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001749 conf->ca_chain = ca_chain;
1750 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001751
1752#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1753 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1754 * cannot be used together. */
1755 conf->f_ca_cb = NULL;
1756 conf->p_ca_cb = NULL;
1757#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001758}
Hanno Becker5adaad92019-03-27 16:54:37 +00001759
1760#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1761void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001762 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001763 void *p_ca_cb )
1764{
1765 conf->f_ca_cb = f_ca_cb;
1766 conf->p_ca_cb = p_ca_cb;
1767
1768 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1769 * cannot be used together. */
1770 conf->ca_chain = NULL;
1771 conf->ca_crl = NULL;
1772}
1773#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001775
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001776#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001777const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1778 size_t *name_len )
1779{
1780 *name_len = ssl->handshake->sni_name_len;
1781 return( ssl->handshake->sni_name );
1782}
1783
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001784int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1785 mbedtls_x509_crt *own_cert,
1786 mbedtls_pk_context *pk_key )
1787{
1788 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1789 own_cert, pk_key ) );
1790}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001791
1792void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1793 mbedtls_x509_crt *ca_chain,
1794 mbedtls_x509_crl *ca_crl )
1795{
1796 ssl->handshake->sni_ca_chain = ca_chain;
1797 ssl->handshake->sni_ca_crl = ca_crl;
1798}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001799
Glenn Strauss999ef702022-03-11 01:37:23 -05001800#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1801void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1802 const mbedtls_x509_crt *crt)
1803{
1804 ssl->handshake->dn_hints = crt;
1805}
1806#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1807
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001808void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1809 int authmode )
1810{
1811 ssl->handshake->sni_authmode = authmode;
1812}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001813#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1814
Hanno Becker8927c832019-04-03 12:52:50 +01001815#if defined(MBEDTLS_X509_CRT_PARSE_C)
1816void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1817 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1818 void *p_vrfy )
1819{
1820 ssl->f_vrfy = f_vrfy;
1821 ssl->p_vrfy = p_vrfy;
1822}
1823#endif
1824
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001825#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001826/*
1827 * Set EC J-PAKE password for current handshake
1828 */
1829int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1830 const unsigned char *pw,
1831 size_t pw_len )
1832{
1833 mbedtls_ecjpake_role role;
1834
Janos Follath8eb64132016-06-03 15:40:57 +01001835 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1837
1838 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1839 role = MBEDTLS_ECJPAKE_SERVER;
1840 else
1841 role = MBEDTLS_ECJPAKE_CLIENT;
1842
1843 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1844 role,
1845 MBEDTLS_MD_SHA256,
1846 MBEDTLS_ECP_DP_SECP256R1,
1847 pw, pw_len ) );
1848}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001849#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001850
Ronald Cron73fe8df2022-10-05 14:31:43 +02001851#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001852int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001853{
Ronald Crond29e13e2022-10-19 10:33:48 +02001854 if( conf->psk_identity == NULL ||
1855 conf->psk_identity_len == 0 )
1856 {
1857 return( 0 );
1858 }
1859
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001860#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001861 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001862 return( 1 );
1863#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001864
1865 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001866 return( 1 );
1867
1868 return( 0 );
1869}
1870
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001871static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1872{
1873 /* Remove reference to existing PSK, if any. */
1874#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001875 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001876 {
1877 /* The maintenance of the PSK key slot is the
1878 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001879 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001880 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001881#endif /* MBEDTLS_USE_PSA_CRYPTO */
1882 if( conf->psk != NULL )
1883 {
1884 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1885
1886 mbedtls_free( conf->psk );
1887 conf->psk = NULL;
1888 conf->psk_len = 0;
1889 }
1890
1891 /* Remove reference to PSK identity, if any. */
1892 if( conf->psk_identity != NULL )
1893 {
1894 mbedtls_free( conf->psk_identity );
1895 conf->psk_identity = NULL;
1896 conf->psk_identity_len = 0;
1897 }
1898}
1899
Hanno Becker7390c712018-11-15 13:33:04 +00001900/* This function assumes that PSK identity in the SSL config is unset.
1901 * It checks that the provided identity is well-formed and attempts
1902 * to make a copy of it in the SSL config.
1903 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001904MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001905static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1906 unsigned char const *psk_identity,
1907 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001908{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001909 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001910 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001911 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001912 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001913 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001914 {
1915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1916 }
1917
Hanno Becker7390c712018-11-15 13:33:04 +00001918 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1919 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001920 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001921
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001922 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001923 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001924
1925 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001926}
1927
Hanno Becker7390c712018-11-15 13:33:04 +00001928int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1929 const unsigned char *psk, size_t psk_len,
1930 const unsigned char *psk_identity, size_t psk_identity_len )
1931{
Janos Follath865b3eb2019-12-16 11:46:15 +00001932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001933
1934 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001935 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001936 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001937
1938 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001939 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001941 if( psk_len == 0 )
1942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1943 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1945
Hanno Becker7390c712018-11-15 13:33:04 +00001946 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1947 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1948 conf->psk_len = psk_len;
1949 memcpy( conf->psk, psk, conf->psk_len );
1950
1951 /* Check and set PSK Identity */
1952 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1953 if( ret != 0 )
1954 ssl_conf_remove_psk( conf );
1955
1956 return( ret );
1957}
1958
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001959static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1960{
1961#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001962 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001963 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001964 /* The maintenance of the external PSK key slot is the
1965 * user's responsibility. */
1966 if( ssl->handshake->psk_opaque_is_internal )
1967 {
1968 psa_destroy_key( ssl->handshake->psk_opaque );
1969 ssl->handshake->psk_opaque_is_internal = 0;
1970 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001971 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001972 }
Neil Armstronge952a302022-05-03 10:22:14 +02001973#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001974 if( ssl->handshake->psk != NULL )
1975 {
1976 mbedtls_platform_zeroize( ssl->handshake->psk,
1977 ssl->handshake->psk_len );
1978 mbedtls_free( ssl->handshake->psk );
1979 ssl->handshake->psk_len = 0;
1980 }
Neil Armstronge952a302022-05-03 10:22:14 +02001981#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001982}
1983
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001984int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1985 const unsigned char *psk, size_t psk_len )
1986{
Neil Armstrong501c9322022-05-03 09:35:09 +02001987#if defined(MBEDTLS_USE_PSA_CRYPTO)
1988 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001989 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001990 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001992#endif /* MBEDTLS_USE_PSA_CRYPTO */
1993
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001994 if( psk == NULL || ssl->handshake == NULL )
1995 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1996
1997 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1999
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002000 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002001
Neil Armstrong501c9322022-05-03 09:35:09 +02002002#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002003#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2004 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2005 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002006 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002007 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2008 else
2009 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2010 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2011 }
2012#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002013
Jerry Yu568ec252022-07-22 21:27:34 +08002014#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002015 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2016 {
2017 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2018 psa_set_key_usage_flags( &key_attributes,
2019 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2020 }
2021#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2022
Neil Armstrong501c9322022-05-03 09:35:09 +02002023 psa_set_key_algorithm( &key_attributes, alg );
2024 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2025
2026 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2027 if( status != PSA_SUCCESS )
2028 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2029
2030 /* Allow calling psa_destroy_key() on psk remove */
2031 ssl->handshake->psk_opaque_is_internal = 1;
2032 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2033#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002034 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002035 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002036
2037 ssl->handshake->psk_len = psk_len;
2038 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2039
2040 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002041#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002042}
2043
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002044#if defined(MBEDTLS_USE_PSA_CRYPTO)
2045int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002046 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002047 const unsigned char *psk_identity,
2048 size_t psk_identity_len )
2049{
Janos Follath865b3eb2019-12-16 11:46:15 +00002050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002051
2052 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002053 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002054 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002055
Hanno Becker7390c712018-11-15 13:33:04 +00002056 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002057 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002059 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002060
2061 /* Check and set PSK Identity */
2062 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2063 psk_identity_len );
2064 if( ret != 0 )
2065 ssl_conf_remove_psk( conf );
2066
2067 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002068}
2069
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002070int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2071 mbedtls_svc_key_id_t psk )
2072{
2073 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2074 ( ssl->handshake == NULL ) )
2075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2076
2077 ssl_remove_psk( ssl );
2078 ssl->handshake->psk_opaque = psk;
2079 return( 0 );
2080}
2081#endif /* MBEDTLS_USE_PSA_CRYPTO */
2082
Jerry Yu8897c072022-08-12 13:56:53 +08002083#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002084void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2085 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2086 size_t),
2087 void *p_psk )
2088{
2089 conf->f_psk = f_psk;
2090 conf->p_psk = p_psk;
2091}
Jerry Yu8897c072022-08-12 13:56:53 +08002092#endif /* MBEDTLS_SSL_SRV_C */
2093
Ronald Cron73fe8df2022-10-05 14:31:43 +02002094#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002095
2096#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002097static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2098 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002099{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002100#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002101 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002102 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002103#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002104 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002105 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002106 return( MBEDTLS_SSL_MODE_STREAM );
2107}
2108
2109#else /* MBEDTLS_USE_PSA_CRYPTO */
2110
2111static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2112 mbedtls_cipher_mode_t mode )
2113{
2114#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2115 if( mode == MBEDTLS_MODE_CBC )
2116 return( MBEDTLS_SSL_MODE_CBC );
2117#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2118
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002119#if defined(MBEDTLS_GCM_C) || \
2120 defined(MBEDTLS_CCM_C) || \
2121 defined(MBEDTLS_CHACHAPOLY_C)
2122 if( mode == MBEDTLS_MODE_GCM ||
2123 mode == MBEDTLS_MODE_CCM ||
2124 mode == MBEDTLS_MODE_CHACHAPOLY )
2125 return( MBEDTLS_SSL_MODE_AEAD );
2126#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002127
2128 return( MBEDTLS_SSL_MODE_STREAM );
2129}
Gilles Peskine301711e2022-04-26 16:57:05 +02002130#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002131
Gilles Peskinee108d982022-04-26 16:50:40 +02002132static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2133 mbedtls_ssl_mode_t base_mode,
2134 int encrypt_then_mac )
2135{
2136#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2137 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2138 base_mode == MBEDTLS_SSL_MODE_CBC )
2139 {
2140 return( MBEDTLS_SSL_MODE_CBC_ETM );
2141 }
2142#else
2143 (void) encrypt_then_mac;
2144#endif
2145 return( base_mode );
2146}
2147
Neil Armstrongab555e02022-04-04 11:07:59 +02002148mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002149 const mbedtls_ssl_transform *transform )
2150{
Gilles Peskinee108d982022-04-26 16:50:40 +02002151 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002152#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002153 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002154#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002155 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2156#endif
2157 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002158
Gilles Peskinee108d982022-04-26 16:50:40 +02002159 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002160#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002161 encrypt_then_mac = transform->encrypt_then_mac;
2162#endif
2163 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002164}
2165
Neil Armstrongab555e02022-04-04 11:07:59 +02002166mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002167#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002168 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002169#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002170 const mbedtls_ssl_ciphersuite_t *suite )
2171{
Gilles Peskinee108d982022-04-26 16:50:40 +02002172 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2173
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002174#if defined(MBEDTLS_USE_PSA_CRYPTO)
2175 psa_status_t status;
2176 psa_algorithm_t alg;
2177 psa_key_type_t type;
2178 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002179 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2180 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002181 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002182#else
2183 const mbedtls_cipher_info_t *cipher =
2184 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002185 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002186 {
2187 base_mode =
2188 mbedtls_ssl_get_base_mode(
2189 mbedtls_cipher_info_get_mode( cipher ) );
2190 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002191#endif /* MBEDTLS_USE_PSA_CRYPTO */
2192
Gilles Peskinee108d982022-04-26 16:50:40 +02002193#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2194 int encrypt_then_mac = 0;
2195#endif
2196 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002197}
2198
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002199#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002200
2201#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002202/* Serialization of TLS 1.3 sessions:
2203 *
2204 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002205 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002206 * uint64 ticket_received;
2207 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002208 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002209 * } ClientOnlyData;
2210 *
2211 * struct {
2212 * uint8 endpoint;
2213 * uint8 ciphersuite[2];
2214 * uint32 ticket_age_add;
2215 * uint8 ticket_flags;
2216 * opaque resumption_key<0..255>;
2217 * select ( endpoint ) {
2218 * case client: ClientOnlyData;
2219 * case server: uint64 start_time;
2220 * };
2221 * } serialized_session_tls13;
2222 *
2223 */
2224#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002225MBEDTLS_CHECK_RETURN_CRITICAL
2226static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2227 unsigned char *buf,
2228 size_t buf_len,
2229 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002230{
2231 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002232#if defined(MBEDTLS_SSL_CLI_C) && \
2233 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2234 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002235 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002236#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002237 size_t needed = 1 /* endpoint */
2238 + 2 /* ciphersuite */
2239 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002240 + 1 /* ticket_flags */
2241 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002242 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002243
2244 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2246 needed += session->resumption_key_len; /* resumption_key */
2247
Jerry Yu438ddd82022-07-07 06:55:50 +00002248#if defined(MBEDTLS_HAVE_TIME)
2249 needed += 8; /* start_time or ticket_received */
2250#endif
2251
2252#if defined(MBEDTLS_SSL_CLI_C)
2253 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2254 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002255#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002256 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002257 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002258#endif
2259
Jerry Yu438ddd82022-07-07 06:55:50 +00002260 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002261 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002262
2263 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002264 if( session->ticket_len > SIZE_MAX - needed )
2265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002266
Jerry Yue28d9742022-08-18 15:44:03 +08002267 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002268 }
2269#endif /* MBEDTLS_SSL_CLI_C */
2270
Jerry Yue36fdd62022-08-17 21:31:36 +08002271 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002272 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002273 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002274
2275 p[0] = session->endpoint;
2276 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2277 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2278 p[7] = session->ticket_flags;
2279
2280 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002281 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002282 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002283 memcpy( p, session->resumption_key, session->resumption_key_len );
2284 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002285
2286#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2287 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2288 {
2289 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2290 p += 8;
2291 }
2292#endif /* MBEDTLS_HAVE_TIME */
2293
2294#if defined(MBEDTLS_SSL_CLI_C)
2295 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2296 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002297#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2298 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2299 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002300 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002301 {
2302 /* save host name */
2303 memcpy( p, session->hostname, hostname_len );
2304 p += hostname_len;
2305 }
2306#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2307
Jerry Yu438ddd82022-07-07 06:55:50 +00002308#if defined(MBEDTLS_HAVE_TIME)
2309 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2310 p += 8;
2311#endif
2312 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2313 p += 4;
2314
2315 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2316 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002317
2318 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002319 {
2320 memcpy( p, session->ticket, session->ticket_len );
2321 p += session->ticket_len;
2322 }
2323 }
2324#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002325 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002326}
2327
2328MBEDTLS_CHECK_RETURN_CRITICAL
2329static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2330 const unsigned char *buf,
2331 size_t len )
2332{
2333 const unsigned char *p = buf;
2334 const unsigned char *end = buf + len;
2335
2336 if( end - p < 9 )
2337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2338 session->endpoint = p[0];
2339 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2340 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2341 session->ticket_flags = p[7];
2342
2343 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002344 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002345 p += 9;
2346
Jerry Yubc7c1a42022-07-21 22:57:37 +08002347 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2349
Jerry Yubc7c1a42022-07-21 22:57:37 +08002350 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002352 memcpy( session->resumption_key, p, session->resumption_key_len );
2353 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002354
2355#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2356 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2357 {
2358 if( end - p < 8 )
2359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2360 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2361 p += 8;
2362 }
2363#endif /* MBEDTLS_HAVE_TIME */
2364
2365#if defined(MBEDTLS_SSL_CLI_C)
2366 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2367 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002368#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2369 defined(MBEDTLS_SSL_SESSION_TICKETS)
2370 size_t hostname_len;
2371 /* load host name */
2372 if( end - p < 2 )
2373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2374 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2375 p += 2;
2376
2377 if( end - p < ( long int )hostname_len )
2378 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2379 if( hostname_len > 0 )
2380 {
2381 session->hostname = mbedtls_calloc( 1, hostname_len );
2382 if( session->hostname == NULL )
2383 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2384 memcpy( session->hostname, p, hostname_len );
2385 p += hostname_len;
2386 }
2387#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2388 MBEDTLS_SSL_SESSION_TICKETS */
2389
Jerry Yu438ddd82022-07-07 06:55:50 +00002390#if defined(MBEDTLS_HAVE_TIME)
2391 if( end - p < 8 )
2392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2393 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2394 p += 8;
2395#endif
2396 if( end - p < 4 )
2397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2398 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2399 p += 4;
2400
2401 if( end - p < 2 )
2402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2403 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2404 p += 2;
2405
2406 if( end - p < ( long int )session->ticket_len )
2407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2408 if( session->ticket_len > 0 )
2409 {
2410 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2411 if( session->ticket == NULL )
2412 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2413 memcpy( session->ticket, p, session->ticket_len );
2414 p += session->ticket_len;
2415 }
2416 }
2417#endif /* MBEDTLS_SSL_CLI_C */
2418
2419 return( 0 );
2420
2421}
2422#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002423MBEDTLS_CHECK_RETURN_CRITICAL
2424static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2425 unsigned char *buf,
2426 size_t buf_len,
2427 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002428{
2429 ((void) session);
2430 ((void) buf);
2431 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002432 *olen = 0;
2433 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002434}
Jerry Yu438ddd82022-07-07 06:55:50 +00002435
2436static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2437 unsigned char *buf,
2438 size_t buf_len )
2439{
2440 ((void) session);
2441 ((void) buf);
2442 ((void) buf_len);
2443 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2444}
2445#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002446#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2447
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002448psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002449 size_t taglen,
2450 psa_algorithm_t *alg,
2451 psa_key_type_t *key_type,
2452 size_t *key_size )
2453{
2454 switch ( mbedtls_cipher_type )
2455 {
2456 case MBEDTLS_CIPHER_AES_128_CBC:
2457 *alg = PSA_ALG_CBC_NO_PADDING;
2458 *key_type = PSA_KEY_TYPE_AES;
2459 *key_size = 128;
2460 break;
2461 case MBEDTLS_CIPHER_AES_128_CCM:
2462 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2463 *key_type = PSA_KEY_TYPE_AES;
2464 *key_size = 128;
2465 break;
2466 case MBEDTLS_CIPHER_AES_128_GCM:
2467 *alg = PSA_ALG_GCM;
2468 *key_type = PSA_KEY_TYPE_AES;
2469 *key_size = 128;
2470 break;
2471 case MBEDTLS_CIPHER_AES_192_CCM:
2472 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2473 *key_type = PSA_KEY_TYPE_AES;
2474 *key_size = 192;
2475 break;
2476 case MBEDTLS_CIPHER_AES_192_GCM:
2477 *alg = PSA_ALG_GCM;
2478 *key_type = PSA_KEY_TYPE_AES;
2479 *key_size = 192;
2480 break;
2481 case MBEDTLS_CIPHER_AES_256_CBC:
2482 *alg = PSA_ALG_CBC_NO_PADDING;
2483 *key_type = PSA_KEY_TYPE_AES;
2484 *key_size = 256;
2485 break;
2486 case MBEDTLS_CIPHER_AES_256_CCM:
2487 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2488 *key_type = PSA_KEY_TYPE_AES;
2489 *key_size = 256;
2490 break;
2491 case MBEDTLS_CIPHER_AES_256_GCM:
2492 *alg = PSA_ALG_GCM;
2493 *key_type = PSA_KEY_TYPE_AES;
2494 *key_size = 256;
2495 break;
2496 case MBEDTLS_CIPHER_ARIA_128_CBC:
2497 *alg = PSA_ALG_CBC_NO_PADDING;
2498 *key_type = PSA_KEY_TYPE_ARIA;
2499 *key_size = 128;
2500 break;
2501 case MBEDTLS_CIPHER_ARIA_128_CCM:
2502 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2503 *key_type = PSA_KEY_TYPE_ARIA;
2504 *key_size = 128;
2505 break;
2506 case MBEDTLS_CIPHER_ARIA_128_GCM:
2507 *alg = PSA_ALG_GCM;
2508 *key_type = PSA_KEY_TYPE_ARIA;
2509 *key_size = 128;
2510 break;
2511 case MBEDTLS_CIPHER_ARIA_192_CCM:
2512 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2513 *key_type = PSA_KEY_TYPE_ARIA;
2514 *key_size = 192;
2515 break;
2516 case MBEDTLS_CIPHER_ARIA_192_GCM:
2517 *alg = PSA_ALG_GCM;
2518 *key_type = PSA_KEY_TYPE_ARIA;
2519 *key_size = 192;
2520 break;
2521 case MBEDTLS_CIPHER_ARIA_256_CBC:
2522 *alg = PSA_ALG_CBC_NO_PADDING;
2523 *key_type = PSA_KEY_TYPE_ARIA;
2524 *key_size = 256;
2525 break;
2526 case MBEDTLS_CIPHER_ARIA_256_CCM:
2527 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2528 *key_type = PSA_KEY_TYPE_ARIA;
2529 *key_size = 256;
2530 break;
2531 case MBEDTLS_CIPHER_ARIA_256_GCM:
2532 *alg = PSA_ALG_GCM;
2533 *key_type = PSA_KEY_TYPE_ARIA;
2534 *key_size = 256;
2535 break;
2536 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2537 *alg = PSA_ALG_CBC_NO_PADDING;
2538 *key_type = PSA_KEY_TYPE_CAMELLIA;
2539 *key_size = 128;
2540 break;
2541 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2542 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2543 *key_type = PSA_KEY_TYPE_CAMELLIA;
2544 *key_size = 128;
2545 break;
2546 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2547 *alg = PSA_ALG_GCM;
2548 *key_type = PSA_KEY_TYPE_CAMELLIA;
2549 *key_size = 128;
2550 break;
2551 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2552 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2553 *key_type = PSA_KEY_TYPE_CAMELLIA;
2554 *key_size = 192;
2555 break;
2556 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2557 *alg = PSA_ALG_GCM;
2558 *key_type = PSA_KEY_TYPE_CAMELLIA;
2559 *key_size = 192;
2560 break;
2561 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2562 *alg = PSA_ALG_CBC_NO_PADDING;
2563 *key_type = PSA_KEY_TYPE_CAMELLIA;
2564 *key_size = 256;
2565 break;
2566 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2567 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2568 *key_type = PSA_KEY_TYPE_CAMELLIA;
2569 *key_size = 256;
2570 break;
2571 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2572 *alg = PSA_ALG_GCM;
2573 *key_type = PSA_KEY_TYPE_CAMELLIA;
2574 *key_size = 256;
2575 break;
2576 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2577 *alg = PSA_ALG_CHACHA20_POLY1305;
2578 *key_type = PSA_KEY_TYPE_CHACHA20;
2579 *key_size = 256;
2580 break;
2581 case MBEDTLS_CIPHER_NULL:
2582 *alg = MBEDTLS_SSL_NULL_CIPHER;
2583 *key_type = 0;
2584 *key_size = 0;
2585 break;
2586 default:
2587 return PSA_ERROR_NOT_SUPPORTED;
2588 }
2589
2590 return PSA_SUCCESS;
2591}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002592#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002593
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002594#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002595int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2596 const unsigned char *dhm_P, size_t P_len,
2597 const unsigned char *dhm_G, size_t G_len )
2598{
Janos Follath865b3eb2019-12-16 11:46:15 +00002599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002600
Glenn Strausscee11292021-12-20 01:43:17 -05002601 mbedtls_mpi_free( &conf->dhm_P );
2602 mbedtls_mpi_free( &conf->dhm_G );
2603
Hanno Beckera90658f2017-10-04 15:29:08 +01002604 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2605 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2606 {
2607 mbedtls_mpi_free( &conf->dhm_P );
2608 mbedtls_mpi_free( &conf->dhm_G );
2609 return( ret );
2610 }
2611
2612 return( 0 );
2613}
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002615int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002616{
Janos Follath865b3eb2019-12-16 11:46:15 +00002617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002618
Glenn Strausscee11292021-12-20 01:43:17 -05002619 mbedtls_mpi_free( &conf->dhm_P );
2620 mbedtls_mpi_free( &conf->dhm_G );
2621
Gilles Peskinee5702482021-06-11 21:59:08 +02002622 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2623 &conf->dhm_P ) ) != 0 ||
2624 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2625 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002626 {
2627 mbedtls_mpi_free( &conf->dhm_P );
2628 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002629 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002630 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002631
2632 return( 0 );
2633}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002634#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002635
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002636#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2637/*
2638 * Set the minimum length for Diffie-Hellman parameters
2639 */
2640void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2641 unsigned int bitlen )
2642{
2643 conf->dhm_min_bitlen = bitlen;
2644}
2645#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2646
Ronald Crone68ab4f2022-10-05 12:46:29 +02002647#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002648#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002649/*
2650 * Set allowed/preferred hashes for handshake signatures
2651 */
2652void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2653 const int *hashes )
2654{
2655 conf->sig_hashes = hashes;
2656}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002657#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002658
Jerry Yuf017ee42022-01-12 15:49:48 +08002659/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002660void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2661 const uint16_t* sig_algs )
2662{
Jerry Yuf017ee42022-01-12 15:49:48 +08002663#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2664 conf->sig_hashes = NULL;
2665#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2666 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002667}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002668#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002669
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002670#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002671#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002672/*
2673 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002674 *
2675 * mbedtls_ssl_setup() takes the provided list
2676 * and translates it to a list of IANA TLS group identifiers,
2677 * stored in ssl->handshake->group_list.
2678 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002679 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002680void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002681 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002682{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002683 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002684 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002685}
Brett Warrene0edc842021-08-17 09:53:13 +01002686#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002687#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002688
Brett Warrene0edc842021-08-17 09:53:13 +01002689/*
2690 * Set the allowed groups
2691 */
2692void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2693 const uint16_t *group_list )
2694{
2695#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2696 conf->curve_list = NULL;
2697#endif
2698 conf->group_list = group_list;
2699}
2700
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002701#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002703{
Hanno Becker947194e2017-04-07 13:25:49 +01002704 /* Initialize to suppress unnecessary compiler warning */
2705 size_t hostname_len = 0;
2706
2707 /* Check if new hostname is valid before
2708 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002709 if( hostname != NULL )
2710 {
2711 hostname_len = strlen( hostname );
2712
2713 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2715 }
2716
2717 /* Now it's clear that we will overwrite the old hostname,
2718 * so we can free it safely */
2719
2720 if( ssl->hostname != NULL )
2721 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002722 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002723 mbedtls_free( ssl->hostname );
2724 }
2725
2726 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002727
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002729 {
2730 ssl->hostname = NULL;
2731 }
2732 else
2733 {
2734 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002735 if( ssl->hostname == NULL )
2736 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002737
Hanno Becker947194e2017-04-07 13:25:49 +01002738 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002739
Hanno Becker947194e2017-04-07 13:25:49 +01002740 ssl->hostname[hostname_len] = '\0';
2741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
2743 return( 0 );
2744}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002745#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002747#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002748void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002750 const unsigned char *, size_t),
2751 void *p_sni )
2752{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002753 conf->f_sni = f_sni;
2754 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002759int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002760{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002761 size_t cur_len, tot_len;
2762 const char **p;
2763
2764 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002765 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2766 * MUST NOT be truncated."
2767 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002768 */
2769 tot_len = 0;
2770 for( p = protos; *p != NULL; p++ )
2771 {
2772 cur_len = strlen( *p );
2773 tot_len += cur_len;
2774
Ronald Cron8216dd32020-04-23 16:41:44 +02002775 if( ( cur_len == 0 ) ||
2776 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2777 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002779 }
2780
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002781 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002782
2783 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002784}
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002787{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002788 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002791
Johan Pascalb62bb512015-12-03 21:56:45 +01002792#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002793void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2794 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002795{
2796 conf->dtls_srtp_mki_support = support_mki_value;
2797}
2798
Ron Eldoref72faf2018-07-12 11:54:20 +03002799int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2800 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002801 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002802{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002803 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002804 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002806 }
Ron Eldor591f1622018-01-22 12:30:04 +02002807
2808 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002809 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002810 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002811 }
Ron Eldor591f1622018-01-22 12:30:04 +02002812
2813 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2814 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002815 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002816}
2817
Ron Eldoref72faf2018-07-12 11:54:20 +03002818int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002819 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002820{
Johan Pascal253d0262020-09-22 13:04:45 +02002821 const mbedtls_ssl_srtp_profile *p;
2822 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002823
Johan Pascal253d0262020-09-22 13:04:45 +02002824 /* check the profiles list: all entry must be valid,
2825 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002826 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2827 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2828 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002829 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002830 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002831 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002832 list_size++;
2833 }
2834 else
2835 {
2836 /* unsupported value, stop parsing and set the size to an error value */
2837 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002838 }
2839 }
2840
Johan Pascal5ef72d22020-10-28 17:05:47 +01002841 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002842 {
Johan Pascal253d0262020-09-22 13:04:45 +02002843 conf->dtls_srtp_profile_list = NULL;
2844 conf->dtls_srtp_profile_list_len = 0;
2845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2846 }
2847
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002848 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002849 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002850
2851 return( 0 );
2852}
2853
Johan Pascal5ef72d22020-10-28 17:05:47 +01002854void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2855 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002856{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002857 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2858 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002859 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002860 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002861 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002862 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002863 else
2864 {
2865 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002866 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2867 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002868 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002869}
Johan Pascalb62bb512015-12-03 21:56:45 +01002870#endif /* MBEDTLS_SSL_DTLS_SRTP */
2871
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302872#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002873void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002874{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002875 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002876}
2877
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002878void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002879{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002880 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002881}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302882#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002883
Janos Follath088ce432017-04-10 12:42:31 +01002884#if defined(MBEDTLS_SSL_SRV_C)
2885void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2886 char cert_req_ca_list )
2887{
2888 conf->cert_req_ca_list = cert_req_ca_list;
2889}
2890#endif
2891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002893void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002894{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002895 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002896}
2897#endif
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002900void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002902 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002903}
2904#endif
2905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002907int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002910 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002913 }
2914
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002915 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002916
2917 return( 0 );
2918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002920
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002921void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002922{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002923 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002924}
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002927void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002928{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002929 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002930}
2931
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002932void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002934 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002935}
2936
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002937void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002938 const unsigned char period[8] )
2939{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002940 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002941}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002945#if defined(MBEDTLS_SSL_CLI_C)
2946void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002947{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002948 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002949}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002950#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002951
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002952#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002953
Jerry Yud0766ec2022-09-22 10:46:57 +08002954#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002955void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2956 uint16_t num_tickets )
2957{
Jerry Yud0766ec2022-09-22 10:46:57 +08002958 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002959}
2960#endif
2961
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002962void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2963 mbedtls_ssl_ticket_write_t *f_ticket_write,
2964 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2965 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002966{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002967 conf->f_ticket_write = f_ticket_write;
2968 conf->f_ticket_parse = f_ticket_parse;
2969 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002970}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002973
Hanno Becker7e6c1782021-06-08 09:24:55 +01002974void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2975 mbedtls_ssl_export_keys_t *f_export_keys,
2976 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002977{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002978 ssl->f_export_keys = f_export_keys;
2979 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002980}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002981
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002982#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002983void mbedtls_ssl_conf_async_private_cb(
2984 mbedtls_ssl_config *conf,
2985 mbedtls_ssl_async_sign_t *f_async_sign,
2986 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2987 mbedtls_ssl_async_resume_t *f_async_resume,
2988 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002989 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002990{
2991 conf->f_async_sign_start = f_async_sign;
2992 conf->f_async_decrypt_start = f_async_decrypt;
2993 conf->f_async_resume = f_async_resume;
2994 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002995 conf->p_async_config_data = async_config_data;
2996}
2997
Gilles Peskine8f97af72018-04-26 11:46:10 +02002998void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2999{
3000 return( conf->p_async_config_data );
3001}
3002
Gilles Peskine1febfef2018-04-30 11:54:39 +02003003void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003004{
3005 if( ssl->handshake == NULL )
3006 return( NULL );
3007 else
3008 return( ssl->handshake->user_async_ctx );
3009}
3010
Gilles Peskine1febfef2018-04-30 11:54:39 +02003011void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003012 void *ctx )
3013{
3014 if( ssl->handshake != NULL )
3015 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003016}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003017#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003018
Paul Bakker5121ce52009-01-03 21:22:43 +00003019/*
3020 * SSL get accessors
3021 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003022uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003023{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003024 if( ssl->session != NULL )
3025 return( ssl->session->verify_result );
3026
3027 if( ssl->session_negotiate != NULL )
3028 return( ssl->session_negotiate->verify_result );
3029
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003030 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003031}
3032
Glenn Strauss8f526902022-01-13 00:04:49 -05003033int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3034{
3035 if( ssl == NULL || ssl->session == NULL )
3036 return( 0 );
3037
3038 return( ssl->session->ciphersuite );
3039}
3040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003042{
Paul Bakker926c8e42013-03-06 10:23:34 +01003043 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003044 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003047}
3048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003052 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003053 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003054 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003055 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003056 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003057 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003058 default:
3059 return( "unknown (DTLS)" );
3060 }
3061 }
3062#endif
3063
Glenn Strauss60bfe602022-03-14 19:04:24 -04003064 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003065 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003066 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003067 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003068 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003069 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003070 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003071 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003072 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003073}
3074
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003075#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003076size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3077{
David Horstmann95d516f2021-05-04 18:36:56 +01003078 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003079 size_t read_mfl;
3080
3081 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3082 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3083 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3084 {
3085 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3086 }
3087
3088 /* Check if a smaller max length was negotiated */
3089 if( ssl->session_out != NULL )
3090 {
3091 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3092 if( read_mfl < max_len )
3093 {
3094 max_len = read_mfl;
3095 }
3096 }
3097
3098 // During a handshake, use the value being negotiated
3099 if( ssl->session_negotiate != NULL )
3100 {
3101 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3102 if( read_mfl < max_len )
3103 {
3104 max_len = read_mfl;
3105 }
3106 }
3107
3108 return( max_len );
3109}
3110
3111size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003112{
3113 size_t max_len;
3114
3115 /*
3116 * Assume mfl_code is correct since it was checked when set
3117 */
Angus Grattond8213d02016-05-25 20:56:48 +10003118 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003119
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003120 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003121 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003122 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003123 {
Angus Grattond8213d02016-05-25 20:56:48 +10003124 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003125 }
3126
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003127 /* During a handshake, use the value being negotiated */
3128 if( ssl->session_negotiate != NULL &&
3129 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3130 {
3131 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3132 }
3133
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003134 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003135}
3136#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3137
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003138#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003139size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003140{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003141 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3142 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3143 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3144 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3145 return ( 0 );
3146
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003147 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3148 return( ssl->mtu );
3149
3150 if( ssl->mtu == 0 )
3151 return( ssl->handshake->mtu );
3152
3153 return( ssl->mtu < ssl->handshake->mtu ?
3154 ssl->mtu : ssl->handshake->mtu );
3155}
3156#endif /* MBEDTLS_SSL_PROTO_DTLS */
3157
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003158int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3159{
3160 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3161
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003162#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3163 !defined(MBEDTLS_SSL_PROTO_DTLS)
3164 (void) ssl;
3165#endif
3166
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003167#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003168 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003169
3170 if( max_len > mfl )
3171 max_len = mfl;
3172#endif
3173
3174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003175 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003176 {
Hanno Becker89490712020-02-05 10:50:12 +00003177 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003178 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3179 const size_t overhead = (size_t) ret;
3180
3181 if( ret < 0 )
3182 return( ret );
3183
3184 if( mtu <= overhead )
3185 {
3186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3188 }
3189
3190 if( max_len > mtu - overhead )
3191 max_len = mtu - overhead;
3192 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003193#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003194
Hanno Becker0defedb2018-08-10 12:35:02 +01003195#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3196 !defined(MBEDTLS_SSL_PROTO_DTLS)
3197 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003198#endif
3199
3200 return( (int) max_len );
3201}
3202
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003203int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3204{
3205 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3206
3207#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3208 (void) ssl;
3209#endif
3210
3211#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3212 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3213
3214 if( max_len > mfl )
3215 max_len = mfl;
3216#endif
3217
3218 return( (int) max_len );
3219}
3220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#if defined(MBEDTLS_X509_CRT_PARSE_C)
3222const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003223{
3224 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003225 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003226
Hanno Beckere6824572019-02-07 13:18:46 +00003227#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003228 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003229#else
3230 return( NULL );
3231#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003236int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3237 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003238{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003239 int ret;
3240
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003241 if( ssl == NULL ||
3242 dst == NULL ||
3243 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003244 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003247 }
3248
Hanno Beckere810bbc2021-05-14 16:01:05 +01003249 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3250 * idempotent: Each session can only be exported once.
3251 *
3252 * (This is in preparation for TLS 1.3 support where we will
3253 * need the ability to export multiple sessions (aka tickets),
3254 * which will be achieved by calling mbedtls_ssl_get_session()
3255 * multiple times until it fails.)
3256 *
3257 * Check whether we have already exported the current session,
3258 * and fail if so.
3259 */
3260 if( ssl->session->exported == 1 )
3261 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3262
3263 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3264 if( ret != 0 )
3265 return( ret );
3266
3267 /* Remember that we've exported the session. */
3268 ssl->session->exported = 1;
3269 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003272
Paul Bakker5121ce52009-01-03 21:22:43 +00003273/*
Hanno Beckera835da52019-05-16 12:39:07 +01003274 * Define ticket header determining Mbed TLS version
3275 * and structure of the ticket.
3276 */
3277
Hanno Becker94ef3b32019-05-16 12:50:45 +01003278/*
Hanno Becker50b59662019-05-28 14:30:45 +01003279 * Define bitflag determining compile-time settings influencing
3280 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003281 */
3282
Hanno Becker50b59662019-05-28 14:30:45 +01003283#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003284#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003285#else
Hanno Becker3e088662019-05-29 11:10:18 +01003286#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003287#endif /* MBEDTLS_HAVE_TIME */
3288
3289#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003290#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003291#else
Hanno Becker3e088662019-05-29 11:10:18 +01003292#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003293#endif /* MBEDTLS_X509_CRT_PARSE_C */
3294
3295#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003296#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003297#else
Hanno Becker3e088662019-05-29 11:10:18 +01003298#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003299#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3300
3301#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003302#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003303#else
Hanno Becker3e088662019-05-29 11:10:18 +01003304#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003305#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3306
Hanno Becker94ef3b32019-05-16 12:50:45 +01003307#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003308#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003309#else
Hanno Becker3e088662019-05-29 11:10:18 +01003310#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003311#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3312
Hanno Becker94ef3b32019-05-16 12:50:45 +01003313#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3314#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3315#else
3316#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3317#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3318
Hanno Becker3e088662019-05-29 11:10:18 +01003319#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3320#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3321#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3322#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003323#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3324#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003325
Hanno Becker50b59662019-05-28 14:30:45 +01003326#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003327 ( (uint16_t) ( \
3328 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3329 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3330 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3331 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003332 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003333 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003334
Hanno Beckerf8787072019-05-16 12:41:07 +01003335static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003336 MBEDTLS_VERSION_MAJOR,
3337 MBEDTLS_VERSION_MINOR,
3338 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003339 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3340 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003341};
Hanno Beckera835da52019-05-16 12:39:07 +01003342
3343/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003344 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003345 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003346 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003347 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003348 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003349 * opaque mbedtls_version[3]; // library version: major, minor, patch
3350 * opaque session_format[2]; // library-version specific 16-bit field
3351 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003352 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003353 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003354 * Note: When updating the format, remember to keep
3355 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003356 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003357 * // In this version, `session_format` determines
3358 * // the setting of those compile-time
3359 * // configuration options which influence
3360 * // the structure of mbedtls_ssl_session.
3361 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003362 * uint8_t minor_ver; // Protocol minor version. Possible values:
3363 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003364 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003365 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003366 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003367 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003368 * serialized_session_tls12 data;
3369 *
3370 * };
3371 *
3372 * } serialized_session;
3373 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003374 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003375
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003376MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003377static int ssl_session_save( const mbedtls_ssl_session *session,
3378 unsigned char omit_header,
3379 unsigned char *buf,
3380 size_t buf_len,
3381 size_t *olen )
3382{
3383 unsigned char *p = buf;
3384 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003385 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003386#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3387 size_t out_len;
3388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3389#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003390 if( session == NULL )
3391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3392
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003393 if( !omit_header )
3394 {
3395 /*
3396 * Add Mbed TLS version identifier
3397 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003398 used += sizeof( ssl_serialized_session_header );
3399
3400 if( used <= buf_len )
3401 {
3402 memcpy( p, ssl_serialized_session_header,
3403 sizeof( ssl_serialized_session_header ) );
3404 p += sizeof( ssl_serialized_session_header );
3405 }
3406 }
3407
3408 /*
3409 * TLS version identifier
3410 */
3411 used += 1;
3412 if( used <= buf_len )
3413 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003414 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003415 }
3416
3417 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003418 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003419 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003420 {
3421#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003422 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003423 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003424 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003425#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3426
Jerry Yu251a12e2022-07-13 15:15:48 +08003427#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3428 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003429 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3430 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003431 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003432 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003433 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003434#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3435
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003436 default:
3437 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3438 }
3439
3440 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003441 if( used > buf_len )
3442 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003443
3444 return( 0 );
3445}
3446
3447/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003448 * Public wrapper for ssl_session_save()
3449 */
3450int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3451 unsigned char *buf,
3452 size_t buf_len,
3453 size_t *olen )
3454{
3455 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3456}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003457
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003458/*
3459 * Deserialize session, see mbedtls_ssl_session_save() for format.
3460 *
3461 * This internal version is wrapped by a public function that cleans up in
3462 * case of error, and has an extra option omit_header.
3463 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003464MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003465static int ssl_session_load( mbedtls_ssl_session *session,
3466 unsigned char omit_header,
3467 const unsigned char *buf,
3468 size_t len )
3469{
3470 const unsigned char *p = buf;
3471 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003472 size_t remaining_len;
3473
3474
3475 if( session == NULL )
3476 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003477
3478 if( !omit_header )
3479 {
3480 /*
3481 * Check Mbed TLS version identifier
3482 */
3483
3484 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3486
3487 if( memcmp( p, ssl_serialized_session_header,
3488 sizeof( ssl_serialized_session_header ) ) != 0 )
3489 {
3490 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3491 }
3492 p += sizeof( ssl_serialized_session_header );
3493 }
3494
3495 /*
3496 * TLS version identifier
3497 */
3498 if( 1 > (size_t)( end - p ) )
3499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003500 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003501
3502 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003503 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003504 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003505 {
3506#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003507 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003508 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3510
Jerry Yu438ddd82022-07-07 06:55:50 +00003511#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3512 case MBEDTLS_SSL_VERSION_TLS1_3:
3513 return( ssl_tls13_session_load( session, p, remaining_len ) );
3514#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3515
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003516 default:
3517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3518 }
3519}
3520
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003521/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003522 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003523 */
3524int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3525 const unsigned char *buf,
3526 size_t len )
3527{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003528 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003529
3530 if( ret != 0 )
3531 mbedtls_ssl_session_free( session );
3532
3533 return( ret );
3534}
3535
3536/*
Paul Bakker1961b702013-01-25 14:49:24 +01003537 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003538 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003539MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003540static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3541{
3542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3543
Ronald Cron66dbf912022-02-02 15:33:46 +01003544 /*
3545 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003546 * that were written into the output buffer by the previous handshake step,
3547 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003548 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3549 * We proceed to the next handshake step only when all data from the
3550 * previous one have been sent to the peer, thus we make sure that this is
3551 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3552 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3553 * we have to wait before to go ahead.
3554 * In the case of TLS 1.3, handshake step handlers do not send data to the
3555 * peer. Data are only sent here and through
3556 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003557 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003558 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003559 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3560 return( ret );
3561
3562#if defined(MBEDTLS_SSL_PROTO_DTLS)
3563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3564 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3565 {
3566 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3567 return( ret );
3568 }
3569#endif /* MBEDTLS_SSL_PROTO_DTLS */
3570
3571 return( ret );
3572}
3573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003575{
Hanno Becker41934dd2021-08-07 19:13:43 +01003576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003577
Hanno Becker41934dd2021-08-07 19:13:43 +01003578 if( ssl == NULL ||
3579 ssl->conf == NULL ||
3580 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003581 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003582 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003584 }
3585
3586 ret = ssl_prepare_handshake_step( ssl );
3587 if( ret != 0 )
3588 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003589
Jerry Yue7047812021-09-13 19:26:39 +08003590 ret = mbedtls_ssl_handle_pending_alert( ssl );
3591 if( ret != 0 )
3592 goto cleanup;
3593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003596 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3598 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003599
Ronald Cron9f0fba32022-02-10 16:45:15 +01003600 switch( ssl->state )
3601 {
3602 case MBEDTLS_SSL_HELLO_REQUEST:
3603 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3604 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003605
Ronald Cron9f0fba32022-02-10 16:45:15 +01003606 case MBEDTLS_SSL_CLIENT_HELLO:
3607 ret = mbedtls_ssl_write_client_hello( ssl );
3608 break;
3609
3610 default:
3611#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003612 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003613 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3614 else
3615 ret = mbedtls_ssl_handshake_client_step( ssl );
3616#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3617 ret = mbedtls_ssl_handshake_client_step( ssl );
3618#else
3619 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3620#endif
3621 }
Jerry Yub9930e72021-08-06 17:11:51 +08003622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003625 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003626 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003627#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003628 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003629 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003630#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003631
3632#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3633 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3634 ret = mbedtls_ssl_handshake_server_step( ssl );
3635#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3636 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003637#endif
3638
Jerry Yue7047812021-09-13 19:26:39 +08003639 if( ret != 0 )
3640 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003641 /* handshake_step return error. And it is same
3642 * with alert_reason.
3643 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003644 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003645 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003646 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003647 goto cleanup;
3648 }
3649 }
3650
3651cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003652 return( ret );
3653}
3654
3655/*
3656 * Perform the SSL handshake
3657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003659{
3660 int ret = 0;
3661
Hanno Beckera817ea42020-10-20 15:20:23 +01003662 /* Sanity checks */
3663
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003664 if( ssl == NULL || ssl->conf == NULL )
3665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3666
Hanno Beckera817ea42020-10-20 15:20:23 +01003667#if defined(MBEDTLS_SSL_PROTO_DTLS)
3668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3669 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3670 {
3671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3672 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3674 }
3675#endif /* MBEDTLS_SSL_PROTO_DTLS */
3676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003678
Hanno Beckera817ea42020-10-20 15:20:23 +01003679 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003680 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003683
3684 if( ret != 0 )
3685 break;
3686 }
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003689
3690 return( ret );
3691}
3692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#if defined(MBEDTLS_SSL_RENEGOTIATION)
3694#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003695/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003696 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003697 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003698MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003700{
Janos Follath865b3eb2019-12-16 11:46:15 +00003701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003704
3705 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3707 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003708
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003709 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003710 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003712 return( ret );
3713 }
3714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003716
3717 return( 0 );
3718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003720
3721/*
3722 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 * - any side: calling mbedtls_ssl_renegotiate(),
3724 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3725 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003726 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003727 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003729 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003730int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003731{
Janos Follath865b3eb2019-12-16 11:46:15 +00003732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003735
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003736 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3737 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003738
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003739 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3740 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003742 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003744 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003745 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003746 ssl->handshake->out_msg_seq = 1;
3747 else
3748 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003749 }
3750#endif
3751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3753 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003758 return( ret );
3759 }
3760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003762
3763 return( 0 );
3764}
3765
3766/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003767 * Renegotiate current connection on client,
3768 * or request renegotiation on server
3769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003773
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003774 if( ssl == NULL || ssl->conf == NULL )
3775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003778 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003779 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003780 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003781 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003785
3786 /* Did we already try/start sending HelloRequest? */
3787 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003789
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003790 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003791 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003795 /*
3796 * On client, either start the renegotiation process or,
3797 * if already in progress, continue the handshake
3798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003800 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003801 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003803
Hanno Becker40cdaa12020-02-05 10:48:27 +00003804 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003805 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003807 return( ret );
3808 }
3809 }
3810 else
3811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003815 return( ret );
3816 }
3817 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003819
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003820 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003823
Gilles Peskine9b562d52018-04-25 20:32:43 +02003824void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003825{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003826 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3827
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003828 if( handshake == NULL )
3829 return;
3830
Brett Warrene0edc842021-08-17 09:53:13 +01003831#if defined(MBEDTLS_ECP_C)
3832#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3833 if ( ssl->handshake->group_list_heap_allocated )
3834 mbedtls_free( (void*) handshake->group_list );
3835 handshake->group_list = NULL;
3836#endif /* MBEDTLS_DEPRECATED_REMOVED */
3837#endif /* MBEDTLS_ECP_C */
3838
Ronald Crone68ab4f2022-10-05 12:46:29 +02003839#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003840#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3841 if ( ssl->handshake->sig_algs_heap_allocated )
3842 mbedtls_free( (void*) handshake->sig_algs );
3843 handshake->sig_algs = NULL;
3844#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003845#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3846 if( ssl->handshake->certificate_request_context )
3847 {
3848 mbedtls_free( (void*) handshake->certificate_request_context );
3849 }
3850#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003851#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003852
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003853#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3854 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3855 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003856 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003857 handshake->async_in_progress = 0;
3858 }
3859#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3860
Andrzej Kurek25f27152022-08-17 16:09:31 -04003861#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003862#if defined(MBEDTLS_USE_PSA_CRYPTO)
3863 psa_hash_abort( &handshake->fin_sha256_psa );
3864#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003865 mbedtls_sha256_free( &handshake->fin_sha256 );
3866#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003867#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003868#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003869#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003870 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003871#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003872 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003873#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003874#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876#if defined(MBEDTLS_DHM_C)
3877 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003878#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003879#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003881#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003882#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003883 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003884#if defined(MBEDTLS_SSL_CLI_C)
3885 mbedtls_free( handshake->ecjpake_cache );
3886 handshake->ecjpake_cache = NULL;
3887 handshake->ecjpake_cache_len = 0;
3888#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003889#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003890
Janos Follath4ae5c292016-02-10 11:27:43 +00003891#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3892 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003893 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003895#endif
3896
Ronald Cron73fe8df2022-10-05 14:31:43 +02003897#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003898#if defined(MBEDTLS_USE_PSA_CRYPTO)
3899 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3900 {
3901 /* The maintenance of the external PSK key slot is the
3902 * user's responsibility. */
3903 if( ssl->handshake->psk_opaque_is_internal )
3904 {
3905 psa_destroy_key( ssl->handshake->psk_opaque );
3906 ssl->handshake->psk_opaque_is_internal = 0;
3907 }
3908 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3909 }
Neil Armstronge952a302022-05-03 10:22:14 +02003910#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003911 if( handshake->psk != NULL )
3912 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003913 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003914 mbedtls_free( handshake->psk );
3915 }
Neil Armstronge952a302022-05-03 10:22:14 +02003916#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003917#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3920 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003921 /*
3922 * Free only the linked list wrapper, not the keys themselves
3923 * since the belong to the SNI callback
3924 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003925 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003927
Gilles Peskineeccd8882020-03-10 12:19:08 +01003928#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003929 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003930 if( handshake->ecrs_peer_cert != NULL )
3931 {
3932 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3933 mbedtls_free( handshake->ecrs_peer_cert );
3934 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003935#endif
3936
Hanno Becker75173122019-02-06 16:18:31 +00003937#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3938 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3939 mbedtls_pk_free( &handshake->peer_pubkey );
3940#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3941
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003942#if defined(MBEDTLS_SSL_CLI_C) && \
3943 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3944 mbedtls_free( handshake->cookie );
3945#endif /* MBEDTLS_SSL_CLI_C &&
3946 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003947
3948#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003949 mbedtls_ssl_flight_free( handshake->flight );
3950 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003951#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003952
Ronald Cronf12b81d2022-03-15 10:42:41 +01003953#if defined(MBEDTLS_ECDH_C) && \
3954 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003955 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003956 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003957#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3958
Ronald Cron6f135e12021-12-08 16:57:54 +01003959#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003960 mbedtls_ssl_transform_free( handshake->transform_handshake );
3961 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003962 mbedtls_free( handshake->transform_earlydata );
3963 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003964#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003965
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003966
3967#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3968 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3969 * processes datagrams and the fact that a datagram is allowed to have
3970 * several records in it, it is possible that the I/O buffers are not
3971 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003972 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3973 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003974#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003975
Jerry Yuba9c7272021-10-30 11:54:10 +08003976 /* mbedtls_platform_zeroize MUST be last one in this function */
3977 mbedtls_platform_zeroize( handshake,
3978 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003979}
3980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003982{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003983 if( session == NULL )
3984 return;
3985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003987 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003988#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003989
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003990#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003991#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3992 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003993 mbedtls_free( session->hostname );
3994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003996#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003997
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003998 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003999}
4000
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004001#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004002
4003#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4004#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4005#else
4006#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4007#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4008
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004009#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004010
4011#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4012#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4013#else
4014#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4015#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4016
4017#if defined(MBEDTLS_SSL_ALPN)
4018#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4019#else
4020#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4021#endif /* MBEDTLS_SSL_ALPN */
4022
4023#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4024#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4025#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4026#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4027
4028#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4029 ( (uint32_t) ( \
4030 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4031 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4032 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4033 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4034 0u ) )
4035
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004036static unsigned char ssl_serialized_context_header[] = {
4037 MBEDTLS_VERSION_MAJOR,
4038 MBEDTLS_VERSION_MINOR,
4039 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004040 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4041 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4042 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4043 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4044 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004045};
4046
Paul Bakker5121ce52009-01-03 21:22:43 +00004047/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004048 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004049 *
4050 * The format of the serialized data is:
4051 * (in the presentation language of TLS, RFC 8446 section 3)
4052 *
4053 * // header
4054 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004055 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004056 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004057 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004058 * Note: When updating the format, remember to keep these
4059 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004060 *
4061 * // session sub-structure
4062 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4063 * // transform sub-structure
4064 * uint8 random[64]; // ServerHello.random+ClientHello.random
4065 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4066 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4067 * // fields from ssl_context
4068 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4069 * uint64 in_window_top; // DTLS: last validated record seq_num
4070 * uint64 in_window; // DTLS: bitmask for replay protection
4071 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4072 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4073 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4074 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4075 *
4076 * Note that many fields of the ssl_context or sub-structures are not
4077 * serialized, as they fall in one of the following categories:
4078 *
4079 * 1. forced value (eg in_left must be 0)
4080 * 2. pointer to dynamically-allocated memory (eg session, transform)
4081 * 3. value can be re-derived from other data (eg session keys from MS)
4082 * 4. value was temporary (eg content of input buffer)
4083 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004084 */
4085int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4086 unsigned char *buf,
4087 size_t buf_len,
4088 size_t *olen )
4089{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004090 unsigned char *p = buf;
4091 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004092 size_t session_len;
4093 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004094
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004095 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004096 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4097 * this function's documentation.
4098 *
4099 * These are due to assumptions/limitations in the implementation. Some of
4100 * them are likely to stay (no handshake in progress) some might go away
4101 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004102 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004103 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00004104 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004105 {
4106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004108 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004109 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004110 {
4111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004113 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004114 /* Double-check that sub-structures are indeed ready */
4115 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004116 {
4117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004119 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004120 /* There must be no pending incoming or outgoing data */
4121 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004122 {
4123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004125 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004126 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004127 {
4128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004130 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004131 /* Protocol must be DLTS, not TLS */
4132 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004133 {
4134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004136 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004137 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004138 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004139 {
4140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004142 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004143 /* We must be using an AEAD ciphersuite */
4144 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004145 {
4146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004148 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004149 /* Renegotiation must not be enabled */
4150#if defined(MBEDTLS_SSL_RENEGOTIATION)
4151 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004152 {
4153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004155 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004156#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004157
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004158 /*
4159 * Version and format identifier
4160 */
4161 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004162
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004163 if( used <= buf_len )
4164 {
4165 memcpy( p, ssl_serialized_context_header,
4166 sizeof( ssl_serialized_context_header ) );
4167 p += sizeof( ssl_serialized_context_header );
4168 }
4169
4170 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004171 * Session (length + data)
4172 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004173 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004174 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4175 return( ret );
4176
4177 used += 4 + session_len;
4178 if( used <= buf_len )
4179 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004180 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4181 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004182
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004183 ret = ssl_session_save( ssl->session, 1,
4184 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004185 if( ret != 0 )
4186 return( ret );
4187
4188 p += session_len;
4189 }
4190
4191 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004192 * Transform
4193 */
4194 used += sizeof( ssl->transform->randbytes );
4195 if( used <= buf_len )
4196 {
4197 memcpy( p, ssl->transform->randbytes,
4198 sizeof( ssl->transform->randbytes ) );
4199 p += sizeof( ssl->transform->randbytes );
4200 }
4201
4202#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4203 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4204 if( used <= buf_len )
4205 {
4206 *p++ = ssl->transform->in_cid_len;
4207 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4208 p += ssl->transform->in_cid_len;
4209
4210 *p++ = ssl->transform->out_cid_len;
4211 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4212 p += ssl->transform->out_cid_len;
4213 }
4214#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4215
4216 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004217 * Saved fields from top-level ssl_context structure
4218 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004219 used += 4;
4220 if( used <= buf_len )
4221 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004222 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4223 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004224 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004225
4226#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4227 used += 16;
4228 if( used <= buf_len )
4229 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004230 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4231 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004232
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004233 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4234 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004235 }
4236#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4237
4238#if defined(MBEDTLS_SSL_PROTO_DTLS)
4239 used += 1;
4240 if( used <= buf_len )
4241 {
4242 *p++ = ssl->disable_datagram_packing;
4243 }
4244#endif /* MBEDTLS_SSL_PROTO_DTLS */
4245
Jerry Yuae0b2e22021-10-08 15:21:19 +08004246 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004247 if( used <= buf_len )
4248 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004249 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4250 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004251 }
4252
4253#if defined(MBEDTLS_SSL_PROTO_DTLS)
4254 used += 2;
4255 if( used <= buf_len )
4256 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004257 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4258 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004259 }
4260#endif /* MBEDTLS_SSL_PROTO_DTLS */
4261
4262#if defined(MBEDTLS_SSL_ALPN)
4263 {
4264 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004265 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004266 : 0;
4267
4268 used += 1 + alpn_len;
4269 if( used <= buf_len )
4270 {
4271 *p++ = alpn_len;
4272
4273 if( ssl->alpn_chosen != NULL )
4274 {
4275 memcpy( p, ssl->alpn_chosen, alpn_len );
4276 p += alpn_len;
4277 }
4278 }
4279 }
4280#endif /* MBEDTLS_SSL_ALPN */
4281
4282 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004283 * Done
4284 */
4285 *olen = used;
4286
4287 if( used > buf_len )
4288 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004289
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004290 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4291
Hanno Becker43aefe22020-02-05 10:44:56 +00004292 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004293}
4294
4295/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004296 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004297 *
4298 * This internal version is wrapped by a public function that cleans up in
4299 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004300 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004301MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004302static int ssl_context_load( mbedtls_ssl_context *ssl,
4303 const unsigned char *buf,
4304 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004305{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004306 const unsigned char *p = buf;
4307 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004308 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004310#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004311 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004312#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004313
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004314 /*
4315 * The context should have been freshly setup or reset.
4316 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004317 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004318 * renegotiating, or if the user mistakenly loaded a session first.)
4319 */
4320 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4321 ssl->session != NULL )
4322 {
4323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4324 }
4325
4326 /*
4327 * We can't check that the config matches the initial one, but we can at
4328 * least check it matches the requirements for serializing.
4329 */
4330 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004331 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4332 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004333#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004334 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004335#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004336 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004337 {
4338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4339 }
4340
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004341 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4342
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004343 /*
4344 * Check version identifier
4345 */
4346 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4348
4349 if( memcmp( p, ssl_serialized_context_header,
4350 sizeof( ssl_serialized_context_header ) ) != 0 )
4351 {
4352 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4353 }
4354 p += sizeof( ssl_serialized_context_header );
4355
4356 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004357 * Session
4358 */
4359 if( (size_t)( end - p ) < 4 )
4360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4361
4362 session_len = ( (size_t) p[0] << 24 ) |
4363 ( (size_t) p[1] << 16 ) |
4364 ( (size_t) p[2] << 8 ) |
4365 ( (size_t) p[3] );
4366 p += 4;
4367
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004368 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004369 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004370 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004371 ssl->session_in = ssl->session;
4372 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004373 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004374
4375 if( (size_t)( end - p ) < session_len )
4376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4377
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004378 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004379 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004380 {
4381 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004382 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004383 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004384
4385 p += session_len;
4386
4387 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004388 * Transform
4389 */
4390
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004391 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004392 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004393 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004394 ssl->transform_in = ssl->transform;
4395 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004396 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004397
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004398#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004399 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4400 if( prf_func == NULL )
4401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4402
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004403 /* Read random bytes and populate structure */
4404 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004406
Hanno Beckerbd257552021-03-22 06:59:27 +00004407 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004408 ssl->session->ciphersuite,
4409 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004410#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004411 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004412#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004413 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004414 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004415 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004416 ssl->conf->endpoint,
4417 ssl );
4418 if( ret != 0 )
4419 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004420#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004421 p += sizeof( ssl->transform->randbytes );
4422
4423#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4424 /* Read connection IDs and store them */
4425 if( (size_t)( end - p ) < 1 )
4426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4427
4428 ssl->transform->in_cid_len = *p++;
4429
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004430 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4432
4433 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4434 p += ssl->transform->in_cid_len;
4435
4436 ssl->transform->out_cid_len = *p++;
4437
4438 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4440
4441 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4442 p += ssl->transform->out_cid_len;
4443#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4444
4445 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004446 * Saved fields from top-level ssl_context structure
4447 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004448 if( (size_t)( end - p ) < 4 )
4449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4450
4451 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4452 ( (uint32_t) p[1] << 16 ) |
4453 ( (uint32_t) p[2] << 8 ) |
4454 ( (uint32_t) p[3] );
4455 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004456
4457#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4458 if( (size_t)( end - p ) < 16 )
4459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4460
4461 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4462 ( (uint64_t) p[1] << 48 ) |
4463 ( (uint64_t) p[2] << 40 ) |
4464 ( (uint64_t) p[3] << 32 ) |
4465 ( (uint64_t) p[4] << 24 ) |
4466 ( (uint64_t) p[5] << 16 ) |
4467 ( (uint64_t) p[6] << 8 ) |
4468 ( (uint64_t) p[7] );
4469 p += 8;
4470
4471 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4472 ( (uint64_t) p[1] << 48 ) |
4473 ( (uint64_t) p[2] << 40 ) |
4474 ( (uint64_t) p[3] << 32 ) |
4475 ( (uint64_t) p[4] << 24 ) |
4476 ( (uint64_t) p[5] << 16 ) |
4477 ( (uint64_t) p[6] << 8 ) |
4478 ( (uint64_t) p[7] );
4479 p += 8;
4480#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4481
4482#if defined(MBEDTLS_SSL_PROTO_DTLS)
4483 if( (size_t)( end - p ) < 1 )
4484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4485
4486 ssl->disable_datagram_packing = *p++;
4487#endif /* MBEDTLS_SSL_PROTO_DTLS */
4488
Jerry Yud9a94fe2021-09-28 18:58:59 +08004489 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004491 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4492 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004493
4494#if defined(MBEDTLS_SSL_PROTO_DTLS)
4495 if( (size_t)( end - p ) < 2 )
4496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4497
4498 ssl->mtu = ( p[0] << 8 ) | p[1];
4499 p += 2;
4500#endif /* MBEDTLS_SSL_PROTO_DTLS */
4501
4502#if defined(MBEDTLS_SSL_ALPN)
4503 {
4504 uint8_t alpn_len;
4505 const char **cur;
4506
4507 if( (size_t)( end - p ) < 1 )
4508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4509
4510 alpn_len = *p++;
4511
4512 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4513 {
4514 /* alpn_chosen should point to an item in the configured list */
4515 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4516 {
4517 if( strlen( *cur ) == alpn_len &&
4518 memcmp( p, cur, alpn_len ) == 0 )
4519 {
4520 ssl->alpn_chosen = *cur;
4521 break;
4522 }
4523 }
4524 }
4525
4526 /* can only happen on conf mismatch */
4527 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4529
4530 p += alpn_len;
4531 }
4532#endif /* MBEDTLS_SSL_ALPN */
4533
4534 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004535 * Forced fields from top-level ssl_context structure
4536 *
4537 * Most of them already set to the correct value by mbedtls_ssl_init() and
4538 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4539 */
4540 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004541 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004542
Hanno Becker361b10d2019-08-30 10:42:49 +01004543 /* Adjust pointers for header fields of outgoing records to
4544 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004545 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004546
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004547#if defined(MBEDTLS_SSL_PROTO_DTLS)
4548 ssl->in_epoch = 1;
4549#endif
4550
4551 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4552 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004553 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4554 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004555 if( ssl->handshake != NULL )
4556 {
4557 mbedtls_ssl_handshake_free( ssl );
4558 mbedtls_free( ssl->handshake );
4559 ssl->handshake = NULL;
4560 }
4561
4562 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004563 * Done - should have consumed entire buffer
4564 */
4565 if( p != end )
4566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004567
4568 return( 0 );
4569}
4570
4571/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004572 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004573 */
4574int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4575 const unsigned char *buf,
4576 size_t len )
4577{
4578 int ret = ssl_context_load( context, buf, len );
4579
4580 if( ret != 0 )
4581 mbedtls_ssl_free( context );
4582
4583 return( ret );
4584}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004585#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004586
4587/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004588 * Free an SSL context
4589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004591{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004592 if( ssl == NULL )
4593 return;
4594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004596
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004597 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004598 {
sander-visserb8aa2072020-05-06 22:05:13 +02004599#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4600 size_t out_buf_len = ssl->out_buf_len;
4601#else
4602 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4603#endif
4604
Darryl Greenb33cc762019-11-28 14:29:44 +00004605 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004607 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 }
4609
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004610 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 {
sander-visserb8aa2072020-05-06 22:05:13 +02004612#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4613 size_t in_buf_len = ssl->in_buf_len;
4614#else
4615 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4616#endif
4617
Darryl Greenb33cc762019-11-28 14:29:44 +00004618 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004620 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 }
4622
Paul Bakker48916f92012-09-16 19:57:18 +00004623 if( ssl->transform )
4624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 mbedtls_ssl_transform_free( ssl->transform );
4626 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004627 }
4628
4629 if( ssl->handshake )
4630 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004631 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4633 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 mbedtls_free( ssl->handshake );
4636 mbedtls_free( ssl->transform_negotiate );
4637 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004638 }
4639
Ronald Cron6f135e12021-12-08 16:57:54 +01004640#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004641 mbedtls_ssl_transform_free( ssl->transform_application );
4642 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004643#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004644
Paul Bakkerc0463502013-02-14 11:19:38 +01004645 if( ssl->session )
4646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 mbedtls_ssl_session_free( ssl->session );
4648 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004649 }
4650
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004651#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004652 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004653 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004654 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004656 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004658
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004659#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004661#endif
4662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004664
Paul Bakker86f04f42013-02-14 11:20:09 +01004665 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004666 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004667}
4668
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004669/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004670 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004671 */
4672void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4673{
4674 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4675}
4676
Gilles Peskineae270bf2021-06-02 00:05:29 +02004677/* The selection should be the same as mbedtls_x509_crt_profile_default in
4678 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004679 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004680 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004681 * about this list.
4682 */
Brett Warrene0edc842021-08-17 09:53:13 +01004683static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004684#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004685 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004686#endif
4687#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004688 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004689#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004690#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004691 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004692#endif
4693#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004694 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004695#endif
4696#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004697 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004698#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004699#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004700 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004701#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004702#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004703 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004704#endif
4705#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004706 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004707#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004708 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004709};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004710
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004711static int ssl_preset_suiteb_ciphersuites[] = {
4712 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4713 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4714 0
4715};
4716
Ronald Crone68ab4f2022-10-05 12:46:29 +02004717#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004718
Jerry Yu909df7b2022-01-22 11:56:27 +08004719/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004720 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004721 * rules SHOULD be upheld.
4722 * - No duplicate entries.
4723 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004724 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004725 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004726 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004727static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004728
Andrzej Kurek25f27152022-08-17 16:09:31 -04004729#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 +08004730 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4731 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004732#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004733 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004734
Andrzej Kurek25f27152022-08-17 16:09:31 -04004735#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 +08004736 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4737 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004738#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004739 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4740
Andrzej Kurek25f27152022-08-17 16:09:31 -04004741#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 +08004742 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4743 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004744#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004745 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004746
Andrzej Kurek25f27152022-08-17 16:09:31 -04004747#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 +08004748 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004749#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 +08004750
Andrzej Kurek25f27152022-08-17 16:09:31 -04004751#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 +08004752 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004753#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 +08004754
Andrzej Kurek25f27152022-08-17 16:09:31 -04004755#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 +08004756 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004757#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 +08004758
Andrzej Kurek25f27152022-08-17 16:09:31 -04004759#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 +08004760 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4761#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4762
Andrzej Kurek25f27152022-08-17 16:09:31 -04004763#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 +08004764 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4765#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4766
Andrzej Kurek25f27152022-08-17 16:09:31 -04004767#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 +08004768 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4769#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4770
Gabor Mezei15b95a62022-05-09 16:37:58 +02004771 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004772};
4773
4774/* NOTICE: see above */
4775#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4776static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004777#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004778#if defined(MBEDTLS_ECDSA_C)
4779 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004780#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004781#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4782 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4783#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004784#if defined(MBEDTLS_RSA_C)
4785 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4786#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004787#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004788#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004789#if defined(MBEDTLS_ECDSA_C)
4790 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004791#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004792#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4793 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4794#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004795#if defined(MBEDTLS_RSA_C)
4796 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4797#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004798#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004799#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004800#if defined(MBEDTLS_ECDSA_C)
4801 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004802#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004803#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4804 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4805#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004806#if defined(MBEDTLS_RSA_C)
4807 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4808#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004809#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004810 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004811};
4812#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4813/* NOTICE: see above */
4814static uint16_t ssl_preset_suiteb_sig_algs[] = {
4815
Andrzej Kurek25f27152022-08-17 16:09:31 -04004816#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 +08004817 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4818 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004819#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004820 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4821
Andrzej Kurek25f27152022-08-17 16:09:31 -04004822#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 +08004823 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4824 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004825#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004826 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004827
Andrzej Kurek25f27152022-08-17 16:09:31 -04004828#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 +08004829 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004830#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 +08004831
Andrzej Kurek25f27152022-08-17 16:09:31 -04004832#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 +08004833 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004834#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004835
Gabor Mezei15b95a62022-05-09 16:37:58 +02004836 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004837};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004838
Jerry Yu909df7b2022-01-22 11:56:27 +08004839/* NOTICE: see above */
4840#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4841static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004842#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004843#if defined(MBEDTLS_ECDSA_C)
4844 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004845#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004846#if defined(MBEDTLS_RSA_C)
4847 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4848#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004849#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004850#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004851#if defined(MBEDTLS_ECDSA_C)
4852 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004853#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004854#if defined(MBEDTLS_RSA_C)
4855 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4856#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004857#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004858 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004859};
Jerry Yu909df7b2022-01-22 11:56:27 +08004860#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4861
Ronald Crone68ab4f2022-10-05 12:46:29 +02004862#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004863
Brett Warrene0edc842021-08-17 09:53:13 +01004864static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004865#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004866 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004867#endif
4868#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004869 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004870#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004871 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004872};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004873
Ronald Crone68ab4f2022-10-05 12:46:29 +02004874#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004875/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004876 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004877MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004878static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004879{
4880 size_t i, j;
4881 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004882
Gabor Mezei15b95a62022-05-09 16:37:58 +02004883 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004884 {
Jerry Yuf377d642022-01-25 10:43:59 +08004885 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004886 {
Jerry Yuf377d642022-01-25 10:43:59 +08004887 if( sig_algs[i] != sig_algs[j] )
4888 continue;
4889 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4890 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4891 sig_algs[i], j, i );
4892 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004893 }
4894 }
4895 return( ret );
4896}
4897
Ronald Crone68ab4f2022-10-05 12:46:29 +02004898#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004899
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004900/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004901 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004902 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004903int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004904 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004905{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004906#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004908#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004909
Ronald Crone68ab4f2022-10-05 12:46:29 +02004910#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004911 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004912 {
4913 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4914 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4915 }
4916
Jerry Yuf377d642022-01-25 10:43:59 +08004917 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004918 {
4919 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4920 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4921 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004922
4923#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004924 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004925 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004926 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004927 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4928 }
4929
Jerry Yuf377d642022-01-25 10:43:59 +08004930 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004931 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004932 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004933 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4934 }
4935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004936#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004937
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004938 /* Use the functions here so that they are covered in tests,
4939 * but otherwise access member directly for efficiency */
4940 mbedtls_ssl_conf_endpoint( conf, endpoint );
4941 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004942
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004943 /*
4944 * Things that are common to all presets
4945 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004946#if defined(MBEDTLS_SSL_CLI_C)
4947 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4948 {
4949 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4950#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4951 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4952#endif
4953 }
4954#endif
4955
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004956#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4957 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4958#endif
4959
4960#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4961 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4962#endif
4963
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004964#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004965 conf->f_cookie_write = ssl_cookie_write_dummy;
4966 conf->f_cookie_check = ssl_cookie_check_dummy;
4967#endif
4968
4969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4970 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4971#endif
4972
Janos Follath088ce432017-04-10 12:42:31 +01004973#if defined(MBEDTLS_SSL_SRV_C)
4974 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004975 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004976#endif
4977
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004978#if defined(MBEDTLS_SSL_PROTO_DTLS)
4979 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4980 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4981#endif
4982
4983#if defined(MBEDTLS_SSL_RENEGOTIATION)
4984 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004985 memset( conf->renego_period, 0x00, 2 );
4986 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004987#endif
4988
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004989#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004990 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4991 {
4992 const unsigned char dhm_p[] =
4993 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4994 const unsigned char dhm_g[] =
4995 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004996
Hanno Beckere2defad2021-07-24 05:59:17 +01004997 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4998 dhm_p, sizeof( dhm_p ),
4999 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5000 {
5001 return( ret );
5002 }
5003 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005004#endif
5005
Ronald Cron6f135e12021-12-08 16:57:54 +01005006#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005007#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005008 mbedtls_ssl_conf_new_session_tickets(
5009 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5010#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005011 /*
5012 * Allow all TLS 1.3 key exchange modes by default.
5013 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005014 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005015#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005016
XiaokangQian4d3a6042022-04-21 13:46:17 +00005017 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5018 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005020 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005021 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005022#else
XiaokangQian060d8672022-04-21 09:24:56 +00005023 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005024#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005025 }
5026 else
5027 {
5028#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5029 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5030 {
5031 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5032 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5033 }
5034 else
5035 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5036 {
5037 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5038 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5039 }
5040#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5041 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5042 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5043#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5044 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5045 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5046#else
5047 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5048#endif
5049 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005050
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005051 /*
5052 * Preset-specific defaults
5053 */
5054 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005055 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005056 /*
5057 * NSA Suite B
5058 */
5059 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005060
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005061 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005062
5063#if defined(MBEDTLS_X509_CRT_PARSE_C)
5064 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005065#endif
5066
Ronald Crone68ab4f2022-10-05 12:46:29 +02005067#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005068#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5069 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5070 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5071 else
5072#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5073 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005074#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005075
Brett Warrene0edc842021-08-17 09:53:13 +01005076#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5077 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005078#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005079 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005080 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005081
5082 /*
5083 * Default
5084 */
5085 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005086
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005087 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005088
5089#if defined(MBEDTLS_X509_CRT_PARSE_C)
5090 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5091#endif
5092
Ronald Crone68ab4f2022-10-05 12:46:29 +02005093#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005094#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5095 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5096 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5097 else
5098#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5099 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005100#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005101
Brett Warrene0edc842021-08-17 09:53:13 +01005102#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5103 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005104#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005105 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005106
5107#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5108 conf->dhm_min_bitlen = 1024;
5109#endif
5110 }
5111
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005112 return( 0 );
5113}
5114
5115/*
5116 * Free mbedtls_ssl_config
5117 */
5118void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5119{
5120#if defined(MBEDTLS_DHM_C)
5121 mbedtls_mpi_free( &conf->dhm_P );
5122 mbedtls_mpi_free( &conf->dhm_G );
5123#endif
5124
Ronald Cron73fe8df2022-10-05 14:31:43 +02005125#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005126#if defined(MBEDTLS_USE_PSA_CRYPTO)
5127 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5128 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005129 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5130 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005131#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005132 if( conf->psk != NULL )
5133 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005134 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005135 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005136 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005137 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005138 }
5139
5140 if( conf->psk_identity != NULL )
5141 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005142 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005143 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005144 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005145 conf->psk_identity_len = 0;
5146 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005147#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005148
5149#if defined(MBEDTLS_X509_CRT_PARSE_C)
5150 ssl_key_cert_free( conf->key_cert );
5151#endif
5152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005153 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005154}
5155
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005156#if defined(MBEDTLS_PK_C) && \
5157 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005158/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163#if defined(MBEDTLS_RSA_C)
5164 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5165 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167#if defined(MBEDTLS_ECDSA_C)
5168 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5169 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005172}
5173
Hanno Becker7e5437a2017-04-28 17:15:26 +01005174unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5175{
5176 switch( type ) {
5177 case MBEDTLS_PK_RSA:
5178 return( MBEDTLS_SSL_SIG_RSA );
5179 case MBEDTLS_PK_ECDSA:
5180 case MBEDTLS_PK_ECKEY:
5181 return( MBEDTLS_SSL_SIG_ECDSA );
5182 default:
5183 return( MBEDTLS_SSL_SIG_ANON );
5184 }
5185}
5186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005188{
5189 switch( sig )
5190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191#if defined(MBEDTLS_RSA_C)
5192 case MBEDTLS_SSL_SIG_RSA:
5193 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195#if defined(MBEDTLS_ECDSA_C)
5196 case MBEDTLS_SSL_SIG_ECDSA:
5197 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005198#endif
5199 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005201 }
5202}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005203#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005204
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005205/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005206 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005209{
5210 switch( hash )
5211 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005212#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213 case MBEDTLS_SSL_HASH_MD5:
5214 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005215#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005216#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 case MBEDTLS_SSL_HASH_SHA1:
5218 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005219#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005220#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 case MBEDTLS_SSL_HASH_SHA224:
5222 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005223#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005224#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 case MBEDTLS_SSL_HASH_SHA256:
5226 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005227#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005228#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 case MBEDTLS_SSL_HASH_SHA384:
5230 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005231#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005232#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 case MBEDTLS_SSL_HASH_SHA512:
5234 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005235#endif
5236 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005238 }
5239}
5240
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005241/*
5242 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5243 */
5244unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5245{
5246 switch( md )
5247 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005248#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005249 case MBEDTLS_MD_MD5:
5250 return( MBEDTLS_SSL_HASH_MD5 );
5251#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005252#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005253 case MBEDTLS_MD_SHA1:
5254 return( MBEDTLS_SSL_HASH_SHA1 );
5255#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005256#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005257 case MBEDTLS_MD_SHA224:
5258 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005259#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005260#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005261 case MBEDTLS_MD_SHA256:
5262 return( MBEDTLS_SSL_HASH_SHA256 );
5263#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005264#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005265 case MBEDTLS_MD_SHA384:
5266 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005267#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005268#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005269 case MBEDTLS_MD_SHA512:
5270 return( MBEDTLS_SSL_HASH_SHA512 );
5271#endif
5272 default:
5273 return( MBEDTLS_SSL_HASH_NONE );
5274 }
5275}
5276
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005277/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005278 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005279 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005280 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005281int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005282{
Brett Warrene0edc842021-08-17 09:53:13 +01005283 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005284
Brett Warrene0edc842021-08-17 09:53:13 +01005285 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005286 return( -1 );
5287
Brett Warrene0edc842021-08-17 09:53:13 +01005288 for( ; *group_list != 0; group_list++ )
5289 {
5290 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005291 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005292 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005293
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005294 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005295}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005296
5297#if defined(MBEDTLS_ECP_C)
5298/*
5299 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5300 */
5301int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5302{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005303 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005304 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005305
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005306 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005307 return -1;
5308
5309 uint16_t tls_id = grp_info->tls_id;
5310
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005311 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5312}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005313#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315#if defined(MBEDTLS_X509_CRT_PARSE_C)
5316int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5317 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005318 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005319 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005320{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005321 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005322 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005323 const char *ext_oid;
5324 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005327 {
5328 /* Server part of the key exchange */
5329 switch( ciphersuite->key_exchange )
5330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 case MBEDTLS_KEY_EXCHANGE_RSA:
5332 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005333 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005334 break;
5335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5337 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5338 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5339 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005340 break;
5341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5343 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005344 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005345 break;
5346
5347 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 case MBEDTLS_KEY_EXCHANGE_NONE:
5349 case MBEDTLS_KEY_EXCHANGE_PSK:
5350 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5351 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005352 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005353 usage = 0;
5354 }
5355 }
5356 else
5357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5359 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005360 }
5361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005363 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005364 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005365 ret = -1;
5366 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5371 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005372 }
5373 else
5374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5376 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005377 }
5378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005380 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005381 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005382 ret = -1;
5383 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005384
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005385 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005388
Jerry Yu148165c2021-09-24 23:20:59 +08005389#if defined(MBEDTLS_USE_PSA_CRYPTO)
5390int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5391 const mbedtls_md_type_t md,
5392 unsigned char *dst,
5393 size_t dst_len,
5394 size_t *olen )
5395{
Ronald Cronf6893e12022-01-07 22:09:01 +01005396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5397 psa_hash_operation_t *hash_operation_to_clone;
5398 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5399
Jerry Yu148165c2021-09-24 23:20:59 +08005400 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005401
5402 switch( md )
5403 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005404#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005405 case MBEDTLS_MD_SHA384:
5406 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5407 break;
5408#endif
5409
Andrzej Kurek25f27152022-08-17 16:09:31 -04005410#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005411 case MBEDTLS_MD_SHA256:
5412 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5413 break;
5414#endif
5415
5416 default:
5417 goto exit;
5418 }
5419
5420 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5421 if( status != PSA_SUCCESS )
5422 goto exit;
5423
5424 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5425 if( status != PSA_SUCCESS )
5426 goto exit;
5427
5428exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005429#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5430 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5431 (void) ssl;
5432#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005433 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005434}
5435#else /* MBEDTLS_USE_PSA_CRYPTO */
5436
Andrzej Kurek25f27152022-08-17 16:09:31 -04005437#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005438MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005439static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5440 unsigned char *dst,
5441 size_t dst_len,
5442 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005443{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005444 int ret;
5445 mbedtls_sha512_context sha512;
5446
5447 if( dst_len < 48 )
5448 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5449
5450 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005451 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005452
5453 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5454 {
5455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5456 goto exit;
5457 }
5458
5459 *olen = 48;
5460
5461exit:
5462
5463 mbedtls_sha512_free( &sha512 );
5464 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005465}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005466#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005467
Andrzej Kurek25f27152022-08-17 16:09:31 -04005468#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005469MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005470static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5471 unsigned char *dst,
5472 size_t dst_len,
5473 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005474{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005475 int ret;
5476 mbedtls_sha256_context sha256;
5477
5478 if( dst_len < 32 )
5479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5480
5481 mbedtls_sha256_init( &sha256 );
5482 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005483
Jerry Yu24c0ec32021-09-09 14:21:07 +08005484 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5485 {
5486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5487 goto exit;
5488 }
5489
5490 *olen = 32;
5491
5492exit:
5493
5494 mbedtls_sha256_free( &sha256 );
5495 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005496}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005497#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005498
Jerry Yu000f9762021-09-14 11:12:51 +08005499int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5500 const mbedtls_md_type_t md,
5501 unsigned char *dst,
5502 size_t dst_len,
5503 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005504{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005505 switch( md )
5506 {
5507
Andrzej Kurek25f27152022-08-17 16:09:31 -04005508#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005509 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005510 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005511#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005512
Andrzej Kurek25f27152022-08-17 16:09:31 -04005513#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005514 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005515 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005516#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005517
5518 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005519#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5520 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5521 (void) ssl;
5522 (void) dst;
5523 (void) dst_len;
5524 (void) olen;
5525#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005526 break;
5527 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005529}
XiaokangQian647719a2021-12-07 09:16:29 +00005530
Jerry Yu148165c2021-09-24 23:20:59 +08005531#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005532
Ronald Crone68ab4f2022-10-05 12:46:29 +02005533#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005534/* mbedtls_ssl_parse_sig_alg_ext()
5535 *
5536 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5537 * value (TLS 1.3 RFC8446):
5538 * enum {
5539 * ....
5540 * ecdsa_secp256r1_sha256( 0x0403 ),
5541 * ecdsa_secp384r1_sha384( 0x0503 ),
5542 * ecdsa_secp521r1_sha512( 0x0603 ),
5543 * ....
5544 * } SignatureScheme;
5545 *
5546 * struct {
5547 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5548 * } SignatureSchemeList;
5549 *
5550 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5551 * value (TLS 1.2 RFC5246):
5552 * enum {
5553 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5554 * sha512(6), (255)
5555 * } HashAlgorithm;
5556 *
5557 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5558 * SignatureAlgorithm;
5559 *
5560 * struct {
5561 * HashAlgorithm hash;
5562 * SignatureAlgorithm signature;
5563 * } SignatureAndHashAlgorithm;
5564 *
5565 * SignatureAndHashAlgorithm
5566 * supported_signature_algorithms<2..2^16-2>;
5567 *
5568 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5569 * generalization of the TLS 1.2 signature algorithm extension.
5570 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5571 * `SignatureScheme` field of TLS 1.3
5572 *
5573 */
5574int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5575 const unsigned char *buf,
5576 const unsigned char *end )
5577{
5578 const unsigned char *p = buf;
5579 size_t supported_sig_algs_len = 0;
5580 const unsigned char *supported_sig_algs_end;
5581 uint16_t sig_alg;
5582 uint32_t common_idx = 0;
5583
5584 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5585 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5586 p += 2;
5587
5588 memset( ssl->handshake->received_sig_algs, 0,
5589 sizeof(ssl->handshake->received_sig_algs) );
5590
5591 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5592 supported_sig_algs_end = p + supported_sig_algs_len;
5593 while( p < supported_sig_algs_end )
5594 {
5595 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5596 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5597 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005598 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5599 sig_alg,
5600 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005601#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005602 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005603 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5604 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5605 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005606 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005607 }
5608#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005609
Jerry Yuf3b46b52022-06-19 16:52:27 +08005610 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5611 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005612
5613 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5614 {
5615 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5616 common_idx += 1;
5617 }
5618 }
5619 /* Check that we consumed all the message. */
5620 if( p != end )
5621 {
5622 MBEDTLS_SSL_DEBUG_MSG( 1,
5623 ( "Signature algorithms extension length misaligned" ) );
5624 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5625 MBEDTLS_ERR_SSL_DECODE_ERROR );
5626 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5627 }
5628
5629 if( common_idx == 0 )
5630 {
5631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5632 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5633 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5634 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5635 }
5636
Gabor Mezei15b95a62022-05-09 16:37:58 +02005637 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005638 return( 0 );
5639}
5640
Ronald Crone68ab4f2022-10-05 12:46:29 +02005641#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005642
Jerry Yudc7bd172022-02-17 13:44:15 +08005643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5644
5645#if defined(MBEDTLS_USE_PSA_CRYPTO)
5646
5647static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5648 mbedtls_svc_key_id_t key,
5649 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005650 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005651 const unsigned char* seed, size_t seed_length,
5652 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005653 const unsigned char* other_secret,
5654 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005655 size_t capacity )
5656{
5657 psa_status_t status;
5658
5659 status = psa_key_derivation_setup( derivation, alg );
5660 if( status != PSA_SUCCESS )
5661 return( status );
5662
5663 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5664 {
5665 status = psa_key_derivation_input_bytes( derivation,
5666 PSA_KEY_DERIVATION_INPUT_SEED,
5667 seed, seed_length );
5668 if( status != PSA_SUCCESS )
5669 return( status );
5670
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005671 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005672 {
5673 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005674 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5675 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005676 if( status != PSA_SUCCESS )
5677 return( status );
5678 }
5679
Jerry Yudc7bd172022-02-17 13:44:15 +08005680 if( mbedtls_svc_key_id_is_null( key ) )
5681 {
5682 status = psa_key_derivation_input_bytes(
5683 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005684 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005685 }
5686 else
5687 {
5688 status = psa_key_derivation_input_key(
5689 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5690 }
5691 if( status != PSA_SUCCESS )
5692 return( status );
5693
5694 status = psa_key_derivation_input_bytes( derivation,
5695 PSA_KEY_DERIVATION_INPUT_LABEL,
5696 label, label_length );
5697 if( status != PSA_SUCCESS )
5698 return( status );
5699 }
5700 else
5701 {
5702 return( PSA_ERROR_NOT_SUPPORTED );
5703 }
5704
5705 status = psa_key_derivation_set_capacity( derivation, capacity );
5706 if( status != PSA_SUCCESS )
5707 return( status );
5708
5709 return( PSA_SUCCESS );
5710}
5711
Andrzej Kurek57d10632022-10-24 10:32:01 -04005712#if defined(PSA_WANT_ALG_SHA_384) || \
5713 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005714MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005715static int tls_prf_generic( mbedtls_md_type_t md_type,
5716 const unsigned char *secret, size_t slen,
5717 const char *label,
5718 const unsigned char *random, size_t rlen,
5719 unsigned char *dstbuf, size_t dlen )
5720{
5721 psa_status_t status;
5722 psa_algorithm_t alg;
5723 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5724 psa_key_derivation_operation_t derivation =
5725 PSA_KEY_DERIVATION_OPERATION_INIT;
5726
5727 if( md_type == MBEDTLS_MD_SHA384 )
5728 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5729 else
5730 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5731
5732 /* Normally a "secret" should be long enough to be impossible to
5733 * find by brute force, and in particular should not be empty. But
5734 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5735 * and for this use case it makes sense to have a 0-length "secret".
5736 * Since the key API doesn't allow importing a key of length 0,
5737 * keep master_key=0, which setup_psa_key_derivation() understands
5738 * to mean a 0-length "secret" input. */
5739 if( slen != 0 )
5740 {
5741 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5742 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5743 psa_set_key_algorithm( &key_attributes, alg );
5744 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5745
5746 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5747 if( status != PSA_SUCCESS )
5748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5749 }
5750
5751 status = setup_psa_key_derivation( &derivation,
5752 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005753 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005754 random, rlen,
5755 (unsigned char const *) label,
5756 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005757 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005758 dlen );
5759 if( status != PSA_SUCCESS )
5760 {
5761 psa_key_derivation_abort( &derivation );
5762 psa_destroy_key( master_key );
5763 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5764 }
5765
5766 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5767 if( status != PSA_SUCCESS )
5768 {
5769 psa_key_derivation_abort( &derivation );
5770 psa_destroy_key( master_key );
5771 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5772 }
5773
5774 status = psa_key_derivation_abort( &derivation );
5775 if( status != PSA_SUCCESS )
5776 {
5777 psa_destroy_key( master_key );
5778 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5779 }
5780
5781 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5782 status = psa_destroy_key( master_key );
5783 if( status != PSA_SUCCESS )
5784 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5785
5786 return( 0 );
5787}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005788#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005789#else /* MBEDTLS_USE_PSA_CRYPTO */
5790
Andrzej Kurek57d10632022-10-24 10:32:01 -04005791#if defined(MBEDTLS_MD_C) && \
5792 ( defined(MBEDTLS_SHA256_C) || \
5793 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005794MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005795static int tls_prf_generic( mbedtls_md_type_t md_type,
5796 const unsigned char *secret, size_t slen,
5797 const char *label,
5798 const unsigned char *random, size_t rlen,
5799 unsigned char *dstbuf, size_t dlen )
5800{
5801 size_t nb;
5802 size_t i, j, k, md_len;
5803 unsigned char *tmp;
5804 size_t tmp_len = 0;
5805 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5806 const mbedtls_md_info_t *md_info;
5807 mbedtls_md_context_t md_ctx;
5808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5809
5810 mbedtls_md_init( &md_ctx );
5811
5812 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5813 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5814
5815 md_len = mbedtls_md_get_size( md_info );
5816
5817 tmp_len = md_len + strlen( label ) + rlen;
5818 tmp = mbedtls_calloc( 1, tmp_len );
5819 if( tmp == NULL )
5820 {
5821 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5822 goto exit;
5823 }
5824
5825 nb = strlen( label );
5826 memcpy( tmp + md_len, label, nb );
5827 memcpy( tmp + md_len + nb, random, rlen );
5828 nb += rlen;
5829
5830 /*
5831 * Compute P_<hash>(secret, label + random)[0..dlen]
5832 */
5833 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5834 goto exit;
5835
5836 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5837 if( ret != 0 )
5838 goto exit;
5839 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5840 if( ret != 0 )
5841 goto exit;
5842 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5843 if( ret != 0 )
5844 goto exit;
5845
5846 for( i = 0; i < dlen; i += md_len )
5847 {
5848 ret = mbedtls_md_hmac_reset ( &md_ctx );
5849 if( ret != 0 )
5850 goto exit;
5851 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5852 if( ret != 0 )
5853 goto exit;
5854 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5855 if( ret != 0 )
5856 goto exit;
5857
5858 ret = mbedtls_md_hmac_reset ( &md_ctx );
5859 if( ret != 0 )
5860 goto exit;
5861 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5862 if( ret != 0 )
5863 goto exit;
5864 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5865 if( ret != 0 )
5866 goto exit;
5867
5868 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5869
5870 for( j = 0; j < k; j++ )
5871 dstbuf[i + j] = h_i[j];
5872 }
5873
5874exit:
5875 mbedtls_md_free( &md_ctx );
5876
5877 mbedtls_platform_zeroize( tmp, tmp_len );
5878 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5879
5880 mbedtls_free( tmp );
5881
5882 return( ret );
5883}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005884#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005885#endif /* MBEDTLS_USE_PSA_CRYPTO */
5886
Andrzej Kurek25f27152022-08-17 16:09:31 -04005887#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005888MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005889static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5890 const char *label,
5891 const unsigned char *random, size_t rlen,
5892 unsigned char *dstbuf, size_t dlen )
5893{
5894 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5895 label, random, rlen, dstbuf, dlen ) );
5896}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005897#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005898
Andrzej Kurek25f27152022-08-17 16:09:31 -04005899#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005900MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005901static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5902 const char *label,
5903 const unsigned char *random, size_t rlen,
5904 unsigned char *dstbuf, size_t dlen )
5905{
5906 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5907 label, random, rlen, dstbuf, dlen ) );
5908}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005909#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005910
Jerry Yuf009d862022-02-17 14:01:37 +08005911/*
5912 * Set appropriate PRF function and other SSL / TLS1.2 functions
5913 *
5914 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005915 * - hash associated with the ciphersuite (only used by TLS 1.2)
5916 *
5917 * Outputs:
5918 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5919 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005920MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005921static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005922 mbedtls_md_type_t hash )
5923{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005924#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005925 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005926 {
5927 handshake->tls_prf = tls_prf_sha384;
5928 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5929 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5930 }
5931 else
5932#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005933#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005934 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005935 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005936 handshake->tls_prf = tls_prf_sha256;
5937 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5938 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5939 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005940#else
Jerry Yuf009d862022-02-17 14:01:37 +08005941 {
Jerry Yuf009d862022-02-17 14:01:37 +08005942 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005943 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005944 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5945 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005946#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005947
5948 return( 0 );
5949}
Jerry Yud6ab2352022-02-17 14:03:43 +08005950
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005951/*
5952 * Compute master secret if needed
5953 *
5954 * Parameters:
5955 * [in/out] handshake
5956 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5957 * (PSA-PSK) ciphersuite_info, psk_opaque
5958 * [out] premaster (cleared)
5959 * [out] master
5960 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5961 * debug: conf->f_dbg, conf->p_dbg
5962 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005963 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005964 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005965MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005966static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5967 unsigned char *master,
5968 const mbedtls_ssl_context *ssl )
5969{
5970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5971
5972 /* cf. RFC 5246, Section 8.1:
5973 * "The master secret is always exactly 48 bytes in length." */
5974 size_t const master_secret_len = 48;
5975
5976#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5977 unsigned char session_hash[48];
5978#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5979
5980 /* The label for the KDF used for key expansion.
5981 * This is either "master secret" or "extended master secret"
5982 * depending on whether the Extended Master Secret extension
5983 * is used. */
5984 char const *lbl = "master secret";
5985
Przemek Stekielae4ed302022-04-05 17:15:55 +02005986 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005987 * - If the Extended Master Secret extension is not used,
5988 * this is ClientHello.Random + ServerHello.Random
5989 * (see Sect. 8.1 in RFC 5246).
5990 * - If the Extended Master Secret extension is used,
5991 * this is the transcript of the handshake so far.
5992 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005993 unsigned char const *seed = handshake->randbytes;
5994 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005995
5996#if !defined(MBEDTLS_DEBUG_C) && \
5997 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5998 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5999 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6000 ssl = NULL; /* make sure we don't use it except for those cases */
6001 (void) ssl;
6002#endif
6003
6004 if( handshake->resume != 0 )
6005 {
6006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6007 return( 0 );
6008 }
6009
6010#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6011 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6012 {
6013 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006014 seed = session_hash;
6015 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006016
6017 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006018 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006019 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006020#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006021
Przemek Stekiel99114f32022-04-22 11:20:09 +02006022#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006023 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006024 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006025 {
6026 /* Perform PSK-to-MS expansion in a single step. */
6027 psa_status_t status;
6028 psa_algorithm_t alg;
6029 mbedtls_svc_key_id_t psk;
6030 psa_key_derivation_operation_t derivation =
6031 PSA_KEY_DERIVATION_OPERATION_INIT;
6032 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6033
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6035
6036 psk = mbedtls_ssl_get_opaque_psk( ssl );
6037
6038 if( hash_alg == MBEDTLS_MD_SHA384 )
6039 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6040 else
6041 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6042
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006043 size_t other_secret_len = 0;
6044 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006045
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006046 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006047 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006048 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006049 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006050 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006051 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006052 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006053 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006054 other_secret_len = 48;
6055 other_secret = handshake->premaster + 2;
6056 break;
6057 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006058 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006059 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6060 other_secret = handshake->premaster + 2;
6061 break;
6062 default:
6063 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006064 }
6065
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006066 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006067 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006068 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006069 (unsigned char const *) lbl,
6070 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006071 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006072 master_secret_len );
6073 if( status != PSA_SUCCESS )
6074 {
6075 psa_key_derivation_abort( &derivation );
6076 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6077 }
6078
6079 status = psa_key_derivation_output_bytes( &derivation,
6080 master,
6081 master_secret_len );
6082 if( status != PSA_SUCCESS )
6083 {
6084 psa_key_derivation_abort( &derivation );
6085 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6086 }
6087
6088 status = psa_key_derivation_abort( &derivation );
6089 if( status != PSA_SUCCESS )
6090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6091 }
6092 else
6093#endif
6094 {
6095 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006096 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006097 master,
6098 master_secret_len );
6099 if( ret != 0 )
6100 {
6101 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6102 return( ret );
6103 }
6104
6105 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6106 handshake->premaster,
6107 handshake->pmslen );
6108
6109 mbedtls_platform_zeroize( handshake->premaster,
6110 sizeof(handshake->premaster) );
6111 }
6112
6113 return( 0 );
6114}
6115
Jerry Yud62f87e2022-02-17 14:09:02 +08006116int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6117{
6118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6119 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6120 ssl->handshake->ciphersuite_info;
6121
6122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6123
6124 /* Set PRF, calc_verify and calc_finished function pointers */
6125 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006126 ciphersuite_info->mac );
6127 if( ret != 0 )
6128 {
6129 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6130 return( ret );
6131 }
6132
6133 /* Compute master secret if needed */
6134 ret = ssl_compute_master( ssl->handshake,
6135 ssl->session_negotiate->master,
6136 ssl );
6137 if( ret != 0 )
6138 {
6139 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6140 return( ret );
6141 }
6142
6143 /* Swap the client and server random values:
6144 * - MS derivation wanted client+server (RFC 5246 8.1)
6145 * - key derivation wants server+client (RFC 5246 6.3) */
6146 {
6147 unsigned char tmp[64];
6148 memcpy( tmp, ssl->handshake->randbytes, 64 );
6149 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6150 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6151 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6152 }
6153
6154 /* Populate transform structure */
6155 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6156 ssl->session_negotiate->ciphersuite,
6157 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006158#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006159 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006160#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006161 ssl->handshake->tls_prf,
6162 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006163 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006164 ssl->conf->endpoint,
6165 ssl );
6166 if( ret != 0 )
6167 {
6168 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6169 return( ret );
6170 }
6171
6172 /* We no longer need Server/ClientHello.random values */
6173 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6174 sizeof( ssl->handshake->randbytes ) );
6175
6176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6177
6178 return( 0 );
6179}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006180
Ronald Cron4dcbca92022-03-07 10:21:40 +01006181int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6182{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006183 switch( md )
6184 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006185#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006186 case MBEDTLS_SSL_HASH_SHA384:
6187 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6188 break;
6189#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006190#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006191 case MBEDTLS_SSL_HASH_SHA256:
6192 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6193 break;
6194#endif
6195 default:
6196 return( -1 );
6197 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006198#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6199 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6200 (void) ssl;
6201#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006202 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006203}
6204
Andrzej Kurek25f27152022-08-17 16:09:31 -04006205#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006206void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6207 unsigned char *hash,
6208 size_t *hlen )
6209{
6210#if defined(MBEDTLS_USE_PSA_CRYPTO)
6211 size_t hash_size;
6212 psa_status_t status;
6213 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6214
6215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6216 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6217 if( status != PSA_SUCCESS )
6218 {
6219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6220 return;
6221 }
6222
6223 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6224 if( status != PSA_SUCCESS )
6225 {
6226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6227 return;
6228 }
6229
6230 *hlen = 32;
6231 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6233#else
6234 mbedtls_sha256_context sha256;
6235
6236 mbedtls_sha256_init( &sha256 );
6237
6238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6239
6240 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6241 mbedtls_sha256_finish( &sha256, hash );
6242
6243 *hlen = 32;
6244
6245 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6247
6248 mbedtls_sha256_free( &sha256 );
6249#endif /* MBEDTLS_USE_PSA_CRYPTO */
6250 return;
6251}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006252#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006253
Andrzej Kurek25f27152022-08-17 16:09:31 -04006254#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006255void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6256 unsigned char *hash,
6257 size_t *hlen )
6258{
6259#if defined(MBEDTLS_USE_PSA_CRYPTO)
6260 size_t hash_size;
6261 psa_status_t status;
6262 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6263
6264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6265 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6266 if( status != PSA_SUCCESS )
6267 {
6268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6269 return;
6270 }
6271
6272 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6273 if( status != PSA_SUCCESS )
6274 {
6275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6276 return;
6277 }
6278
6279 *hlen = 48;
6280 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6282#else
6283 mbedtls_sha512_context sha512;
6284
6285 mbedtls_sha512_init( &sha512 );
6286
6287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6288
Andrzej Kureka242e832022-08-11 10:03:14 -04006289 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006290 mbedtls_sha512_finish( &sha512, hash );
6291
6292 *hlen = 48;
6293
6294 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6296
6297 mbedtls_sha512_free( &sha512 );
6298#endif /* MBEDTLS_USE_PSA_CRYPTO */
6299 return;
6300}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006301#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006302
Neil Armstrong80f6f322022-05-03 17:56:38 +02006303#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6304 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006305int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6306{
6307 unsigned char *p = ssl->handshake->premaster;
6308 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6309 const unsigned char *psk = NULL;
6310 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006311 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006312
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006313 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006314 {
6315 /*
6316 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006317 * checked before calling this function.
6318 *
6319 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006320 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006321 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006322 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6323 {
6324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6326 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006327 }
6328
6329 /*
6330 * PMS = struct {
6331 * opaque other_secret<0..2^16-1>;
6332 * opaque psk<0..2^16-1>;
6333 * };
6334 * with "other_secret" depending on the particular key exchange
6335 */
6336#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6337 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6338 {
6339 if( end - p < 2 )
6340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6341
6342 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6343 p += 2;
6344
6345 if( end < p || (size_t)( end - p ) < psk_len )
6346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6347
6348 memset( p, 0, psk_len );
6349 p += psk_len;
6350 }
6351 else
6352#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6353#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6354 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6355 {
6356 /*
6357 * other_secret already set by the ClientKeyExchange message,
6358 * and is 48 bytes long
6359 */
6360 if( end - p < 2 )
6361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6362
6363 *p++ = 0;
6364 *p++ = 48;
6365 p += 48;
6366 }
6367 else
6368#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6369#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6370 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6371 {
6372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6373 size_t len;
6374
6375 /* Write length only when we know the actual value */
6376 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6377 p + 2, end - ( p + 2 ), &len,
6378 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6379 {
6380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6381 return( ret );
6382 }
6383 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6384 p += 2 + len;
6385
6386 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6387 }
6388 else
6389#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006390#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006391 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6392 {
6393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6394 size_t zlen;
6395
6396 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6397 p + 2, end - ( p + 2 ),
6398 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6399 {
6400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6401 return( ret );
6402 }
6403
6404 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6405 p += 2 + zlen;
6406
6407 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6408 MBEDTLS_DEBUG_ECDH_Z );
6409 }
6410 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006411#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006412 {
6413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6415 }
6416
6417 /* opaque psk<0..2^16-1>; */
6418 if( end - p < 2 )
6419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6420
6421 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6422 p += 2;
6423
6424 if( end < p || (size_t)( end - p ) < psk_len )
6425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6426
6427 memcpy( p, psk, psk_len );
6428 p += psk_len;
6429
6430 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6431
6432 return( 0 );
6433}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006434#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006435
6436#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006437MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006438static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6439
6440#if defined(MBEDTLS_SSL_PROTO_DTLS)
6441int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6442{
6443 /* If renegotiation is not enforced, retransmit until we would reach max
6444 * timeout if we were using the usual handshake doubling scheme */
6445 if( ssl->conf->renego_max_records < 0 )
6446 {
6447 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6448 unsigned char doublings = 1;
6449
6450 while( ratio != 0 )
6451 {
6452 ++doublings;
6453 ratio >>= 1;
6454 }
6455
6456 if( ++ssl->renego_records_seen > doublings )
6457 {
6458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6459 return( 0 );
6460 }
6461 }
6462
6463 return( ssl_write_hello_request( ssl ) );
6464}
6465#endif
6466#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006467
Jerry Yud9526692022-02-17 14:23:47 +08006468/*
6469 * Handshake functions
6470 */
6471#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6472/* No certificate support -> dummy functions */
6473int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6474{
6475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6476 ssl->handshake->ciphersuite_info;
6477
6478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6479
6480 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6481 {
6482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6483 ssl->state++;
6484 return( 0 );
6485 }
6486
6487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6489}
6490
6491int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6492{
6493 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6494 ssl->handshake->ciphersuite_info;
6495
6496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6497
6498 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6499 {
6500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6501 ssl->state++;
6502 return( 0 );
6503 }
6504
6505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6507}
6508
6509#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6510/* Some certificate support -> implement write and parse */
6511
6512int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6513{
6514 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6515 size_t i, n;
6516 const mbedtls_x509_crt *crt;
6517 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6518 ssl->handshake->ciphersuite_info;
6519
6520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6521
6522 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6523 {
6524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6525 ssl->state++;
6526 return( 0 );
6527 }
6528
6529#if defined(MBEDTLS_SSL_CLI_C)
6530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6531 {
6532 if( ssl->handshake->client_auth == 0 )
6533 {
6534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6535 ssl->state++;
6536 return( 0 );
6537 }
6538 }
6539#endif /* MBEDTLS_SSL_CLI_C */
6540#if defined(MBEDTLS_SSL_SRV_C)
6541 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6542 {
6543 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6544 {
6545 /* Should never happen because we shouldn't have picked the
6546 * ciphersuite if we don't have a certificate. */
6547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6548 }
6549 }
6550#endif
6551
6552 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6553
6554 /*
6555 * 0 . 0 handshake type
6556 * 1 . 3 handshake length
6557 * 4 . 6 length of all certs
6558 * 7 . 9 length of cert. 1
6559 * 10 . n-1 peer certificate
6560 * n . n+2 length of cert. 2
6561 * n+3 . ... upper level cert, etc.
6562 */
6563 i = 7;
6564 crt = mbedtls_ssl_own_cert( ssl );
6565
6566 while( crt != NULL )
6567 {
6568 n = crt->raw.len;
6569 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6570 {
6571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6572 " > %" MBEDTLS_PRINTF_SIZET,
6573 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6574 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6575 }
6576
6577 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6578 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6579 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6580
6581 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6582 i += n; crt = crt->next;
6583 }
6584
6585 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6586 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6587 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6588
6589 ssl->out_msglen = i;
6590 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6591 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6592
6593 ssl->state++;
6594
6595 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6596 {
6597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6598 return( ret );
6599 }
6600
6601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6602
6603 return( ret );
6604}
6605
6606#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6607
6608#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006609MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006610static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6611 unsigned char *crt_buf,
6612 size_t crt_buf_len )
6613{
6614 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6615
6616 if( peer_crt == NULL )
6617 return( -1 );
6618
6619 if( peer_crt->raw.len != crt_buf_len )
6620 return( -1 );
6621
6622 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6623}
6624#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006625MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006626static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6627 unsigned char *crt_buf,
6628 size_t crt_buf_len )
6629{
6630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6631 unsigned char const * const peer_cert_digest =
6632 ssl->session->peer_cert_digest;
6633 mbedtls_md_type_t const peer_cert_digest_type =
6634 ssl->session->peer_cert_digest_type;
6635 mbedtls_md_info_t const * const digest_info =
6636 mbedtls_md_info_from_type( peer_cert_digest_type );
6637 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6638 size_t digest_len;
6639
6640 if( peer_cert_digest == NULL || digest_info == NULL )
6641 return( -1 );
6642
6643 digest_len = mbedtls_md_get_size( digest_info );
6644 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6645 return( -1 );
6646
6647 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6648 if( ret != 0 )
6649 return( -1 );
6650
6651 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6652}
6653#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6654#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6655
6656/*
6657 * Once the certificate message is read, parse it into a cert chain and
6658 * perform basic checks, but leave actual verification to the caller
6659 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006660MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006661static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6662 mbedtls_x509_crt *chain )
6663{
6664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6665#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6666 int crt_cnt=0;
6667#endif
6668 size_t i, n;
6669 uint8_t alert;
6670
6671 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6672 {
6673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6674 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6675 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6676 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6677 }
6678
6679 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6680 {
6681 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6682 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6683 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6684 }
6685
6686 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6687 {
6688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6689 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6690 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6691 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6692 }
6693
6694 i = mbedtls_ssl_hs_hdr_len( ssl );
6695
6696 /*
6697 * Same message structure as in mbedtls_ssl_write_certificate()
6698 */
6699 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6700
6701 if( ssl->in_msg[i] != 0 ||
6702 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6703 {
6704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6706 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6707 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6708 }
6709
6710 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6711 i += 3;
6712
6713 /* Iterate through and parse the CRTs in the provided chain. */
6714 while( i < ssl->in_hslen )
6715 {
6716 /* Check that there's room for the next CRT's length fields. */
6717 if ( i + 3 > ssl->in_hslen ) {
6718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6719 mbedtls_ssl_send_alert_message( ssl,
6720 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6721 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6722 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6723 }
6724 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6725 * anything beyond 2**16 ~ 64K. */
6726 if( ssl->in_msg[i] != 0 )
6727 {
6728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6729 mbedtls_ssl_send_alert_message( ssl,
6730 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6731 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6732 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6733 }
6734
6735 /* Read length of the next CRT in the chain. */
6736 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6737 | (unsigned int) ssl->in_msg[i + 2];
6738 i += 3;
6739
6740 if( n < 128 || i + n > ssl->in_hslen )
6741 {
6742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6743 mbedtls_ssl_send_alert_message( ssl,
6744 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6745 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6746 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6747 }
6748
6749 /* Check if we're handling the first CRT in the chain. */
6750#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6751 if( crt_cnt++ == 0 &&
6752 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6753 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6754 {
6755 /* During client-side renegotiation, check that the server's
6756 * end-CRTs hasn't changed compared to the initial handshake,
6757 * mitigating the triple handshake attack. On success, reuse
6758 * the original end-CRT instead of parsing it again. */
6759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6760 if( ssl_check_peer_crt_unchanged( ssl,
6761 &ssl->in_msg[i],
6762 n ) != 0 )
6763 {
6764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6765 mbedtls_ssl_send_alert_message( ssl,
6766 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6767 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6768 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6769 }
6770
6771 /* Now we can safely free the original chain. */
6772 ssl_clear_peer_cert( ssl->session );
6773 }
6774#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6775
6776 /* Parse the next certificate in the chain. */
6777#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6778 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6779#else
6780 /* If we don't need to store the CRT chain permanently, parse
6781 * it in-place from the input buffer instead of making a copy. */
6782 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6783#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6784 switch( ret )
6785 {
6786 case 0: /*ok*/
6787 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6788 /* Ignore certificate with an unknown algorithm: maybe a
6789 prior certificate was already trusted. */
6790 break;
6791
6792 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6793 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6794 goto crt_parse_der_failed;
6795
6796 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6797 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6798 goto crt_parse_der_failed;
6799
6800 default:
6801 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6802 crt_parse_der_failed:
6803 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6804 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6805 return( ret );
6806 }
6807
6808 i += n;
6809 }
6810
6811 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6812 return( 0 );
6813}
6814
6815#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006816MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006817static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6818{
6819 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6820 return( -1 );
6821
6822 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6823 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6824 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6825 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6826 {
Ronald Cron19385882022-06-15 16:26:13 +02006827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006828 return( 0 );
6829 }
6830 return( -1 );
6831}
6832#endif /* MBEDTLS_SSL_SRV_C */
6833
6834/* Check if a certificate message is expected.
6835 * Return either
6836 * - SSL_CERTIFICATE_EXPECTED, or
6837 * - SSL_CERTIFICATE_SKIP
6838 * indicating whether a Certificate message is expected or not.
6839 */
6840#define SSL_CERTIFICATE_EXPECTED 0
6841#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006842MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006843static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6844 int authmode )
6845{
6846 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6847 ssl->handshake->ciphersuite_info;
6848
6849 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6850 return( SSL_CERTIFICATE_SKIP );
6851
6852#if defined(MBEDTLS_SSL_SRV_C)
6853 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6854 {
6855 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6856 return( SSL_CERTIFICATE_SKIP );
6857
6858 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6859 {
6860 ssl->session_negotiate->verify_result =
6861 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6862 return( SSL_CERTIFICATE_SKIP );
6863 }
6864 }
6865#else
6866 ((void) authmode);
6867#endif /* MBEDTLS_SSL_SRV_C */
6868
6869 return( SSL_CERTIFICATE_EXPECTED );
6870}
6871
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006872MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006873static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6874 int authmode,
6875 mbedtls_x509_crt *chain,
6876 void *rs_ctx )
6877{
6878 int ret = 0;
6879 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6880 ssl->handshake->ciphersuite_info;
6881 int have_ca_chain = 0;
6882
6883 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6884 void *p_vrfy;
6885
6886 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6887 return( 0 );
6888
6889 if( ssl->f_vrfy != NULL )
6890 {
6891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6892 f_vrfy = ssl->f_vrfy;
6893 p_vrfy = ssl->p_vrfy;
6894 }
6895 else
6896 {
6897 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6898 f_vrfy = ssl->conf->f_vrfy;
6899 p_vrfy = ssl->conf->p_vrfy;
6900 }
6901
6902 /*
6903 * Main check: verify certificate
6904 */
6905#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6906 if( ssl->conf->f_ca_cb != NULL )
6907 {
6908 ((void) rs_ctx);
6909 have_ca_chain = 1;
6910
6911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6912 ret = mbedtls_x509_crt_verify_with_ca_cb(
6913 chain,
6914 ssl->conf->f_ca_cb,
6915 ssl->conf->p_ca_cb,
6916 ssl->conf->cert_profile,
6917 ssl->hostname,
6918 &ssl->session_negotiate->verify_result,
6919 f_vrfy, p_vrfy );
6920 }
6921 else
6922#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6923 {
6924 mbedtls_x509_crt *ca_chain;
6925 mbedtls_x509_crl *ca_crl;
6926
6927#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6928 if( ssl->handshake->sni_ca_chain != NULL )
6929 {
6930 ca_chain = ssl->handshake->sni_ca_chain;
6931 ca_crl = ssl->handshake->sni_ca_crl;
6932 }
6933 else
6934#endif
6935 {
6936 ca_chain = ssl->conf->ca_chain;
6937 ca_crl = ssl->conf->ca_crl;
6938 }
6939
6940 if( ca_chain != NULL )
6941 have_ca_chain = 1;
6942
6943 ret = mbedtls_x509_crt_verify_restartable(
6944 chain,
6945 ca_chain, ca_crl,
6946 ssl->conf->cert_profile,
6947 ssl->hostname,
6948 &ssl->session_negotiate->verify_result,
6949 f_vrfy, p_vrfy, rs_ctx );
6950 }
6951
6952 if( ret != 0 )
6953 {
6954 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6955 }
6956
6957#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6958 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6959 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6960#endif
6961
6962 /*
6963 * Secondary checks: always done, but change 'ret' only if it was 0
6964 */
6965
6966#if defined(MBEDTLS_ECP_C)
6967 {
6968 const mbedtls_pk_context *pk = &chain->pk;
6969
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006970 /* If certificate uses an EC key, make sure the curve is OK.
6971 * This is a public key, so it can't be opaque, so can_do() is a good
6972 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006973 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006974 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006975 /* and in the unlikely case the above assumption no longer holds
6976 * we are making sure that pk_ec() here does not return a NULL
6977 */
6978 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6979 if( ec == NULL )
6980 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6983 }
Jerry Yud9526692022-02-17 14:23:47 +08006984
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006985 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6986 {
6987 ssl->session_negotiate->verify_result |=
6988 MBEDTLS_X509_BADCERT_BAD_KEY;
6989
6990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6991 if( ret == 0 )
6992 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6993 }
Jerry Yud9526692022-02-17 14:23:47 +08006994 }
6995 }
6996#endif /* MBEDTLS_ECP_C */
6997
6998 if( mbedtls_ssl_check_cert_usage( chain,
6999 ciphersuite_info,
7000 ! ssl->conf->endpoint,
7001 &ssl->session_negotiate->verify_result ) != 0 )
7002 {
7003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7004 if( ret == 0 )
7005 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7006 }
7007
7008 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7009 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7010 * with details encoded in the verification flags. All other kinds
7011 * of error codes, including those from the user provided f_vrfy
7012 * functions, are treated as fatal and lead to a failure of
7013 * ssl_parse_certificate even if verification was optional. */
7014 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7015 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7016 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7017 {
7018 ret = 0;
7019 }
7020
7021 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7022 {
7023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7024 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7025 }
7026
7027 if( ret != 0 )
7028 {
7029 uint8_t alert;
7030
7031 /* The certificate may have been rejected for several reasons.
7032 Pick one and send the corresponding alert. Which alert to send
7033 may be a subject of debate in some cases. */
7034 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7035 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7036 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7037 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7038 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7039 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7040 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7041 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7042 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7043 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7044 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7045 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7046 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7047 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7048 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7049 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7050 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7051 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7052 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7053 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7054 else
7055 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7056 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7057 alert );
7058 }
7059
7060#if defined(MBEDTLS_DEBUG_C)
7061 if( ssl->session_negotiate->verify_result != 0 )
7062 {
7063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7064 (unsigned int) ssl->session_negotiate->verify_result ) );
7065 }
7066 else
7067 {
7068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7069 }
7070#endif /* MBEDTLS_DEBUG_C */
7071
7072 return( ret );
7073}
7074
7075#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007076MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007077static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7078 unsigned char *start, size_t len )
7079{
7080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7081 /* Remember digest of the peer's end-CRT. */
7082 ssl->session_negotiate->peer_cert_digest =
7083 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7084 if( ssl->session_negotiate->peer_cert_digest == NULL )
7085 {
7086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7087 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7088 mbedtls_ssl_send_alert_message( ssl,
7089 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7090 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7091
7092 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7093 }
7094
7095 ret = mbedtls_md( mbedtls_md_info_from_type(
7096 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7097 start, len,
7098 ssl->session_negotiate->peer_cert_digest );
7099
7100 ssl->session_negotiate->peer_cert_digest_type =
7101 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7102 ssl->session_negotiate->peer_cert_digest_len =
7103 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7104
7105 return( ret );
7106}
7107
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007108MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007109static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7110 unsigned char *start, size_t len )
7111{
7112 unsigned char *end = start + len;
7113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7114
7115 /* Make a copy of the peer's raw public key. */
7116 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7117 ret = mbedtls_pk_parse_subpubkey( &start, end,
7118 &ssl->handshake->peer_pubkey );
7119 if( ret != 0 )
7120 {
7121 /* We should have parsed the public key before. */
7122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7123 }
7124
7125 return( 0 );
7126}
7127#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7128
7129int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7130{
7131 int ret = 0;
7132 int crt_expected;
7133#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7134 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7135 ? ssl->handshake->sni_authmode
7136 : ssl->conf->authmode;
7137#else
7138 const int authmode = ssl->conf->authmode;
7139#endif
7140 void *rs_ctx = NULL;
7141 mbedtls_x509_crt *chain = NULL;
7142
7143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7144
7145 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7146 if( crt_expected == SSL_CERTIFICATE_SKIP )
7147 {
7148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7149 goto exit;
7150 }
7151
7152#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7153 if( ssl->handshake->ecrs_enabled &&
7154 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7155 {
7156 chain = ssl->handshake->ecrs_peer_cert;
7157 ssl->handshake->ecrs_peer_cert = NULL;
7158 goto crt_verify;
7159 }
7160#endif
7161
7162 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7163 {
7164 /* mbedtls_ssl_read_record may have sent an alert already. We
7165 let it decide whether to alert. */
7166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7167 goto exit;
7168 }
7169
7170#if defined(MBEDTLS_SSL_SRV_C)
7171 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7172 {
7173 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7174
7175 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7176 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7177
7178 goto exit;
7179 }
7180#endif /* MBEDTLS_SSL_SRV_C */
7181
7182 /* Clear existing peer CRT structure in case we tried to
7183 * reuse a session but it failed, and allocate a new one. */
7184 ssl_clear_peer_cert( ssl->session_negotiate );
7185
7186 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7187 if( chain == NULL )
7188 {
7189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7190 sizeof( mbedtls_x509_crt ) ) );
7191 mbedtls_ssl_send_alert_message( ssl,
7192 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7193 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7194
7195 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7196 goto exit;
7197 }
7198 mbedtls_x509_crt_init( chain );
7199
7200 ret = ssl_parse_certificate_chain( ssl, chain );
7201 if( ret != 0 )
7202 goto exit;
7203
7204#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7205 if( ssl->handshake->ecrs_enabled)
7206 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7207
7208crt_verify:
7209 if( ssl->handshake->ecrs_enabled)
7210 rs_ctx = &ssl->handshake->ecrs_ctx;
7211#endif
7212
7213 ret = ssl_parse_certificate_verify( ssl, authmode,
7214 chain, rs_ctx );
7215 if( ret != 0 )
7216 goto exit;
7217
7218#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7219 {
7220 unsigned char *crt_start, *pk_start;
7221 size_t crt_len, pk_len;
7222
7223 /* We parse the CRT chain without copying, so
7224 * these pointers point into the input buffer,
7225 * and are hence still valid after freeing the
7226 * CRT chain. */
7227
7228 crt_start = chain->raw.p;
7229 crt_len = chain->raw.len;
7230
7231 pk_start = chain->pk_raw.p;
7232 pk_len = chain->pk_raw.len;
7233
7234 /* Free the CRT structures before computing
7235 * digest and copying the peer's public key. */
7236 mbedtls_x509_crt_free( chain );
7237 mbedtls_free( chain );
7238 chain = NULL;
7239
7240 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7241 if( ret != 0 )
7242 goto exit;
7243
7244 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7245 if( ret != 0 )
7246 goto exit;
7247 }
7248#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7249 /* Pass ownership to session structure. */
7250 ssl->session_negotiate->peer_cert = chain;
7251 chain = NULL;
7252#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7253
7254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7255
7256exit:
7257
7258 if( ret == 0 )
7259 ssl->state++;
7260
7261#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7262 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7263 {
7264 ssl->handshake->ecrs_peer_cert = chain;
7265 chain = NULL;
7266 }
7267#endif
7268
7269 if( chain != NULL )
7270 {
7271 mbedtls_x509_crt_free( chain );
7272 mbedtls_free( chain );
7273 }
7274
7275 return( ret );
7276}
7277#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7278
Andrzej Kurek25f27152022-08-17 16:09:31 -04007279#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007280static void ssl_calc_finished_tls_sha256(
7281 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7282{
7283 int len = 12;
7284 const char *sender;
7285 unsigned char padbuf[32];
7286#if defined(MBEDTLS_USE_PSA_CRYPTO)
7287 size_t hash_size;
7288 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7289 psa_status_t status;
7290#else
7291 mbedtls_sha256_context sha256;
7292#endif
7293
7294 mbedtls_ssl_session *session = ssl->session_negotiate;
7295 if( !session )
7296 session = ssl->session;
7297
7298 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7299 ? "client finished"
7300 : "server finished";
7301
7302#if defined(MBEDTLS_USE_PSA_CRYPTO)
7303 sha256_psa = psa_hash_operation_init();
7304
7305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7306
7307 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7308 if( status != PSA_SUCCESS )
7309 {
7310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7311 return;
7312 }
7313
7314 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7315 if( status != PSA_SUCCESS )
7316 {
7317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7318 return;
7319 }
7320 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7321#else
7322
7323 mbedtls_sha256_init( &sha256 );
7324
7325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7326
7327 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7328
7329 /*
7330 * TLSv1.2:
7331 * hash = PRF( master, finished_label,
7332 * Hash( handshake ) )[0.11]
7333 */
7334
7335#if !defined(MBEDTLS_SHA256_ALT)
7336 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7337 sha256.state, sizeof( sha256.state ) );
7338#endif
7339
7340 mbedtls_sha256_finish( &sha256, padbuf );
7341 mbedtls_sha256_free( &sha256 );
7342#endif /* MBEDTLS_USE_PSA_CRYPTO */
7343
7344 ssl->handshake->tls_prf( session->master, 48, sender,
7345 padbuf, 32, buf, len );
7346
7347 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7348
7349 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7350
7351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7352}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007353#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007354
Jerry Yub7ba49e2022-02-17 14:25:53 +08007355
Andrzej Kurek25f27152022-08-17 16:09:31 -04007356#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007357static void ssl_calc_finished_tls_sha384(
7358 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7359{
7360 int len = 12;
7361 const char *sender;
7362 unsigned char padbuf[48];
7363#if defined(MBEDTLS_USE_PSA_CRYPTO)
7364 size_t hash_size;
7365 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7366 psa_status_t status;
7367#else
7368 mbedtls_sha512_context sha512;
7369#endif
7370
7371 mbedtls_ssl_session *session = ssl->session_negotiate;
7372 if( !session )
7373 session = ssl->session;
7374
7375 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7376 ? "client finished"
7377 : "server finished";
7378
7379#if defined(MBEDTLS_USE_PSA_CRYPTO)
7380 sha384_psa = psa_hash_operation_init();
7381
7382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7383
7384 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7385 if( status != PSA_SUCCESS )
7386 {
7387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7388 return;
7389 }
7390
7391 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7392 if( status != PSA_SUCCESS )
7393 {
7394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7395 return;
7396 }
7397 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7398#else
7399 mbedtls_sha512_init( &sha512 );
7400
7401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7402
Andrzej Kureka242e832022-08-11 10:03:14 -04007403 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007404
7405 /*
7406 * TLSv1.2:
7407 * hash = PRF( master, finished_label,
7408 * Hash( handshake ) )[0.11]
7409 */
7410
7411#if !defined(MBEDTLS_SHA512_ALT)
7412 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7413 sha512.state, sizeof( sha512.state ) );
7414#endif
7415 mbedtls_sha512_finish( &sha512, padbuf );
7416
7417 mbedtls_sha512_free( &sha512 );
7418#endif
7419
7420 ssl->handshake->tls_prf( session->master, 48, sender,
7421 padbuf, 48, buf, len );
7422
7423 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7424
7425 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7426
7427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7428}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007429#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007430
Jerry Yuaef00152022-02-17 14:27:31 +08007431void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7432{
7433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7434
7435 /*
7436 * Free our handshake params
7437 */
7438 mbedtls_ssl_handshake_free( ssl );
7439 mbedtls_free( ssl->handshake );
7440 ssl->handshake = NULL;
7441
7442 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007443 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007444 */
7445 if( ssl->transform )
7446 {
7447 mbedtls_ssl_transform_free( ssl->transform );
7448 mbedtls_free( ssl->transform );
7449 }
7450 ssl->transform = ssl->transform_negotiate;
7451 ssl->transform_negotiate = NULL;
7452
7453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7454}
7455
Jerry Yu2a9fff52022-02-17 14:28:51 +08007456void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7457{
7458 int resume = ssl->handshake->resume;
7459
7460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7461
7462#if defined(MBEDTLS_SSL_RENEGOTIATION)
7463 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7464 {
7465 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7466 ssl->renego_records_seen = 0;
7467 }
7468#endif
7469
7470 /*
7471 * Free the previous session and switch in the current one
7472 */
7473 if( ssl->session )
7474 {
7475#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7476 /* RFC 7366 3.1: keep the EtM state */
7477 ssl->session_negotiate->encrypt_then_mac =
7478 ssl->session->encrypt_then_mac;
7479#endif
7480
7481 mbedtls_ssl_session_free( ssl->session );
7482 mbedtls_free( ssl->session );
7483 }
7484 ssl->session = ssl->session_negotiate;
7485 ssl->session_negotiate = NULL;
7486
7487 /*
7488 * Add cache entry
7489 */
7490 if( ssl->conf->f_set_cache != NULL &&
7491 ssl->session->id_len != 0 &&
7492 resume == 0 )
7493 {
7494 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7495 ssl->session->id,
7496 ssl->session->id_len,
7497 ssl->session ) != 0 )
7498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7499 }
7500
7501#if defined(MBEDTLS_SSL_PROTO_DTLS)
7502 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7503 ssl->handshake->flight != NULL )
7504 {
7505 /* Cancel handshake timer */
7506 mbedtls_ssl_set_timer( ssl, 0 );
7507
7508 /* Keep last flight around in case we need to resend it:
7509 * we need the handshake and transform structures for that */
7510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7511 }
7512 else
7513#endif
7514 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7515
7516 ssl->state++;
7517
7518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7519}
7520
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007521int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7522{
7523 int ret, hash_len;
7524
7525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7526
7527 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7528
7529 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7530
7531 /*
7532 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7533 * may define some other value. Currently (early 2016), no defined
7534 * ciphersuite does this (and this is unlikely to change as activity has
7535 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7536 */
7537 hash_len = 12;
7538
7539#if defined(MBEDTLS_SSL_RENEGOTIATION)
7540 ssl->verify_data_len = hash_len;
7541 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7542#endif
7543
7544 ssl->out_msglen = 4 + hash_len;
7545 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7546 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7547
7548 /*
7549 * In case of session resuming, invert the client and server
7550 * ChangeCipherSpec messages order.
7551 */
7552 if( ssl->handshake->resume != 0 )
7553 {
7554#if defined(MBEDTLS_SSL_CLI_C)
7555 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7556 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7557#endif
7558#if defined(MBEDTLS_SSL_SRV_C)
7559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7560 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7561#endif
7562 }
7563 else
7564 ssl->state++;
7565
7566 /*
7567 * Switch to our negotiated transform and session parameters for outbound
7568 * data.
7569 */
7570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7571
7572#if defined(MBEDTLS_SSL_PROTO_DTLS)
7573 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7574 {
7575 unsigned char i;
7576
7577 /* Remember current epoch settings for resending */
7578 ssl->handshake->alt_transform_out = ssl->transform_out;
7579 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7580 sizeof( ssl->handshake->alt_out_ctr ) );
7581
7582 /* Set sequence_number to zero */
7583 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7584
7585
7586 /* Increment epoch */
7587 for( i = 2; i > 0; i-- )
7588 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7589 break;
7590
7591 /* The loop goes to its end iff the counter is wrapping */
7592 if( i == 0 )
7593 {
7594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7595 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7596 }
7597 }
7598 else
7599#endif /* MBEDTLS_SSL_PROTO_DTLS */
7600 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7601
7602 ssl->transform_out = ssl->transform_negotiate;
7603 ssl->session_out = ssl->session_negotiate;
7604
7605#if defined(MBEDTLS_SSL_PROTO_DTLS)
7606 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7607 mbedtls_ssl_send_flight_completed( ssl );
7608#endif
7609
7610 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7611 {
7612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7613 return( ret );
7614 }
7615
7616#if defined(MBEDTLS_SSL_PROTO_DTLS)
7617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7618 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7619 {
7620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7621 return( ret );
7622 }
7623#endif
7624
7625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7626
7627 return( 0 );
7628}
7629
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007630#define SSL_MAX_HASH_LEN 12
7631
7632int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7633{
7634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7635 unsigned int hash_len = 12;
7636 unsigned char buf[SSL_MAX_HASH_LEN];
7637
7638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7639
7640 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7641
7642 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7643 {
7644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7645 goto exit;
7646 }
7647
7648 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7649 {
7650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7651 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7652 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7653 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7654 goto exit;
7655 }
7656
7657 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7658 {
7659 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7660 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7661 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7662 goto exit;
7663 }
7664
7665 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7666 {
7667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7668 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7669 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7670 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7671 goto exit;
7672 }
7673
7674 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7675 buf, hash_len ) != 0 )
7676 {
7677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7678 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7679 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7680 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7681 goto exit;
7682 }
7683
7684#if defined(MBEDTLS_SSL_RENEGOTIATION)
7685 ssl->verify_data_len = hash_len;
7686 memcpy( ssl->peer_verify_data, buf, hash_len );
7687#endif
7688
7689 if( ssl->handshake->resume != 0 )
7690 {
7691#if defined(MBEDTLS_SSL_CLI_C)
7692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7693 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7694#endif
7695#if defined(MBEDTLS_SSL_SRV_C)
7696 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7697 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7698#endif
7699 }
7700 else
7701 ssl->state++;
7702
7703#if defined(MBEDTLS_SSL_PROTO_DTLS)
7704 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7705 mbedtls_ssl_recv_flight_completed( ssl );
7706#endif
7707
7708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7709
7710exit:
7711 mbedtls_platform_zeroize( buf, hash_len );
7712 return( ret );
7713}
7714
Jerry Yu392112c2022-02-17 14:34:10 +08007715#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7716/*
7717 * Helper to get TLS 1.2 PRF from ciphersuite
7718 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7719 */
7720static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7721{
Jerry Yu392112c2022-02-17 14:34:10 +08007722 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007723 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7724#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007725 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007726 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007727 else
Jerry Yu392112c2022-02-17 14:34:10 +08007728#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007729#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7730 {
7731 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7732 return( tls_prf_sha256 );
7733 }
7734#endif
7735#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7736 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7737 (void) ciphersuite_info;
7738#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007739
Andrzej Kurek894edde2022-09-29 06:31:14 -04007740 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007741}
7742#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007743
7744static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7745{
7746 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007747#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007748 if( tls_prf == tls_prf_sha384 )
7749 {
7750 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7751 }
7752 else
7753#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007754#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007755 if( tls_prf == tls_prf_sha256 )
7756 {
7757 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7758 }
7759 else
7760#endif
7761 return( MBEDTLS_SSL_TLS_PRF_NONE );
7762}
7763
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007764/*
7765 * Populate a transform structure with session keys and all the other
7766 * necessary information.
7767 *
7768 * Parameters:
7769 * - [in/out]: transform: structure to populate
7770 * [in] must be just initialised with mbedtls_ssl_transform_init()
7771 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7772 * - [in] ciphersuite
7773 * - [in] master
7774 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007775 * - [in] tls_prf: pointer to PRF to use for key derivation
7776 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007777 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007778 * - [in] endpoint: client or server
7779 * - [in] ssl: used for:
7780 * - ssl->conf->{f,p}_export_keys
7781 * [in] optionally used for:
7782 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7783 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007784MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007785static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7786 int ciphersuite,
7787 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007788#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007789 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007790#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007791 ssl_tls_prf_t tls_prf,
7792 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007793 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007794 unsigned endpoint,
7795 const mbedtls_ssl_context *ssl )
7796{
7797 int ret = 0;
7798 unsigned char keyblk[256];
7799 unsigned char *key1;
7800 unsigned char *key2;
7801 unsigned char *mac_enc;
7802 unsigned char *mac_dec;
7803 size_t mac_key_len = 0;
7804 size_t iv_copy_len;
7805 size_t keylen;
7806 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007807 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007808#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007809 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007810 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007811#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007812
7813#if defined(MBEDTLS_USE_PSA_CRYPTO)
7814 psa_key_type_t key_type;
7815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7816 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007817 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007818 size_t key_bits;
7819 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7820#endif
7821
7822#if !defined(MBEDTLS_DEBUG_C) && \
7823 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7824 if( ssl->f_export_keys == NULL )
7825 {
7826 ssl = NULL; /* make sure we don't use it except for these cases */
7827 (void) ssl;
7828 }
7829#endif
7830
7831 /*
7832 * Some data just needs copying into the structure
7833 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007834#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007835 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007836#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007837 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007838
7839#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7840 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7841#endif
7842
7843#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007844 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007845 {
7846 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7847 * generation separate. This should never happen. */
7848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7849 }
7850#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7851
7852 /*
7853 * Get various info structures
7854 */
7855 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7856 if( ciphersuite_info == NULL )
7857 {
7858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7859 ciphersuite ) );
7860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7861 }
7862
Neil Armstrongab555e02022-04-04 11:07:59 +02007863 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007864#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007865 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007866#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007867 ciphersuite_info );
7868
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007869 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7870 transform->taglen =
7871 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7872
7873#if defined(MBEDTLS_USE_PSA_CRYPTO)
7874 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7875 transform->taglen,
7876 &alg,
7877 &key_type,
7878 &key_bits ) ) != PSA_SUCCESS )
7879 {
7880 ret = psa_ssl_status_to_mbedtls( status );
7881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7882 goto end;
7883 }
7884#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007885 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7886 if( cipher_info == NULL )
7887 {
7888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7889 ciphersuite_info->cipher ) );
7890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7891 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007892#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007893
Neil Armstronge4512952022-03-08 09:08:22 +01007894#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007895 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007896 if( mac_alg == 0 )
7897 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007899 (unsigned) ciphersuite_info->mac ) );
7900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7901 }
7902#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007903 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7904 if( md_info == NULL )
7905 {
7906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7907 (unsigned) ciphersuite_info->mac ) );
7908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7909 }
Neil Armstronge4512952022-03-08 09:08:22 +01007910#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007911
7912#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7913 /* Copy own and peer's CID if the use of the CID
7914 * extension has been negotiated. */
7915 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7916 {
7917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7918
7919 transform->in_cid_len = ssl->own_cid_len;
7920 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7921 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7922 transform->in_cid_len );
7923
7924 transform->out_cid_len = ssl->handshake->peer_cid_len;
7925 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7926 ssl->handshake->peer_cid_len );
7927 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7928 transform->out_cid_len );
7929 }
7930#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7931
7932 /*
7933 * Compute key block using the PRF
7934 */
7935 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7936 if( ret != 0 )
7937 {
7938 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7939 return( ret );
7940 }
7941
7942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7943 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7944 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7945 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7946 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7947
7948 /*
7949 * Determine the appropriate key, IV and MAC length.
7950 */
7951
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007952#if defined(MBEDTLS_USE_PSA_CRYPTO)
7953 keylen = PSA_BITS_TO_BYTES(key_bits);
7954#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007955 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007956#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007957
7958#if defined(MBEDTLS_GCM_C) || \
7959 defined(MBEDTLS_CCM_C) || \
7960 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007961 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007962 {
7963 size_t explicit_ivlen;
7964
7965 transform->maclen = 0;
7966 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007967
7968 /* All modes haves 96-bit IVs, but the length of the static parts vary
7969 * with mode and version:
7970 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7971 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7972 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7973 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7974 * sequence number).
7975 */
7976 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007977
7978 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007979#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007980 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007981#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007982 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7983 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007984#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007985
7986 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007987 transform->fixed_ivlen = 12;
7988 else
7989 transform->fixed_ivlen = 4;
7990
7991 /* Minimum length of encrypted record */
7992 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7993 transform->minlen = explicit_ivlen + transform->taglen;
7994 }
7995 else
7996#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7997#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007998 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7999 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8000 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008001 {
Neil Armstronge4512952022-03-08 09:08:22 +01008002#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008003 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008004#else
8005 size_t block_size = cipher_info->block_size;
8006#endif /* MBEDTLS_USE_PSA_CRYPTO */
8007
8008#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008009 /* Get MAC length */
8010 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8011#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008012 /* Initialize HMAC contexts */
8013 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8014 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8015 {
8016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8017 goto end;
8018 }
8019
8020 /* Get MAC length */
8021 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008022#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008023 transform->maclen = mac_key_len;
8024
8025 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008026#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008027 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008028#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008029 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008030#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008031
8032 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008033 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008034 transform->minlen = transform->maclen;
8035 else
8036 {
8037 /*
8038 * GenericBlockCipher:
8039 * 1. if EtM is in use: one block plus MAC
8040 * otherwise: * first multiple of blocklen greater than maclen
8041 * 2. IV
8042 */
8043#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008044 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008045 {
8046 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008047 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008048 }
8049 else
8050#endif
8051 {
8052 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008053 + block_size
8054 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008055 }
8056
Glenn Strauss07c64162022-03-14 12:34:51 -04008057 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008058 {
8059 transform->minlen += transform->ivlen;
8060 }
8061 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008062 {
8063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8064 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8065 goto end;
8066 }
8067 }
8068 }
8069 else
8070#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8071 {
8072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8074 }
8075
8076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8077 (unsigned) keylen,
8078 (unsigned) transform->minlen,
8079 (unsigned) transform->ivlen,
8080 (unsigned) transform->maclen ) );
8081
8082 /*
8083 * Finally setup the cipher contexts, IVs and MAC secrets.
8084 */
8085#if defined(MBEDTLS_SSL_CLI_C)
8086 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8087 {
8088 key1 = keyblk + mac_key_len * 2;
8089 key2 = keyblk + mac_key_len * 2 + keylen;
8090
8091 mac_enc = keyblk;
8092 mac_dec = keyblk + mac_key_len;
8093
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008094 iv_copy_len = ( transform->fixed_ivlen ) ?
8095 transform->fixed_ivlen : transform->ivlen;
8096 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8097 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8098 iv_copy_len );
8099 }
8100 else
8101#endif /* MBEDTLS_SSL_CLI_C */
8102#if defined(MBEDTLS_SSL_SRV_C)
8103 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8104 {
8105 key1 = keyblk + mac_key_len * 2 + keylen;
8106 key2 = keyblk + mac_key_len * 2;
8107
8108 mac_enc = keyblk + mac_key_len;
8109 mac_dec = keyblk;
8110
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008111 iv_copy_len = ( transform->fixed_ivlen ) ?
8112 transform->fixed_ivlen : transform->ivlen;
8113 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8114 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8115 iv_copy_len );
8116 }
8117 else
8118#endif /* MBEDTLS_SSL_SRV_C */
8119 {
8120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8121 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8122 goto end;
8123 }
8124
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008125 if( ssl != NULL && ssl->f_export_keys != NULL )
8126 {
8127 ssl->f_export_keys( ssl->p_export_keys,
8128 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8129 master, 48,
8130 randbytes + 32,
8131 randbytes,
8132 tls_prf_get_type( tls_prf ) );
8133 }
8134
8135#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008136 transform->psa_alg = alg;
8137
8138 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8139 {
8140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8141 psa_set_key_algorithm( &attributes, alg );
8142 psa_set_key_type( &attributes, key_type );
8143
8144 if( ( status = psa_import_key( &attributes,
8145 key1,
8146 PSA_BITS_TO_BYTES( key_bits ),
8147 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8148 {
8149 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8150 ret = psa_ssl_status_to_mbedtls( status );
8151 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8152 goto end;
8153 }
8154
8155 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8156
8157 if( ( status = psa_import_key( &attributes,
8158 key2,
8159 PSA_BITS_TO_BYTES( key_bits ),
8160 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8161 {
8162 ret = psa_ssl_status_to_mbedtls( status );
8163 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8164 goto end;
8165 }
8166 }
8167#else
8168 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8169 cipher_info ) ) != 0 )
8170 {
8171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8172 goto end;
8173 }
8174
8175 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8176 cipher_info ) ) != 0 )
8177 {
8178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8179 goto end;
8180 }
8181
8182 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8183 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8184 MBEDTLS_ENCRYPT ) ) != 0 )
8185 {
8186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8187 goto end;
8188 }
8189
8190 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8191 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8192 MBEDTLS_DECRYPT ) ) != 0 )
8193 {
8194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8195 goto end;
8196 }
8197
8198#if defined(MBEDTLS_CIPHER_MODE_CBC)
8199 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8200 {
8201 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8202 MBEDTLS_PADDING_NONE ) ) != 0 )
8203 {
8204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8205 goto end;
8206 }
8207
8208 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8209 MBEDTLS_PADDING_NONE ) ) != 0 )
8210 {
8211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8212 goto end;
8213 }
8214 }
8215#endif /* MBEDTLS_CIPHER_MODE_CBC */
8216#endif /* MBEDTLS_USE_PSA_CRYPTO */
8217
Neil Armstrong29c0c042022-03-17 17:47:28 +01008218#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8219 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8220 For AEAD-based ciphersuites, there is nothing to do here. */
8221 if( mac_key_len != 0 )
8222 {
8223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008224 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008225
8226 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008227 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008228 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8229
8230 if( ( status = psa_import_key( &attributes,
8231 mac_enc, mac_key_len,
8232 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8233 {
8234 ret = psa_ssl_status_to_mbedtls( status );
8235 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8236 goto end;
8237 }
8238
Ronald Cronfb39f152022-03-25 14:36:28 +01008239 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008240 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8241#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8242 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8243#endif
8244 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008245 /* mbedtls_ct_hmac() requires the key to be exportable */
8246 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8247 PSA_KEY_USAGE_VERIFY_HASH );
8248 else
8249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8250
8251 if( ( status = psa_import_key( &attributes,
8252 mac_dec, mac_key_len,
8253 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8254 {
8255 ret = psa_ssl_status_to_mbedtls( status );
8256 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8257 goto end;
8258 }
8259#else
8260 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8261 if( ret != 0 )
8262 goto end;
8263 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8264 if( ret != 0 )
8265 goto end;
8266#endif /* MBEDTLS_USE_PSA_CRYPTO */
8267 }
8268#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8269
8270 ((void) mac_dec);
8271 ((void) mac_enc);
8272
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008273end:
8274 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8275 return( ret );
8276}
8277
Jerry Yuee40f9d2022-02-17 14:55:16 +08008278#if defined(MBEDTLS_USE_PSA_CRYPTO)
8279int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8280 unsigned char *hash, size_t *hashlen,
8281 unsigned char *data, size_t data_len,
8282 mbedtls_md_type_t md_alg )
8283{
8284 psa_status_t status;
8285 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008286 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008287
8288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8289
8290 if( ( status = psa_hash_setup( &hash_operation,
8291 hash_alg ) ) != PSA_SUCCESS )
8292 {
8293 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8294 goto exit;
8295 }
8296
8297 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8298 64 ) ) != PSA_SUCCESS )
8299 {
8300 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8301 goto exit;
8302 }
8303
8304 if( ( status = psa_hash_update( &hash_operation,
8305 data, data_len ) ) != PSA_SUCCESS )
8306 {
8307 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8308 goto exit;
8309 }
8310
8311 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8312 hashlen ) ) != PSA_SUCCESS )
8313 {
8314 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8315 goto exit;
8316 }
8317
8318exit:
8319 if( status != PSA_SUCCESS )
8320 {
8321 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8322 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8323 switch( status )
8324 {
8325 case PSA_ERROR_NOT_SUPPORTED:
8326 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8327 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8328 case PSA_ERROR_BUFFER_TOO_SMALL:
8329 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8330 case PSA_ERROR_INSUFFICIENT_MEMORY:
8331 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8332 default:
8333 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8334 }
8335 }
8336 return( 0 );
8337}
8338
8339#else
8340
8341int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8342 unsigned char *hash, size_t *hashlen,
8343 unsigned char *data, size_t data_len,
8344 mbedtls_md_type_t md_alg )
8345{
8346 int ret = 0;
8347 mbedtls_md_context_t ctx;
8348 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8349 *hashlen = mbedtls_md_get_size( md_info );
8350
8351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8352
8353 mbedtls_md_init( &ctx );
8354
8355 /*
8356 * digitally-signed struct {
8357 * opaque client_random[32];
8358 * opaque server_random[32];
8359 * ServerDHParams params;
8360 * };
8361 */
8362 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8363 {
8364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8365 goto exit;
8366 }
8367 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8368 {
8369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8370 goto exit;
8371 }
8372 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8373 {
8374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8375 goto exit;
8376 }
8377 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8378 {
8379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8380 goto exit;
8381 }
8382 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8383 {
8384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8385 goto exit;
8386 }
8387
8388exit:
8389 mbedtls_md_free( &ctx );
8390
8391 if( ret != 0 )
8392 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8393 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8394
8395 return( ret );
8396}
8397#endif /* MBEDTLS_USE_PSA_CRYPTO */
8398
Jerry Yud9d91da2022-02-17 14:57:06 +08008399#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8400
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008401/* Find the preferred hash for a given signature algorithm. */
8402unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8403 mbedtls_ssl_context *ssl,
8404 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008405{
Gabor Mezei078e8032022-04-27 21:17:56 +02008406 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008407 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008408
8409 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008410 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008411
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008412 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008413 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008414 unsigned int hash_alg_received =
8415 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8416 received_sig_algs[i] );
8417 unsigned int sig_alg_received =
8418 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8419 received_sig_algs[i] );
8420
8421 if( sig_alg == sig_alg_received )
8422 {
8423#if defined(MBEDTLS_USE_PSA_CRYPTO)
8424 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8425 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008426 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008427 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008428
Neil Armstrong96eceb82022-06-30 18:05:05 +02008429 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8430 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8431 PSA_ALG_ECDSA( psa_hash_alg ),
8432 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008433 continue;
8434
Neil Armstrong96eceb82022-06-30 18:05:05 +02008435 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008436 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8437 PSA_ALG_RSA_PKCS1V15_SIGN(
8438 psa_hash_alg ),
8439 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008440 continue;
8441 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008442#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008443
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008444 return( hash_alg_received );
8445 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008446 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008447
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008448 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008449}
8450
8451#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8452
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008453/* Serialization of TLS 1.2 sessions:
8454 *
8455 * struct {
8456 * uint64 start_time;
8457 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008458 * uint8 session_id_len; // at most 32
8459 * opaque session_id[32];
8460 * opaque master[48]; // fixed length in the standard
8461 * uint32 verify_result;
8462 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8463 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8464 * uint32 ticket_lifetime;
8465 * uint8 mfl_code; // up to 255 according to standard
8466 * uint8 encrypt_then_mac; // 0 or 1
8467 * } serialized_session_tls12;
8468 *
8469 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008470static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008471 unsigned char *buf,
8472 size_t buf_len )
8473{
8474 unsigned char *p = buf;
8475 size_t used = 0;
8476
8477#if defined(MBEDTLS_HAVE_TIME)
8478 uint64_t start;
8479#endif
8480#if defined(MBEDTLS_X509_CRT_PARSE_C)
8481#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8482 size_t cert_len;
8483#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8484#endif /* MBEDTLS_X509_CRT_PARSE_C */
8485
8486 /*
8487 * Time
8488 */
8489#if defined(MBEDTLS_HAVE_TIME)
8490 used += 8;
8491
8492 if( used <= buf_len )
8493 {
8494 start = (uint64_t) session->start;
8495
8496 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8497 p += 8;
8498 }
8499#endif /* MBEDTLS_HAVE_TIME */
8500
8501 /*
8502 * Basic mandatory fields
8503 */
8504 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008505 + 1 /* id_len */
8506 + sizeof( session->id )
8507 + sizeof( session->master )
8508 + 4; /* verify_result */
8509
8510 if( used <= buf_len )
8511 {
8512 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8513 p += 2;
8514
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008515 *p++ = MBEDTLS_BYTE_0( session->id_len );
8516 memcpy( p, session->id, 32 );
8517 p += 32;
8518
8519 memcpy( p, session->master, 48 );
8520 p += 48;
8521
8522 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8523 p += 4;
8524 }
8525
8526 /*
8527 * Peer's end-entity certificate
8528 */
8529#if defined(MBEDTLS_X509_CRT_PARSE_C)
8530#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8531 if( session->peer_cert == NULL )
8532 cert_len = 0;
8533 else
8534 cert_len = session->peer_cert->raw.len;
8535
8536 used += 3 + cert_len;
8537
8538 if( used <= buf_len )
8539 {
8540 *p++ = MBEDTLS_BYTE_2( cert_len );
8541 *p++ = MBEDTLS_BYTE_1( cert_len );
8542 *p++ = MBEDTLS_BYTE_0( cert_len );
8543
8544 if( session->peer_cert != NULL )
8545 {
8546 memcpy( p, session->peer_cert->raw.p, cert_len );
8547 p += cert_len;
8548 }
8549 }
8550#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8551 if( session->peer_cert_digest != NULL )
8552 {
8553 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8554 if( used <= buf_len )
8555 {
8556 *p++ = (unsigned char) session->peer_cert_digest_type;
8557 *p++ = (unsigned char) session->peer_cert_digest_len;
8558 memcpy( p, session->peer_cert_digest,
8559 session->peer_cert_digest_len );
8560 p += session->peer_cert_digest_len;
8561 }
8562 }
8563 else
8564 {
8565 used += 2;
8566 if( used <= buf_len )
8567 {
8568 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8569 *p++ = 0;
8570 }
8571 }
8572#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8573#endif /* MBEDTLS_X509_CRT_PARSE_C */
8574
8575 /*
8576 * Session ticket if any, plus associated data
8577 */
8578#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8579 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8580
8581 if( used <= buf_len )
8582 {
8583 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8584 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8585 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8586
8587 if( session->ticket != NULL )
8588 {
8589 memcpy( p, session->ticket, session->ticket_len );
8590 p += session->ticket_len;
8591 }
8592
8593 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8594 p += 4;
8595 }
8596#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8597
8598 /*
8599 * Misc extension-related info
8600 */
8601#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8602 used += 1;
8603
8604 if( used <= buf_len )
8605 *p++ = session->mfl_code;
8606#endif
8607
8608#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8609 used += 1;
8610
8611 if( used <= buf_len )
8612 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8613#endif
8614
8615 return( used );
8616}
8617
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008618MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008619static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008620 const unsigned char *buf,
8621 size_t len )
8622{
8623#if defined(MBEDTLS_HAVE_TIME)
8624 uint64_t start;
8625#endif
8626#if defined(MBEDTLS_X509_CRT_PARSE_C)
8627#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8628 size_t cert_len;
8629#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8630#endif /* MBEDTLS_X509_CRT_PARSE_C */
8631
8632 const unsigned char *p = buf;
8633 const unsigned char * const end = buf + len;
8634
8635 /*
8636 * Time
8637 */
8638#if defined(MBEDTLS_HAVE_TIME)
8639 if( 8 > (size_t)( end - p ) )
8640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8641
8642 start = ( (uint64_t) p[0] << 56 ) |
8643 ( (uint64_t) p[1] << 48 ) |
8644 ( (uint64_t) p[2] << 40 ) |
8645 ( (uint64_t) p[3] << 32 ) |
8646 ( (uint64_t) p[4] << 24 ) |
8647 ( (uint64_t) p[5] << 16 ) |
8648 ( (uint64_t) p[6] << 8 ) |
8649 ( (uint64_t) p[7] );
8650 p += 8;
8651
8652 session->start = (time_t) start;
8653#endif /* MBEDTLS_HAVE_TIME */
8654
8655 /*
8656 * Basic mandatory fields
8657 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008658 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8660
8661 session->ciphersuite = ( p[0] << 8 ) | p[1];
8662 p += 2;
8663
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008664 session->id_len = *p++;
8665 memcpy( session->id, p, 32 );
8666 p += 32;
8667
8668 memcpy( session->master, p, 48 );
8669 p += 48;
8670
8671 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8672 ( (uint32_t) p[1] << 16 ) |
8673 ( (uint32_t) p[2] << 8 ) |
8674 ( (uint32_t) p[3] );
8675 p += 4;
8676
8677 /* Immediately clear invalid pointer values that have been read, in case
8678 * we exit early before we replaced them with valid ones. */
8679#if defined(MBEDTLS_X509_CRT_PARSE_C)
8680#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8681 session->peer_cert = NULL;
8682#else
8683 session->peer_cert_digest = NULL;
8684#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8685#endif /* MBEDTLS_X509_CRT_PARSE_C */
8686#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8687 session->ticket = NULL;
8688#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8689
8690 /*
8691 * Peer certificate
8692 */
8693#if defined(MBEDTLS_X509_CRT_PARSE_C)
8694#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8695 /* Deserialize CRT from the end of the ticket. */
8696 if( 3 > (size_t)( end - p ) )
8697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8698
8699 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8700 p += 3;
8701
8702 if( cert_len != 0 )
8703 {
8704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8705
8706 if( cert_len > (size_t)( end - p ) )
8707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8708
8709 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8710
8711 if( session->peer_cert == NULL )
8712 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8713
8714 mbedtls_x509_crt_init( session->peer_cert );
8715
8716 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8717 p, cert_len ) ) != 0 )
8718 {
8719 mbedtls_x509_crt_free( session->peer_cert );
8720 mbedtls_free( session->peer_cert );
8721 session->peer_cert = NULL;
8722 return( ret );
8723 }
8724
8725 p += cert_len;
8726 }
8727#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8728 /* Deserialize CRT digest from the end of the ticket. */
8729 if( 2 > (size_t)( end - p ) )
8730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8731
8732 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8733 session->peer_cert_digest_len = (size_t) *p++;
8734
8735 if( session->peer_cert_digest_len != 0 )
8736 {
8737 const mbedtls_md_info_t *md_info =
8738 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8739 if( md_info == NULL )
8740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8741 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8743
8744 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8746
8747 session->peer_cert_digest =
8748 mbedtls_calloc( 1, session->peer_cert_digest_len );
8749 if( session->peer_cert_digest == NULL )
8750 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8751
8752 memcpy( session->peer_cert_digest, p,
8753 session->peer_cert_digest_len );
8754 p += session->peer_cert_digest_len;
8755 }
8756#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8757#endif /* MBEDTLS_X509_CRT_PARSE_C */
8758
8759 /*
8760 * Session ticket and associated data
8761 */
8762#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8763 if( 3 > (size_t)( end - p ) )
8764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8765
8766 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8767 p += 3;
8768
8769 if( session->ticket_len != 0 )
8770 {
8771 if( session->ticket_len > (size_t)( end - p ) )
8772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8773
8774 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8775 if( session->ticket == NULL )
8776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8777
8778 memcpy( session->ticket, p, session->ticket_len );
8779 p += session->ticket_len;
8780 }
8781
8782 if( 4 > (size_t)( end - p ) )
8783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8784
8785 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8786 ( (uint32_t) p[1] << 16 ) |
8787 ( (uint32_t) p[2] << 8 ) |
8788 ( (uint32_t) p[3] );
8789 p += 4;
8790#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8791
8792 /*
8793 * Misc extension-related info
8794 */
8795#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8796 if( 1 > (size_t)( end - p ) )
8797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8798
8799 session->mfl_code = *p++;
8800#endif
8801
8802#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8803 if( 1 > (size_t)( end - p ) )
8804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8805
8806 session->encrypt_then_mac = *p++;
8807#endif
8808
8809 /* Done, should have consumed entire buffer */
8810 if( p != end )
8811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8812
8813 return( 0 );
8814}
Jerry Yudc7bd172022-02-17 13:44:15 +08008815#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8816
XiaokangQian75d40ef2022-04-20 11:05:24 +00008817int mbedtls_ssl_validate_ciphersuite(
8818 const mbedtls_ssl_context *ssl,
8819 const mbedtls_ssl_ciphersuite_t *suite_info,
8820 mbedtls_ssl_protocol_version min_tls_version,
8821 mbedtls_ssl_protocol_version max_tls_version )
8822{
8823 (void) ssl;
8824
8825 if( suite_info == NULL )
8826 return( -1 );
8827
8828 if( ( suite_info->min_tls_version > max_tls_version ) ||
8829 ( suite_info->max_tls_version < min_tls_version ) )
8830 {
8831 return( -1 );
8832 }
8833
XiaokangQian060d8672022-04-21 09:24:56 +00008834#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008835#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8836 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8837 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8838 {
8839 return( -1 );
8840 }
8841#endif
8842
8843 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8844#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8845 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8846 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8847 {
8848 return( -1 );
8849 }
8850#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8852
8853 return( 0 );
8854}
8855
Ronald Crone68ab4f2022-10-05 12:46:29 +02008856#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008857/*
8858 * Function for writing a signature algorithm extension.
8859 *
8860 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8861 * value (TLS 1.3 RFC8446):
8862 * enum {
8863 * ....
8864 * ecdsa_secp256r1_sha256( 0x0403 ),
8865 * ecdsa_secp384r1_sha384( 0x0503 ),
8866 * ecdsa_secp521r1_sha512( 0x0603 ),
8867 * ....
8868 * } SignatureScheme;
8869 *
8870 * struct {
8871 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8872 * } SignatureSchemeList;
8873 *
8874 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8875 * value (TLS 1.2 RFC5246):
8876 * enum {
8877 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8878 * sha512(6), (255)
8879 * } HashAlgorithm;
8880 *
8881 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8882 * SignatureAlgorithm;
8883 *
8884 * struct {
8885 * HashAlgorithm hash;
8886 * SignatureAlgorithm signature;
8887 * } SignatureAndHashAlgorithm;
8888 *
8889 * SignatureAndHashAlgorithm
8890 * supported_signature_algorithms<2..2^16-2>;
8891 *
8892 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8893 * generalization of the TLS 1.2 signature algorithm extension.
8894 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8895 * `SignatureScheme` field of TLS 1.3
8896 *
8897 */
8898int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8899 const unsigned char *end, size_t *out_len )
8900{
8901 unsigned char *p = buf;
8902 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8903 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8904
8905 *out_len = 0;
8906
8907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8908
8909 /* Check if we have space for header and length field:
8910 * - extension_type (2 bytes)
8911 * - extension_data_length (2 bytes)
8912 * - supported_signature_algorithms_length (2 bytes)
8913 */
8914 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8915 p += 6;
8916
8917 /*
8918 * Write supported_signature_algorithms
8919 */
8920 supported_sig_alg = p;
8921 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8922 if( sig_alg == NULL )
8923 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8924
8925 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8926 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8928 *sig_alg,
8929 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008930 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8931 continue;
8932 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8933 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8934 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008936 *sig_alg,
8937 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008938 }
8939
8940 /* Length of supported_signature_algorithms */
8941 supported_sig_alg_len = p - supported_sig_alg;
8942 if( supported_sig_alg_len == 0 )
8943 {
8944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8946 }
8947
XiaokangQianeaf36512022-04-24 09:07:44 +00008948 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008949 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008950 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8951
XiaokangQianeaf36512022-04-24 09:07:44 +00008952 *out_len = p - buf;
8953
8954#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08008955 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00008956#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08008957
XiaokangQianeaf36512022-04-24 09:07:44 +00008958 return( 0 );
8959}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008960#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008961
XiaokangQian40a35232022-05-07 09:02:40 +00008962#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008963/*
8964 * mbedtls_ssl_parse_server_name_ext
8965 *
8966 * Structure of server_name extension:
8967 *
8968 * enum {
8969 * host_name(0), (255)
8970 * } NameType;
8971 * opaque HostName<1..2^16-1>;
8972 *
8973 * struct {
8974 * NameType name_type;
8975 * select (name_type) {
8976 * case host_name: HostName;
8977 * } name;
8978 * } ServerName;
8979 * struct {
8980 * ServerName server_name_list<1..2^16-1>
8981 * } ServerNameList;
8982 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008983MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008984int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8985 const unsigned char *buf,
8986 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008987{
8988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8989 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008990 size_t server_name_list_len, hostname_len;
8991 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008992
XiaokangQianf2a94202022-05-20 06:44:24 +00008993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008994
8995 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008996 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008997 p += 2;
8998
XiaokangQian9b2b7712022-05-17 02:57:00 +00008999 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9000 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009001 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009002 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009003 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009004 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009005 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9006 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009007
9008 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9009 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009010 /* sni_name is intended to be used only during the parsing of the
9011 * ClientHello message (it is reset to NULL before the end of
9012 * the message parsing). Thus it is ok to just point to the
9013 * reception buffer and not make a copy of it.
9014 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009015 ssl->handshake->sni_name = p + 3;
9016 ssl->handshake->sni_name_len = hostname_len;
9017 if( ssl->conf->f_sni == NULL )
9018 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009019 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009020 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009021 if( ret != 0 )
9022 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009023 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009024 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9025 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009026 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9027 }
9028 return( 0 );
9029 }
9030
9031 p += hostname_len + 3;
9032 }
9033
9034 return( 0 );
9035}
9036#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9037
XiaokangQianacb39922022-06-17 10:18:48 +00009038#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009039MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009040int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9041 const unsigned char *buf,
9042 const unsigned char *end )
9043{
9044 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009045 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009046 const unsigned char *protocol_name_list;
9047 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009048 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009049
9050 /* If ALPN not configured, just ignore the extension */
9051 if( ssl->conf->alpn_list == NULL )
9052 return( 0 );
9053
9054 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009055 * RFC7301, section 3.1
9056 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009057 *
XiaokangQianc7403452022-06-23 03:24:12 +00009058 * struct {
9059 * ProtocolName protocol_name_list<2..2^16-1>
9060 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009061 */
9062
XiaokangQianc7403452022-06-23 03:24:12 +00009063 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009064 * protocol_name_list_len 2 bytes
9065 * protocol_name_len 1 bytes
9066 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009067 */
XiaokangQianacb39922022-06-17 10:18:48 +00009068 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9069
XiaokangQianc7403452022-06-23 03:24:12 +00009070 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009071 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009072 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009073 protocol_name_list = p;
9074 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009075
9076 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009077 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009078 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009079 protocol_name_len = *p++;
9080 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9081 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009082 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009083 {
9084 MBEDTLS_SSL_PEND_FATAL_ALERT(
9085 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9086 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009087 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009088 }
9089
9090 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009091 }
9092
9093 /* Use our order of preference */
9094 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9095 {
9096 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009097 p = protocol_name_list;
9098 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009099 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009100 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009101 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009102 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009103 {
9104 ssl->alpn_chosen = *alpn;
9105 return( 0 );
9106 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009107
9108 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009109 }
9110 }
9111
XiaokangQian95d5f542022-06-24 02:29:26 +00009112 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009113 MBEDTLS_SSL_PEND_FATAL_ALERT(
9114 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9115 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9116 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9117}
9118
9119int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9120 unsigned char *buf,
9121 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009122 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009123{
9124 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009125 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009126 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009127
9128 if( ssl->alpn_chosen == NULL )
9129 {
9130 return( 0 );
9131 }
9132
XiaokangQian95d5f542022-06-24 02:29:26 +00009133 protocol_name_len = strlen( ssl->alpn_chosen );
9134 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009135
9136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9137 /*
9138 * 0 . 1 ext identifier
9139 * 2 . 3 ext length
9140 * 4 . 5 protocol list length
9141 * 6 . 6 protocol name length
9142 * 7 . 7+n protocol name
9143 */
9144 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9145
XiaokangQian95d5f542022-06-24 02:29:26 +00009146 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009147
XiaokangQian95d5f542022-06-24 02:29:26 +00009148 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9149 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009150 /* Note: the length of the chosen protocol has been checked to be less
9151 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9152 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009153 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009154
XiaokangQian95d5f542022-06-24 02:29:26 +00009155 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009156
9157#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9158 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9159#endif
9160
XiaokangQianacb39922022-06-17 10:18:48 +00009161 return ( 0 );
9162}
9163#endif /* MBEDTLS_SSL_ALPN */
9164
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009165#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009166 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009167 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009168 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009169int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9170 const char *hostname )
9171{
9172 /* Initialize to suppress unnecessary compiler warning */
9173 size_t hostname_len = 0;
9174
9175 /* Check if new hostname is valid before
9176 * making any change to current one */
9177 if( hostname != NULL )
9178 {
9179 hostname_len = strlen( hostname );
9180
9181 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9183 }
9184
9185 /* Now it's clear that we will overwrite the old hostname,
9186 * so we can free it safely */
9187 if( session->hostname != NULL )
9188 {
9189 mbedtls_platform_zeroize( session->hostname,
9190 strlen( session->hostname ) );
9191 mbedtls_free( session->hostname );
9192 }
9193
9194 /* Passing NULL as hostname shall clear the old one */
9195 if( hostname == NULL )
9196 {
9197 session->hostname = NULL;
9198 }
9199 else
9200 {
9201 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9202 if( session->hostname == NULL )
9203 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9204
9205 memcpy( session->hostname, hostname, hostname_len );
9206 }
9207
9208 return( 0 );
9209}
Xiaokang Qian03409292022-10-12 02:49:52 +00009210#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9211 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009212 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009213 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215#endif /* MBEDTLS_SSL_TLS_C */