blob: 4787ca058504ee5cf9e84b83d89592acb4eaca7b [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 }
707 return( NULL );
708}
709
710void mbedtls_ssl_print_extension_type( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type,
713 unsigned int extension_type,
714 const char *extra_msg0,
715 const char *extra_msg1 )
716{
717 const char *extra_msg;
718 if( extra_msg0 && extra_msg1 )
719 {
720 mbedtls_debug_print_msg(
721 ssl, level, file, line,
722 "%s: %s(%u) extension %s %s.",
723 ssl_tls13_get_hs_msg_name( hs_msg_type ),
724 mbedtls_ssl_get_extension_name( extension_type ),
725 extension_type,
726 extra_msg0, extra_msg1 );
727 return;
728 }
729
730 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
731 if( extra_msg )
732 {
733 mbedtls_debug_print_msg(
734 ssl, level, file, line,
735 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
736 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
737 extra_msg );
738 return;
739 }
740
741 mbedtls_debug_print_msg(
742 ssl, level, file, line,
743 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
744 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
745}
746
747void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
748 int level, const char *file, int line,
749 int hs_msg_type, uint32_t extensions_mask,
750 const char *extra )
751{
752
753 for( unsigned i = 0;
754 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
755 i++ )
756 {
757 mbedtls_ssl_print_extension_type(
Jerry Yuea52ed92022-11-08 21:01:17 +0800758 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yud25cab02022-10-31 12:48:30 +0800759 extensions_mask & ( 1 << i ) ? "was" : "was not", extra );
760 }
761}
762
763#endif /* MBEDTLS_DEBUG_C */
764
Jerry Yuc73c6182022-02-08 20:29:25 +0800765void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
766 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
767{
768 ((void) ciphersuite_info);
769
Andrzej Kurek25f27152022-08-17 16:09:31 -0400770#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800771 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
772 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
773 else
774#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400775#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800776 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
777 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
778 else
779#endif
780 {
781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
782 return;
783 }
784}
785
XiaokangQianadab9a62022-07-18 07:41:26 +0000786void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
787 unsigned hs_type,
788 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100789{
790 unsigned char hs_hdr[4];
791
792 /* Build HS header for checksum update. */
793 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
794 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
795 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
796 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
797
798 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
799}
800
801void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
802 unsigned hs_type,
803 unsigned char const *msg,
804 size_t msg_len )
805{
806 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
807 ssl->handshake->update_checksum( ssl, msg, msg_len );
808}
809
Jerry Yuc73c6182022-02-08 20:29:25 +0800810void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
811{
812 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400813#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800814#if defined(MBEDTLS_USE_PSA_CRYPTO)
815 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
816 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
817#else
818 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
819#endif
820#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400821#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800822#if defined(MBEDTLS_USE_PSA_CRYPTO)
823 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
824 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
825#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400826 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800827#endif
828#endif
829}
830
831static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
832 const unsigned char *buf, size_t len )
833{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400834#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800835#if defined(MBEDTLS_USE_PSA_CRYPTO)
836 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
837#else
838 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
839#endif
840#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400841#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800842#if defined(MBEDTLS_USE_PSA_CRYPTO)
843 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
844#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400845 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800846#endif
847#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400848#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
849 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
850 (void) ssl;
851 (void) buf;
852 (void) len;
853#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800854}
855
Andrzej Kurek25f27152022-08-17 16:09:31 -0400856#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800857static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
858 const unsigned char *buf, size_t len )
859{
860#if defined(MBEDTLS_USE_PSA_CRYPTO)
861 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
862#else
863 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
864#endif
865}
866#endif
867
Andrzej Kurek25f27152022-08-17 16:09:31 -0400868#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800869static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
870 const unsigned char *buf, size_t len )
871{
872#if defined(MBEDTLS_USE_PSA_CRYPTO)
873 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
874#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400875 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800876#endif
877}
878#endif
879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200883
Andrzej Kurek25f27152022-08-17 16:09:31 -0400884#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500885#if defined(MBEDTLS_USE_PSA_CRYPTO)
886 handshake->fin_sha256_psa = psa_hash_operation_init();
887 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
888#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200890 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200891#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400893#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500894#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500895 handshake->fin_sha384_psa = psa_hash_operation_init();
896 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500897#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400898 mbedtls_sha512_init( &handshake->fin_sha384 );
899 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500901#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200902
903 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#if defined(MBEDTLS_DHM_C)
906 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200907#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200908#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200910#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200911#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200912 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200913#if defined(MBEDTLS_SSL_CLI_C)
914 handshake->ecjpake_cache = NULL;
915 handshake->ecjpake_cache_len = 0;
916#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200917#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200918
Gilles Peskineeccd8882020-03-10 12:19:08 +0100919#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200920 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200921#endif
922
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200923#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
924 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
925#endif
Hanno Becker75173122019-02-06 16:18:31 +0000926
927#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
928 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
929 mbedtls_pk_init( &handshake->peer_pubkey );
930#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200931}
932
Hanno Beckera18d1322018-01-03 14:27:32 +0000933void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200936
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100937#if defined(MBEDTLS_USE_PSA_CRYPTO)
938 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
939 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100940#else
941 mbedtls_cipher_init( &transform->cipher_ctx_enc );
942 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100943#endif
944
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000945#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100946#if defined(MBEDTLS_USE_PSA_CRYPTO)
947 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
948 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100949#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_md_init( &transform->md_ctx_enc );
951 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000952#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100953#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200954}
955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200959}
960
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200961MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000963{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200964 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000965 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200970 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971
972 /*
973 * Either the pointers are now NULL or cleared properly and can be freed.
974 * Now allocate missing structures.
975 */
976 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200977 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200978 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200979 }
Paul Bakker48916f92012-09-16 19:57:18 +0000980
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200981 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200983 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200984 }
Paul Bakker48916f92012-09-16 19:57:18 +0000985
Paul Bakker82788fb2014-10-20 13:59:19 +0200986 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200988 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200989 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500990#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
991 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400992
Andrzej Kurek4a063792020-10-21 15:08:44 +0200993 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
994 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500995#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000996
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200997 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000998 if( ssl->handshake == NULL ||
999 ssl->transform_negotiate == NULL ||
1000 ssl->session_negotiate == NULL )
1001 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_free( ssl->handshake );
1005 mbedtls_free( ssl->transform_negotiate );
1006 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001007
1008 ssl->handshake = NULL;
1009 ssl->transform_negotiate = NULL;
1010 ssl->session_negotiate = NULL;
1011
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001012 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001013 }
1014
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001015 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001017 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001018 ssl_handshake_params_init( ssl->handshake );
1019
Jerry Yud0766ec2022-09-22 10:46:57 +08001020#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1021 defined(MBEDTLS_SSL_SRV_C) && \
1022 defined(MBEDTLS_SSL_SESSION_TICKETS)
1023 ssl->handshake->new_session_tickets_count =
1024 ssl->conf->new_session_tickets_count ;
1025#endif
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001028 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1029 {
1030 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001031
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1033 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1034 else
1035 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001036
Hanno Becker0f57a652020-02-05 10:37:26 +00001037 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001038 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001039#endif
1040
Brett Warrene0edc842021-08-17 09:53:13 +01001041/*
1042 * curve_list is translated to IANA TLS group identifiers here because
1043 * mbedtls_ssl_conf_curves returns void and so can't return
1044 * any error codes.
1045 */
1046#if defined(MBEDTLS_ECP_C)
1047#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1048 /* Heap allocate and translate curve_list from internal to IANA group ids */
1049 if ( ssl->conf->curve_list != NULL )
1050 {
1051 size_t length;
1052 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1053
1054 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1055 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1056
1057 /* Leave room for zero termination */
1058 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1059 if ( group_list == NULL )
1060 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1061
1062 for( size_t i = 0; i < length; i++ )
1063 {
1064 const mbedtls_ecp_curve_info *info =
1065 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1066 if ( info == NULL )
1067 {
1068 mbedtls_free( group_list );
1069 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1070 }
1071 group_list[i] = info->tls_id;
1072 }
1073
1074 group_list[length] = 0;
1075
1076 ssl->handshake->group_list = group_list;
1077 ssl->handshake->group_list_heap_allocated = 1;
1078 }
1079 else
1080 {
1081 ssl->handshake->group_list = ssl->conf->group_list;
1082 ssl->handshake->group_list_heap_allocated = 0;
1083 }
1084#endif /* MBEDTLS_DEPRECATED_REMOVED */
1085#endif /* MBEDTLS_ECP_C */
1086
Ronald Crone68ab4f2022-10-05 12:46:29 +02001087#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001088#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001089#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001090 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1091 signature algorithms IANA identifiers. */
1092 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001093 ssl->conf->sig_hashes != NULL )
1094 {
1095 const int *md;
1096 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001097 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001098 uint16_t *p;
1099
Jerry Yub476a442022-01-21 18:14:45 +08001100#if defined(static_assert)
1101 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1102 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1103 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001104#endif
1105
Jerry Yuf017ee42022-01-12 15:49:48 +08001106 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1107 {
1108 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1109 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001110#if defined(MBEDTLS_ECDSA_C)
1111 sig_algs_len += sizeof( uint16_t );
1112#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001113
Jerry Yub476a442022-01-21 18:14:45 +08001114#if defined(MBEDTLS_RSA_C)
1115 sig_algs_len += sizeof( uint16_t );
1116#endif
1117 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1118 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001119 }
1120
Jerry Yub476a442022-01-21 18:14:45 +08001121 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001122 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1123
Jerry Yub476a442022-01-21 18:14:45 +08001124 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1125 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001126 if( ssl->handshake->sig_algs == NULL )
1127 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1128
1129 p = (uint16_t *)ssl->handshake->sig_algs;
1130 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1131 {
1132 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1133 if( hash == MBEDTLS_SSL_HASH_NONE )
1134 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001135#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001136 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1137 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001138#endif
1139#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001140 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1141 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001142#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001144 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001145 ssl->handshake->sig_algs_heap_allocated = 1;
1146 }
1147 else
Jerry Yua69269a2022-01-17 21:06:01 +08001148#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001149 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001150 ssl->handshake->sig_algs_heap_allocated = 0;
1151 }
Jerry Yucc539102022-06-27 16:27:35 +08001152#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001153#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001154 return( 0 );
1155}
1156
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001157#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001158/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001159MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001160static int ssl_cookie_write_dummy( void *ctx,
1161 unsigned char **p, unsigned char *end,
1162 const unsigned char *cli_id, size_t cli_id_len )
1163{
1164 ((void) ctx);
1165 ((void) p);
1166 ((void) end);
1167 ((void) cli_id);
1168 ((void) cli_id_len);
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001171}
1172
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001173MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001174static int ssl_cookie_check_dummy( void *ctx,
1175 const unsigned char *cookie, size_t cookie_len,
1176 const unsigned char *cli_id, size_t cli_id_len )
1177{
1178 ((void) ctx);
1179 ((void) cookie);
1180 ((void) cookie_len);
1181 ((void) cli_id);
1182 ((void) cli_id_len);
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001185}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001186#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001187
Paul Bakker5121ce52009-01-03 21:22:43 +00001188/*
1189 * Initialize an SSL context
1190 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001191void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1192{
1193 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1194}
1195
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001196MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001197static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1198{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001199 const mbedtls_ssl_config *conf = ssl->conf;
1200
Ronald Cron6f135e12021-12-08 16:57:54 +01001201#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001202 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1203 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001204 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1205 {
1206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1207 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1208 }
1209
Jerry Yu60835a82021-08-04 10:13:52 +08001210 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1211 return( 0 );
1212 }
1213#endif
1214
1215#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001216 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001217 {
1218 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1219 return( 0 );
1220 }
1221#endif
1222
Ronald Cron6f135e12021-12-08 16:57:54 +01001223#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001224 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001225 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001226 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1227 {
1228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1229 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1230 }
1231
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001232 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1233 {
1234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1235 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1236 }
1237
1238
Ronald Crone1d3f062022-02-10 14:50:54 +01001239 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1240 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001241 }
1242#endif
1243
1244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1245 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1246}
1247
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001248MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001249static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1250{
1251 int ret;
1252 ret = ssl_conf_version_check( ssl );
1253 if( ret != 0 )
1254 return( ret );
1255
1256 /* Space for further checks */
1257
1258 return( 0 );
1259}
1260
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001261/*
1262 * Setup an SSL context
1263 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001264
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001265int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001266 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001267{
Janos Follath865b3eb2019-12-16 11:46:15 +00001268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001269 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1270 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001272 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001273
Jerry Yu60835a82021-08-04 10:13:52 +08001274 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1275 return( ret );
1276
Paul Bakker62f2dee2012-09-28 07:31:51 +00001277 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001278 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001279 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001280
1281 /* Set to NULL in case of an error condition */
1282 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001283
Darryl Greenb33cc762019-11-28 14:29:44 +00001284#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1285 ssl->in_buf_len = in_buf_len;
1286#endif
1287 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001288 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001291 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001292 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001293 }
1294
Darryl Greenb33cc762019-11-28 14:29:44 +00001295#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1296 ssl->out_buf_len = out_buf_len;
1297#endif
1298 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001299 if( ssl->out_buf == NULL )
1300 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001302 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001303 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 }
1305
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001306 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001307
Johan Pascalb62bb512015-12-03 21:56:45 +01001308#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001309 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001310#endif
1311
Paul Bakker48916f92012-09-16 19:57:18 +00001312 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001313 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
1315 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001316
1317error:
1318 mbedtls_free( ssl->in_buf );
1319 mbedtls_free( ssl->out_buf );
1320
1321 ssl->conf = NULL;
1322
Darryl Greenb33cc762019-11-28 14:29:44 +00001323#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1324 ssl->in_buf_len = 0;
1325 ssl->out_buf_len = 0;
1326#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001327 ssl->in_buf = NULL;
1328 ssl->out_buf = NULL;
1329
1330 ssl->in_hdr = NULL;
1331 ssl->in_ctr = NULL;
1332 ssl->in_len = NULL;
1333 ssl->in_iv = NULL;
1334 ssl->in_msg = NULL;
1335
1336 ssl->out_hdr = NULL;
1337 ssl->out_ctr = NULL;
1338 ssl->out_len = NULL;
1339 ssl->out_iv = NULL;
1340 ssl->out_msg = NULL;
1341
k-stachowiak9f7798e2018-07-31 16:52:32 +02001342 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343}
1344
1345/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001346 * Reset an initialized and used SSL context for re-use while retaining
1347 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001348 *
1349 * If partial is non-zero, keep data in the input buffer and client ID.
1350 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001351 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001352void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1353 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001354{
Darryl Greenb33cc762019-11-28 14:29:44 +00001355#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1356 size_t in_buf_len = ssl->in_buf_len;
1357 size_t out_buf_len = ssl->out_buf_len;
1358#else
1359 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1360 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1361#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001362
Hanno Beckerb0302c42021-08-03 09:39:42 +01001363#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1364 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001365#endif
1366
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001367 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001368 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001369
Hanno Beckerb0302c42021-08-03 09:39:42 +01001370 mbedtls_ssl_reset_in_out_pointers( ssl );
1371
1372 /* Reset incoming message parsing */
1373 ssl->in_offt = NULL;
1374 ssl->nb_zero = 0;
1375 ssl->in_msgtype = 0;
1376 ssl->in_msglen = 0;
1377 ssl->in_hslen = 0;
1378 ssl->keep_current_message = 0;
1379 ssl->transform_in = NULL;
1380
1381#if defined(MBEDTLS_SSL_PROTO_DTLS)
1382 ssl->next_record_offset = 0;
1383 ssl->in_epoch = 0;
1384#endif
1385
1386 /* Keep current datagram if partial == 1 */
1387 if( partial == 0 )
1388 {
1389 ssl->in_left = 0;
1390 memset( ssl->in_buf, 0, in_buf_len );
1391 }
1392
Ronald Cronad8c17b2022-06-10 17:18:09 +02001393 ssl->send_alert = 0;
1394
Hanno Beckerb0302c42021-08-03 09:39:42 +01001395 /* Reset outgoing message writing */
1396 ssl->out_msgtype = 0;
1397 ssl->out_msglen = 0;
1398 ssl->out_left = 0;
1399 memset( ssl->out_buf, 0, out_buf_len );
1400 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1401 ssl->transform_out = NULL;
1402
1403#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1404 mbedtls_ssl_dtls_replay_reset( ssl );
1405#endif
1406
XiaokangQian2b01dc32022-01-21 02:53:13 +00001407#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001408 if( ssl->transform )
1409 {
1410 mbedtls_ssl_transform_free( ssl->transform );
1411 mbedtls_free( ssl->transform );
1412 ssl->transform = NULL;
1413 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001414#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1415
XiaokangQian2b01dc32022-01-21 02:53:13 +00001416#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1417 mbedtls_ssl_transform_free( ssl->transform_application );
1418 mbedtls_free( ssl->transform_application );
1419 ssl->transform_application = NULL;
1420
1421 if( ssl->handshake != NULL )
1422 {
1423 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1424 mbedtls_free( ssl->handshake->transform_earlydata );
1425 ssl->handshake->transform_earlydata = NULL;
1426
1427 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1428 mbedtls_free( ssl->handshake->transform_handshake );
1429 ssl->handshake->transform_handshake = NULL;
1430 }
1431
XiaokangQian2b01dc32022-01-21 02:53:13 +00001432#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001433}
1434
1435int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1436{
1437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1438
1439 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1440
XiaokangQian78b1fa72022-01-19 06:56:30 +00001441 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001442
1443 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#if defined(MBEDTLS_SSL_RENEGOTIATION)
1445 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001446 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001447
1448 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1450 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001451#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001453
Hanno Beckerb0302c42021-08-03 09:39:42 +01001454 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001455 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001456 if( ssl->session )
1457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_ssl_session_free( ssl->session );
1459 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001460 ssl->session = NULL;
1461 }
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001464 ssl->alpn_chosen = NULL;
1465#endif
1466
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001467#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001468 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001469#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001470 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001471#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001472 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001473 {
1474 mbedtls_free( ssl->cli_id );
1475 ssl->cli_id = NULL;
1476 ssl->cli_id_len = 0;
1477 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001478#endif
1479
Paul Bakker48916f92012-09-16 19:57:18 +00001480 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1481 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001482
1483 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001484}
1485
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001486/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001487 * Reset an initialized and used SSL context for re-use while retaining
1488 * all application-set variables, function pointers and data.
1489 */
1490int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1491{
Hanno Becker43aefe22020-02-05 10:44:56 +00001492 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001493}
1494
1495/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 * SSL set accessors
1497 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001498void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001499{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001500 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001501}
1502
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001503void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001505 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001506}
1507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001509void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001510{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001511 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001512}
1513#endif
1514
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001515void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001516{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001517 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001518}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001521
Hanno Becker1841b0a2018-08-24 11:13:57 +01001522void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1523 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001524{
1525 ssl->disable_datagram_packing = !allow_packing;
1526}
1527
1528void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1529 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001530{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001531 conf->hs_timeout_min = min;
1532 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001533}
1534#endif
1535
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001536void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001538 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001539}
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001542void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001543 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001544 void *p_vrfy )
1545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->f_vrfy = f_vrfy;
1547 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001550
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001551void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001552 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 void *p_rng )
1554{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001555 conf->f_rng = f_rng;
1556 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001557}
1558
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001559void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001560 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 void *p_dbg )
1562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001563 conf->f_dbg = f_dbg;
1564 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001565}
1566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001568 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001569 mbedtls_ssl_send_t *f_send,
1570 mbedtls_ssl_recv_t *f_recv,
1571 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001572{
1573 ssl->p_bio = p_bio;
1574 ssl->f_send = f_send;
1575 ssl->f_recv = f_recv;
1576 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001577}
1578
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001579#if defined(MBEDTLS_SSL_PROTO_DTLS)
1580void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1581{
1582 ssl->mtu = mtu;
1583}
1584#endif
1585
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001586void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001587{
1588 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001589}
1590
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001591void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1592 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001593 mbedtls_ssl_set_timer_t *f_set_timer,
1594 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001595{
1596 ssl->p_timer = p_timer;
1597 ssl->f_set_timer = f_set_timer;
1598 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001599
1600 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001601 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001602}
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001605void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001606 void *p_cache,
1607 mbedtls_ssl_cache_get_t *f_get_cache,
1608 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001609{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001610 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001611 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001612 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#if defined(MBEDTLS_SSL_CLI_C)
1617int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001618{
Janos Follath865b3eb2019-12-16 11:46:15 +00001619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001620
1621 if( ssl == NULL ||
1622 session == NULL ||
1623 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001624 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001627 }
1628
Hanno Beckere810bbc2021-05-14 16:01:05 +01001629 if( ssl->handshake->resume == 1 )
1630 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1631
Jerry Yu21092062022-10-10 21:21:31 +08001632#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1633 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001634 {
Jerry Yu21092062022-10-10 21:21:31 +08001635 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1636 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1637
1638 if( mbedtls_ssl_validate_ciphersuite(
1639 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1640 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1641 {
1642 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1643 session->ciphersuite ) );
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1645 }
Jerry Yu40afab62022-10-08 10:42:13 +08001646 }
Jerry Yu21092062022-10-10 21:21:31 +08001647#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001648
Hanno Becker52055ae2019-02-06 14:30:46 +00001649 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1650 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001651 return( ret );
1652
Paul Bakker0a597072012-09-25 21:55:46 +00001653 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654
1655 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001659void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1660 const int *ciphersuites )
1661{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001662 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001663}
1664
Ronald Cron6f135e12021-12-08 16:57:54 +01001665#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001666void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001667 const int kex_modes )
1668{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001669 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001670}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001671
1672#if defined(MBEDTLS_SSL_EARLY_DATA)
1673void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001674 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001675{
1676 conf->early_data_enabled = early_data_enabled;
1677}
1678#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001679#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001682void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001683 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001684{
1685 conf->cert_profile = profile;
1686}
1687
Glenn Strauss36872db2022-01-22 05:06:31 -05001688static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1689{
1690 mbedtls_ssl_key_cert *cur = key_cert, *next;
1691
1692 while( cur != NULL )
1693 {
1694 next = cur->next;
1695 mbedtls_free( cur );
1696 cur = next;
1697 }
1698}
1699
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001700/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001701MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001702static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1703 mbedtls_x509_crt *cert,
1704 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001705{
niisato8ee24222018-06-25 19:05:48 +09001706 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001707
Glenn Strauss36872db2022-01-22 05:06:31 -05001708 if( cert == NULL )
1709 {
1710 /* Free list if cert is null */
1711 ssl_key_cert_free( *head );
1712 *head = NULL;
1713 return( 0 );
1714 }
1715
niisato8ee24222018-06-25 19:05:48 +09001716 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1717 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001718 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001719
niisato8ee24222018-06-25 19:05:48 +09001720 new_cert->cert = cert;
1721 new_cert->key = key;
1722 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001723
Glenn Strauss36872db2022-01-22 05:06:31 -05001724 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001725 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001726 {
niisato8ee24222018-06-25 19:05:48 +09001727 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001728 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001729 else
1730 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001731 mbedtls_ssl_key_cert *cur = *head;
1732 while( cur->next != NULL )
1733 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001734 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001735 }
1736
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001737 return( 0 );
1738}
1739
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001740int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001741 mbedtls_x509_crt *own_cert,
1742 mbedtls_pk_context *pk_key )
1743{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001744 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001745}
1746
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001747void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001748 mbedtls_x509_crt *ca_chain,
1749 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001750{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001751 conf->ca_chain = ca_chain;
1752 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001753
1754#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1755 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1756 * cannot be used together. */
1757 conf->f_ca_cb = NULL;
1758 conf->p_ca_cb = NULL;
1759#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001760}
Hanno Becker5adaad92019-03-27 16:54:37 +00001761
1762#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1763void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001764 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001765 void *p_ca_cb )
1766{
1767 conf->f_ca_cb = f_ca_cb;
1768 conf->p_ca_cb = p_ca_cb;
1769
1770 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1771 * cannot be used together. */
1772 conf->ca_chain = NULL;
1773 conf->ca_crl = NULL;
1774}
1775#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001777
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001778#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001779const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1780 size_t *name_len )
1781{
1782 *name_len = ssl->handshake->sni_name_len;
1783 return( ssl->handshake->sni_name );
1784}
1785
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001786int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1787 mbedtls_x509_crt *own_cert,
1788 mbedtls_pk_context *pk_key )
1789{
1790 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1791 own_cert, pk_key ) );
1792}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001793
1794void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1795 mbedtls_x509_crt *ca_chain,
1796 mbedtls_x509_crl *ca_crl )
1797{
1798 ssl->handshake->sni_ca_chain = ca_chain;
1799 ssl->handshake->sni_ca_crl = ca_crl;
1800}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001801
Glenn Strauss999ef702022-03-11 01:37:23 -05001802#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1803void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1804 const mbedtls_x509_crt *crt)
1805{
1806 ssl->handshake->dn_hints = crt;
1807}
1808#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1809
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001810void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1811 int authmode )
1812{
1813 ssl->handshake->sni_authmode = authmode;
1814}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001815#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1816
Hanno Becker8927c832019-04-03 12:52:50 +01001817#if defined(MBEDTLS_X509_CRT_PARSE_C)
1818void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1819 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1820 void *p_vrfy )
1821{
1822 ssl->f_vrfy = f_vrfy;
1823 ssl->p_vrfy = p_vrfy;
1824}
1825#endif
1826
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001827#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001828/*
1829 * Set EC J-PAKE password for current handshake
1830 */
1831int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1832 const unsigned char *pw,
1833 size_t pw_len )
1834{
1835 mbedtls_ecjpake_role role;
1836
Janos Follath8eb64132016-06-03 15:40:57 +01001837 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1839
1840 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1841 role = MBEDTLS_ECJPAKE_SERVER;
1842 else
1843 role = MBEDTLS_ECJPAKE_CLIENT;
1844
1845 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1846 role,
1847 MBEDTLS_MD_SHA256,
1848 MBEDTLS_ECP_DP_SECP256R1,
1849 pw, pw_len ) );
1850}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001851#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001852
Ronald Cron73fe8df2022-10-05 14:31:43 +02001853#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001854int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001855{
Ronald Crond29e13e2022-10-19 10:33:48 +02001856 if( conf->psk_identity == NULL ||
1857 conf->psk_identity_len == 0 )
1858 {
1859 return( 0 );
1860 }
1861
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001862#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001863 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001864 return( 1 );
1865#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001866
1867 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001868 return( 1 );
1869
1870 return( 0 );
1871}
1872
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001873static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1874{
1875 /* Remove reference to existing PSK, if any. */
1876#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001877 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001878 {
1879 /* The maintenance of the PSK key slot is the
1880 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001881 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001882 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001883#endif /* MBEDTLS_USE_PSA_CRYPTO */
1884 if( conf->psk != NULL )
1885 {
1886 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1887
1888 mbedtls_free( conf->psk );
1889 conf->psk = NULL;
1890 conf->psk_len = 0;
1891 }
1892
1893 /* Remove reference to PSK identity, if any. */
1894 if( conf->psk_identity != NULL )
1895 {
1896 mbedtls_free( conf->psk_identity );
1897 conf->psk_identity = NULL;
1898 conf->psk_identity_len = 0;
1899 }
1900}
1901
Hanno Becker7390c712018-11-15 13:33:04 +00001902/* This function assumes that PSK identity in the SSL config is unset.
1903 * It checks that the provided identity is well-formed and attempts
1904 * to make a copy of it in the SSL config.
1905 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001906MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001907static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1908 unsigned char const *psk_identity,
1909 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001910{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001911 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001912 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001913 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001914 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001915 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001916 {
1917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1918 }
1919
Hanno Becker7390c712018-11-15 13:33:04 +00001920 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1921 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001922 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001923
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001924 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001925 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001926
1927 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001928}
1929
Hanno Becker7390c712018-11-15 13:33:04 +00001930int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1931 const unsigned char *psk, size_t psk_len,
1932 const unsigned char *psk_identity, size_t psk_identity_len )
1933{
Janos Follath865b3eb2019-12-16 11:46:15 +00001934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001935
1936 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001937 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001938 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001939
1940 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001941 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001943 if( psk_len == 0 )
1944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1945 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1947
Hanno Becker7390c712018-11-15 13:33:04 +00001948 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1949 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1950 conf->psk_len = psk_len;
1951 memcpy( conf->psk, psk, conf->psk_len );
1952
1953 /* Check and set PSK Identity */
1954 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1955 if( ret != 0 )
1956 ssl_conf_remove_psk( conf );
1957
1958 return( ret );
1959}
1960
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001961static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1962{
1963#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001964 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001965 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001966 /* The maintenance of the external PSK key slot is the
1967 * user's responsibility. */
1968 if( ssl->handshake->psk_opaque_is_internal )
1969 {
1970 psa_destroy_key( ssl->handshake->psk_opaque );
1971 ssl->handshake->psk_opaque_is_internal = 0;
1972 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001973 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001974 }
Neil Armstronge952a302022-05-03 10:22:14 +02001975#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001976 if( ssl->handshake->psk != NULL )
1977 {
1978 mbedtls_platform_zeroize( ssl->handshake->psk,
1979 ssl->handshake->psk_len );
1980 mbedtls_free( ssl->handshake->psk );
1981 ssl->handshake->psk_len = 0;
1982 }
Neil Armstronge952a302022-05-03 10:22:14 +02001983#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001984}
1985
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001986int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1987 const unsigned char *psk, size_t psk_len )
1988{
Neil Armstrong501c9322022-05-03 09:35:09 +02001989#if defined(MBEDTLS_USE_PSA_CRYPTO)
1990 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001991 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001992 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001994#endif /* MBEDTLS_USE_PSA_CRYPTO */
1995
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001996 if( psk == NULL || ssl->handshake == NULL )
1997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1998
1999 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2001
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002002 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002003
Neil Armstrong501c9322022-05-03 09:35:09 +02002004#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002005#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2006 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2007 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002008 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002009 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2010 else
2011 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2012 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2013 }
2014#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002015
Jerry Yu568ec252022-07-22 21:27:34 +08002016#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002017 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2018 {
2019 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2020 psa_set_key_usage_flags( &key_attributes,
2021 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2022 }
2023#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2024
Neil Armstrong501c9322022-05-03 09:35:09 +02002025 psa_set_key_algorithm( &key_attributes, alg );
2026 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2027
2028 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2029 if( status != PSA_SUCCESS )
2030 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2031
2032 /* Allow calling psa_destroy_key() on psk remove */
2033 ssl->handshake->psk_opaque_is_internal = 1;
2034 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2035#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002036 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002037 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002038
2039 ssl->handshake->psk_len = psk_len;
2040 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2041
2042 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002043#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002044}
2045
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002046#if defined(MBEDTLS_USE_PSA_CRYPTO)
2047int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002048 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002049 const unsigned char *psk_identity,
2050 size_t psk_identity_len )
2051{
Janos Follath865b3eb2019-12-16 11:46:15 +00002052 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002053
2054 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002055 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002056 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002057
Hanno Becker7390c712018-11-15 13:33:04 +00002058 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002059 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002061 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002062
2063 /* Check and set PSK Identity */
2064 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2065 psk_identity_len );
2066 if( ret != 0 )
2067 ssl_conf_remove_psk( conf );
2068
2069 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002070}
2071
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002072int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2073 mbedtls_svc_key_id_t psk )
2074{
2075 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2076 ( ssl->handshake == NULL ) )
2077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2078
2079 ssl_remove_psk( ssl );
2080 ssl->handshake->psk_opaque = psk;
2081 return( 0 );
2082}
2083#endif /* MBEDTLS_USE_PSA_CRYPTO */
2084
Jerry Yu8897c072022-08-12 13:56:53 +08002085#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002086void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2087 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2088 size_t),
2089 void *p_psk )
2090{
2091 conf->f_psk = f_psk;
2092 conf->p_psk = p_psk;
2093}
Jerry Yu8897c072022-08-12 13:56:53 +08002094#endif /* MBEDTLS_SSL_SRV_C */
2095
Ronald Cron73fe8df2022-10-05 14:31:43 +02002096#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002097
2098#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002099static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2100 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002101{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002102#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002103 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002104 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002105#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002106 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002107 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002108 return( MBEDTLS_SSL_MODE_STREAM );
2109}
2110
2111#else /* MBEDTLS_USE_PSA_CRYPTO */
2112
2113static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2114 mbedtls_cipher_mode_t mode )
2115{
2116#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2117 if( mode == MBEDTLS_MODE_CBC )
2118 return( MBEDTLS_SSL_MODE_CBC );
2119#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2120
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002121#if defined(MBEDTLS_GCM_C) || \
2122 defined(MBEDTLS_CCM_C) || \
2123 defined(MBEDTLS_CHACHAPOLY_C)
2124 if( mode == MBEDTLS_MODE_GCM ||
2125 mode == MBEDTLS_MODE_CCM ||
2126 mode == MBEDTLS_MODE_CHACHAPOLY )
2127 return( MBEDTLS_SSL_MODE_AEAD );
2128#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002129
2130 return( MBEDTLS_SSL_MODE_STREAM );
2131}
Gilles Peskine301711e2022-04-26 16:57:05 +02002132#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002133
Gilles Peskinee108d982022-04-26 16:50:40 +02002134static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2135 mbedtls_ssl_mode_t base_mode,
2136 int encrypt_then_mac )
2137{
2138#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2139 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2140 base_mode == MBEDTLS_SSL_MODE_CBC )
2141 {
2142 return( MBEDTLS_SSL_MODE_CBC_ETM );
2143 }
2144#else
2145 (void) encrypt_then_mac;
2146#endif
2147 return( base_mode );
2148}
2149
Neil Armstrongab555e02022-04-04 11:07:59 +02002150mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002151 const mbedtls_ssl_transform *transform )
2152{
Gilles Peskinee108d982022-04-26 16:50:40 +02002153 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002154#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002155 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002156#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002157 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2158#endif
2159 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002160
Gilles Peskinee108d982022-04-26 16:50:40 +02002161 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002162#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002163 encrypt_then_mac = transform->encrypt_then_mac;
2164#endif
2165 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002166}
2167
Neil Armstrongab555e02022-04-04 11:07:59 +02002168mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002169#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002170 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002171#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002172 const mbedtls_ssl_ciphersuite_t *suite )
2173{
Gilles Peskinee108d982022-04-26 16:50:40 +02002174 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2175
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002176#if defined(MBEDTLS_USE_PSA_CRYPTO)
2177 psa_status_t status;
2178 psa_algorithm_t alg;
2179 psa_key_type_t type;
2180 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002181 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2182 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002183 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002184#else
2185 const mbedtls_cipher_info_t *cipher =
2186 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002187 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002188 {
2189 base_mode =
2190 mbedtls_ssl_get_base_mode(
2191 mbedtls_cipher_info_get_mode( cipher ) );
2192 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002193#endif /* MBEDTLS_USE_PSA_CRYPTO */
2194
Gilles Peskinee108d982022-04-26 16:50:40 +02002195#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2196 int encrypt_then_mac = 0;
2197#endif
2198 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002199}
2200
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002201#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002202
2203#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002204/* Serialization of TLS 1.3 sessions:
2205 *
2206 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002207 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002208 * uint64 ticket_received;
2209 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002210 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002211 * } ClientOnlyData;
2212 *
2213 * struct {
2214 * uint8 endpoint;
2215 * uint8 ciphersuite[2];
2216 * uint32 ticket_age_add;
2217 * uint8 ticket_flags;
2218 * opaque resumption_key<0..255>;
2219 * select ( endpoint ) {
2220 * case client: ClientOnlyData;
2221 * case server: uint64 start_time;
2222 * };
2223 * } serialized_session_tls13;
2224 *
2225 */
2226#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002227MBEDTLS_CHECK_RETURN_CRITICAL
2228static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2229 unsigned char *buf,
2230 size_t buf_len,
2231 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002232{
2233 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002234#if defined(MBEDTLS_SSL_CLI_C) && \
2235 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2236 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002237 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002238#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002239 size_t needed = 1 /* endpoint */
2240 + 2 /* ciphersuite */
2241 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002242 + 1 /* ticket_flags */
2243 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002244 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002245
2246 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2248 needed += session->resumption_key_len; /* resumption_key */
2249
Jerry Yu438ddd82022-07-07 06:55:50 +00002250#if defined(MBEDTLS_HAVE_TIME)
2251 needed += 8; /* start_time or ticket_received */
2252#endif
2253
2254#if defined(MBEDTLS_SSL_CLI_C)
2255 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2256 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002257#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002258 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002259 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002260#endif
2261
Jerry Yu438ddd82022-07-07 06:55:50 +00002262 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002263 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002264
2265 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002266 if( session->ticket_len > SIZE_MAX - needed )
2267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002268
Jerry Yue28d9742022-08-18 15:44:03 +08002269 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002270 }
2271#endif /* MBEDTLS_SSL_CLI_C */
2272
Jerry Yue36fdd62022-08-17 21:31:36 +08002273 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002274 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002275 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002276
2277 p[0] = session->endpoint;
2278 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2279 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2280 p[7] = session->ticket_flags;
2281
2282 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002283 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002284 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002285 memcpy( p, session->resumption_key, session->resumption_key_len );
2286 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002287
2288#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2289 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2290 {
2291 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2292 p += 8;
2293 }
2294#endif /* MBEDTLS_HAVE_TIME */
2295
2296#if defined(MBEDTLS_SSL_CLI_C)
2297 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2298 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002299#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2300 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2301 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002302 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002303 {
2304 /* save host name */
2305 memcpy( p, session->hostname, hostname_len );
2306 p += hostname_len;
2307 }
2308#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2309
Jerry Yu438ddd82022-07-07 06:55:50 +00002310#if defined(MBEDTLS_HAVE_TIME)
2311 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2312 p += 8;
2313#endif
2314 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2315 p += 4;
2316
2317 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2318 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002319
2320 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002321 {
2322 memcpy( p, session->ticket, session->ticket_len );
2323 p += session->ticket_len;
2324 }
2325 }
2326#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002327 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002328}
2329
2330MBEDTLS_CHECK_RETURN_CRITICAL
2331static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2332 const unsigned char *buf,
2333 size_t len )
2334{
2335 const unsigned char *p = buf;
2336 const unsigned char *end = buf + len;
2337
2338 if( end - p < 9 )
2339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2340 session->endpoint = p[0];
2341 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2342 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2343 session->ticket_flags = p[7];
2344
2345 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002346 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002347 p += 9;
2348
Jerry Yubc7c1a42022-07-21 22:57:37 +08002349 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2351
Jerry Yubc7c1a42022-07-21 22:57:37 +08002352 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002354 memcpy( session->resumption_key, p, session->resumption_key_len );
2355 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002356
2357#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2358 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2359 {
2360 if( end - p < 8 )
2361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2362 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2363 p += 8;
2364 }
2365#endif /* MBEDTLS_HAVE_TIME */
2366
2367#if defined(MBEDTLS_SSL_CLI_C)
2368 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2369 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002370#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2371 defined(MBEDTLS_SSL_SESSION_TICKETS)
2372 size_t hostname_len;
2373 /* load host name */
2374 if( end - p < 2 )
2375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2376 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2377 p += 2;
2378
2379 if( end - p < ( long int )hostname_len )
2380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2381 if( hostname_len > 0 )
2382 {
2383 session->hostname = mbedtls_calloc( 1, hostname_len );
2384 if( session->hostname == NULL )
2385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2386 memcpy( session->hostname, p, hostname_len );
2387 p += hostname_len;
2388 }
2389#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2390 MBEDTLS_SSL_SESSION_TICKETS */
2391
Jerry Yu438ddd82022-07-07 06:55:50 +00002392#if defined(MBEDTLS_HAVE_TIME)
2393 if( end - p < 8 )
2394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2395 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2396 p += 8;
2397#endif
2398 if( end - p < 4 )
2399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2400 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2401 p += 4;
2402
2403 if( end - p < 2 )
2404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2405 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2406 p += 2;
2407
2408 if( end - p < ( long int )session->ticket_len )
2409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2410 if( session->ticket_len > 0 )
2411 {
2412 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2413 if( session->ticket == NULL )
2414 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2415 memcpy( session->ticket, p, session->ticket_len );
2416 p += session->ticket_len;
2417 }
2418 }
2419#endif /* MBEDTLS_SSL_CLI_C */
2420
2421 return( 0 );
2422
2423}
2424#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002425MBEDTLS_CHECK_RETURN_CRITICAL
2426static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2427 unsigned char *buf,
2428 size_t buf_len,
2429 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002430{
2431 ((void) session);
2432 ((void) buf);
2433 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002434 *olen = 0;
2435 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002436}
Jerry Yu438ddd82022-07-07 06:55:50 +00002437
2438static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2439 unsigned char *buf,
2440 size_t buf_len )
2441{
2442 ((void) session);
2443 ((void) buf);
2444 ((void) buf_len);
2445 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2446}
2447#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002448#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2449
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002450psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002451 size_t taglen,
2452 psa_algorithm_t *alg,
2453 psa_key_type_t *key_type,
2454 size_t *key_size )
2455{
2456 switch ( mbedtls_cipher_type )
2457 {
2458 case MBEDTLS_CIPHER_AES_128_CBC:
2459 *alg = PSA_ALG_CBC_NO_PADDING;
2460 *key_type = PSA_KEY_TYPE_AES;
2461 *key_size = 128;
2462 break;
2463 case MBEDTLS_CIPHER_AES_128_CCM:
2464 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2465 *key_type = PSA_KEY_TYPE_AES;
2466 *key_size = 128;
2467 break;
2468 case MBEDTLS_CIPHER_AES_128_GCM:
2469 *alg = PSA_ALG_GCM;
2470 *key_type = PSA_KEY_TYPE_AES;
2471 *key_size = 128;
2472 break;
2473 case MBEDTLS_CIPHER_AES_192_CCM:
2474 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2475 *key_type = PSA_KEY_TYPE_AES;
2476 *key_size = 192;
2477 break;
2478 case MBEDTLS_CIPHER_AES_192_GCM:
2479 *alg = PSA_ALG_GCM;
2480 *key_type = PSA_KEY_TYPE_AES;
2481 *key_size = 192;
2482 break;
2483 case MBEDTLS_CIPHER_AES_256_CBC:
2484 *alg = PSA_ALG_CBC_NO_PADDING;
2485 *key_type = PSA_KEY_TYPE_AES;
2486 *key_size = 256;
2487 break;
2488 case MBEDTLS_CIPHER_AES_256_CCM:
2489 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2490 *key_type = PSA_KEY_TYPE_AES;
2491 *key_size = 256;
2492 break;
2493 case MBEDTLS_CIPHER_AES_256_GCM:
2494 *alg = PSA_ALG_GCM;
2495 *key_type = PSA_KEY_TYPE_AES;
2496 *key_size = 256;
2497 break;
2498 case MBEDTLS_CIPHER_ARIA_128_CBC:
2499 *alg = PSA_ALG_CBC_NO_PADDING;
2500 *key_type = PSA_KEY_TYPE_ARIA;
2501 *key_size = 128;
2502 break;
2503 case MBEDTLS_CIPHER_ARIA_128_CCM:
2504 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2505 *key_type = PSA_KEY_TYPE_ARIA;
2506 *key_size = 128;
2507 break;
2508 case MBEDTLS_CIPHER_ARIA_128_GCM:
2509 *alg = PSA_ALG_GCM;
2510 *key_type = PSA_KEY_TYPE_ARIA;
2511 *key_size = 128;
2512 break;
2513 case MBEDTLS_CIPHER_ARIA_192_CCM:
2514 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2515 *key_type = PSA_KEY_TYPE_ARIA;
2516 *key_size = 192;
2517 break;
2518 case MBEDTLS_CIPHER_ARIA_192_GCM:
2519 *alg = PSA_ALG_GCM;
2520 *key_type = PSA_KEY_TYPE_ARIA;
2521 *key_size = 192;
2522 break;
2523 case MBEDTLS_CIPHER_ARIA_256_CBC:
2524 *alg = PSA_ALG_CBC_NO_PADDING;
2525 *key_type = PSA_KEY_TYPE_ARIA;
2526 *key_size = 256;
2527 break;
2528 case MBEDTLS_CIPHER_ARIA_256_CCM:
2529 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2530 *key_type = PSA_KEY_TYPE_ARIA;
2531 *key_size = 256;
2532 break;
2533 case MBEDTLS_CIPHER_ARIA_256_GCM:
2534 *alg = PSA_ALG_GCM;
2535 *key_type = PSA_KEY_TYPE_ARIA;
2536 *key_size = 256;
2537 break;
2538 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2539 *alg = PSA_ALG_CBC_NO_PADDING;
2540 *key_type = PSA_KEY_TYPE_CAMELLIA;
2541 *key_size = 128;
2542 break;
2543 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2544 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2545 *key_type = PSA_KEY_TYPE_CAMELLIA;
2546 *key_size = 128;
2547 break;
2548 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2549 *alg = PSA_ALG_GCM;
2550 *key_type = PSA_KEY_TYPE_CAMELLIA;
2551 *key_size = 128;
2552 break;
2553 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2554 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2555 *key_type = PSA_KEY_TYPE_CAMELLIA;
2556 *key_size = 192;
2557 break;
2558 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2559 *alg = PSA_ALG_GCM;
2560 *key_type = PSA_KEY_TYPE_CAMELLIA;
2561 *key_size = 192;
2562 break;
2563 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2564 *alg = PSA_ALG_CBC_NO_PADDING;
2565 *key_type = PSA_KEY_TYPE_CAMELLIA;
2566 *key_size = 256;
2567 break;
2568 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2569 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2570 *key_type = PSA_KEY_TYPE_CAMELLIA;
2571 *key_size = 256;
2572 break;
2573 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2574 *alg = PSA_ALG_GCM;
2575 *key_type = PSA_KEY_TYPE_CAMELLIA;
2576 *key_size = 256;
2577 break;
2578 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2579 *alg = PSA_ALG_CHACHA20_POLY1305;
2580 *key_type = PSA_KEY_TYPE_CHACHA20;
2581 *key_size = 256;
2582 break;
2583 case MBEDTLS_CIPHER_NULL:
2584 *alg = MBEDTLS_SSL_NULL_CIPHER;
2585 *key_type = 0;
2586 *key_size = 0;
2587 break;
2588 default:
2589 return PSA_ERROR_NOT_SUPPORTED;
2590 }
2591
2592 return PSA_SUCCESS;
2593}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002594#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002595
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002596#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002597int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2598 const unsigned char *dhm_P, size_t P_len,
2599 const unsigned char *dhm_G, size_t G_len )
2600{
Janos Follath865b3eb2019-12-16 11:46:15 +00002601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002602
Glenn Strausscee11292021-12-20 01:43:17 -05002603 mbedtls_mpi_free( &conf->dhm_P );
2604 mbedtls_mpi_free( &conf->dhm_G );
2605
Hanno Beckera90658f2017-10-04 15:29:08 +01002606 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2607 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2608 {
2609 mbedtls_mpi_free( &conf->dhm_P );
2610 mbedtls_mpi_free( &conf->dhm_G );
2611 return( ret );
2612 }
2613
2614 return( 0 );
2615}
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002617int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002618{
Janos Follath865b3eb2019-12-16 11:46:15 +00002619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002620
Glenn Strausscee11292021-12-20 01:43:17 -05002621 mbedtls_mpi_free( &conf->dhm_P );
2622 mbedtls_mpi_free( &conf->dhm_G );
2623
Gilles Peskinee5702482021-06-11 21:59:08 +02002624 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2625 &conf->dhm_P ) ) != 0 ||
2626 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2627 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002628 {
2629 mbedtls_mpi_free( &conf->dhm_P );
2630 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002631 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002632 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002633
2634 return( 0 );
2635}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002636#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002637
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002638#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2639/*
2640 * Set the minimum length for Diffie-Hellman parameters
2641 */
2642void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2643 unsigned int bitlen )
2644{
2645 conf->dhm_min_bitlen = bitlen;
2646}
2647#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2648
Ronald Crone68ab4f2022-10-05 12:46:29 +02002649#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002650#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002651/*
2652 * Set allowed/preferred hashes for handshake signatures
2653 */
2654void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2655 const int *hashes )
2656{
2657 conf->sig_hashes = hashes;
2658}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002659#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002660
Jerry Yuf017ee42022-01-12 15:49:48 +08002661/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002662void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2663 const uint16_t* sig_algs )
2664{
Jerry Yuf017ee42022-01-12 15:49:48 +08002665#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2666 conf->sig_hashes = NULL;
2667#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2668 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002669}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002670#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002671
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002672#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002673#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002674/*
2675 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002676 *
2677 * mbedtls_ssl_setup() takes the provided list
2678 * and translates it to a list of IANA TLS group identifiers,
2679 * stored in ssl->handshake->group_list.
2680 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002681 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002682void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002683 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002685 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002686 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002687}
Brett Warrene0edc842021-08-17 09:53:13 +01002688#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002689#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002690
Brett Warrene0edc842021-08-17 09:53:13 +01002691/*
2692 * Set the allowed groups
2693 */
2694void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2695 const uint16_t *group_list )
2696{
2697#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2698 conf->curve_list = NULL;
2699#endif
2700 conf->group_list = group_list;
2701}
2702
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002703#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705{
Hanno Becker947194e2017-04-07 13:25:49 +01002706 /* Initialize to suppress unnecessary compiler warning */
2707 size_t hostname_len = 0;
2708
2709 /* Check if new hostname is valid before
2710 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002711 if( hostname != NULL )
2712 {
2713 hostname_len = strlen( hostname );
2714
2715 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2717 }
2718
2719 /* Now it's clear that we will overwrite the old hostname,
2720 * so we can free it safely */
2721
2722 if( ssl->hostname != NULL )
2723 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002724 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002725 mbedtls_free( ssl->hostname );
2726 }
2727
2728 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002729
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002731 {
2732 ssl->hostname = NULL;
2733 }
2734 else
2735 {
2736 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002737 if( ssl->hostname == NULL )
2738 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002739
Hanno Becker947194e2017-04-07 13:25:49 +01002740 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002741
Hanno Becker947194e2017-04-07 13:25:49 +01002742 ssl->hostname[hostname_len] = '\0';
2743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
2745 return( 0 );
2746}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002747#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002749#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002750void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002752 const unsigned char *, size_t),
2753 void *p_sni )
2754{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002755 conf->f_sni = f_sni;
2756 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002761int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002762{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002763 size_t cur_len, tot_len;
2764 const char **p;
2765
2766 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002767 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2768 * MUST NOT be truncated."
2769 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002770 */
2771 tot_len = 0;
2772 for( p = protos; *p != NULL; p++ )
2773 {
2774 cur_len = strlen( *p );
2775 tot_len += cur_len;
2776
Ronald Cron8216dd32020-04-23 16:41:44 +02002777 if( ( cur_len == 0 ) ||
2778 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2779 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002781 }
2782
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002783 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002784
2785 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002786}
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002789{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002790 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002793
Johan Pascalb62bb512015-12-03 21:56:45 +01002794#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002795void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2796 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002797{
2798 conf->dtls_srtp_mki_support = support_mki_value;
2799}
2800
Ron Eldoref72faf2018-07-12 11:54:20 +03002801int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2802 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002803 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002804{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002805 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002806 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002808 }
Ron Eldor591f1622018-01-22 12:30:04 +02002809
2810 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002811 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002812 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002813 }
Ron Eldor591f1622018-01-22 12:30:04 +02002814
2815 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2816 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002817 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002818}
2819
Ron Eldoref72faf2018-07-12 11:54:20 +03002820int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002821 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002822{
Johan Pascal253d0262020-09-22 13:04:45 +02002823 const mbedtls_ssl_srtp_profile *p;
2824 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002825
Johan Pascal253d0262020-09-22 13:04:45 +02002826 /* check the profiles list: all entry must be valid,
2827 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002828 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2829 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2830 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002831 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002832 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002833 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002834 list_size++;
2835 }
2836 else
2837 {
2838 /* unsupported value, stop parsing and set the size to an error value */
2839 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002840 }
2841 }
2842
Johan Pascal5ef72d22020-10-28 17:05:47 +01002843 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002844 {
Johan Pascal253d0262020-09-22 13:04:45 +02002845 conf->dtls_srtp_profile_list = NULL;
2846 conf->dtls_srtp_profile_list_len = 0;
2847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2848 }
2849
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002850 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002851 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002852
2853 return( 0 );
2854}
2855
Johan Pascal5ef72d22020-10-28 17:05:47 +01002856void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2857 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002858{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002859 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2860 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002861 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002862 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002863 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002864 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002865 else
2866 {
2867 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002868 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2869 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002870 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002871}
Johan Pascalb62bb512015-12-03 21:56:45 +01002872#endif /* MBEDTLS_SSL_DTLS_SRTP */
2873
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302874#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002875void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002876{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002877 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002878}
2879
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002880void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002881{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002882 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002883}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302884#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002885
Janos Follath088ce432017-04-10 12:42:31 +01002886#if defined(MBEDTLS_SSL_SRV_C)
2887void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2888 char cert_req_ca_list )
2889{
2890 conf->cert_req_ca_list = cert_req_ca_list;
2891}
2892#endif
2893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002895void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002896{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002897 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002898}
2899#endif
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002902void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002903{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002904 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002905}
2906#endif
2907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002909int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002912 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002915 }
2916
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002917 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002918
2919 return( 0 );
2920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002922
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002923void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002925 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002926}
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002929void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002930{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002931 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002932}
2933
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002934void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002935{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002936 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002937}
2938
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002939void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002940 const unsigned char period[8] )
2941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002942 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002943}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002947#if defined(MBEDTLS_SSL_CLI_C)
2948void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002949{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002950 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002951}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002952#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002953
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002954#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002955
Jerry Yud0766ec2022-09-22 10:46:57 +08002956#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002957void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2958 uint16_t num_tickets )
2959{
Jerry Yud0766ec2022-09-22 10:46:57 +08002960 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002961}
2962#endif
2963
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002964void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2965 mbedtls_ssl_ticket_write_t *f_ticket_write,
2966 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2967 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002968{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002969 conf->f_ticket_write = f_ticket_write;
2970 conf->f_ticket_parse = f_ticket_parse;
2971 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002972}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002973#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002975
Hanno Becker7e6c1782021-06-08 09:24:55 +01002976void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2977 mbedtls_ssl_export_keys_t *f_export_keys,
2978 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002979{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002980 ssl->f_export_keys = f_export_keys;
2981 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002982}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002983
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002984#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002985void mbedtls_ssl_conf_async_private_cb(
2986 mbedtls_ssl_config *conf,
2987 mbedtls_ssl_async_sign_t *f_async_sign,
2988 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2989 mbedtls_ssl_async_resume_t *f_async_resume,
2990 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002991 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002992{
2993 conf->f_async_sign_start = f_async_sign;
2994 conf->f_async_decrypt_start = f_async_decrypt;
2995 conf->f_async_resume = f_async_resume;
2996 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002997 conf->p_async_config_data = async_config_data;
2998}
2999
Gilles Peskine8f97af72018-04-26 11:46:10 +02003000void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3001{
3002 return( conf->p_async_config_data );
3003}
3004
Gilles Peskine1febfef2018-04-30 11:54:39 +02003005void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003006{
3007 if( ssl->handshake == NULL )
3008 return( NULL );
3009 else
3010 return( ssl->handshake->user_async_ctx );
3011}
3012
Gilles Peskine1febfef2018-04-30 11:54:39 +02003013void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003014 void *ctx )
3015{
3016 if( ssl->handshake != NULL )
3017 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003018}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003019#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003020
Paul Bakker5121ce52009-01-03 21:22:43 +00003021/*
3022 * SSL get accessors
3023 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003024uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003025{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003026 if( ssl->session != NULL )
3027 return( ssl->session->verify_result );
3028
3029 if( ssl->session_negotiate != NULL )
3030 return( ssl->session_negotiate->verify_result );
3031
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003032 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003033}
3034
Glenn Strauss8f526902022-01-13 00:04:49 -05003035int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3036{
3037 if( ssl == NULL || ssl->session == NULL )
3038 return( 0 );
3039
3040 return( ssl->session->ciphersuite );
3041}
3042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003044{
Paul Bakker926c8e42013-03-06 10:23:34 +01003045 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003046 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003049}
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003054 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003055 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003056 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003057 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003058 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003059 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003060 default:
3061 return( "unknown (DTLS)" );
3062 }
3063 }
3064#endif
3065
Glenn Strauss60bfe602022-03-14 19:04:24 -04003066 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003067 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003068 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003069 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003070 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003071 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003072 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003073 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003074 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003075}
3076
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003077#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003078size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3079{
David Horstmann95d516f2021-05-04 18:36:56 +01003080 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003081 size_t read_mfl;
3082
3083 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3084 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3085 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3086 {
3087 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3088 }
3089
3090 /* Check if a smaller max length was negotiated */
3091 if( ssl->session_out != NULL )
3092 {
3093 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3094 if( read_mfl < max_len )
3095 {
3096 max_len = read_mfl;
3097 }
3098 }
3099
3100 // During a handshake, use the value being negotiated
3101 if( ssl->session_negotiate != NULL )
3102 {
3103 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3104 if( read_mfl < max_len )
3105 {
3106 max_len = read_mfl;
3107 }
3108 }
3109
3110 return( max_len );
3111}
3112
3113size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003114{
3115 size_t max_len;
3116
3117 /*
3118 * Assume mfl_code is correct since it was checked when set
3119 */
Angus Grattond8213d02016-05-25 20:56:48 +10003120 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003121
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003122 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003123 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003124 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003125 {
Angus Grattond8213d02016-05-25 20:56:48 +10003126 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003127 }
3128
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003129 /* During a handshake, use the value being negotiated */
3130 if( ssl->session_negotiate != NULL &&
3131 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3132 {
3133 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3134 }
3135
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003136 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003137}
3138#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3139
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003141size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003142{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003143 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3144 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3145 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3146 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3147 return ( 0 );
3148
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003149 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3150 return( ssl->mtu );
3151
3152 if( ssl->mtu == 0 )
3153 return( ssl->handshake->mtu );
3154
3155 return( ssl->mtu < ssl->handshake->mtu ?
3156 ssl->mtu : ssl->handshake->mtu );
3157}
3158#endif /* MBEDTLS_SSL_PROTO_DTLS */
3159
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003160int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3161{
3162 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3163
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003164#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3165 !defined(MBEDTLS_SSL_PROTO_DTLS)
3166 (void) ssl;
3167#endif
3168
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003169#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003170 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003171
3172 if( max_len > mfl )
3173 max_len = mfl;
3174#endif
3175
3176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003177 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003178 {
Hanno Becker89490712020-02-05 10:50:12 +00003179 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003180 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3181 const size_t overhead = (size_t) ret;
3182
3183 if( ret < 0 )
3184 return( ret );
3185
3186 if( mtu <= overhead )
3187 {
3188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3189 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3190 }
3191
3192 if( max_len > mtu - overhead )
3193 max_len = mtu - overhead;
3194 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003195#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003196
Hanno Becker0defedb2018-08-10 12:35:02 +01003197#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3198 !defined(MBEDTLS_SSL_PROTO_DTLS)
3199 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003200#endif
3201
3202 return( (int) max_len );
3203}
3204
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003205int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3206{
3207 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3208
3209#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3210 (void) ssl;
3211#endif
3212
3213#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3214 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3215
3216 if( max_len > mfl )
3217 max_len = mfl;
3218#endif
3219
3220 return( (int) max_len );
3221}
3222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#if defined(MBEDTLS_X509_CRT_PARSE_C)
3224const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003225{
3226 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003227 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003228
Hanno Beckere6824572019-02-07 13:18:46 +00003229#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003230 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003231#else
3232 return( NULL );
3233#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003238int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3239 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003240{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003241 int ret;
3242
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003243 if( ssl == NULL ||
3244 dst == NULL ||
3245 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003246 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003249 }
3250
Hanno Beckere810bbc2021-05-14 16:01:05 +01003251 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3252 * idempotent: Each session can only be exported once.
3253 *
3254 * (This is in preparation for TLS 1.3 support where we will
3255 * need the ability to export multiple sessions (aka tickets),
3256 * which will be achieved by calling mbedtls_ssl_get_session()
3257 * multiple times until it fails.)
3258 *
3259 * Check whether we have already exported the current session,
3260 * and fail if so.
3261 */
3262 if( ssl->session->exported == 1 )
3263 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3264
3265 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3266 if( ret != 0 )
3267 return( ret );
3268
3269 /* Remember that we've exported the session. */
3270 ssl->session->exported = 1;
3271 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003274
Paul Bakker5121ce52009-01-03 21:22:43 +00003275/*
Hanno Beckera835da52019-05-16 12:39:07 +01003276 * Define ticket header determining Mbed TLS version
3277 * and structure of the ticket.
3278 */
3279
Hanno Becker94ef3b32019-05-16 12:50:45 +01003280/*
Hanno Becker50b59662019-05-28 14:30:45 +01003281 * Define bitflag determining compile-time settings influencing
3282 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003283 */
3284
Hanno Becker50b59662019-05-28 14:30:45 +01003285#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003286#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003287#else
Hanno Becker3e088662019-05-29 11:10:18 +01003288#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003289#endif /* MBEDTLS_HAVE_TIME */
3290
3291#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003292#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003293#else
Hanno Becker3e088662019-05-29 11:10:18 +01003294#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003295#endif /* MBEDTLS_X509_CRT_PARSE_C */
3296
3297#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003298#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003299#else
Hanno Becker3e088662019-05-29 11:10:18 +01003300#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003301#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3302
3303#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003304#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003305#else
Hanno Becker3e088662019-05-29 11:10:18 +01003306#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003307#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3308
Hanno Becker94ef3b32019-05-16 12:50:45 +01003309#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003310#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003311#else
Hanno Becker3e088662019-05-29 11:10:18 +01003312#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003313#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3314
Hanno Becker94ef3b32019-05-16 12:50:45 +01003315#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3316#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3317#else
3318#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3319#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3320
Hanno Becker3e088662019-05-29 11:10:18 +01003321#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3322#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3323#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3324#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003325#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3326#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003327
Hanno Becker50b59662019-05-28 14:30:45 +01003328#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003329 ( (uint16_t) ( \
3330 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3331 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3332 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3333 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003334 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003335 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003336
Hanno Beckerf8787072019-05-16 12:41:07 +01003337static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003338 MBEDTLS_VERSION_MAJOR,
3339 MBEDTLS_VERSION_MINOR,
3340 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003341 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3342 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003343};
Hanno Beckera835da52019-05-16 12:39:07 +01003344
3345/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003346 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003347 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003348 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003349 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003350 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003351 * opaque mbedtls_version[3]; // library version: major, minor, patch
3352 * opaque session_format[2]; // library-version specific 16-bit field
3353 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003354 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003355 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003356 * Note: When updating the format, remember to keep
3357 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003358 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003359 * // In this version, `session_format` determines
3360 * // the setting of those compile-time
3361 * // configuration options which influence
3362 * // the structure of mbedtls_ssl_session.
3363 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003364 * uint8_t minor_ver; // Protocol minor version. Possible values:
3365 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003366 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003367 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003368 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003369 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003370 * serialized_session_tls12 data;
3371 *
3372 * };
3373 *
3374 * } serialized_session;
3375 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003376 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003377
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003378MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003379static int ssl_session_save( const mbedtls_ssl_session *session,
3380 unsigned char omit_header,
3381 unsigned char *buf,
3382 size_t buf_len,
3383 size_t *olen )
3384{
3385 unsigned char *p = buf;
3386 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003387 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003388#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3389 size_t out_len;
3390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3391#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003392 if( session == NULL )
3393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3394
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003395 if( !omit_header )
3396 {
3397 /*
3398 * Add Mbed TLS version identifier
3399 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003400 used += sizeof( ssl_serialized_session_header );
3401
3402 if( used <= buf_len )
3403 {
3404 memcpy( p, ssl_serialized_session_header,
3405 sizeof( ssl_serialized_session_header ) );
3406 p += sizeof( ssl_serialized_session_header );
3407 }
3408 }
3409
3410 /*
3411 * TLS version identifier
3412 */
3413 used += 1;
3414 if( used <= buf_len )
3415 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003416 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003417 }
3418
3419 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003420 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003421 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003422 {
3423#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003424 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003425 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003426 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3428
Jerry Yu251a12e2022-07-13 15:15:48 +08003429#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3430 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003431 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3432 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003433 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003434 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003435 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003436#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3437
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003438 default:
3439 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3440 }
3441
3442 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003443 if( used > buf_len )
3444 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003445
3446 return( 0 );
3447}
3448
3449/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003450 * Public wrapper for ssl_session_save()
3451 */
3452int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3453 unsigned char *buf,
3454 size_t buf_len,
3455 size_t *olen )
3456{
3457 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3458}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003459
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003460/*
3461 * Deserialize session, see mbedtls_ssl_session_save() for format.
3462 *
3463 * This internal version is wrapped by a public function that cleans up in
3464 * case of error, and has an extra option omit_header.
3465 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003466MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003467static int ssl_session_load( mbedtls_ssl_session *session,
3468 unsigned char omit_header,
3469 const unsigned char *buf,
3470 size_t len )
3471{
3472 const unsigned char *p = buf;
3473 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003474 size_t remaining_len;
3475
3476
3477 if( session == NULL )
3478 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003479
3480 if( !omit_header )
3481 {
3482 /*
3483 * Check Mbed TLS version identifier
3484 */
3485
3486 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3488
3489 if( memcmp( p, ssl_serialized_session_header,
3490 sizeof( ssl_serialized_session_header ) ) != 0 )
3491 {
3492 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3493 }
3494 p += sizeof( ssl_serialized_session_header );
3495 }
3496
3497 /*
3498 * TLS version identifier
3499 */
3500 if( 1 > (size_t)( end - p ) )
3501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003502 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003503
3504 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003505 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003506 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003507 {
3508#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003509 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003510 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003511#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3512
Jerry Yu438ddd82022-07-07 06:55:50 +00003513#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3514 case MBEDTLS_SSL_VERSION_TLS1_3:
3515 return( ssl_tls13_session_load( session, p, remaining_len ) );
3516#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3517
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003518 default:
3519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3520 }
3521}
3522
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003523/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003524 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003525 */
3526int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3527 const unsigned char *buf,
3528 size_t len )
3529{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003530 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003531
3532 if( ret != 0 )
3533 mbedtls_ssl_session_free( session );
3534
3535 return( ret );
3536}
3537
3538/*
Paul Bakker1961b702013-01-25 14:49:24 +01003539 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003540 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003541MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003542static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3543{
3544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3545
Ronald Cron66dbf912022-02-02 15:33:46 +01003546 /*
3547 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003548 * that were written into the output buffer by the previous handshake step,
3549 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003550 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3551 * We proceed to the next handshake step only when all data from the
3552 * previous one have been sent to the peer, thus we make sure that this is
3553 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3554 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3555 * we have to wait before to go ahead.
3556 * In the case of TLS 1.3, handshake step handlers do not send data to the
3557 * peer. Data are only sent here and through
3558 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003559 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003560 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003561 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3562 return( ret );
3563
3564#if defined(MBEDTLS_SSL_PROTO_DTLS)
3565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3566 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3567 {
3568 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3569 return( ret );
3570 }
3571#endif /* MBEDTLS_SSL_PROTO_DTLS */
3572
3573 return( ret );
3574}
3575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003577{
Hanno Becker41934dd2021-08-07 19:13:43 +01003578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003579
Hanno Becker41934dd2021-08-07 19:13:43 +01003580 if( ssl == NULL ||
3581 ssl->conf == NULL ||
3582 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003583 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003584 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003586 }
3587
3588 ret = ssl_prepare_handshake_step( ssl );
3589 if( ret != 0 )
3590 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003591
Jerry Yue7047812021-09-13 19:26:39 +08003592 ret = mbedtls_ssl_handle_pending_alert( ssl );
3593 if( ret != 0 )
3594 goto cleanup;
3595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003597 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003598 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3600 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003601
Ronald Cron9f0fba32022-02-10 16:45:15 +01003602 switch( ssl->state )
3603 {
3604 case MBEDTLS_SSL_HELLO_REQUEST:
3605 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3606 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003607
Ronald Cron9f0fba32022-02-10 16:45:15 +01003608 case MBEDTLS_SSL_CLIENT_HELLO:
3609 ret = mbedtls_ssl_write_client_hello( ssl );
3610 break;
3611
3612 default:
3613#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003614 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003615 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3616 else
3617 ret = mbedtls_ssl_handshake_client_step( ssl );
3618#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3619 ret = mbedtls_ssl_handshake_client_step( ssl );
3620#else
3621 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3622#endif
3623 }
Jerry Yub9930e72021-08-06 17:11:51 +08003624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003627 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003628 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003629#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003630 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003631 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003632#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003633
3634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3635 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3636 ret = mbedtls_ssl_handshake_server_step( ssl );
3637#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3638 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003639#endif
3640
Jerry Yue7047812021-09-13 19:26:39 +08003641 if( ret != 0 )
3642 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003643 /* handshake_step return error. And it is same
3644 * with alert_reason.
3645 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003646 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003647 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003648 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003649 goto cleanup;
3650 }
3651 }
3652
3653cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003654 return( ret );
3655}
3656
3657/*
3658 * Perform the SSL handshake
3659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003661{
3662 int ret = 0;
3663
Hanno Beckera817ea42020-10-20 15:20:23 +01003664 /* Sanity checks */
3665
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003666 if( ssl == NULL || ssl->conf == NULL )
3667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3668
Hanno Beckera817ea42020-10-20 15:20:23 +01003669#if defined(MBEDTLS_SSL_PROTO_DTLS)
3670 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3671 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3672 {
3673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3674 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3675 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3676 }
3677#endif /* MBEDTLS_SSL_PROTO_DTLS */
3678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003680
Hanno Beckera817ea42020-10-20 15:20:23 +01003681 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003682 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003685
3686 if( ret != 0 )
3687 break;
3688 }
3689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003691
3692 return( ret );
3693}
3694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#if defined(MBEDTLS_SSL_RENEGOTIATION)
3696#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003697/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003698 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003699 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003700MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003702{
Janos Follath865b3eb2019-12-16 11:46:15 +00003703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003706
3707 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3709 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003710
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003711 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003712 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003713 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003714 return( ret );
3715 }
3716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003718
3719 return( 0 );
3720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003722
3723/*
3724 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 * - any side: calling mbedtls_ssl_renegotiate(),
3726 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3727 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003728 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003729 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003731 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003732int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003733{
Janos Follath865b3eb2019-12-16 11:46:15 +00003734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003737
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003738 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3739 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003740
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003741 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3742 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003744 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003746 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003747 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003748 ssl->handshake->out_msg_seq = 1;
3749 else
3750 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003751 }
3752#endif
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3755 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003760 return( ret );
3761 }
3762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003764
3765 return( 0 );
3766}
3767
3768/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003769 * Renegotiate current connection on client,
3770 * or request renegotiation on server
3771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003775
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003776 if( ssl == NULL || ssl->conf == NULL )
3777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003780 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003781 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003782 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003783 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003787
3788 /* Did we already try/start sending HelloRequest? */
3789 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003791
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003792 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003797 /*
3798 * On client, either start the renegotiation process or,
3799 * if already in progress, continue the handshake
3800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003802 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003803 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003805
Hanno Becker40cdaa12020-02-05 10:48:27 +00003806 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003807 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003809 return( ret );
3810 }
3811 }
3812 else
3813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003817 return( ret );
3818 }
3819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003821
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003822 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003825
Gilles Peskine9b562d52018-04-25 20:32:43 +02003826void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003827{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003828 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3829
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003830 if( handshake == NULL )
3831 return;
3832
Brett Warrene0edc842021-08-17 09:53:13 +01003833#if defined(MBEDTLS_ECP_C)
3834#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3835 if ( ssl->handshake->group_list_heap_allocated )
3836 mbedtls_free( (void*) handshake->group_list );
3837 handshake->group_list = NULL;
3838#endif /* MBEDTLS_DEPRECATED_REMOVED */
3839#endif /* MBEDTLS_ECP_C */
3840
Ronald Crone68ab4f2022-10-05 12:46:29 +02003841#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003842#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3843 if ( ssl->handshake->sig_algs_heap_allocated )
3844 mbedtls_free( (void*) handshake->sig_algs );
3845 handshake->sig_algs = NULL;
3846#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003847#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3848 if( ssl->handshake->certificate_request_context )
3849 {
3850 mbedtls_free( (void*) handshake->certificate_request_context );
3851 }
3852#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003853#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003854
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003855#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3856 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3857 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003858 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003859 handshake->async_in_progress = 0;
3860 }
3861#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3862
Andrzej Kurek25f27152022-08-17 16:09:31 -04003863#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003864#if defined(MBEDTLS_USE_PSA_CRYPTO)
3865 psa_hash_abort( &handshake->fin_sha256_psa );
3866#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003867 mbedtls_sha256_free( &handshake->fin_sha256 );
3868#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003869#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003870#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003871#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003872 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003873#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003874 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003875#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003876#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#if defined(MBEDTLS_DHM_C)
3879 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003880#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003881#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003883#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003884#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003885 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003886#if defined(MBEDTLS_SSL_CLI_C)
3887 mbedtls_free( handshake->ecjpake_cache );
3888 handshake->ecjpake_cache = NULL;
3889 handshake->ecjpake_cache_len = 0;
3890#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003891#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003892
Janos Follath4ae5c292016-02-10 11:27:43 +00003893#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3894 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003895 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003897#endif
3898
Ronald Cron73fe8df2022-10-05 14:31:43 +02003899#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003900#if defined(MBEDTLS_USE_PSA_CRYPTO)
3901 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3902 {
3903 /* The maintenance of the external PSK key slot is the
3904 * user's responsibility. */
3905 if( ssl->handshake->psk_opaque_is_internal )
3906 {
3907 psa_destroy_key( ssl->handshake->psk_opaque );
3908 ssl->handshake->psk_opaque_is_internal = 0;
3909 }
3910 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3911 }
Neil Armstronge952a302022-05-03 10:22:14 +02003912#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003913 if( handshake->psk != NULL )
3914 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003915 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003916 mbedtls_free( handshake->psk );
3917 }
Neil Armstronge952a302022-05-03 10:22:14 +02003918#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003919#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3922 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003923 /*
3924 * Free only the linked list wrapper, not the keys themselves
3925 * since the belong to the SNI callback
3926 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003927 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003929
Gilles Peskineeccd8882020-03-10 12:19:08 +01003930#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003931 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003932 if( handshake->ecrs_peer_cert != NULL )
3933 {
3934 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3935 mbedtls_free( handshake->ecrs_peer_cert );
3936 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003937#endif
3938
Hanno Becker75173122019-02-06 16:18:31 +00003939#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3940 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3941 mbedtls_pk_free( &handshake->peer_pubkey );
3942#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3943
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003944#if defined(MBEDTLS_SSL_CLI_C) && \
3945 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3946 mbedtls_free( handshake->cookie );
3947#endif /* MBEDTLS_SSL_CLI_C &&
3948 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003949
3950#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003951 mbedtls_ssl_flight_free( handshake->flight );
3952 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003953#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003954
Ronald Cronf12b81d2022-03-15 10:42:41 +01003955#if defined(MBEDTLS_ECDH_C) && \
3956 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003957 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003958 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003959#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3960
Ronald Cron6f135e12021-12-08 16:57:54 +01003961#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003962 mbedtls_ssl_transform_free( handshake->transform_handshake );
3963 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003964 mbedtls_free( handshake->transform_earlydata );
3965 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003966#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003967
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003968
3969#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3970 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3971 * processes datagrams and the fact that a datagram is allowed to have
3972 * several records in it, it is possible that the I/O buffers are not
3973 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003974 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3975 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003976#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003977
Jerry Yuba9c7272021-10-30 11:54:10 +08003978 /* mbedtls_platform_zeroize MUST be last one in this function */
3979 mbedtls_platform_zeroize( handshake,
3980 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003981}
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003984{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003985 if( session == NULL )
3986 return;
3987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003989 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003990#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003991
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003992#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003993#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3994 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003995 mbedtls_free( session->hostname );
3996#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003998#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003999
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004000 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004001}
4002
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004003#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004004
4005#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4006#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4007#else
4008#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4009#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4010
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004011#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004012
4013#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4014#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4015#else
4016#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4017#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4018
4019#if defined(MBEDTLS_SSL_ALPN)
4020#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4021#else
4022#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4023#endif /* MBEDTLS_SSL_ALPN */
4024
4025#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4026#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4027#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4028#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4029
4030#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4031 ( (uint32_t) ( \
4032 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4033 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4034 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4035 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4036 0u ) )
4037
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004038static unsigned char ssl_serialized_context_header[] = {
4039 MBEDTLS_VERSION_MAJOR,
4040 MBEDTLS_VERSION_MINOR,
4041 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004042 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4043 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4044 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4045 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4046 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004047};
4048
Paul Bakker5121ce52009-01-03 21:22:43 +00004049/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004050 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004051 *
4052 * The format of the serialized data is:
4053 * (in the presentation language of TLS, RFC 8446 section 3)
4054 *
4055 * // header
4056 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004057 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004058 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004059 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004060 * Note: When updating the format, remember to keep these
4061 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004062 *
4063 * // session sub-structure
4064 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4065 * // transform sub-structure
4066 * uint8 random[64]; // ServerHello.random+ClientHello.random
4067 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4068 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4069 * // fields from ssl_context
4070 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4071 * uint64 in_window_top; // DTLS: last validated record seq_num
4072 * uint64 in_window; // DTLS: bitmask for replay protection
4073 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4074 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4075 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4076 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4077 *
4078 * Note that many fields of the ssl_context or sub-structures are not
4079 * serialized, as they fall in one of the following categories:
4080 *
4081 * 1. forced value (eg in_left must be 0)
4082 * 2. pointer to dynamically-allocated memory (eg session, transform)
4083 * 3. value can be re-derived from other data (eg session keys from MS)
4084 * 4. value was temporary (eg content of input buffer)
4085 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004086 */
4087int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4088 unsigned char *buf,
4089 size_t buf_len,
4090 size_t *olen )
4091{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004092 unsigned char *p = buf;
4093 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004094 size_t session_len;
4095 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004096
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004097 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004098 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4099 * this function's documentation.
4100 *
4101 * These are due to assumptions/limitations in the implementation. Some of
4102 * them are likely to stay (no handshake in progress) some might go away
4103 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004104 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004105 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00004106 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004107 {
4108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004110 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004111 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004112 {
4113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004115 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004116 /* Double-check that sub-structures are indeed ready */
4117 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004118 {
4119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004121 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004122 /* There must be no pending incoming or outgoing data */
4123 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004124 {
4125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004127 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004128 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004129 {
4130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004132 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004133 /* Protocol must be DLTS, not TLS */
4134 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004138 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004139 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004140 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004141 {
4142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004144 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004145 /* We must be using an AEAD ciphersuite */
4146 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004147 {
4148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004150 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004151 /* Renegotiation must not be enabled */
4152#if defined(MBEDTLS_SSL_RENEGOTIATION)
4153 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004154 {
4155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004157 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004158#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004159
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004160 /*
4161 * Version and format identifier
4162 */
4163 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004164
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004165 if( used <= buf_len )
4166 {
4167 memcpy( p, ssl_serialized_context_header,
4168 sizeof( ssl_serialized_context_header ) );
4169 p += sizeof( ssl_serialized_context_header );
4170 }
4171
4172 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004173 * Session (length + data)
4174 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004175 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004176 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4177 return( ret );
4178
4179 used += 4 + session_len;
4180 if( used <= buf_len )
4181 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004182 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4183 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004184
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004185 ret = ssl_session_save( ssl->session, 1,
4186 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004187 if( ret != 0 )
4188 return( ret );
4189
4190 p += session_len;
4191 }
4192
4193 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004194 * Transform
4195 */
4196 used += sizeof( ssl->transform->randbytes );
4197 if( used <= buf_len )
4198 {
4199 memcpy( p, ssl->transform->randbytes,
4200 sizeof( ssl->transform->randbytes ) );
4201 p += sizeof( ssl->transform->randbytes );
4202 }
4203
4204#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4205 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4206 if( used <= buf_len )
4207 {
4208 *p++ = ssl->transform->in_cid_len;
4209 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4210 p += ssl->transform->in_cid_len;
4211
4212 *p++ = ssl->transform->out_cid_len;
4213 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4214 p += ssl->transform->out_cid_len;
4215 }
4216#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4217
4218 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004219 * Saved fields from top-level ssl_context structure
4220 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004221 used += 4;
4222 if( used <= buf_len )
4223 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004224 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4225 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004226 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004227
4228#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4229 used += 16;
4230 if( used <= buf_len )
4231 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004232 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4233 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004234
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004235 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4236 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004237 }
4238#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4239
4240#if defined(MBEDTLS_SSL_PROTO_DTLS)
4241 used += 1;
4242 if( used <= buf_len )
4243 {
4244 *p++ = ssl->disable_datagram_packing;
4245 }
4246#endif /* MBEDTLS_SSL_PROTO_DTLS */
4247
Jerry Yuae0b2e22021-10-08 15:21:19 +08004248 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004249 if( used <= buf_len )
4250 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004251 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4252 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004253 }
4254
4255#if defined(MBEDTLS_SSL_PROTO_DTLS)
4256 used += 2;
4257 if( used <= buf_len )
4258 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004259 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4260 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004261 }
4262#endif /* MBEDTLS_SSL_PROTO_DTLS */
4263
4264#if defined(MBEDTLS_SSL_ALPN)
4265 {
4266 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004267 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004268 : 0;
4269
4270 used += 1 + alpn_len;
4271 if( used <= buf_len )
4272 {
4273 *p++ = alpn_len;
4274
4275 if( ssl->alpn_chosen != NULL )
4276 {
4277 memcpy( p, ssl->alpn_chosen, alpn_len );
4278 p += alpn_len;
4279 }
4280 }
4281 }
4282#endif /* MBEDTLS_SSL_ALPN */
4283
4284 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004285 * Done
4286 */
4287 *olen = used;
4288
4289 if( used > buf_len )
4290 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004291
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004292 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4293
Hanno Becker43aefe22020-02-05 10:44:56 +00004294 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004295}
4296
4297/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004298 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004299 *
4300 * This internal version is wrapped by a public function that cleans up in
4301 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004302 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004303MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004304static int ssl_context_load( mbedtls_ssl_context *ssl,
4305 const unsigned char *buf,
4306 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004307{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004308 const unsigned char *p = buf;
4309 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004310 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004312#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004313 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004314#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004315
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004316 /*
4317 * The context should have been freshly setup or reset.
4318 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004319 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004320 * renegotiating, or if the user mistakenly loaded a session first.)
4321 */
4322 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4323 ssl->session != NULL )
4324 {
4325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4326 }
4327
4328 /*
4329 * We can't check that the config matches the initial one, but we can at
4330 * least check it matches the requirements for serializing.
4331 */
4332 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004333 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4334 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004335#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004336 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004337#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004338 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004339 {
4340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4341 }
4342
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004343 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4344
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004345 /*
4346 * Check version identifier
4347 */
4348 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4350
4351 if( memcmp( p, ssl_serialized_context_header,
4352 sizeof( ssl_serialized_context_header ) ) != 0 )
4353 {
4354 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4355 }
4356 p += sizeof( ssl_serialized_context_header );
4357
4358 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004359 * Session
4360 */
4361 if( (size_t)( end - p ) < 4 )
4362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4363
4364 session_len = ( (size_t) p[0] << 24 ) |
4365 ( (size_t) p[1] << 16 ) |
4366 ( (size_t) p[2] << 8 ) |
4367 ( (size_t) p[3] );
4368 p += 4;
4369
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004370 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004371 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004372 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004373 ssl->session_in = ssl->session;
4374 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004375 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004376
4377 if( (size_t)( end - p ) < session_len )
4378 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4379
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004380 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004381 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004382 {
4383 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004384 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004385 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004386
4387 p += session_len;
4388
4389 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004390 * Transform
4391 */
4392
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004393 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004394 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004395 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004396 ssl->transform_in = ssl->transform;
4397 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004398 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004399
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004401 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4402 if( prf_func == NULL )
4403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4404
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004405 /* Read random bytes and populate structure */
4406 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004408
Hanno Beckerbd257552021-03-22 06:59:27 +00004409 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004410 ssl->session->ciphersuite,
4411 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004412#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004413 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004414#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004415 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004416 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004417 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004418 ssl->conf->endpoint,
4419 ssl );
4420 if( ret != 0 )
4421 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004422#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004423 p += sizeof( ssl->transform->randbytes );
4424
4425#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4426 /* Read connection IDs and store them */
4427 if( (size_t)( end - p ) < 1 )
4428 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4429
4430 ssl->transform->in_cid_len = *p++;
4431
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004432 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4434
4435 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4436 p += ssl->transform->in_cid_len;
4437
4438 ssl->transform->out_cid_len = *p++;
4439
4440 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4442
4443 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4444 p += ssl->transform->out_cid_len;
4445#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4446
4447 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004448 * Saved fields from top-level ssl_context structure
4449 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004450 if( (size_t)( end - p ) < 4 )
4451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4452
4453 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4454 ( (uint32_t) p[1] << 16 ) |
4455 ( (uint32_t) p[2] << 8 ) |
4456 ( (uint32_t) p[3] );
4457 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004458
4459#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4460 if( (size_t)( end - p ) < 16 )
4461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4462
4463 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4464 ( (uint64_t) p[1] << 48 ) |
4465 ( (uint64_t) p[2] << 40 ) |
4466 ( (uint64_t) p[3] << 32 ) |
4467 ( (uint64_t) p[4] << 24 ) |
4468 ( (uint64_t) p[5] << 16 ) |
4469 ( (uint64_t) p[6] << 8 ) |
4470 ( (uint64_t) p[7] );
4471 p += 8;
4472
4473 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4474 ( (uint64_t) p[1] << 48 ) |
4475 ( (uint64_t) p[2] << 40 ) |
4476 ( (uint64_t) p[3] << 32 ) |
4477 ( (uint64_t) p[4] << 24 ) |
4478 ( (uint64_t) p[5] << 16 ) |
4479 ( (uint64_t) p[6] << 8 ) |
4480 ( (uint64_t) p[7] );
4481 p += 8;
4482#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4483
4484#if defined(MBEDTLS_SSL_PROTO_DTLS)
4485 if( (size_t)( end - p ) < 1 )
4486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4487
4488 ssl->disable_datagram_packing = *p++;
4489#endif /* MBEDTLS_SSL_PROTO_DTLS */
4490
Jerry Yud9a94fe2021-09-28 18:58:59 +08004491 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004493 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4494 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004495
4496#if defined(MBEDTLS_SSL_PROTO_DTLS)
4497 if( (size_t)( end - p ) < 2 )
4498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4499
4500 ssl->mtu = ( p[0] << 8 ) | p[1];
4501 p += 2;
4502#endif /* MBEDTLS_SSL_PROTO_DTLS */
4503
4504#if defined(MBEDTLS_SSL_ALPN)
4505 {
4506 uint8_t alpn_len;
4507 const char **cur;
4508
4509 if( (size_t)( end - p ) < 1 )
4510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4511
4512 alpn_len = *p++;
4513
4514 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4515 {
4516 /* alpn_chosen should point to an item in the configured list */
4517 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4518 {
4519 if( strlen( *cur ) == alpn_len &&
4520 memcmp( p, cur, alpn_len ) == 0 )
4521 {
4522 ssl->alpn_chosen = *cur;
4523 break;
4524 }
4525 }
4526 }
4527
4528 /* can only happen on conf mismatch */
4529 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4531
4532 p += alpn_len;
4533 }
4534#endif /* MBEDTLS_SSL_ALPN */
4535
4536 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004537 * Forced fields from top-level ssl_context structure
4538 *
4539 * Most of them already set to the correct value by mbedtls_ssl_init() and
4540 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4541 */
4542 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004543 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004544
Hanno Becker361b10d2019-08-30 10:42:49 +01004545 /* Adjust pointers for header fields of outgoing records to
4546 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004547 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004548
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004549#if defined(MBEDTLS_SSL_PROTO_DTLS)
4550 ssl->in_epoch = 1;
4551#endif
4552
4553 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4554 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004555 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4556 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004557 if( ssl->handshake != NULL )
4558 {
4559 mbedtls_ssl_handshake_free( ssl );
4560 mbedtls_free( ssl->handshake );
4561 ssl->handshake = NULL;
4562 }
4563
4564 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004565 * Done - should have consumed entire buffer
4566 */
4567 if( p != end )
4568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004569
4570 return( 0 );
4571}
4572
4573/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004574 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004575 */
4576int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4577 const unsigned char *buf,
4578 size_t len )
4579{
4580 int ret = ssl_context_load( context, buf, len );
4581
4582 if( ret != 0 )
4583 mbedtls_ssl_free( context );
4584
4585 return( ret );
4586}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004587#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004588
4589/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 * Free an SSL context
4591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004593{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004594 if( ssl == NULL )
4595 return;
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004599 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 {
sander-visserb8aa2072020-05-06 22:05:13 +02004601#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4602 size_t out_buf_len = ssl->out_buf_len;
4603#else
4604 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4605#endif
4606
Darryl Greenb33cc762019-11-28 14:29:44 +00004607 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004609 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004610 }
4611
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004612 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 {
sander-visserb8aa2072020-05-06 22:05:13 +02004614#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4615 size_t in_buf_len = ssl->in_buf_len;
4616#else
4617 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4618#endif
4619
Darryl Greenb33cc762019-11-28 14:29:44 +00004620 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004622 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 }
4624
Paul Bakker48916f92012-09-16 19:57:18 +00004625 if( ssl->transform )
4626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 mbedtls_ssl_transform_free( ssl->transform );
4628 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004629 }
4630
4631 if( ssl->handshake )
4632 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004633 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4635 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 mbedtls_free( ssl->handshake );
4638 mbedtls_free( ssl->transform_negotiate );
4639 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004640 }
4641
Ronald Cron6f135e12021-12-08 16:57:54 +01004642#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004643 mbedtls_ssl_transform_free( ssl->transform_application );
4644 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004645#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004646
Paul Bakkerc0463502013-02-14 11:19:38 +01004647 if( ssl->session )
4648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 mbedtls_ssl_session_free( ssl->session );
4650 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004651 }
4652
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004653#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004654 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004656 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004660
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004661#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004663#endif
4664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004666
Paul Bakker86f04f42013-02-14 11:20:09 +01004667 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004668 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004669}
4670
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004671/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004672 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004673 */
4674void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4675{
4676 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4677}
4678
Gilles Peskineae270bf2021-06-02 00:05:29 +02004679/* The selection should be the same as mbedtls_x509_crt_profile_default in
4680 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004681 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004682 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004683 * about this list.
4684 */
Brett Warrene0edc842021-08-17 09:53:13 +01004685static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004686#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004687 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004688#endif
4689#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004690 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004691#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004692#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004693 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004694#endif
4695#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004696 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004697#endif
4698#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004699 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004700#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004701#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004702 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004703#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004704#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004705 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004706#endif
4707#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004708 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004709#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004710 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004711};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004712
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004713static int ssl_preset_suiteb_ciphersuites[] = {
4714 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4715 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4716 0
4717};
4718
Ronald Crone68ab4f2022-10-05 12:46:29 +02004719#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004720
Jerry Yu909df7b2022-01-22 11:56:27 +08004721/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004722 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004723 * rules SHOULD be upheld.
4724 * - No duplicate entries.
4725 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004726 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004727 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004728 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004729static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004730
Andrzej Kurek25f27152022-08-17 16:09:31 -04004731#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 +08004732 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4733 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004734#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004735 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004736
Andrzej Kurek25f27152022-08-17 16:09:31 -04004737#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 +08004738 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4739 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004740#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004741 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4742
Andrzej Kurek25f27152022-08-17 16:09:31 -04004743#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 +08004744 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4745 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004746#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004747 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004748
Andrzej Kurek25f27152022-08-17 16:09:31 -04004749#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 +08004750 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004751#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 +08004752
Andrzej Kurek25f27152022-08-17 16:09:31 -04004753#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 +08004754 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004755#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 +08004756
Andrzej Kurek25f27152022-08-17 16:09:31 -04004757#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 +08004758 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004759#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 +08004760
Andrzej Kurek25f27152022-08-17 16:09:31 -04004761#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 +08004762 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4763#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4764
Andrzej Kurek25f27152022-08-17 16:09:31 -04004765#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 +08004766 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4767#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4768
Andrzej Kurek25f27152022-08-17 16:09:31 -04004769#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 +08004770 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4771#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4772
Gabor Mezei15b95a62022-05-09 16:37:58 +02004773 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004774};
4775
4776/* NOTICE: see above */
4777#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4778static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004779#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004780#if defined(MBEDTLS_ECDSA_C)
4781 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004782#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004783#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4784 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4785#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004786#if defined(MBEDTLS_RSA_C)
4787 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4788#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004789#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004790#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004791#if defined(MBEDTLS_ECDSA_C)
4792 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004793#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004794#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4795 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4796#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004797#if defined(MBEDTLS_RSA_C)
4798 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4799#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004800#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004801#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004802#if defined(MBEDTLS_ECDSA_C)
4803 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004804#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004805#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4806 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4807#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004808#if defined(MBEDTLS_RSA_C)
4809 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4810#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004811#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004812 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004813};
4814#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4815/* NOTICE: see above */
4816static uint16_t ssl_preset_suiteb_sig_algs[] = {
4817
Andrzej Kurek25f27152022-08-17 16:09:31 -04004818#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 +08004819 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4820 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004821#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004822 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4823
Andrzej Kurek25f27152022-08-17 16:09:31 -04004824#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 +08004825 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4826 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004827#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004828 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004829
Andrzej Kurek25f27152022-08-17 16:09:31 -04004830#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 +08004831 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004832#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 +08004833
Andrzej Kurek25f27152022-08-17 16:09:31 -04004834#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 +08004835 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004836#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004837
Gabor Mezei15b95a62022-05-09 16:37:58 +02004838 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004839};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004840
Jerry Yu909df7b2022-01-22 11:56:27 +08004841/* NOTICE: see above */
4842#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4843static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004844#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004845#if defined(MBEDTLS_ECDSA_C)
4846 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004847#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004848#if defined(MBEDTLS_RSA_C)
4849 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4850#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004851#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004852#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004853#if defined(MBEDTLS_ECDSA_C)
4854 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004855#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004856#if defined(MBEDTLS_RSA_C)
4857 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4858#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004859#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004860 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004861};
Jerry Yu909df7b2022-01-22 11:56:27 +08004862#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4863
Ronald Crone68ab4f2022-10-05 12:46:29 +02004864#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004865
Brett Warrene0edc842021-08-17 09:53:13 +01004866static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004867#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004868 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004869#endif
4870#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004871 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004872#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004873 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004874};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004875
Ronald Crone68ab4f2022-10-05 12:46:29 +02004876#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004877/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004878 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004879MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004880static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004881{
4882 size_t i, j;
4883 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004884
Gabor Mezei15b95a62022-05-09 16:37:58 +02004885 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004886 {
Jerry Yuf377d642022-01-25 10:43:59 +08004887 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004888 {
Jerry Yuf377d642022-01-25 10:43:59 +08004889 if( sig_algs[i] != sig_algs[j] )
4890 continue;
4891 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4892 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4893 sig_algs[i], j, i );
4894 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004895 }
4896 }
4897 return( ret );
4898}
4899
Ronald Crone68ab4f2022-10-05 12:46:29 +02004900#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004901
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004902/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004903 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004904 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004905int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004906 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004907{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004908#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004910#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004911
Ronald Crone68ab4f2022-10-05 12:46:29 +02004912#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004913 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004914 {
4915 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4916 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4917 }
4918
Jerry Yuf377d642022-01-25 10:43:59 +08004919 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004920 {
4921 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4922 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4923 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004924
4925#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004926 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004927 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004928 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004929 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4930 }
4931
Jerry Yuf377d642022-01-25 10:43:59 +08004932 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004933 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004934 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004935 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4936 }
4937#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004938#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004939
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004940 /* Use the functions here so that they are covered in tests,
4941 * but otherwise access member directly for efficiency */
4942 mbedtls_ssl_conf_endpoint( conf, endpoint );
4943 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004944
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004945 /*
4946 * Things that are common to all presets
4947 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004948#if defined(MBEDTLS_SSL_CLI_C)
4949 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4950 {
4951 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4952#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4953 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4954#endif
4955 }
4956#endif
4957
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004958#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4959 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4960#endif
4961
4962#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4963 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4964#endif
4965
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004966#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004967 conf->f_cookie_write = ssl_cookie_write_dummy;
4968 conf->f_cookie_check = ssl_cookie_check_dummy;
4969#endif
4970
4971#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4972 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4973#endif
4974
Janos Follath088ce432017-04-10 12:42:31 +01004975#if defined(MBEDTLS_SSL_SRV_C)
4976 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004977 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004978#endif
4979
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004980#if defined(MBEDTLS_SSL_PROTO_DTLS)
4981 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4982 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4983#endif
4984
4985#if defined(MBEDTLS_SSL_RENEGOTIATION)
4986 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004987 memset( conf->renego_period, 0x00, 2 );
4988 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004989#endif
4990
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004991#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004992 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4993 {
4994 const unsigned char dhm_p[] =
4995 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4996 const unsigned char dhm_g[] =
4997 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004998
Hanno Beckere2defad2021-07-24 05:59:17 +01004999 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5000 dhm_p, sizeof( dhm_p ),
5001 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5002 {
5003 return( ret );
5004 }
5005 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005006#endif
5007
Ronald Cron6f135e12021-12-08 16:57:54 +01005008#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005009#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005010 mbedtls_ssl_conf_new_session_tickets(
5011 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5012#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005013 /*
5014 * Allow all TLS 1.3 key exchange modes by default.
5015 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005016 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005017#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005018
XiaokangQian4d3a6042022-04-21 13:46:17 +00005019 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5020 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005022 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005023 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005024#else
XiaokangQian060d8672022-04-21 09:24:56 +00005025 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005026#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005027 }
5028 else
5029 {
5030#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5031 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5032 {
5033 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5034 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5035 }
5036 else
5037 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5038 {
5039 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5040 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5041 }
5042#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5043 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5044 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5045#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5046 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5047 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5048#else
5049 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5050#endif
5051 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005052
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005053 /*
5054 * Preset-specific defaults
5055 */
5056 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005057 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005058 /*
5059 * NSA Suite B
5060 */
5061 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005062
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005063 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005064
5065#if defined(MBEDTLS_X509_CRT_PARSE_C)
5066 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005067#endif
5068
Ronald Crone68ab4f2022-10-05 12:46:29 +02005069#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005070#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5071 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5072 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5073 else
5074#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5075 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005076#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005077
Brett Warrene0edc842021-08-17 09:53:13 +01005078#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5079 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005080#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005081 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005082 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005083
5084 /*
5085 * Default
5086 */
5087 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005088
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005089 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005090
5091#if defined(MBEDTLS_X509_CRT_PARSE_C)
5092 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5093#endif
5094
Ronald Crone68ab4f2022-10-05 12:46:29 +02005095#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005096#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5097 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5098 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5099 else
5100#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5101 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005102#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005103
Brett Warrene0edc842021-08-17 09:53:13 +01005104#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5105 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005106#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005107 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005108
5109#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5110 conf->dhm_min_bitlen = 1024;
5111#endif
5112 }
5113
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005114 return( 0 );
5115}
5116
5117/*
5118 * Free mbedtls_ssl_config
5119 */
5120void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5121{
5122#if defined(MBEDTLS_DHM_C)
5123 mbedtls_mpi_free( &conf->dhm_P );
5124 mbedtls_mpi_free( &conf->dhm_G );
5125#endif
5126
Ronald Cron73fe8df2022-10-05 14:31:43 +02005127#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005128#if defined(MBEDTLS_USE_PSA_CRYPTO)
5129 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5130 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005131 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5132 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005133#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005134 if( conf->psk != NULL )
5135 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005136 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005137 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005138 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005139 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005140 }
5141
5142 if( conf->psk_identity != NULL )
5143 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005144 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005145 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005146 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005147 conf->psk_identity_len = 0;
5148 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005149#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005150
5151#if defined(MBEDTLS_X509_CRT_PARSE_C)
5152 ssl_key_cert_free( conf->key_cert );
5153#endif
5154
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005155 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005156}
5157
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005158#if defined(MBEDTLS_PK_C) && \
5159 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005160/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165#if defined(MBEDTLS_RSA_C)
5166 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5167 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169#if defined(MBEDTLS_ECDSA_C)
5170 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5171 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005173 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005174}
5175
Hanno Becker7e5437a2017-04-28 17:15:26 +01005176unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5177{
5178 switch( type ) {
5179 case MBEDTLS_PK_RSA:
5180 return( MBEDTLS_SSL_SIG_RSA );
5181 case MBEDTLS_PK_ECDSA:
5182 case MBEDTLS_PK_ECKEY:
5183 return( MBEDTLS_SSL_SIG_ECDSA );
5184 default:
5185 return( MBEDTLS_SSL_SIG_ANON );
5186 }
5187}
5188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005190{
5191 switch( sig )
5192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193#if defined(MBEDTLS_RSA_C)
5194 case MBEDTLS_SSL_SIG_RSA:
5195 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197#if defined(MBEDTLS_ECDSA_C)
5198 case MBEDTLS_SSL_SIG_ECDSA:
5199 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005200#endif
5201 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005203 }
5204}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005205#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005206
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005207/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005208 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005211{
5212 switch( hash )
5213 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005214#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 case MBEDTLS_SSL_HASH_MD5:
5216 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005217#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005218#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 case MBEDTLS_SSL_HASH_SHA1:
5220 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005221#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005222#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 case MBEDTLS_SSL_HASH_SHA224:
5224 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005225#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005226#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227 case MBEDTLS_SSL_HASH_SHA256:
5228 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005229#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005230#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 case MBEDTLS_SSL_HASH_SHA384:
5232 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005233#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005234#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235 case MBEDTLS_SSL_HASH_SHA512:
5236 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005237#endif
5238 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005239 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005240 }
5241}
5242
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005243/*
5244 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5245 */
5246unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5247{
5248 switch( md )
5249 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005250#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005251 case MBEDTLS_MD_MD5:
5252 return( MBEDTLS_SSL_HASH_MD5 );
5253#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005254#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005255 case MBEDTLS_MD_SHA1:
5256 return( MBEDTLS_SSL_HASH_SHA1 );
5257#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005258#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005259 case MBEDTLS_MD_SHA224:
5260 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005261#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005262#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005263 case MBEDTLS_MD_SHA256:
5264 return( MBEDTLS_SSL_HASH_SHA256 );
5265#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005266#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005267 case MBEDTLS_MD_SHA384:
5268 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005269#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005270#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005271 case MBEDTLS_MD_SHA512:
5272 return( MBEDTLS_SSL_HASH_SHA512 );
5273#endif
5274 default:
5275 return( MBEDTLS_SSL_HASH_NONE );
5276 }
5277}
5278
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005279/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005280 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005281 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005282 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005283int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005284{
Brett Warrene0edc842021-08-17 09:53:13 +01005285 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005286
Brett Warrene0edc842021-08-17 09:53:13 +01005287 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005288 return( -1 );
5289
Brett Warrene0edc842021-08-17 09:53:13 +01005290 for( ; *group_list != 0; group_list++ )
5291 {
5292 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005293 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005294 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005295
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005296 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005297}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005298
5299#if defined(MBEDTLS_ECP_C)
5300/*
5301 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5302 */
5303int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5304{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005305 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005306 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005307
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005308 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005309 return -1;
5310
5311 uint16_t tls_id = grp_info->tls_id;
5312
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005313 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5314}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005315#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317#if defined(MBEDTLS_X509_CRT_PARSE_C)
5318int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5319 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005320 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005321 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005322{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005323 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005324 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005325 const char *ext_oid;
5326 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005329 {
5330 /* Server part of the key exchange */
5331 switch( ciphersuite->key_exchange )
5332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 case MBEDTLS_KEY_EXCHANGE_RSA:
5334 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005335 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005336 break;
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5339 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5340 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5341 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005342 break;
5343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5345 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005346 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005347 break;
5348
5349 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 case MBEDTLS_KEY_EXCHANGE_NONE:
5351 case MBEDTLS_KEY_EXCHANGE_PSK:
5352 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5353 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005354 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005355 usage = 0;
5356 }
5357 }
5358 else
5359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5361 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005362 }
5363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005365 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005366 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005367 ret = -1;
5368 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5373 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005374 }
5375 else
5376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5378 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005379 }
5380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005382 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005383 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005384 ret = -1;
5385 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005386
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005387 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005390
Jerry Yu148165c2021-09-24 23:20:59 +08005391#if defined(MBEDTLS_USE_PSA_CRYPTO)
5392int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5393 const mbedtls_md_type_t md,
5394 unsigned char *dst,
5395 size_t dst_len,
5396 size_t *olen )
5397{
Ronald Cronf6893e12022-01-07 22:09:01 +01005398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5399 psa_hash_operation_t *hash_operation_to_clone;
5400 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5401
Jerry Yu148165c2021-09-24 23:20:59 +08005402 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005403
5404 switch( md )
5405 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005406#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005407 case MBEDTLS_MD_SHA384:
5408 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5409 break;
5410#endif
5411
Andrzej Kurek25f27152022-08-17 16:09:31 -04005412#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005413 case MBEDTLS_MD_SHA256:
5414 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5415 break;
5416#endif
5417
5418 default:
5419 goto exit;
5420 }
5421
5422 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5423 if( status != PSA_SUCCESS )
5424 goto exit;
5425
5426 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5427 if( status != PSA_SUCCESS )
5428 goto exit;
5429
5430exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005431#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5432 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5433 (void) ssl;
5434#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005435 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005436}
5437#else /* MBEDTLS_USE_PSA_CRYPTO */
5438
Andrzej Kurek25f27152022-08-17 16:09:31 -04005439#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005440MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005441static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5442 unsigned char *dst,
5443 size_t dst_len,
5444 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005445{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005446 int ret;
5447 mbedtls_sha512_context sha512;
5448
5449 if( dst_len < 48 )
5450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5451
5452 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005453 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005454
5455 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5456 {
5457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5458 goto exit;
5459 }
5460
5461 *olen = 48;
5462
5463exit:
5464
5465 mbedtls_sha512_free( &sha512 );
5466 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005467}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005468#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005469
Andrzej Kurek25f27152022-08-17 16:09:31 -04005470#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005471MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005472static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5473 unsigned char *dst,
5474 size_t dst_len,
5475 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005476{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005477 int ret;
5478 mbedtls_sha256_context sha256;
5479
5480 if( dst_len < 32 )
5481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5482
5483 mbedtls_sha256_init( &sha256 );
5484 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005485
Jerry Yu24c0ec32021-09-09 14:21:07 +08005486 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5487 {
5488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5489 goto exit;
5490 }
5491
5492 *olen = 32;
5493
5494exit:
5495
5496 mbedtls_sha256_free( &sha256 );
5497 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005498}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005499#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005500
Jerry Yu000f9762021-09-14 11:12:51 +08005501int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5502 const mbedtls_md_type_t md,
5503 unsigned char *dst,
5504 size_t dst_len,
5505 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005506{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005507 switch( md )
5508 {
5509
Andrzej Kurek25f27152022-08-17 16:09:31 -04005510#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005511 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005512 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005513#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005514
Andrzej Kurek25f27152022-08-17 16:09:31 -04005515#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005516 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005517 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005518#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005519
5520 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005521#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5522 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5523 (void) ssl;
5524 (void) dst;
5525 (void) dst_len;
5526 (void) olen;
5527#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005528 break;
5529 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005531}
XiaokangQian647719a2021-12-07 09:16:29 +00005532
Jerry Yu148165c2021-09-24 23:20:59 +08005533#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005534
Ronald Crone68ab4f2022-10-05 12:46:29 +02005535#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005536/* mbedtls_ssl_parse_sig_alg_ext()
5537 *
5538 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5539 * value (TLS 1.3 RFC8446):
5540 * enum {
5541 * ....
5542 * ecdsa_secp256r1_sha256( 0x0403 ),
5543 * ecdsa_secp384r1_sha384( 0x0503 ),
5544 * ecdsa_secp521r1_sha512( 0x0603 ),
5545 * ....
5546 * } SignatureScheme;
5547 *
5548 * struct {
5549 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5550 * } SignatureSchemeList;
5551 *
5552 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5553 * value (TLS 1.2 RFC5246):
5554 * enum {
5555 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5556 * sha512(6), (255)
5557 * } HashAlgorithm;
5558 *
5559 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5560 * SignatureAlgorithm;
5561 *
5562 * struct {
5563 * HashAlgorithm hash;
5564 * SignatureAlgorithm signature;
5565 * } SignatureAndHashAlgorithm;
5566 *
5567 * SignatureAndHashAlgorithm
5568 * supported_signature_algorithms<2..2^16-2>;
5569 *
5570 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5571 * generalization of the TLS 1.2 signature algorithm extension.
5572 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5573 * `SignatureScheme` field of TLS 1.3
5574 *
5575 */
5576int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5577 const unsigned char *buf,
5578 const unsigned char *end )
5579{
5580 const unsigned char *p = buf;
5581 size_t supported_sig_algs_len = 0;
5582 const unsigned char *supported_sig_algs_end;
5583 uint16_t sig_alg;
5584 uint32_t common_idx = 0;
5585
5586 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5587 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5588 p += 2;
5589
5590 memset( ssl->handshake->received_sig_algs, 0,
5591 sizeof(ssl->handshake->received_sig_algs) );
5592
5593 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5594 supported_sig_algs_end = p + supported_sig_algs_len;
5595 while( p < supported_sig_algs_end )
5596 {
5597 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5598 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5599 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005600 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5601 sig_alg,
5602 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005603#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005604 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005605 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5606 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5607 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005608 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005609 }
5610#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005611
Jerry Yuf3b46b52022-06-19 16:52:27 +08005612 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5613 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005614
5615 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5616 {
5617 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5618 common_idx += 1;
5619 }
5620 }
5621 /* Check that we consumed all the message. */
5622 if( p != end )
5623 {
5624 MBEDTLS_SSL_DEBUG_MSG( 1,
5625 ( "Signature algorithms extension length misaligned" ) );
5626 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5627 MBEDTLS_ERR_SSL_DECODE_ERROR );
5628 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5629 }
5630
5631 if( common_idx == 0 )
5632 {
5633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5634 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5635 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5636 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5637 }
5638
Gabor Mezei15b95a62022-05-09 16:37:58 +02005639 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005640 return( 0 );
5641}
5642
Ronald Crone68ab4f2022-10-05 12:46:29 +02005643#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005644
Jerry Yudc7bd172022-02-17 13:44:15 +08005645#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5646
5647#if defined(MBEDTLS_USE_PSA_CRYPTO)
5648
5649static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5650 mbedtls_svc_key_id_t key,
5651 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005652 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005653 const unsigned char* seed, size_t seed_length,
5654 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005655 const unsigned char* other_secret,
5656 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005657 size_t capacity )
5658{
5659 psa_status_t status;
5660
5661 status = psa_key_derivation_setup( derivation, alg );
5662 if( status != PSA_SUCCESS )
5663 return( status );
5664
5665 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5666 {
5667 status = psa_key_derivation_input_bytes( derivation,
5668 PSA_KEY_DERIVATION_INPUT_SEED,
5669 seed, seed_length );
5670 if( status != PSA_SUCCESS )
5671 return( status );
5672
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005673 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005674 {
5675 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005676 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5677 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005678 if( status != PSA_SUCCESS )
5679 return( status );
5680 }
5681
Jerry Yudc7bd172022-02-17 13:44:15 +08005682 if( mbedtls_svc_key_id_is_null( key ) )
5683 {
5684 status = psa_key_derivation_input_bytes(
5685 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005686 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005687 }
5688 else
5689 {
5690 status = psa_key_derivation_input_key(
5691 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5692 }
5693 if( status != PSA_SUCCESS )
5694 return( status );
5695
5696 status = psa_key_derivation_input_bytes( derivation,
5697 PSA_KEY_DERIVATION_INPUT_LABEL,
5698 label, label_length );
5699 if( status != PSA_SUCCESS )
5700 return( status );
5701 }
5702 else
5703 {
5704 return( PSA_ERROR_NOT_SUPPORTED );
5705 }
5706
5707 status = psa_key_derivation_set_capacity( derivation, capacity );
5708 if( status != PSA_SUCCESS )
5709 return( status );
5710
5711 return( PSA_SUCCESS );
5712}
5713
Andrzej Kurek57d10632022-10-24 10:32:01 -04005714#if defined(PSA_WANT_ALG_SHA_384) || \
5715 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005716MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005717static int tls_prf_generic( mbedtls_md_type_t md_type,
5718 const unsigned char *secret, size_t slen,
5719 const char *label,
5720 const unsigned char *random, size_t rlen,
5721 unsigned char *dstbuf, size_t dlen )
5722{
5723 psa_status_t status;
5724 psa_algorithm_t alg;
5725 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5726 psa_key_derivation_operation_t derivation =
5727 PSA_KEY_DERIVATION_OPERATION_INIT;
5728
5729 if( md_type == MBEDTLS_MD_SHA384 )
5730 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5731 else
5732 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5733
5734 /* Normally a "secret" should be long enough to be impossible to
5735 * find by brute force, and in particular should not be empty. But
5736 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5737 * and for this use case it makes sense to have a 0-length "secret".
5738 * Since the key API doesn't allow importing a key of length 0,
5739 * keep master_key=0, which setup_psa_key_derivation() understands
5740 * to mean a 0-length "secret" input. */
5741 if( slen != 0 )
5742 {
5743 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5744 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5745 psa_set_key_algorithm( &key_attributes, alg );
5746 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5747
5748 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5749 if( status != PSA_SUCCESS )
5750 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5751 }
5752
5753 status = setup_psa_key_derivation( &derivation,
5754 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005755 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005756 random, rlen,
5757 (unsigned char const *) label,
5758 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005759 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005760 dlen );
5761 if( status != PSA_SUCCESS )
5762 {
5763 psa_key_derivation_abort( &derivation );
5764 psa_destroy_key( master_key );
5765 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5766 }
5767
5768 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5769 if( status != PSA_SUCCESS )
5770 {
5771 psa_key_derivation_abort( &derivation );
5772 psa_destroy_key( master_key );
5773 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5774 }
5775
5776 status = psa_key_derivation_abort( &derivation );
5777 if( status != PSA_SUCCESS )
5778 {
5779 psa_destroy_key( master_key );
5780 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5781 }
5782
5783 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5784 status = psa_destroy_key( master_key );
5785 if( status != PSA_SUCCESS )
5786 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5787
5788 return( 0 );
5789}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005790#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005791#else /* MBEDTLS_USE_PSA_CRYPTO */
5792
Andrzej Kurek57d10632022-10-24 10:32:01 -04005793#if defined(MBEDTLS_MD_C) && \
5794 ( defined(MBEDTLS_SHA256_C) || \
5795 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005796MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005797static int tls_prf_generic( mbedtls_md_type_t md_type,
5798 const unsigned char *secret, size_t slen,
5799 const char *label,
5800 const unsigned char *random, size_t rlen,
5801 unsigned char *dstbuf, size_t dlen )
5802{
5803 size_t nb;
5804 size_t i, j, k, md_len;
5805 unsigned char *tmp;
5806 size_t tmp_len = 0;
5807 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5808 const mbedtls_md_info_t *md_info;
5809 mbedtls_md_context_t md_ctx;
5810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5811
5812 mbedtls_md_init( &md_ctx );
5813
5814 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5815 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5816
5817 md_len = mbedtls_md_get_size( md_info );
5818
5819 tmp_len = md_len + strlen( label ) + rlen;
5820 tmp = mbedtls_calloc( 1, tmp_len );
5821 if( tmp == NULL )
5822 {
5823 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5824 goto exit;
5825 }
5826
5827 nb = strlen( label );
5828 memcpy( tmp + md_len, label, nb );
5829 memcpy( tmp + md_len + nb, random, rlen );
5830 nb += rlen;
5831
5832 /*
5833 * Compute P_<hash>(secret, label + random)[0..dlen]
5834 */
5835 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5836 goto exit;
5837
5838 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5839 if( ret != 0 )
5840 goto exit;
5841 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5842 if( ret != 0 )
5843 goto exit;
5844 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5845 if( ret != 0 )
5846 goto exit;
5847
5848 for( i = 0; i < dlen; i += md_len )
5849 {
5850 ret = mbedtls_md_hmac_reset ( &md_ctx );
5851 if( ret != 0 )
5852 goto exit;
5853 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5854 if( ret != 0 )
5855 goto exit;
5856 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5857 if( ret != 0 )
5858 goto exit;
5859
5860 ret = mbedtls_md_hmac_reset ( &md_ctx );
5861 if( ret != 0 )
5862 goto exit;
5863 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5864 if( ret != 0 )
5865 goto exit;
5866 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5867 if( ret != 0 )
5868 goto exit;
5869
5870 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5871
5872 for( j = 0; j < k; j++ )
5873 dstbuf[i + j] = h_i[j];
5874 }
5875
5876exit:
5877 mbedtls_md_free( &md_ctx );
5878
5879 mbedtls_platform_zeroize( tmp, tmp_len );
5880 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5881
5882 mbedtls_free( tmp );
5883
5884 return( ret );
5885}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005886#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005887#endif /* MBEDTLS_USE_PSA_CRYPTO */
5888
Andrzej Kurek25f27152022-08-17 16:09:31 -04005889#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005890MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005891static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5892 const char *label,
5893 const unsigned char *random, size_t rlen,
5894 unsigned char *dstbuf, size_t dlen )
5895{
5896 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5897 label, random, rlen, dstbuf, dlen ) );
5898}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005899#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005900
Andrzej Kurek25f27152022-08-17 16:09:31 -04005901#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005902MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005903static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5904 const char *label,
5905 const unsigned char *random, size_t rlen,
5906 unsigned char *dstbuf, size_t dlen )
5907{
5908 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5909 label, random, rlen, dstbuf, dlen ) );
5910}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005911#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005912
Jerry Yuf009d862022-02-17 14:01:37 +08005913/*
5914 * Set appropriate PRF function and other SSL / TLS1.2 functions
5915 *
5916 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005917 * - hash associated with the ciphersuite (only used by TLS 1.2)
5918 *
5919 * Outputs:
5920 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5921 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005922MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005923static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005924 mbedtls_md_type_t hash )
5925{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005926#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005927 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005928 {
5929 handshake->tls_prf = tls_prf_sha384;
5930 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5931 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5932 }
5933 else
5934#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005935#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005936 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005937 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005938 handshake->tls_prf = tls_prf_sha256;
5939 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5940 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5941 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005942#else
Jerry Yuf009d862022-02-17 14:01:37 +08005943 {
Jerry Yuf009d862022-02-17 14:01:37 +08005944 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005945 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5947 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005948#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005949
5950 return( 0 );
5951}
Jerry Yud6ab2352022-02-17 14:03:43 +08005952
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005953/*
5954 * Compute master secret if needed
5955 *
5956 * Parameters:
5957 * [in/out] handshake
5958 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5959 * (PSA-PSK) ciphersuite_info, psk_opaque
5960 * [out] premaster (cleared)
5961 * [out] master
5962 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5963 * debug: conf->f_dbg, conf->p_dbg
5964 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005965 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005966 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005967MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005968static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5969 unsigned char *master,
5970 const mbedtls_ssl_context *ssl )
5971{
5972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5973
5974 /* cf. RFC 5246, Section 8.1:
5975 * "The master secret is always exactly 48 bytes in length." */
5976 size_t const master_secret_len = 48;
5977
5978#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5979 unsigned char session_hash[48];
5980#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5981
5982 /* The label for the KDF used for key expansion.
5983 * This is either "master secret" or "extended master secret"
5984 * depending on whether the Extended Master Secret extension
5985 * is used. */
5986 char const *lbl = "master secret";
5987
Przemek Stekielae4ed302022-04-05 17:15:55 +02005988 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005989 * - If the Extended Master Secret extension is not used,
5990 * this is ClientHello.Random + ServerHello.Random
5991 * (see Sect. 8.1 in RFC 5246).
5992 * - If the Extended Master Secret extension is used,
5993 * this is the transcript of the handshake so far.
5994 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005995 unsigned char const *seed = handshake->randbytes;
5996 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005997
5998#if !defined(MBEDTLS_DEBUG_C) && \
5999 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6000 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6001 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6002 ssl = NULL; /* make sure we don't use it except for those cases */
6003 (void) ssl;
6004#endif
6005
6006 if( handshake->resume != 0 )
6007 {
6008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6009 return( 0 );
6010 }
6011
6012#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6013 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6014 {
6015 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006016 seed = session_hash;
6017 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006018
6019 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006020 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006021 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006022#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006023
Przemek Stekiel99114f32022-04-22 11:20:09 +02006024#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006025 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006026 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006027 {
6028 /* Perform PSK-to-MS expansion in a single step. */
6029 psa_status_t status;
6030 psa_algorithm_t alg;
6031 mbedtls_svc_key_id_t psk;
6032 psa_key_derivation_operation_t derivation =
6033 PSA_KEY_DERIVATION_OPERATION_INIT;
6034 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6035
6036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6037
6038 psk = mbedtls_ssl_get_opaque_psk( ssl );
6039
6040 if( hash_alg == MBEDTLS_MD_SHA384 )
6041 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6042 else
6043 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6044
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006045 size_t other_secret_len = 0;
6046 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006047
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006048 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006049 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006050 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006051 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006052 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006053 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006054 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006055 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006056 other_secret_len = 48;
6057 other_secret = handshake->premaster + 2;
6058 break;
6059 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006060 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006061 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6062 other_secret = handshake->premaster + 2;
6063 break;
6064 default:
6065 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006066 }
6067
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006068 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006069 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006070 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006071 (unsigned char const *) lbl,
6072 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006073 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006074 master_secret_len );
6075 if( status != PSA_SUCCESS )
6076 {
6077 psa_key_derivation_abort( &derivation );
6078 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6079 }
6080
6081 status = psa_key_derivation_output_bytes( &derivation,
6082 master,
6083 master_secret_len );
6084 if( status != PSA_SUCCESS )
6085 {
6086 psa_key_derivation_abort( &derivation );
6087 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6088 }
6089
6090 status = psa_key_derivation_abort( &derivation );
6091 if( status != PSA_SUCCESS )
6092 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6093 }
6094 else
6095#endif
6096 {
6097 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006098 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006099 master,
6100 master_secret_len );
6101 if( ret != 0 )
6102 {
6103 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6104 return( ret );
6105 }
6106
6107 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6108 handshake->premaster,
6109 handshake->pmslen );
6110
6111 mbedtls_platform_zeroize( handshake->premaster,
6112 sizeof(handshake->premaster) );
6113 }
6114
6115 return( 0 );
6116}
6117
Jerry Yud62f87e2022-02-17 14:09:02 +08006118int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6119{
6120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6121 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6122 ssl->handshake->ciphersuite_info;
6123
6124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6125
6126 /* Set PRF, calc_verify and calc_finished function pointers */
6127 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006128 ciphersuite_info->mac );
6129 if( ret != 0 )
6130 {
6131 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6132 return( ret );
6133 }
6134
6135 /* Compute master secret if needed */
6136 ret = ssl_compute_master( ssl->handshake,
6137 ssl->session_negotiate->master,
6138 ssl );
6139 if( ret != 0 )
6140 {
6141 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6142 return( ret );
6143 }
6144
6145 /* Swap the client and server random values:
6146 * - MS derivation wanted client+server (RFC 5246 8.1)
6147 * - key derivation wants server+client (RFC 5246 6.3) */
6148 {
6149 unsigned char tmp[64];
6150 memcpy( tmp, ssl->handshake->randbytes, 64 );
6151 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6152 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6153 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6154 }
6155
6156 /* Populate transform structure */
6157 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6158 ssl->session_negotiate->ciphersuite,
6159 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006160#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006161 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006162#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006163 ssl->handshake->tls_prf,
6164 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006165 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006166 ssl->conf->endpoint,
6167 ssl );
6168 if( ret != 0 )
6169 {
6170 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6171 return( ret );
6172 }
6173
6174 /* We no longer need Server/ClientHello.random values */
6175 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6176 sizeof( ssl->handshake->randbytes ) );
6177
6178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6179
6180 return( 0 );
6181}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006182
Ronald Cron4dcbca92022-03-07 10:21:40 +01006183int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6184{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006185 switch( md )
6186 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006187#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006188 case MBEDTLS_SSL_HASH_SHA384:
6189 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6190 break;
6191#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006192#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006193 case MBEDTLS_SSL_HASH_SHA256:
6194 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6195 break;
6196#endif
6197 default:
6198 return( -1 );
6199 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006200#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6201 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6202 (void) ssl;
6203#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006204 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006205}
6206
Andrzej Kurek25f27152022-08-17 16:09:31 -04006207#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006208void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6209 unsigned char *hash,
6210 size_t *hlen )
6211{
6212#if defined(MBEDTLS_USE_PSA_CRYPTO)
6213 size_t hash_size;
6214 psa_status_t status;
6215 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6216
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6218 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6219 if( status != PSA_SUCCESS )
6220 {
6221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6222 return;
6223 }
6224
6225 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6226 if( status != PSA_SUCCESS )
6227 {
6228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6229 return;
6230 }
6231
6232 *hlen = 32;
6233 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6235#else
6236 mbedtls_sha256_context sha256;
6237
6238 mbedtls_sha256_init( &sha256 );
6239
6240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6241
6242 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6243 mbedtls_sha256_finish( &sha256, hash );
6244
6245 *hlen = 32;
6246
6247 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6249
6250 mbedtls_sha256_free( &sha256 );
6251#endif /* MBEDTLS_USE_PSA_CRYPTO */
6252 return;
6253}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006254#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006255
Andrzej Kurek25f27152022-08-17 16:09:31 -04006256#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006257void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6258 unsigned char *hash,
6259 size_t *hlen )
6260{
6261#if defined(MBEDTLS_USE_PSA_CRYPTO)
6262 size_t hash_size;
6263 psa_status_t status;
6264 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6265
6266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6267 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6268 if( status != PSA_SUCCESS )
6269 {
6270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6271 return;
6272 }
6273
6274 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6275 if( status != PSA_SUCCESS )
6276 {
6277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6278 return;
6279 }
6280
6281 *hlen = 48;
6282 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6284#else
6285 mbedtls_sha512_context sha512;
6286
6287 mbedtls_sha512_init( &sha512 );
6288
6289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6290
Andrzej Kureka242e832022-08-11 10:03:14 -04006291 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006292 mbedtls_sha512_finish( &sha512, hash );
6293
6294 *hlen = 48;
6295
6296 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6298
6299 mbedtls_sha512_free( &sha512 );
6300#endif /* MBEDTLS_USE_PSA_CRYPTO */
6301 return;
6302}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006303#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006304
Neil Armstrong80f6f322022-05-03 17:56:38 +02006305#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6306 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006307int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6308{
6309 unsigned char *p = ssl->handshake->premaster;
6310 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6311 const unsigned char *psk = NULL;
6312 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006313 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006314
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006315 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006316 {
6317 /*
6318 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006319 * checked before calling this function.
6320 *
6321 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006322 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006323 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006324 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6328 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006329 }
6330
6331 /*
6332 * PMS = struct {
6333 * opaque other_secret<0..2^16-1>;
6334 * opaque psk<0..2^16-1>;
6335 * };
6336 * with "other_secret" depending on the particular key exchange
6337 */
6338#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6339 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6340 {
6341 if( end - p < 2 )
6342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6343
6344 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6345 p += 2;
6346
6347 if( end < p || (size_t)( end - p ) < psk_len )
6348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6349
6350 memset( p, 0, psk_len );
6351 p += psk_len;
6352 }
6353 else
6354#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6355#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6356 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6357 {
6358 /*
6359 * other_secret already set by the ClientKeyExchange message,
6360 * and is 48 bytes long
6361 */
6362 if( end - p < 2 )
6363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6364
6365 *p++ = 0;
6366 *p++ = 48;
6367 p += 48;
6368 }
6369 else
6370#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6371#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6372 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6373 {
6374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6375 size_t len;
6376
6377 /* Write length only when we know the actual value */
6378 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6379 p + 2, end - ( p + 2 ), &len,
6380 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6381 {
6382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6383 return( ret );
6384 }
6385 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6386 p += 2 + len;
6387
6388 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6389 }
6390 else
6391#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006392#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006393 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6394 {
6395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6396 size_t zlen;
6397
6398 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6399 p + 2, end - ( p + 2 ),
6400 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6401 {
6402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6403 return( ret );
6404 }
6405
6406 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6407 p += 2 + zlen;
6408
6409 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6410 MBEDTLS_DEBUG_ECDH_Z );
6411 }
6412 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006413#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006414 {
6415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6417 }
6418
6419 /* opaque psk<0..2^16-1>; */
6420 if( end - p < 2 )
6421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6422
6423 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6424 p += 2;
6425
6426 if( end < p || (size_t)( end - p ) < psk_len )
6427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6428
6429 memcpy( p, psk, psk_len );
6430 p += psk_len;
6431
6432 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6433
6434 return( 0 );
6435}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006436#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006437
6438#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006439MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006440static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6441
6442#if defined(MBEDTLS_SSL_PROTO_DTLS)
6443int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6444{
6445 /* If renegotiation is not enforced, retransmit until we would reach max
6446 * timeout if we were using the usual handshake doubling scheme */
6447 if( ssl->conf->renego_max_records < 0 )
6448 {
6449 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6450 unsigned char doublings = 1;
6451
6452 while( ratio != 0 )
6453 {
6454 ++doublings;
6455 ratio >>= 1;
6456 }
6457
6458 if( ++ssl->renego_records_seen > doublings )
6459 {
6460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6461 return( 0 );
6462 }
6463 }
6464
6465 return( ssl_write_hello_request( ssl ) );
6466}
6467#endif
6468#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006469
Jerry Yud9526692022-02-17 14:23:47 +08006470/*
6471 * Handshake functions
6472 */
6473#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6474/* No certificate support -> dummy functions */
6475int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6476{
6477 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6478 ssl->handshake->ciphersuite_info;
6479
6480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6481
6482 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6483 {
6484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6485 ssl->state++;
6486 return( 0 );
6487 }
6488
6489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6490 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6491}
6492
6493int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6494{
6495 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6496 ssl->handshake->ciphersuite_info;
6497
6498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6499
6500 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6501 {
6502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6503 ssl->state++;
6504 return( 0 );
6505 }
6506
6507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6509}
6510
6511#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6512/* Some certificate support -> implement write and parse */
6513
6514int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6515{
6516 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6517 size_t i, n;
6518 const mbedtls_x509_crt *crt;
6519 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6520 ssl->handshake->ciphersuite_info;
6521
6522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6523
6524 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6525 {
6526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6527 ssl->state++;
6528 return( 0 );
6529 }
6530
6531#if defined(MBEDTLS_SSL_CLI_C)
6532 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6533 {
6534 if( ssl->handshake->client_auth == 0 )
6535 {
6536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6537 ssl->state++;
6538 return( 0 );
6539 }
6540 }
6541#endif /* MBEDTLS_SSL_CLI_C */
6542#if defined(MBEDTLS_SSL_SRV_C)
6543 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6544 {
6545 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6546 {
6547 /* Should never happen because we shouldn't have picked the
6548 * ciphersuite if we don't have a certificate. */
6549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6550 }
6551 }
6552#endif
6553
6554 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6555
6556 /*
6557 * 0 . 0 handshake type
6558 * 1 . 3 handshake length
6559 * 4 . 6 length of all certs
6560 * 7 . 9 length of cert. 1
6561 * 10 . n-1 peer certificate
6562 * n . n+2 length of cert. 2
6563 * n+3 . ... upper level cert, etc.
6564 */
6565 i = 7;
6566 crt = mbedtls_ssl_own_cert( ssl );
6567
6568 while( crt != NULL )
6569 {
6570 n = crt->raw.len;
6571 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6572 {
6573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6574 " > %" MBEDTLS_PRINTF_SIZET,
6575 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6576 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6577 }
6578
6579 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6580 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6581 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6582
6583 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6584 i += n; crt = crt->next;
6585 }
6586
6587 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6588 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6589 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6590
6591 ssl->out_msglen = i;
6592 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6593 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6594
6595 ssl->state++;
6596
6597 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6598 {
6599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6600 return( ret );
6601 }
6602
6603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6604
6605 return( ret );
6606}
6607
6608#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6609
6610#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006611MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006612static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6613 unsigned char *crt_buf,
6614 size_t crt_buf_len )
6615{
6616 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6617
6618 if( peer_crt == NULL )
6619 return( -1 );
6620
6621 if( peer_crt->raw.len != crt_buf_len )
6622 return( -1 );
6623
6624 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6625}
6626#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006627MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006628static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6629 unsigned char *crt_buf,
6630 size_t crt_buf_len )
6631{
6632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6633 unsigned char const * const peer_cert_digest =
6634 ssl->session->peer_cert_digest;
6635 mbedtls_md_type_t const peer_cert_digest_type =
6636 ssl->session->peer_cert_digest_type;
6637 mbedtls_md_info_t const * const digest_info =
6638 mbedtls_md_info_from_type( peer_cert_digest_type );
6639 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6640 size_t digest_len;
6641
6642 if( peer_cert_digest == NULL || digest_info == NULL )
6643 return( -1 );
6644
6645 digest_len = mbedtls_md_get_size( digest_info );
6646 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6647 return( -1 );
6648
6649 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6650 if( ret != 0 )
6651 return( -1 );
6652
6653 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6654}
6655#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6656#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6657
6658/*
6659 * Once the certificate message is read, parse it into a cert chain and
6660 * perform basic checks, but leave actual verification to the caller
6661 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006662MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006663static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6664 mbedtls_x509_crt *chain )
6665{
6666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6667#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6668 int crt_cnt=0;
6669#endif
6670 size_t i, n;
6671 uint8_t alert;
6672
6673 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6674 {
6675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6676 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6677 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6678 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6679 }
6680
6681 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6682 {
6683 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6684 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6685 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6686 }
6687
6688 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6689 {
6690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6691 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6692 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6693 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6694 }
6695
6696 i = mbedtls_ssl_hs_hdr_len( ssl );
6697
6698 /*
6699 * Same message structure as in mbedtls_ssl_write_certificate()
6700 */
6701 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6702
6703 if( ssl->in_msg[i] != 0 ||
6704 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6705 {
6706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6708 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6709 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6710 }
6711
6712 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6713 i += 3;
6714
6715 /* Iterate through and parse the CRTs in the provided chain. */
6716 while( i < ssl->in_hslen )
6717 {
6718 /* Check that there's room for the next CRT's length fields. */
6719 if ( i + 3 > ssl->in_hslen ) {
6720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6721 mbedtls_ssl_send_alert_message( ssl,
6722 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6724 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6725 }
6726 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6727 * anything beyond 2**16 ~ 64K. */
6728 if( ssl->in_msg[i] != 0 )
6729 {
6730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6731 mbedtls_ssl_send_alert_message( ssl,
6732 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6733 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6734 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6735 }
6736
6737 /* Read length of the next CRT in the chain. */
6738 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6739 | (unsigned int) ssl->in_msg[i + 2];
6740 i += 3;
6741
6742 if( n < 128 || i + n > ssl->in_hslen )
6743 {
6744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6745 mbedtls_ssl_send_alert_message( ssl,
6746 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6747 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6748 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6749 }
6750
6751 /* Check if we're handling the first CRT in the chain. */
6752#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6753 if( crt_cnt++ == 0 &&
6754 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6755 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6756 {
6757 /* During client-side renegotiation, check that the server's
6758 * end-CRTs hasn't changed compared to the initial handshake,
6759 * mitigating the triple handshake attack. On success, reuse
6760 * the original end-CRT instead of parsing it again. */
6761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6762 if( ssl_check_peer_crt_unchanged( ssl,
6763 &ssl->in_msg[i],
6764 n ) != 0 )
6765 {
6766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6767 mbedtls_ssl_send_alert_message( ssl,
6768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6769 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6770 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6771 }
6772
6773 /* Now we can safely free the original chain. */
6774 ssl_clear_peer_cert( ssl->session );
6775 }
6776#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6777
6778 /* Parse the next certificate in the chain. */
6779#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6780 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6781#else
6782 /* If we don't need to store the CRT chain permanently, parse
6783 * it in-place from the input buffer instead of making a copy. */
6784 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6785#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6786 switch( ret )
6787 {
6788 case 0: /*ok*/
6789 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6790 /* Ignore certificate with an unknown algorithm: maybe a
6791 prior certificate was already trusted. */
6792 break;
6793
6794 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6795 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6796 goto crt_parse_der_failed;
6797
6798 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6799 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6800 goto crt_parse_der_failed;
6801
6802 default:
6803 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6804 crt_parse_der_failed:
6805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6806 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6807 return( ret );
6808 }
6809
6810 i += n;
6811 }
6812
6813 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6814 return( 0 );
6815}
6816
6817#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006818MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006819static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6820{
6821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6822 return( -1 );
6823
6824 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6825 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6826 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6827 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6828 {
Ronald Cron19385882022-06-15 16:26:13 +02006829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006830 return( 0 );
6831 }
6832 return( -1 );
6833}
6834#endif /* MBEDTLS_SSL_SRV_C */
6835
6836/* Check if a certificate message is expected.
6837 * Return either
6838 * - SSL_CERTIFICATE_EXPECTED, or
6839 * - SSL_CERTIFICATE_SKIP
6840 * indicating whether a Certificate message is expected or not.
6841 */
6842#define SSL_CERTIFICATE_EXPECTED 0
6843#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006844MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006845static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6846 int authmode )
6847{
6848 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6849 ssl->handshake->ciphersuite_info;
6850
6851 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6852 return( SSL_CERTIFICATE_SKIP );
6853
6854#if defined(MBEDTLS_SSL_SRV_C)
6855 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6856 {
6857 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6858 return( SSL_CERTIFICATE_SKIP );
6859
6860 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6861 {
6862 ssl->session_negotiate->verify_result =
6863 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6864 return( SSL_CERTIFICATE_SKIP );
6865 }
6866 }
6867#else
6868 ((void) authmode);
6869#endif /* MBEDTLS_SSL_SRV_C */
6870
6871 return( SSL_CERTIFICATE_EXPECTED );
6872}
6873
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006874MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006875static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6876 int authmode,
6877 mbedtls_x509_crt *chain,
6878 void *rs_ctx )
6879{
6880 int ret = 0;
6881 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6882 ssl->handshake->ciphersuite_info;
6883 int have_ca_chain = 0;
6884
6885 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6886 void *p_vrfy;
6887
6888 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6889 return( 0 );
6890
6891 if( ssl->f_vrfy != NULL )
6892 {
6893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6894 f_vrfy = ssl->f_vrfy;
6895 p_vrfy = ssl->p_vrfy;
6896 }
6897 else
6898 {
6899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6900 f_vrfy = ssl->conf->f_vrfy;
6901 p_vrfy = ssl->conf->p_vrfy;
6902 }
6903
6904 /*
6905 * Main check: verify certificate
6906 */
6907#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6908 if( ssl->conf->f_ca_cb != NULL )
6909 {
6910 ((void) rs_ctx);
6911 have_ca_chain = 1;
6912
6913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6914 ret = mbedtls_x509_crt_verify_with_ca_cb(
6915 chain,
6916 ssl->conf->f_ca_cb,
6917 ssl->conf->p_ca_cb,
6918 ssl->conf->cert_profile,
6919 ssl->hostname,
6920 &ssl->session_negotiate->verify_result,
6921 f_vrfy, p_vrfy );
6922 }
6923 else
6924#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6925 {
6926 mbedtls_x509_crt *ca_chain;
6927 mbedtls_x509_crl *ca_crl;
6928
6929#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6930 if( ssl->handshake->sni_ca_chain != NULL )
6931 {
6932 ca_chain = ssl->handshake->sni_ca_chain;
6933 ca_crl = ssl->handshake->sni_ca_crl;
6934 }
6935 else
6936#endif
6937 {
6938 ca_chain = ssl->conf->ca_chain;
6939 ca_crl = ssl->conf->ca_crl;
6940 }
6941
6942 if( ca_chain != NULL )
6943 have_ca_chain = 1;
6944
6945 ret = mbedtls_x509_crt_verify_restartable(
6946 chain,
6947 ca_chain, ca_crl,
6948 ssl->conf->cert_profile,
6949 ssl->hostname,
6950 &ssl->session_negotiate->verify_result,
6951 f_vrfy, p_vrfy, rs_ctx );
6952 }
6953
6954 if( ret != 0 )
6955 {
6956 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6957 }
6958
6959#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6960 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6961 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6962#endif
6963
6964 /*
6965 * Secondary checks: always done, but change 'ret' only if it was 0
6966 */
6967
6968#if defined(MBEDTLS_ECP_C)
6969 {
6970 const mbedtls_pk_context *pk = &chain->pk;
6971
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006972 /* If certificate uses an EC key, make sure the curve is OK.
6973 * This is a public key, so it can't be opaque, so can_do() is a good
6974 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006975 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006976 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006977 /* and in the unlikely case the above assumption no longer holds
6978 * we are making sure that pk_ec() here does not return a NULL
6979 */
6980 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6981 if( ec == NULL )
6982 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6985 }
Jerry Yud9526692022-02-17 14:23:47 +08006986
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006987 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6988 {
6989 ssl->session_negotiate->verify_result |=
6990 MBEDTLS_X509_BADCERT_BAD_KEY;
6991
6992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6993 if( ret == 0 )
6994 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6995 }
Jerry Yud9526692022-02-17 14:23:47 +08006996 }
6997 }
6998#endif /* MBEDTLS_ECP_C */
6999
7000 if( mbedtls_ssl_check_cert_usage( chain,
7001 ciphersuite_info,
7002 ! ssl->conf->endpoint,
7003 &ssl->session_negotiate->verify_result ) != 0 )
7004 {
7005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7006 if( ret == 0 )
7007 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7008 }
7009
7010 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7011 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7012 * with details encoded in the verification flags. All other kinds
7013 * of error codes, including those from the user provided f_vrfy
7014 * functions, are treated as fatal and lead to a failure of
7015 * ssl_parse_certificate even if verification was optional. */
7016 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7017 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7018 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7019 {
7020 ret = 0;
7021 }
7022
7023 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7024 {
7025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7026 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7027 }
7028
7029 if( ret != 0 )
7030 {
7031 uint8_t alert;
7032
7033 /* The certificate may have been rejected for several reasons.
7034 Pick one and send the corresponding alert. Which alert to send
7035 may be a subject of debate in some cases. */
7036 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7037 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7038 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7039 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7040 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7041 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7042 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7043 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7044 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7045 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7046 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7047 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7048 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7049 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7050 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7051 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7052 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7053 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7054 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7055 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7056 else
7057 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7058 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7059 alert );
7060 }
7061
7062#if defined(MBEDTLS_DEBUG_C)
7063 if( ssl->session_negotiate->verify_result != 0 )
7064 {
7065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7066 (unsigned int) ssl->session_negotiate->verify_result ) );
7067 }
7068 else
7069 {
7070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7071 }
7072#endif /* MBEDTLS_DEBUG_C */
7073
7074 return( ret );
7075}
7076
7077#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007078MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007079static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7080 unsigned char *start, size_t len )
7081{
7082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7083 /* Remember digest of the peer's end-CRT. */
7084 ssl->session_negotiate->peer_cert_digest =
7085 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7086 if( ssl->session_negotiate->peer_cert_digest == NULL )
7087 {
7088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7089 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7090 mbedtls_ssl_send_alert_message( ssl,
7091 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7092 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7093
7094 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7095 }
7096
7097 ret = mbedtls_md( mbedtls_md_info_from_type(
7098 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7099 start, len,
7100 ssl->session_negotiate->peer_cert_digest );
7101
7102 ssl->session_negotiate->peer_cert_digest_type =
7103 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7104 ssl->session_negotiate->peer_cert_digest_len =
7105 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7106
7107 return( ret );
7108}
7109
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007110MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007111static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7112 unsigned char *start, size_t len )
7113{
7114 unsigned char *end = start + len;
7115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7116
7117 /* Make a copy of the peer's raw public key. */
7118 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7119 ret = mbedtls_pk_parse_subpubkey( &start, end,
7120 &ssl->handshake->peer_pubkey );
7121 if( ret != 0 )
7122 {
7123 /* We should have parsed the public key before. */
7124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7125 }
7126
7127 return( 0 );
7128}
7129#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7130
7131int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7132{
7133 int ret = 0;
7134 int crt_expected;
7135#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7136 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7137 ? ssl->handshake->sni_authmode
7138 : ssl->conf->authmode;
7139#else
7140 const int authmode = ssl->conf->authmode;
7141#endif
7142 void *rs_ctx = NULL;
7143 mbedtls_x509_crt *chain = NULL;
7144
7145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7146
7147 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7148 if( crt_expected == SSL_CERTIFICATE_SKIP )
7149 {
7150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7151 goto exit;
7152 }
7153
7154#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7155 if( ssl->handshake->ecrs_enabled &&
7156 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7157 {
7158 chain = ssl->handshake->ecrs_peer_cert;
7159 ssl->handshake->ecrs_peer_cert = NULL;
7160 goto crt_verify;
7161 }
7162#endif
7163
7164 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7165 {
7166 /* mbedtls_ssl_read_record may have sent an alert already. We
7167 let it decide whether to alert. */
7168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7169 goto exit;
7170 }
7171
7172#if defined(MBEDTLS_SSL_SRV_C)
7173 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7174 {
7175 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7176
7177 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7178 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7179
7180 goto exit;
7181 }
7182#endif /* MBEDTLS_SSL_SRV_C */
7183
7184 /* Clear existing peer CRT structure in case we tried to
7185 * reuse a session but it failed, and allocate a new one. */
7186 ssl_clear_peer_cert( ssl->session_negotiate );
7187
7188 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7189 if( chain == NULL )
7190 {
7191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7192 sizeof( mbedtls_x509_crt ) ) );
7193 mbedtls_ssl_send_alert_message( ssl,
7194 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7195 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7196
7197 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7198 goto exit;
7199 }
7200 mbedtls_x509_crt_init( chain );
7201
7202 ret = ssl_parse_certificate_chain( ssl, chain );
7203 if( ret != 0 )
7204 goto exit;
7205
7206#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7207 if( ssl->handshake->ecrs_enabled)
7208 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7209
7210crt_verify:
7211 if( ssl->handshake->ecrs_enabled)
7212 rs_ctx = &ssl->handshake->ecrs_ctx;
7213#endif
7214
7215 ret = ssl_parse_certificate_verify( ssl, authmode,
7216 chain, rs_ctx );
7217 if( ret != 0 )
7218 goto exit;
7219
7220#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7221 {
7222 unsigned char *crt_start, *pk_start;
7223 size_t crt_len, pk_len;
7224
7225 /* We parse the CRT chain without copying, so
7226 * these pointers point into the input buffer,
7227 * and are hence still valid after freeing the
7228 * CRT chain. */
7229
7230 crt_start = chain->raw.p;
7231 crt_len = chain->raw.len;
7232
7233 pk_start = chain->pk_raw.p;
7234 pk_len = chain->pk_raw.len;
7235
7236 /* Free the CRT structures before computing
7237 * digest and copying the peer's public key. */
7238 mbedtls_x509_crt_free( chain );
7239 mbedtls_free( chain );
7240 chain = NULL;
7241
7242 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7243 if( ret != 0 )
7244 goto exit;
7245
7246 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7247 if( ret != 0 )
7248 goto exit;
7249 }
7250#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7251 /* Pass ownership to session structure. */
7252 ssl->session_negotiate->peer_cert = chain;
7253 chain = NULL;
7254#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7255
7256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7257
7258exit:
7259
7260 if( ret == 0 )
7261 ssl->state++;
7262
7263#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7264 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7265 {
7266 ssl->handshake->ecrs_peer_cert = chain;
7267 chain = NULL;
7268 }
7269#endif
7270
7271 if( chain != NULL )
7272 {
7273 mbedtls_x509_crt_free( chain );
7274 mbedtls_free( chain );
7275 }
7276
7277 return( ret );
7278}
7279#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7280
Andrzej Kurek25f27152022-08-17 16:09:31 -04007281#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007282static void ssl_calc_finished_tls_sha256(
7283 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7284{
7285 int len = 12;
7286 const char *sender;
7287 unsigned char padbuf[32];
7288#if defined(MBEDTLS_USE_PSA_CRYPTO)
7289 size_t hash_size;
7290 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7291 psa_status_t status;
7292#else
7293 mbedtls_sha256_context sha256;
7294#endif
7295
7296 mbedtls_ssl_session *session = ssl->session_negotiate;
7297 if( !session )
7298 session = ssl->session;
7299
7300 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7301 ? "client finished"
7302 : "server finished";
7303
7304#if defined(MBEDTLS_USE_PSA_CRYPTO)
7305 sha256_psa = psa_hash_operation_init();
7306
7307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7308
7309 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7310 if( status != PSA_SUCCESS )
7311 {
7312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7313 return;
7314 }
7315
7316 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7317 if( status != PSA_SUCCESS )
7318 {
7319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7320 return;
7321 }
7322 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7323#else
7324
7325 mbedtls_sha256_init( &sha256 );
7326
7327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7328
7329 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7330
7331 /*
7332 * TLSv1.2:
7333 * hash = PRF( master, finished_label,
7334 * Hash( handshake ) )[0.11]
7335 */
7336
7337#if !defined(MBEDTLS_SHA256_ALT)
7338 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7339 sha256.state, sizeof( sha256.state ) );
7340#endif
7341
7342 mbedtls_sha256_finish( &sha256, padbuf );
7343 mbedtls_sha256_free( &sha256 );
7344#endif /* MBEDTLS_USE_PSA_CRYPTO */
7345
7346 ssl->handshake->tls_prf( session->master, 48, sender,
7347 padbuf, 32, buf, len );
7348
7349 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7350
7351 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7352
7353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7354}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007355#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007356
Jerry Yub7ba49e2022-02-17 14:25:53 +08007357
Andrzej Kurek25f27152022-08-17 16:09:31 -04007358#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007359static void ssl_calc_finished_tls_sha384(
7360 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7361{
7362 int len = 12;
7363 const char *sender;
7364 unsigned char padbuf[48];
7365#if defined(MBEDTLS_USE_PSA_CRYPTO)
7366 size_t hash_size;
7367 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7368 psa_status_t status;
7369#else
7370 mbedtls_sha512_context sha512;
7371#endif
7372
7373 mbedtls_ssl_session *session = ssl->session_negotiate;
7374 if( !session )
7375 session = ssl->session;
7376
7377 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7378 ? "client finished"
7379 : "server finished";
7380
7381#if defined(MBEDTLS_USE_PSA_CRYPTO)
7382 sha384_psa = psa_hash_operation_init();
7383
7384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7385
7386 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7387 if( status != PSA_SUCCESS )
7388 {
7389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7390 return;
7391 }
7392
7393 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7394 if( status != PSA_SUCCESS )
7395 {
7396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7397 return;
7398 }
7399 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7400#else
7401 mbedtls_sha512_init( &sha512 );
7402
7403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7404
Andrzej Kureka242e832022-08-11 10:03:14 -04007405 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007406
7407 /*
7408 * TLSv1.2:
7409 * hash = PRF( master, finished_label,
7410 * Hash( handshake ) )[0.11]
7411 */
7412
7413#if !defined(MBEDTLS_SHA512_ALT)
7414 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7415 sha512.state, sizeof( sha512.state ) );
7416#endif
7417 mbedtls_sha512_finish( &sha512, padbuf );
7418
7419 mbedtls_sha512_free( &sha512 );
7420#endif
7421
7422 ssl->handshake->tls_prf( session->master, 48, sender,
7423 padbuf, 48, buf, len );
7424
7425 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7426
7427 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7428
7429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7430}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007431#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007432
Jerry Yuaef00152022-02-17 14:27:31 +08007433void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7434{
7435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7436
7437 /*
7438 * Free our handshake params
7439 */
7440 mbedtls_ssl_handshake_free( ssl );
7441 mbedtls_free( ssl->handshake );
7442 ssl->handshake = NULL;
7443
7444 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007445 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007446 */
7447 if( ssl->transform )
7448 {
7449 mbedtls_ssl_transform_free( ssl->transform );
7450 mbedtls_free( ssl->transform );
7451 }
7452 ssl->transform = ssl->transform_negotiate;
7453 ssl->transform_negotiate = NULL;
7454
7455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7456}
7457
Jerry Yu2a9fff52022-02-17 14:28:51 +08007458void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7459{
7460 int resume = ssl->handshake->resume;
7461
7462 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7463
7464#if defined(MBEDTLS_SSL_RENEGOTIATION)
7465 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7466 {
7467 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7468 ssl->renego_records_seen = 0;
7469 }
7470#endif
7471
7472 /*
7473 * Free the previous session and switch in the current one
7474 */
7475 if( ssl->session )
7476 {
7477#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7478 /* RFC 7366 3.1: keep the EtM state */
7479 ssl->session_negotiate->encrypt_then_mac =
7480 ssl->session->encrypt_then_mac;
7481#endif
7482
7483 mbedtls_ssl_session_free( ssl->session );
7484 mbedtls_free( ssl->session );
7485 }
7486 ssl->session = ssl->session_negotiate;
7487 ssl->session_negotiate = NULL;
7488
7489 /*
7490 * Add cache entry
7491 */
7492 if( ssl->conf->f_set_cache != NULL &&
7493 ssl->session->id_len != 0 &&
7494 resume == 0 )
7495 {
7496 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7497 ssl->session->id,
7498 ssl->session->id_len,
7499 ssl->session ) != 0 )
7500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7501 }
7502
7503#if defined(MBEDTLS_SSL_PROTO_DTLS)
7504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7505 ssl->handshake->flight != NULL )
7506 {
7507 /* Cancel handshake timer */
7508 mbedtls_ssl_set_timer( ssl, 0 );
7509
7510 /* Keep last flight around in case we need to resend it:
7511 * we need the handshake and transform structures for that */
7512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7513 }
7514 else
7515#endif
7516 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7517
7518 ssl->state++;
7519
7520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7521}
7522
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007523int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7524{
7525 int ret, hash_len;
7526
7527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7528
7529 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7530
7531 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7532
7533 /*
7534 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7535 * may define some other value. Currently (early 2016), no defined
7536 * ciphersuite does this (and this is unlikely to change as activity has
7537 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7538 */
7539 hash_len = 12;
7540
7541#if defined(MBEDTLS_SSL_RENEGOTIATION)
7542 ssl->verify_data_len = hash_len;
7543 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7544#endif
7545
7546 ssl->out_msglen = 4 + hash_len;
7547 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7548 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7549
7550 /*
7551 * In case of session resuming, invert the client and server
7552 * ChangeCipherSpec messages order.
7553 */
7554 if( ssl->handshake->resume != 0 )
7555 {
7556#if defined(MBEDTLS_SSL_CLI_C)
7557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7558 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7559#endif
7560#if defined(MBEDTLS_SSL_SRV_C)
7561 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7562 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7563#endif
7564 }
7565 else
7566 ssl->state++;
7567
7568 /*
7569 * Switch to our negotiated transform and session parameters for outbound
7570 * data.
7571 */
7572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7573
7574#if defined(MBEDTLS_SSL_PROTO_DTLS)
7575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7576 {
7577 unsigned char i;
7578
7579 /* Remember current epoch settings for resending */
7580 ssl->handshake->alt_transform_out = ssl->transform_out;
7581 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7582 sizeof( ssl->handshake->alt_out_ctr ) );
7583
7584 /* Set sequence_number to zero */
7585 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7586
7587
7588 /* Increment epoch */
7589 for( i = 2; i > 0; i-- )
7590 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7591 break;
7592
7593 /* The loop goes to its end iff the counter is wrapping */
7594 if( i == 0 )
7595 {
7596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7597 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7598 }
7599 }
7600 else
7601#endif /* MBEDTLS_SSL_PROTO_DTLS */
7602 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7603
7604 ssl->transform_out = ssl->transform_negotiate;
7605 ssl->session_out = ssl->session_negotiate;
7606
7607#if defined(MBEDTLS_SSL_PROTO_DTLS)
7608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7609 mbedtls_ssl_send_flight_completed( ssl );
7610#endif
7611
7612 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7613 {
7614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7615 return( ret );
7616 }
7617
7618#if defined(MBEDTLS_SSL_PROTO_DTLS)
7619 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7620 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7621 {
7622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7623 return( ret );
7624 }
7625#endif
7626
7627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7628
7629 return( 0 );
7630}
7631
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007632#define SSL_MAX_HASH_LEN 12
7633
7634int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7635{
7636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7637 unsigned int hash_len = 12;
7638 unsigned char buf[SSL_MAX_HASH_LEN];
7639
7640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7641
7642 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7643
7644 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7645 {
7646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7647 goto exit;
7648 }
7649
7650 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7651 {
7652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7653 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7654 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7655 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7656 goto exit;
7657 }
7658
7659 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7660 {
7661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7662 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7663 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7664 goto exit;
7665 }
7666
7667 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7668 {
7669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7670 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7671 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7672 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7673 goto exit;
7674 }
7675
7676 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7677 buf, hash_len ) != 0 )
7678 {
7679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7680 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7681 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7682 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7683 goto exit;
7684 }
7685
7686#if defined(MBEDTLS_SSL_RENEGOTIATION)
7687 ssl->verify_data_len = hash_len;
7688 memcpy( ssl->peer_verify_data, buf, hash_len );
7689#endif
7690
7691 if( ssl->handshake->resume != 0 )
7692 {
7693#if defined(MBEDTLS_SSL_CLI_C)
7694 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7695 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7696#endif
7697#if defined(MBEDTLS_SSL_SRV_C)
7698 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7699 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7700#endif
7701 }
7702 else
7703 ssl->state++;
7704
7705#if defined(MBEDTLS_SSL_PROTO_DTLS)
7706 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7707 mbedtls_ssl_recv_flight_completed( ssl );
7708#endif
7709
7710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7711
7712exit:
7713 mbedtls_platform_zeroize( buf, hash_len );
7714 return( ret );
7715}
7716
Jerry Yu392112c2022-02-17 14:34:10 +08007717#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7718/*
7719 * Helper to get TLS 1.2 PRF from ciphersuite
7720 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7721 */
7722static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7723{
Jerry Yu392112c2022-02-17 14:34:10 +08007724 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007725 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7726#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007727 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007728 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007729 else
Jerry Yu392112c2022-02-17 14:34:10 +08007730#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007731#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7732 {
7733 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7734 return( tls_prf_sha256 );
7735 }
7736#endif
7737#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7738 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7739 (void) ciphersuite_info;
7740#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007741
Andrzej Kurek894edde2022-09-29 06:31:14 -04007742 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007743}
7744#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007745
7746static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7747{
7748 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007749#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007750 if( tls_prf == tls_prf_sha384 )
7751 {
7752 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7753 }
7754 else
7755#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007756#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007757 if( tls_prf == tls_prf_sha256 )
7758 {
7759 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7760 }
7761 else
7762#endif
7763 return( MBEDTLS_SSL_TLS_PRF_NONE );
7764}
7765
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007766/*
7767 * Populate a transform structure with session keys and all the other
7768 * necessary information.
7769 *
7770 * Parameters:
7771 * - [in/out]: transform: structure to populate
7772 * [in] must be just initialised with mbedtls_ssl_transform_init()
7773 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7774 * - [in] ciphersuite
7775 * - [in] master
7776 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007777 * - [in] tls_prf: pointer to PRF to use for key derivation
7778 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007779 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007780 * - [in] endpoint: client or server
7781 * - [in] ssl: used for:
7782 * - ssl->conf->{f,p}_export_keys
7783 * [in] optionally used for:
7784 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7785 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007786MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007787static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7788 int ciphersuite,
7789 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007790#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007791 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007792#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007793 ssl_tls_prf_t tls_prf,
7794 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007795 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007796 unsigned endpoint,
7797 const mbedtls_ssl_context *ssl )
7798{
7799 int ret = 0;
7800 unsigned char keyblk[256];
7801 unsigned char *key1;
7802 unsigned char *key2;
7803 unsigned char *mac_enc;
7804 unsigned char *mac_dec;
7805 size_t mac_key_len = 0;
7806 size_t iv_copy_len;
7807 size_t keylen;
7808 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007809 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007810#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007811 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007812 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007813#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007814
7815#if defined(MBEDTLS_USE_PSA_CRYPTO)
7816 psa_key_type_t key_type;
7817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7818 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007819 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007820 size_t key_bits;
7821 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7822#endif
7823
7824#if !defined(MBEDTLS_DEBUG_C) && \
7825 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7826 if( ssl->f_export_keys == NULL )
7827 {
7828 ssl = NULL; /* make sure we don't use it except for these cases */
7829 (void) ssl;
7830 }
7831#endif
7832
7833 /*
7834 * Some data just needs copying into the structure
7835 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007836#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007837 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007838#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007839 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007840
7841#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7842 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7843#endif
7844
7845#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007846 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007847 {
7848 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7849 * generation separate. This should never happen. */
7850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7851 }
7852#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7853
7854 /*
7855 * Get various info structures
7856 */
7857 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7858 if( ciphersuite_info == NULL )
7859 {
7860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7861 ciphersuite ) );
7862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7863 }
7864
Neil Armstrongab555e02022-04-04 11:07:59 +02007865 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007866#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007867 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007868#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007869 ciphersuite_info );
7870
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007871 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7872 transform->taglen =
7873 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7874
7875#if defined(MBEDTLS_USE_PSA_CRYPTO)
7876 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7877 transform->taglen,
7878 &alg,
7879 &key_type,
7880 &key_bits ) ) != PSA_SUCCESS )
7881 {
7882 ret = psa_ssl_status_to_mbedtls( status );
7883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7884 goto end;
7885 }
7886#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007887 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7888 if( cipher_info == NULL )
7889 {
7890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7891 ciphersuite_info->cipher ) );
7892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7893 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007894#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007895
Neil Armstronge4512952022-03-08 09:08:22 +01007896#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007897 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007898 if( mac_alg == 0 )
7899 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007901 (unsigned) ciphersuite_info->mac ) );
7902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7903 }
7904#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007905 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7906 if( md_info == NULL )
7907 {
7908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7909 (unsigned) ciphersuite_info->mac ) );
7910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7911 }
Neil Armstronge4512952022-03-08 09:08:22 +01007912#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007913
7914#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7915 /* Copy own and peer's CID if the use of the CID
7916 * extension has been negotiated. */
7917 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7918 {
7919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7920
7921 transform->in_cid_len = ssl->own_cid_len;
7922 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7923 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7924 transform->in_cid_len );
7925
7926 transform->out_cid_len = ssl->handshake->peer_cid_len;
7927 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7928 ssl->handshake->peer_cid_len );
7929 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7930 transform->out_cid_len );
7931 }
7932#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7933
7934 /*
7935 * Compute key block using the PRF
7936 */
7937 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7938 if( ret != 0 )
7939 {
7940 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7941 return( ret );
7942 }
7943
7944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7945 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7946 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7947 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7948 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7949
7950 /*
7951 * Determine the appropriate key, IV and MAC length.
7952 */
7953
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007954#if defined(MBEDTLS_USE_PSA_CRYPTO)
7955 keylen = PSA_BITS_TO_BYTES(key_bits);
7956#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007957 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007958#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007959
7960#if defined(MBEDTLS_GCM_C) || \
7961 defined(MBEDTLS_CCM_C) || \
7962 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007963 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007964 {
7965 size_t explicit_ivlen;
7966
7967 transform->maclen = 0;
7968 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007969
7970 /* All modes haves 96-bit IVs, but the length of the static parts vary
7971 * with mode and version:
7972 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7973 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7974 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7975 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7976 * sequence number).
7977 */
7978 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007979
7980 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007981#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007982 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007983#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007984 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7985 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007986#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007987
7988 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007989 transform->fixed_ivlen = 12;
7990 else
7991 transform->fixed_ivlen = 4;
7992
7993 /* Minimum length of encrypted record */
7994 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7995 transform->minlen = explicit_ivlen + transform->taglen;
7996 }
7997 else
7998#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7999#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008000 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8001 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8002 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008003 {
Neil Armstronge4512952022-03-08 09:08:22 +01008004#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008005 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008006#else
8007 size_t block_size = cipher_info->block_size;
8008#endif /* MBEDTLS_USE_PSA_CRYPTO */
8009
8010#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008011 /* Get MAC length */
8012 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8013#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008014 /* Initialize HMAC contexts */
8015 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8016 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8017 {
8018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8019 goto end;
8020 }
8021
8022 /* Get MAC length */
8023 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008024#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008025 transform->maclen = mac_key_len;
8026
8027 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008029 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008030#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008031 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008032#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008033
8034 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008035 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008036 transform->minlen = transform->maclen;
8037 else
8038 {
8039 /*
8040 * GenericBlockCipher:
8041 * 1. if EtM is in use: one block plus MAC
8042 * otherwise: * first multiple of blocklen greater than maclen
8043 * 2. IV
8044 */
8045#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008046 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008047 {
8048 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008049 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008050 }
8051 else
8052#endif
8053 {
8054 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008055 + block_size
8056 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008057 }
8058
Glenn Strauss07c64162022-03-14 12:34:51 -04008059 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008060 {
8061 transform->minlen += transform->ivlen;
8062 }
8063 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008064 {
8065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8066 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8067 goto end;
8068 }
8069 }
8070 }
8071 else
8072#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8073 {
8074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8076 }
8077
8078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8079 (unsigned) keylen,
8080 (unsigned) transform->minlen,
8081 (unsigned) transform->ivlen,
8082 (unsigned) transform->maclen ) );
8083
8084 /*
8085 * Finally setup the cipher contexts, IVs and MAC secrets.
8086 */
8087#if defined(MBEDTLS_SSL_CLI_C)
8088 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8089 {
8090 key1 = keyblk + mac_key_len * 2;
8091 key2 = keyblk + mac_key_len * 2 + keylen;
8092
8093 mac_enc = keyblk;
8094 mac_dec = keyblk + mac_key_len;
8095
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008096 iv_copy_len = ( transform->fixed_ivlen ) ?
8097 transform->fixed_ivlen : transform->ivlen;
8098 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8099 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8100 iv_copy_len );
8101 }
8102 else
8103#endif /* MBEDTLS_SSL_CLI_C */
8104#if defined(MBEDTLS_SSL_SRV_C)
8105 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8106 {
8107 key1 = keyblk + mac_key_len * 2 + keylen;
8108 key2 = keyblk + mac_key_len * 2;
8109
8110 mac_enc = keyblk + mac_key_len;
8111 mac_dec = keyblk;
8112
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008113 iv_copy_len = ( transform->fixed_ivlen ) ?
8114 transform->fixed_ivlen : transform->ivlen;
8115 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8116 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8117 iv_copy_len );
8118 }
8119 else
8120#endif /* MBEDTLS_SSL_SRV_C */
8121 {
8122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8123 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8124 goto end;
8125 }
8126
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008127 if( ssl != NULL && ssl->f_export_keys != NULL )
8128 {
8129 ssl->f_export_keys( ssl->p_export_keys,
8130 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8131 master, 48,
8132 randbytes + 32,
8133 randbytes,
8134 tls_prf_get_type( tls_prf ) );
8135 }
8136
8137#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008138 transform->psa_alg = alg;
8139
8140 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8141 {
8142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8143 psa_set_key_algorithm( &attributes, alg );
8144 psa_set_key_type( &attributes, key_type );
8145
8146 if( ( status = psa_import_key( &attributes,
8147 key1,
8148 PSA_BITS_TO_BYTES( key_bits ),
8149 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8150 {
8151 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8152 ret = psa_ssl_status_to_mbedtls( status );
8153 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8154 goto end;
8155 }
8156
8157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8158
8159 if( ( status = psa_import_key( &attributes,
8160 key2,
8161 PSA_BITS_TO_BYTES( key_bits ),
8162 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8163 {
8164 ret = psa_ssl_status_to_mbedtls( status );
8165 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8166 goto end;
8167 }
8168 }
8169#else
8170 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8171 cipher_info ) ) != 0 )
8172 {
8173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8174 goto end;
8175 }
8176
8177 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8178 cipher_info ) ) != 0 )
8179 {
8180 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8181 goto end;
8182 }
8183
8184 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8185 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8186 MBEDTLS_ENCRYPT ) ) != 0 )
8187 {
8188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8189 goto end;
8190 }
8191
8192 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8193 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8194 MBEDTLS_DECRYPT ) ) != 0 )
8195 {
8196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8197 goto end;
8198 }
8199
8200#if defined(MBEDTLS_CIPHER_MODE_CBC)
8201 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8202 {
8203 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8204 MBEDTLS_PADDING_NONE ) ) != 0 )
8205 {
8206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8207 goto end;
8208 }
8209
8210 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8211 MBEDTLS_PADDING_NONE ) ) != 0 )
8212 {
8213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8214 goto end;
8215 }
8216 }
8217#endif /* MBEDTLS_CIPHER_MODE_CBC */
8218#endif /* MBEDTLS_USE_PSA_CRYPTO */
8219
Neil Armstrong29c0c042022-03-17 17:47:28 +01008220#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8221 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8222 For AEAD-based ciphersuites, there is nothing to do here. */
8223 if( mac_key_len != 0 )
8224 {
8225#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008226 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008227
8228 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008229 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008230 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8231
8232 if( ( status = psa_import_key( &attributes,
8233 mac_enc, mac_key_len,
8234 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8235 {
8236 ret = psa_ssl_status_to_mbedtls( status );
8237 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8238 goto end;
8239 }
8240
Ronald Cronfb39f152022-03-25 14:36:28 +01008241 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008242 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8243#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8244 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8245#endif
8246 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008247 /* mbedtls_ct_hmac() requires the key to be exportable */
8248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8249 PSA_KEY_USAGE_VERIFY_HASH );
8250 else
8251 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8252
8253 if( ( status = psa_import_key( &attributes,
8254 mac_dec, mac_key_len,
8255 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8256 {
8257 ret = psa_ssl_status_to_mbedtls( status );
8258 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8259 goto end;
8260 }
8261#else
8262 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8263 if( ret != 0 )
8264 goto end;
8265 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8266 if( ret != 0 )
8267 goto end;
8268#endif /* MBEDTLS_USE_PSA_CRYPTO */
8269 }
8270#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8271
8272 ((void) mac_dec);
8273 ((void) mac_enc);
8274
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008275end:
8276 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8277 return( ret );
8278}
8279
Jerry Yuee40f9d2022-02-17 14:55:16 +08008280#if defined(MBEDTLS_USE_PSA_CRYPTO)
8281int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8282 unsigned char *hash, size_t *hashlen,
8283 unsigned char *data, size_t data_len,
8284 mbedtls_md_type_t md_alg )
8285{
8286 psa_status_t status;
8287 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008288 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008289
8290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8291
8292 if( ( status = psa_hash_setup( &hash_operation,
8293 hash_alg ) ) != PSA_SUCCESS )
8294 {
8295 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8296 goto exit;
8297 }
8298
8299 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8300 64 ) ) != PSA_SUCCESS )
8301 {
8302 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8303 goto exit;
8304 }
8305
8306 if( ( status = psa_hash_update( &hash_operation,
8307 data, data_len ) ) != PSA_SUCCESS )
8308 {
8309 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8310 goto exit;
8311 }
8312
8313 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8314 hashlen ) ) != PSA_SUCCESS )
8315 {
8316 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8317 goto exit;
8318 }
8319
8320exit:
8321 if( status != PSA_SUCCESS )
8322 {
8323 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8324 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8325 switch( status )
8326 {
8327 case PSA_ERROR_NOT_SUPPORTED:
8328 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8329 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8330 case PSA_ERROR_BUFFER_TOO_SMALL:
8331 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8332 case PSA_ERROR_INSUFFICIENT_MEMORY:
8333 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8334 default:
8335 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8336 }
8337 }
8338 return( 0 );
8339}
8340
8341#else
8342
8343int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8344 unsigned char *hash, size_t *hashlen,
8345 unsigned char *data, size_t data_len,
8346 mbedtls_md_type_t md_alg )
8347{
8348 int ret = 0;
8349 mbedtls_md_context_t ctx;
8350 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8351 *hashlen = mbedtls_md_get_size( md_info );
8352
8353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8354
8355 mbedtls_md_init( &ctx );
8356
8357 /*
8358 * digitally-signed struct {
8359 * opaque client_random[32];
8360 * opaque server_random[32];
8361 * ServerDHParams params;
8362 * };
8363 */
8364 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8365 {
8366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8367 goto exit;
8368 }
8369 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8370 {
8371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8372 goto exit;
8373 }
8374 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8375 {
8376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8377 goto exit;
8378 }
8379 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8380 {
8381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8382 goto exit;
8383 }
8384 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8385 {
8386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8387 goto exit;
8388 }
8389
8390exit:
8391 mbedtls_md_free( &ctx );
8392
8393 if( ret != 0 )
8394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8395 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8396
8397 return( ret );
8398}
8399#endif /* MBEDTLS_USE_PSA_CRYPTO */
8400
Jerry Yud9d91da2022-02-17 14:57:06 +08008401#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8402
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008403/* Find the preferred hash for a given signature algorithm. */
8404unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8405 mbedtls_ssl_context *ssl,
8406 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008407{
Gabor Mezei078e8032022-04-27 21:17:56 +02008408 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008409 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008410
8411 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008412 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008413
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008414 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008415 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008416 unsigned int hash_alg_received =
8417 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8418 received_sig_algs[i] );
8419 unsigned int sig_alg_received =
8420 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8421 received_sig_algs[i] );
8422
8423 if( sig_alg == sig_alg_received )
8424 {
8425#if defined(MBEDTLS_USE_PSA_CRYPTO)
8426 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8427 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008428 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008429 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008430
Neil Armstrong96eceb82022-06-30 18:05:05 +02008431 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8432 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8433 PSA_ALG_ECDSA( psa_hash_alg ),
8434 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008435 continue;
8436
Neil Armstrong96eceb82022-06-30 18:05:05 +02008437 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008438 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8439 PSA_ALG_RSA_PKCS1V15_SIGN(
8440 psa_hash_alg ),
8441 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008442 continue;
8443 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008444#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008445
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008446 return( hash_alg_received );
8447 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008448 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008449
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008450 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008451}
8452
8453#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8454
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008455/* Serialization of TLS 1.2 sessions:
8456 *
8457 * struct {
8458 * uint64 start_time;
8459 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008460 * uint8 session_id_len; // at most 32
8461 * opaque session_id[32];
8462 * opaque master[48]; // fixed length in the standard
8463 * uint32 verify_result;
8464 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8465 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8466 * uint32 ticket_lifetime;
8467 * uint8 mfl_code; // up to 255 according to standard
8468 * uint8 encrypt_then_mac; // 0 or 1
8469 * } serialized_session_tls12;
8470 *
8471 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008472static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008473 unsigned char *buf,
8474 size_t buf_len )
8475{
8476 unsigned char *p = buf;
8477 size_t used = 0;
8478
8479#if defined(MBEDTLS_HAVE_TIME)
8480 uint64_t start;
8481#endif
8482#if defined(MBEDTLS_X509_CRT_PARSE_C)
8483#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8484 size_t cert_len;
8485#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8486#endif /* MBEDTLS_X509_CRT_PARSE_C */
8487
8488 /*
8489 * Time
8490 */
8491#if defined(MBEDTLS_HAVE_TIME)
8492 used += 8;
8493
8494 if( used <= buf_len )
8495 {
8496 start = (uint64_t) session->start;
8497
8498 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8499 p += 8;
8500 }
8501#endif /* MBEDTLS_HAVE_TIME */
8502
8503 /*
8504 * Basic mandatory fields
8505 */
8506 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008507 + 1 /* id_len */
8508 + sizeof( session->id )
8509 + sizeof( session->master )
8510 + 4; /* verify_result */
8511
8512 if( used <= buf_len )
8513 {
8514 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8515 p += 2;
8516
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008517 *p++ = MBEDTLS_BYTE_0( session->id_len );
8518 memcpy( p, session->id, 32 );
8519 p += 32;
8520
8521 memcpy( p, session->master, 48 );
8522 p += 48;
8523
8524 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8525 p += 4;
8526 }
8527
8528 /*
8529 * Peer's end-entity certificate
8530 */
8531#if defined(MBEDTLS_X509_CRT_PARSE_C)
8532#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8533 if( session->peer_cert == NULL )
8534 cert_len = 0;
8535 else
8536 cert_len = session->peer_cert->raw.len;
8537
8538 used += 3 + cert_len;
8539
8540 if( used <= buf_len )
8541 {
8542 *p++ = MBEDTLS_BYTE_2( cert_len );
8543 *p++ = MBEDTLS_BYTE_1( cert_len );
8544 *p++ = MBEDTLS_BYTE_0( cert_len );
8545
8546 if( session->peer_cert != NULL )
8547 {
8548 memcpy( p, session->peer_cert->raw.p, cert_len );
8549 p += cert_len;
8550 }
8551 }
8552#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8553 if( session->peer_cert_digest != NULL )
8554 {
8555 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8556 if( used <= buf_len )
8557 {
8558 *p++ = (unsigned char) session->peer_cert_digest_type;
8559 *p++ = (unsigned char) session->peer_cert_digest_len;
8560 memcpy( p, session->peer_cert_digest,
8561 session->peer_cert_digest_len );
8562 p += session->peer_cert_digest_len;
8563 }
8564 }
8565 else
8566 {
8567 used += 2;
8568 if( used <= buf_len )
8569 {
8570 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8571 *p++ = 0;
8572 }
8573 }
8574#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8575#endif /* MBEDTLS_X509_CRT_PARSE_C */
8576
8577 /*
8578 * Session ticket if any, plus associated data
8579 */
8580#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8581 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8582
8583 if( used <= buf_len )
8584 {
8585 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8586 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8587 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8588
8589 if( session->ticket != NULL )
8590 {
8591 memcpy( p, session->ticket, session->ticket_len );
8592 p += session->ticket_len;
8593 }
8594
8595 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8596 p += 4;
8597 }
8598#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8599
8600 /*
8601 * Misc extension-related info
8602 */
8603#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8604 used += 1;
8605
8606 if( used <= buf_len )
8607 *p++ = session->mfl_code;
8608#endif
8609
8610#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8611 used += 1;
8612
8613 if( used <= buf_len )
8614 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8615#endif
8616
8617 return( used );
8618}
8619
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008620MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008621static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008622 const unsigned char *buf,
8623 size_t len )
8624{
8625#if defined(MBEDTLS_HAVE_TIME)
8626 uint64_t start;
8627#endif
8628#if defined(MBEDTLS_X509_CRT_PARSE_C)
8629#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8630 size_t cert_len;
8631#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8632#endif /* MBEDTLS_X509_CRT_PARSE_C */
8633
8634 const unsigned char *p = buf;
8635 const unsigned char * const end = buf + len;
8636
8637 /*
8638 * Time
8639 */
8640#if defined(MBEDTLS_HAVE_TIME)
8641 if( 8 > (size_t)( end - p ) )
8642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8643
8644 start = ( (uint64_t) p[0] << 56 ) |
8645 ( (uint64_t) p[1] << 48 ) |
8646 ( (uint64_t) p[2] << 40 ) |
8647 ( (uint64_t) p[3] << 32 ) |
8648 ( (uint64_t) p[4] << 24 ) |
8649 ( (uint64_t) p[5] << 16 ) |
8650 ( (uint64_t) p[6] << 8 ) |
8651 ( (uint64_t) p[7] );
8652 p += 8;
8653
8654 session->start = (time_t) start;
8655#endif /* MBEDTLS_HAVE_TIME */
8656
8657 /*
8658 * Basic mandatory fields
8659 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008660 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8662
8663 session->ciphersuite = ( p[0] << 8 ) | p[1];
8664 p += 2;
8665
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008666 session->id_len = *p++;
8667 memcpy( session->id, p, 32 );
8668 p += 32;
8669
8670 memcpy( session->master, p, 48 );
8671 p += 48;
8672
8673 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8674 ( (uint32_t) p[1] << 16 ) |
8675 ( (uint32_t) p[2] << 8 ) |
8676 ( (uint32_t) p[3] );
8677 p += 4;
8678
8679 /* Immediately clear invalid pointer values that have been read, in case
8680 * we exit early before we replaced them with valid ones. */
8681#if defined(MBEDTLS_X509_CRT_PARSE_C)
8682#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8683 session->peer_cert = NULL;
8684#else
8685 session->peer_cert_digest = NULL;
8686#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8687#endif /* MBEDTLS_X509_CRT_PARSE_C */
8688#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8689 session->ticket = NULL;
8690#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8691
8692 /*
8693 * Peer certificate
8694 */
8695#if defined(MBEDTLS_X509_CRT_PARSE_C)
8696#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8697 /* Deserialize CRT from the end of the ticket. */
8698 if( 3 > (size_t)( end - p ) )
8699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8700
8701 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8702 p += 3;
8703
8704 if( cert_len != 0 )
8705 {
8706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8707
8708 if( cert_len > (size_t)( end - p ) )
8709 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8710
8711 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8712
8713 if( session->peer_cert == NULL )
8714 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8715
8716 mbedtls_x509_crt_init( session->peer_cert );
8717
8718 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8719 p, cert_len ) ) != 0 )
8720 {
8721 mbedtls_x509_crt_free( session->peer_cert );
8722 mbedtls_free( session->peer_cert );
8723 session->peer_cert = NULL;
8724 return( ret );
8725 }
8726
8727 p += cert_len;
8728 }
8729#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8730 /* Deserialize CRT digest from the end of the ticket. */
8731 if( 2 > (size_t)( end - p ) )
8732 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8733
8734 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8735 session->peer_cert_digest_len = (size_t) *p++;
8736
8737 if( session->peer_cert_digest_len != 0 )
8738 {
8739 const mbedtls_md_info_t *md_info =
8740 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8741 if( md_info == NULL )
8742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8743 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8745
8746 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8748
8749 session->peer_cert_digest =
8750 mbedtls_calloc( 1, session->peer_cert_digest_len );
8751 if( session->peer_cert_digest == NULL )
8752 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8753
8754 memcpy( session->peer_cert_digest, p,
8755 session->peer_cert_digest_len );
8756 p += session->peer_cert_digest_len;
8757 }
8758#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8759#endif /* MBEDTLS_X509_CRT_PARSE_C */
8760
8761 /*
8762 * Session ticket and associated data
8763 */
8764#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8765 if( 3 > (size_t)( end - p ) )
8766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8767
8768 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8769 p += 3;
8770
8771 if( session->ticket_len != 0 )
8772 {
8773 if( session->ticket_len > (size_t)( end - p ) )
8774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8775
8776 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8777 if( session->ticket == NULL )
8778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8779
8780 memcpy( session->ticket, p, session->ticket_len );
8781 p += session->ticket_len;
8782 }
8783
8784 if( 4 > (size_t)( end - p ) )
8785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8786
8787 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8788 ( (uint32_t) p[1] << 16 ) |
8789 ( (uint32_t) p[2] << 8 ) |
8790 ( (uint32_t) p[3] );
8791 p += 4;
8792#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8793
8794 /*
8795 * Misc extension-related info
8796 */
8797#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8798 if( 1 > (size_t)( end - p ) )
8799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8800
8801 session->mfl_code = *p++;
8802#endif
8803
8804#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8805 if( 1 > (size_t)( end - p ) )
8806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8807
8808 session->encrypt_then_mac = *p++;
8809#endif
8810
8811 /* Done, should have consumed entire buffer */
8812 if( p != end )
8813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8814
8815 return( 0 );
8816}
Jerry Yudc7bd172022-02-17 13:44:15 +08008817#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8818
XiaokangQian75d40ef2022-04-20 11:05:24 +00008819int mbedtls_ssl_validate_ciphersuite(
8820 const mbedtls_ssl_context *ssl,
8821 const mbedtls_ssl_ciphersuite_t *suite_info,
8822 mbedtls_ssl_protocol_version min_tls_version,
8823 mbedtls_ssl_protocol_version max_tls_version )
8824{
8825 (void) ssl;
8826
8827 if( suite_info == NULL )
8828 return( -1 );
8829
8830 if( ( suite_info->min_tls_version > max_tls_version ) ||
8831 ( suite_info->max_tls_version < min_tls_version ) )
8832 {
8833 return( -1 );
8834 }
8835
XiaokangQian060d8672022-04-21 09:24:56 +00008836#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008837#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8838 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8839 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8840 {
8841 return( -1 );
8842 }
8843#endif
8844
8845 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8846#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8847 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8848 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8849 {
8850 return( -1 );
8851 }
8852#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8854
8855 return( 0 );
8856}
8857
Ronald Crone68ab4f2022-10-05 12:46:29 +02008858#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008859/*
8860 * Function for writing a signature algorithm extension.
8861 *
8862 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8863 * value (TLS 1.3 RFC8446):
8864 * enum {
8865 * ....
8866 * ecdsa_secp256r1_sha256( 0x0403 ),
8867 * ecdsa_secp384r1_sha384( 0x0503 ),
8868 * ecdsa_secp521r1_sha512( 0x0603 ),
8869 * ....
8870 * } SignatureScheme;
8871 *
8872 * struct {
8873 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8874 * } SignatureSchemeList;
8875 *
8876 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8877 * value (TLS 1.2 RFC5246):
8878 * enum {
8879 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8880 * sha512(6), (255)
8881 * } HashAlgorithm;
8882 *
8883 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8884 * SignatureAlgorithm;
8885 *
8886 * struct {
8887 * HashAlgorithm hash;
8888 * SignatureAlgorithm signature;
8889 * } SignatureAndHashAlgorithm;
8890 *
8891 * SignatureAndHashAlgorithm
8892 * supported_signature_algorithms<2..2^16-2>;
8893 *
8894 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8895 * generalization of the TLS 1.2 signature algorithm extension.
8896 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8897 * `SignatureScheme` field of TLS 1.3
8898 *
8899 */
8900int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8901 const unsigned char *end, size_t *out_len )
8902{
8903 unsigned char *p = buf;
8904 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8905 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8906
8907 *out_len = 0;
8908
8909 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8910
8911 /* Check if we have space for header and length field:
8912 * - extension_type (2 bytes)
8913 * - extension_data_length (2 bytes)
8914 * - supported_signature_algorithms_length (2 bytes)
8915 */
8916 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8917 p += 6;
8918
8919 /*
8920 * Write supported_signature_algorithms
8921 */
8922 supported_sig_alg = p;
8923 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8924 if( sig_alg == NULL )
8925 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8926
8927 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8928 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8930 *sig_alg,
8931 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008932 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8933 continue;
8934 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8935 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8936 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008938 *sig_alg,
8939 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008940 }
8941
8942 /* Length of supported_signature_algorithms */
8943 supported_sig_alg_len = p - supported_sig_alg;
8944 if( supported_sig_alg_len == 0 )
8945 {
8946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8948 }
8949
XiaokangQianeaf36512022-04-24 09:07:44 +00008950 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008951 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008952 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8953
XiaokangQianeaf36512022-04-24 09:07:44 +00008954 *out_len = p - buf;
8955
8956#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08008957 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00008958#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08008959
XiaokangQianeaf36512022-04-24 09:07:44 +00008960 return( 0 );
8961}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008962#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008963
XiaokangQian40a35232022-05-07 09:02:40 +00008964#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008965/*
8966 * mbedtls_ssl_parse_server_name_ext
8967 *
8968 * Structure of server_name extension:
8969 *
8970 * enum {
8971 * host_name(0), (255)
8972 * } NameType;
8973 * opaque HostName<1..2^16-1>;
8974 *
8975 * struct {
8976 * NameType name_type;
8977 * select (name_type) {
8978 * case host_name: HostName;
8979 * } name;
8980 * } ServerName;
8981 * struct {
8982 * ServerName server_name_list<1..2^16-1>
8983 * } ServerNameList;
8984 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008985MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008986int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8987 const unsigned char *buf,
8988 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008989{
8990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8991 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008992 size_t server_name_list_len, hostname_len;
8993 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008994
XiaokangQianf2a94202022-05-20 06:44:24 +00008995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008996
8997 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008998 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008999 p += 2;
9000
XiaokangQian9b2b7712022-05-17 02:57:00 +00009001 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9002 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009003 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009004 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009005 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009006 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009007 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9008 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009009
9010 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9011 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009012 /* sni_name is intended to be used only during the parsing of the
9013 * ClientHello message (it is reset to NULL before the end of
9014 * the message parsing). Thus it is ok to just point to the
9015 * reception buffer and not make a copy of it.
9016 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009017 ssl->handshake->sni_name = p + 3;
9018 ssl->handshake->sni_name_len = hostname_len;
9019 if( ssl->conf->f_sni == NULL )
9020 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009021 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009022 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009023 if( ret != 0 )
9024 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009025 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009026 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9027 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009028 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9029 }
9030 return( 0 );
9031 }
9032
9033 p += hostname_len + 3;
9034 }
9035
9036 return( 0 );
9037}
9038#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9039
XiaokangQianacb39922022-06-17 10:18:48 +00009040#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009041MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009042int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9043 const unsigned char *buf,
9044 const unsigned char *end )
9045{
9046 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009047 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009048 const unsigned char *protocol_name_list;
9049 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009050 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009051
9052 /* If ALPN not configured, just ignore the extension */
9053 if( ssl->conf->alpn_list == NULL )
9054 return( 0 );
9055
9056 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009057 * RFC7301, section 3.1
9058 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009059 *
XiaokangQianc7403452022-06-23 03:24:12 +00009060 * struct {
9061 * ProtocolName protocol_name_list<2..2^16-1>
9062 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009063 */
9064
XiaokangQianc7403452022-06-23 03:24:12 +00009065 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009066 * protocol_name_list_len 2 bytes
9067 * protocol_name_len 1 bytes
9068 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009069 */
XiaokangQianacb39922022-06-17 10:18:48 +00009070 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9071
XiaokangQianc7403452022-06-23 03:24:12 +00009072 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009073 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009074 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009075 protocol_name_list = p;
9076 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009077
9078 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009079 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009080 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009081 protocol_name_len = *p++;
9082 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9083 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009084 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009085 {
9086 MBEDTLS_SSL_PEND_FATAL_ALERT(
9087 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9088 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009089 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009090 }
9091
9092 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009093 }
9094
9095 /* Use our order of preference */
9096 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9097 {
9098 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009099 p = protocol_name_list;
9100 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009101 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009102 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009103 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009104 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009105 {
9106 ssl->alpn_chosen = *alpn;
9107 return( 0 );
9108 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009109
9110 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009111 }
9112 }
9113
XiaokangQian95d5f542022-06-24 02:29:26 +00009114 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009115 MBEDTLS_SSL_PEND_FATAL_ALERT(
9116 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9117 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9118 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9119}
9120
9121int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9122 unsigned char *buf,
9123 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009124 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009125{
9126 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009127 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009128 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009129
9130 if( ssl->alpn_chosen == NULL )
9131 {
9132 return( 0 );
9133 }
9134
XiaokangQian95d5f542022-06-24 02:29:26 +00009135 protocol_name_len = strlen( ssl->alpn_chosen );
9136 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009137
9138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9139 /*
9140 * 0 . 1 ext identifier
9141 * 2 . 3 ext length
9142 * 4 . 5 protocol list length
9143 * 6 . 6 protocol name length
9144 * 7 . 7+n protocol name
9145 */
9146 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9147
XiaokangQian95d5f542022-06-24 02:29:26 +00009148 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009149
XiaokangQian95d5f542022-06-24 02:29:26 +00009150 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9151 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009152 /* Note: the length of the chosen protocol has been checked to be less
9153 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9154 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009155 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009156
XiaokangQian95d5f542022-06-24 02:29:26 +00009157 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009158
9159#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9160 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9161#endif
9162
XiaokangQianacb39922022-06-17 10:18:48 +00009163 return ( 0 );
9164}
9165#endif /* MBEDTLS_SSL_ALPN */
9166
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009167#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009168 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009169 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009170 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009171int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9172 const char *hostname )
9173{
9174 /* Initialize to suppress unnecessary compiler warning */
9175 size_t hostname_len = 0;
9176
9177 /* Check if new hostname is valid before
9178 * making any change to current one */
9179 if( hostname != NULL )
9180 {
9181 hostname_len = strlen( hostname );
9182
9183 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9185 }
9186
9187 /* Now it's clear that we will overwrite the old hostname,
9188 * so we can free it safely */
9189 if( session->hostname != NULL )
9190 {
9191 mbedtls_platform_zeroize( session->hostname,
9192 strlen( session->hostname ) );
9193 mbedtls_free( session->hostname );
9194 }
9195
9196 /* Passing NULL as hostname shall clear the old one */
9197 if( hostname == NULL )
9198 {
9199 session->hostname = NULL;
9200 }
9201 else
9202 {
9203 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9204 if( session->hostname == NULL )
9205 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9206
9207 memcpy( session->hostname, hostname, hostname_len );
9208 }
9209
9210 return( 0 );
9211}
Xiaokang Qian03409292022-10-12 02:49:52 +00009212#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9213 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009214 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009215 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217#endif /* MBEDTLS_SSL_TLS_C */