blob: 47c02a60349d4edd27da3a9f2ed62bc113fbda0b [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 ||
Jerry Yu6848a612022-10-27 13:03:26 +0800171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yud25cab02022-10-31 12:48:30 +0800619#if defined(MBEDTLS_DEBUG_C)
620static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800621 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800622 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
623 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
624 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
625 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
626 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
627 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
628 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
629 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
630 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
631 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
632 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
633 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
634 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
635 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
636 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
637 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
638 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
639 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
640 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
641 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
642 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
643 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
644 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
646 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
647 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
648 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
649};
650
Jerry Yuea52ed92022-11-08 21:01:17 +0800651static unsigned int extension_type_table[]={
Jerry Yud25cab02022-10-31 12:48:30 +0800652 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
653 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
654 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
655 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
657 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
658 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
659 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
660 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
661 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
662 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
664 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
665 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
666 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
668 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
669 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
670 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
671 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
672 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
673 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
674 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
675 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
676 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
677 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
678 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
679 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
680};
681
682const char *mbedtls_ssl_get_extension_name( unsigned int extension_type )
683{
684 return( extension_name_table[
685 mbedtls_ssl_get_extension_id( extension_type ) ] );
686}
687
688static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type )
689{
690 switch( hs_msg_type )
691 {
692 case MBEDTLS_SSL_HS_CLIENT_HELLO:
693 return( "ClientHello" );
694 case MBEDTLS_SSL_HS_SERVER_HELLO:
695 return( "ServerHello" );
696 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
697 return( "HelloRetryRequest" );
698 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
699 return( "NewSessionTicket" );
700 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
701 return( "EncryptedExtensions" );
702 case MBEDTLS_SSL_HS_CERTIFICATE:
703 return( "Certificate" );
704 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
705 return( "CertificateRequest" );
706 }
Jerry Yu79aa7212022-11-08 21:30:21 +0800707 return( "Unknown" );
Jerry Yud25cab02022-10-31 12:48:30 +0800708}
709
Jerry Yu79aa7212022-11-08 21:30:21 +0800710void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type, unsigned int extension_type,
713 const char *extra_msg0, const char *extra_msg1 )
Jerry Yud25cab02022-10-31 12:48:30 +0800714{
715 const char *extra_msg;
716 if( extra_msg0 && extra_msg1 )
717 {
718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
720 "%s: %s(%u) extension %s %s.",
721 ssl_tls13_get_hs_msg_name( hs_msg_type ),
722 mbedtls_ssl_get_extension_name( extension_type ),
723 extension_type,
724 extra_msg0, extra_msg1 );
725 return;
726 }
727
728 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
729 if( extra_msg )
730 {
731 mbedtls_debug_print_msg(
732 ssl, level, file, line,
733 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
734 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
735 extra_msg );
736 return;
737 }
738
739 mbedtls_debug_print_msg(
740 ssl, level, file, line,
741 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
742 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
743}
744
745void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
746 int level, const char *file, int line,
747 int hs_msg_type, uint32_t extensions_mask,
748 const char *extra )
749{
750
751 for( unsigned i = 0;
752 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
753 i++ )
754 {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yu97be6a92022-11-09 22:43:31 +0800757 extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra );
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
761#endif /* MBEDTLS_DEBUG_C */
762
Jerry Yuc73c6182022-02-08 20:29:25 +0800763void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
765{
766 ((void) ciphersuite_info);
767
Andrzej Kurek25f27152022-08-17 16:09:31 -0400768#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800769 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
770 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
771 else
772#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400773#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
775 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
776 else
777#endif
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
780 return;
781 }
782}
783
XiaokangQianadab9a62022-07-18 07:41:26 +0000784void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
785 unsigned hs_type,
786 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100787{
788 unsigned char hs_hdr[4];
789
790 /* Build HS header for checksum update. */
791 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
792 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
793 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
794 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
795
796 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
797}
798
799void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
800 unsigned hs_type,
801 unsigned char const *msg,
802 size_t msg_len )
803{
804 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
805 ssl->handshake->update_checksum( ssl, msg, msg_len );
806}
807
Jerry Yuc73c6182022-02-08 20:29:25 +0800808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
809{
810 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#if defined(MBEDTLS_USE_PSA_CRYPTO)
813 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
814 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
815#else
816 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
817#endif
818#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400819#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
822 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
823#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400824 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#endif
826#endif
827}
828
829static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
830 const unsigned char *buf, size_t len )
831{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
834 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
835#else
836 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
837#endif
838#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
842#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400843 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400846#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
847 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
848 (void) ssl;
849 (void) buf;
850 (void) len;
851#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800852}
853
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
856 const unsigned char *buf, size_t len )
857{
858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
860#else
861 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
862#endif
863}
864#endif
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
868 const unsigned char *buf, size_t len )
869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
871 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
872#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400873 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875}
876#endif
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881
Andrzej Kurek25f27152022-08-17 16:09:31 -0400882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884 handshake->fin_sha256_psa = psa_hash_operation_init();
885 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200888 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500893 handshake->fin_sha384_psa = psa_hash_operation_init();
894 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400896 mbedtls_sha512_init( &handshake->fin_sha384 );
897 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900
901 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_DHM_C)
904 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200906#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200910#if defined(MBEDTLS_USE_PSA_CRYPTO)
911 handshake->psa_pake_ctx = psa_pake_operation_init();
912 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
913#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200914 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200916#if defined(MBEDTLS_SSL_CLI_C)
917 handshake->ecjpake_cache = NULL;
918 handshake->ecjpake_cache_len = 0;
919#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200920#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200921
Gilles Peskineeccd8882020-03-10 12:19:08 +0100922#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200923 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200924#endif
925
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
927 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
928#endif
Hanno Becker75173122019-02-06 16:18:31 +0000929
930#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
931 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
932 mbedtls_pk_init( &handshake->peer_pubkey );
933#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934}
935
Hanno Beckera18d1322018-01-03 14:27:32 +0000936void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200939
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
942 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100943#else
944 mbedtls_cipher_init( &transform->cipher_ctx_enc );
945 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100946#endif
947
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
951 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100952#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_md_init( &transform->md_ctx_enc );
954 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000955#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100956#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957}
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962}
963
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200964MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000966{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000968 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200973 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974
975 /*
976 * Either the pointers are now NULL or cleared properly and can be freed.
977 * Now allocate missing structures.
978 */
979 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200981 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Paul Bakker48916f92012-09-16 19:57:18 +0000988
Paul Bakker82788fb2014-10-20 13:59:19 +0200989 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200991 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200992 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500993#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
994 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400995
Andrzej Kurek4a063792020-10-21 15:08:44 +0200996 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
997 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500998#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000999
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001000 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00001001 if( ssl->handshake == NULL ||
1002 ssl->transform_negotiate == NULL ||
1003 ssl->session_negotiate == NULL )
1004 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_free( ssl->handshake );
1008 mbedtls_free( ssl->transform_negotiate );
1009 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010
1011 ssl->handshake = NULL;
1012 ssl->transform_negotiate = NULL;
1013 ssl->session_negotiate = NULL;
1014
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001015 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001016 }
1017
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001020 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001021 ssl_handshake_params_init( ssl->handshake );
1022
Jerry Yud0766ec2022-09-22 10:46:57 +08001023#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1024 defined(MBEDTLS_SSL_SRV_C) && \
1025 defined(MBEDTLS_SSL_SESSION_TICKETS)
1026 ssl->handshake->new_session_tickets_count =
1027 ssl->conf->new_session_tickets_count ;
1028#endif
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1032 {
1033 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001034
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1036 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1037 else
1038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001039
Hanno Becker0f57a652020-02-05 10:37:26 +00001040 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001041 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001042#endif
1043
Brett Warrene0edc842021-08-17 09:53:13 +01001044/*
1045 * curve_list is translated to IANA TLS group identifiers here because
1046 * mbedtls_ssl_conf_curves returns void and so can't return
1047 * any error codes.
1048 */
1049#if defined(MBEDTLS_ECP_C)
1050#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1051 /* Heap allocate and translate curve_list from internal to IANA group ids */
1052 if ( ssl->conf->curve_list != NULL )
1053 {
1054 size_t length;
1055 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1056
1057 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1058 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1059
1060 /* Leave room for zero termination */
1061 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1062 if ( group_list == NULL )
1063 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1064
1065 for( size_t i = 0; i < length; i++ )
1066 {
1067 const mbedtls_ecp_curve_info *info =
1068 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1069 if ( info == NULL )
1070 {
1071 mbedtls_free( group_list );
1072 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1073 }
1074 group_list[i] = info->tls_id;
1075 }
1076
1077 group_list[length] = 0;
1078
1079 ssl->handshake->group_list = group_list;
1080 ssl->handshake->group_list_heap_allocated = 1;
1081 }
1082 else
1083 {
1084 ssl->handshake->group_list = ssl->conf->group_list;
1085 ssl->handshake->group_list_heap_allocated = 0;
1086 }
1087#endif /* MBEDTLS_DEPRECATED_REMOVED */
1088#endif /* MBEDTLS_ECP_C */
1089
Ronald Crone68ab4f2022-10-05 12:46:29 +02001090#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001091#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001093 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1094 signature algorithms IANA identifiers. */
1095 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001096 ssl->conf->sig_hashes != NULL )
1097 {
1098 const int *md;
1099 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001100 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001101 uint16_t *p;
1102
Jerry Yub476a442022-01-21 18:14:45 +08001103#if defined(static_assert)
1104 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1105 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1106 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001107#endif
1108
Jerry Yuf017ee42022-01-12 15:49:48 +08001109 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1110 {
1111 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1112 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001113#if defined(MBEDTLS_ECDSA_C)
1114 sig_algs_len += sizeof( uint16_t );
1115#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001116
Jerry Yub476a442022-01-21 18:14:45 +08001117#if defined(MBEDTLS_RSA_C)
1118 sig_algs_len += sizeof( uint16_t );
1119#endif
1120 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1121 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001122 }
1123
Jerry Yub476a442022-01-21 18:14:45 +08001124 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001125 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1126
Jerry Yub476a442022-01-21 18:14:45 +08001127 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1128 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001129 if( ssl->handshake->sig_algs == NULL )
1130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1131
1132 p = (uint16_t *)ssl->handshake->sig_algs;
1133 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1134 {
1135 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1136 if( hash == MBEDTLS_SSL_HASH_NONE )
1137 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001138#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001139 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1140 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001141#endif
1142#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1144 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001145#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001146 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001147 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001148 ssl->handshake->sig_algs_heap_allocated = 1;
1149 }
1150 else
Jerry Yua69269a2022-01-17 21:06:01 +08001151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001152 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001153 ssl->handshake->sig_algs_heap_allocated = 0;
1154 }
Jerry Yucc539102022-06-27 16:27:35 +08001155#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001156#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001161/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001162MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001163static int ssl_cookie_write_dummy( void *ctx,
1164 unsigned char **p, unsigned char *end,
1165 const unsigned char *cli_id, size_t cli_id_len )
1166{
1167 ((void) ctx);
1168 ((void) p);
1169 ((void) end);
1170 ((void) cli_id);
1171 ((void) cli_id_len);
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001174}
1175
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001176MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001177static int ssl_cookie_check_dummy( void *ctx,
1178 const unsigned char *cookie, size_t cookie_len,
1179 const unsigned char *cli_id, size_t cli_id_len )
1180{
1181 ((void) ctx);
1182 ((void) cookie);
1183 ((void) cookie_len);
1184 ((void) cli_id);
1185 ((void) cli_id_len);
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001188}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001189#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001190
Paul Bakker5121ce52009-01-03 21:22:43 +00001191/*
1192 * Initialize an SSL context
1193 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001194void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1195{
1196 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1197}
1198
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001199MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001200static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1201{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001202 const mbedtls_ssl_config *conf = ssl->conf;
1203
Ronald Cron6f135e12021-12-08 16:57:54 +01001204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001205 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1206 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1208 {
1209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1211 }
1212
Jerry Yu60835a82021-08-04 10:13:52 +08001213 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1214 return( 0 );
1215 }
1216#endif
1217
1218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001219 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1222 return( 0 );
1223 }
1224#endif
1225
Ronald Cron6f135e12021-12-08 16:57:54 +01001226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001227 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001228 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1230 {
1231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1232 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1233 }
1234
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001235 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1236 {
1237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1238 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1239 }
1240
1241
Ronald Crone1d3f062022-02-10 14:50:54 +01001242 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1243 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001244 }
1245#endif
1246
1247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1248 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1249}
1250
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001251MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001252static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1253{
1254 int ret;
1255 ret = ssl_conf_version_check( ssl );
1256 if( ret != 0 )
1257 return( ret );
1258
Jerry Yudef7ae42022-10-30 14:13:19 +08001259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1260 /* RFC 8446 section 4.4.3
1261 *
1262 * If the verification fails, the receiver MUST terminate the handshake with
1263 * a "decrypt_error" alert.
1264 *
1265 * If the client is configured as TLS 1.3 only with optional verify, return
1266 * bad config.
1267 *
1268 */
1269 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1270 (mbedtls_ssl_context *)ssl ) &&
1271 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1272 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1273 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1274 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1275 {
1276 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001277 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001278 "is not available for TLS 1.3 client" ) );
1279 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1280 }
1281#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1282
Jerry Yu60835a82021-08-04 10:13:52 +08001283 /* Space for further checks */
1284
1285 return( 0 );
1286}
1287
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001288/*
1289 * Setup an SSL context
1290 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001291
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001292int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001293 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294{
Janos Follath865b3eb2019-12-16 11:46:15 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001296 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1297 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001299 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001300
Jerry Yu60835a82021-08-04 10:13:52 +08001301 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1302 return( ret );
1303
Paul Bakker62f2dee2012-09-28 07:31:51 +00001304 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001305 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001306 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001307
1308 /* Set to NULL in case of an error condition */
1309 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001310
Darryl Greenb33cc762019-11-28 14:29:44 +00001311#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1312 ssl->in_buf_len = in_buf_len;
1313#endif
1314 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001315 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001318 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001319 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001320 }
1321
Darryl Greenb33cc762019-11-28 14:29:44 +00001322#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1323 ssl->out_buf_len = out_buf_len;
1324#endif
1325 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001326 if( ssl->out_buf == NULL )
1327 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001330 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
1332
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001333 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001334
Johan Pascalb62bb512015-12-03 21:56:45 +01001335#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001336 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001337#endif
1338
Paul Bakker48916f92012-09-16 19:57:18 +00001339 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001340 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001343
1344error:
1345 mbedtls_free( ssl->in_buf );
1346 mbedtls_free( ssl->out_buf );
1347
1348 ssl->conf = NULL;
1349
Darryl Greenb33cc762019-11-28 14:29:44 +00001350#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1351 ssl->in_buf_len = 0;
1352 ssl->out_buf_len = 0;
1353#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001354 ssl->in_buf = NULL;
1355 ssl->out_buf = NULL;
1356
1357 ssl->in_hdr = NULL;
1358 ssl->in_ctr = NULL;
1359 ssl->in_len = NULL;
1360 ssl->in_iv = NULL;
1361 ssl->in_msg = NULL;
1362
1363 ssl->out_hdr = NULL;
1364 ssl->out_ctr = NULL;
1365 ssl->out_len = NULL;
1366 ssl->out_iv = NULL;
1367 ssl->out_msg = NULL;
1368
k-stachowiak9f7798e2018-07-31 16:52:32 +02001369 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370}
1371
1372/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001373 * Reset an initialized and used SSL context for re-use while retaining
1374 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001375 *
1376 * If partial is non-zero, keep data in the input buffer and client ID.
1377 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001378 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001379void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1380 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001381{
Darryl Greenb33cc762019-11-28 14:29:44 +00001382#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1383 size_t in_buf_len = ssl->in_buf_len;
1384 size_t out_buf_len = ssl->out_buf_len;
1385#else
1386 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1388#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001389
Hanno Beckerb0302c42021-08-03 09:39:42 +01001390#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1391 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001392#endif
1393
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001394 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001395 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001396
Hanno Beckerb0302c42021-08-03 09:39:42 +01001397 mbedtls_ssl_reset_in_out_pointers( ssl );
1398
1399 /* Reset incoming message parsing */
1400 ssl->in_offt = NULL;
1401 ssl->nb_zero = 0;
1402 ssl->in_msgtype = 0;
1403 ssl->in_msglen = 0;
1404 ssl->in_hslen = 0;
1405 ssl->keep_current_message = 0;
1406 ssl->transform_in = NULL;
1407
1408#if defined(MBEDTLS_SSL_PROTO_DTLS)
1409 ssl->next_record_offset = 0;
1410 ssl->in_epoch = 0;
1411#endif
1412
1413 /* Keep current datagram if partial == 1 */
1414 if( partial == 0 )
1415 {
1416 ssl->in_left = 0;
1417 memset( ssl->in_buf, 0, in_buf_len );
1418 }
1419
Ronald Cronad8c17b2022-06-10 17:18:09 +02001420 ssl->send_alert = 0;
1421
Hanno Beckerb0302c42021-08-03 09:39:42 +01001422 /* Reset outgoing message writing */
1423 ssl->out_msgtype = 0;
1424 ssl->out_msglen = 0;
1425 ssl->out_left = 0;
1426 memset( ssl->out_buf, 0, out_buf_len );
1427 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1428 ssl->transform_out = NULL;
1429
1430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1431 mbedtls_ssl_dtls_replay_reset( ssl );
1432#endif
1433
XiaokangQian2b01dc32022-01-21 02:53:13 +00001434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435 if( ssl->transform )
1436 {
1437 mbedtls_ssl_transform_free( ssl->transform );
1438 mbedtls_free( ssl->transform );
1439 ssl->transform = NULL;
1440 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1442
XiaokangQian2b01dc32022-01-21 02:53:13 +00001443#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1444 mbedtls_ssl_transform_free( ssl->transform_application );
1445 mbedtls_free( ssl->transform_application );
1446 ssl->transform_application = NULL;
1447
1448 if( ssl->handshake != NULL )
1449 {
1450 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1451 mbedtls_free( ssl->handshake->transform_earlydata );
1452 ssl->handshake->transform_earlydata = NULL;
1453
1454 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1455 mbedtls_free( ssl->handshake->transform_handshake );
1456 ssl->handshake->transform_handshake = NULL;
1457 }
1458
XiaokangQian2b01dc32022-01-21 02:53:13 +00001459#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001460}
1461
1462int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1463{
1464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1465
1466 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1467
XiaokangQian78b1fa72022-01-19 06:56:30 +00001468 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001469
1470 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_RENEGOTIATION)
1472 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001473 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001474
1475 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1477 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001480
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001482 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001483 if( ssl->session )
1484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_ssl_session_free( ssl->session );
1486 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001487 ssl->session = NULL;
1488 }
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001491 ssl->alpn_chosen = NULL;
1492#endif
1493
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001494#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001495 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001496#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001497 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001498#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001499 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001500 {
1501 mbedtls_free( ssl->cli_id );
1502 ssl->cli_id = NULL;
1503 ssl->cli_id_len = 0;
1504 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001505#endif
1506
Paul Bakker48916f92012-09-16 19:57:18 +00001507 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1508 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001509
1510 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001511}
1512
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001513/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001514 * Reset an initialized and used SSL context for re-use while retaining
1515 * all application-set variables, function pointers and data.
1516 */
1517int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1518{
Hanno Becker43aefe22020-02-05 10:44:56 +00001519 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001520}
1521
1522/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 * SSL set accessors
1524 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001525void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001527 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001530void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001532 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001533}
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001536void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001538 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001539}
1540#endif
1541
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001542void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001544 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001545}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001548
Hanno Becker1841b0a2018-08-24 11:13:57 +01001549void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1550 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001551{
1552 ssl->disable_datagram_packing = !allow_packing;
1553}
1554
1555void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1556 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001558 conf->hs_timeout_min = min;
1559 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001560}
1561#endif
1562
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001563void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001565 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566}
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001569void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001570 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001571 void *p_vrfy )
1572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001573 conf->f_vrfy = f_vrfy;
1574 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001577
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001578void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001579 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 void *p_rng )
1581{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001582 conf->f_rng = f_rng;
1583 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001584}
1585
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001586void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001587 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 void *p_dbg )
1589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001590 conf->f_dbg = f_dbg;
1591 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001592}
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001595 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001596 mbedtls_ssl_send_t *f_send,
1597 mbedtls_ssl_recv_t *f_recv,
1598 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001599{
1600 ssl->p_bio = p_bio;
1601 ssl->f_send = f_send;
1602 ssl->f_recv = f_recv;
1603 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001604}
1605
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001606#if defined(MBEDTLS_SSL_PROTO_DTLS)
1607void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1608{
1609 ssl->mtu = mtu;
1610}
1611#endif
1612
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001613void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001614{
1615 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001616}
1617
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001618void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1619 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001620 mbedtls_ssl_set_timer_t *f_set_timer,
1621 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001622{
1623 ssl->p_timer = p_timer;
1624 ssl->f_set_timer = f_set_timer;
1625 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001626
1627 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001628 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001629}
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001632void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001633 void *p_cache,
1634 mbedtls_ssl_cache_get_t *f_get_cache,
1635 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001636{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001637 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001638 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001639 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_CLI_C)
1644int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001645{
Janos Follath865b3eb2019-12-16 11:46:15 +00001646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001647
1648 if( ssl == NULL ||
1649 session == NULL ||
1650 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001651 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654 }
1655
Hanno Beckere810bbc2021-05-14 16:01:05 +01001656 if( ssl->handshake->resume == 1 )
1657 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1658
Jerry Yu21092062022-10-10 21:21:31 +08001659#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1660 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001661 {
Jerry Yu21092062022-10-10 21:21:31 +08001662 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1663 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1664
1665 if( mbedtls_ssl_validate_ciphersuite(
1666 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1667 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1668 {
1669 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1670 session->ciphersuite ) );
1671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1672 }
Jerry Yu40afab62022-10-08 10:42:13 +08001673 }
Jerry Yu21092062022-10-10 21:21:31 +08001674#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001675
Hanno Becker52055ae2019-02-06 14:30:46 +00001676 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1677 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001678 return( ret );
1679
Paul Bakker0a597072012-09-25 21:55:46 +00001680 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001681
1682 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001686void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1687 const int *ciphersuites )
1688{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001689 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690}
1691
Ronald Cron6f135e12021-12-08 16:57:54 +01001692#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001693void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001694 const int kex_modes )
1695{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001696 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001697}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001698
1699#if defined(MBEDTLS_SSL_EARLY_DATA)
1700void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001701 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001702{
1703 conf->early_data_enabled = early_data_enabled;
1704}
1705#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001706#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001709void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001710 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001711{
1712 conf->cert_profile = profile;
1713}
1714
Glenn Strauss36872db2022-01-22 05:06:31 -05001715static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1716{
1717 mbedtls_ssl_key_cert *cur = key_cert, *next;
1718
1719 while( cur != NULL )
1720 {
1721 next = cur->next;
1722 mbedtls_free( cur );
1723 cur = next;
1724 }
1725}
1726
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001727/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001728MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001729static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1730 mbedtls_x509_crt *cert,
1731 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001732{
niisato8ee24222018-06-25 19:05:48 +09001733 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001734
Glenn Strauss36872db2022-01-22 05:06:31 -05001735 if( cert == NULL )
1736 {
1737 /* Free list if cert is null */
1738 ssl_key_cert_free( *head );
1739 *head = NULL;
1740 return( 0 );
1741 }
1742
niisato8ee24222018-06-25 19:05:48 +09001743 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1744 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001746
niisato8ee24222018-06-25 19:05:48 +09001747 new_cert->cert = cert;
1748 new_cert->key = key;
1749 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001750
Glenn Strauss36872db2022-01-22 05:06:31 -05001751 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001752 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001753 {
niisato8ee24222018-06-25 19:05:48 +09001754 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001755 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001756 else
1757 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001758 mbedtls_ssl_key_cert *cur = *head;
1759 while( cur->next != NULL )
1760 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001761 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001762 }
1763
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001764 return( 0 );
1765}
1766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001767int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001768 mbedtls_x509_crt *own_cert,
1769 mbedtls_pk_context *pk_key )
1770{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001771 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001772}
1773
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001774void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001775 mbedtls_x509_crt *ca_chain,
1776 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001778 conf->ca_chain = ca_chain;
1779 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001780
1781#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1782 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1783 * cannot be used together. */
1784 conf->f_ca_cb = NULL;
1785 conf->p_ca_cb = NULL;
1786#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001787}
Hanno Becker5adaad92019-03-27 16:54:37 +00001788
1789#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1790void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001791 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001792 void *p_ca_cb )
1793{
1794 conf->f_ca_cb = f_ca_cb;
1795 conf->p_ca_cb = p_ca_cb;
1796
1797 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1798 * cannot be used together. */
1799 conf->ca_chain = NULL;
1800 conf->ca_crl = NULL;
1801}
1802#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001804
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001805#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001806const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1807 size_t *name_len )
1808{
1809 *name_len = ssl->handshake->sni_name_len;
1810 return( ssl->handshake->sni_name );
1811}
1812
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001813int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1814 mbedtls_x509_crt *own_cert,
1815 mbedtls_pk_context *pk_key )
1816{
1817 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1818 own_cert, pk_key ) );
1819}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001820
1821void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1822 mbedtls_x509_crt *ca_chain,
1823 mbedtls_x509_crl *ca_crl )
1824{
1825 ssl->handshake->sni_ca_chain = ca_chain;
1826 ssl->handshake->sni_ca_crl = ca_crl;
1827}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001828
Glenn Strauss999ef702022-03-11 01:37:23 -05001829#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1830void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1831 const mbedtls_x509_crt *crt)
1832{
1833 ssl->handshake->dn_hints = crt;
1834}
1835#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1836
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001837void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1838 int authmode )
1839{
1840 ssl->handshake->sni_authmode = authmode;
1841}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001842#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1843
Hanno Becker8927c832019-04-03 12:52:50 +01001844#if defined(MBEDTLS_X509_CRT_PARSE_C)
1845void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1846 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1847 void *p_vrfy )
1848{
1849 ssl->f_vrfy = f_vrfy;
1850 ssl->p_vrfy = p_vrfy;
1851}
1852#endif
1853
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001854#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001855/*
1856 * Set EC J-PAKE password for current handshake
1857 */
Valerio Setti02c25b52022-11-15 14:08:42 +01001858#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001859int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1860 const unsigned char *pw,
1861 size_t pw_len )
1862{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001863 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1865 psa_pake_role_t psa_role;
1866 psa_status_t status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001867
Janos Follath8eb64132016-06-03 15:40:57 +01001868 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1870
Neil Armstrongca7d5062022-05-31 14:43:23 +02001871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1872 psa_role = PSA_PAKE_ROLE_SERVER;
1873 else
1874 psa_role = PSA_PAKE_ROLE_CLIENT;
1875
Valerio Settiaca21b72022-11-17 18:17:01 +01001876 /* Empty password is not valid */
1877 if( ( pw == NULL) || ( pw_len == 0 ) )
1878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001879
Valerio Settiaca21b72022-11-17 18:17:01 +01001880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1881 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1882 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001883
Valerio Settiaca21b72022-11-17 18:17:01 +01001884 status = psa_import_key( &attributes, pw, pw_len,
1885 &ssl->handshake->psa_pake_password );
1886 if( status != PSA_SUCCESS )
1887 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001888
1889 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1890 psa_pake_cs_set_primitive( &cipher_suite,
1891 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1892 PSA_ECC_FAMILY_SECP_R1,
1893 256) );
1894 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1895
1896 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1897 if( status != PSA_SUCCESS )
1898 {
1899 psa_destroy_key( ssl->handshake->psa_pake_password );
1900 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1901 }
1902
1903 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1904 if( status != PSA_SUCCESS )
1905 {
1906 psa_destroy_key( ssl->handshake->psa_pake_password );
1907 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1908 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1909 }
1910
Valerio Settiaca21b72022-11-17 18:17:01 +01001911 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1912 ssl->handshake->psa_pake_password );
1913 if( status != PSA_SUCCESS )
Neil Armstrongca7d5062022-05-31 14:43:23 +02001914 {
Valerio Settiaca21b72022-11-17 18:17:01 +01001915 psa_destroy_key( ssl->handshake->psa_pake_password );
1916 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1917 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001918 }
1919
1920 ssl->handshake->psa_pake_ctx_is_ok = 1;
1921
1922 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001923}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001924
1925int mbedtls_ssl_set_hs_ecjpake_password_opaque( mbedtls_ssl_context *ssl,
1926 mbedtls_svc_key_id_t pwd )
1927{
1928 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1929 psa_pake_role_t psa_role;
1930 psa_status_t status;
1931
1932 if( ssl->handshake == NULL || ssl->conf == NULL )
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934
1935 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1936 psa_role = PSA_PAKE_ROLE_SERVER;
1937 else
1938 psa_role = PSA_PAKE_ROLE_CLIENT;
1939
1940 if( mbedtls_svc_key_id_is_null( pwd ) )
1941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1942 ssl->handshake->psa_pake_password = pwd;
1943
1944 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1945 psa_pake_cs_set_primitive( &cipher_suite,
1946 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1947 PSA_ECC_FAMILY_SECP_R1,
1948 256) );
1949 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1950
1951 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1952 if( status != PSA_SUCCESS )
1953 {
1954 psa_destroy_key( ssl->handshake->psa_pake_password );
1955 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1956 }
1957
1958 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1959 if( status != PSA_SUCCESS )
1960 {
1961 psa_destroy_key( ssl->handshake->psa_pake_password );
1962 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1963 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1964 }
1965
1966 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1967 ssl->handshake->psa_pake_password );
1968 if( status != PSA_SUCCESS )
1969 {
1970 psa_destroy_key( ssl->handshake->psa_pake_password );
1971 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1972 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1973 }
1974
1975 ssl->handshake->psa_pake_ctx_is_ok = 1;
1976
1977 return( 0 );
1978}
Valerio Setti02c25b52022-11-15 14:08:42 +01001979#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001980int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1981 const unsigned char *pw,
1982 size_t pw_len )
1983{
1984 mbedtls_ecjpake_role role;
1985
1986 if( ssl->handshake == NULL || ssl->conf == NULL )
1987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1988
1989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1990 role = MBEDTLS_ECJPAKE_SERVER;
1991 else
1992 role = MBEDTLS_ECJPAKE_CLIENT;
1993
Valerio Setti09443292022-12-01 15:06:09 +01001994 /* Empty password is not valid */
1995 if( ( pw == NULL) || ( pw_len == 0 ) )
1996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1997
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001998 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1999 role,
2000 MBEDTLS_MD_SHA256,
2001 MBEDTLS_ECP_DP_SECP256R1,
2002 pw, pw_len ) );
2003}
Valerio Setti02c25b52022-11-15 14:08:42 +01002004#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002005#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002006
Ronald Cron73fe8df2022-10-05 14:31:43 +02002007#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02002008int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002009{
Ronald Crond29e13e2022-10-19 10:33:48 +02002010 if( conf->psk_identity == NULL ||
2011 conf->psk_identity_len == 0 )
2012 {
2013 return( 0 );
2014 }
2015
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002016#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02002017 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002018 return( 1 );
2019#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002020
2021 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002022 return( 1 );
2023
2024 return( 0 );
2025}
2026
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002027static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
2028{
2029 /* Remove reference to existing PSK, if any. */
2030#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002031 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002032 {
2033 /* The maintenance of the PSK key slot is the
2034 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002035 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002036 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002037#endif /* MBEDTLS_USE_PSA_CRYPTO */
2038 if( conf->psk != NULL )
2039 {
2040 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
2041
2042 mbedtls_free( conf->psk );
2043 conf->psk = NULL;
2044 conf->psk_len = 0;
2045 }
2046
2047 /* Remove reference to PSK identity, if any. */
2048 if( conf->psk_identity != NULL )
2049 {
2050 mbedtls_free( conf->psk_identity );
2051 conf->psk_identity = NULL;
2052 conf->psk_identity_len = 0;
2053 }
2054}
2055
Hanno Becker7390c712018-11-15 13:33:04 +00002056/* This function assumes that PSK identity in the SSL config is unset.
2057 * It checks that the provided identity is well-formed and attempts
2058 * to make a copy of it in the SSL config.
2059 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002060MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002061static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2062 unsigned char const *psk_identity,
2063 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002064{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002065 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002066 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002067 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002068 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002069 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002070 {
2071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2072 }
2073
Hanno Becker7390c712018-11-15 13:33:04 +00002074 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2075 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002076 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002077
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002078 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002079 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002080
2081 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002082}
2083
Hanno Becker7390c712018-11-15 13:33:04 +00002084int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2085 const unsigned char *psk, size_t psk_len,
2086 const unsigned char *psk_identity, size_t psk_identity_len )
2087{
Janos Follath865b3eb2019-12-16 11:46:15 +00002088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002089
2090 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002091 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002092 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002093
2094 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002095 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002097 if( psk_len == 0 )
2098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2099 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2101
Hanno Becker7390c712018-11-15 13:33:04 +00002102 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2103 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2104 conf->psk_len = psk_len;
2105 memcpy( conf->psk, psk, conf->psk_len );
2106
2107 /* Check and set PSK Identity */
2108 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2109 if( ret != 0 )
2110 ssl_conf_remove_psk( conf );
2111
2112 return( ret );
2113}
2114
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002115static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2116{
2117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002118 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002119 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002120 /* The maintenance of the external PSK key slot is the
2121 * user's responsibility. */
2122 if( ssl->handshake->psk_opaque_is_internal )
2123 {
2124 psa_destroy_key( ssl->handshake->psk_opaque );
2125 ssl->handshake->psk_opaque_is_internal = 0;
2126 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002127 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002128 }
Neil Armstronge952a302022-05-03 10:22:14 +02002129#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002130 if( ssl->handshake->psk != NULL )
2131 {
2132 mbedtls_platform_zeroize( ssl->handshake->psk,
2133 ssl->handshake->psk_len );
2134 mbedtls_free( ssl->handshake->psk );
2135 ssl->handshake->psk_len = 0;
2136 }
Neil Armstronge952a302022-05-03 10:22:14 +02002137#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002138}
2139
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002140int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2141 const unsigned char *psk, size_t psk_len )
2142{
Neil Armstrong501c9322022-05-03 09:35:09 +02002143#if defined(MBEDTLS_USE_PSA_CRYPTO)
2144 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002146 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002148#endif /* MBEDTLS_USE_PSA_CRYPTO */
2149
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002150 if( psk == NULL || ssl->handshake == NULL )
2151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2152
2153 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2155
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002156 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002157
Neil Armstrong501c9322022-05-03 09:35:09 +02002158#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2160 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2161 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002162 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002163 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2164 else
2165 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2166 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2167 }
2168#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002169
Jerry Yu568ec252022-07-22 21:27:34 +08002170#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002171 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2172 {
2173 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2174 psa_set_key_usage_flags( &key_attributes,
2175 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2176 }
2177#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2178
Neil Armstrong501c9322022-05-03 09:35:09 +02002179 psa_set_key_algorithm( &key_attributes, alg );
2180 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2181
2182 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2183 if( status != PSA_SUCCESS )
2184 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2185
2186 /* Allow calling psa_destroy_key() on psk remove */
2187 ssl->handshake->psk_opaque_is_internal = 1;
2188 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2189#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002190 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002191 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002192
2193 ssl->handshake->psk_len = psk_len;
2194 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2195
2196 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002197#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002198}
2199
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002200#if defined(MBEDTLS_USE_PSA_CRYPTO)
2201int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002202 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002203 const unsigned char *psk_identity,
2204 size_t psk_identity_len )
2205{
Janos Follath865b3eb2019-12-16 11:46:15 +00002206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002207
2208 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002209 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002211
Hanno Becker7390c712018-11-15 13:33:04 +00002212 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002213 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002215 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002216
2217 /* Check and set PSK Identity */
2218 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2219 psk_identity_len );
2220 if( ret != 0 )
2221 ssl_conf_remove_psk( conf );
2222
2223 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002224}
2225
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002226int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2227 mbedtls_svc_key_id_t psk )
2228{
2229 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2230 ( ssl->handshake == NULL ) )
2231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2232
2233 ssl_remove_psk( ssl );
2234 ssl->handshake->psk_opaque = psk;
2235 return( 0 );
2236}
2237#endif /* MBEDTLS_USE_PSA_CRYPTO */
2238
Jerry Yu8897c072022-08-12 13:56:53 +08002239#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002240void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2241 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2242 size_t),
2243 void *p_psk )
2244{
2245 conf->f_psk = f_psk;
2246 conf->p_psk = p_psk;
2247}
Jerry Yu8897c072022-08-12 13:56:53 +08002248#endif /* MBEDTLS_SSL_SRV_C */
2249
Ronald Cron73fe8df2022-10-05 14:31:43 +02002250#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002251
2252#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002253static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2254 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002255{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002256#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002257 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002258 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002259#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002260 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002261 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002262 return( MBEDTLS_SSL_MODE_STREAM );
2263}
2264
2265#else /* MBEDTLS_USE_PSA_CRYPTO */
2266
2267static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2268 mbedtls_cipher_mode_t mode )
2269{
2270#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2271 if( mode == MBEDTLS_MODE_CBC )
2272 return( MBEDTLS_SSL_MODE_CBC );
2273#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2274
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002275#if defined(MBEDTLS_GCM_C) || \
2276 defined(MBEDTLS_CCM_C) || \
2277 defined(MBEDTLS_CHACHAPOLY_C)
2278 if( mode == MBEDTLS_MODE_GCM ||
2279 mode == MBEDTLS_MODE_CCM ||
2280 mode == MBEDTLS_MODE_CHACHAPOLY )
2281 return( MBEDTLS_SSL_MODE_AEAD );
2282#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002283
2284 return( MBEDTLS_SSL_MODE_STREAM );
2285}
Gilles Peskine301711e2022-04-26 16:57:05 +02002286#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002287
Gilles Peskinee108d982022-04-26 16:50:40 +02002288static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2289 mbedtls_ssl_mode_t base_mode,
2290 int encrypt_then_mac )
2291{
2292#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2293 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2294 base_mode == MBEDTLS_SSL_MODE_CBC )
2295 {
2296 return( MBEDTLS_SSL_MODE_CBC_ETM );
2297 }
2298#else
2299 (void) encrypt_then_mac;
2300#endif
2301 return( base_mode );
2302}
2303
Neil Armstrongab555e02022-04-04 11:07:59 +02002304mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002305 const mbedtls_ssl_transform *transform )
2306{
Gilles Peskinee108d982022-04-26 16:50:40 +02002307 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002308#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002309 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002310#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002311 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2312#endif
2313 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002314
Gilles Peskinee108d982022-04-26 16:50:40 +02002315 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002316#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002317 encrypt_then_mac = transform->encrypt_then_mac;
2318#endif
2319 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002320}
2321
Neil Armstrongab555e02022-04-04 11:07:59 +02002322mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002323#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002324 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002325#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002326 const mbedtls_ssl_ciphersuite_t *suite )
2327{
Gilles Peskinee108d982022-04-26 16:50:40 +02002328 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2329
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002330#if defined(MBEDTLS_USE_PSA_CRYPTO)
2331 psa_status_t status;
2332 psa_algorithm_t alg;
2333 psa_key_type_t type;
2334 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002335 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2336 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002337 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002338#else
2339 const mbedtls_cipher_info_t *cipher =
2340 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002341 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002342 {
2343 base_mode =
2344 mbedtls_ssl_get_base_mode(
2345 mbedtls_cipher_info_get_mode( cipher ) );
2346 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002347#endif /* MBEDTLS_USE_PSA_CRYPTO */
2348
Gilles Peskinee108d982022-04-26 16:50:40 +02002349#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2350 int encrypt_then_mac = 0;
2351#endif
2352 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002353}
2354
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002355#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002356
2357#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002358/* Serialization of TLS 1.3 sessions:
2359 *
2360 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002361 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002362 * uint64 ticket_received;
2363 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002364 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002365 * } ClientOnlyData;
2366 *
2367 * struct {
2368 * uint8 endpoint;
2369 * uint8 ciphersuite[2];
2370 * uint32 ticket_age_add;
2371 * uint8 ticket_flags;
2372 * opaque resumption_key<0..255>;
2373 * select ( endpoint ) {
2374 * case client: ClientOnlyData;
2375 * case server: uint64 start_time;
2376 * };
2377 * } serialized_session_tls13;
2378 *
2379 */
2380#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002381MBEDTLS_CHECK_RETURN_CRITICAL
2382static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2383 unsigned char *buf,
2384 size_t buf_len,
2385 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002386{
2387 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002388#if defined(MBEDTLS_SSL_CLI_C) && \
2389 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2390 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002391 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002392#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002393 size_t needed = 1 /* endpoint */
2394 + 2 /* ciphersuite */
2395 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002396 + 1 /* ticket_flags */
2397 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002398 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002399
2400 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2402 needed += session->resumption_key_len; /* resumption_key */
2403
Jerry Yu438ddd82022-07-07 06:55:50 +00002404#if defined(MBEDTLS_HAVE_TIME)
2405 needed += 8; /* start_time or ticket_received */
2406#endif
2407
2408#if defined(MBEDTLS_SSL_CLI_C)
2409 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2410 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002411#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002412 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002413 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002414#endif
2415
Jerry Yu438ddd82022-07-07 06:55:50 +00002416 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002417 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002418
2419 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002420 if( session->ticket_len > SIZE_MAX - needed )
2421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002422
Jerry Yue28d9742022-08-18 15:44:03 +08002423 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002424 }
2425#endif /* MBEDTLS_SSL_CLI_C */
2426
Jerry Yue36fdd62022-08-17 21:31:36 +08002427 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002428 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002429 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002430
2431 p[0] = session->endpoint;
2432 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2433 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2434 p[7] = session->ticket_flags;
2435
2436 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002437 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002438 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002439 memcpy( p, session->resumption_key, session->resumption_key_len );
2440 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002441
2442#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2443 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2444 {
2445 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2446 p += 8;
2447 }
2448#endif /* MBEDTLS_HAVE_TIME */
2449
2450#if defined(MBEDTLS_SSL_CLI_C)
2451 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2452 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002453#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2454 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2455 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002456 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002457 {
2458 /* save host name */
2459 memcpy( p, session->hostname, hostname_len );
2460 p += hostname_len;
2461 }
2462#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2463
Jerry Yu438ddd82022-07-07 06:55:50 +00002464#if defined(MBEDTLS_HAVE_TIME)
2465 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2466 p += 8;
2467#endif
2468 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2469 p += 4;
2470
2471 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2472 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002473
2474 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002475 {
2476 memcpy( p, session->ticket, session->ticket_len );
2477 p += session->ticket_len;
2478 }
2479 }
2480#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002481 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002482}
2483
2484MBEDTLS_CHECK_RETURN_CRITICAL
2485static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2486 const unsigned char *buf,
2487 size_t len )
2488{
2489 const unsigned char *p = buf;
2490 const unsigned char *end = buf + len;
2491
2492 if( end - p < 9 )
2493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2494 session->endpoint = p[0];
2495 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2496 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2497 session->ticket_flags = p[7];
2498
2499 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002500 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002501 p += 9;
2502
Jerry Yubc7c1a42022-07-21 22:57:37 +08002503 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2505
Jerry Yubc7c1a42022-07-21 22:57:37 +08002506 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002508 memcpy( session->resumption_key, p, session->resumption_key_len );
2509 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002510
2511#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2512 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2513 {
2514 if( end - p < 8 )
2515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2516 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2517 p += 8;
2518 }
2519#endif /* MBEDTLS_HAVE_TIME */
2520
2521#if defined(MBEDTLS_SSL_CLI_C)
2522 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2523 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002524#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2525 defined(MBEDTLS_SSL_SESSION_TICKETS)
2526 size_t hostname_len;
2527 /* load host name */
2528 if( end - p < 2 )
2529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2530 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2531 p += 2;
2532
2533 if( end - p < ( long int )hostname_len )
2534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2535 if( hostname_len > 0 )
2536 {
2537 session->hostname = mbedtls_calloc( 1, hostname_len );
2538 if( session->hostname == NULL )
2539 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2540 memcpy( session->hostname, p, hostname_len );
2541 p += hostname_len;
2542 }
2543#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2544 MBEDTLS_SSL_SESSION_TICKETS */
2545
Jerry Yu438ddd82022-07-07 06:55:50 +00002546#if defined(MBEDTLS_HAVE_TIME)
2547 if( end - p < 8 )
2548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2549 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2550 p += 8;
2551#endif
2552 if( end - p < 4 )
2553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2554 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2555 p += 4;
2556
2557 if( end - p < 2 )
2558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2559 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2560 p += 2;
2561
2562 if( end - p < ( long int )session->ticket_len )
2563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2564 if( session->ticket_len > 0 )
2565 {
2566 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2567 if( session->ticket == NULL )
2568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2569 memcpy( session->ticket, p, session->ticket_len );
2570 p += session->ticket_len;
2571 }
2572 }
2573#endif /* MBEDTLS_SSL_CLI_C */
2574
2575 return( 0 );
2576
2577}
2578#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002579MBEDTLS_CHECK_RETURN_CRITICAL
2580static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2581 unsigned char *buf,
2582 size_t buf_len,
2583 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002584{
2585 ((void) session);
2586 ((void) buf);
2587 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002588 *olen = 0;
2589 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002590}
Jerry Yu438ddd82022-07-07 06:55:50 +00002591
2592static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2593 unsigned char *buf,
2594 size_t buf_len )
2595{
2596 ((void) session);
2597 ((void) buf);
2598 ((void) buf_len);
2599 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2600}
2601#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002602#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2603
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002604psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002605 size_t taglen,
2606 psa_algorithm_t *alg,
2607 psa_key_type_t *key_type,
2608 size_t *key_size )
2609{
2610 switch ( mbedtls_cipher_type )
2611 {
2612 case MBEDTLS_CIPHER_AES_128_CBC:
2613 *alg = PSA_ALG_CBC_NO_PADDING;
2614 *key_type = PSA_KEY_TYPE_AES;
2615 *key_size = 128;
2616 break;
2617 case MBEDTLS_CIPHER_AES_128_CCM:
2618 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2619 *key_type = PSA_KEY_TYPE_AES;
2620 *key_size = 128;
2621 break;
2622 case MBEDTLS_CIPHER_AES_128_GCM:
2623 *alg = PSA_ALG_GCM;
2624 *key_type = PSA_KEY_TYPE_AES;
2625 *key_size = 128;
2626 break;
2627 case MBEDTLS_CIPHER_AES_192_CCM:
2628 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2629 *key_type = PSA_KEY_TYPE_AES;
2630 *key_size = 192;
2631 break;
2632 case MBEDTLS_CIPHER_AES_192_GCM:
2633 *alg = PSA_ALG_GCM;
2634 *key_type = PSA_KEY_TYPE_AES;
2635 *key_size = 192;
2636 break;
2637 case MBEDTLS_CIPHER_AES_256_CBC:
2638 *alg = PSA_ALG_CBC_NO_PADDING;
2639 *key_type = PSA_KEY_TYPE_AES;
2640 *key_size = 256;
2641 break;
2642 case MBEDTLS_CIPHER_AES_256_CCM:
2643 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2644 *key_type = PSA_KEY_TYPE_AES;
2645 *key_size = 256;
2646 break;
2647 case MBEDTLS_CIPHER_AES_256_GCM:
2648 *alg = PSA_ALG_GCM;
2649 *key_type = PSA_KEY_TYPE_AES;
2650 *key_size = 256;
2651 break;
2652 case MBEDTLS_CIPHER_ARIA_128_CBC:
2653 *alg = PSA_ALG_CBC_NO_PADDING;
2654 *key_type = PSA_KEY_TYPE_ARIA;
2655 *key_size = 128;
2656 break;
2657 case MBEDTLS_CIPHER_ARIA_128_CCM:
2658 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2659 *key_type = PSA_KEY_TYPE_ARIA;
2660 *key_size = 128;
2661 break;
2662 case MBEDTLS_CIPHER_ARIA_128_GCM:
2663 *alg = PSA_ALG_GCM;
2664 *key_type = PSA_KEY_TYPE_ARIA;
2665 *key_size = 128;
2666 break;
2667 case MBEDTLS_CIPHER_ARIA_192_CCM:
2668 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2669 *key_type = PSA_KEY_TYPE_ARIA;
2670 *key_size = 192;
2671 break;
2672 case MBEDTLS_CIPHER_ARIA_192_GCM:
2673 *alg = PSA_ALG_GCM;
2674 *key_type = PSA_KEY_TYPE_ARIA;
2675 *key_size = 192;
2676 break;
2677 case MBEDTLS_CIPHER_ARIA_256_CBC:
2678 *alg = PSA_ALG_CBC_NO_PADDING;
2679 *key_type = PSA_KEY_TYPE_ARIA;
2680 *key_size = 256;
2681 break;
2682 case MBEDTLS_CIPHER_ARIA_256_CCM:
2683 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2684 *key_type = PSA_KEY_TYPE_ARIA;
2685 *key_size = 256;
2686 break;
2687 case MBEDTLS_CIPHER_ARIA_256_GCM:
2688 *alg = PSA_ALG_GCM;
2689 *key_type = PSA_KEY_TYPE_ARIA;
2690 *key_size = 256;
2691 break;
2692 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2693 *alg = PSA_ALG_CBC_NO_PADDING;
2694 *key_type = PSA_KEY_TYPE_CAMELLIA;
2695 *key_size = 128;
2696 break;
2697 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2698 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2699 *key_type = PSA_KEY_TYPE_CAMELLIA;
2700 *key_size = 128;
2701 break;
2702 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2703 *alg = PSA_ALG_GCM;
2704 *key_type = PSA_KEY_TYPE_CAMELLIA;
2705 *key_size = 128;
2706 break;
2707 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2708 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2709 *key_type = PSA_KEY_TYPE_CAMELLIA;
2710 *key_size = 192;
2711 break;
2712 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2713 *alg = PSA_ALG_GCM;
2714 *key_type = PSA_KEY_TYPE_CAMELLIA;
2715 *key_size = 192;
2716 break;
2717 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2718 *alg = PSA_ALG_CBC_NO_PADDING;
2719 *key_type = PSA_KEY_TYPE_CAMELLIA;
2720 *key_size = 256;
2721 break;
2722 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2723 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2724 *key_type = PSA_KEY_TYPE_CAMELLIA;
2725 *key_size = 256;
2726 break;
2727 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2728 *alg = PSA_ALG_GCM;
2729 *key_type = PSA_KEY_TYPE_CAMELLIA;
2730 *key_size = 256;
2731 break;
2732 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2733 *alg = PSA_ALG_CHACHA20_POLY1305;
2734 *key_type = PSA_KEY_TYPE_CHACHA20;
2735 *key_size = 256;
2736 break;
2737 case MBEDTLS_CIPHER_NULL:
2738 *alg = MBEDTLS_SSL_NULL_CIPHER;
2739 *key_type = 0;
2740 *key_size = 0;
2741 break;
2742 default:
2743 return PSA_ERROR_NOT_SUPPORTED;
2744 }
2745
2746 return PSA_SUCCESS;
2747}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002748#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002749
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002750#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002751int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2752 const unsigned char *dhm_P, size_t P_len,
2753 const unsigned char *dhm_G, size_t G_len )
2754{
Janos Follath865b3eb2019-12-16 11:46:15 +00002755 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002756
Glenn Strausscee11292021-12-20 01:43:17 -05002757 mbedtls_mpi_free( &conf->dhm_P );
2758 mbedtls_mpi_free( &conf->dhm_G );
2759
Hanno Beckera90658f2017-10-04 15:29:08 +01002760 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2761 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2762 {
2763 mbedtls_mpi_free( &conf->dhm_P );
2764 mbedtls_mpi_free( &conf->dhm_G );
2765 return( ret );
2766 }
2767
2768 return( 0 );
2769}
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002771int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002772{
Janos Follath865b3eb2019-12-16 11:46:15 +00002773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002774
Glenn Strausscee11292021-12-20 01:43:17 -05002775 mbedtls_mpi_free( &conf->dhm_P );
2776 mbedtls_mpi_free( &conf->dhm_G );
2777
Gilles Peskinee5702482021-06-11 21:59:08 +02002778 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2779 &conf->dhm_P ) ) != 0 ||
2780 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2781 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002782 {
2783 mbedtls_mpi_free( &conf->dhm_P );
2784 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002785 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002786 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002787
2788 return( 0 );
2789}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002790#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002791
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002792#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2793/*
2794 * Set the minimum length for Diffie-Hellman parameters
2795 */
2796void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2797 unsigned int bitlen )
2798{
2799 conf->dhm_min_bitlen = bitlen;
2800}
2801#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2802
Ronald Crone68ab4f2022-10-05 12:46:29 +02002803#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002804#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002805/*
2806 * Set allowed/preferred hashes for handshake signatures
2807 */
2808void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2809 const int *hashes )
2810{
2811 conf->sig_hashes = hashes;
2812}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002813#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002814
Jerry Yuf017ee42022-01-12 15:49:48 +08002815/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002816void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2817 const uint16_t* sig_algs )
2818{
Jerry Yuf017ee42022-01-12 15:49:48 +08002819#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2820 conf->sig_hashes = NULL;
2821#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2822 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002823}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002824#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002825
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002826#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002827#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002828/*
2829 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002830 *
2831 * mbedtls_ssl_setup() takes the provided list
2832 * and translates it to a list of IANA TLS group identifiers,
2833 * stored in ssl->handshake->group_list.
2834 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002835 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002836void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002837 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002838{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002839 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002840 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002841}
Brett Warrene0edc842021-08-17 09:53:13 +01002842#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002843#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002844
Brett Warrene0edc842021-08-17 09:53:13 +01002845/*
2846 * Set the allowed groups
2847 */
2848void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2849 const uint16_t *group_list )
2850{
2851#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2852 conf->curve_list = NULL;
2853#endif
2854 conf->group_list = group_list;
2855}
2856
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002857#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002859{
Hanno Becker947194e2017-04-07 13:25:49 +01002860 /* Initialize to suppress unnecessary compiler warning */
2861 size_t hostname_len = 0;
2862
2863 /* Check if new hostname is valid before
2864 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002865 if( hostname != NULL )
2866 {
2867 hostname_len = strlen( hostname );
2868
2869 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2871 }
2872
2873 /* Now it's clear that we will overwrite the old hostname,
2874 * so we can free it safely */
2875
2876 if( ssl->hostname != NULL )
2877 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002878 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002879 mbedtls_free( ssl->hostname );
2880 }
2881
2882 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002883
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002885 {
2886 ssl->hostname = NULL;
2887 }
2888 else
2889 {
2890 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002891 if( ssl->hostname == NULL )
2892 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002893
Hanno Becker947194e2017-04-07 13:25:49 +01002894 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002895
Hanno Becker947194e2017-04-07 13:25:49 +01002896 ssl->hostname[hostname_len] = '\0';
2897 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
2899 return( 0 );
2900}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002901#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002903#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002904void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002906 const unsigned char *, size_t),
2907 void *p_sni )
2908{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002909 conf->f_sni = f_sni;
2910 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002915int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002916{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002917 size_t cur_len, tot_len;
2918 const char **p;
2919
2920 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002921 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2922 * MUST NOT be truncated."
2923 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002924 */
2925 tot_len = 0;
2926 for( p = protos; *p != NULL; p++ )
2927 {
2928 cur_len = strlen( *p );
2929 tot_len += cur_len;
2930
Ronald Cron8216dd32020-04-23 16:41:44 +02002931 if( ( cur_len == 0 ) ||
2932 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2933 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002935 }
2936
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002937 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002938
2939 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002940}
2941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002943{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002944 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002947
Johan Pascalb62bb512015-12-03 21:56:45 +01002948#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002949void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2950 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002951{
2952 conf->dtls_srtp_mki_support = support_mki_value;
2953}
2954
Ron Eldoref72faf2018-07-12 11:54:20 +03002955int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2956 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002957 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002958{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002959 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002960 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002962 }
Ron Eldor591f1622018-01-22 12:30:04 +02002963
2964 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002965 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002967 }
Ron Eldor591f1622018-01-22 12:30:04 +02002968
2969 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2970 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002971 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002972}
2973
Ron Eldoref72faf2018-07-12 11:54:20 +03002974int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002975 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002976{
Johan Pascal253d0262020-09-22 13:04:45 +02002977 const mbedtls_ssl_srtp_profile *p;
2978 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002979
Johan Pascal253d0262020-09-22 13:04:45 +02002980 /* check the profiles list: all entry must be valid,
2981 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002982 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2983 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2984 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002985 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002986 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002987 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002988 list_size++;
2989 }
2990 else
2991 {
2992 /* unsupported value, stop parsing and set the size to an error value */
2993 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002994 }
2995 }
2996
Johan Pascal5ef72d22020-10-28 17:05:47 +01002997 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002998 {
Johan Pascal253d0262020-09-22 13:04:45 +02002999 conf->dtls_srtp_profile_list = NULL;
3000 conf->dtls_srtp_profile_list_len = 0;
3001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3002 }
3003
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003004 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003005 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003006
3007 return( 0 );
3008}
3009
Johan Pascal5ef72d22020-10-28 17:05:47 +01003010void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
3011 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01003012{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003013 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3014 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01003015 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003016 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003017 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003018 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01003019 else
3020 {
3021 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01003022 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3023 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01003024 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003025}
Johan Pascalb62bb512015-12-03 21:56:45 +01003026#endif /* MBEDTLS_SSL_DTLS_SRTP */
3027
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303028#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003029void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00003030{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003031 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003032}
3033
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003034void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00003035{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003036 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003037}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303038#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003039
Janos Follath088ce432017-04-10 12:42:31 +01003040#if defined(MBEDTLS_SSL_SRV_C)
3041void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
3042 char cert_req_ca_list )
3043{
3044 conf->cert_req_ca_list = cert_req_ca_list;
3045}
3046#endif
3047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003049void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003050{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003051 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003052}
3053#endif
3054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003056void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003058 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003059}
3060#endif
3061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003063int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003066 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003069 }
3070
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003071 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003072
3073 return( 0 );
3074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003076
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003077void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003079 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003080}
3081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003083void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003084{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003085 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003086}
3087
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003088void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003089{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003090 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003091}
3092
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003093void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003094 const unsigned char period[8] )
3095{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003096 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003101#if defined(MBEDTLS_SSL_CLI_C)
3102void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003103{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003104 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003105}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003106#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003107
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003108#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003109
Jerry Yud0766ec2022-09-22 10:46:57 +08003110#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003111void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3112 uint16_t num_tickets )
3113{
Jerry Yud0766ec2022-09-22 10:46:57 +08003114 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003115}
3116#endif
3117
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003118void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3119 mbedtls_ssl_ticket_write_t *f_ticket_write,
3120 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3121 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003122{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003123 conf->f_ticket_write = f_ticket_write;
3124 conf->f_ticket_parse = f_ticket_parse;
3125 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003126}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003129
Hanno Becker7e6c1782021-06-08 09:24:55 +01003130void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3131 mbedtls_ssl_export_keys_t *f_export_keys,
3132 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003133{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003134 ssl->f_export_keys = f_export_keys;
3135 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003136}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003137
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003138#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003139void mbedtls_ssl_conf_async_private_cb(
3140 mbedtls_ssl_config *conf,
3141 mbedtls_ssl_async_sign_t *f_async_sign,
3142 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3143 mbedtls_ssl_async_resume_t *f_async_resume,
3144 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003145 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003146{
3147 conf->f_async_sign_start = f_async_sign;
3148 conf->f_async_decrypt_start = f_async_decrypt;
3149 conf->f_async_resume = f_async_resume;
3150 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003151 conf->p_async_config_data = async_config_data;
3152}
3153
Gilles Peskine8f97af72018-04-26 11:46:10 +02003154void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3155{
3156 return( conf->p_async_config_data );
3157}
3158
Gilles Peskine1febfef2018-04-30 11:54:39 +02003159void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003160{
3161 if( ssl->handshake == NULL )
3162 return( NULL );
3163 else
3164 return( ssl->handshake->user_async_ctx );
3165}
3166
Gilles Peskine1febfef2018-04-30 11:54:39 +02003167void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003168 void *ctx )
3169{
3170 if( ssl->handshake != NULL )
3171 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003172}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003173#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003174
Paul Bakker5121ce52009-01-03 21:22:43 +00003175/*
3176 * SSL get accessors
3177 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003178uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003179{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003180 if( ssl->session != NULL )
3181 return( ssl->session->verify_result );
3182
3183 if( ssl->session_negotiate != NULL )
3184 return( ssl->session_negotiate->verify_result );
3185
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003186 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003187}
3188
Glenn Strauss8f526902022-01-13 00:04:49 -05003189int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3190{
3191 if( ssl == NULL || ssl->session == NULL )
3192 return( 0 );
3193
3194 return( ssl->session->ciphersuite );
3195}
3196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003198{
Paul Bakker926c8e42013-03-06 10:23:34 +01003199 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003200 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003203}
3204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003208 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003209 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003210 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003211 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003212 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003213 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003214 default:
3215 return( "unknown (DTLS)" );
3216 }
3217 }
3218#endif
3219
Glenn Strauss60bfe602022-03-14 19:04:24 -04003220 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003221 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003222 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003223 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003224 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003225 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003226 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003227 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003228 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003229}
3230
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003231#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003232size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3233{
David Horstmann95d516f2021-05-04 18:36:56 +01003234 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003235 size_t read_mfl;
3236
3237 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3238 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3239 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3240 {
3241 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3242 }
3243
3244 /* Check if a smaller max length was negotiated */
3245 if( ssl->session_out != NULL )
3246 {
3247 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3248 if( read_mfl < max_len )
3249 {
3250 max_len = read_mfl;
3251 }
3252 }
3253
3254 // During a handshake, use the value being negotiated
3255 if( ssl->session_negotiate != NULL )
3256 {
3257 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3258 if( read_mfl < max_len )
3259 {
3260 max_len = read_mfl;
3261 }
3262 }
3263
3264 return( max_len );
3265}
3266
3267size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003268{
3269 size_t max_len;
3270
3271 /*
3272 * Assume mfl_code is correct since it was checked when set
3273 */
Angus Grattond8213d02016-05-25 20:56:48 +10003274 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003275
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003276 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003277 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003278 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003279 {
Angus Grattond8213d02016-05-25 20:56:48 +10003280 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003281 }
3282
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003283 /* During a handshake, use the value being negotiated */
3284 if( ssl->session_negotiate != NULL &&
3285 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3286 {
3287 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3288 }
3289
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003290 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003291}
3292#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3293
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003294#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003295size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003296{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003297 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3299 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3300 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3301 return ( 0 );
3302
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003303 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3304 return( ssl->mtu );
3305
3306 if( ssl->mtu == 0 )
3307 return( ssl->handshake->mtu );
3308
3309 return( ssl->mtu < ssl->handshake->mtu ?
3310 ssl->mtu : ssl->handshake->mtu );
3311}
3312#endif /* MBEDTLS_SSL_PROTO_DTLS */
3313
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003314int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3315{
3316 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3317
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003318#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3319 !defined(MBEDTLS_SSL_PROTO_DTLS)
3320 (void) ssl;
3321#endif
3322
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003323#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003324 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003325
3326 if( max_len > mfl )
3327 max_len = mfl;
3328#endif
3329
3330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003331 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003332 {
Hanno Becker89490712020-02-05 10:50:12 +00003333 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003334 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3335 const size_t overhead = (size_t) ret;
3336
3337 if( ret < 0 )
3338 return( ret );
3339
3340 if( mtu <= overhead )
3341 {
3342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3343 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3344 }
3345
3346 if( max_len > mtu - overhead )
3347 max_len = mtu - overhead;
3348 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003349#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003350
Hanno Becker0defedb2018-08-10 12:35:02 +01003351#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3352 !defined(MBEDTLS_SSL_PROTO_DTLS)
3353 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003354#endif
3355
3356 return( (int) max_len );
3357}
3358
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003359int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3360{
3361 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3362
3363#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3364 (void) ssl;
3365#endif
3366
3367#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3368 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3369
3370 if( max_len > mfl )
3371 max_len = mfl;
3372#endif
3373
3374 return( (int) max_len );
3375}
3376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_X509_CRT_PARSE_C)
3378const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003379{
3380 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003381 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003382
Hanno Beckere6824572019-02-07 13:18:46 +00003383#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003384 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003385#else
3386 return( NULL );
3387#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003392int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3393 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003394{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003395 int ret;
3396
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003397 if( ssl == NULL ||
3398 dst == NULL ||
3399 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003400 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003403 }
3404
Hanno Beckere810bbc2021-05-14 16:01:05 +01003405 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3406 * idempotent: Each session can only be exported once.
3407 *
3408 * (This is in preparation for TLS 1.3 support where we will
3409 * need the ability to export multiple sessions (aka tickets),
3410 * which will be achieved by calling mbedtls_ssl_get_session()
3411 * multiple times until it fails.)
3412 *
3413 * Check whether we have already exported the current session,
3414 * and fail if so.
3415 */
3416 if( ssl->session->exported == 1 )
3417 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3418
3419 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3420 if( ret != 0 )
3421 return( ret );
3422
3423 /* Remember that we've exported the session. */
3424 ssl->session->exported = 1;
3425 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003428
Paul Bakker5121ce52009-01-03 21:22:43 +00003429/*
Hanno Beckera835da52019-05-16 12:39:07 +01003430 * Define ticket header determining Mbed TLS version
3431 * and structure of the ticket.
3432 */
3433
Hanno Becker94ef3b32019-05-16 12:50:45 +01003434/*
Hanno Becker50b59662019-05-28 14:30:45 +01003435 * Define bitflag determining compile-time settings influencing
3436 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003437 */
3438
Hanno Becker50b59662019-05-28 14:30:45 +01003439#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003440#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003441#else
Hanno Becker3e088662019-05-29 11:10:18 +01003442#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003443#endif /* MBEDTLS_HAVE_TIME */
3444
3445#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003446#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003447#else
Hanno Becker3e088662019-05-29 11:10:18 +01003448#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003449#endif /* MBEDTLS_X509_CRT_PARSE_C */
3450
3451#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003452#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003453#else
Hanno Becker3e088662019-05-29 11:10:18 +01003454#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003455#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3456
3457#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003458#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003459#else
Hanno Becker3e088662019-05-29 11:10:18 +01003460#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003461#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3462
Hanno Becker94ef3b32019-05-16 12:50:45 +01003463#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003464#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003465#else
Hanno Becker3e088662019-05-29 11:10:18 +01003466#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003467#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3468
Hanno Becker94ef3b32019-05-16 12:50:45 +01003469#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3470#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3471#else
3472#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3473#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3474
Hanno Becker3e088662019-05-29 11:10:18 +01003475#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3476#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3477#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3478#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003479#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3480#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003481
Hanno Becker50b59662019-05-28 14:30:45 +01003482#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003483 ( (uint16_t) ( \
3484 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3485 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3486 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3487 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003488 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003489 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003490
Hanno Beckerf8787072019-05-16 12:41:07 +01003491static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003492 MBEDTLS_VERSION_MAJOR,
3493 MBEDTLS_VERSION_MINOR,
3494 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003495 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3496 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003497};
Hanno Beckera835da52019-05-16 12:39:07 +01003498
3499/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003500 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003501 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003502 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003503 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003504 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003505 * opaque mbedtls_version[3]; // library version: major, minor, patch
3506 * opaque session_format[2]; // library-version specific 16-bit field
3507 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003508 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003509 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003510 * Note: When updating the format, remember to keep
3511 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003512 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003513 * // In this version, `session_format` determines
3514 * // the setting of those compile-time
3515 * // configuration options which influence
3516 * // the structure of mbedtls_ssl_session.
3517 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003518 * uint8_t minor_ver; // Protocol minor version. Possible values:
3519 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003520 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003521 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003522 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003523 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003524 * serialized_session_tls12 data;
3525 *
3526 * };
3527 *
3528 * } serialized_session;
3529 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003530 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003531
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003532MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003533static int ssl_session_save( const mbedtls_ssl_session *session,
3534 unsigned char omit_header,
3535 unsigned char *buf,
3536 size_t buf_len,
3537 size_t *olen )
3538{
3539 unsigned char *p = buf;
3540 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003541 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003542#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3543 size_t out_len;
3544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3545#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003546 if( session == NULL )
3547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3548
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003549 if( !omit_header )
3550 {
3551 /*
3552 * Add Mbed TLS version identifier
3553 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003554 used += sizeof( ssl_serialized_session_header );
3555
3556 if( used <= buf_len )
3557 {
3558 memcpy( p, ssl_serialized_session_header,
3559 sizeof( ssl_serialized_session_header ) );
3560 p += sizeof( ssl_serialized_session_header );
3561 }
3562 }
3563
3564 /*
3565 * TLS version identifier
3566 */
3567 used += 1;
3568 if( used <= buf_len )
3569 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003570 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003571 }
3572
3573 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003574 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003575 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003576 {
3577#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003578 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003579 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003580 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003581#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3582
Jerry Yu251a12e2022-07-13 15:15:48 +08003583#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3584 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003585 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3586 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003587 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003588 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003589 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003590#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3591
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003592 default:
3593 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3594 }
3595
3596 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003597 if( used > buf_len )
3598 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003599
3600 return( 0 );
3601}
3602
3603/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003604 * Public wrapper for ssl_session_save()
3605 */
3606int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3607 unsigned char *buf,
3608 size_t buf_len,
3609 size_t *olen )
3610{
3611 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3612}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003613
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003614/*
3615 * Deserialize session, see mbedtls_ssl_session_save() for format.
3616 *
3617 * This internal version is wrapped by a public function that cleans up in
3618 * case of error, and has an extra option omit_header.
3619 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003620MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003621static int ssl_session_load( mbedtls_ssl_session *session,
3622 unsigned char omit_header,
3623 const unsigned char *buf,
3624 size_t len )
3625{
3626 const unsigned char *p = buf;
3627 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003628 size_t remaining_len;
3629
3630
3631 if( session == NULL )
3632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003633
3634 if( !omit_header )
3635 {
3636 /*
3637 * Check Mbed TLS version identifier
3638 */
3639
3640 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3642
3643 if( memcmp( p, ssl_serialized_session_header,
3644 sizeof( ssl_serialized_session_header ) ) != 0 )
3645 {
3646 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3647 }
3648 p += sizeof( ssl_serialized_session_header );
3649 }
3650
3651 /*
3652 * TLS version identifier
3653 */
3654 if( 1 > (size_t)( end - p ) )
3655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003656 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003657
3658 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003659 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003660 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661 {
3662#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003663 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003664 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003665#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3666
Jerry Yu438ddd82022-07-07 06:55:50 +00003667#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3668 case MBEDTLS_SSL_VERSION_TLS1_3:
3669 return( ssl_tls13_session_load( session, p, remaining_len ) );
3670#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3671
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003672 default:
3673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3674 }
3675}
3676
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003677/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003678 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003679 */
3680int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3681 const unsigned char *buf,
3682 size_t len )
3683{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003684 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003685
3686 if( ret != 0 )
3687 mbedtls_ssl_session_free( session );
3688
3689 return( ret );
3690}
3691
3692/*
Paul Bakker1961b702013-01-25 14:49:24 +01003693 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003694 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003695MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003696static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3697{
3698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3699
Ronald Cron66dbf912022-02-02 15:33:46 +01003700 /*
3701 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003702 * that were written into the output buffer by the previous handshake step,
3703 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003704 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3705 * We proceed to the next handshake step only when all data from the
3706 * previous one have been sent to the peer, thus we make sure that this is
3707 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3708 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3709 * we have to wait before to go ahead.
3710 * In the case of TLS 1.3, handshake step handlers do not send data to the
3711 * peer. Data are only sent here and through
3712 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003713 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003714 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003715 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3716 return( ret );
3717
3718#if defined(MBEDTLS_SSL_PROTO_DTLS)
3719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3720 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3721 {
3722 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3723 return( ret );
3724 }
3725#endif /* MBEDTLS_SSL_PROTO_DTLS */
3726
3727 return( ret );
3728}
3729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731{
Hanno Becker41934dd2021-08-07 19:13:43 +01003732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003733
Hanno Becker41934dd2021-08-07 19:13:43 +01003734 if( ssl == NULL ||
3735 ssl->conf == NULL ||
3736 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003737 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003738 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003740 }
3741
3742 ret = ssl_prepare_handshake_step( ssl );
3743 if( ret != 0 )
3744 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003745
Jerry Yue7047812021-09-13 19:26:39 +08003746 ret = mbedtls_ssl_handle_pending_alert( ssl );
3747 if( ret != 0 )
3748 goto cleanup;
3749
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003750 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3751 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3752 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003755 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003756 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3758 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003759
Ronald Cron9f0fba32022-02-10 16:45:15 +01003760 switch( ssl->state )
3761 {
3762 case MBEDTLS_SSL_HELLO_REQUEST:
3763 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003764 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003765 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003766
Ronald Cron9f0fba32022-02-10 16:45:15 +01003767 case MBEDTLS_SSL_CLIENT_HELLO:
3768 ret = mbedtls_ssl_write_client_hello( ssl );
3769 break;
3770
3771 default:
3772#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003773 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003774 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3775 else
3776 ret = mbedtls_ssl_handshake_client_step( ssl );
3777#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3778 ret = mbedtls_ssl_handshake_client_step( ssl );
3779#else
3780 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3781#endif
3782 }
Jerry Yub9930e72021-08-06 17:11:51 +08003783 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003786 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003787 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003788#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003789 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003790 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003791#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003792
3793#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3794 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3795 ret = mbedtls_ssl_handshake_server_step( ssl );
3796#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3797 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003798#endif
3799
Jerry Yue7047812021-09-13 19:26:39 +08003800 if( ret != 0 )
3801 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003802 /* handshake_step return error. And it is same
3803 * with alert_reason.
3804 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003805 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003806 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003807 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003808 goto cleanup;
3809 }
3810 }
3811
3812cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003813 return( ret );
3814}
3815
3816/*
3817 * Perform the SSL handshake
3818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003820{
3821 int ret = 0;
3822
Hanno Beckera817ea42020-10-20 15:20:23 +01003823 /* Sanity checks */
3824
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003825 if( ssl == NULL || ssl->conf == NULL )
3826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3827
Hanno Beckera817ea42020-10-20 15:20:23 +01003828#if defined(MBEDTLS_SSL_PROTO_DTLS)
3829 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3830 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3831 {
3832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3833 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3835 }
3836#endif /* MBEDTLS_SSL_PROTO_DTLS */
3837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003839
Hanno Beckera817ea42020-10-20 15:20:23 +01003840 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003841 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003844
3845 if( ret != 0 )
3846 break;
3847 }
3848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850
3851 return( ret );
3852}
3853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854#if defined(MBEDTLS_SSL_RENEGOTIATION)
3855#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003856/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003857 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003858 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003859MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003861{
Janos Follath865b3eb2019-12-16 11:46:15 +00003862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003865
3866 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3868 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003869
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003870 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003871 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003873 return( ret );
3874 }
3875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003877
3878 return( 0 );
3879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003881
3882/*
3883 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 * - any side: calling mbedtls_ssl_renegotiate(),
3885 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3886 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003887 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003888 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003890 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003891int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003892{
Janos Follath865b3eb2019-12-16 11:46:15 +00003893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003896
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003897 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3898 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003899
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003900 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3901 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003903 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003905 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003906 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003907 ssl->handshake->out_msg_seq = 1;
3908 else
3909 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003910 }
3911#endif
3912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3914 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003919 return( ret );
3920 }
3921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003923
3924 return( 0 );
3925}
3926
3927/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003928 * Renegotiate current connection on client,
3929 * or request renegotiation on server
3930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003934
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003935 if( ssl == NULL || ssl->conf == NULL )
3936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003939 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003940 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003941 {
Jerry Yu6848a612022-10-27 13:03:26 +08003942 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003946
3947 /* Did we already try/start sending HelloRequest? */
3948 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003950
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003951 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003952 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003956 /*
3957 * On client, either start the renegotiation process or,
3958 * if already in progress, continue the handshake
3959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003961 {
Jerry Yu6848a612022-10-27 13:03:26 +08003962 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003964
Hanno Becker40cdaa12020-02-05 10:48:27 +00003965 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003966 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003968 return( ret );
3969 }
3970 }
3971 else
3972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003976 return( ret );
3977 }
3978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003980
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003981 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003984
Gilles Peskine9b562d52018-04-25 20:32:43 +02003985void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003986{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003987 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3988
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003989 if( handshake == NULL )
3990 return;
3991
Brett Warrene0edc842021-08-17 09:53:13 +01003992#if defined(MBEDTLS_ECP_C)
3993#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3994 if ( ssl->handshake->group_list_heap_allocated )
3995 mbedtls_free( (void*) handshake->group_list );
3996 handshake->group_list = NULL;
3997#endif /* MBEDTLS_DEPRECATED_REMOVED */
3998#endif /* MBEDTLS_ECP_C */
3999
Ronald Crone68ab4f2022-10-05 12:46:29 +02004000#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004001#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4002 if ( ssl->handshake->sig_algs_heap_allocated )
4003 mbedtls_free( (void*) handshake->sig_algs );
4004 handshake->sig_algs = NULL;
4005#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004006#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4007 if( ssl->handshake->certificate_request_context )
4008 {
4009 mbedtls_free( (void*) handshake->certificate_request_context );
4010 }
4011#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004012#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004013
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004014#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4015 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
4016 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02004017 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004018 handshake->async_in_progress = 0;
4019 }
4020#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4021
Andrzej Kurek25f27152022-08-17 16:09:31 -04004022#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004023#if defined(MBEDTLS_USE_PSA_CRYPTO)
4024 psa_hash_abort( &handshake->fin_sha256_psa );
4025#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004026 mbedtls_sha256_free( &handshake->fin_sha256 );
4027#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004028#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004029#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004030#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05004031 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05004032#else
Andrzej Kureka242e832022-08-11 10:03:14 -04004033 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004034#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004035#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037#if defined(MBEDTLS_DHM_C)
4038 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00004039#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004040#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02004042#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004043
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004044#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004045#if defined(MBEDTLS_USE_PSA_CRYPTO)
4046 psa_pake_abort( &handshake->psa_pake_ctx );
4047 psa_destroy_key( handshake->psa_pake_password );
4048 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4049#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004050 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004052#if defined(MBEDTLS_SSL_CLI_C)
4053 mbedtls_free( handshake->ecjpake_cache );
4054 handshake->ecjpake_cache = NULL;
4055 handshake->ecjpake_cache_len = 0;
4056#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004057#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004058
Janos Follath4ae5c292016-02-10 11:27:43 +00004059#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4060 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004061 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004063#endif
4064
Ronald Cron73fe8df2022-10-05 14:31:43 +02004065#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004066#if defined(MBEDTLS_USE_PSA_CRYPTO)
4067 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4068 {
4069 /* The maintenance of the external PSK key slot is the
4070 * user's responsibility. */
4071 if( ssl->handshake->psk_opaque_is_internal )
4072 {
4073 psa_destroy_key( ssl->handshake->psk_opaque );
4074 ssl->handshake->psk_opaque_is_internal = 0;
4075 }
4076 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4077 }
Neil Armstronge952a302022-05-03 10:22:14 +02004078#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004079 if( handshake->psk != NULL )
4080 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004081 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004082 mbedtls_free( handshake->psk );
4083 }
Neil Armstronge952a302022-05-03 10:22:14 +02004084#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004085#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4088 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004089 /*
4090 * Free only the linked list wrapper, not the keys themselves
4091 * since the belong to the SNI callback
4092 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004093 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004095
Gilles Peskineeccd8882020-03-10 12:19:08 +01004096#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004097 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004098 if( handshake->ecrs_peer_cert != NULL )
4099 {
4100 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4101 mbedtls_free( handshake->ecrs_peer_cert );
4102 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004103#endif
4104
Hanno Becker75173122019-02-06 16:18:31 +00004105#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4106 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4107 mbedtls_pk_free( &handshake->peer_pubkey );
4108#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4109
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004110#if defined(MBEDTLS_SSL_CLI_C) && \
4111 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4112 mbedtls_free( handshake->cookie );
4113#endif /* MBEDTLS_SSL_CLI_C &&
4114 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004115
4116#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004117 mbedtls_ssl_flight_free( handshake->flight );
4118 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004119#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004120
Ronald Cronf12b81d2022-03-15 10:42:41 +01004121#if defined(MBEDTLS_ECDH_C) && \
4122 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004123 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004124 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004125#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4126
Ronald Cron6f135e12021-12-08 16:57:54 +01004127#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004128 mbedtls_ssl_transform_free( handshake->transform_handshake );
4129 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004130 mbedtls_free( handshake->transform_earlydata );
4131 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01004132#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004133
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004134
4135#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4136 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4137 * processes datagrams and the fact that a datagram is allowed to have
4138 * several records in it, it is possible that the I/O buffers are not
4139 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004140 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4141 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004142#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004143
Jerry Yuba9c7272021-10-30 11:54:10 +08004144 /* mbedtls_platform_zeroize MUST be last one in this function */
4145 mbedtls_platform_zeroize( handshake,
4146 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004147}
4148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004150{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004151 if( session == NULL )
4152 return;
4153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004155 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004156#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004157
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004158#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004159#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4160 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004161 mbedtls_free( session->hostname );
4162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004164#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004165
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004166 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004167}
4168
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004169#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004170
4171#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4172#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4173#else
4174#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4175#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4176
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004177#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004178
4179#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4180#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4181#else
4182#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4183#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4184
4185#if defined(MBEDTLS_SSL_ALPN)
4186#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4187#else
4188#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4189#endif /* MBEDTLS_SSL_ALPN */
4190
4191#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4192#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4193#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4194#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4195
4196#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4197 ( (uint32_t) ( \
4198 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4199 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4200 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4201 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4202 0u ) )
4203
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004204static unsigned char ssl_serialized_context_header[] = {
4205 MBEDTLS_VERSION_MAJOR,
4206 MBEDTLS_VERSION_MINOR,
4207 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004208 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4209 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4210 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4211 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4212 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004213};
4214
Paul Bakker5121ce52009-01-03 21:22:43 +00004215/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004216 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004217 *
4218 * The format of the serialized data is:
4219 * (in the presentation language of TLS, RFC 8446 section 3)
4220 *
4221 * // header
4222 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004223 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004224 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004225 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004226 * Note: When updating the format, remember to keep these
4227 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004228 *
4229 * // session sub-structure
4230 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4231 * // transform sub-structure
4232 * uint8 random[64]; // ServerHello.random+ClientHello.random
4233 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4234 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4235 * // fields from ssl_context
4236 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4237 * uint64 in_window_top; // DTLS: last validated record seq_num
4238 * uint64 in_window; // DTLS: bitmask for replay protection
4239 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4240 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4241 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4242 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4243 *
4244 * Note that many fields of the ssl_context or sub-structures are not
4245 * serialized, as they fall in one of the following categories:
4246 *
4247 * 1. forced value (eg in_left must be 0)
4248 * 2. pointer to dynamically-allocated memory (eg session, transform)
4249 * 3. value can be re-derived from other data (eg session keys from MS)
4250 * 4. value was temporary (eg content of input buffer)
4251 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004252 */
4253int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4254 unsigned char *buf,
4255 size_t buf_len,
4256 size_t *olen )
4257{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004258 unsigned char *p = buf;
4259 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004260 size_t session_len;
4261 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004262
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004263 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004264 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4265 * this function's documentation.
4266 *
4267 * These are due to assumptions/limitations in the implementation. Some of
4268 * them are likely to stay (no handshake in progress) some might go away
4269 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004270 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004271 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004272 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004273 {
4274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004276 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004277 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004278 {
4279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004281 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004282 /* Double-check that sub-structures are indeed ready */
4283 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004284 {
4285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004287 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004288 /* There must be no pending incoming or outgoing data */
4289 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004290 {
4291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004293 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004294 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004295 {
4296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004298 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004299 /* Protocol must be DLTS, not TLS */
4300 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004301 {
4302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004304 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004305 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004306 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004307 {
4308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004310 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004311 /* We must be using an AEAD ciphersuite */
4312 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004313 {
4314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004316 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004317 /* Renegotiation must not be enabled */
4318#if defined(MBEDTLS_SSL_RENEGOTIATION)
4319 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004320 {
4321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004323 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004324#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004325
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004326 /*
4327 * Version and format identifier
4328 */
4329 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004330
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004331 if( used <= buf_len )
4332 {
4333 memcpy( p, ssl_serialized_context_header,
4334 sizeof( ssl_serialized_context_header ) );
4335 p += sizeof( ssl_serialized_context_header );
4336 }
4337
4338 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004339 * Session (length + data)
4340 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004341 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004342 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4343 return( ret );
4344
4345 used += 4 + session_len;
4346 if( used <= buf_len )
4347 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004348 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4349 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004350
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004351 ret = ssl_session_save( ssl->session, 1,
4352 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004353 if( ret != 0 )
4354 return( ret );
4355
4356 p += session_len;
4357 }
4358
4359 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004360 * Transform
4361 */
4362 used += sizeof( ssl->transform->randbytes );
4363 if( used <= buf_len )
4364 {
4365 memcpy( p, ssl->transform->randbytes,
4366 sizeof( ssl->transform->randbytes ) );
4367 p += sizeof( ssl->transform->randbytes );
4368 }
4369
4370#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4371 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4372 if( used <= buf_len )
4373 {
4374 *p++ = ssl->transform->in_cid_len;
4375 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4376 p += ssl->transform->in_cid_len;
4377
4378 *p++ = ssl->transform->out_cid_len;
4379 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4380 p += ssl->transform->out_cid_len;
4381 }
4382#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4383
4384 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004385 * Saved fields from top-level ssl_context structure
4386 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004387 used += 4;
4388 if( used <= buf_len )
4389 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004390 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4391 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004392 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004393
4394#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4395 used += 16;
4396 if( used <= buf_len )
4397 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004398 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4399 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004400
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004401 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4402 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004403 }
4404#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4405
4406#if defined(MBEDTLS_SSL_PROTO_DTLS)
4407 used += 1;
4408 if( used <= buf_len )
4409 {
4410 *p++ = ssl->disable_datagram_packing;
4411 }
4412#endif /* MBEDTLS_SSL_PROTO_DTLS */
4413
Jerry Yuae0b2e22021-10-08 15:21:19 +08004414 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004415 if( used <= buf_len )
4416 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004417 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4418 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004419 }
4420
4421#if defined(MBEDTLS_SSL_PROTO_DTLS)
4422 used += 2;
4423 if( used <= buf_len )
4424 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004425 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4426 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004427 }
4428#endif /* MBEDTLS_SSL_PROTO_DTLS */
4429
4430#if defined(MBEDTLS_SSL_ALPN)
4431 {
4432 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004433 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004434 : 0;
4435
4436 used += 1 + alpn_len;
4437 if( used <= buf_len )
4438 {
4439 *p++ = alpn_len;
4440
4441 if( ssl->alpn_chosen != NULL )
4442 {
4443 memcpy( p, ssl->alpn_chosen, alpn_len );
4444 p += alpn_len;
4445 }
4446 }
4447 }
4448#endif /* MBEDTLS_SSL_ALPN */
4449
4450 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004451 * Done
4452 */
4453 *olen = used;
4454
4455 if( used > buf_len )
4456 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004457
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004458 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4459
Hanno Becker43aefe22020-02-05 10:44:56 +00004460 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004461}
4462
4463/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004464 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004465 *
4466 * This internal version is wrapped by a public function that cleans up in
4467 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004468 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004469MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004470static int ssl_context_load( mbedtls_ssl_context *ssl,
4471 const unsigned char *buf,
4472 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004473{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004474 const unsigned char *p = buf;
4475 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004476 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004478#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004479 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004480#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004481
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004482 /*
4483 * The context should have been freshly setup or reset.
4484 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004485 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004486 * renegotiating, or if the user mistakenly loaded a session first.)
4487 */
4488 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4489 ssl->session != NULL )
4490 {
4491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4492 }
4493
4494 /*
4495 * We can't check that the config matches the initial one, but we can at
4496 * least check it matches the requirements for serializing.
4497 */
4498 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004499 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4500 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004501#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004502 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004503#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004504 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004505 {
4506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4507 }
4508
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004509 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4510
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004511 /*
4512 * Check version identifier
4513 */
4514 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4516
4517 if( memcmp( p, ssl_serialized_context_header,
4518 sizeof( ssl_serialized_context_header ) ) != 0 )
4519 {
4520 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4521 }
4522 p += sizeof( ssl_serialized_context_header );
4523
4524 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004525 * Session
4526 */
4527 if( (size_t)( end - p ) < 4 )
4528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4529
4530 session_len = ( (size_t) p[0] << 24 ) |
4531 ( (size_t) p[1] << 16 ) |
4532 ( (size_t) p[2] << 8 ) |
4533 ( (size_t) p[3] );
4534 p += 4;
4535
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004536 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004537 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004538 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004539 ssl->session_in = ssl->session;
4540 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004541 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004542
4543 if( (size_t)( end - p ) < session_len )
4544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4545
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004546 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004547 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004548 {
4549 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004550 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004551 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004552
4553 p += session_len;
4554
4555 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004556 * Transform
4557 */
4558
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004559 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004560 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004561 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004562 ssl->transform_in = ssl->transform;
4563 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004564 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004565
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004567 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4568 if( prf_func == NULL )
4569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4570
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004571 /* Read random bytes and populate structure */
4572 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004574
Hanno Beckerbd257552021-03-22 06:59:27 +00004575 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004576 ssl->session->ciphersuite,
4577 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004578#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004579 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004580#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004581 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004582 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004583 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004584 ssl->conf->endpoint,
4585 ssl );
4586 if( ret != 0 )
4587 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004588#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004589 p += sizeof( ssl->transform->randbytes );
4590
4591#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4592 /* Read connection IDs and store them */
4593 if( (size_t)( end - p ) < 1 )
4594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4595
4596 ssl->transform->in_cid_len = *p++;
4597
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004598 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4600
4601 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4602 p += ssl->transform->in_cid_len;
4603
4604 ssl->transform->out_cid_len = *p++;
4605
4606 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4608
4609 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4610 p += ssl->transform->out_cid_len;
4611#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4612
4613 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004614 * Saved fields from top-level ssl_context structure
4615 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004616 if( (size_t)( end - p ) < 4 )
4617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4618
4619 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4620 ( (uint32_t) p[1] << 16 ) |
4621 ( (uint32_t) p[2] << 8 ) |
4622 ( (uint32_t) p[3] );
4623 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004624
4625#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4626 if( (size_t)( end - p ) < 16 )
4627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4628
4629 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4630 ( (uint64_t) p[1] << 48 ) |
4631 ( (uint64_t) p[2] << 40 ) |
4632 ( (uint64_t) p[3] << 32 ) |
4633 ( (uint64_t) p[4] << 24 ) |
4634 ( (uint64_t) p[5] << 16 ) |
4635 ( (uint64_t) p[6] << 8 ) |
4636 ( (uint64_t) p[7] );
4637 p += 8;
4638
4639 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4640 ( (uint64_t) p[1] << 48 ) |
4641 ( (uint64_t) p[2] << 40 ) |
4642 ( (uint64_t) p[3] << 32 ) |
4643 ( (uint64_t) p[4] << 24 ) |
4644 ( (uint64_t) p[5] << 16 ) |
4645 ( (uint64_t) p[6] << 8 ) |
4646 ( (uint64_t) p[7] );
4647 p += 8;
4648#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4649
4650#if defined(MBEDTLS_SSL_PROTO_DTLS)
4651 if( (size_t)( end - p ) < 1 )
4652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4653
4654 ssl->disable_datagram_packing = *p++;
4655#endif /* MBEDTLS_SSL_PROTO_DTLS */
4656
Jerry Yud9a94fe2021-09-28 18:58:59 +08004657 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004659 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4660 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004661
4662#if defined(MBEDTLS_SSL_PROTO_DTLS)
4663 if( (size_t)( end - p ) < 2 )
4664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4665
4666 ssl->mtu = ( p[0] << 8 ) | p[1];
4667 p += 2;
4668#endif /* MBEDTLS_SSL_PROTO_DTLS */
4669
4670#if defined(MBEDTLS_SSL_ALPN)
4671 {
4672 uint8_t alpn_len;
4673 const char **cur;
4674
4675 if( (size_t)( end - p ) < 1 )
4676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4677
4678 alpn_len = *p++;
4679
4680 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4681 {
4682 /* alpn_chosen should point to an item in the configured list */
4683 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4684 {
4685 if( strlen( *cur ) == alpn_len &&
4686 memcmp( p, cur, alpn_len ) == 0 )
4687 {
4688 ssl->alpn_chosen = *cur;
4689 break;
4690 }
4691 }
4692 }
4693
4694 /* can only happen on conf mismatch */
4695 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4697
4698 p += alpn_len;
4699 }
4700#endif /* MBEDTLS_SSL_ALPN */
4701
4702 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004703 * Forced fields from top-level ssl_context structure
4704 *
4705 * Most of them already set to the correct value by mbedtls_ssl_init() and
4706 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4707 */
4708 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004709 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004710
Hanno Becker361b10d2019-08-30 10:42:49 +01004711 /* Adjust pointers for header fields of outgoing records to
4712 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004713 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004714
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004715#if defined(MBEDTLS_SSL_PROTO_DTLS)
4716 ssl->in_epoch = 1;
4717#endif
4718
4719 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4720 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004721 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4722 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004723 if( ssl->handshake != NULL )
4724 {
4725 mbedtls_ssl_handshake_free( ssl );
4726 mbedtls_free( ssl->handshake );
4727 ssl->handshake = NULL;
4728 }
4729
4730 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004731 * Done - should have consumed entire buffer
4732 */
4733 if( p != end )
4734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004735
4736 return( 0 );
4737}
4738
4739/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004740 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004741 */
4742int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4743 const unsigned char *buf,
4744 size_t len )
4745{
4746 int ret = ssl_context_load( context, buf, len );
4747
4748 if( ret != 0 )
4749 mbedtls_ssl_free( context );
4750
4751 return( ret );
4752}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004753#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004754
4755/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 * Free an SSL context
4757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004759{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004760 if( ssl == NULL )
4761 return;
4762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004764
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004765 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004766 {
sander-visserb8aa2072020-05-06 22:05:13 +02004767#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4768 size_t out_buf_len = ssl->out_buf_len;
4769#else
4770 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4771#endif
4772
Darryl Greenb33cc762019-11-28 14:29:44 +00004773 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004775 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004776 }
4777
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004778 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 {
sander-visserb8aa2072020-05-06 22:05:13 +02004780#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4781 size_t in_buf_len = ssl->in_buf_len;
4782#else
4783 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4784#endif
4785
Darryl Greenb33cc762019-11-28 14:29:44 +00004786 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004788 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004789 }
4790
Paul Bakker48916f92012-09-16 19:57:18 +00004791 if( ssl->transform )
4792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 mbedtls_ssl_transform_free( ssl->transform );
4794 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004795 }
4796
4797 if( ssl->handshake )
4798 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004799 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4801 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803 mbedtls_free( ssl->handshake );
4804 mbedtls_free( ssl->transform_negotiate );
4805 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004806 }
4807
Ronald Cron6f135e12021-12-08 16:57:54 +01004808#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004809 mbedtls_ssl_transform_free( ssl->transform_application );
4810 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004811#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004812
Paul Bakkerc0463502013-02-14 11:19:38 +01004813 if( ssl->session )
4814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 mbedtls_ssl_session_free( ssl->session );
4816 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004817 }
4818
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004819#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004820 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004822 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004824 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004826
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004827#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004829#endif
4830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004832
Paul Bakker86f04f42013-02-14 11:20:09 +01004833 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004834 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835}
4836
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004837/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004838 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004839 */
4840void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4841{
4842 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4843}
4844
Gilles Peskineae270bf2021-06-02 00:05:29 +02004845/* The selection should be the same as mbedtls_x509_crt_profile_default in
4846 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004847 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004848 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004849 * about this list.
4850 */
Brett Warrene0edc842021-08-17 09:53:13 +01004851static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004852#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004853 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004854#endif
4855#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004856 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004857#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004858#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004859 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004860#endif
4861#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004862 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004863#endif
4864#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004865 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004866#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004867#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004868 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004869#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004870#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004871 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004872#endif
4873#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004874 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004875#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004876 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004877};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004878
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004879static int ssl_preset_suiteb_ciphersuites[] = {
4880 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4881 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4882 0
4883};
4884
Ronald Crone68ab4f2022-10-05 12:46:29 +02004885#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004886
Jerry Yu909df7b2022-01-22 11:56:27 +08004887/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004888 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004889 * rules SHOULD be upheld.
4890 * - No duplicate entries.
4891 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004892 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004893 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004894 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004895static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004896
Andrzej Kurek25f27152022-08-17 16:09:31 -04004897#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 +08004898 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4899 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004900#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004901 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004902
Andrzej Kurek25f27152022-08-17 16:09:31 -04004903#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 +08004904 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4905 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004906#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004907 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4908
Andrzej Kurek25f27152022-08-17 16:09:31 -04004909#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 +08004910 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4911 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004912#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004913 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004914
Andrzej Kurek25f27152022-08-17 16:09:31 -04004915#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 +08004916 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004917#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 +08004918
Andrzej Kurek25f27152022-08-17 16:09:31 -04004919#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 +08004920 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004921#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 +08004922
Andrzej Kurek25f27152022-08-17 16:09:31 -04004923#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 +08004924 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004925#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 +08004926
Andrzej Kurek25f27152022-08-17 16:09:31 -04004927#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 +08004928 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4929#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4930
Andrzej Kurek25f27152022-08-17 16:09:31 -04004931#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 +08004932 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4933#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4934
Andrzej Kurek25f27152022-08-17 16:09:31 -04004935#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 +08004936 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4937#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4938
Gabor Mezei15b95a62022-05-09 16:37:58 +02004939 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004940};
4941
4942/* NOTICE: see above */
4943#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4944static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004945#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004946#if defined(MBEDTLS_ECDSA_C)
4947 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004948#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004949#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4950 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4951#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004952#if defined(MBEDTLS_RSA_C)
4953 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4954#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004955#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004956#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004957#if defined(MBEDTLS_ECDSA_C)
4958 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004959#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004960#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4961 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4962#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004963#if defined(MBEDTLS_RSA_C)
4964 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4965#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004966#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004967#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004968#if defined(MBEDTLS_ECDSA_C)
4969 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004970#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004971#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4972 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4973#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004974#if defined(MBEDTLS_RSA_C)
4975 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4976#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004977#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004978 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004979};
4980#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4981/* NOTICE: see above */
4982static uint16_t ssl_preset_suiteb_sig_algs[] = {
4983
Andrzej Kurek25f27152022-08-17 16:09:31 -04004984#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 +08004985 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4986 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004987#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004988 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4989
Andrzej Kurek25f27152022-08-17 16:09:31 -04004990#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 +08004991 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4992 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004993#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004994 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004995
Andrzej Kurek25f27152022-08-17 16:09:31 -04004996#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 +08004997 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004998#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 +08004999
Andrzej Kurek25f27152022-08-17 16:09:31 -04005000#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 +08005001 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005002#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005003
Gabor Mezei15b95a62022-05-09 16:37:58 +02005004 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005005};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005006
Jerry Yu909df7b2022-01-22 11:56:27 +08005007/* NOTICE: see above */
5008#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5009static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005010#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005011#if defined(MBEDTLS_ECDSA_C)
5012 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08005013#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005014#if defined(MBEDTLS_RSA_C)
5015 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
5016#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005017#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005018#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005019#if defined(MBEDTLS_ECDSA_C)
5020 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08005021#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005022#if defined(MBEDTLS_RSA_C)
5023 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
5024#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005025#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005026 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005027};
Jerry Yu909df7b2022-01-22 11:56:27 +08005028#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5029
Ronald Crone68ab4f2022-10-05 12:46:29 +02005030#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005031
Brett Warrene0edc842021-08-17 09:53:13 +01005032static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005033#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005034 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005035#endif
5036#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005037 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005038#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005039 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005040};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005041
Ronald Crone68ab4f2022-10-05 12:46:29 +02005042#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005043/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005044 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005045MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005046static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005047{
5048 size_t i, j;
5049 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005050
Gabor Mezei15b95a62022-05-09 16:37:58 +02005051 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005052 {
Jerry Yuf377d642022-01-25 10:43:59 +08005053 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005054 {
Jerry Yuf377d642022-01-25 10:43:59 +08005055 if( sig_algs[i] != sig_algs[j] )
5056 continue;
5057 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5058 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5059 sig_algs[i], j, i );
5060 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005061 }
5062 }
5063 return( ret );
5064}
5065
Ronald Crone68ab4f2022-10-05 12:46:29 +02005066#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005067
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005068/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005069 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005070 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005071int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005072 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005073{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005074#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005076#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005077
Ronald Crone68ab4f2022-10-05 12:46:29 +02005078#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005079 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005080 {
5081 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5082 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5083 }
5084
Jerry Yuf377d642022-01-25 10:43:59 +08005085 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005086 {
5087 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5088 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5089 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005090
5091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005092 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005093 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005094 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005095 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5096 }
5097
Jerry Yuf377d642022-01-25 10:43:59 +08005098 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005099 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005100 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005101 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5102 }
5103#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005104#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005105
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005106 /* Use the functions here so that they are covered in tests,
5107 * but otherwise access member directly for efficiency */
5108 mbedtls_ssl_conf_endpoint( conf, endpoint );
5109 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005110
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005111 /*
5112 * Things that are common to all presets
5113 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005114#if defined(MBEDTLS_SSL_CLI_C)
5115 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5116 {
5117 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5118#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5119 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5120#endif
5121 }
5122#endif
5123
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005124#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5125 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5126#endif
5127
5128#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5129 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5130#endif
5131
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005132#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005133 conf->f_cookie_write = ssl_cookie_write_dummy;
5134 conf->f_cookie_check = ssl_cookie_check_dummy;
5135#endif
5136
5137#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5138 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5139#endif
5140
Janos Follath088ce432017-04-10 12:42:31 +01005141#if defined(MBEDTLS_SSL_SRV_C)
5142 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005143 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005144#endif
5145
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005146#if defined(MBEDTLS_SSL_PROTO_DTLS)
5147 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5148 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5149#endif
5150
5151#if defined(MBEDTLS_SSL_RENEGOTIATION)
5152 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005153 memset( conf->renego_period, 0x00, 2 );
5154 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005155#endif
5156
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005157#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005158 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5159 {
5160 const unsigned char dhm_p[] =
5161 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5162 const unsigned char dhm_g[] =
5163 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005164
Hanno Beckere2defad2021-07-24 05:59:17 +01005165 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5166 dhm_p, sizeof( dhm_p ),
5167 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5168 {
5169 return( ret );
5170 }
5171 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005172#endif
5173
Ronald Cron6f135e12021-12-08 16:57:54 +01005174#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005175#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005176 mbedtls_ssl_conf_new_session_tickets(
5177 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5178#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005179 /*
5180 * Allow all TLS 1.3 key exchange modes by default.
5181 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005182 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005183#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005184
XiaokangQian4d3a6042022-04-21 13:46:17 +00005185 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5186 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005187#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005188 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005189 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005190#else
XiaokangQian060d8672022-04-21 09:24:56 +00005191 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005192#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005193 }
5194 else
5195 {
5196#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5197 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5198 {
5199 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5200 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5201 }
5202 else
5203 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5204 {
5205 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5206 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5207 }
5208#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5209 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5210 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5211#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5212 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5213 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5214#else
5215 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5216#endif
5217 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005218
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005219 /*
5220 * Preset-specific defaults
5221 */
5222 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005223 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005224 /*
5225 * NSA Suite B
5226 */
5227 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005228
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005229 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005230
5231#if defined(MBEDTLS_X509_CRT_PARSE_C)
5232 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005233#endif
5234
Ronald Crone68ab4f2022-10-05 12:46:29 +02005235#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005236#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5237 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5238 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5239 else
5240#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5241 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005242#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005243
Brett Warrene0edc842021-08-17 09:53:13 +01005244#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5245 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005246#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005247 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005248 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005249
5250 /*
5251 * Default
5252 */
5253 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005254
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005255 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005256
5257#if defined(MBEDTLS_X509_CRT_PARSE_C)
5258 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5259#endif
5260
Ronald Crone68ab4f2022-10-05 12:46:29 +02005261#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5263 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5264 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5265 else
5266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5267 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005268#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005269
Brett Warrene0edc842021-08-17 09:53:13 +01005270#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5271 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005272#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005273 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005274
5275#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5276 conf->dhm_min_bitlen = 1024;
5277#endif
5278 }
5279
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005280 return( 0 );
5281}
5282
5283/*
5284 * Free mbedtls_ssl_config
5285 */
5286void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5287{
5288#if defined(MBEDTLS_DHM_C)
5289 mbedtls_mpi_free( &conf->dhm_P );
5290 mbedtls_mpi_free( &conf->dhm_G );
5291#endif
5292
Ronald Cron73fe8df2022-10-05 14:31:43 +02005293#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005294#if defined(MBEDTLS_USE_PSA_CRYPTO)
5295 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5296 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005297 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5298 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005299#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005300 if( conf->psk != NULL )
5301 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005302 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005303 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005304 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005305 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005306 }
5307
5308 if( conf->psk_identity != NULL )
5309 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005310 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005311 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005312 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005313 conf->psk_identity_len = 0;
5314 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005315#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005316
5317#if defined(MBEDTLS_X509_CRT_PARSE_C)
5318 ssl_key_cert_free( conf->key_cert );
5319#endif
5320
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005321 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005322}
5323
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005324#if defined(MBEDTLS_PK_C) && \
5325 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005326/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_RSA_C)
5332 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5333 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_ECDSA_C)
5336 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5337 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005340}
5341
Hanno Becker7e5437a2017-04-28 17:15:26 +01005342unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5343{
5344 switch( type ) {
5345 case MBEDTLS_PK_RSA:
5346 return( MBEDTLS_SSL_SIG_RSA );
5347 case MBEDTLS_PK_ECDSA:
5348 case MBEDTLS_PK_ECKEY:
5349 return( MBEDTLS_SSL_SIG_ECDSA );
5350 default:
5351 return( MBEDTLS_SSL_SIG_ANON );
5352 }
5353}
5354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005356{
5357 switch( sig )
5358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359#if defined(MBEDTLS_RSA_C)
5360 case MBEDTLS_SSL_SIG_RSA:
5361 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363#if defined(MBEDTLS_ECDSA_C)
5364 case MBEDTLS_SSL_SIG_ECDSA:
5365 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005366#endif
5367 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005369 }
5370}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005371#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005372
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005373/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005374 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005377{
5378 switch( hash )
5379 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005380#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 case MBEDTLS_SSL_HASH_MD5:
5382 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005383#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005384#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 case MBEDTLS_SSL_HASH_SHA1:
5386 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005387#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005388#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 case MBEDTLS_SSL_HASH_SHA224:
5390 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005391#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005392#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 case MBEDTLS_SSL_HASH_SHA256:
5394 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005395#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005396#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 case MBEDTLS_SSL_HASH_SHA384:
5398 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005399#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005400#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 case MBEDTLS_SSL_HASH_SHA512:
5402 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005403#endif
5404 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005406 }
5407}
5408
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005409/*
5410 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5411 */
5412unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5413{
5414 switch( md )
5415 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005416#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005417 case MBEDTLS_MD_MD5:
5418 return( MBEDTLS_SSL_HASH_MD5 );
5419#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005420#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005421 case MBEDTLS_MD_SHA1:
5422 return( MBEDTLS_SSL_HASH_SHA1 );
5423#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005424#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005425 case MBEDTLS_MD_SHA224:
5426 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005427#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005428#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005429 case MBEDTLS_MD_SHA256:
5430 return( MBEDTLS_SSL_HASH_SHA256 );
5431#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005432#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005433 case MBEDTLS_MD_SHA384:
5434 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005435#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005436#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005437 case MBEDTLS_MD_SHA512:
5438 return( MBEDTLS_SSL_HASH_SHA512 );
5439#endif
5440 default:
5441 return( MBEDTLS_SSL_HASH_NONE );
5442 }
5443}
5444
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005445/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005446 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005447 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005448 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005449int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005450{
Brett Warrene0edc842021-08-17 09:53:13 +01005451 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005452
Brett Warrene0edc842021-08-17 09:53:13 +01005453 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005454 return( -1 );
5455
Brett Warrene0edc842021-08-17 09:53:13 +01005456 for( ; *group_list != 0; group_list++ )
5457 {
5458 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005459 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005460 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005461
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005462 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005463}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005464
5465#if defined(MBEDTLS_ECP_C)
5466/*
5467 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5468 */
5469int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5470{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005471 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005472 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005473
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005474 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005475 return -1;
5476
5477 uint16_t tls_id = grp_info->tls_id;
5478
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005479 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5480}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005481#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483#if defined(MBEDTLS_X509_CRT_PARSE_C)
5484int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5485 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005486 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005487 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005488{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005489 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005490 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005491 const char *ext_oid;
5492 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005495 {
5496 /* Server part of the key exchange */
5497 switch( ciphersuite->key_exchange )
5498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 case MBEDTLS_KEY_EXCHANGE_RSA:
5500 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005501 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005502 break;
5503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5505 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5506 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5507 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005508 break;
5509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5511 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005512 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005513 break;
5514
5515 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 case MBEDTLS_KEY_EXCHANGE_NONE:
5517 case MBEDTLS_KEY_EXCHANGE_PSK:
5518 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5519 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005520 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005521 usage = 0;
5522 }
5523 }
5524 else
5525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5527 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005528 }
5529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005531 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005532 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005533 ret = -1;
5534 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5539 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005540 }
5541 else
5542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5544 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005545 }
5546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005548 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005549 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005550 ret = -1;
5551 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005552
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005553 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005554}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005556
Jerry Yu148165c2021-09-24 23:20:59 +08005557#if defined(MBEDTLS_USE_PSA_CRYPTO)
5558int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5559 const mbedtls_md_type_t md,
5560 unsigned char *dst,
5561 size_t dst_len,
5562 size_t *olen )
5563{
Ronald Cronf6893e12022-01-07 22:09:01 +01005564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5565 psa_hash_operation_t *hash_operation_to_clone;
5566 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5567
Jerry Yu148165c2021-09-24 23:20:59 +08005568 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005569
5570 switch( md )
5571 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005572#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005573 case MBEDTLS_MD_SHA384:
5574 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5575 break;
5576#endif
5577
Andrzej Kurek25f27152022-08-17 16:09:31 -04005578#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005579 case MBEDTLS_MD_SHA256:
5580 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5581 break;
5582#endif
5583
5584 default:
5585 goto exit;
5586 }
5587
5588 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5589 if( status != PSA_SUCCESS )
5590 goto exit;
5591
5592 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5593 if( status != PSA_SUCCESS )
5594 goto exit;
5595
5596exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005597#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5598 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5599 (void) ssl;
5600#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005601 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005602}
5603#else /* MBEDTLS_USE_PSA_CRYPTO */
5604
Andrzej Kurek25f27152022-08-17 16:09:31 -04005605#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005606MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005607static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5608 unsigned char *dst,
5609 size_t dst_len,
5610 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005611{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005612 int ret;
5613 mbedtls_sha512_context sha512;
5614
5615 if( dst_len < 48 )
5616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5617
5618 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005619 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005620
5621 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5622 {
5623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5624 goto exit;
5625 }
5626
5627 *olen = 48;
5628
5629exit:
5630
5631 mbedtls_sha512_free( &sha512 );
5632 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005633}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005634#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005635
Andrzej Kurek25f27152022-08-17 16:09:31 -04005636#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005637MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005638static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5639 unsigned char *dst,
5640 size_t dst_len,
5641 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005642{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005643 int ret;
5644 mbedtls_sha256_context sha256;
5645
5646 if( dst_len < 32 )
5647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5648
5649 mbedtls_sha256_init( &sha256 );
5650 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005651
Jerry Yu24c0ec32021-09-09 14:21:07 +08005652 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5653 {
5654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5655 goto exit;
5656 }
5657
5658 *olen = 32;
5659
5660exit:
5661
5662 mbedtls_sha256_free( &sha256 );
5663 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005664}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005665#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005666
Jerry Yu000f9762021-09-14 11:12:51 +08005667int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5668 const mbedtls_md_type_t md,
5669 unsigned char *dst,
5670 size_t dst_len,
5671 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005672{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005673 switch( md )
5674 {
5675
Andrzej Kurek25f27152022-08-17 16:09:31 -04005676#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005677 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005678 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005679#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005680
Andrzej Kurek25f27152022-08-17 16:09:31 -04005681#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005682 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005683 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005684#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005685
5686 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005687#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5688 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5689 (void) ssl;
5690 (void) dst;
5691 (void) dst_len;
5692 (void) olen;
5693#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005694 break;
5695 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005697}
XiaokangQian647719a2021-12-07 09:16:29 +00005698
Jerry Yu148165c2021-09-24 23:20:59 +08005699#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005700
Ronald Crone68ab4f2022-10-05 12:46:29 +02005701#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005702/* mbedtls_ssl_parse_sig_alg_ext()
5703 *
5704 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5705 * value (TLS 1.3 RFC8446):
5706 * enum {
5707 * ....
5708 * ecdsa_secp256r1_sha256( 0x0403 ),
5709 * ecdsa_secp384r1_sha384( 0x0503 ),
5710 * ecdsa_secp521r1_sha512( 0x0603 ),
5711 * ....
5712 * } SignatureScheme;
5713 *
5714 * struct {
5715 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5716 * } SignatureSchemeList;
5717 *
5718 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5719 * value (TLS 1.2 RFC5246):
5720 * enum {
5721 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5722 * sha512(6), (255)
5723 * } HashAlgorithm;
5724 *
5725 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5726 * SignatureAlgorithm;
5727 *
5728 * struct {
5729 * HashAlgorithm hash;
5730 * SignatureAlgorithm signature;
5731 * } SignatureAndHashAlgorithm;
5732 *
5733 * SignatureAndHashAlgorithm
5734 * supported_signature_algorithms<2..2^16-2>;
5735 *
5736 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5737 * generalization of the TLS 1.2 signature algorithm extension.
5738 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5739 * `SignatureScheme` field of TLS 1.3
5740 *
5741 */
5742int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5743 const unsigned char *buf,
5744 const unsigned char *end )
5745{
5746 const unsigned char *p = buf;
5747 size_t supported_sig_algs_len = 0;
5748 const unsigned char *supported_sig_algs_end;
5749 uint16_t sig_alg;
5750 uint32_t common_idx = 0;
5751
5752 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5753 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5754 p += 2;
5755
5756 memset( ssl->handshake->received_sig_algs, 0,
5757 sizeof(ssl->handshake->received_sig_algs) );
5758
5759 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5760 supported_sig_algs_end = p + supported_sig_algs_len;
5761 while( p < supported_sig_algs_end )
5762 {
5763 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5764 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5765 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005766 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5767 sig_alg,
5768 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005769#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005770 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005771 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5772 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5773 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005774 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005775 }
5776#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005777
Jerry Yuf3b46b52022-06-19 16:52:27 +08005778 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5779 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005780
5781 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5782 {
5783 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5784 common_idx += 1;
5785 }
5786 }
5787 /* Check that we consumed all the message. */
5788 if( p != end )
5789 {
5790 MBEDTLS_SSL_DEBUG_MSG( 1,
5791 ( "Signature algorithms extension length misaligned" ) );
5792 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5793 MBEDTLS_ERR_SSL_DECODE_ERROR );
5794 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5795 }
5796
5797 if( common_idx == 0 )
5798 {
5799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5800 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5801 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5802 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5803 }
5804
Gabor Mezei15b95a62022-05-09 16:37:58 +02005805 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005806 return( 0 );
5807}
5808
Ronald Crone68ab4f2022-10-05 12:46:29 +02005809#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005810
Jerry Yudc7bd172022-02-17 13:44:15 +08005811#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5812
5813#if defined(MBEDTLS_USE_PSA_CRYPTO)
5814
5815static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5816 mbedtls_svc_key_id_t key,
5817 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005818 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005819 const unsigned char* seed, size_t seed_length,
5820 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005821 const unsigned char* other_secret,
5822 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005823 size_t capacity )
5824{
5825 psa_status_t status;
5826
5827 status = psa_key_derivation_setup( derivation, alg );
5828 if( status != PSA_SUCCESS )
5829 return( status );
5830
5831 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5832 {
5833 status = psa_key_derivation_input_bytes( derivation,
5834 PSA_KEY_DERIVATION_INPUT_SEED,
5835 seed, seed_length );
5836 if( status != PSA_SUCCESS )
5837 return( status );
5838
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005839 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005840 {
5841 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005842 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5843 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005844 if( status != PSA_SUCCESS )
5845 return( status );
5846 }
5847
Jerry Yudc7bd172022-02-17 13:44:15 +08005848 if( mbedtls_svc_key_id_is_null( key ) )
5849 {
5850 status = psa_key_derivation_input_bytes(
5851 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005852 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005853 }
5854 else
5855 {
5856 status = psa_key_derivation_input_key(
5857 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5858 }
5859 if( status != PSA_SUCCESS )
5860 return( status );
5861
5862 status = psa_key_derivation_input_bytes( derivation,
5863 PSA_KEY_DERIVATION_INPUT_LABEL,
5864 label, label_length );
5865 if( status != PSA_SUCCESS )
5866 return( status );
5867 }
5868 else
5869 {
5870 return( PSA_ERROR_NOT_SUPPORTED );
5871 }
5872
5873 status = psa_key_derivation_set_capacity( derivation, capacity );
5874 if( status != PSA_SUCCESS )
5875 return( status );
5876
5877 return( PSA_SUCCESS );
5878}
5879
Andrzej Kurek57d10632022-10-24 10:32:01 -04005880#if defined(PSA_WANT_ALG_SHA_384) || \
5881 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005882MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005883static int tls_prf_generic( mbedtls_md_type_t md_type,
5884 const unsigned char *secret, size_t slen,
5885 const char *label,
5886 const unsigned char *random, size_t rlen,
5887 unsigned char *dstbuf, size_t dlen )
5888{
5889 psa_status_t status;
5890 psa_algorithm_t alg;
5891 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5892 psa_key_derivation_operation_t derivation =
5893 PSA_KEY_DERIVATION_OPERATION_INIT;
5894
5895 if( md_type == MBEDTLS_MD_SHA384 )
5896 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5897 else
5898 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5899
5900 /* Normally a "secret" should be long enough to be impossible to
5901 * find by brute force, and in particular should not be empty. But
5902 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5903 * and for this use case it makes sense to have a 0-length "secret".
5904 * Since the key API doesn't allow importing a key of length 0,
5905 * keep master_key=0, which setup_psa_key_derivation() understands
5906 * to mean a 0-length "secret" input. */
5907 if( slen != 0 )
5908 {
5909 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5910 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5911 psa_set_key_algorithm( &key_attributes, alg );
5912 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5913
5914 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5915 if( status != PSA_SUCCESS )
5916 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5917 }
5918
5919 status = setup_psa_key_derivation( &derivation,
5920 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005921 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005922 random, rlen,
5923 (unsigned char const *) label,
5924 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005925 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005926 dlen );
5927 if( status != PSA_SUCCESS )
5928 {
5929 psa_key_derivation_abort( &derivation );
5930 psa_destroy_key( master_key );
5931 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5932 }
5933
5934 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5935 if( status != PSA_SUCCESS )
5936 {
5937 psa_key_derivation_abort( &derivation );
5938 psa_destroy_key( master_key );
5939 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5940 }
5941
5942 status = psa_key_derivation_abort( &derivation );
5943 if( status != PSA_SUCCESS )
5944 {
5945 psa_destroy_key( master_key );
5946 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5947 }
5948
5949 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5950 status = psa_destroy_key( master_key );
5951 if( status != PSA_SUCCESS )
5952 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5953
5954 return( 0 );
5955}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005956#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005957#else /* MBEDTLS_USE_PSA_CRYPTO */
5958
Andrzej Kurek57d10632022-10-24 10:32:01 -04005959#if defined(MBEDTLS_MD_C) && \
5960 ( defined(MBEDTLS_SHA256_C) || \
5961 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005962MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005963static int tls_prf_generic( mbedtls_md_type_t md_type,
5964 const unsigned char *secret, size_t slen,
5965 const char *label,
5966 const unsigned char *random, size_t rlen,
5967 unsigned char *dstbuf, size_t dlen )
5968{
5969 size_t nb;
5970 size_t i, j, k, md_len;
5971 unsigned char *tmp;
5972 size_t tmp_len = 0;
5973 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5974 const mbedtls_md_info_t *md_info;
5975 mbedtls_md_context_t md_ctx;
5976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5977
5978 mbedtls_md_init( &md_ctx );
5979
5980 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5982
5983 md_len = mbedtls_md_get_size( md_info );
5984
5985 tmp_len = md_len + strlen( label ) + rlen;
5986 tmp = mbedtls_calloc( 1, tmp_len );
5987 if( tmp == NULL )
5988 {
5989 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5990 goto exit;
5991 }
5992
5993 nb = strlen( label );
5994 memcpy( tmp + md_len, label, nb );
5995 memcpy( tmp + md_len + nb, random, rlen );
5996 nb += rlen;
5997
5998 /*
5999 * Compute P_<hash>(secret, label + random)[0..dlen]
6000 */
6001 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
6002 goto exit;
6003
6004 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
6005 if( ret != 0 )
6006 goto exit;
6007 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
6008 if( ret != 0 )
6009 goto exit;
6010 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6011 if( ret != 0 )
6012 goto exit;
6013
6014 for( i = 0; i < dlen; i += md_len )
6015 {
6016 ret = mbedtls_md_hmac_reset ( &md_ctx );
6017 if( ret != 0 )
6018 goto exit;
6019 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
6020 if( ret != 0 )
6021 goto exit;
6022 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
6023 if( ret != 0 )
6024 goto exit;
6025
6026 ret = mbedtls_md_hmac_reset ( &md_ctx );
6027 if( ret != 0 )
6028 goto exit;
6029 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
6030 if( ret != 0 )
6031 goto exit;
6032 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6033 if( ret != 0 )
6034 goto exit;
6035
6036 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
6037
6038 for( j = 0; j < k; j++ )
6039 dstbuf[i + j] = h_i[j];
6040 }
6041
6042exit:
6043 mbedtls_md_free( &md_ctx );
6044
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006045 if ( tmp != NULL )
6046 mbedtls_platform_zeroize( tmp, tmp_len );
6047
Jerry Yudc7bd172022-02-17 13:44:15 +08006048 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6049
6050 mbedtls_free( tmp );
6051
6052 return( ret );
6053}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006054#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006055#endif /* MBEDTLS_USE_PSA_CRYPTO */
6056
Andrzej Kurek25f27152022-08-17 16:09:31 -04006057#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006058MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006059static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6060 const char *label,
6061 const unsigned char *random, size_t rlen,
6062 unsigned char *dstbuf, size_t dlen )
6063{
6064 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6065 label, random, rlen, dstbuf, dlen ) );
6066}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006067#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006068
Andrzej Kurek25f27152022-08-17 16:09:31 -04006069#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006070MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006071static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6072 const char *label,
6073 const unsigned char *random, size_t rlen,
6074 unsigned char *dstbuf, size_t dlen )
6075{
6076 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6077 label, random, rlen, dstbuf, dlen ) );
6078}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006079#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006080
Jerry Yuf009d862022-02-17 14:01:37 +08006081/*
6082 * Set appropriate PRF function and other SSL / TLS1.2 functions
6083 *
6084 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006085 * - hash associated with the ciphersuite (only used by TLS 1.2)
6086 *
6087 * Outputs:
6088 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6089 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006090MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006091static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006092 mbedtls_md_type_t hash )
6093{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006094#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006095 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006096 {
6097 handshake->tls_prf = tls_prf_sha384;
6098 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6099 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6100 }
6101 else
6102#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006103#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006104 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006105 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006106 handshake->tls_prf = tls_prf_sha256;
6107 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6108 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6109 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006110#else
Jerry Yuf009d862022-02-17 14:01:37 +08006111 {
Jerry Yuf009d862022-02-17 14:01:37 +08006112 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006113 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6115 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006116#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006117
6118 return( 0 );
6119}
Jerry Yud6ab2352022-02-17 14:03:43 +08006120
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006121/*
6122 * Compute master secret if needed
6123 *
6124 * Parameters:
6125 * [in/out] handshake
6126 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6127 * (PSA-PSK) ciphersuite_info, psk_opaque
6128 * [out] premaster (cleared)
6129 * [out] master
6130 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6131 * debug: conf->f_dbg, conf->p_dbg
6132 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006133 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006134 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006135MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006136static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6137 unsigned char *master,
6138 const mbedtls_ssl_context *ssl )
6139{
6140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6141
6142 /* cf. RFC 5246, Section 8.1:
6143 * "The master secret is always exactly 48 bytes in length." */
6144 size_t const master_secret_len = 48;
6145
6146#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6147 unsigned char session_hash[48];
6148#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6149
6150 /* The label for the KDF used for key expansion.
6151 * This is either "master secret" or "extended master secret"
6152 * depending on whether the Extended Master Secret extension
6153 * is used. */
6154 char const *lbl = "master secret";
6155
Przemek Stekielae4ed302022-04-05 17:15:55 +02006156 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006157 * - If the Extended Master Secret extension is not used,
6158 * this is ClientHello.Random + ServerHello.Random
6159 * (see Sect. 8.1 in RFC 5246).
6160 * - If the Extended Master Secret extension is used,
6161 * this is the transcript of the handshake so far.
6162 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006163 unsigned char const *seed = handshake->randbytes;
6164 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006165
6166#if !defined(MBEDTLS_DEBUG_C) && \
6167 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6168 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6169 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6170 ssl = NULL; /* make sure we don't use it except for those cases */
6171 (void) ssl;
6172#endif
6173
6174 if( handshake->resume != 0 )
6175 {
6176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6177 return( 0 );
6178 }
6179
6180#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6181 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6182 {
6183 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006184 seed = session_hash;
6185 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006186
6187 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006188 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006189 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006190#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006191
Przemek Stekiel99114f32022-04-22 11:20:09 +02006192#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006193 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006194 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006195 {
6196 /* Perform PSK-to-MS expansion in a single step. */
6197 psa_status_t status;
6198 psa_algorithm_t alg;
6199 mbedtls_svc_key_id_t psk;
6200 psa_key_derivation_operation_t derivation =
6201 PSA_KEY_DERIVATION_OPERATION_INIT;
6202 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6203
6204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6205
6206 psk = mbedtls_ssl_get_opaque_psk( ssl );
6207
6208 if( hash_alg == MBEDTLS_MD_SHA384 )
6209 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6210 else
6211 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6212
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006213 size_t other_secret_len = 0;
6214 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006215
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006216 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006217 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006218 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006219 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006220 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006221 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006222 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006223 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006224 other_secret_len = 48;
6225 other_secret = handshake->premaster + 2;
6226 break;
6227 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006228 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006229 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6230 other_secret = handshake->premaster + 2;
6231 break;
6232 default:
6233 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006234 }
6235
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006236 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006237 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006238 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006239 (unsigned char const *) lbl,
6240 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006241 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006242 master_secret_len );
6243 if( status != PSA_SUCCESS )
6244 {
6245 psa_key_derivation_abort( &derivation );
6246 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6247 }
6248
6249 status = psa_key_derivation_output_bytes( &derivation,
6250 master,
6251 master_secret_len );
6252 if( status != PSA_SUCCESS )
6253 {
6254 psa_key_derivation_abort( &derivation );
6255 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6256 }
6257
6258 status = psa_key_derivation_abort( &derivation );
6259 if( status != PSA_SUCCESS )
6260 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6261 }
6262 else
6263#endif
6264 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006265#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6266 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6267 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6268 {
6269 psa_status_t status;
6270 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6271 psa_key_derivation_operation_t derivation =
6272 PSA_KEY_DERIVATION_OPERATION_INIT;
6273
6274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6275
6276 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6277
6278 status = psa_key_derivation_setup( &derivation, alg );
6279 if( status != PSA_SUCCESS )
6280 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6281
6282 status = psa_key_derivation_set_capacity( &derivation,
6283 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6284 if( status != PSA_SUCCESS )
6285 {
6286 psa_key_derivation_abort( &derivation );
6287 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6288 }
6289
6290 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6291 &derivation );
6292 if( status != PSA_SUCCESS )
6293 {
6294 psa_key_derivation_abort( &derivation );
6295 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6296 }
6297
6298 status = psa_key_derivation_output_bytes( &derivation,
6299 handshake->premaster,
6300 handshake->pmslen );
6301 if( status != PSA_SUCCESS )
6302 {
6303 psa_key_derivation_abort( &derivation );
6304 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6305 }
6306
6307 status = psa_key_derivation_abort( &derivation );
6308 if( status != PSA_SUCCESS )
6309 {
6310 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6311 }
6312 }
6313#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006314 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006315 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006316 master,
6317 master_secret_len );
6318 if( ret != 0 )
6319 {
6320 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6321 return( ret );
6322 }
6323
6324 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6325 handshake->premaster,
6326 handshake->pmslen );
6327
6328 mbedtls_platform_zeroize( handshake->premaster,
6329 sizeof(handshake->premaster) );
6330 }
6331
6332 return( 0 );
6333}
6334
Jerry Yud62f87e2022-02-17 14:09:02 +08006335int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6336{
6337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6338 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6339 ssl->handshake->ciphersuite_info;
6340
6341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6342
6343 /* Set PRF, calc_verify and calc_finished function pointers */
6344 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006345 ciphersuite_info->mac );
6346 if( ret != 0 )
6347 {
6348 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6349 return( ret );
6350 }
6351
6352 /* Compute master secret if needed */
6353 ret = ssl_compute_master( ssl->handshake,
6354 ssl->session_negotiate->master,
6355 ssl );
6356 if( ret != 0 )
6357 {
6358 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6359 return( ret );
6360 }
6361
6362 /* Swap the client and server random values:
6363 * - MS derivation wanted client+server (RFC 5246 8.1)
6364 * - key derivation wants server+client (RFC 5246 6.3) */
6365 {
6366 unsigned char tmp[64];
6367 memcpy( tmp, ssl->handshake->randbytes, 64 );
6368 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6369 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6370 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6371 }
6372
6373 /* Populate transform structure */
6374 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6375 ssl->session_negotiate->ciphersuite,
6376 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006377#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006378 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006379#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006380 ssl->handshake->tls_prf,
6381 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006382 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006383 ssl->conf->endpoint,
6384 ssl );
6385 if( ret != 0 )
6386 {
6387 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6388 return( ret );
6389 }
6390
6391 /* We no longer need Server/ClientHello.random values */
6392 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6393 sizeof( ssl->handshake->randbytes ) );
6394
6395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6396
6397 return( 0 );
6398}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006399
Ronald Cron4dcbca92022-03-07 10:21:40 +01006400int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6401{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006402 switch( md )
6403 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006404#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006405 case MBEDTLS_SSL_HASH_SHA384:
6406 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6407 break;
6408#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006409#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006410 case MBEDTLS_SSL_HASH_SHA256:
6411 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6412 break;
6413#endif
6414 default:
6415 return( -1 );
6416 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006417#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6418 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6419 (void) ssl;
6420#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006421 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006422}
6423
Andrzej Kurek25f27152022-08-17 16:09:31 -04006424#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006425void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6426 unsigned char *hash,
6427 size_t *hlen )
6428{
6429#if defined(MBEDTLS_USE_PSA_CRYPTO)
6430 size_t hash_size;
6431 psa_status_t status;
6432 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6433
6434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6435 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6436 if( status != PSA_SUCCESS )
6437 {
6438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6439 return;
6440 }
6441
6442 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6443 if( status != PSA_SUCCESS )
6444 {
6445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6446 return;
6447 }
6448
6449 *hlen = 32;
6450 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6452#else
6453 mbedtls_sha256_context sha256;
6454
6455 mbedtls_sha256_init( &sha256 );
6456
6457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6458
6459 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6460 mbedtls_sha256_finish( &sha256, hash );
6461
6462 *hlen = 32;
6463
6464 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6466
6467 mbedtls_sha256_free( &sha256 );
6468#endif /* MBEDTLS_USE_PSA_CRYPTO */
6469 return;
6470}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006471#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006472
Andrzej Kurek25f27152022-08-17 16:09:31 -04006473#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006474void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6475 unsigned char *hash,
6476 size_t *hlen )
6477{
6478#if defined(MBEDTLS_USE_PSA_CRYPTO)
6479 size_t hash_size;
6480 psa_status_t status;
6481 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6482
6483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6484 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6485 if( status != PSA_SUCCESS )
6486 {
6487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6488 return;
6489 }
6490
6491 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6492 if( status != PSA_SUCCESS )
6493 {
6494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6495 return;
6496 }
6497
6498 *hlen = 48;
6499 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6501#else
6502 mbedtls_sha512_context sha512;
6503
6504 mbedtls_sha512_init( &sha512 );
6505
6506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6507
Andrzej Kureka242e832022-08-11 10:03:14 -04006508 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006509 mbedtls_sha512_finish( &sha512, hash );
6510
6511 *hlen = 48;
6512
6513 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6515
6516 mbedtls_sha512_free( &sha512 );
6517#endif /* MBEDTLS_USE_PSA_CRYPTO */
6518 return;
6519}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006520#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006521
Neil Armstrong80f6f322022-05-03 17:56:38 +02006522#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6523 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006524int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6525{
6526 unsigned char *p = ssl->handshake->premaster;
6527 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6528 const unsigned char *psk = NULL;
6529 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006530 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006531
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006532 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006533 {
6534 /*
6535 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006536 * checked before calling this function.
6537 *
6538 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006539 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006540 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006541 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6542 {
6543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6545 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006546 }
6547
6548 /*
6549 * PMS = struct {
6550 * opaque other_secret<0..2^16-1>;
6551 * opaque psk<0..2^16-1>;
6552 * };
6553 * with "other_secret" depending on the particular key exchange
6554 */
6555#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6556 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6557 {
6558 if( end - p < 2 )
6559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6560
6561 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6562 p += 2;
6563
6564 if( end < p || (size_t)( end - p ) < psk_len )
6565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6566
6567 memset( p, 0, psk_len );
6568 p += psk_len;
6569 }
6570 else
6571#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6572#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6573 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6574 {
6575 /*
6576 * other_secret already set by the ClientKeyExchange message,
6577 * and is 48 bytes long
6578 */
6579 if( end - p < 2 )
6580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6581
6582 *p++ = 0;
6583 *p++ = 48;
6584 p += 48;
6585 }
6586 else
6587#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6588#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6589 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6590 {
6591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6592 size_t len;
6593
6594 /* Write length only when we know the actual value */
6595 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6596 p + 2, end - ( p + 2 ), &len,
6597 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6598 {
6599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6600 return( ret );
6601 }
6602 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6603 p += 2 + len;
6604
6605 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6606 }
6607 else
6608#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006609#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006610 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6611 {
6612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6613 size_t zlen;
6614
6615 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6616 p + 2, end - ( p + 2 ),
6617 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6618 {
6619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6620 return( ret );
6621 }
6622
6623 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6624 p += 2 + zlen;
6625
6626 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6627 MBEDTLS_DEBUG_ECDH_Z );
6628 }
6629 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006630#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006631 {
6632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6634 }
6635
6636 /* opaque psk<0..2^16-1>; */
6637 if( end - p < 2 )
6638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6639
6640 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6641 p += 2;
6642
6643 if( end < p || (size_t)( end - p ) < psk_len )
6644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6645
6646 memcpy( p, psk, psk_len );
6647 p += psk_len;
6648
6649 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6650
6651 return( 0 );
6652}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006653#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006654
6655#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006656MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006657static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6658
6659#if defined(MBEDTLS_SSL_PROTO_DTLS)
6660int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6661{
6662 /* If renegotiation is not enforced, retransmit until we would reach max
6663 * timeout if we were using the usual handshake doubling scheme */
6664 if( ssl->conf->renego_max_records < 0 )
6665 {
6666 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6667 unsigned char doublings = 1;
6668
6669 while( ratio != 0 )
6670 {
6671 ++doublings;
6672 ratio >>= 1;
6673 }
6674
6675 if( ++ssl->renego_records_seen > doublings )
6676 {
6677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6678 return( 0 );
6679 }
6680 }
6681
6682 return( ssl_write_hello_request( ssl ) );
6683}
6684#endif
6685#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006686
Jerry Yud9526692022-02-17 14:23:47 +08006687/*
6688 * Handshake functions
6689 */
6690#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6691/* No certificate support -> dummy functions */
6692int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6693{
6694 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6695 ssl->handshake->ciphersuite_info;
6696
6697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6698
6699 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6700 {
6701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6702 ssl->state++;
6703 return( 0 );
6704 }
6705
6706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6708}
6709
6710int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6711{
6712 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6713 ssl->handshake->ciphersuite_info;
6714
6715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6716
6717 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6718 {
6719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6720 ssl->state++;
6721 return( 0 );
6722 }
6723
6724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6726}
6727
6728#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6729/* Some certificate support -> implement write and parse */
6730
6731int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6732{
6733 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6734 size_t i, n;
6735 const mbedtls_x509_crt *crt;
6736 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6737 ssl->handshake->ciphersuite_info;
6738
6739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6740
6741 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6742 {
6743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6744 ssl->state++;
6745 return( 0 );
6746 }
6747
6748#if defined(MBEDTLS_SSL_CLI_C)
6749 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6750 {
6751 if( ssl->handshake->client_auth == 0 )
6752 {
6753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6754 ssl->state++;
6755 return( 0 );
6756 }
6757 }
6758#endif /* MBEDTLS_SSL_CLI_C */
6759#if defined(MBEDTLS_SSL_SRV_C)
6760 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6761 {
6762 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6763 {
6764 /* Should never happen because we shouldn't have picked the
6765 * ciphersuite if we don't have a certificate. */
6766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6767 }
6768 }
6769#endif
6770
6771 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6772
6773 /*
6774 * 0 . 0 handshake type
6775 * 1 . 3 handshake length
6776 * 4 . 6 length of all certs
6777 * 7 . 9 length of cert. 1
6778 * 10 . n-1 peer certificate
6779 * n . n+2 length of cert. 2
6780 * n+3 . ... upper level cert, etc.
6781 */
6782 i = 7;
6783 crt = mbedtls_ssl_own_cert( ssl );
6784
6785 while( crt != NULL )
6786 {
6787 n = crt->raw.len;
6788 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6789 {
6790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6791 " > %" MBEDTLS_PRINTF_SIZET,
6792 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6793 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6794 }
6795
6796 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6797 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6798 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6799
6800 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6801 i += n; crt = crt->next;
6802 }
6803
6804 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6805 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6806 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6807
6808 ssl->out_msglen = i;
6809 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6810 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6811
6812 ssl->state++;
6813
6814 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6815 {
6816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6817 return( ret );
6818 }
6819
6820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6821
6822 return( ret );
6823}
6824
6825#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6826
6827#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006828MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006829static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6830 unsigned char *crt_buf,
6831 size_t crt_buf_len )
6832{
6833 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6834
6835 if( peer_crt == NULL )
6836 return( -1 );
6837
6838 if( peer_crt->raw.len != crt_buf_len )
6839 return( -1 );
6840
6841 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6842}
6843#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006844MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006845static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6846 unsigned char *crt_buf,
6847 size_t crt_buf_len )
6848{
6849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6850 unsigned char const * const peer_cert_digest =
6851 ssl->session->peer_cert_digest;
6852 mbedtls_md_type_t const peer_cert_digest_type =
6853 ssl->session->peer_cert_digest_type;
6854 mbedtls_md_info_t const * const digest_info =
6855 mbedtls_md_info_from_type( peer_cert_digest_type );
6856 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6857 size_t digest_len;
6858
6859 if( peer_cert_digest == NULL || digest_info == NULL )
6860 return( -1 );
6861
6862 digest_len = mbedtls_md_get_size( digest_info );
6863 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6864 return( -1 );
6865
6866 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6867 if( ret != 0 )
6868 return( -1 );
6869
6870 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6871}
6872#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6873#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6874
6875/*
6876 * Once the certificate message is read, parse it into a cert chain and
6877 * perform basic checks, but leave actual verification to the caller
6878 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006879MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006880static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6881 mbedtls_x509_crt *chain )
6882{
6883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6884#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6885 int crt_cnt=0;
6886#endif
6887 size_t i, n;
6888 uint8_t alert;
6889
6890 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6891 {
6892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6894 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6895 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6896 }
6897
6898 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6899 {
6900 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6901 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6902 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6903 }
6904
6905 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6906 {
6907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6908 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6909 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6910 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6911 }
6912
6913 i = mbedtls_ssl_hs_hdr_len( ssl );
6914
6915 /*
6916 * Same message structure as in mbedtls_ssl_write_certificate()
6917 */
6918 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6919
6920 if( ssl->in_msg[i] != 0 ||
6921 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6922 {
6923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6924 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6925 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6926 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6927 }
6928
6929 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6930 i += 3;
6931
6932 /* Iterate through and parse the CRTs in the provided chain. */
6933 while( i < ssl->in_hslen )
6934 {
6935 /* Check that there's room for the next CRT's length fields. */
6936 if ( i + 3 > ssl->in_hslen ) {
6937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6938 mbedtls_ssl_send_alert_message( ssl,
6939 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6940 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6941 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6942 }
6943 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6944 * anything beyond 2**16 ~ 64K. */
6945 if( ssl->in_msg[i] != 0 )
6946 {
6947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6948 mbedtls_ssl_send_alert_message( ssl,
6949 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6950 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6951 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6952 }
6953
6954 /* Read length of the next CRT in the chain. */
6955 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6956 | (unsigned int) ssl->in_msg[i + 2];
6957 i += 3;
6958
6959 if( n < 128 || i + n > ssl->in_hslen )
6960 {
6961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6962 mbedtls_ssl_send_alert_message( ssl,
6963 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6965 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6966 }
6967
6968 /* Check if we're handling the first CRT in the chain. */
6969#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6970 if( crt_cnt++ == 0 &&
6971 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6972 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6973 {
6974 /* During client-side renegotiation, check that the server's
6975 * end-CRTs hasn't changed compared to the initial handshake,
6976 * mitigating the triple handshake attack. On success, reuse
6977 * the original end-CRT instead of parsing it again. */
6978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6979 if( ssl_check_peer_crt_unchanged( ssl,
6980 &ssl->in_msg[i],
6981 n ) != 0 )
6982 {
6983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6984 mbedtls_ssl_send_alert_message( ssl,
6985 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6986 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6987 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6988 }
6989
6990 /* Now we can safely free the original chain. */
6991 ssl_clear_peer_cert( ssl->session );
6992 }
6993#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6994
6995 /* Parse the next certificate in the chain. */
6996#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6997 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6998#else
6999 /* If we don't need to store the CRT chain permanently, parse
7000 * it in-place from the input buffer instead of making a copy. */
7001 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7002#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7003 switch( ret )
7004 {
7005 case 0: /*ok*/
7006 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7007 /* Ignore certificate with an unknown algorithm: maybe a
7008 prior certificate was already trusted. */
7009 break;
7010
7011 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7012 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7013 goto crt_parse_der_failed;
7014
7015 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7016 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7017 goto crt_parse_der_failed;
7018
7019 default:
7020 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7021 crt_parse_der_failed:
7022 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
7023 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7024 return( ret );
7025 }
7026
7027 i += n;
7028 }
7029
7030 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
7031 return( 0 );
7032}
7033
7034#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007035MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007036static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7037{
7038 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7039 return( -1 );
7040
7041 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7042 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7043 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7044 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7045 {
Ronald Cron19385882022-06-15 16:26:13 +02007046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007047 return( 0 );
7048 }
7049 return( -1 );
7050}
7051#endif /* MBEDTLS_SSL_SRV_C */
7052
7053/* Check if a certificate message is expected.
7054 * Return either
7055 * - SSL_CERTIFICATE_EXPECTED, or
7056 * - SSL_CERTIFICATE_SKIP
7057 * indicating whether a Certificate message is expected or not.
7058 */
7059#define SSL_CERTIFICATE_EXPECTED 0
7060#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007061MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007062static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7063 int authmode )
7064{
7065 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7066 ssl->handshake->ciphersuite_info;
7067
7068 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7069 return( SSL_CERTIFICATE_SKIP );
7070
7071#if defined(MBEDTLS_SSL_SRV_C)
7072 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7073 {
7074 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7075 return( SSL_CERTIFICATE_SKIP );
7076
7077 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7078 {
7079 ssl->session_negotiate->verify_result =
7080 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7081 return( SSL_CERTIFICATE_SKIP );
7082 }
7083 }
7084#else
7085 ((void) authmode);
7086#endif /* MBEDTLS_SSL_SRV_C */
7087
7088 return( SSL_CERTIFICATE_EXPECTED );
7089}
7090
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007091MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007092static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7093 int authmode,
7094 mbedtls_x509_crt *chain,
7095 void *rs_ctx )
7096{
7097 int ret = 0;
7098 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7099 ssl->handshake->ciphersuite_info;
7100 int have_ca_chain = 0;
7101
7102 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7103 void *p_vrfy;
7104
7105 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7106 return( 0 );
7107
7108 if( ssl->f_vrfy != NULL )
7109 {
7110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7111 f_vrfy = ssl->f_vrfy;
7112 p_vrfy = ssl->p_vrfy;
7113 }
7114 else
7115 {
7116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7117 f_vrfy = ssl->conf->f_vrfy;
7118 p_vrfy = ssl->conf->p_vrfy;
7119 }
7120
7121 /*
7122 * Main check: verify certificate
7123 */
7124#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7125 if( ssl->conf->f_ca_cb != NULL )
7126 {
7127 ((void) rs_ctx);
7128 have_ca_chain = 1;
7129
7130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7131 ret = mbedtls_x509_crt_verify_with_ca_cb(
7132 chain,
7133 ssl->conf->f_ca_cb,
7134 ssl->conf->p_ca_cb,
7135 ssl->conf->cert_profile,
7136 ssl->hostname,
7137 &ssl->session_negotiate->verify_result,
7138 f_vrfy, p_vrfy );
7139 }
7140 else
7141#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7142 {
7143 mbedtls_x509_crt *ca_chain;
7144 mbedtls_x509_crl *ca_crl;
7145
7146#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7147 if( ssl->handshake->sni_ca_chain != NULL )
7148 {
7149 ca_chain = ssl->handshake->sni_ca_chain;
7150 ca_crl = ssl->handshake->sni_ca_crl;
7151 }
7152 else
7153#endif
7154 {
7155 ca_chain = ssl->conf->ca_chain;
7156 ca_crl = ssl->conf->ca_crl;
7157 }
7158
7159 if( ca_chain != NULL )
7160 have_ca_chain = 1;
7161
7162 ret = mbedtls_x509_crt_verify_restartable(
7163 chain,
7164 ca_chain, ca_crl,
7165 ssl->conf->cert_profile,
7166 ssl->hostname,
7167 &ssl->session_negotiate->verify_result,
7168 f_vrfy, p_vrfy, rs_ctx );
7169 }
7170
7171 if( ret != 0 )
7172 {
7173 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7174 }
7175
7176#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7177 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7178 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7179#endif
7180
7181 /*
7182 * Secondary checks: always done, but change 'ret' only if it was 0
7183 */
7184
7185#if defined(MBEDTLS_ECP_C)
7186 {
7187 const mbedtls_pk_context *pk = &chain->pk;
7188
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007189 /* If certificate uses an EC key, make sure the curve is OK.
7190 * This is a public key, so it can't be opaque, so can_do() is a good
7191 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007192 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007193 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007194 /* and in the unlikely case the above assumption no longer holds
7195 * we are making sure that pk_ec() here does not return a NULL
7196 */
7197 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7198 if( ec == NULL )
7199 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7202 }
Jerry Yud9526692022-02-17 14:23:47 +08007203
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007204 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7205 {
7206 ssl->session_negotiate->verify_result |=
7207 MBEDTLS_X509_BADCERT_BAD_KEY;
7208
7209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7210 if( ret == 0 )
7211 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7212 }
Jerry Yud9526692022-02-17 14:23:47 +08007213 }
7214 }
7215#endif /* MBEDTLS_ECP_C */
7216
7217 if( mbedtls_ssl_check_cert_usage( chain,
7218 ciphersuite_info,
7219 ! ssl->conf->endpoint,
7220 &ssl->session_negotiate->verify_result ) != 0 )
7221 {
7222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7223 if( ret == 0 )
7224 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7225 }
7226
7227 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7228 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7229 * with details encoded in the verification flags. All other kinds
7230 * of error codes, including those from the user provided f_vrfy
7231 * functions, are treated as fatal and lead to a failure of
7232 * ssl_parse_certificate even if verification was optional. */
7233 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7234 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7235 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7236 {
7237 ret = 0;
7238 }
7239
7240 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7241 {
7242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7243 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7244 }
7245
7246 if( ret != 0 )
7247 {
7248 uint8_t alert;
7249
7250 /* The certificate may have been rejected for several reasons.
7251 Pick one and send the corresponding alert. Which alert to send
7252 may be a subject of debate in some cases. */
7253 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7254 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7256 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7258 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7260 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7261 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7262 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7263 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7264 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7265 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7266 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7267 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7268 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7269 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7270 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7271 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7272 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7273 else
7274 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7275 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7276 alert );
7277 }
7278
7279#if defined(MBEDTLS_DEBUG_C)
7280 if( ssl->session_negotiate->verify_result != 0 )
7281 {
7282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7283 (unsigned int) ssl->session_negotiate->verify_result ) );
7284 }
7285 else
7286 {
7287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7288 }
7289#endif /* MBEDTLS_DEBUG_C */
7290
7291 return( ret );
7292}
7293
7294#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007295MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007296static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7297 unsigned char *start, size_t len )
7298{
7299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7300 /* Remember digest of the peer's end-CRT. */
7301 ssl->session_negotiate->peer_cert_digest =
7302 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7303 if( ssl->session_negotiate->peer_cert_digest == NULL )
7304 {
7305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7306 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7307 mbedtls_ssl_send_alert_message( ssl,
7308 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7309 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7310
7311 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7312 }
7313
7314 ret = mbedtls_md( mbedtls_md_info_from_type(
7315 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7316 start, len,
7317 ssl->session_negotiate->peer_cert_digest );
7318
7319 ssl->session_negotiate->peer_cert_digest_type =
7320 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7321 ssl->session_negotiate->peer_cert_digest_len =
7322 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7323
7324 return( ret );
7325}
7326
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007327MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007328static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7329 unsigned char *start, size_t len )
7330{
7331 unsigned char *end = start + len;
7332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7333
7334 /* Make a copy of the peer's raw public key. */
7335 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7336 ret = mbedtls_pk_parse_subpubkey( &start, end,
7337 &ssl->handshake->peer_pubkey );
7338 if( ret != 0 )
7339 {
7340 /* We should have parsed the public key before. */
7341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7342 }
7343
7344 return( 0 );
7345}
7346#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7347
7348int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7349{
7350 int ret = 0;
7351 int crt_expected;
7352#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7353 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7354 ? ssl->handshake->sni_authmode
7355 : ssl->conf->authmode;
7356#else
7357 const int authmode = ssl->conf->authmode;
7358#endif
7359 void *rs_ctx = NULL;
7360 mbedtls_x509_crt *chain = NULL;
7361
7362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7363
7364 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7365 if( crt_expected == SSL_CERTIFICATE_SKIP )
7366 {
7367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7368 goto exit;
7369 }
7370
7371#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7372 if( ssl->handshake->ecrs_enabled &&
7373 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7374 {
7375 chain = ssl->handshake->ecrs_peer_cert;
7376 ssl->handshake->ecrs_peer_cert = NULL;
7377 goto crt_verify;
7378 }
7379#endif
7380
7381 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7382 {
7383 /* mbedtls_ssl_read_record may have sent an alert already. We
7384 let it decide whether to alert. */
7385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7386 goto exit;
7387 }
7388
7389#if defined(MBEDTLS_SSL_SRV_C)
7390 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7391 {
7392 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7393
7394 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7395 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7396
7397 goto exit;
7398 }
7399#endif /* MBEDTLS_SSL_SRV_C */
7400
7401 /* Clear existing peer CRT structure in case we tried to
7402 * reuse a session but it failed, and allocate a new one. */
7403 ssl_clear_peer_cert( ssl->session_negotiate );
7404
7405 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7406 if( chain == NULL )
7407 {
7408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7409 sizeof( mbedtls_x509_crt ) ) );
7410 mbedtls_ssl_send_alert_message( ssl,
7411 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7412 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7413
7414 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7415 goto exit;
7416 }
7417 mbedtls_x509_crt_init( chain );
7418
7419 ret = ssl_parse_certificate_chain( ssl, chain );
7420 if( ret != 0 )
7421 goto exit;
7422
7423#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7424 if( ssl->handshake->ecrs_enabled)
7425 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7426
7427crt_verify:
7428 if( ssl->handshake->ecrs_enabled)
7429 rs_ctx = &ssl->handshake->ecrs_ctx;
7430#endif
7431
7432 ret = ssl_parse_certificate_verify( ssl, authmode,
7433 chain, rs_ctx );
7434 if( ret != 0 )
7435 goto exit;
7436
7437#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7438 {
7439 unsigned char *crt_start, *pk_start;
7440 size_t crt_len, pk_len;
7441
7442 /* We parse the CRT chain without copying, so
7443 * these pointers point into the input buffer,
7444 * and are hence still valid after freeing the
7445 * CRT chain. */
7446
7447 crt_start = chain->raw.p;
7448 crt_len = chain->raw.len;
7449
7450 pk_start = chain->pk_raw.p;
7451 pk_len = chain->pk_raw.len;
7452
7453 /* Free the CRT structures before computing
7454 * digest and copying the peer's public key. */
7455 mbedtls_x509_crt_free( chain );
7456 mbedtls_free( chain );
7457 chain = NULL;
7458
7459 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7460 if( ret != 0 )
7461 goto exit;
7462
7463 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7464 if( ret != 0 )
7465 goto exit;
7466 }
7467#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7468 /* Pass ownership to session structure. */
7469 ssl->session_negotiate->peer_cert = chain;
7470 chain = NULL;
7471#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7472
7473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7474
7475exit:
7476
7477 if( ret == 0 )
7478 ssl->state++;
7479
7480#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7481 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7482 {
7483 ssl->handshake->ecrs_peer_cert = chain;
7484 chain = NULL;
7485 }
7486#endif
7487
7488 if( chain != NULL )
7489 {
7490 mbedtls_x509_crt_free( chain );
7491 mbedtls_free( chain );
7492 }
7493
7494 return( ret );
7495}
7496#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7497
Andrzej Kurek25f27152022-08-17 16:09:31 -04007498#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007499static void ssl_calc_finished_tls_sha256(
7500 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7501{
7502 int len = 12;
7503 const char *sender;
7504 unsigned char padbuf[32];
7505#if defined(MBEDTLS_USE_PSA_CRYPTO)
7506 size_t hash_size;
7507 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7508 psa_status_t status;
7509#else
7510 mbedtls_sha256_context sha256;
7511#endif
7512
7513 mbedtls_ssl_session *session = ssl->session_negotiate;
7514 if( !session )
7515 session = ssl->session;
7516
7517 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7518 ? "client finished"
7519 : "server finished";
7520
7521#if defined(MBEDTLS_USE_PSA_CRYPTO)
7522 sha256_psa = psa_hash_operation_init();
7523
7524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7525
7526 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7527 if( status != PSA_SUCCESS )
7528 {
7529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7530 return;
7531 }
7532
7533 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7534 if( status != PSA_SUCCESS )
7535 {
7536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7537 return;
7538 }
7539 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7540#else
7541
7542 mbedtls_sha256_init( &sha256 );
7543
7544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7545
7546 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7547
7548 /*
7549 * TLSv1.2:
7550 * hash = PRF( master, finished_label,
7551 * Hash( handshake ) )[0.11]
7552 */
7553
7554#if !defined(MBEDTLS_SHA256_ALT)
7555 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7556 sha256.state, sizeof( sha256.state ) );
7557#endif
7558
7559 mbedtls_sha256_finish( &sha256, padbuf );
7560 mbedtls_sha256_free( &sha256 );
7561#endif /* MBEDTLS_USE_PSA_CRYPTO */
7562
7563 ssl->handshake->tls_prf( session->master, 48, sender,
7564 padbuf, 32, buf, len );
7565
7566 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7567
7568 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7569
7570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7571}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007572#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007573
Jerry Yub7ba49e2022-02-17 14:25:53 +08007574
Andrzej Kurek25f27152022-08-17 16:09:31 -04007575#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007576static void ssl_calc_finished_tls_sha384(
7577 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7578{
7579 int len = 12;
7580 const char *sender;
7581 unsigned char padbuf[48];
7582#if defined(MBEDTLS_USE_PSA_CRYPTO)
7583 size_t hash_size;
7584 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7585 psa_status_t status;
7586#else
7587 mbedtls_sha512_context sha512;
7588#endif
7589
7590 mbedtls_ssl_session *session = ssl->session_negotiate;
7591 if( !session )
7592 session = ssl->session;
7593
7594 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7595 ? "client finished"
7596 : "server finished";
7597
7598#if defined(MBEDTLS_USE_PSA_CRYPTO)
7599 sha384_psa = psa_hash_operation_init();
7600
7601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7602
7603 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7604 if( status != PSA_SUCCESS )
7605 {
7606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7607 return;
7608 }
7609
7610 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7611 if( status != PSA_SUCCESS )
7612 {
7613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7614 return;
7615 }
7616 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7617#else
7618 mbedtls_sha512_init( &sha512 );
7619
7620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7621
Andrzej Kureka242e832022-08-11 10:03:14 -04007622 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007623
7624 /*
7625 * TLSv1.2:
7626 * hash = PRF( master, finished_label,
7627 * Hash( handshake ) )[0.11]
7628 */
7629
7630#if !defined(MBEDTLS_SHA512_ALT)
7631 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7632 sha512.state, sizeof( sha512.state ) );
7633#endif
7634 mbedtls_sha512_finish( &sha512, padbuf );
7635
7636 mbedtls_sha512_free( &sha512 );
7637#endif
7638
7639 ssl->handshake->tls_prf( session->master, 48, sender,
7640 padbuf, 48, buf, len );
7641
7642 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7643
7644 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7645
7646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7647}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007648#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007649
Jerry Yuaef00152022-02-17 14:27:31 +08007650void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7651{
7652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7653
7654 /*
7655 * Free our handshake params
7656 */
7657 mbedtls_ssl_handshake_free( ssl );
7658 mbedtls_free( ssl->handshake );
7659 ssl->handshake = NULL;
7660
7661 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007662 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007663 */
7664 if( ssl->transform )
7665 {
7666 mbedtls_ssl_transform_free( ssl->transform );
7667 mbedtls_free( ssl->transform );
7668 }
7669 ssl->transform = ssl->transform_negotiate;
7670 ssl->transform_negotiate = NULL;
7671
7672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7673}
7674
Jerry Yu2a9fff52022-02-17 14:28:51 +08007675void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7676{
7677 int resume = ssl->handshake->resume;
7678
7679 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7680
7681#if defined(MBEDTLS_SSL_RENEGOTIATION)
7682 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7683 {
7684 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7685 ssl->renego_records_seen = 0;
7686 }
7687#endif
7688
7689 /*
7690 * Free the previous session and switch in the current one
7691 */
7692 if( ssl->session )
7693 {
7694#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7695 /* RFC 7366 3.1: keep the EtM state */
7696 ssl->session_negotiate->encrypt_then_mac =
7697 ssl->session->encrypt_then_mac;
7698#endif
7699
7700 mbedtls_ssl_session_free( ssl->session );
7701 mbedtls_free( ssl->session );
7702 }
7703 ssl->session = ssl->session_negotiate;
7704 ssl->session_negotiate = NULL;
7705
7706 /*
7707 * Add cache entry
7708 */
7709 if( ssl->conf->f_set_cache != NULL &&
7710 ssl->session->id_len != 0 &&
7711 resume == 0 )
7712 {
7713 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7714 ssl->session->id,
7715 ssl->session->id_len,
7716 ssl->session ) != 0 )
7717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7718 }
7719
7720#if defined(MBEDTLS_SSL_PROTO_DTLS)
7721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7722 ssl->handshake->flight != NULL )
7723 {
7724 /* Cancel handshake timer */
7725 mbedtls_ssl_set_timer( ssl, 0 );
7726
7727 /* Keep last flight around in case we need to resend it:
7728 * we need the handshake and transform structures for that */
7729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7730 }
7731 else
7732#endif
7733 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7734
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007735 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007736
7737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7738}
7739
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007740int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7741{
7742 int ret, hash_len;
7743
7744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7745
7746 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7747
7748 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7749
7750 /*
7751 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7752 * may define some other value. Currently (early 2016), no defined
7753 * ciphersuite does this (and this is unlikely to change as activity has
7754 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7755 */
7756 hash_len = 12;
7757
7758#if defined(MBEDTLS_SSL_RENEGOTIATION)
7759 ssl->verify_data_len = hash_len;
7760 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7761#endif
7762
7763 ssl->out_msglen = 4 + hash_len;
7764 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7765 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7766
7767 /*
7768 * In case of session resuming, invert the client and server
7769 * ChangeCipherSpec messages order.
7770 */
7771 if( ssl->handshake->resume != 0 )
7772 {
7773#if defined(MBEDTLS_SSL_CLI_C)
7774 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7775 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7776#endif
7777#if defined(MBEDTLS_SSL_SRV_C)
7778 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7779 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7780#endif
7781 }
7782 else
7783 ssl->state++;
7784
7785 /*
7786 * Switch to our negotiated transform and session parameters for outbound
7787 * data.
7788 */
7789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7790
7791#if defined(MBEDTLS_SSL_PROTO_DTLS)
7792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7793 {
7794 unsigned char i;
7795
7796 /* Remember current epoch settings for resending */
7797 ssl->handshake->alt_transform_out = ssl->transform_out;
7798 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7799 sizeof( ssl->handshake->alt_out_ctr ) );
7800
7801 /* Set sequence_number to zero */
7802 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7803
7804
7805 /* Increment epoch */
7806 for( i = 2; i > 0; i-- )
7807 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7808 break;
7809
7810 /* The loop goes to its end iff the counter is wrapping */
7811 if( i == 0 )
7812 {
7813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7814 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7815 }
7816 }
7817 else
7818#endif /* MBEDTLS_SSL_PROTO_DTLS */
7819 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7820
7821 ssl->transform_out = ssl->transform_negotiate;
7822 ssl->session_out = ssl->session_negotiate;
7823
7824#if defined(MBEDTLS_SSL_PROTO_DTLS)
7825 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7826 mbedtls_ssl_send_flight_completed( ssl );
7827#endif
7828
7829 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7830 {
7831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7832 return( ret );
7833 }
7834
7835#if defined(MBEDTLS_SSL_PROTO_DTLS)
7836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7837 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7838 {
7839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7840 return( ret );
7841 }
7842#endif
7843
7844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7845
7846 return( 0 );
7847}
7848
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007849#define SSL_MAX_HASH_LEN 12
7850
7851int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7852{
7853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7854 unsigned int hash_len = 12;
7855 unsigned char buf[SSL_MAX_HASH_LEN];
7856
7857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7858
7859 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7860
7861 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7862 {
7863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7864 goto exit;
7865 }
7866
7867 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7868 {
7869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7870 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7871 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7872 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7873 goto exit;
7874 }
7875
7876 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7877 {
7878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7879 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7880 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7881 goto exit;
7882 }
7883
7884 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7885 {
7886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7887 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7888 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7889 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7890 goto exit;
7891 }
7892
7893 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7894 buf, hash_len ) != 0 )
7895 {
7896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7897 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7898 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7899 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7900 goto exit;
7901 }
7902
7903#if defined(MBEDTLS_SSL_RENEGOTIATION)
7904 ssl->verify_data_len = hash_len;
7905 memcpy( ssl->peer_verify_data, buf, hash_len );
7906#endif
7907
7908 if( ssl->handshake->resume != 0 )
7909 {
7910#if defined(MBEDTLS_SSL_CLI_C)
7911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7912 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7913#endif
7914#if defined(MBEDTLS_SSL_SRV_C)
7915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7916 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7917#endif
7918 }
7919 else
7920 ssl->state++;
7921
7922#if defined(MBEDTLS_SSL_PROTO_DTLS)
7923 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7924 mbedtls_ssl_recv_flight_completed( ssl );
7925#endif
7926
7927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7928
7929exit:
7930 mbedtls_platform_zeroize( buf, hash_len );
7931 return( ret );
7932}
7933
Jerry Yu392112c2022-02-17 14:34:10 +08007934#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7935/*
7936 * Helper to get TLS 1.2 PRF from ciphersuite
7937 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7938 */
7939static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7940{
Jerry Yu392112c2022-02-17 14:34:10 +08007941 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007942 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7943#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007944 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007945 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007946 else
Jerry Yu392112c2022-02-17 14:34:10 +08007947#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007948#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7949 {
7950 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7951 return( tls_prf_sha256 );
7952 }
7953#endif
7954#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7955 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7956 (void) ciphersuite_info;
7957#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007958
Andrzej Kurek894edde2022-09-29 06:31:14 -04007959 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007960}
7961#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007962
7963static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7964{
7965 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007966#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007967 if( tls_prf == tls_prf_sha384 )
7968 {
7969 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7970 }
7971 else
7972#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007973#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007974 if( tls_prf == tls_prf_sha256 )
7975 {
7976 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7977 }
7978 else
7979#endif
7980 return( MBEDTLS_SSL_TLS_PRF_NONE );
7981}
7982
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007983/*
7984 * Populate a transform structure with session keys and all the other
7985 * necessary information.
7986 *
7987 * Parameters:
7988 * - [in/out]: transform: structure to populate
7989 * [in] must be just initialised with mbedtls_ssl_transform_init()
7990 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7991 * - [in] ciphersuite
7992 * - [in] master
7993 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007994 * - [in] tls_prf: pointer to PRF to use for key derivation
7995 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007996 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007997 * - [in] endpoint: client or server
7998 * - [in] ssl: used for:
7999 * - ssl->conf->{f,p}_export_keys
8000 * [in] optionally used for:
8001 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8002 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008003MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008004static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
8005 int ciphersuite,
8006 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008007#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008008 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008009#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008010 ssl_tls_prf_t tls_prf,
8011 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04008012 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008013 unsigned endpoint,
8014 const mbedtls_ssl_context *ssl )
8015{
8016 int ret = 0;
8017 unsigned char keyblk[256];
8018 unsigned char *key1;
8019 unsigned char *key2;
8020 unsigned char *mac_enc;
8021 unsigned char *mac_dec;
8022 size_t mac_key_len = 0;
8023 size_t iv_copy_len;
8024 size_t keylen;
8025 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008026 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008027#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008028 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008029 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008030#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008031
8032#if defined(MBEDTLS_USE_PSA_CRYPTO)
8033 psa_key_type_t key_type;
8034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8035 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008036 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008037 size_t key_bits;
8038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8039#endif
8040
8041#if !defined(MBEDTLS_DEBUG_C) && \
8042 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8043 if( ssl->f_export_keys == NULL )
8044 {
8045 ssl = NULL; /* make sure we don't use it except for these cases */
8046 (void) ssl;
8047 }
8048#endif
8049
8050 /*
8051 * Some data just needs copying into the structure
8052 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008053#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008054 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008055#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008056 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008057
8058#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8059 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8060#endif
8061
8062#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008063 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008064 {
8065 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8066 * generation separate. This should never happen. */
8067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8068 }
8069#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8070
8071 /*
8072 * Get various info structures
8073 */
8074 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8075 if( ciphersuite_info == NULL )
8076 {
8077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8078 ciphersuite ) );
8079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8080 }
8081
Neil Armstrongab555e02022-04-04 11:07:59 +02008082 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008083#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008084 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008085#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008086 ciphersuite_info );
8087
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008088 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8089 transform->taglen =
8090 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8091
8092#if defined(MBEDTLS_USE_PSA_CRYPTO)
8093 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8094 transform->taglen,
8095 &alg,
8096 &key_type,
8097 &key_bits ) ) != PSA_SUCCESS )
8098 {
8099 ret = psa_ssl_status_to_mbedtls( status );
8100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8101 goto end;
8102 }
8103#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008104 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8105 if( cipher_info == NULL )
8106 {
8107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8108 ciphersuite_info->cipher ) );
8109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8110 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008111#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008112
Neil Armstronge4512952022-03-08 09:08:22 +01008113#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008114 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008115 if( mac_alg == 0 )
8116 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008118 (unsigned) ciphersuite_info->mac ) );
8119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8120 }
8121#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008122 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8123 if( md_info == NULL )
8124 {
8125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8126 (unsigned) ciphersuite_info->mac ) );
8127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8128 }
Neil Armstronge4512952022-03-08 09:08:22 +01008129#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008130
8131#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8132 /* Copy own and peer's CID if the use of the CID
8133 * extension has been negotiated. */
8134 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8135 {
8136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8137
8138 transform->in_cid_len = ssl->own_cid_len;
8139 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8140 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8141 transform->in_cid_len );
8142
8143 transform->out_cid_len = ssl->handshake->peer_cid_len;
8144 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8145 ssl->handshake->peer_cid_len );
8146 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8147 transform->out_cid_len );
8148 }
8149#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8150
8151 /*
8152 * Compute key block using the PRF
8153 */
8154 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8155 if( ret != 0 )
8156 {
8157 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8158 return( ret );
8159 }
8160
8161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8162 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8163 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8164 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8165 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8166
8167 /*
8168 * Determine the appropriate key, IV and MAC length.
8169 */
8170
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008171#if defined(MBEDTLS_USE_PSA_CRYPTO)
8172 keylen = PSA_BITS_TO_BYTES(key_bits);
8173#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008174 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008175#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008176
8177#if defined(MBEDTLS_GCM_C) || \
8178 defined(MBEDTLS_CCM_C) || \
8179 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008180 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008181 {
8182 size_t explicit_ivlen;
8183
8184 transform->maclen = 0;
8185 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008186
8187 /* All modes haves 96-bit IVs, but the length of the static parts vary
8188 * with mode and version:
8189 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8190 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8191 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8192 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8193 * sequence number).
8194 */
8195 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008196
8197 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008198#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008199 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008200#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008201 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8202 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008203#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008204
8205 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008206 transform->fixed_ivlen = 12;
8207 else
8208 transform->fixed_ivlen = 4;
8209
8210 /* Minimum length of encrypted record */
8211 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8212 transform->minlen = explicit_ivlen + transform->taglen;
8213 }
8214 else
8215#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8216#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008217 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8218 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8219 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008220 {
Neil Armstronge4512952022-03-08 09:08:22 +01008221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008222 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008223#else
8224 size_t block_size = cipher_info->block_size;
8225#endif /* MBEDTLS_USE_PSA_CRYPTO */
8226
8227#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008228 /* Get MAC length */
8229 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8230#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008231 /* Initialize HMAC contexts */
8232 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8233 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8234 {
8235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8236 goto end;
8237 }
8238
8239 /* Get MAC length */
8240 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008241#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008242 transform->maclen = mac_key_len;
8243
8244 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008245#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008246 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008247#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008248 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008249#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008250
8251 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008252 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008253 transform->minlen = transform->maclen;
8254 else
8255 {
8256 /*
8257 * GenericBlockCipher:
8258 * 1. if EtM is in use: one block plus MAC
8259 * otherwise: * first multiple of blocklen greater than maclen
8260 * 2. IV
8261 */
8262#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008263 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008264 {
8265 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008266 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008267 }
8268 else
8269#endif
8270 {
8271 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008272 + block_size
8273 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008274 }
8275
Glenn Strauss07c64162022-03-14 12:34:51 -04008276 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008277 {
8278 transform->minlen += transform->ivlen;
8279 }
8280 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008281 {
8282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8283 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8284 goto end;
8285 }
8286 }
8287 }
8288 else
8289#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8290 {
8291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8293 }
8294
8295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8296 (unsigned) keylen,
8297 (unsigned) transform->minlen,
8298 (unsigned) transform->ivlen,
8299 (unsigned) transform->maclen ) );
8300
8301 /*
8302 * Finally setup the cipher contexts, IVs and MAC secrets.
8303 */
8304#if defined(MBEDTLS_SSL_CLI_C)
8305 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8306 {
8307 key1 = keyblk + mac_key_len * 2;
8308 key2 = keyblk + mac_key_len * 2 + keylen;
8309
8310 mac_enc = keyblk;
8311 mac_dec = keyblk + mac_key_len;
8312
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008313 iv_copy_len = ( transform->fixed_ivlen ) ?
8314 transform->fixed_ivlen : transform->ivlen;
8315 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8316 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8317 iv_copy_len );
8318 }
8319 else
8320#endif /* MBEDTLS_SSL_CLI_C */
8321#if defined(MBEDTLS_SSL_SRV_C)
8322 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8323 {
8324 key1 = keyblk + mac_key_len * 2 + keylen;
8325 key2 = keyblk + mac_key_len * 2;
8326
8327 mac_enc = keyblk + mac_key_len;
8328 mac_dec = keyblk;
8329
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008330 iv_copy_len = ( transform->fixed_ivlen ) ?
8331 transform->fixed_ivlen : transform->ivlen;
8332 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8333 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8334 iv_copy_len );
8335 }
8336 else
8337#endif /* MBEDTLS_SSL_SRV_C */
8338 {
8339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8340 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8341 goto end;
8342 }
8343
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008344 if( ssl != NULL && ssl->f_export_keys != NULL )
8345 {
8346 ssl->f_export_keys( ssl->p_export_keys,
8347 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8348 master, 48,
8349 randbytes + 32,
8350 randbytes,
8351 tls_prf_get_type( tls_prf ) );
8352 }
8353
8354#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008355 transform->psa_alg = alg;
8356
8357 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8358 {
8359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8360 psa_set_key_algorithm( &attributes, alg );
8361 psa_set_key_type( &attributes, key_type );
8362
8363 if( ( status = psa_import_key( &attributes,
8364 key1,
8365 PSA_BITS_TO_BYTES( key_bits ),
8366 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8367 {
8368 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8369 ret = psa_ssl_status_to_mbedtls( status );
8370 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8371 goto end;
8372 }
8373
8374 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8375
8376 if( ( status = psa_import_key( &attributes,
8377 key2,
8378 PSA_BITS_TO_BYTES( key_bits ),
8379 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8380 {
8381 ret = psa_ssl_status_to_mbedtls( status );
8382 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8383 goto end;
8384 }
8385 }
8386#else
8387 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8388 cipher_info ) ) != 0 )
8389 {
8390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8391 goto end;
8392 }
8393
8394 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8395 cipher_info ) ) != 0 )
8396 {
8397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8398 goto end;
8399 }
8400
8401 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8402 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8403 MBEDTLS_ENCRYPT ) ) != 0 )
8404 {
8405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8406 goto end;
8407 }
8408
8409 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8410 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8411 MBEDTLS_DECRYPT ) ) != 0 )
8412 {
8413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8414 goto end;
8415 }
8416
8417#if defined(MBEDTLS_CIPHER_MODE_CBC)
8418 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8419 {
8420 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8421 MBEDTLS_PADDING_NONE ) ) != 0 )
8422 {
8423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8424 goto end;
8425 }
8426
8427 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8428 MBEDTLS_PADDING_NONE ) ) != 0 )
8429 {
8430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8431 goto end;
8432 }
8433 }
8434#endif /* MBEDTLS_CIPHER_MODE_CBC */
8435#endif /* MBEDTLS_USE_PSA_CRYPTO */
8436
Neil Armstrong29c0c042022-03-17 17:47:28 +01008437#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8438 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8439 For AEAD-based ciphersuites, there is nothing to do here. */
8440 if( mac_key_len != 0 )
8441 {
8442#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008443 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008444
8445 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008446 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008447 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8448
8449 if( ( status = psa_import_key( &attributes,
8450 mac_enc, mac_key_len,
8451 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8452 {
8453 ret = psa_ssl_status_to_mbedtls( status );
8454 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8455 goto end;
8456 }
8457
Ronald Cronfb39f152022-03-25 14:36:28 +01008458 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008459 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8460#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8461 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8462#endif
8463 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008464 /* mbedtls_ct_hmac() requires the key to be exportable */
8465 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8466 PSA_KEY_USAGE_VERIFY_HASH );
8467 else
8468 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8469
8470 if( ( status = psa_import_key( &attributes,
8471 mac_dec, mac_key_len,
8472 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8473 {
8474 ret = psa_ssl_status_to_mbedtls( status );
8475 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8476 goto end;
8477 }
8478#else
8479 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8480 if( ret != 0 )
8481 goto end;
8482 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8483 if( ret != 0 )
8484 goto end;
8485#endif /* MBEDTLS_USE_PSA_CRYPTO */
8486 }
8487#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8488
8489 ((void) mac_dec);
8490 ((void) mac_enc);
8491
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008492end:
8493 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8494 return( ret );
8495}
8496
Valerio Settia08b1a42022-11-17 15:10:02 +01008497#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8498 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008499int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008500 psa_pake_operation_t *pake_ctx,
8501 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008502 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008503{
8504 psa_status_t status;
8505 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008506 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008507 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8508 * At round two perform a single cycle
8509 */
8510 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008511
Valerio Setti6b3dab02022-11-17 17:14:54 +01008512 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008513 {
8514 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8515 step <= PSA_PAKE_STEP_ZK_PROOF;
8516 ++step )
8517 {
8518 /* Length is stored at the first byte */
8519 size_t length = buf[input_offset];
8520 input_offset += 1;
8521
8522 if( input_offset + length > len )
8523 {
8524 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8525 }
8526
8527 status = psa_pake_input( pake_ctx, step,
8528 buf + input_offset, length );
8529 if( status != PSA_SUCCESS)
8530 {
8531 return psa_ssl_status_to_mbedtls( status );
8532 }
8533
8534 input_offset += length;
8535 }
8536 }
8537
Valerio Setti819de862022-11-17 18:05:19 +01008538 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008539 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008540
Valerio Settia08b1a42022-11-17 15:10:02 +01008541 return( 0 );
8542}
8543
Valerio Setti6b3dab02022-11-17 17:14:54 +01008544int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008545 psa_pake_operation_t *pake_ctx,
8546 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008547 size_t len, size_t *olen,
8548 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008549{
8550 psa_status_t status;
8551 size_t output_offset = 0;
8552 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008553 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008554 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8555 * At round two perform a single cycle
8556 */
8557 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008558
Valerio Setti6b3dab02022-11-17 17:14:54 +01008559 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008560 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008561 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8562 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008563 ++step )
8564 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008565 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008566 * For each step, prepend 1 byte with the length of the data as
8567 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008568 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008569 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008570 buf + output_offset + 1,
8571 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008572 &output_len );
8573 if( status != PSA_SUCCESS )
8574 {
8575 return( psa_ssl_status_to_mbedtls( status ) );
8576 }
8577
Valerio Setti99d88c12022-11-22 16:03:43 +01008578 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008579
8580 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008581 }
8582 }
8583
8584 *olen = output_offset;
8585
8586 return( 0 );
8587}
Valerio Settia08b1a42022-11-17 15:10:02 +01008588#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8589
Jerry Yuee40f9d2022-02-17 14:55:16 +08008590#if defined(MBEDTLS_USE_PSA_CRYPTO)
8591int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8592 unsigned char *hash, size_t *hashlen,
8593 unsigned char *data, size_t data_len,
8594 mbedtls_md_type_t md_alg )
8595{
8596 psa_status_t status;
8597 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008598 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008599
8600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8601
8602 if( ( status = psa_hash_setup( &hash_operation,
8603 hash_alg ) ) != PSA_SUCCESS )
8604 {
8605 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8606 goto exit;
8607 }
8608
8609 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8610 64 ) ) != PSA_SUCCESS )
8611 {
8612 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8613 goto exit;
8614 }
8615
8616 if( ( status = psa_hash_update( &hash_operation,
8617 data, data_len ) ) != PSA_SUCCESS )
8618 {
8619 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8620 goto exit;
8621 }
8622
8623 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8624 hashlen ) ) != PSA_SUCCESS )
8625 {
8626 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8627 goto exit;
8628 }
8629
8630exit:
8631 if( status != PSA_SUCCESS )
8632 {
8633 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8634 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8635 switch( status )
8636 {
8637 case PSA_ERROR_NOT_SUPPORTED:
8638 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8639 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8640 case PSA_ERROR_BUFFER_TOO_SMALL:
8641 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8642 case PSA_ERROR_INSUFFICIENT_MEMORY:
8643 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8644 default:
8645 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8646 }
8647 }
8648 return( 0 );
8649}
8650
8651#else
8652
8653int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8654 unsigned char *hash, size_t *hashlen,
8655 unsigned char *data, size_t data_len,
8656 mbedtls_md_type_t md_alg )
8657{
8658 int ret = 0;
8659 mbedtls_md_context_t ctx;
8660 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8661 *hashlen = mbedtls_md_get_size( md_info );
8662
8663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8664
8665 mbedtls_md_init( &ctx );
8666
8667 /*
8668 * digitally-signed struct {
8669 * opaque client_random[32];
8670 * opaque server_random[32];
8671 * ServerDHParams params;
8672 * };
8673 */
8674 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8675 {
8676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8677 goto exit;
8678 }
8679 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8680 {
8681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8682 goto exit;
8683 }
8684 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8685 {
8686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8687 goto exit;
8688 }
8689 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8690 {
8691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8692 goto exit;
8693 }
8694 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8695 {
8696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8697 goto exit;
8698 }
8699
8700exit:
8701 mbedtls_md_free( &ctx );
8702
8703 if( ret != 0 )
8704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8705 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8706
8707 return( ret );
8708}
8709#endif /* MBEDTLS_USE_PSA_CRYPTO */
8710
Jerry Yud9d91da2022-02-17 14:57:06 +08008711#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8712
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008713/* Find the preferred hash for a given signature algorithm. */
8714unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8715 mbedtls_ssl_context *ssl,
8716 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008717{
Gabor Mezei078e8032022-04-27 21:17:56 +02008718 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008719 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008720
8721 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008722 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008723
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008724 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008725 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008726 unsigned int hash_alg_received =
8727 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8728 received_sig_algs[i] );
8729 unsigned int sig_alg_received =
8730 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8731 received_sig_algs[i] );
8732
8733 if( sig_alg == sig_alg_received )
8734 {
8735#if defined(MBEDTLS_USE_PSA_CRYPTO)
8736 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8737 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008738 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008739 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008740
Neil Armstrong96eceb82022-06-30 18:05:05 +02008741 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8742 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8743 PSA_ALG_ECDSA( psa_hash_alg ),
8744 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008745 continue;
8746
Neil Armstrong96eceb82022-06-30 18:05:05 +02008747 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008748 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8749 PSA_ALG_RSA_PKCS1V15_SIGN(
8750 psa_hash_alg ),
8751 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008752 continue;
8753 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008754#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008755
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008756 return( hash_alg_received );
8757 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008758 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008759
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008760 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008761}
8762
8763#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8764
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008765/* Serialization of TLS 1.2 sessions:
8766 *
8767 * struct {
8768 * uint64 start_time;
8769 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008770 * uint8 session_id_len; // at most 32
8771 * opaque session_id[32];
8772 * opaque master[48]; // fixed length in the standard
8773 * uint32 verify_result;
8774 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8775 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8776 * uint32 ticket_lifetime;
8777 * uint8 mfl_code; // up to 255 according to standard
8778 * uint8 encrypt_then_mac; // 0 or 1
8779 * } serialized_session_tls12;
8780 *
8781 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008782static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008783 unsigned char *buf,
8784 size_t buf_len )
8785{
8786 unsigned char *p = buf;
8787 size_t used = 0;
8788
8789#if defined(MBEDTLS_HAVE_TIME)
8790 uint64_t start;
8791#endif
8792#if defined(MBEDTLS_X509_CRT_PARSE_C)
8793#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8794 size_t cert_len;
8795#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8796#endif /* MBEDTLS_X509_CRT_PARSE_C */
8797
8798 /*
8799 * Time
8800 */
8801#if defined(MBEDTLS_HAVE_TIME)
8802 used += 8;
8803
8804 if( used <= buf_len )
8805 {
8806 start = (uint64_t) session->start;
8807
8808 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8809 p += 8;
8810 }
8811#endif /* MBEDTLS_HAVE_TIME */
8812
8813 /*
8814 * Basic mandatory fields
8815 */
8816 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008817 + 1 /* id_len */
8818 + sizeof( session->id )
8819 + sizeof( session->master )
8820 + 4; /* verify_result */
8821
8822 if( used <= buf_len )
8823 {
8824 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8825 p += 2;
8826
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008827 *p++ = MBEDTLS_BYTE_0( session->id_len );
8828 memcpy( p, session->id, 32 );
8829 p += 32;
8830
8831 memcpy( p, session->master, 48 );
8832 p += 48;
8833
8834 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8835 p += 4;
8836 }
8837
8838 /*
8839 * Peer's end-entity certificate
8840 */
8841#if defined(MBEDTLS_X509_CRT_PARSE_C)
8842#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8843 if( session->peer_cert == NULL )
8844 cert_len = 0;
8845 else
8846 cert_len = session->peer_cert->raw.len;
8847
8848 used += 3 + cert_len;
8849
8850 if( used <= buf_len )
8851 {
8852 *p++ = MBEDTLS_BYTE_2( cert_len );
8853 *p++ = MBEDTLS_BYTE_1( cert_len );
8854 *p++ = MBEDTLS_BYTE_0( cert_len );
8855
8856 if( session->peer_cert != NULL )
8857 {
8858 memcpy( p, session->peer_cert->raw.p, cert_len );
8859 p += cert_len;
8860 }
8861 }
8862#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8863 if( session->peer_cert_digest != NULL )
8864 {
8865 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8866 if( used <= buf_len )
8867 {
8868 *p++ = (unsigned char) session->peer_cert_digest_type;
8869 *p++ = (unsigned char) session->peer_cert_digest_len;
8870 memcpy( p, session->peer_cert_digest,
8871 session->peer_cert_digest_len );
8872 p += session->peer_cert_digest_len;
8873 }
8874 }
8875 else
8876 {
8877 used += 2;
8878 if( used <= buf_len )
8879 {
8880 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8881 *p++ = 0;
8882 }
8883 }
8884#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8885#endif /* MBEDTLS_X509_CRT_PARSE_C */
8886
8887 /*
8888 * Session ticket if any, plus associated data
8889 */
8890#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8891 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8892
8893 if( used <= buf_len )
8894 {
8895 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8896 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8897 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8898
8899 if( session->ticket != NULL )
8900 {
8901 memcpy( p, session->ticket, session->ticket_len );
8902 p += session->ticket_len;
8903 }
8904
8905 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8906 p += 4;
8907 }
8908#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8909
8910 /*
8911 * Misc extension-related info
8912 */
8913#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8914 used += 1;
8915
8916 if( used <= buf_len )
8917 *p++ = session->mfl_code;
8918#endif
8919
8920#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8921 used += 1;
8922
8923 if( used <= buf_len )
8924 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8925#endif
8926
8927 return( used );
8928}
8929
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008930MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008931static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008932 const unsigned char *buf,
8933 size_t len )
8934{
8935#if defined(MBEDTLS_HAVE_TIME)
8936 uint64_t start;
8937#endif
8938#if defined(MBEDTLS_X509_CRT_PARSE_C)
8939#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8940 size_t cert_len;
8941#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8942#endif /* MBEDTLS_X509_CRT_PARSE_C */
8943
8944 const unsigned char *p = buf;
8945 const unsigned char * const end = buf + len;
8946
8947 /*
8948 * Time
8949 */
8950#if defined(MBEDTLS_HAVE_TIME)
8951 if( 8 > (size_t)( end - p ) )
8952 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8953
8954 start = ( (uint64_t) p[0] << 56 ) |
8955 ( (uint64_t) p[1] << 48 ) |
8956 ( (uint64_t) p[2] << 40 ) |
8957 ( (uint64_t) p[3] << 32 ) |
8958 ( (uint64_t) p[4] << 24 ) |
8959 ( (uint64_t) p[5] << 16 ) |
8960 ( (uint64_t) p[6] << 8 ) |
8961 ( (uint64_t) p[7] );
8962 p += 8;
8963
8964 session->start = (time_t) start;
8965#endif /* MBEDTLS_HAVE_TIME */
8966
8967 /*
8968 * Basic mandatory fields
8969 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008970 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8972
8973 session->ciphersuite = ( p[0] << 8 ) | p[1];
8974 p += 2;
8975
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008976 session->id_len = *p++;
8977 memcpy( session->id, p, 32 );
8978 p += 32;
8979
8980 memcpy( session->master, p, 48 );
8981 p += 48;
8982
8983 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8984 ( (uint32_t) p[1] << 16 ) |
8985 ( (uint32_t) p[2] << 8 ) |
8986 ( (uint32_t) p[3] );
8987 p += 4;
8988
8989 /* Immediately clear invalid pointer values that have been read, in case
8990 * we exit early before we replaced them with valid ones. */
8991#if defined(MBEDTLS_X509_CRT_PARSE_C)
8992#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8993 session->peer_cert = NULL;
8994#else
8995 session->peer_cert_digest = NULL;
8996#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8997#endif /* MBEDTLS_X509_CRT_PARSE_C */
8998#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8999 session->ticket = NULL;
9000#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9001
9002 /*
9003 * Peer certificate
9004 */
9005#if defined(MBEDTLS_X509_CRT_PARSE_C)
9006#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9007 /* Deserialize CRT from the end of the ticket. */
9008 if( 3 > (size_t)( end - p ) )
9009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9010
9011 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9012 p += 3;
9013
9014 if( cert_len != 0 )
9015 {
9016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9017
9018 if( cert_len > (size_t)( end - p ) )
9019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9020
9021 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9022
9023 if( session->peer_cert == NULL )
9024 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9025
9026 mbedtls_x509_crt_init( session->peer_cert );
9027
9028 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9029 p, cert_len ) ) != 0 )
9030 {
9031 mbedtls_x509_crt_free( session->peer_cert );
9032 mbedtls_free( session->peer_cert );
9033 session->peer_cert = NULL;
9034 return( ret );
9035 }
9036
9037 p += cert_len;
9038 }
9039#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9040 /* Deserialize CRT digest from the end of the ticket. */
9041 if( 2 > (size_t)( end - p ) )
9042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9043
9044 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9045 session->peer_cert_digest_len = (size_t) *p++;
9046
9047 if( session->peer_cert_digest_len != 0 )
9048 {
9049 const mbedtls_md_info_t *md_info =
9050 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9051 if( md_info == NULL )
9052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9053 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9055
9056 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9058
9059 session->peer_cert_digest =
9060 mbedtls_calloc( 1, session->peer_cert_digest_len );
9061 if( session->peer_cert_digest == NULL )
9062 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9063
9064 memcpy( session->peer_cert_digest, p,
9065 session->peer_cert_digest_len );
9066 p += session->peer_cert_digest_len;
9067 }
9068#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9069#endif /* MBEDTLS_X509_CRT_PARSE_C */
9070
9071 /*
9072 * Session ticket and associated data
9073 */
9074#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9075 if( 3 > (size_t)( end - p ) )
9076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9077
9078 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9079 p += 3;
9080
9081 if( session->ticket_len != 0 )
9082 {
9083 if( session->ticket_len > (size_t)( end - p ) )
9084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9085
9086 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9087 if( session->ticket == NULL )
9088 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9089
9090 memcpy( session->ticket, p, session->ticket_len );
9091 p += session->ticket_len;
9092 }
9093
9094 if( 4 > (size_t)( end - p ) )
9095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9096
9097 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9098 ( (uint32_t) p[1] << 16 ) |
9099 ( (uint32_t) p[2] << 8 ) |
9100 ( (uint32_t) p[3] );
9101 p += 4;
9102#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9103
9104 /*
9105 * Misc extension-related info
9106 */
9107#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9108 if( 1 > (size_t)( end - p ) )
9109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9110
9111 session->mfl_code = *p++;
9112#endif
9113
9114#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9115 if( 1 > (size_t)( end - p ) )
9116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9117
9118 session->encrypt_then_mac = *p++;
9119#endif
9120
9121 /* Done, should have consumed entire buffer */
9122 if( p != end )
9123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9124
9125 return( 0 );
9126}
Jerry Yudc7bd172022-02-17 13:44:15 +08009127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9128
XiaokangQian75d40ef2022-04-20 11:05:24 +00009129int mbedtls_ssl_validate_ciphersuite(
9130 const mbedtls_ssl_context *ssl,
9131 const mbedtls_ssl_ciphersuite_t *suite_info,
9132 mbedtls_ssl_protocol_version min_tls_version,
9133 mbedtls_ssl_protocol_version max_tls_version )
9134{
9135 (void) ssl;
9136
9137 if( suite_info == NULL )
9138 return( -1 );
9139
9140 if( ( suite_info->min_tls_version > max_tls_version ) ||
9141 ( suite_info->max_tls_version < min_tls_version ) )
9142 {
9143 return( -1 );
9144 }
9145
XiaokangQian060d8672022-04-21 09:24:56 +00009146#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009147#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009148#if defined(MBEDTLS_USE_PSA_CRYPTO)
9149 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9150 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9151#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009152 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9153 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009154#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009155 {
9156 return( -1 );
9157 }
9158#endif
9159
9160 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9161#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9162 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9163 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9164 {
9165 return( -1 );
9166 }
9167#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9168#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9169
9170 return( 0 );
9171}
9172
Ronald Crone68ab4f2022-10-05 12:46:29 +02009173#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009174/*
9175 * Function for writing a signature algorithm extension.
9176 *
9177 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9178 * value (TLS 1.3 RFC8446):
9179 * enum {
9180 * ....
9181 * ecdsa_secp256r1_sha256( 0x0403 ),
9182 * ecdsa_secp384r1_sha384( 0x0503 ),
9183 * ecdsa_secp521r1_sha512( 0x0603 ),
9184 * ....
9185 * } SignatureScheme;
9186 *
9187 * struct {
9188 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9189 * } SignatureSchemeList;
9190 *
9191 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9192 * value (TLS 1.2 RFC5246):
9193 * enum {
9194 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9195 * sha512(6), (255)
9196 * } HashAlgorithm;
9197 *
9198 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9199 * SignatureAlgorithm;
9200 *
9201 * struct {
9202 * HashAlgorithm hash;
9203 * SignatureAlgorithm signature;
9204 * } SignatureAndHashAlgorithm;
9205 *
9206 * SignatureAndHashAlgorithm
9207 * supported_signature_algorithms<2..2^16-2>;
9208 *
9209 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9210 * generalization of the TLS 1.2 signature algorithm extension.
9211 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9212 * `SignatureScheme` field of TLS 1.3
9213 *
9214 */
9215int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9216 const unsigned char *end, size_t *out_len )
9217{
9218 unsigned char *p = buf;
9219 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9220 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9221
9222 *out_len = 0;
9223
9224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9225
9226 /* Check if we have space for header and length field:
9227 * - extension_type (2 bytes)
9228 * - extension_data_length (2 bytes)
9229 * - supported_signature_algorithms_length (2 bytes)
9230 */
9231 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9232 p += 6;
9233
9234 /*
9235 * Write supported_signature_algorithms
9236 */
9237 supported_sig_alg = p;
9238 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9239 if( sig_alg == NULL )
9240 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9241
9242 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9243 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9245 *sig_alg,
9246 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009247 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9248 continue;
9249 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9250 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9251 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009253 *sig_alg,
9254 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009255 }
9256
9257 /* Length of supported_signature_algorithms */
9258 supported_sig_alg_len = p - supported_sig_alg;
9259 if( supported_sig_alg_len == 0 )
9260 {
9261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9263 }
9264
XiaokangQianeaf36512022-04-24 09:07:44 +00009265 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009266 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009267 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9268
XiaokangQianeaf36512022-04-24 09:07:44 +00009269 *out_len = p - buf;
9270
9271#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009272 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009273#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009274
XiaokangQianeaf36512022-04-24 09:07:44 +00009275 return( 0 );
9276}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009277#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009278
XiaokangQian40a35232022-05-07 09:02:40 +00009279#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009280/*
9281 * mbedtls_ssl_parse_server_name_ext
9282 *
9283 * Structure of server_name extension:
9284 *
9285 * enum {
9286 * host_name(0), (255)
9287 * } NameType;
9288 * opaque HostName<1..2^16-1>;
9289 *
9290 * struct {
9291 * NameType name_type;
9292 * select (name_type) {
9293 * case host_name: HostName;
9294 * } name;
9295 * } ServerName;
9296 * struct {
9297 * ServerName server_name_list<1..2^16-1>
9298 * } ServerNameList;
9299 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009300MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009301int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9302 const unsigned char *buf,
9303 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009304{
9305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9306 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009307 size_t server_name_list_len, hostname_len;
9308 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009309
XiaokangQianf2a94202022-05-20 06:44:24 +00009310 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009311
9312 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009313 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009314 p += 2;
9315
XiaokangQian9b2b7712022-05-17 02:57:00 +00009316 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9317 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009318 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009319 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009320 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009321 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009322 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9323 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009324
9325 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9326 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009327 /* sni_name is intended to be used only during the parsing of the
9328 * ClientHello message (it is reset to NULL before the end of
9329 * the message parsing). Thus it is ok to just point to the
9330 * reception buffer and not make a copy of it.
9331 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009332 ssl->handshake->sni_name = p + 3;
9333 ssl->handshake->sni_name_len = hostname_len;
9334 if( ssl->conf->f_sni == NULL )
9335 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009336 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009337 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009338 if( ret != 0 )
9339 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009340 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009341 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9342 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009343 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9344 }
9345 return( 0 );
9346 }
9347
9348 p += hostname_len + 3;
9349 }
9350
9351 return( 0 );
9352}
9353#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9354
XiaokangQianacb39922022-06-17 10:18:48 +00009355#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009356MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009357int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9358 const unsigned char *buf,
9359 const unsigned char *end )
9360{
9361 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009362 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009363 const unsigned char *protocol_name_list;
9364 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009365 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009366
9367 /* If ALPN not configured, just ignore the extension */
9368 if( ssl->conf->alpn_list == NULL )
9369 return( 0 );
9370
9371 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009372 * RFC7301, section 3.1
9373 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009374 *
XiaokangQianc7403452022-06-23 03:24:12 +00009375 * struct {
9376 * ProtocolName protocol_name_list<2..2^16-1>
9377 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009378 */
9379
XiaokangQianc7403452022-06-23 03:24:12 +00009380 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009381 * protocol_name_list_len 2 bytes
9382 * protocol_name_len 1 bytes
9383 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009384 */
XiaokangQianacb39922022-06-17 10:18:48 +00009385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9386
XiaokangQianc7403452022-06-23 03:24:12 +00009387 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009388 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009389 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009390 protocol_name_list = p;
9391 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009392
9393 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009394 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009395 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009396 protocol_name_len = *p++;
9397 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9398 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009399 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009400 {
9401 MBEDTLS_SSL_PEND_FATAL_ALERT(
9402 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9403 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009404 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009405 }
9406
9407 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009408 }
9409
9410 /* Use our order of preference */
9411 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9412 {
9413 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009414 p = protocol_name_list;
9415 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009416 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009417 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009418 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009419 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009420 {
9421 ssl->alpn_chosen = *alpn;
9422 return( 0 );
9423 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009424
9425 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009426 }
9427 }
9428
XiaokangQian95d5f542022-06-24 02:29:26 +00009429 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009430 MBEDTLS_SSL_PEND_FATAL_ALERT(
9431 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9432 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9433 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9434}
9435
9436int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9437 unsigned char *buf,
9438 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009439 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009440{
9441 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009442 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009443 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009444
9445 if( ssl->alpn_chosen == NULL )
9446 {
9447 return( 0 );
9448 }
9449
XiaokangQian95d5f542022-06-24 02:29:26 +00009450 protocol_name_len = strlen( ssl->alpn_chosen );
9451 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009452
9453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9454 /*
9455 * 0 . 1 ext identifier
9456 * 2 . 3 ext length
9457 * 4 . 5 protocol list length
9458 * 6 . 6 protocol name length
9459 * 7 . 7+n protocol name
9460 */
9461 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9462
XiaokangQian95d5f542022-06-24 02:29:26 +00009463 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009464
XiaokangQian95d5f542022-06-24 02:29:26 +00009465 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9466 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009467 /* Note: the length of the chosen protocol has been checked to be less
9468 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9469 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009470 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009471
XiaokangQian95d5f542022-06-24 02:29:26 +00009472 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009473
9474#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9475 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9476#endif
9477
XiaokangQianacb39922022-06-17 10:18:48 +00009478 return ( 0 );
9479}
9480#endif /* MBEDTLS_SSL_ALPN */
9481
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009482#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009483 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009484 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009485 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009486int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9487 const char *hostname )
9488{
9489 /* Initialize to suppress unnecessary compiler warning */
9490 size_t hostname_len = 0;
9491
9492 /* Check if new hostname is valid before
9493 * making any change to current one */
9494 if( hostname != NULL )
9495 {
9496 hostname_len = strlen( hostname );
9497
9498 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9500 }
9501
9502 /* Now it's clear that we will overwrite the old hostname,
9503 * so we can free it safely */
9504 if( session->hostname != NULL )
9505 {
9506 mbedtls_platform_zeroize( session->hostname,
9507 strlen( session->hostname ) );
9508 mbedtls_free( session->hostname );
9509 }
9510
9511 /* Passing NULL as hostname shall clear the old one */
9512 if( hostname == NULL )
9513 {
9514 session->hostname = NULL;
9515 }
9516 else
9517 {
9518 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9519 if( session->hostname == NULL )
9520 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9521
9522 memcpy( session->hostname, hostname, hostname_len );
9523 }
9524
9525 return( 0 );
9526}
Xiaokang Qian03409292022-10-12 02:49:52 +00009527#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9528 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009529 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009530 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#endif /* MBEDTLS_SSL_TLS_C */