blob: 65af6b62a85fa03c13679434fe936109865ee5bc [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 {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001450#if defined(MBEDTLS_SSL_EARLY_DATA)
XiaokangQian2b01dc32022-01-21 02:53:13 +00001451 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1452 mbedtls_free( ssl->handshake->transform_earlydata );
1453 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001454#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455
1456 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1457 mbedtls_free( ssl->handshake->transform_handshake );
1458 ssl->handshake->transform_handshake = NULL;
1459 }
1460
XiaokangQian2b01dc32022-01-21 02:53:13 +00001461#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001462}
1463
1464int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1465{
1466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1467
1468 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1469
XiaokangQian78b1fa72022-01-19 06:56:30 +00001470 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001471
1472 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_RENEGOTIATION)
1474 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001475 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001476
1477 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1479 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001482
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001484 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001485 if( ssl->session )
1486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 mbedtls_ssl_session_free( ssl->session );
1488 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001489 ssl->session = NULL;
1490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001493 ssl->alpn_chosen = NULL;
1494#endif
1495
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001496#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001497 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001498#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001499 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001500#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001501 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001502 {
1503 mbedtls_free( ssl->cli_id );
1504 ssl->cli_id = NULL;
1505 ssl->cli_id_len = 0;
1506 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001507#endif
1508
Paul Bakker48916f92012-09-16 19:57:18 +00001509 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1510 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001511
1512 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001513}
1514
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001515/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001516 * Reset an initialized and used SSL context for re-use while retaining
1517 * all application-set variables, function pointers and data.
1518 */
1519int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1520{
Hanno Becker43aefe22020-02-05 10:44:56 +00001521 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001522}
1523
1524/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 * SSL set accessors
1526 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001527void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001529 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530}
1531
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001532void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001534 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001538void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001540 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001541}
1542#endif
1543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001544void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001547}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001550
Hanno Becker1841b0a2018-08-24 11:13:57 +01001551void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1552 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001553{
1554 ssl->disable_datagram_packing = !allow_packing;
1555}
1556
1557void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1558 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001559{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001560 conf->hs_timeout_min = min;
1561 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001562}
1563#endif
1564
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001565void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001567 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568}
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001571void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001572 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001573 void *p_vrfy )
1574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001575 conf->f_vrfy = f_vrfy;
1576 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001579
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001580void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001581 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 void *p_rng )
1583{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001584 conf->f_rng = f_rng;
1585 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001586}
1587
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001588void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001589 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 void *p_dbg )
1591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001592 conf->f_dbg = f_dbg;
1593 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001597 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001598 mbedtls_ssl_send_t *f_send,
1599 mbedtls_ssl_recv_t *f_recv,
1600 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001601{
1602 ssl->p_bio = p_bio;
1603 ssl->f_send = f_send;
1604 ssl->f_recv = f_recv;
1605 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001606}
1607
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001608#if defined(MBEDTLS_SSL_PROTO_DTLS)
1609void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1610{
1611 ssl->mtu = mtu;
1612}
1613#endif
1614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001615void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001616{
1617 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001618}
1619
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001620void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1621 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001622 mbedtls_ssl_set_timer_t *f_set_timer,
1623 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001624{
1625 ssl->p_timer = p_timer;
1626 ssl->f_set_timer = f_set_timer;
1627 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001628
1629 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001630 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001631}
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001634void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001635 void *p_cache,
1636 mbedtls_ssl_cache_get_t *f_get_cache,
1637 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001639 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001640 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001641 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_SSL_CLI_C)
1646int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001647{
Janos Follath865b3eb2019-12-16 11:46:15 +00001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001649
1650 if( ssl == NULL ||
1651 session == NULL ||
1652 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001653 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001656 }
1657
Hanno Beckere810bbc2021-05-14 16:01:05 +01001658 if( ssl->handshake->resume == 1 )
1659 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1660
Jerry Yu21092062022-10-10 21:21:31 +08001661#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1662 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001663 {
Jerry Yu21092062022-10-10 21:21:31 +08001664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1665 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1666
1667 if( mbedtls_ssl_validate_ciphersuite(
1668 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1669 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1670 {
1671 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1672 session->ciphersuite ) );
1673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1674 }
Jerry Yu40afab62022-10-08 10:42:13 +08001675 }
Jerry Yu21092062022-10-10 21:21:31 +08001676#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001677
Hanno Becker52055ae2019-02-06 14:30:46 +00001678 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1679 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001680 return( ret );
1681
Paul Bakker0a597072012-09-25 21:55:46 +00001682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001683
1684 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001688void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1689 const int *ciphersuites )
1690{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001691 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692}
1693
Ronald Cron6f135e12021-12-08 16:57:54 +01001694#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001695void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001696 const int kex_modes )
1697{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001698 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001699}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001700
1701#if defined(MBEDTLS_SSL_EARLY_DATA)
1702void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001703 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001704{
1705 conf->early_data_enabled = early_data_enabled;
1706}
Jerry Yucc4e0072022-11-22 17:22:22 +08001707
1708#if defined(MBEDTLS_SSL_SRV_C)
1709void mbedtls_ssl_tls13_conf_max_early_data_size(
1710 mbedtls_ssl_config *conf, uint32_t max_early_data_size )
1711{
Jerry Yu39da9852022-12-06 16:58:36 +08001712 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001713}
1714#endif /* MBEDTLS_SSL_SRV_C */
1715
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001716#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001717#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001720void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001721 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001722{
1723 conf->cert_profile = profile;
1724}
1725
Glenn Strauss36872db2022-01-22 05:06:31 -05001726static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1727{
1728 mbedtls_ssl_key_cert *cur = key_cert, *next;
1729
1730 while( cur != NULL )
1731 {
1732 next = cur->next;
1733 mbedtls_free( cur );
1734 cur = next;
1735 }
1736}
1737
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001738/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001739MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001740static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1741 mbedtls_x509_crt *cert,
1742 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001743{
niisato8ee24222018-06-25 19:05:48 +09001744 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001745
Glenn Strauss36872db2022-01-22 05:06:31 -05001746 if( cert == NULL )
1747 {
1748 /* Free list if cert is null */
1749 ssl_key_cert_free( *head );
1750 *head = NULL;
1751 return( 0 );
1752 }
1753
niisato8ee24222018-06-25 19:05:48 +09001754 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1755 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001756 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001757
niisato8ee24222018-06-25 19:05:48 +09001758 new_cert->cert = cert;
1759 new_cert->key = key;
1760 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001761
Glenn Strauss36872db2022-01-22 05:06:31 -05001762 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001763 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001764 {
niisato8ee24222018-06-25 19:05:48 +09001765 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001766 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001767 else
1768 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001769 mbedtls_ssl_key_cert *cur = *head;
1770 while( cur->next != NULL )
1771 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001772 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001773 }
1774
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001775 return( 0 );
1776}
1777
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001778int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001779 mbedtls_x509_crt *own_cert,
1780 mbedtls_pk_context *pk_key )
1781{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001782 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001783}
1784
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001785void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001786 mbedtls_x509_crt *ca_chain,
1787 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001788{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001789 conf->ca_chain = ca_chain;
1790 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001791
1792#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1793 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1794 * cannot be used together. */
1795 conf->f_ca_cb = NULL;
1796 conf->p_ca_cb = NULL;
1797#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001798}
Hanno Becker5adaad92019-03-27 16:54:37 +00001799
1800#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1801void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001802 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001803 void *p_ca_cb )
1804{
1805 conf->f_ca_cb = f_ca_cb;
1806 conf->p_ca_cb = p_ca_cb;
1807
1808 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1809 * cannot be used together. */
1810 conf->ca_chain = NULL;
1811 conf->ca_crl = NULL;
1812}
1813#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001815
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001816#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001817const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1818 size_t *name_len )
1819{
1820 *name_len = ssl->handshake->sni_name_len;
1821 return( ssl->handshake->sni_name );
1822}
1823
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001824int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1825 mbedtls_x509_crt *own_cert,
1826 mbedtls_pk_context *pk_key )
1827{
1828 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1829 own_cert, pk_key ) );
1830}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001831
1832void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1833 mbedtls_x509_crt *ca_chain,
1834 mbedtls_x509_crl *ca_crl )
1835{
1836 ssl->handshake->sni_ca_chain = ca_chain;
1837 ssl->handshake->sni_ca_crl = ca_crl;
1838}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001839
Glenn Strauss999ef702022-03-11 01:37:23 -05001840#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1841void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1842 const mbedtls_x509_crt *crt)
1843{
1844 ssl->handshake->dn_hints = crt;
1845}
1846#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1847
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001848void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1849 int authmode )
1850{
1851 ssl->handshake->sni_authmode = authmode;
1852}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001853#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1854
Hanno Becker8927c832019-04-03 12:52:50 +01001855#if defined(MBEDTLS_X509_CRT_PARSE_C)
1856void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1857 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1858 void *p_vrfy )
1859{
1860 ssl->f_vrfy = f_vrfy;
1861 ssl->p_vrfy = p_vrfy;
1862}
1863#endif
1864
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001865#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001866/*
1867 * Set EC J-PAKE password for current handshake
1868 */
Valerio Setti02c25b52022-11-15 14:08:42 +01001869#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001870int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1871 const unsigned char *pw,
1872 size_t pw_len )
1873{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001874 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1876 psa_pake_role_t psa_role;
1877 psa_status_t status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001878
Janos Follath8eb64132016-06-03 15:40:57 +01001879 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1881
Neil Armstrongca7d5062022-05-31 14:43:23 +02001882 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1883 psa_role = PSA_PAKE_ROLE_SERVER;
1884 else
1885 psa_role = PSA_PAKE_ROLE_CLIENT;
1886
Valerio Settiaca21b72022-11-17 18:17:01 +01001887 /* Empty password is not valid */
1888 if( ( pw == NULL) || ( pw_len == 0 ) )
1889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001890
Valerio Settiaca21b72022-11-17 18:17:01 +01001891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1892 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1893 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001894
Valerio Settiaca21b72022-11-17 18:17:01 +01001895 status = psa_import_key( &attributes, pw, pw_len,
1896 &ssl->handshake->psa_pake_password );
1897 if( status != PSA_SUCCESS )
1898 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001899
1900 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1901 psa_pake_cs_set_primitive( &cipher_suite,
1902 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1903 PSA_ECC_FAMILY_SECP_R1,
1904 256) );
1905 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1906
1907 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1908 if( status != PSA_SUCCESS )
1909 {
1910 psa_destroy_key( ssl->handshake->psa_pake_password );
1911 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1912 }
1913
1914 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1915 if( status != PSA_SUCCESS )
1916 {
1917 psa_destroy_key( ssl->handshake->psa_pake_password );
1918 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1919 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1920 }
1921
Valerio Settiaca21b72022-11-17 18:17:01 +01001922 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1923 ssl->handshake->psa_pake_password );
1924 if( status != PSA_SUCCESS )
Neil Armstrongca7d5062022-05-31 14:43:23 +02001925 {
Valerio Settiaca21b72022-11-17 18:17:01 +01001926 psa_destroy_key( ssl->handshake->psa_pake_password );
1927 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1928 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001929 }
1930
1931 ssl->handshake->psa_pake_ctx_is_ok = 1;
1932
1933 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001934}
1935#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001936int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1937 const unsigned char *pw,
1938 size_t pw_len )
1939{
1940 mbedtls_ecjpake_role role;
1941
1942 if( ssl->handshake == NULL || ssl->conf == NULL )
1943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1944
1945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1946 role = MBEDTLS_ECJPAKE_SERVER;
1947 else
1948 role = MBEDTLS_ECJPAKE_CLIENT;
1949
1950 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1951 role,
1952 MBEDTLS_MD_SHA256,
1953 MBEDTLS_ECP_DP_SECP256R1,
1954 pw, pw_len ) );
1955}
Valerio Setti02c25b52022-11-15 14:08:42 +01001956#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001957#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001958
Ronald Cron73fe8df2022-10-05 14:31:43 +02001959#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001960int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001961{
Ronald Crond29e13e2022-10-19 10:33:48 +02001962 if( conf->psk_identity == NULL ||
1963 conf->psk_identity_len == 0 )
1964 {
1965 return( 0 );
1966 }
1967
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001968#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001969 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001970 return( 1 );
1971#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001972
1973 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001974 return( 1 );
1975
1976 return( 0 );
1977}
1978
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001979static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1980{
1981 /* Remove reference to existing PSK, if any. */
1982#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001983 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001984 {
1985 /* The maintenance of the PSK key slot is the
1986 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001987 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001988 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001989#endif /* MBEDTLS_USE_PSA_CRYPTO */
1990 if( conf->psk != NULL )
1991 {
1992 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1993
1994 mbedtls_free( conf->psk );
1995 conf->psk = NULL;
1996 conf->psk_len = 0;
1997 }
1998
1999 /* Remove reference to PSK identity, if any. */
2000 if( conf->psk_identity != NULL )
2001 {
2002 mbedtls_free( conf->psk_identity );
2003 conf->psk_identity = NULL;
2004 conf->psk_identity_len = 0;
2005 }
2006}
2007
Hanno Becker7390c712018-11-15 13:33:04 +00002008/* This function assumes that PSK identity in the SSL config is unset.
2009 * It checks that the provided identity is well-formed and attempts
2010 * to make a copy of it in the SSL config.
2011 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002012MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002013static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2014 unsigned char const *psk_identity,
2015 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002016{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002017 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002018 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002019 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002020 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002021 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002022 {
2023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2024 }
2025
Hanno Becker7390c712018-11-15 13:33:04 +00002026 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2027 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002028 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002029
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002030 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002031 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002032
2033 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002034}
2035
Hanno Becker7390c712018-11-15 13:33:04 +00002036int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2037 const unsigned char *psk, size_t psk_len,
2038 const unsigned char *psk_identity, size_t psk_identity_len )
2039{
Janos Follath865b3eb2019-12-16 11:46:15 +00002040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002041
2042 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002043 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002044 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002045
2046 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002047 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002049 if( psk_len == 0 )
2050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2051 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2053
Hanno Becker7390c712018-11-15 13:33:04 +00002054 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2055 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2056 conf->psk_len = psk_len;
2057 memcpy( conf->psk, psk, conf->psk_len );
2058
2059 /* Check and set PSK Identity */
2060 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2061 if( ret != 0 )
2062 ssl_conf_remove_psk( conf );
2063
2064 return( ret );
2065}
2066
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002067static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2068{
2069#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002070 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002071 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002072 /* The maintenance of the external PSK key slot is the
2073 * user's responsibility. */
2074 if( ssl->handshake->psk_opaque_is_internal )
2075 {
2076 psa_destroy_key( ssl->handshake->psk_opaque );
2077 ssl->handshake->psk_opaque_is_internal = 0;
2078 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002079 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002080 }
Neil Armstronge952a302022-05-03 10:22:14 +02002081#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002082 if( ssl->handshake->psk != NULL )
2083 {
2084 mbedtls_platform_zeroize( ssl->handshake->psk,
2085 ssl->handshake->psk_len );
2086 mbedtls_free( ssl->handshake->psk );
2087 ssl->handshake->psk_len = 0;
2088 }
Neil Armstronge952a302022-05-03 10:22:14 +02002089#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002090}
2091
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002092int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2093 const unsigned char *psk, size_t psk_len )
2094{
Neil Armstrong501c9322022-05-03 09:35:09 +02002095#if defined(MBEDTLS_USE_PSA_CRYPTO)
2096 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002098 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002100#endif /* MBEDTLS_USE_PSA_CRYPTO */
2101
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002102 if( psk == NULL || ssl->handshake == NULL )
2103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2104
2105 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2107
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002108 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002109
Neil Armstrong501c9322022-05-03 09:35:09 +02002110#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2112 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2113 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002114 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002115 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2116 else
2117 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2118 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2119 }
2120#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002121
Jerry Yu568ec252022-07-22 21:27:34 +08002122#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002123 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2124 {
2125 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2126 psa_set_key_usage_flags( &key_attributes,
2127 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2128 }
2129#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2130
Neil Armstrong501c9322022-05-03 09:35:09 +02002131 psa_set_key_algorithm( &key_attributes, alg );
2132 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2133
2134 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2135 if( status != PSA_SUCCESS )
2136 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2137
2138 /* Allow calling psa_destroy_key() on psk remove */
2139 ssl->handshake->psk_opaque_is_internal = 1;
2140 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2141#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002142 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002143 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002144
2145 ssl->handshake->psk_len = psk_len;
2146 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2147
2148 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002149#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002150}
2151
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002152#if defined(MBEDTLS_USE_PSA_CRYPTO)
2153int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002154 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002155 const unsigned char *psk_identity,
2156 size_t psk_identity_len )
2157{
Janos Follath865b3eb2019-12-16 11:46:15 +00002158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002159
2160 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002161 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002162 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002163
Hanno Becker7390c712018-11-15 13:33:04 +00002164 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002165 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002167 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002168
2169 /* Check and set PSK Identity */
2170 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2171 psk_identity_len );
2172 if( ret != 0 )
2173 ssl_conf_remove_psk( conf );
2174
2175 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002176}
2177
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002178int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2179 mbedtls_svc_key_id_t psk )
2180{
2181 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2182 ( ssl->handshake == NULL ) )
2183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2184
2185 ssl_remove_psk( ssl );
2186 ssl->handshake->psk_opaque = psk;
2187 return( 0 );
2188}
2189#endif /* MBEDTLS_USE_PSA_CRYPTO */
2190
Jerry Yu8897c072022-08-12 13:56:53 +08002191#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002192void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2193 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2194 size_t),
2195 void *p_psk )
2196{
2197 conf->f_psk = f_psk;
2198 conf->p_psk = p_psk;
2199}
Jerry Yu8897c072022-08-12 13:56:53 +08002200#endif /* MBEDTLS_SSL_SRV_C */
2201
Ronald Cron73fe8df2022-10-05 14:31:43 +02002202#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002203
2204#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002205static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2206 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002207{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002208#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002209 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002210 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002211#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002212 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002213 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002214 return( MBEDTLS_SSL_MODE_STREAM );
2215}
2216
2217#else /* MBEDTLS_USE_PSA_CRYPTO */
2218
2219static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2220 mbedtls_cipher_mode_t mode )
2221{
2222#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2223 if( mode == MBEDTLS_MODE_CBC )
2224 return( MBEDTLS_SSL_MODE_CBC );
2225#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2226
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002227#if defined(MBEDTLS_GCM_C) || \
2228 defined(MBEDTLS_CCM_C) || \
2229 defined(MBEDTLS_CHACHAPOLY_C)
2230 if( mode == MBEDTLS_MODE_GCM ||
2231 mode == MBEDTLS_MODE_CCM ||
2232 mode == MBEDTLS_MODE_CHACHAPOLY )
2233 return( MBEDTLS_SSL_MODE_AEAD );
2234#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002235
2236 return( MBEDTLS_SSL_MODE_STREAM );
2237}
Gilles Peskine301711e2022-04-26 16:57:05 +02002238#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002239
Gilles Peskinee108d982022-04-26 16:50:40 +02002240static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2241 mbedtls_ssl_mode_t base_mode,
2242 int encrypt_then_mac )
2243{
2244#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2245 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2246 base_mode == MBEDTLS_SSL_MODE_CBC )
2247 {
2248 return( MBEDTLS_SSL_MODE_CBC_ETM );
2249 }
2250#else
2251 (void) encrypt_then_mac;
2252#endif
2253 return( base_mode );
2254}
2255
Neil Armstrongab555e02022-04-04 11:07:59 +02002256mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002257 const mbedtls_ssl_transform *transform )
2258{
Gilles Peskinee108d982022-04-26 16:50:40 +02002259 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002260#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002261 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002262#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002263 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2264#endif
2265 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002266
Gilles Peskinee108d982022-04-26 16:50:40 +02002267 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002268#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002269 encrypt_then_mac = transform->encrypt_then_mac;
2270#endif
2271 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002272}
2273
Neil Armstrongab555e02022-04-04 11:07:59 +02002274mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002275#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002276 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002277#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002278 const mbedtls_ssl_ciphersuite_t *suite )
2279{
Gilles Peskinee108d982022-04-26 16:50:40 +02002280 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2281
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002282#if defined(MBEDTLS_USE_PSA_CRYPTO)
2283 psa_status_t status;
2284 psa_algorithm_t alg;
2285 psa_key_type_t type;
2286 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002287 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2288 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002289 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002290#else
2291 const mbedtls_cipher_info_t *cipher =
2292 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002293 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002294 {
2295 base_mode =
2296 mbedtls_ssl_get_base_mode(
2297 mbedtls_cipher_info_get_mode( cipher ) );
2298 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002299#endif /* MBEDTLS_USE_PSA_CRYPTO */
2300
Gilles Peskinee108d982022-04-26 16:50:40 +02002301#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2302 int encrypt_then_mac = 0;
2303#endif
2304 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002305}
2306
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002307#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002308
2309#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002310/* Serialization of TLS 1.3 sessions:
2311 *
2312 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002313 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002314 * uint64 ticket_received;
2315 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002316 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002317 * } ClientOnlyData;
2318 *
2319 * struct {
2320 * uint8 endpoint;
2321 * uint8 ciphersuite[2];
2322 * uint32 ticket_age_add;
2323 * uint8 ticket_flags;
2324 * opaque resumption_key<0..255>;
2325 * select ( endpoint ) {
2326 * case client: ClientOnlyData;
2327 * case server: uint64 start_time;
2328 * };
2329 * } serialized_session_tls13;
2330 *
2331 */
2332#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002333MBEDTLS_CHECK_RETURN_CRITICAL
2334static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2335 unsigned char *buf,
2336 size_t buf_len,
2337 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002338{
2339 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002340#if defined(MBEDTLS_SSL_CLI_C) && \
2341 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2342 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002343 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002344#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002345 size_t needed = 1 /* endpoint */
2346 + 2 /* ciphersuite */
2347 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002348 + 1 /* ticket_flags */
2349 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002350 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002351
2352 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2354 needed += session->resumption_key_len; /* resumption_key */
2355
Jerry Yu438ddd82022-07-07 06:55:50 +00002356#if defined(MBEDTLS_HAVE_TIME)
2357 needed += 8; /* start_time or ticket_received */
2358#endif
2359
2360#if defined(MBEDTLS_SSL_CLI_C)
2361 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2362 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002363#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002364 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002365 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002366#endif
2367
Jerry Yu438ddd82022-07-07 06:55:50 +00002368 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002369 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002370
2371 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002372 if( session->ticket_len > SIZE_MAX - needed )
2373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002374
Jerry Yue28d9742022-08-18 15:44:03 +08002375 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002376 }
2377#endif /* MBEDTLS_SSL_CLI_C */
2378
Jerry Yue36fdd62022-08-17 21:31:36 +08002379 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002380 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002381 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002382
2383 p[0] = session->endpoint;
2384 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2385 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2386 p[7] = session->ticket_flags;
2387
2388 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002389 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002390 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002391 memcpy( p, session->resumption_key, session->resumption_key_len );
2392 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002393
2394#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2395 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2396 {
2397 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2398 p += 8;
2399 }
2400#endif /* MBEDTLS_HAVE_TIME */
2401
2402#if defined(MBEDTLS_SSL_CLI_C)
2403 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2404 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002405#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2406 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2407 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002408 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002409 {
2410 /* save host name */
2411 memcpy( p, session->hostname, hostname_len );
2412 p += hostname_len;
2413 }
2414#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2415
Jerry Yu438ddd82022-07-07 06:55:50 +00002416#if defined(MBEDTLS_HAVE_TIME)
2417 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2418 p += 8;
2419#endif
2420 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2421 p += 4;
2422
2423 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2424 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002425
2426 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002427 {
2428 memcpy( p, session->ticket, session->ticket_len );
2429 p += session->ticket_len;
2430 }
2431 }
2432#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002433 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002434}
2435
2436MBEDTLS_CHECK_RETURN_CRITICAL
2437static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2438 const unsigned char *buf,
2439 size_t len )
2440{
2441 const unsigned char *p = buf;
2442 const unsigned char *end = buf + len;
2443
2444 if( end - p < 9 )
2445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2446 session->endpoint = p[0];
2447 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2448 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2449 session->ticket_flags = p[7];
2450
2451 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002452 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002453 p += 9;
2454
Jerry Yubc7c1a42022-07-21 22:57:37 +08002455 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2457
Jerry Yubc7c1a42022-07-21 22:57:37 +08002458 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002460 memcpy( session->resumption_key, p, session->resumption_key_len );
2461 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002462
2463#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2464 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2465 {
2466 if( end - p < 8 )
2467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2468 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2469 p += 8;
2470 }
2471#endif /* MBEDTLS_HAVE_TIME */
2472
2473#if defined(MBEDTLS_SSL_CLI_C)
2474 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2475 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002476#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2477 defined(MBEDTLS_SSL_SESSION_TICKETS)
2478 size_t hostname_len;
2479 /* load host name */
2480 if( end - p < 2 )
2481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2482 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2483 p += 2;
2484
2485 if( end - p < ( long int )hostname_len )
2486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2487 if( hostname_len > 0 )
2488 {
2489 session->hostname = mbedtls_calloc( 1, hostname_len );
2490 if( session->hostname == NULL )
2491 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2492 memcpy( session->hostname, p, hostname_len );
2493 p += hostname_len;
2494 }
2495#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2496 MBEDTLS_SSL_SESSION_TICKETS */
2497
Jerry Yu438ddd82022-07-07 06:55:50 +00002498#if defined(MBEDTLS_HAVE_TIME)
2499 if( end - p < 8 )
2500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2501 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2502 p += 8;
2503#endif
2504 if( end - p < 4 )
2505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2506 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2507 p += 4;
2508
2509 if( end - p < 2 )
2510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2511 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2512 p += 2;
2513
2514 if( end - p < ( long int )session->ticket_len )
2515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2516 if( session->ticket_len > 0 )
2517 {
2518 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2519 if( session->ticket == NULL )
2520 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2521 memcpy( session->ticket, p, session->ticket_len );
2522 p += session->ticket_len;
2523 }
2524 }
2525#endif /* MBEDTLS_SSL_CLI_C */
2526
2527 return( 0 );
2528
2529}
2530#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002531MBEDTLS_CHECK_RETURN_CRITICAL
2532static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2533 unsigned char *buf,
2534 size_t buf_len,
2535 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002536{
2537 ((void) session);
2538 ((void) buf);
2539 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002540 *olen = 0;
2541 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002542}
Jerry Yu438ddd82022-07-07 06:55:50 +00002543
2544static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2545 unsigned char *buf,
2546 size_t buf_len )
2547{
2548 ((void) session);
2549 ((void) buf);
2550 ((void) buf_len);
2551 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2552}
2553#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002554#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2555
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002556psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002557 size_t taglen,
2558 psa_algorithm_t *alg,
2559 psa_key_type_t *key_type,
2560 size_t *key_size )
2561{
2562 switch ( mbedtls_cipher_type )
2563 {
2564 case MBEDTLS_CIPHER_AES_128_CBC:
2565 *alg = PSA_ALG_CBC_NO_PADDING;
2566 *key_type = PSA_KEY_TYPE_AES;
2567 *key_size = 128;
2568 break;
2569 case MBEDTLS_CIPHER_AES_128_CCM:
2570 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2571 *key_type = PSA_KEY_TYPE_AES;
2572 *key_size = 128;
2573 break;
2574 case MBEDTLS_CIPHER_AES_128_GCM:
2575 *alg = PSA_ALG_GCM;
2576 *key_type = PSA_KEY_TYPE_AES;
2577 *key_size = 128;
2578 break;
2579 case MBEDTLS_CIPHER_AES_192_CCM:
2580 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2581 *key_type = PSA_KEY_TYPE_AES;
2582 *key_size = 192;
2583 break;
2584 case MBEDTLS_CIPHER_AES_192_GCM:
2585 *alg = PSA_ALG_GCM;
2586 *key_type = PSA_KEY_TYPE_AES;
2587 *key_size = 192;
2588 break;
2589 case MBEDTLS_CIPHER_AES_256_CBC:
2590 *alg = PSA_ALG_CBC_NO_PADDING;
2591 *key_type = PSA_KEY_TYPE_AES;
2592 *key_size = 256;
2593 break;
2594 case MBEDTLS_CIPHER_AES_256_CCM:
2595 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2596 *key_type = PSA_KEY_TYPE_AES;
2597 *key_size = 256;
2598 break;
2599 case MBEDTLS_CIPHER_AES_256_GCM:
2600 *alg = PSA_ALG_GCM;
2601 *key_type = PSA_KEY_TYPE_AES;
2602 *key_size = 256;
2603 break;
2604 case MBEDTLS_CIPHER_ARIA_128_CBC:
2605 *alg = PSA_ALG_CBC_NO_PADDING;
2606 *key_type = PSA_KEY_TYPE_ARIA;
2607 *key_size = 128;
2608 break;
2609 case MBEDTLS_CIPHER_ARIA_128_CCM:
2610 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2611 *key_type = PSA_KEY_TYPE_ARIA;
2612 *key_size = 128;
2613 break;
2614 case MBEDTLS_CIPHER_ARIA_128_GCM:
2615 *alg = PSA_ALG_GCM;
2616 *key_type = PSA_KEY_TYPE_ARIA;
2617 *key_size = 128;
2618 break;
2619 case MBEDTLS_CIPHER_ARIA_192_CCM:
2620 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2621 *key_type = PSA_KEY_TYPE_ARIA;
2622 *key_size = 192;
2623 break;
2624 case MBEDTLS_CIPHER_ARIA_192_GCM:
2625 *alg = PSA_ALG_GCM;
2626 *key_type = PSA_KEY_TYPE_ARIA;
2627 *key_size = 192;
2628 break;
2629 case MBEDTLS_CIPHER_ARIA_256_CBC:
2630 *alg = PSA_ALG_CBC_NO_PADDING;
2631 *key_type = PSA_KEY_TYPE_ARIA;
2632 *key_size = 256;
2633 break;
2634 case MBEDTLS_CIPHER_ARIA_256_CCM:
2635 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2636 *key_type = PSA_KEY_TYPE_ARIA;
2637 *key_size = 256;
2638 break;
2639 case MBEDTLS_CIPHER_ARIA_256_GCM:
2640 *alg = PSA_ALG_GCM;
2641 *key_type = PSA_KEY_TYPE_ARIA;
2642 *key_size = 256;
2643 break;
2644 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2645 *alg = PSA_ALG_CBC_NO_PADDING;
2646 *key_type = PSA_KEY_TYPE_CAMELLIA;
2647 *key_size = 128;
2648 break;
2649 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2650 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2651 *key_type = PSA_KEY_TYPE_CAMELLIA;
2652 *key_size = 128;
2653 break;
2654 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2655 *alg = PSA_ALG_GCM;
2656 *key_type = PSA_KEY_TYPE_CAMELLIA;
2657 *key_size = 128;
2658 break;
2659 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2660 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2661 *key_type = PSA_KEY_TYPE_CAMELLIA;
2662 *key_size = 192;
2663 break;
2664 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2665 *alg = PSA_ALG_GCM;
2666 *key_type = PSA_KEY_TYPE_CAMELLIA;
2667 *key_size = 192;
2668 break;
2669 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2670 *alg = PSA_ALG_CBC_NO_PADDING;
2671 *key_type = PSA_KEY_TYPE_CAMELLIA;
2672 *key_size = 256;
2673 break;
2674 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2675 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2676 *key_type = PSA_KEY_TYPE_CAMELLIA;
2677 *key_size = 256;
2678 break;
2679 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2680 *alg = PSA_ALG_GCM;
2681 *key_type = PSA_KEY_TYPE_CAMELLIA;
2682 *key_size = 256;
2683 break;
2684 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2685 *alg = PSA_ALG_CHACHA20_POLY1305;
2686 *key_type = PSA_KEY_TYPE_CHACHA20;
2687 *key_size = 256;
2688 break;
2689 case MBEDTLS_CIPHER_NULL:
2690 *alg = MBEDTLS_SSL_NULL_CIPHER;
2691 *key_type = 0;
2692 *key_size = 0;
2693 break;
2694 default:
2695 return PSA_ERROR_NOT_SUPPORTED;
2696 }
2697
2698 return PSA_SUCCESS;
2699}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002700#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002701
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002702#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002703int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2704 const unsigned char *dhm_P, size_t P_len,
2705 const unsigned char *dhm_G, size_t G_len )
2706{
Janos Follath865b3eb2019-12-16 11:46:15 +00002707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002708
Glenn Strausscee11292021-12-20 01:43:17 -05002709 mbedtls_mpi_free( &conf->dhm_P );
2710 mbedtls_mpi_free( &conf->dhm_G );
2711
Hanno Beckera90658f2017-10-04 15:29:08 +01002712 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2713 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2714 {
2715 mbedtls_mpi_free( &conf->dhm_P );
2716 mbedtls_mpi_free( &conf->dhm_G );
2717 return( ret );
2718 }
2719
2720 return( 0 );
2721}
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002723int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002724{
Janos Follath865b3eb2019-12-16 11:46:15 +00002725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002726
Glenn Strausscee11292021-12-20 01:43:17 -05002727 mbedtls_mpi_free( &conf->dhm_P );
2728 mbedtls_mpi_free( &conf->dhm_G );
2729
Gilles Peskinee5702482021-06-11 21:59:08 +02002730 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2731 &conf->dhm_P ) ) != 0 ||
2732 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2733 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002734 {
2735 mbedtls_mpi_free( &conf->dhm_P );
2736 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002737 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002738 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002739
2740 return( 0 );
2741}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002742#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002743
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002744#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2745/*
2746 * Set the minimum length for Diffie-Hellman parameters
2747 */
2748void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2749 unsigned int bitlen )
2750{
2751 conf->dhm_min_bitlen = bitlen;
2752}
2753#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2754
Ronald Crone68ab4f2022-10-05 12:46:29 +02002755#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002756#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002757/*
2758 * Set allowed/preferred hashes for handshake signatures
2759 */
2760void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2761 const int *hashes )
2762{
2763 conf->sig_hashes = hashes;
2764}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002765#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002766
Jerry Yuf017ee42022-01-12 15:49:48 +08002767/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002768void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2769 const uint16_t* sig_algs )
2770{
Jerry Yuf017ee42022-01-12 15:49:48 +08002771#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2772 conf->sig_hashes = NULL;
2773#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2774 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002775}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002776#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002777
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002778#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002779#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002780/*
2781 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002782 *
2783 * mbedtls_ssl_setup() takes the provided list
2784 * and translates it to a list of IANA TLS group identifiers,
2785 * stored in ssl->handshake->group_list.
2786 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002787 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002788void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002789 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002790{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002791 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002792 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002793}
Brett Warrene0edc842021-08-17 09:53:13 +01002794#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002795#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002796
Brett Warrene0edc842021-08-17 09:53:13 +01002797/*
2798 * Set the allowed groups
2799 */
2800void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2801 const uint16_t *group_list )
2802{
2803#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2804 conf->curve_list = NULL;
2805#endif
2806 conf->group_list = group_list;
2807}
2808
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002809#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811{
Hanno Becker947194e2017-04-07 13:25:49 +01002812 /* Initialize to suppress unnecessary compiler warning */
2813 size_t hostname_len = 0;
2814
2815 /* Check if new hostname is valid before
2816 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002817 if( hostname != NULL )
2818 {
2819 hostname_len = strlen( hostname );
2820
2821 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2823 }
2824
2825 /* Now it's clear that we will overwrite the old hostname,
2826 * so we can free it safely */
2827
2828 if( ssl->hostname != NULL )
2829 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002830 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002831 mbedtls_free( ssl->hostname );
2832 }
2833
2834 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002835
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002837 {
2838 ssl->hostname = NULL;
2839 }
2840 else
2841 {
2842 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002843 if( ssl->hostname == NULL )
2844 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002845
Hanno Becker947194e2017-04-07 13:25:49 +01002846 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002847
Hanno Becker947194e2017-04-07 13:25:49 +01002848 ssl->hostname[hostname_len] = '\0';
2849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
2851 return( 0 );
2852}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002853#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002855#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002856void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002858 const unsigned char *, size_t),
2859 void *p_sni )
2860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002861 conf->f_sni = f_sni;
2862 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002867int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002868{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002869 size_t cur_len, tot_len;
2870 const char **p;
2871
2872 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002873 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2874 * MUST NOT be truncated."
2875 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002876 */
2877 tot_len = 0;
2878 for( p = protos; *p != NULL; p++ )
2879 {
2880 cur_len = strlen( *p );
2881 tot_len += cur_len;
2882
Ronald Cron8216dd32020-04-23 16:41:44 +02002883 if( ( cur_len == 0 ) ||
2884 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2885 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002887 }
2888
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002889 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002890
2891 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002892}
2893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002895{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002896 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002899
Johan Pascalb62bb512015-12-03 21:56:45 +01002900#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002901void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2902 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002903{
2904 conf->dtls_srtp_mki_support = support_mki_value;
2905}
2906
Ron Eldoref72faf2018-07-12 11:54:20 +03002907int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2908 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002909 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002910{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002911 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002912 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002914 }
Ron Eldor591f1622018-01-22 12:30:04 +02002915
2916 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002917 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002918 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002919 }
Ron Eldor591f1622018-01-22 12:30:04 +02002920
2921 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2922 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002923 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002924}
2925
Ron Eldoref72faf2018-07-12 11:54:20 +03002926int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002927 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002928{
Johan Pascal253d0262020-09-22 13:04:45 +02002929 const mbedtls_ssl_srtp_profile *p;
2930 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002931
Johan Pascal253d0262020-09-22 13:04:45 +02002932 /* check the profiles list: all entry must be valid,
2933 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002934 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2935 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2936 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002937 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002938 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002939 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002940 list_size++;
2941 }
2942 else
2943 {
2944 /* unsupported value, stop parsing and set the size to an error value */
2945 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002946 }
2947 }
2948
Johan Pascal5ef72d22020-10-28 17:05:47 +01002949 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002950 {
Johan Pascal253d0262020-09-22 13:04:45 +02002951 conf->dtls_srtp_profile_list = NULL;
2952 conf->dtls_srtp_profile_list_len = 0;
2953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2954 }
2955
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002956 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002957 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002958
2959 return( 0 );
2960}
2961
Johan Pascal5ef72d22020-10-28 17:05:47 +01002962void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2963 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002964{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002965 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2966 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002967 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002968 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002969 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002970 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002971 else
2972 {
2973 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002974 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2975 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002976 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002977}
Johan Pascalb62bb512015-12-03 21:56:45 +01002978#endif /* MBEDTLS_SSL_DTLS_SRTP */
2979
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302980#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002981void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002982{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002983 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002984}
2985
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002986void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002987{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002988 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002989}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302990#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002991
Janos Follath088ce432017-04-10 12:42:31 +01002992#if defined(MBEDTLS_SSL_SRV_C)
2993void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2994 char cert_req_ca_list )
2995{
2996 conf->cert_req_ca_list = cert_req_ca_list;
2997}
2998#endif
2999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003001void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003002{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003003 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003004}
3005#endif
3006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003008void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003009{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003010 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003011}
3012#endif
3013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003015int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003018 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003021 }
3022
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003023 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003024
3025 return( 0 );
3026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003028
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003029void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003030{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003031 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003032}
3033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003035void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003036{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003037 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003038}
3039
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003040void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003041{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003042 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003043}
3044
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003045void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003046 const unsigned char period[8] )
3047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003048 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003049}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003053#if defined(MBEDTLS_SSL_CLI_C)
3054void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003055{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003056 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003057}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003058#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003059
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003060#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003061
Jerry Yud0766ec2022-09-22 10:46:57 +08003062#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003063void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3064 uint16_t num_tickets )
3065{
Jerry Yud0766ec2022-09-22 10:46:57 +08003066 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003067}
3068#endif
3069
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003070void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3071 mbedtls_ssl_ticket_write_t *f_ticket_write,
3072 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3073 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003074{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003075 conf->f_ticket_write = f_ticket_write;
3076 conf->f_ticket_parse = f_ticket_parse;
3077 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003078}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003081
Hanno Becker7e6c1782021-06-08 09:24:55 +01003082void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3083 mbedtls_ssl_export_keys_t *f_export_keys,
3084 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003085{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003086 ssl->f_export_keys = f_export_keys;
3087 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003088}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003089
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003090#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003091void mbedtls_ssl_conf_async_private_cb(
3092 mbedtls_ssl_config *conf,
3093 mbedtls_ssl_async_sign_t *f_async_sign,
3094 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3095 mbedtls_ssl_async_resume_t *f_async_resume,
3096 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003097 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003098{
3099 conf->f_async_sign_start = f_async_sign;
3100 conf->f_async_decrypt_start = f_async_decrypt;
3101 conf->f_async_resume = f_async_resume;
3102 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003103 conf->p_async_config_data = async_config_data;
3104}
3105
Gilles Peskine8f97af72018-04-26 11:46:10 +02003106void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3107{
3108 return( conf->p_async_config_data );
3109}
3110
Gilles Peskine1febfef2018-04-30 11:54:39 +02003111void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003112{
3113 if( ssl->handshake == NULL )
3114 return( NULL );
3115 else
3116 return( ssl->handshake->user_async_ctx );
3117}
3118
Gilles Peskine1febfef2018-04-30 11:54:39 +02003119void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003120 void *ctx )
3121{
3122 if( ssl->handshake != NULL )
3123 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003124}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003125#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003126
Paul Bakker5121ce52009-01-03 21:22:43 +00003127/*
3128 * SSL get accessors
3129 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003130uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003131{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003132 if( ssl->session != NULL )
3133 return( ssl->session->verify_result );
3134
3135 if( ssl->session_negotiate != NULL )
3136 return( ssl->session_negotiate->verify_result );
3137
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003138 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003139}
3140
Glenn Strauss8f526902022-01-13 00:04:49 -05003141int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3142{
3143 if( ssl == NULL || ssl->session == NULL )
3144 return( 0 );
3145
3146 return( ssl->session->ciphersuite );
3147}
3148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003150{
Paul Bakker926c8e42013-03-06 10:23:34 +01003151 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003152 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003155}
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003161 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003162 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003163 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003164 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003165 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003166 default:
3167 return( "unknown (DTLS)" );
3168 }
3169 }
3170#endif
3171
Glenn Strauss60bfe602022-03-14 19:04:24 -04003172 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003173 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003174 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003175 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003176 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003177 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003178 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003179 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003180 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003181}
3182
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003183#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003184size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3185{
David Horstmann95d516f2021-05-04 18:36:56 +01003186 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003187 size_t read_mfl;
3188
3189 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3190 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3191 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3192 {
3193 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3194 }
3195
3196 /* Check if a smaller max length was negotiated */
3197 if( ssl->session_out != NULL )
3198 {
3199 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3200 if( read_mfl < max_len )
3201 {
3202 max_len = read_mfl;
3203 }
3204 }
3205
3206 // During a handshake, use the value being negotiated
3207 if( ssl->session_negotiate != NULL )
3208 {
3209 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3210 if( read_mfl < max_len )
3211 {
3212 max_len = read_mfl;
3213 }
3214 }
3215
3216 return( max_len );
3217}
3218
3219size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003220{
3221 size_t max_len;
3222
3223 /*
3224 * Assume mfl_code is correct since it was checked when set
3225 */
Angus Grattond8213d02016-05-25 20:56:48 +10003226 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003227
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003228 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003229 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003230 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003231 {
Angus Grattond8213d02016-05-25 20:56:48 +10003232 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003233 }
3234
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003235 /* During a handshake, use the value being negotiated */
3236 if( ssl->session_negotiate != NULL &&
3237 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3238 {
3239 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3240 }
3241
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003242 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003243}
3244#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3245
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003246#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003247size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003248{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003249 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3250 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3251 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3252 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3253 return ( 0 );
3254
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003255 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3256 return( ssl->mtu );
3257
3258 if( ssl->mtu == 0 )
3259 return( ssl->handshake->mtu );
3260
3261 return( ssl->mtu < ssl->handshake->mtu ?
3262 ssl->mtu : ssl->handshake->mtu );
3263}
3264#endif /* MBEDTLS_SSL_PROTO_DTLS */
3265
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003266int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3267{
3268 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3269
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003270#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3271 !defined(MBEDTLS_SSL_PROTO_DTLS)
3272 (void) ssl;
3273#endif
3274
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003275#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003276 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003277
3278 if( max_len > mfl )
3279 max_len = mfl;
3280#endif
3281
3282#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003283 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003284 {
Hanno Becker89490712020-02-05 10:50:12 +00003285 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003286 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3287 const size_t overhead = (size_t) ret;
3288
3289 if( ret < 0 )
3290 return( ret );
3291
3292 if( mtu <= overhead )
3293 {
3294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3295 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3296 }
3297
3298 if( max_len > mtu - overhead )
3299 max_len = mtu - overhead;
3300 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003301#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003302
Hanno Becker0defedb2018-08-10 12:35:02 +01003303#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3304 !defined(MBEDTLS_SSL_PROTO_DTLS)
3305 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003306#endif
3307
3308 return( (int) max_len );
3309}
3310
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003311int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3312{
3313 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3314
3315#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3316 (void) ssl;
3317#endif
3318
3319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3320 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3321
3322 if( max_len > mfl )
3323 max_len = mfl;
3324#endif
3325
3326 return( (int) max_len );
3327}
3328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#if defined(MBEDTLS_X509_CRT_PARSE_C)
3330const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003331{
3332 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003333 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003334
Hanno Beckere6824572019-02-07 13:18:46 +00003335#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003336 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003337#else
3338 return( NULL );
3339#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003344int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3345 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003346{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003347 int ret;
3348
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003349 if( ssl == NULL ||
3350 dst == NULL ||
3351 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003352 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003355 }
3356
Hanno Beckere810bbc2021-05-14 16:01:05 +01003357 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3358 * idempotent: Each session can only be exported once.
3359 *
3360 * (This is in preparation for TLS 1.3 support where we will
3361 * need the ability to export multiple sessions (aka tickets),
3362 * which will be achieved by calling mbedtls_ssl_get_session()
3363 * multiple times until it fails.)
3364 *
3365 * Check whether we have already exported the current session,
3366 * and fail if so.
3367 */
3368 if( ssl->session->exported == 1 )
3369 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3370
3371 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3372 if( ret != 0 )
3373 return( ret );
3374
3375 /* Remember that we've exported the session. */
3376 ssl->session->exported = 1;
3377 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003380
Paul Bakker5121ce52009-01-03 21:22:43 +00003381/*
Hanno Beckera835da52019-05-16 12:39:07 +01003382 * Define ticket header determining Mbed TLS version
3383 * and structure of the ticket.
3384 */
3385
Hanno Becker94ef3b32019-05-16 12:50:45 +01003386/*
Hanno Becker50b59662019-05-28 14:30:45 +01003387 * Define bitflag determining compile-time settings influencing
3388 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003389 */
3390
Hanno Becker50b59662019-05-28 14:30:45 +01003391#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003392#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003393#else
Hanno Becker3e088662019-05-29 11:10:18 +01003394#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003395#endif /* MBEDTLS_HAVE_TIME */
3396
3397#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003398#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003399#else
Hanno Becker3e088662019-05-29 11:10:18 +01003400#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003401#endif /* MBEDTLS_X509_CRT_PARSE_C */
3402
3403#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003404#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003405#else
Hanno Becker3e088662019-05-29 11:10:18 +01003406#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003407#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3408
3409#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003410#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003411#else
Hanno Becker3e088662019-05-29 11:10:18 +01003412#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003413#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3414
Hanno Becker94ef3b32019-05-16 12:50:45 +01003415#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003416#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003417#else
Hanno Becker3e088662019-05-29 11:10:18 +01003418#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003419#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3420
Hanno Becker94ef3b32019-05-16 12:50:45 +01003421#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3422#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3423#else
3424#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3425#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3426
Hanno Becker3e088662019-05-29 11:10:18 +01003427#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3428#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3429#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3430#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003431#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3432#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003433
Hanno Becker50b59662019-05-28 14:30:45 +01003434#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003435 ( (uint16_t) ( \
3436 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3437 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3438 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3439 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003440 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003441 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003442
Hanno Beckerf8787072019-05-16 12:41:07 +01003443static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003444 MBEDTLS_VERSION_MAJOR,
3445 MBEDTLS_VERSION_MINOR,
3446 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003447 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3448 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003449};
Hanno Beckera835da52019-05-16 12:39:07 +01003450
3451/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003452 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003453 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003454 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003455 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003456 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003457 * opaque mbedtls_version[3]; // library version: major, minor, patch
3458 * opaque session_format[2]; // library-version specific 16-bit field
3459 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003460 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003461 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003462 * Note: When updating the format, remember to keep
3463 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003464 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003465 * // In this version, `session_format` determines
3466 * // the setting of those compile-time
3467 * // configuration options which influence
3468 * // the structure of mbedtls_ssl_session.
3469 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003470 * uint8_t minor_ver; // Protocol minor version. Possible values:
3471 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003472 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003473 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003474 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003475 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003476 * serialized_session_tls12 data;
3477 *
3478 * };
3479 *
3480 * } serialized_session;
3481 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003482 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003483
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003484MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003485static int ssl_session_save( const mbedtls_ssl_session *session,
3486 unsigned char omit_header,
3487 unsigned char *buf,
3488 size_t buf_len,
3489 size_t *olen )
3490{
3491 unsigned char *p = buf;
3492 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003493 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003494#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3495 size_t out_len;
3496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3497#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003498 if( session == NULL )
3499 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3500
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003501 if( !omit_header )
3502 {
3503 /*
3504 * Add Mbed TLS version identifier
3505 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003506 used += sizeof( ssl_serialized_session_header );
3507
3508 if( used <= buf_len )
3509 {
3510 memcpy( p, ssl_serialized_session_header,
3511 sizeof( ssl_serialized_session_header ) );
3512 p += sizeof( ssl_serialized_session_header );
3513 }
3514 }
3515
3516 /*
3517 * TLS version identifier
3518 */
3519 used += 1;
3520 if( used <= buf_len )
3521 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003522 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003523 }
3524
3525 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003526 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003527 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003528 {
3529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003530 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003531 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003532 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003533#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3534
Jerry Yu251a12e2022-07-13 15:15:48 +08003535#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3536 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003537 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3538 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003539 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003540 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003541 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003542#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3543
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003544 default:
3545 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3546 }
3547
3548 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003549 if( used > buf_len )
3550 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003551
3552 return( 0 );
3553}
3554
3555/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003556 * Public wrapper for ssl_session_save()
3557 */
3558int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3559 unsigned char *buf,
3560 size_t buf_len,
3561 size_t *olen )
3562{
3563 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3564}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003565
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003566/*
3567 * Deserialize session, see mbedtls_ssl_session_save() for format.
3568 *
3569 * This internal version is wrapped by a public function that cleans up in
3570 * case of error, and has an extra option omit_header.
3571 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003572MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003573static int ssl_session_load( mbedtls_ssl_session *session,
3574 unsigned char omit_header,
3575 const unsigned char *buf,
3576 size_t len )
3577{
3578 const unsigned char *p = buf;
3579 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003580 size_t remaining_len;
3581
3582
3583 if( session == NULL )
3584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003585
3586 if( !omit_header )
3587 {
3588 /*
3589 * Check Mbed TLS version identifier
3590 */
3591
3592 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3594
3595 if( memcmp( p, ssl_serialized_session_header,
3596 sizeof( ssl_serialized_session_header ) ) != 0 )
3597 {
3598 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3599 }
3600 p += sizeof( ssl_serialized_session_header );
3601 }
3602
3603 /*
3604 * TLS version identifier
3605 */
3606 if( 1 > (size_t)( end - p ) )
3607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003608 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003609
3610 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003611 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003612 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003613 {
3614#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003615 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003616 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003617#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3618
Jerry Yu438ddd82022-07-07 06:55:50 +00003619#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3620 case MBEDTLS_SSL_VERSION_TLS1_3:
3621 return( ssl_tls13_session_load( session, p, remaining_len ) );
3622#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3623
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003624 default:
3625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3626 }
3627}
3628
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003629/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003630 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003631 */
3632int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3633 const unsigned char *buf,
3634 size_t len )
3635{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003636 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003637
3638 if( ret != 0 )
3639 mbedtls_ssl_session_free( session );
3640
3641 return( ret );
3642}
3643
3644/*
Paul Bakker1961b702013-01-25 14:49:24 +01003645 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003647MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003648static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3649{
3650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3651
Ronald Cron66dbf912022-02-02 15:33:46 +01003652 /*
3653 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003654 * that were written into the output buffer by the previous handshake step,
3655 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003656 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3657 * We proceed to the next handshake step only when all data from the
3658 * previous one have been sent to the peer, thus we make sure that this is
3659 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3660 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3661 * we have to wait before to go ahead.
3662 * In the case of TLS 1.3, handshake step handlers do not send data to the
3663 * peer. Data are only sent here and through
3664 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003665 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003666 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003667 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3668 return( ret );
3669
3670#if defined(MBEDTLS_SSL_PROTO_DTLS)
3671 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3672 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3673 {
3674 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3675 return( ret );
3676 }
3677#endif /* MBEDTLS_SSL_PROTO_DTLS */
3678
3679 return( ret );
3680}
3681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003683{
Hanno Becker41934dd2021-08-07 19:13:43 +01003684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003685
Hanno Becker41934dd2021-08-07 19:13:43 +01003686 if( ssl == NULL ||
3687 ssl->conf == NULL ||
3688 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003689 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003690 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003692 }
3693
3694 ret = ssl_prepare_handshake_step( ssl );
3695 if( ret != 0 )
3696 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003697
Jerry Yue7047812021-09-13 19:26:39 +08003698 ret = mbedtls_ssl_handle_pending_alert( ssl );
3699 if( ret != 0 )
3700 goto cleanup;
3701
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003702 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3703 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3704 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003708 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3710 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003711
Ronald Cron9f0fba32022-02-10 16:45:15 +01003712 switch( ssl->state )
3713 {
3714 case MBEDTLS_SSL_HELLO_REQUEST:
3715 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003716 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003717 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003718
Ronald Cron9f0fba32022-02-10 16:45:15 +01003719 case MBEDTLS_SSL_CLIENT_HELLO:
3720 ret = mbedtls_ssl_write_client_hello( ssl );
3721 break;
3722
3723 default:
3724#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003725 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003726 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3727 else
3728 ret = mbedtls_ssl_handshake_client_step( ssl );
3729#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3730 ret = mbedtls_ssl_handshake_client_step( ssl );
3731#else
3732 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3733#endif
3734 }
Jerry Yub9930e72021-08-06 17:11:51 +08003735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003736#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003739 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003740#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003741 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003742 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003743#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003744
3745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3746 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3747 ret = mbedtls_ssl_handshake_server_step( ssl );
3748#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003750#endif
3751
Jerry Yue7047812021-09-13 19:26:39 +08003752 if( ret != 0 )
3753 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003754 /* handshake_step return error. And it is same
3755 * with alert_reason.
3756 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003757 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003758 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003759 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003760 goto cleanup;
3761 }
3762 }
3763
3764cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003765 return( ret );
3766}
3767
3768/*
3769 * Perform the SSL handshake
3770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003772{
3773 int ret = 0;
3774
Hanno Beckera817ea42020-10-20 15:20:23 +01003775 /* Sanity checks */
3776
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003777 if( ssl == NULL || ssl->conf == NULL )
3778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3779
Hanno Beckera817ea42020-10-20 15:20:23 +01003780#if defined(MBEDTLS_SSL_PROTO_DTLS)
3781 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3782 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3783 {
3784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3785 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3787 }
3788#endif /* MBEDTLS_SSL_PROTO_DTLS */
3789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003791
Hanno Beckera817ea42020-10-20 15:20:23 +01003792 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003793 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003796
3797 if( ret != 0 )
3798 break;
3799 }
3800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003802
3803 return( ret );
3804}
3805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806#if defined(MBEDTLS_SSL_RENEGOTIATION)
3807#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003808/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003809 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003810 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003811MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003813{
Janos Follath865b3eb2019-12-16 11:46:15 +00003814 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003817
3818 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3820 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003821
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003822 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003823 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003825 return( ret );
3826 }
3827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003829
3830 return( 0 );
3831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003833
3834/*
3835 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 * - any side: calling mbedtls_ssl_renegotiate(),
3837 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3838 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003839 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003840 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003842 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003843int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003844{
Janos Follath865b3eb2019-12-16 11:46:15 +00003845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003848
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003849 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3850 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003851
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003852 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3853 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003857 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003858 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003859 ssl->handshake->out_msg_seq = 1;
3860 else
3861 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003862 }
3863#endif
3864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3866 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003871 return( ret );
3872 }
3873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003875
3876 return( 0 );
3877}
3878
3879/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003880 * Renegotiate current connection on client,
3881 * or request renegotiation on server
3882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003886
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003887 if( ssl == NULL || ssl->conf == NULL )
3888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003891 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003892 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003893 {
Jerry Yu6848a612022-10-27 13:03:26 +08003894 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003898
3899 /* Did we already try/start sending HelloRequest? */
3900 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003902
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003903 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003904 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003908 /*
3909 * On client, either start the renegotiation process or,
3910 * if already in progress, continue the handshake
3911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003913 {
Jerry Yu6848a612022-10-27 13:03:26 +08003914 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003916
Hanno Becker40cdaa12020-02-05 10:48:27 +00003917 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003918 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003920 return( ret );
3921 }
3922 }
3923 else
3924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003928 return( ret );
3929 }
3930 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003932
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003933 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003936
Gilles Peskine9b562d52018-04-25 20:32:43 +02003937void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003938{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003939 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3940
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003941 if( handshake == NULL )
3942 return;
3943
Brett Warrene0edc842021-08-17 09:53:13 +01003944#if defined(MBEDTLS_ECP_C)
3945#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3946 if ( ssl->handshake->group_list_heap_allocated )
3947 mbedtls_free( (void*) handshake->group_list );
3948 handshake->group_list = NULL;
3949#endif /* MBEDTLS_DEPRECATED_REMOVED */
3950#endif /* MBEDTLS_ECP_C */
3951
Ronald Crone68ab4f2022-10-05 12:46:29 +02003952#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003953#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3954 if ( ssl->handshake->sig_algs_heap_allocated )
3955 mbedtls_free( (void*) handshake->sig_algs );
3956 handshake->sig_algs = NULL;
3957#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003958#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3959 if( ssl->handshake->certificate_request_context )
3960 {
3961 mbedtls_free( (void*) handshake->certificate_request_context );
3962 }
3963#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003964#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003965
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003966#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3967 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3968 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003969 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003970 handshake->async_in_progress = 0;
3971 }
3972#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3973
Andrzej Kurek25f27152022-08-17 16:09:31 -04003974#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003975#if defined(MBEDTLS_USE_PSA_CRYPTO)
3976 psa_hash_abort( &handshake->fin_sha256_psa );
3977#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003978 mbedtls_sha256_free( &handshake->fin_sha256 );
3979#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003980#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003981#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003982#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003983 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003984#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003985 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003986#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003987#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989#if defined(MBEDTLS_DHM_C)
3990 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003991#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003992#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003994#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01003995
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003996#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02003997#if defined(MBEDTLS_USE_PSA_CRYPTO)
3998 psa_pake_abort( &handshake->psa_pake_ctx );
3999 psa_destroy_key( handshake->psa_pake_password );
4000 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4001#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004002 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004003#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004004#if defined(MBEDTLS_SSL_CLI_C)
4005 mbedtls_free( handshake->ecjpake_cache );
4006 handshake->ecjpake_cache = NULL;
4007 handshake->ecjpake_cache_len = 0;
4008#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004009#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004010
Janos Follath4ae5c292016-02-10 11:27:43 +00004011#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4012 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004013 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004015#endif
4016
Ronald Cron73fe8df2022-10-05 14:31:43 +02004017#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004018#if defined(MBEDTLS_USE_PSA_CRYPTO)
4019 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4020 {
4021 /* The maintenance of the external PSK key slot is the
4022 * user's responsibility. */
4023 if( ssl->handshake->psk_opaque_is_internal )
4024 {
4025 psa_destroy_key( ssl->handshake->psk_opaque );
4026 ssl->handshake->psk_opaque_is_internal = 0;
4027 }
4028 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4029 }
Neil Armstronge952a302022-05-03 10:22:14 +02004030#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004031 if( handshake->psk != NULL )
4032 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004033 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004034 mbedtls_free( handshake->psk );
4035 }
Neil Armstronge952a302022-05-03 10:22:14 +02004036#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004037#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4040 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004041 /*
4042 * Free only the linked list wrapper, not the keys themselves
4043 * since the belong to the SNI callback
4044 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004045 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004047
Gilles Peskineeccd8882020-03-10 12:19:08 +01004048#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004049 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004050 if( handshake->ecrs_peer_cert != NULL )
4051 {
4052 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4053 mbedtls_free( handshake->ecrs_peer_cert );
4054 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004055#endif
4056
Hanno Becker75173122019-02-06 16:18:31 +00004057#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4058 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4059 mbedtls_pk_free( &handshake->peer_pubkey );
4060#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4061
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004062#if defined(MBEDTLS_SSL_CLI_C) && \
4063 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4064 mbedtls_free( handshake->cookie );
4065#endif /* MBEDTLS_SSL_CLI_C &&
4066 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004067
4068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004069 mbedtls_ssl_flight_free( handshake->flight );
4070 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004071#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004072
Ronald Cronf12b81d2022-03-15 10:42:41 +01004073#if defined(MBEDTLS_ECDH_C) && \
4074 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004075 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004076 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004077#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4078
Ronald Cron6f135e12021-12-08 16:57:54 +01004079#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004080 mbedtls_ssl_transform_free( handshake->transform_handshake );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004081 mbedtls_free( handshake->transform_handshake );
4082#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yua1a568c2021-11-09 10:17:21 +08004083 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004084 mbedtls_free( handshake->transform_earlydata );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004085#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004086#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004087
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004088
4089#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4090 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4091 * processes datagrams and the fact that a datagram is allowed to have
4092 * several records in it, it is possible that the I/O buffers are not
4093 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004094 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4095 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004096#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004097
Jerry Yuba9c7272021-10-30 11:54:10 +08004098 /* mbedtls_platform_zeroize MUST be last one in this function */
4099 mbedtls_platform_zeroize( handshake,
4100 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004101}
4102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004104{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004105 if( session == NULL )
4106 return;
4107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004109 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004110#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004111
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004112#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004113#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4114 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004115 mbedtls_free( session->hostname );
4116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004118#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004119
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004120 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004121}
4122
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004123#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004124
4125#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4126#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4127#else
4128#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4129#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4130
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004131#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004132
4133#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4134#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4135#else
4136#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4137#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4138
4139#if defined(MBEDTLS_SSL_ALPN)
4140#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4141#else
4142#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4143#endif /* MBEDTLS_SSL_ALPN */
4144
4145#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4146#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4147#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4148#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4149
4150#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4151 ( (uint32_t) ( \
4152 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4153 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4154 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4155 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4156 0u ) )
4157
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004158static unsigned char ssl_serialized_context_header[] = {
4159 MBEDTLS_VERSION_MAJOR,
4160 MBEDTLS_VERSION_MINOR,
4161 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004162 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4163 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4164 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4165 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4166 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004167};
4168
Paul Bakker5121ce52009-01-03 21:22:43 +00004169/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004170 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004171 *
4172 * The format of the serialized data is:
4173 * (in the presentation language of TLS, RFC 8446 section 3)
4174 *
4175 * // header
4176 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004177 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004178 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004179 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004180 * Note: When updating the format, remember to keep these
4181 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004182 *
4183 * // session sub-structure
4184 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4185 * // transform sub-structure
4186 * uint8 random[64]; // ServerHello.random+ClientHello.random
4187 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4188 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4189 * // fields from ssl_context
4190 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4191 * uint64 in_window_top; // DTLS: last validated record seq_num
4192 * uint64 in_window; // DTLS: bitmask for replay protection
4193 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4194 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4195 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4196 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4197 *
4198 * Note that many fields of the ssl_context or sub-structures are not
4199 * serialized, as they fall in one of the following categories:
4200 *
4201 * 1. forced value (eg in_left must be 0)
4202 * 2. pointer to dynamically-allocated memory (eg session, transform)
4203 * 3. value can be re-derived from other data (eg session keys from MS)
4204 * 4. value was temporary (eg content of input buffer)
4205 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004206 */
4207int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4208 unsigned char *buf,
4209 size_t buf_len,
4210 size_t *olen )
4211{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004212 unsigned char *p = buf;
4213 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004214 size_t session_len;
4215 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004216
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004217 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004218 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4219 * this function's documentation.
4220 *
4221 * These are due to assumptions/limitations in the implementation. Some of
4222 * them are likely to stay (no handshake in progress) some might go away
4223 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004224 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004225 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004226 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004227 {
4228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004230 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004231 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004235 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004236 /* Double-check that sub-structures are indeed ready */
4237 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004241 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004242 /* There must be no pending incoming or outgoing data */
4243 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004244 {
4245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004247 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004248 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004249 {
4250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004252 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004253 /* Protocol must be DLTS, not TLS */
4254 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004255 {
4256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004258 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004259 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004260 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004261 {
4262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004264 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004265 /* We must be using an AEAD ciphersuite */
4266 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004267 {
4268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004270 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004271 /* Renegotiation must not be enabled */
4272#if defined(MBEDTLS_SSL_RENEGOTIATION)
4273 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004274 {
4275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004276 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004277 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004278#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004279
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004280 /*
4281 * Version and format identifier
4282 */
4283 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004284
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004285 if( used <= buf_len )
4286 {
4287 memcpy( p, ssl_serialized_context_header,
4288 sizeof( ssl_serialized_context_header ) );
4289 p += sizeof( ssl_serialized_context_header );
4290 }
4291
4292 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004293 * Session (length + data)
4294 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004295 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004296 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4297 return( ret );
4298
4299 used += 4 + session_len;
4300 if( used <= buf_len )
4301 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004302 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4303 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004304
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004305 ret = ssl_session_save( ssl->session, 1,
4306 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004307 if( ret != 0 )
4308 return( ret );
4309
4310 p += session_len;
4311 }
4312
4313 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004314 * Transform
4315 */
4316 used += sizeof( ssl->transform->randbytes );
4317 if( used <= buf_len )
4318 {
4319 memcpy( p, ssl->transform->randbytes,
4320 sizeof( ssl->transform->randbytes ) );
4321 p += sizeof( ssl->transform->randbytes );
4322 }
4323
4324#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4325 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4326 if( used <= buf_len )
4327 {
4328 *p++ = ssl->transform->in_cid_len;
4329 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4330 p += ssl->transform->in_cid_len;
4331
4332 *p++ = ssl->transform->out_cid_len;
4333 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4334 p += ssl->transform->out_cid_len;
4335 }
4336#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4337
4338 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004339 * Saved fields from top-level ssl_context structure
4340 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004341 used += 4;
4342 if( used <= buf_len )
4343 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004344 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4345 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004346 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004347
4348#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4349 used += 16;
4350 if( used <= buf_len )
4351 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004352 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4353 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004354
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004355 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4356 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004357 }
4358#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4359
4360#if defined(MBEDTLS_SSL_PROTO_DTLS)
4361 used += 1;
4362 if( used <= buf_len )
4363 {
4364 *p++ = ssl->disable_datagram_packing;
4365 }
4366#endif /* MBEDTLS_SSL_PROTO_DTLS */
4367
Jerry Yuae0b2e22021-10-08 15:21:19 +08004368 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004369 if( used <= buf_len )
4370 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004371 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4372 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004373 }
4374
4375#if defined(MBEDTLS_SSL_PROTO_DTLS)
4376 used += 2;
4377 if( used <= buf_len )
4378 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004379 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4380 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004381 }
4382#endif /* MBEDTLS_SSL_PROTO_DTLS */
4383
4384#if defined(MBEDTLS_SSL_ALPN)
4385 {
4386 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004387 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004388 : 0;
4389
4390 used += 1 + alpn_len;
4391 if( used <= buf_len )
4392 {
4393 *p++ = alpn_len;
4394
4395 if( ssl->alpn_chosen != NULL )
4396 {
4397 memcpy( p, ssl->alpn_chosen, alpn_len );
4398 p += alpn_len;
4399 }
4400 }
4401 }
4402#endif /* MBEDTLS_SSL_ALPN */
4403
4404 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004405 * Done
4406 */
4407 *olen = used;
4408
4409 if( used > buf_len )
4410 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004411
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004412 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4413
Hanno Becker43aefe22020-02-05 10:44:56 +00004414 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004415}
4416
4417/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004418 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004419 *
4420 * This internal version is wrapped by a public function that cleans up in
4421 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004422 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004423MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004424static int ssl_context_load( mbedtls_ssl_context *ssl,
4425 const unsigned char *buf,
4426 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004427{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004428 const unsigned char *p = buf;
4429 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004430 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004432#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004433 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004434#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004435
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004436 /*
4437 * The context should have been freshly setup or reset.
4438 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004439 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004440 * renegotiating, or if the user mistakenly loaded a session first.)
4441 */
4442 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4443 ssl->session != NULL )
4444 {
4445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4446 }
4447
4448 /*
4449 * We can't check that the config matches the initial one, but we can at
4450 * least check it matches the requirements for serializing.
4451 */
4452 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004453 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4454 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004455#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004456 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004457#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004458 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004459 {
4460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4461 }
4462
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004463 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4464
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004465 /*
4466 * Check version identifier
4467 */
4468 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4470
4471 if( memcmp( p, ssl_serialized_context_header,
4472 sizeof( ssl_serialized_context_header ) ) != 0 )
4473 {
4474 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4475 }
4476 p += sizeof( ssl_serialized_context_header );
4477
4478 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004479 * Session
4480 */
4481 if( (size_t)( end - p ) < 4 )
4482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4483
4484 session_len = ( (size_t) p[0] << 24 ) |
4485 ( (size_t) p[1] << 16 ) |
4486 ( (size_t) p[2] << 8 ) |
4487 ( (size_t) p[3] );
4488 p += 4;
4489
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004490 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004491 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004492 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004493 ssl->session_in = ssl->session;
4494 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004495 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004496
4497 if( (size_t)( end - p ) < session_len )
4498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4499
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004500 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004501 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004502 {
4503 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004504 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004505 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004506
4507 p += session_len;
4508
4509 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004510 * Transform
4511 */
4512
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004513 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004514 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004515 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004516 ssl->transform_in = ssl->transform;
4517 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004518 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004519
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004521 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4522 if( prf_func == NULL )
4523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4524
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004525 /* Read random bytes and populate structure */
4526 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004528
Hanno Beckerbd257552021-03-22 06:59:27 +00004529 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004530 ssl->session->ciphersuite,
4531 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004532#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004533 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004534#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004535 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004536 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004537 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004538 ssl->conf->endpoint,
4539 ssl );
4540 if( ret != 0 )
4541 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004543 p += sizeof( ssl->transform->randbytes );
4544
4545#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4546 /* Read connection IDs and store them */
4547 if( (size_t)( end - p ) < 1 )
4548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4549
4550 ssl->transform->in_cid_len = *p++;
4551
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004552 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4554
4555 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4556 p += ssl->transform->in_cid_len;
4557
4558 ssl->transform->out_cid_len = *p++;
4559
4560 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4562
4563 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4564 p += ssl->transform->out_cid_len;
4565#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4566
4567 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004568 * Saved fields from top-level ssl_context structure
4569 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004570 if( (size_t)( end - p ) < 4 )
4571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4572
4573 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4574 ( (uint32_t) p[1] << 16 ) |
4575 ( (uint32_t) p[2] << 8 ) |
4576 ( (uint32_t) p[3] );
4577 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004578
4579#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4580 if( (size_t)( end - p ) < 16 )
4581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4582
4583 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4584 ( (uint64_t) p[1] << 48 ) |
4585 ( (uint64_t) p[2] << 40 ) |
4586 ( (uint64_t) p[3] << 32 ) |
4587 ( (uint64_t) p[4] << 24 ) |
4588 ( (uint64_t) p[5] << 16 ) |
4589 ( (uint64_t) p[6] << 8 ) |
4590 ( (uint64_t) p[7] );
4591 p += 8;
4592
4593 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4594 ( (uint64_t) p[1] << 48 ) |
4595 ( (uint64_t) p[2] << 40 ) |
4596 ( (uint64_t) p[3] << 32 ) |
4597 ( (uint64_t) p[4] << 24 ) |
4598 ( (uint64_t) p[5] << 16 ) |
4599 ( (uint64_t) p[6] << 8 ) |
4600 ( (uint64_t) p[7] );
4601 p += 8;
4602#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4603
4604#if defined(MBEDTLS_SSL_PROTO_DTLS)
4605 if( (size_t)( end - p ) < 1 )
4606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4607
4608 ssl->disable_datagram_packing = *p++;
4609#endif /* MBEDTLS_SSL_PROTO_DTLS */
4610
Jerry Yud9a94fe2021-09-28 18:58:59 +08004611 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004613 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4614 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004615
4616#if defined(MBEDTLS_SSL_PROTO_DTLS)
4617 if( (size_t)( end - p ) < 2 )
4618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4619
4620 ssl->mtu = ( p[0] << 8 ) | p[1];
4621 p += 2;
4622#endif /* MBEDTLS_SSL_PROTO_DTLS */
4623
4624#if defined(MBEDTLS_SSL_ALPN)
4625 {
4626 uint8_t alpn_len;
4627 const char **cur;
4628
4629 if( (size_t)( end - p ) < 1 )
4630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4631
4632 alpn_len = *p++;
4633
4634 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4635 {
4636 /* alpn_chosen should point to an item in the configured list */
4637 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4638 {
4639 if( strlen( *cur ) == alpn_len &&
4640 memcmp( p, cur, alpn_len ) == 0 )
4641 {
4642 ssl->alpn_chosen = *cur;
4643 break;
4644 }
4645 }
4646 }
4647
4648 /* can only happen on conf mismatch */
4649 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4651
4652 p += alpn_len;
4653 }
4654#endif /* MBEDTLS_SSL_ALPN */
4655
4656 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004657 * Forced fields from top-level ssl_context structure
4658 *
4659 * Most of them already set to the correct value by mbedtls_ssl_init() and
4660 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4661 */
4662 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004663 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004664
Hanno Becker361b10d2019-08-30 10:42:49 +01004665 /* Adjust pointers for header fields of outgoing records to
4666 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004667 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004668
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004669#if defined(MBEDTLS_SSL_PROTO_DTLS)
4670 ssl->in_epoch = 1;
4671#endif
4672
4673 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4674 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004675 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4676 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004677 if( ssl->handshake != NULL )
4678 {
4679 mbedtls_ssl_handshake_free( ssl );
4680 mbedtls_free( ssl->handshake );
4681 ssl->handshake = NULL;
4682 }
4683
4684 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004685 * Done - should have consumed entire buffer
4686 */
4687 if( p != end )
4688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004689
4690 return( 0 );
4691}
4692
4693/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004694 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004695 */
4696int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4697 const unsigned char *buf,
4698 size_t len )
4699{
4700 int ret = ssl_context_load( context, buf, len );
4701
4702 if( ret != 0 )
4703 mbedtls_ssl_free( context );
4704
4705 return( ret );
4706}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004707#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004708
4709/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004710 * Free an SSL context
4711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004713{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004714 if( ssl == NULL )
4715 return;
4716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004718
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004719 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 {
sander-visserb8aa2072020-05-06 22:05:13 +02004721#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4722 size_t out_buf_len = ssl->out_buf_len;
4723#else
4724 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4725#endif
4726
Darryl Greenb33cc762019-11-28 14:29:44 +00004727 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004729 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004730 }
4731
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004732 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004733 {
sander-visserb8aa2072020-05-06 22:05:13 +02004734#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4735 size_t in_buf_len = ssl->in_buf_len;
4736#else
4737 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4738#endif
4739
Darryl Greenb33cc762019-11-28 14:29:44 +00004740 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004742 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 }
4744
Paul Bakker48916f92012-09-16 19:57:18 +00004745 if( ssl->transform )
4746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 mbedtls_ssl_transform_free( ssl->transform );
4748 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004749 }
4750
4751 if( ssl->handshake )
4752 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004753 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4755 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 mbedtls_free( ssl->handshake );
4758 mbedtls_free( ssl->transform_negotiate );
4759 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004760 }
4761
Ronald Cron6f135e12021-12-08 16:57:54 +01004762#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004763 mbedtls_ssl_transform_free( ssl->transform_application );
4764 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004765#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004766
Paul Bakkerc0463502013-02-14 11:19:38 +01004767 if( ssl->session )
4768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 mbedtls_ssl_session_free( ssl->session );
4770 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004771 }
4772
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004773#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004774 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004775 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004776 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004779#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004780
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004781#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004783#endif
4784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004786
Paul Bakker86f04f42013-02-14 11:20:09 +01004787 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004788 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004789}
4790
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004791/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004792 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004793 */
4794void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4795{
4796 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4797}
4798
Gilles Peskineae270bf2021-06-02 00:05:29 +02004799/* The selection should be the same as mbedtls_x509_crt_profile_default in
4800 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004801 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004802 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004803 * about this list.
4804 */
Brett Warrene0edc842021-08-17 09:53:13 +01004805static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004806#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004807 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004808#endif
4809#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004810 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004811#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004812#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004813 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004814#endif
4815#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004816 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004817#endif
4818#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004819 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004820#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004821#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004822 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004823#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004824#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004825 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004826#endif
4827#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004828 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004829#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004830 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004831};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004832
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004833static int ssl_preset_suiteb_ciphersuites[] = {
4834 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4835 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4836 0
4837};
4838
Ronald Crone68ab4f2022-10-05 12:46:29 +02004839#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004840
Jerry Yu909df7b2022-01-22 11:56:27 +08004841/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004842 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004843 * rules SHOULD be upheld.
4844 * - No duplicate entries.
4845 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004846 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004847 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004848 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004849static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004850
Andrzej Kurek25f27152022-08-17 16:09:31 -04004851#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 +08004852 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4853 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004854#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004855 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004856
Andrzej Kurek25f27152022-08-17 16:09:31 -04004857#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 +08004858 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4859 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004860#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004861 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4862
Andrzej Kurek25f27152022-08-17 16:09:31 -04004863#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 +08004864 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4865 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004866#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004867 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004868
Andrzej Kurek25f27152022-08-17 16:09:31 -04004869#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 +08004870 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004871#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 +08004872
Andrzej Kurek25f27152022-08-17 16:09:31 -04004873#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 +08004874 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004875#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 +08004876
Andrzej Kurek25f27152022-08-17 16:09:31 -04004877#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 +08004878 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004879#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 +08004880
Andrzej Kurek25f27152022-08-17 16:09:31 -04004881#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 +08004882 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4883#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4884
Andrzej Kurek25f27152022-08-17 16:09:31 -04004885#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 +08004886 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4887#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4888
Andrzej Kurek25f27152022-08-17 16:09:31 -04004889#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 +08004890 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4891#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4892
Gabor Mezei15b95a62022-05-09 16:37:58 +02004893 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004894};
4895
4896/* NOTICE: see above */
4897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4898static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004899#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004900#if defined(MBEDTLS_ECDSA_C)
4901 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004902#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004903#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4904 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4905#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004906#if defined(MBEDTLS_RSA_C)
4907 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4908#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004909#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004910#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004911#if defined(MBEDTLS_ECDSA_C)
4912 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004913#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004914#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4915 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4916#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004917#if defined(MBEDTLS_RSA_C)
4918 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4919#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004920#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004921#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004922#if defined(MBEDTLS_ECDSA_C)
4923 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004924#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004925#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4926 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4927#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004928#if defined(MBEDTLS_RSA_C)
4929 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4930#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004931#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004932 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004933};
4934#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4935/* NOTICE: see above */
4936static uint16_t ssl_preset_suiteb_sig_algs[] = {
4937
Andrzej Kurek25f27152022-08-17 16:09:31 -04004938#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 +08004939 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4940 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004941#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004942 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4943
Andrzej Kurek25f27152022-08-17 16:09:31 -04004944#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 +08004945 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4946 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004947#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004948 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004949
Andrzej Kurek25f27152022-08-17 16:09:31 -04004950#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 +08004951 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004952#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 +08004953
Andrzej Kurek25f27152022-08-17 16:09:31 -04004954#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 +08004955 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004956#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004957
Gabor Mezei15b95a62022-05-09 16:37:58 +02004958 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004959};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004960
Jerry Yu909df7b2022-01-22 11:56:27 +08004961/* NOTICE: see above */
4962#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4963static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004964#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004965#if defined(MBEDTLS_ECDSA_C)
4966 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004967#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004968#if defined(MBEDTLS_RSA_C)
4969 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4970#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004971#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004972#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004973#if defined(MBEDTLS_ECDSA_C)
4974 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004975#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004976#if defined(MBEDTLS_RSA_C)
4977 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4978#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004979#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004980 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004981};
Jerry Yu909df7b2022-01-22 11:56:27 +08004982#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4983
Ronald Crone68ab4f2022-10-05 12:46:29 +02004984#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004985
Brett Warrene0edc842021-08-17 09:53:13 +01004986static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004987#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004988 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004989#endif
4990#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004991 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004992#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004993 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004994};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004995
Ronald Crone68ab4f2022-10-05 12:46:29 +02004996#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004997/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004998 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004999MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005000static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005001{
5002 size_t i, j;
5003 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005004
Gabor Mezei15b95a62022-05-09 16:37:58 +02005005 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005006 {
Jerry Yuf377d642022-01-25 10:43:59 +08005007 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005008 {
Jerry Yuf377d642022-01-25 10:43:59 +08005009 if( sig_algs[i] != sig_algs[j] )
5010 continue;
5011 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5012 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5013 sig_algs[i], j, i );
5014 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005015 }
5016 }
5017 return( ret );
5018}
5019
Ronald Crone68ab4f2022-10-05 12:46:29 +02005020#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005021
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005022/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005023 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005024 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005025int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005026 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005027{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005028#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005030#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005031
Ronald Crone68ab4f2022-10-05 12:46:29 +02005032#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005033 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005034 {
5035 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5036 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5037 }
5038
Jerry Yuf377d642022-01-25 10:43:59 +08005039 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005040 {
5041 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5042 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5043 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005044
5045#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005046 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005047 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005048 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005049 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5050 }
5051
Jerry Yuf377d642022-01-25 10:43:59 +08005052 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005053 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005054 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005055 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5056 }
5057#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005058#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005059
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005060 /* Use the functions here so that they are covered in tests,
5061 * but otherwise access member directly for efficiency */
5062 mbedtls_ssl_conf_endpoint( conf, endpoint );
5063 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005064
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005065 /*
5066 * Things that are common to all presets
5067 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005068#if defined(MBEDTLS_SSL_CLI_C)
5069 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5070 {
5071 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5072#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5073 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5074#endif
5075 }
5076#endif
5077
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005078#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5079 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5080#endif
5081
5082#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5083 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5084#endif
5085
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005086#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005087 conf->f_cookie_write = ssl_cookie_write_dummy;
5088 conf->f_cookie_check = ssl_cookie_check_dummy;
5089#endif
5090
5091#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5092 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5093#endif
5094
Janos Follath088ce432017-04-10 12:42:31 +01005095#if defined(MBEDTLS_SSL_SRV_C)
5096 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005097 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005098#endif
5099
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005100#if defined(MBEDTLS_SSL_PROTO_DTLS)
5101 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5102 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5103#endif
5104
5105#if defined(MBEDTLS_SSL_RENEGOTIATION)
5106 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005107 memset( conf->renego_period, 0x00, 2 );
5108 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005109#endif
5110
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005111#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005112 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5113 {
5114 const unsigned char dhm_p[] =
5115 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5116 const unsigned char dhm_g[] =
5117 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005118
Hanno Beckere2defad2021-07-24 05:59:17 +01005119 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5120 dhm_p, sizeof( dhm_p ),
5121 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5122 {
5123 return( ret );
5124 }
5125 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005126#endif
5127
Ronald Cron6f135e12021-12-08 16:57:54 +01005128#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005129#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005130 mbedtls_ssl_conf_new_session_tickets(
5131 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5132#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005133 /*
5134 * Allow all TLS 1.3 key exchange modes by default.
5135 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005136 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005137#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005138
XiaokangQian4d3a6042022-04-21 13:46:17 +00005139 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5140 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005141#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005142 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005143 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005144#else
XiaokangQian060d8672022-04-21 09:24:56 +00005145 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005146#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005147 }
5148 else
5149 {
5150#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5151 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5152 {
5153 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5154 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5155 }
5156 else
5157 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5158 {
5159 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5160 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5161 }
5162#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5163 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5164 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5165#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5166 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5167 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5168#else
5169 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5170#endif
5171 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005172
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005173 /*
5174 * Preset-specific defaults
5175 */
5176 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005177 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005178 /*
5179 * NSA Suite B
5180 */
5181 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005182
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005183 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005184
5185#if defined(MBEDTLS_X509_CRT_PARSE_C)
5186 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005187#endif
5188
Ronald Crone68ab4f2022-10-05 12:46:29 +02005189#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005190#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5191 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5192 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5193 else
5194#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5195 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005196#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005197
Brett Warrene0edc842021-08-17 09:53:13 +01005198#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5199 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005200#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005201 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005202 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005203
5204 /*
5205 * Default
5206 */
5207 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005208
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005209 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005210
5211#if defined(MBEDTLS_X509_CRT_PARSE_C)
5212 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5213#endif
5214
Ronald Crone68ab4f2022-10-05 12:46:29 +02005215#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005216#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5217 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5218 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5219 else
5220#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5221 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005222#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005223
Brett Warrene0edc842021-08-17 09:53:13 +01005224#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5225 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005226#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005227 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005228
5229#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5230 conf->dhm_min_bitlen = 1024;
5231#endif
5232 }
5233
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005234 return( 0 );
5235}
5236
5237/*
5238 * Free mbedtls_ssl_config
5239 */
5240void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5241{
5242#if defined(MBEDTLS_DHM_C)
5243 mbedtls_mpi_free( &conf->dhm_P );
5244 mbedtls_mpi_free( &conf->dhm_G );
5245#endif
5246
Ronald Cron73fe8df2022-10-05 14:31:43 +02005247#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005248#if defined(MBEDTLS_USE_PSA_CRYPTO)
5249 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5250 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005251 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5252 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005253#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005254 if( conf->psk != NULL )
5255 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005256 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005257 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005258 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005259 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005260 }
5261
5262 if( conf->psk_identity != NULL )
5263 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005264 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005265 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005266 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005267 conf->psk_identity_len = 0;
5268 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005269#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005270
5271#if defined(MBEDTLS_X509_CRT_PARSE_C)
5272 ssl_key_cert_free( conf->key_cert );
5273#endif
5274
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005275 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005276}
5277
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005278#if defined(MBEDTLS_PK_C) && \
5279 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005280/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285#if defined(MBEDTLS_RSA_C)
5286 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5287 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005288#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289#if defined(MBEDTLS_ECDSA_C)
5290 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5291 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005294}
5295
Hanno Becker7e5437a2017-04-28 17:15:26 +01005296unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5297{
5298 switch( type ) {
5299 case MBEDTLS_PK_RSA:
5300 return( MBEDTLS_SSL_SIG_RSA );
5301 case MBEDTLS_PK_ECDSA:
5302 case MBEDTLS_PK_ECKEY:
5303 return( MBEDTLS_SSL_SIG_ECDSA );
5304 default:
5305 return( MBEDTLS_SSL_SIG_ANON );
5306 }
5307}
5308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005310{
5311 switch( sig )
5312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313#if defined(MBEDTLS_RSA_C)
5314 case MBEDTLS_SSL_SIG_RSA:
5315 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005316#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317#if defined(MBEDTLS_ECDSA_C)
5318 case MBEDTLS_SSL_SIG_ECDSA:
5319 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005320#endif
5321 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005323 }
5324}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005325#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005326
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005327/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005328 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005331{
5332 switch( hash )
5333 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005334#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335 case MBEDTLS_SSL_HASH_MD5:
5336 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005337#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005338#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 case MBEDTLS_SSL_HASH_SHA1:
5340 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005341#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005342#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 case MBEDTLS_SSL_HASH_SHA224:
5344 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005345#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005346#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 case MBEDTLS_SSL_HASH_SHA256:
5348 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005349#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005350#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 case MBEDTLS_SSL_HASH_SHA384:
5352 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005353#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005354#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 case MBEDTLS_SSL_HASH_SHA512:
5356 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005357#endif
5358 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005360 }
5361}
5362
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005363/*
5364 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5365 */
5366unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5367{
5368 switch( md )
5369 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005370#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005371 case MBEDTLS_MD_MD5:
5372 return( MBEDTLS_SSL_HASH_MD5 );
5373#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005374#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005375 case MBEDTLS_MD_SHA1:
5376 return( MBEDTLS_SSL_HASH_SHA1 );
5377#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005378#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005379 case MBEDTLS_MD_SHA224:
5380 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005381#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005382#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005383 case MBEDTLS_MD_SHA256:
5384 return( MBEDTLS_SSL_HASH_SHA256 );
5385#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005386#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005387 case MBEDTLS_MD_SHA384:
5388 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005389#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005390#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005391 case MBEDTLS_MD_SHA512:
5392 return( MBEDTLS_SSL_HASH_SHA512 );
5393#endif
5394 default:
5395 return( MBEDTLS_SSL_HASH_NONE );
5396 }
5397}
5398
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005399/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005400 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005401 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005402 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005403int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005404{
Brett Warrene0edc842021-08-17 09:53:13 +01005405 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005406
Brett Warrene0edc842021-08-17 09:53:13 +01005407 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005408 return( -1 );
5409
Brett Warrene0edc842021-08-17 09:53:13 +01005410 for( ; *group_list != 0; group_list++ )
5411 {
5412 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005413 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005414 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005415
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005416 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005417}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005418
5419#if defined(MBEDTLS_ECP_C)
5420/*
5421 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5422 */
5423int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5424{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005425 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005426 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005427
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005428 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005429 return -1;
5430
5431 uint16_t tls_id = grp_info->tls_id;
5432
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005433 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5434}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005435#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437#if defined(MBEDTLS_X509_CRT_PARSE_C)
5438int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5439 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005440 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005441 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005442{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005443 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005444 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005445 const char *ext_oid;
5446 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005449 {
5450 /* Server part of the key exchange */
5451 switch( ciphersuite->key_exchange )
5452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 case MBEDTLS_KEY_EXCHANGE_RSA:
5454 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005455 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005456 break;
5457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5459 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5460 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5461 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005462 break;
5463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5465 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005466 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005467 break;
5468
5469 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 case MBEDTLS_KEY_EXCHANGE_NONE:
5471 case MBEDTLS_KEY_EXCHANGE_PSK:
5472 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5473 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005474 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005475 usage = 0;
5476 }
5477 }
5478 else
5479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5481 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005482 }
5483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005485 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005486 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005487 ret = -1;
5488 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5493 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005494 }
5495 else
5496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5498 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005499 }
5500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005502 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005503 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005504 ret = -1;
5505 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005506
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005507 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005510
Jerry Yu148165c2021-09-24 23:20:59 +08005511#if defined(MBEDTLS_USE_PSA_CRYPTO)
5512int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5513 const mbedtls_md_type_t md,
5514 unsigned char *dst,
5515 size_t dst_len,
5516 size_t *olen )
5517{
Ronald Cronf6893e12022-01-07 22:09:01 +01005518 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5519 psa_hash_operation_t *hash_operation_to_clone;
5520 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5521
Jerry Yu148165c2021-09-24 23:20:59 +08005522 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005523
5524 switch( md )
5525 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005526#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005527 case MBEDTLS_MD_SHA384:
5528 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5529 break;
5530#endif
5531
Andrzej Kurek25f27152022-08-17 16:09:31 -04005532#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005533 case MBEDTLS_MD_SHA256:
5534 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5535 break;
5536#endif
5537
5538 default:
5539 goto exit;
5540 }
5541
5542 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5543 if( status != PSA_SUCCESS )
5544 goto exit;
5545
5546 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5547 if( status != PSA_SUCCESS )
5548 goto exit;
5549
5550exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005551#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5552 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5553 (void) ssl;
5554#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005555 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005556}
5557#else /* MBEDTLS_USE_PSA_CRYPTO */
5558
Andrzej Kurek25f27152022-08-17 16:09:31 -04005559#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005560MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005561static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5562 unsigned char *dst,
5563 size_t dst_len,
5564 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005565{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005566 int ret;
5567 mbedtls_sha512_context sha512;
5568
5569 if( dst_len < 48 )
5570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5571
5572 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005573 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005574
5575 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5576 {
5577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5578 goto exit;
5579 }
5580
5581 *olen = 48;
5582
5583exit:
5584
5585 mbedtls_sha512_free( &sha512 );
5586 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005587}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005588#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005589
Andrzej Kurek25f27152022-08-17 16:09:31 -04005590#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005591MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005592static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5593 unsigned char *dst,
5594 size_t dst_len,
5595 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005596{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005597 int ret;
5598 mbedtls_sha256_context sha256;
5599
5600 if( dst_len < 32 )
5601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5602
5603 mbedtls_sha256_init( &sha256 );
5604 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005605
Jerry Yu24c0ec32021-09-09 14:21:07 +08005606 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5607 {
5608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5609 goto exit;
5610 }
5611
5612 *olen = 32;
5613
5614exit:
5615
5616 mbedtls_sha256_free( &sha256 );
5617 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005618}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005619#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005620
Jerry Yu000f9762021-09-14 11:12:51 +08005621int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5622 const mbedtls_md_type_t md,
5623 unsigned char *dst,
5624 size_t dst_len,
5625 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005626{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005627 switch( md )
5628 {
5629
Andrzej Kurek25f27152022-08-17 16:09:31 -04005630#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005631 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005632 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005633#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005634
Andrzej Kurek25f27152022-08-17 16:09:31 -04005635#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005636 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005637 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005638#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005639
5640 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005641#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5642 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5643 (void) ssl;
5644 (void) dst;
5645 (void) dst_len;
5646 (void) olen;
5647#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005648 break;
5649 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005651}
XiaokangQian647719a2021-12-07 09:16:29 +00005652
Jerry Yu148165c2021-09-24 23:20:59 +08005653#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005654
Ronald Crone68ab4f2022-10-05 12:46:29 +02005655#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005656/* mbedtls_ssl_parse_sig_alg_ext()
5657 *
5658 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5659 * value (TLS 1.3 RFC8446):
5660 * enum {
5661 * ....
5662 * ecdsa_secp256r1_sha256( 0x0403 ),
5663 * ecdsa_secp384r1_sha384( 0x0503 ),
5664 * ecdsa_secp521r1_sha512( 0x0603 ),
5665 * ....
5666 * } SignatureScheme;
5667 *
5668 * struct {
5669 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5670 * } SignatureSchemeList;
5671 *
5672 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5673 * value (TLS 1.2 RFC5246):
5674 * enum {
5675 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5676 * sha512(6), (255)
5677 * } HashAlgorithm;
5678 *
5679 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5680 * SignatureAlgorithm;
5681 *
5682 * struct {
5683 * HashAlgorithm hash;
5684 * SignatureAlgorithm signature;
5685 * } SignatureAndHashAlgorithm;
5686 *
5687 * SignatureAndHashAlgorithm
5688 * supported_signature_algorithms<2..2^16-2>;
5689 *
5690 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5691 * generalization of the TLS 1.2 signature algorithm extension.
5692 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5693 * `SignatureScheme` field of TLS 1.3
5694 *
5695 */
5696int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5697 const unsigned char *buf,
5698 const unsigned char *end )
5699{
5700 const unsigned char *p = buf;
5701 size_t supported_sig_algs_len = 0;
5702 const unsigned char *supported_sig_algs_end;
5703 uint16_t sig_alg;
5704 uint32_t common_idx = 0;
5705
5706 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5707 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5708 p += 2;
5709
5710 memset( ssl->handshake->received_sig_algs, 0,
5711 sizeof(ssl->handshake->received_sig_algs) );
5712
5713 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5714 supported_sig_algs_end = p + supported_sig_algs_len;
5715 while( p < supported_sig_algs_end )
5716 {
5717 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5718 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5719 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005720 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5721 sig_alg,
5722 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005723#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005724 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005725 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5726 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5727 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005728 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005729 }
5730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005731
Jerry Yuf3b46b52022-06-19 16:52:27 +08005732 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5733 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005734
5735 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5736 {
5737 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5738 common_idx += 1;
5739 }
5740 }
5741 /* Check that we consumed all the message. */
5742 if( p != end )
5743 {
5744 MBEDTLS_SSL_DEBUG_MSG( 1,
5745 ( "Signature algorithms extension length misaligned" ) );
5746 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5747 MBEDTLS_ERR_SSL_DECODE_ERROR );
5748 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5749 }
5750
5751 if( common_idx == 0 )
5752 {
5753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5754 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5755 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5756 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5757 }
5758
Gabor Mezei15b95a62022-05-09 16:37:58 +02005759 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005760 return( 0 );
5761}
5762
Ronald Crone68ab4f2022-10-05 12:46:29 +02005763#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005764
Jerry Yudc7bd172022-02-17 13:44:15 +08005765#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5766
5767#if defined(MBEDTLS_USE_PSA_CRYPTO)
5768
5769static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5770 mbedtls_svc_key_id_t key,
5771 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005772 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005773 const unsigned char* seed, size_t seed_length,
5774 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005775 const unsigned char* other_secret,
5776 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005777 size_t capacity )
5778{
5779 psa_status_t status;
5780
5781 status = psa_key_derivation_setup( derivation, alg );
5782 if( status != PSA_SUCCESS )
5783 return( status );
5784
5785 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5786 {
5787 status = psa_key_derivation_input_bytes( derivation,
5788 PSA_KEY_DERIVATION_INPUT_SEED,
5789 seed, seed_length );
5790 if( status != PSA_SUCCESS )
5791 return( status );
5792
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005793 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005794 {
5795 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005796 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5797 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005798 if( status != PSA_SUCCESS )
5799 return( status );
5800 }
5801
Jerry Yudc7bd172022-02-17 13:44:15 +08005802 if( mbedtls_svc_key_id_is_null( key ) )
5803 {
5804 status = psa_key_derivation_input_bytes(
5805 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005806 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005807 }
5808 else
5809 {
5810 status = psa_key_derivation_input_key(
5811 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5812 }
5813 if( status != PSA_SUCCESS )
5814 return( status );
5815
5816 status = psa_key_derivation_input_bytes( derivation,
5817 PSA_KEY_DERIVATION_INPUT_LABEL,
5818 label, label_length );
5819 if( status != PSA_SUCCESS )
5820 return( status );
5821 }
5822 else
5823 {
5824 return( PSA_ERROR_NOT_SUPPORTED );
5825 }
5826
5827 status = psa_key_derivation_set_capacity( derivation, capacity );
5828 if( status != PSA_SUCCESS )
5829 return( status );
5830
5831 return( PSA_SUCCESS );
5832}
5833
Andrzej Kurek57d10632022-10-24 10:32:01 -04005834#if defined(PSA_WANT_ALG_SHA_384) || \
5835 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005836MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005837static int tls_prf_generic( mbedtls_md_type_t md_type,
5838 const unsigned char *secret, size_t slen,
5839 const char *label,
5840 const unsigned char *random, size_t rlen,
5841 unsigned char *dstbuf, size_t dlen )
5842{
5843 psa_status_t status;
5844 psa_algorithm_t alg;
5845 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5846 psa_key_derivation_operation_t derivation =
5847 PSA_KEY_DERIVATION_OPERATION_INIT;
5848
5849 if( md_type == MBEDTLS_MD_SHA384 )
5850 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5851 else
5852 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5853
5854 /* Normally a "secret" should be long enough to be impossible to
5855 * find by brute force, and in particular should not be empty. But
5856 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5857 * and for this use case it makes sense to have a 0-length "secret".
5858 * Since the key API doesn't allow importing a key of length 0,
5859 * keep master_key=0, which setup_psa_key_derivation() understands
5860 * to mean a 0-length "secret" input. */
5861 if( slen != 0 )
5862 {
5863 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5864 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5865 psa_set_key_algorithm( &key_attributes, alg );
5866 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5867
5868 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5869 if( status != PSA_SUCCESS )
5870 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5871 }
5872
5873 status = setup_psa_key_derivation( &derivation,
5874 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005875 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005876 random, rlen,
5877 (unsigned char const *) label,
5878 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005879 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005880 dlen );
5881 if( status != PSA_SUCCESS )
5882 {
5883 psa_key_derivation_abort( &derivation );
5884 psa_destroy_key( master_key );
5885 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5886 }
5887
5888 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5889 if( status != PSA_SUCCESS )
5890 {
5891 psa_key_derivation_abort( &derivation );
5892 psa_destroy_key( master_key );
5893 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5894 }
5895
5896 status = psa_key_derivation_abort( &derivation );
5897 if( status != PSA_SUCCESS )
5898 {
5899 psa_destroy_key( master_key );
5900 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5901 }
5902
5903 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5904 status = psa_destroy_key( master_key );
5905 if( status != PSA_SUCCESS )
5906 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5907
5908 return( 0 );
5909}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005910#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005911#else /* MBEDTLS_USE_PSA_CRYPTO */
5912
Andrzej Kurek57d10632022-10-24 10:32:01 -04005913#if defined(MBEDTLS_MD_C) && \
5914 ( defined(MBEDTLS_SHA256_C) || \
5915 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005916MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005917static int tls_prf_generic( mbedtls_md_type_t md_type,
5918 const unsigned char *secret, size_t slen,
5919 const char *label,
5920 const unsigned char *random, size_t rlen,
5921 unsigned char *dstbuf, size_t dlen )
5922{
5923 size_t nb;
5924 size_t i, j, k, md_len;
5925 unsigned char *tmp;
5926 size_t tmp_len = 0;
5927 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5928 const mbedtls_md_info_t *md_info;
5929 mbedtls_md_context_t md_ctx;
5930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5931
5932 mbedtls_md_init( &md_ctx );
5933
5934 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5936
5937 md_len = mbedtls_md_get_size( md_info );
5938
5939 tmp_len = md_len + strlen( label ) + rlen;
5940 tmp = mbedtls_calloc( 1, tmp_len );
5941 if( tmp == NULL )
5942 {
5943 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5944 goto exit;
5945 }
5946
5947 nb = strlen( label );
5948 memcpy( tmp + md_len, label, nb );
5949 memcpy( tmp + md_len + nb, random, rlen );
5950 nb += rlen;
5951
5952 /*
5953 * Compute P_<hash>(secret, label + random)[0..dlen]
5954 */
5955 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5956 goto exit;
5957
5958 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5959 if( ret != 0 )
5960 goto exit;
5961 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5962 if( ret != 0 )
5963 goto exit;
5964 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5965 if( ret != 0 )
5966 goto exit;
5967
5968 for( i = 0; i < dlen; i += md_len )
5969 {
5970 ret = mbedtls_md_hmac_reset ( &md_ctx );
5971 if( ret != 0 )
5972 goto exit;
5973 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5974 if( ret != 0 )
5975 goto exit;
5976 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5977 if( ret != 0 )
5978 goto exit;
5979
5980 ret = mbedtls_md_hmac_reset ( &md_ctx );
5981 if( ret != 0 )
5982 goto exit;
5983 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5984 if( ret != 0 )
5985 goto exit;
5986 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5987 if( ret != 0 )
5988 goto exit;
5989
5990 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5991
5992 for( j = 0; j < k; j++ )
5993 dstbuf[i + j] = h_i[j];
5994 }
5995
5996exit:
5997 mbedtls_md_free( &md_ctx );
5998
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00005999 if ( tmp != NULL )
6000 mbedtls_platform_zeroize( tmp, tmp_len );
6001
Jerry Yudc7bd172022-02-17 13:44:15 +08006002 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6003
6004 mbedtls_free( tmp );
6005
6006 return( ret );
6007}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006008#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006009#endif /* MBEDTLS_USE_PSA_CRYPTO */
6010
Andrzej Kurek25f27152022-08-17 16:09:31 -04006011#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006012MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006013static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6014 const char *label,
6015 const unsigned char *random, size_t rlen,
6016 unsigned char *dstbuf, size_t dlen )
6017{
6018 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6019 label, random, rlen, dstbuf, dlen ) );
6020}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006021#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006022
Andrzej Kurek25f27152022-08-17 16:09:31 -04006023#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006024MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006025static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6026 const char *label,
6027 const unsigned char *random, size_t rlen,
6028 unsigned char *dstbuf, size_t dlen )
6029{
6030 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6031 label, random, rlen, dstbuf, dlen ) );
6032}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006033#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006034
Jerry Yuf009d862022-02-17 14:01:37 +08006035/*
6036 * Set appropriate PRF function and other SSL / TLS1.2 functions
6037 *
6038 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006039 * - hash associated with the ciphersuite (only used by TLS 1.2)
6040 *
6041 * Outputs:
6042 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6043 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006044MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006045static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006046 mbedtls_md_type_t hash )
6047{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006048#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006049 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006050 {
6051 handshake->tls_prf = tls_prf_sha384;
6052 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6053 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6054 }
6055 else
6056#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006057#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006058 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006059 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006060 handshake->tls_prf = tls_prf_sha256;
6061 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6062 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6063 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006064#else
Jerry Yuf009d862022-02-17 14:01:37 +08006065 {
Jerry Yuf009d862022-02-17 14:01:37 +08006066 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006067 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6069 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006070#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006071
6072 return( 0 );
6073}
Jerry Yud6ab2352022-02-17 14:03:43 +08006074
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006075/*
6076 * Compute master secret if needed
6077 *
6078 * Parameters:
6079 * [in/out] handshake
6080 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6081 * (PSA-PSK) ciphersuite_info, psk_opaque
6082 * [out] premaster (cleared)
6083 * [out] master
6084 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6085 * debug: conf->f_dbg, conf->p_dbg
6086 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006087 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006088 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006089MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006090static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6091 unsigned char *master,
6092 const mbedtls_ssl_context *ssl )
6093{
6094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6095
6096 /* cf. RFC 5246, Section 8.1:
6097 * "The master secret is always exactly 48 bytes in length." */
6098 size_t const master_secret_len = 48;
6099
6100#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6101 unsigned char session_hash[48];
6102#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6103
6104 /* The label for the KDF used for key expansion.
6105 * This is either "master secret" or "extended master secret"
6106 * depending on whether the Extended Master Secret extension
6107 * is used. */
6108 char const *lbl = "master secret";
6109
Przemek Stekielae4ed302022-04-05 17:15:55 +02006110 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006111 * - If the Extended Master Secret extension is not used,
6112 * this is ClientHello.Random + ServerHello.Random
6113 * (see Sect. 8.1 in RFC 5246).
6114 * - If the Extended Master Secret extension is used,
6115 * this is the transcript of the handshake so far.
6116 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006117 unsigned char const *seed = handshake->randbytes;
6118 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006119
6120#if !defined(MBEDTLS_DEBUG_C) && \
6121 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6122 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6123 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6124 ssl = NULL; /* make sure we don't use it except for those cases */
6125 (void) ssl;
6126#endif
6127
6128 if( handshake->resume != 0 )
6129 {
6130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6131 return( 0 );
6132 }
6133
6134#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6135 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6136 {
6137 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006138 seed = session_hash;
6139 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006140
6141 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006142 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006143 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006144#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006145
Przemek Stekiel99114f32022-04-22 11:20:09 +02006146#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006147 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006148 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006149 {
6150 /* Perform PSK-to-MS expansion in a single step. */
6151 psa_status_t status;
6152 psa_algorithm_t alg;
6153 mbedtls_svc_key_id_t psk;
6154 psa_key_derivation_operation_t derivation =
6155 PSA_KEY_DERIVATION_OPERATION_INIT;
6156 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6157
6158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6159
6160 psk = mbedtls_ssl_get_opaque_psk( ssl );
6161
6162 if( hash_alg == MBEDTLS_MD_SHA384 )
6163 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6164 else
6165 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6166
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006167 size_t other_secret_len = 0;
6168 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006169
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006170 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006171 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006172 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006173 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006174 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006175 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006176 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006177 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006178 other_secret_len = 48;
6179 other_secret = handshake->premaster + 2;
6180 break;
6181 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006182 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006183 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6184 other_secret = handshake->premaster + 2;
6185 break;
6186 default:
6187 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006188 }
6189
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006190 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006191 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006192 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006193 (unsigned char const *) lbl,
6194 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006195 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006196 master_secret_len );
6197 if( status != PSA_SUCCESS )
6198 {
6199 psa_key_derivation_abort( &derivation );
6200 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6201 }
6202
6203 status = psa_key_derivation_output_bytes( &derivation,
6204 master,
6205 master_secret_len );
6206 if( status != PSA_SUCCESS )
6207 {
6208 psa_key_derivation_abort( &derivation );
6209 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6210 }
6211
6212 status = psa_key_derivation_abort( &derivation );
6213 if( status != PSA_SUCCESS )
6214 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6215 }
6216 else
6217#endif
6218 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006219#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6220 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6221 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6222 {
6223 psa_status_t status;
6224 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6225 psa_key_derivation_operation_t derivation =
6226 PSA_KEY_DERIVATION_OPERATION_INIT;
6227
6228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6229
6230 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6231
6232 status = psa_key_derivation_setup( &derivation, alg );
6233 if( status != PSA_SUCCESS )
6234 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6235
6236 status = psa_key_derivation_set_capacity( &derivation,
6237 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6238 if( status != PSA_SUCCESS )
6239 {
6240 psa_key_derivation_abort( &derivation );
6241 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6242 }
6243
6244 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6245 &derivation );
6246 if( status != PSA_SUCCESS )
6247 {
6248 psa_key_derivation_abort( &derivation );
6249 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6250 }
6251
6252 status = psa_key_derivation_output_bytes( &derivation,
6253 handshake->premaster,
6254 handshake->pmslen );
6255 if( status != PSA_SUCCESS )
6256 {
6257 psa_key_derivation_abort( &derivation );
6258 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6259 }
6260
6261 status = psa_key_derivation_abort( &derivation );
6262 if( status != PSA_SUCCESS )
6263 {
6264 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6265 }
6266 }
6267#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006268 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006269 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006270 master,
6271 master_secret_len );
6272 if( ret != 0 )
6273 {
6274 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6275 return( ret );
6276 }
6277
6278 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6279 handshake->premaster,
6280 handshake->pmslen );
6281
6282 mbedtls_platform_zeroize( handshake->premaster,
6283 sizeof(handshake->premaster) );
6284 }
6285
6286 return( 0 );
6287}
6288
Jerry Yud62f87e2022-02-17 14:09:02 +08006289int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6290{
6291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6292 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6293 ssl->handshake->ciphersuite_info;
6294
6295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6296
6297 /* Set PRF, calc_verify and calc_finished function pointers */
6298 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006299 ciphersuite_info->mac );
6300 if( ret != 0 )
6301 {
6302 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6303 return( ret );
6304 }
6305
6306 /* Compute master secret if needed */
6307 ret = ssl_compute_master( ssl->handshake,
6308 ssl->session_negotiate->master,
6309 ssl );
6310 if( ret != 0 )
6311 {
6312 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6313 return( ret );
6314 }
6315
6316 /* Swap the client and server random values:
6317 * - MS derivation wanted client+server (RFC 5246 8.1)
6318 * - key derivation wants server+client (RFC 5246 6.3) */
6319 {
6320 unsigned char tmp[64];
6321 memcpy( tmp, ssl->handshake->randbytes, 64 );
6322 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6323 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6324 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6325 }
6326
6327 /* Populate transform structure */
6328 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6329 ssl->session_negotiate->ciphersuite,
6330 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006331#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006332 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006333#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006334 ssl->handshake->tls_prf,
6335 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006336 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006337 ssl->conf->endpoint,
6338 ssl );
6339 if( ret != 0 )
6340 {
6341 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6342 return( ret );
6343 }
6344
6345 /* We no longer need Server/ClientHello.random values */
6346 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6347 sizeof( ssl->handshake->randbytes ) );
6348
6349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6350
6351 return( 0 );
6352}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006353
Ronald Cron4dcbca92022-03-07 10:21:40 +01006354int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6355{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006356 switch( md )
6357 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006358#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006359 case MBEDTLS_SSL_HASH_SHA384:
6360 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6361 break;
6362#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006363#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006364 case MBEDTLS_SSL_HASH_SHA256:
6365 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6366 break;
6367#endif
6368 default:
6369 return( -1 );
6370 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006371#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6372 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6373 (void) ssl;
6374#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006375 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006376}
6377
Andrzej Kurek25f27152022-08-17 16:09:31 -04006378#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006379void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6380 unsigned char *hash,
6381 size_t *hlen )
6382{
6383#if defined(MBEDTLS_USE_PSA_CRYPTO)
6384 size_t hash_size;
6385 psa_status_t status;
6386 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6387
6388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6389 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6390 if( status != PSA_SUCCESS )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6393 return;
6394 }
6395
6396 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6397 if( status != PSA_SUCCESS )
6398 {
6399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6400 return;
6401 }
6402
6403 *hlen = 32;
6404 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6406#else
6407 mbedtls_sha256_context sha256;
6408
6409 mbedtls_sha256_init( &sha256 );
6410
6411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6412
6413 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6414 mbedtls_sha256_finish( &sha256, hash );
6415
6416 *hlen = 32;
6417
6418 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6420
6421 mbedtls_sha256_free( &sha256 );
6422#endif /* MBEDTLS_USE_PSA_CRYPTO */
6423 return;
6424}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006425#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006426
Andrzej Kurek25f27152022-08-17 16:09:31 -04006427#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006428void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6429 unsigned char *hash,
6430 size_t *hlen )
6431{
6432#if defined(MBEDTLS_USE_PSA_CRYPTO)
6433 size_t hash_size;
6434 psa_status_t status;
6435 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6436
6437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6438 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6439 if( status != PSA_SUCCESS )
6440 {
6441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6442 return;
6443 }
6444
6445 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6446 if( status != PSA_SUCCESS )
6447 {
6448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6449 return;
6450 }
6451
6452 *hlen = 48;
6453 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6455#else
6456 mbedtls_sha512_context sha512;
6457
6458 mbedtls_sha512_init( &sha512 );
6459
6460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6461
Andrzej Kureka242e832022-08-11 10:03:14 -04006462 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006463 mbedtls_sha512_finish( &sha512, hash );
6464
6465 *hlen = 48;
6466
6467 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6469
6470 mbedtls_sha512_free( &sha512 );
6471#endif /* MBEDTLS_USE_PSA_CRYPTO */
6472 return;
6473}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006474#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006475
Neil Armstrong80f6f322022-05-03 17:56:38 +02006476#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6477 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006478int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6479{
6480 unsigned char *p = ssl->handshake->premaster;
6481 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6482 const unsigned char *psk = NULL;
6483 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006484 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006485
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006486 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006487 {
6488 /*
6489 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006490 * checked before calling this function.
6491 *
6492 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006493 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006494 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006495 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6496 {
6497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6499 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006500 }
6501
6502 /*
6503 * PMS = struct {
6504 * opaque other_secret<0..2^16-1>;
6505 * opaque psk<0..2^16-1>;
6506 * };
6507 * with "other_secret" depending on the particular key exchange
6508 */
6509#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6510 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6511 {
6512 if( end - p < 2 )
6513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6514
6515 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6516 p += 2;
6517
6518 if( end < p || (size_t)( end - p ) < psk_len )
6519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6520
6521 memset( p, 0, psk_len );
6522 p += psk_len;
6523 }
6524 else
6525#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6526#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6527 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6528 {
6529 /*
6530 * other_secret already set by the ClientKeyExchange message,
6531 * and is 48 bytes long
6532 */
6533 if( end - p < 2 )
6534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6535
6536 *p++ = 0;
6537 *p++ = 48;
6538 p += 48;
6539 }
6540 else
6541#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6542#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6543 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6544 {
6545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6546 size_t len;
6547
6548 /* Write length only when we know the actual value */
6549 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6550 p + 2, end - ( p + 2 ), &len,
6551 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6552 {
6553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6554 return( ret );
6555 }
6556 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6557 p += 2 + len;
6558
6559 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6560 }
6561 else
6562#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006563#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006564 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6565 {
6566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6567 size_t zlen;
6568
6569 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6570 p + 2, end - ( p + 2 ),
6571 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6572 {
6573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6574 return( ret );
6575 }
6576
6577 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6578 p += 2 + zlen;
6579
6580 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6581 MBEDTLS_DEBUG_ECDH_Z );
6582 }
6583 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006584#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006585 {
6586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6587 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6588 }
6589
6590 /* opaque psk<0..2^16-1>; */
6591 if( end - p < 2 )
6592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6593
6594 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6595 p += 2;
6596
6597 if( end < p || (size_t)( end - p ) < psk_len )
6598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6599
6600 memcpy( p, psk, psk_len );
6601 p += psk_len;
6602
6603 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6604
6605 return( 0 );
6606}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006607#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006608
6609#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006610MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006611static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6612
6613#if defined(MBEDTLS_SSL_PROTO_DTLS)
6614int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6615{
6616 /* If renegotiation is not enforced, retransmit until we would reach max
6617 * timeout if we were using the usual handshake doubling scheme */
6618 if( ssl->conf->renego_max_records < 0 )
6619 {
6620 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6621 unsigned char doublings = 1;
6622
6623 while( ratio != 0 )
6624 {
6625 ++doublings;
6626 ratio >>= 1;
6627 }
6628
6629 if( ++ssl->renego_records_seen > doublings )
6630 {
6631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6632 return( 0 );
6633 }
6634 }
6635
6636 return( ssl_write_hello_request( ssl ) );
6637}
6638#endif
6639#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006640
Jerry Yud9526692022-02-17 14:23:47 +08006641/*
6642 * Handshake functions
6643 */
6644#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6645/* No certificate support -> dummy functions */
6646int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6647{
6648 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6649 ssl->handshake->ciphersuite_info;
6650
6651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6652
6653 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6654 {
6655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6656 ssl->state++;
6657 return( 0 );
6658 }
6659
6660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6662}
6663
6664int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6665{
6666 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6667 ssl->handshake->ciphersuite_info;
6668
6669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6670
6671 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6672 {
6673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6674 ssl->state++;
6675 return( 0 );
6676 }
6677
6678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6679 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6680}
6681
6682#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6683/* Some certificate support -> implement write and parse */
6684
6685int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6686{
6687 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6688 size_t i, n;
6689 const mbedtls_x509_crt *crt;
6690 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6691 ssl->handshake->ciphersuite_info;
6692
6693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6694
6695 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6696 {
6697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6698 ssl->state++;
6699 return( 0 );
6700 }
6701
6702#if defined(MBEDTLS_SSL_CLI_C)
6703 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6704 {
6705 if( ssl->handshake->client_auth == 0 )
6706 {
6707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6708 ssl->state++;
6709 return( 0 );
6710 }
6711 }
6712#endif /* MBEDTLS_SSL_CLI_C */
6713#if defined(MBEDTLS_SSL_SRV_C)
6714 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6715 {
6716 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6717 {
6718 /* Should never happen because we shouldn't have picked the
6719 * ciphersuite if we don't have a certificate. */
6720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6721 }
6722 }
6723#endif
6724
6725 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6726
6727 /*
6728 * 0 . 0 handshake type
6729 * 1 . 3 handshake length
6730 * 4 . 6 length of all certs
6731 * 7 . 9 length of cert. 1
6732 * 10 . n-1 peer certificate
6733 * n . n+2 length of cert. 2
6734 * n+3 . ... upper level cert, etc.
6735 */
6736 i = 7;
6737 crt = mbedtls_ssl_own_cert( ssl );
6738
6739 while( crt != NULL )
6740 {
6741 n = crt->raw.len;
6742 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6743 {
6744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6745 " > %" MBEDTLS_PRINTF_SIZET,
6746 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6747 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6748 }
6749
6750 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6751 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6752 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6753
6754 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6755 i += n; crt = crt->next;
6756 }
6757
6758 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6759 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6760 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6761
6762 ssl->out_msglen = i;
6763 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6764 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6765
6766 ssl->state++;
6767
6768 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6769 {
6770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6771 return( ret );
6772 }
6773
6774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6775
6776 return( ret );
6777}
6778
6779#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6780
6781#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006782MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006783static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6784 unsigned char *crt_buf,
6785 size_t crt_buf_len )
6786{
6787 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6788
6789 if( peer_crt == NULL )
6790 return( -1 );
6791
6792 if( peer_crt->raw.len != crt_buf_len )
6793 return( -1 );
6794
6795 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6796}
6797#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006798MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006799static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6800 unsigned char *crt_buf,
6801 size_t crt_buf_len )
6802{
6803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6804 unsigned char const * const peer_cert_digest =
6805 ssl->session->peer_cert_digest;
6806 mbedtls_md_type_t const peer_cert_digest_type =
6807 ssl->session->peer_cert_digest_type;
6808 mbedtls_md_info_t const * const digest_info =
6809 mbedtls_md_info_from_type( peer_cert_digest_type );
6810 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6811 size_t digest_len;
6812
6813 if( peer_cert_digest == NULL || digest_info == NULL )
6814 return( -1 );
6815
6816 digest_len = mbedtls_md_get_size( digest_info );
6817 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6818 return( -1 );
6819
6820 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6821 if( ret != 0 )
6822 return( -1 );
6823
6824 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6825}
6826#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6827#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6828
6829/*
6830 * Once the certificate message is read, parse it into a cert chain and
6831 * perform basic checks, but leave actual verification to the caller
6832 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006833MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006834static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6835 mbedtls_x509_crt *chain )
6836{
6837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6838#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6839 int crt_cnt=0;
6840#endif
6841 size_t i, n;
6842 uint8_t alert;
6843
6844 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6845 {
6846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6847 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6848 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6849 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6850 }
6851
6852 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6853 {
6854 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6855 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6856 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6857 }
6858
6859 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6860 {
6861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6862 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6863 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6864 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6865 }
6866
6867 i = mbedtls_ssl_hs_hdr_len( ssl );
6868
6869 /*
6870 * Same message structure as in mbedtls_ssl_write_certificate()
6871 */
6872 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6873
6874 if( ssl->in_msg[i] != 0 ||
6875 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6876 {
6877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6879 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6880 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6881 }
6882
6883 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6884 i += 3;
6885
6886 /* Iterate through and parse the CRTs in the provided chain. */
6887 while( i < ssl->in_hslen )
6888 {
6889 /* Check that there's room for the next CRT's length fields. */
6890 if ( i + 3 > ssl->in_hslen ) {
6891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6892 mbedtls_ssl_send_alert_message( ssl,
6893 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6894 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6895 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6896 }
6897 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6898 * anything beyond 2**16 ~ 64K. */
6899 if( ssl->in_msg[i] != 0 )
6900 {
6901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6902 mbedtls_ssl_send_alert_message( ssl,
6903 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6904 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6905 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6906 }
6907
6908 /* Read length of the next CRT in the chain. */
6909 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6910 | (unsigned int) ssl->in_msg[i + 2];
6911 i += 3;
6912
6913 if( n < 128 || i + n > ssl->in_hslen )
6914 {
6915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6916 mbedtls_ssl_send_alert_message( ssl,
6917 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6918 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6919 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6920 }
6921
6922 /* Check if we're handling the first CRT in the chain. */
6923#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6924 if( crt_cnt++ == 0 &&
6925 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6926 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6927 {
6928 /* During client-side renegotiation, check that the server's
6929 * end-CRTs hasn't changed compared to the initial handshake,
6930 * mitigating the triple handshake attack. On success, reuse
6931 * the original end-CRT instead of parsing it again. */
6932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6933 if( ssl_check_peer_crt_unchanged( ssl,
6934 &ssl->in_msg[i],
6935 n ) != 0 )
6936 {
6937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6938 mbedtls_ssl_send_alert_message( ssl,
6939 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6940 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6941 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6942 }
6943
6944 /* Now we can safely free the original chain. */
6945 ssl_clear_peer_cert( ssl->session );
6946 }
6947#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6948
6949 /* Parse the next certificate in the chain. */
6950#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6951 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6952#else
6953 /* If we don't need to store the CRT chain permanently, parse
6954 * it in-place from the input buffer instead of making a copy. */
6955 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6956#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6957 switch( ret )
6958 {
6959 case 0: /*ok*/
6960 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6961 /* Ignore certificate with an unknown algorithm: maybe a
6962 prior certificate was already trusted. */
6963 break;
6964
6965 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6966 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6967 goto crt_parse_der_failed;
6968
6969 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6970 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6971 goto crt_parse_der_failed;
6972
6973 default:
6974 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6975 crt_parse_der_failed:
6976 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6977 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6978 return( ret );
6979 }
6980
6981 i += n;
6982 }
6983
6984 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6985 return( 0 );
6986}
6987
6988#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006989MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006990static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6991{
6992 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6993 return( -1 );
6994
6995 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6996 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6997 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6998 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6999 {
Ronald Cron19385882022-06-15 16:26:13 +02007000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007001 return( 0 );
7002 }
7003 return( -1 );
7004}
7005#endif /* MBEDTLS_SSL_SRV_C */
7006
7007/* Check if a certificate message is expected.
7008 * Return either
7009 * - SSL_CERTIFICATE_EXPECTED, or
7010 * - SSL_CERTIFICATE_SKIP
7011 * indicating whether a Certificate message is expected or not.
7012 */
7013#define SSL_CERTIFICATE_EXPECTED 0
7014#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007015MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007016static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7017 int authmode )
7018{
7019 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7020 ssl->handshake->ciphersuite_info;
7021
7022 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7023 return( SSL_CERTIFICATE_SKIP );
7024
7025#if defined(MBEDTLS_SSL_SRV_C)
7026 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7027 {
7028 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7029 return( SSL_CERTIFICATE_SKIP );
7030
7031 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7032 {
7033 ssl->session_negotiate->verify_result =
7034 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7035 return( SSL_CERTIFICATE_SKIP );
7036 }
7037 }
7038#else
7039 ((void) authmode);
7040#endif /* MBEDTLS_SSL_SRV_C */
7041
7042 return( SSL_CERTIFICATE_EXPECTED );
7043}
7044
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007045MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007046static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7047 int authmode,
7048 mbedtls_x509_crt *chain,
7049 void *rs_ctx )
7050{
7051 int ret = 0;
7052 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7053 ssl->handshake->ciphersuite_info;
7054 int have_ca_chain = 0;
7055
7056 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7057 void *p_vrfy;
7058
7059 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7060 return( 0 );
7061
7062 if( ssl->f_vrfy != NULL )
7063 {
7064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7065 f_vrfy = ssl->f_vrfy;
7066 p_vrfy = ssl->p_vrfy;
7067 }
7068 else
7069 {
7070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7071 f_vrfy = ssl->conf->f_vrfy;
7072 p_vrfy = ssl->conf->p_vrfy;
7073 }
7074
7075 /*
7076 * Main check: verify certificate
7077 */
7078#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7079 if( ssl->conf->f_ca_cb != NULL )
7080 {
7081 ((void) rs_ctx);
7082 have_ca_chain = 1;
7083
7084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7085 ret = mbedtls_x509_crt_verify_with_ca_cb(
7086 chain,
7087 ssl->conf->f_ca_cb,
7088 ssl->conf->p_ca_cb,
7089 ssl->conf->cert_profile,
7090 ssl->hostname,
7091 &ssl->session_negotiate->verify_result,
7092 f_vrfy, p_vrfy );
7093 }
7094 else
7095#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7096 {
7097 mbedtls_x509_crt *ca_chain;
7098 mbedtls_x509_crl *ca_crl;
7099
7100#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7101 if( ssl->handshake->sni_ca_chain != NULL )
7102 {
7103 ca_chain = ssl->handshake->sni_ca_chain;
7104 ca_crl = ssl->handshake->sni_ca_crl;
7105 }
7106 else
7107#endif
7108 {
7109 ca_chain = ssl->conf->ca_chain;
7110 ca_crl = ssl->conf->ca_crl;
7111 }
7112
7113 if( ca_chain != NULL )
7114 have_ca_chain = 1;
7115
7116 ret = mbedtls_x509_crt_verify_restartable(
7117 chain,
7118 ca_chain, ca_crl,
7119 ssl->conf->cert_profile,
7120 ssl->hostname,
7121 &ssl->session_negotiate->verify_result,
7122 f_vrfy, p_vrfy, rs_ctx );
7123 }
7124
7125 if( ret != 0 )
7126 {
7127 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7128 }
7129
7130#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7131 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7132 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7133#endif
7134
7135 /*
7136 * Secondary checks: always done, but change 'ret' only if it was 0
7137 */
7138
7139#if defined(MBEDTLS_ECP_C)
7140 {
7141 const mbedtls_pk_context *pk = &chain->pk;
7142
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007143 /* If certificate uses an EC key, make sure the curve is OK.
7144 * This is a public key, so it can't be opaque, so can_do() is a good
7145 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007146 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007147 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007148 /* and in the unlikely case the above assumption no longer holds
7149 * we are making sure that pk_ec() here does not return a NULL
7150 */
7151 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7152 if( ec == NULL )
7153 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7156 }
Jerry Yud9526692022-02-17 14:23:47 +08007157
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007158 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7159 {
7160 ssl->session_negotiate->verify_result |=
7161 MBEDTLS_X509_BADCERT_BAD_KEY;
7162
7163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7164 if( ret == 0 )
7165 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7166 }
Jerry Yud9526692022-02-17 14:23:47 +08007167 }
7168 }
7169#endif /* MBEDTLS_ECP_C */
7170
7171 if( mbedtls_ssl_check_cert_usage( chain,
7172 ciphersuite_info,
7173 ! ssl->conf->endpoint,
7174 &ssl->session_negotiate->verify_result ) != 0 )
7175 {
7176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7177 if( ret == 0 )
7178 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7179 }
7180
7181 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7182 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7183 * with details encoded in the verification flags. All other kinds
7184 * of error codes, including those from the user provided f_vrfy
7185 * functions, are treated as fatal and lead to a failure of
7186 * ssl_parse_certificate even if verification was optional. */
7187 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7188 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7189 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7190 {
7191 ret = 0;
7192 }
7193
7194 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7195 {
7196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7197 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7198 }
7199
7200 if( ret != 0 )
7201 {
7202 uint8_t alert;
7203
7204 /* The certificate may have been rejected for several reasons.
7205 Pick one and send the corresponding alert. Which alert to send
7206 may be a subject of debate in some cases. */
7207 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7208 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7209 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7210 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7211 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7212 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7213 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7214 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7215 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7216 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7217 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7218 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7219 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7220 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7221 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7222 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7223 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7224 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7225 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7226 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7227 else
7228 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7229 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7230 alert );
7231 }
7232
7233#if defined(MBEDTLS_DEBUG_C)
7234 if( ssl->session_negotiate->verify_result != 0 )
7235 {
7236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7237 (unsigned int) ssl->session_negotiate->verify_result ) );
7238 }
7239 else
7240 {
7241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7242 }
7243#endif /* MBEDTLS_DEBUG_C */
7244
7245 return( ret );
7246}
7247
7248#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007249MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007250static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7251 unsigned char *start, size_t len )
7252{
7253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7254 /* Remember digest of the peer's end-CRT. */
7255 ssl->session_negotiate->peer_cert_digest =
7256 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7257 if( ssl->session_negotiate->peer_cert_digest == NULL )
7258 {
7259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7260 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7261 mbedtls_ssl_send_alert_message( ssl,
7262 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7263 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7264
7265 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7266 }
7267
7268 ret = mbedtls_md( mbedtls_md_info_from_type(
7269 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7270 start, len,
7271 ssl->session_negotiate->peer_cert_digest );
7272
7273 ssl->session_negotiate->peer_cert_digest_type =
7274 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7275 ssl->session_negotiate->peer_cert_digest_len =
7276 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7277
7278 return( ret );
7279}
7280
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007281MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007282static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7283 unsigned char *start, size_t len )
7284{
7285 unsigned char *end = start + len;
7286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7287
7288 /* Make a copy of the peer's raw public key. */
7289 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7290 ret = mbedtls_pk_parse_subpubkey( &start, end,
7291 &ssl->handshake->peer_pubkey );
7292 if( ret != 0 )
7293 {
7294 /* We should have parsed the public key before. */
7295 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7296 }
7297
7298 return( 0 );
7299}
7300#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7301
7302int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7303{
7304 int ret = 0;
7305 int crt_expected;
7306#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7307 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7308 ? ssl->handshake->sni_authmode
7309 : ssl->conf->authmode;
7310#else
7311 const int authmode = ssl->conf->authmode;
7312#endif
7313 void *rs_ctx = NULL;
7314 mbedtls_x509_crt *chain = NULL;
7315
7316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7317
7318 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7319 if( crt_expected == SSL_CERTIFICATE_SKIP )
7320 {
7321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7322 goto exit;
7323 }
7324
7325#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7326 if( ssl->handshake->ecrs_enabled &&
7327 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7328 {
7329 chain = ssl->handshake->ecrs_peer_cert;
7330 ssl->handshake->ecrs_peer_cert = NULL;
7331 goto crt_verify;
7332 }
7333#endif
7334
7335 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7336 {
7337 /* mbedtls_ssl_read_record may have sent an alert already. We
7338 let it decide whether to alert. */
7339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7340 goto exit;
7341 }
7342
7343#if defined(MBEDTLS_SSL_SRV_C)
7344 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7345 {
7346 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7347
7348 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7349 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7350
7351 goto exit;
7352 }
7353#endif /* MBEDTLS_SSL_SRV_C */
7354
7355 /* Clear existing peer CRT structure in case we tried to
7356 * reuse a session but it failed, and allocate a new one. */
7357 ssl_clear_peer_cert( ssl->session_negotiate );
7358
7359 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7360 if( chain == NULL )
7361 {
7362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7363 sizeof( mbedtls_x509_crt ) ) );
7364 mbedtls_ssl_send_alert_message( ssl,
7365 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7366 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7367
7368 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7369 goto exit;
7370 }
7371 mbedtls_x509_crt_init( chain );
7372
7373 ret = ssl_parse_certificate_chain( ssl, chain );
7374 if( ret != 0 )
7375 goto exit;
7376
7377#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7378 if( ssl->handshake->ecrs_enabled)
7379 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7380
7381crt_verify:
7382 if( ssl->handshake->ecrs_enabled)
7383 rs_ctx = &ssl->handshake->ecrs_ctx;
7384#endif
7385
7386 ret = ssl_parse_certificate_verify( ssl, authmode,
7387 chain, rs_ctx );
7388 if( ret != 0 )
7389 goto exit;
7390
7391#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7392 {
7393 unsigned char *crt_start, *pk_start;
7394 size_t crt_len, pk_len;
7395
7396 /* We parse the CRT chain without copying, so
7397 * these pointers point into the input buffer,
7398 * and are hence still valid after freeing the
7399 * CRT chain. */
7400
7401 crt_start = chain->raw.p;
7402 crt_len = chain->raw.len;
7403
7404 pk_start = chain->pk_raw.p;
7405 pk_len = chain->pk_raw.len;
7406
7407 /* Free the CRT structures before computing
7408 * digest and copying the peer's public key. */
7409 mbedtls_x509_crt_free( chain );
7410 mbedtls_free( chain );
7411 chain = NULL;
7412
7413 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7414 if( ret != 0 )
7415 goto exit;
7416
7417 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7418 if( ret != 0 )
7419 goto exit;
7420 }
7421#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7422 /* Pass ownership to session structure. */
7423 ssl->session_negotiate->peer_cert = chain;
7424 chain = NULL;
7425#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7426
7427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7428
7429exit:
7430
7431 if( ret == 0 )
7432 ssl->state++;
7433
7434#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7435 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7436 {
7437 ssl->handshake->ecrs_peer_cert = chain;
7438 chain = NULL;
7439 }
7440#endif
7441
7442 if( chain != NULL )
7443 {
7444 mbedtls_x509_crt_free( chain );
7445 mbedtls_free( chain );
7446 }
7447
7448 return( ret );
7449}
7450#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7451
Andrzej Kurek25f27152022-08-17 16:09:31 -04007452#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007453static void ssl_calc_finished_tls_sha256(
7454 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7455{
7456 int len = 12;
7457 const char *sender;
7458 unsigned char padbuf[32];
7459#if defined(MBEDTLS_USE_PSA_CRYPTO)
7460 size_t hash_size;
7461 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7462 psa_status_t status;
7463#else
7464 mbedtls_sha256_context sha256;
7465#endif
7466
7467 mbedtls_ssl_session *session = ssl->session_negotiate;
7468 if( !session )
7469 session = ssl->session;
7470
7471 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7472 ? "client finished"
7473 : "server finished";
7474
7475#if defined(MBEDTLS_USE_PSA_CRYPTO)
7476 sha256_psa = psa_hash_operation_init();
7477
7478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7479
7480 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7481 if( status != PSA_SUCCESS )
7482 {
7483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7484 return;
7485 }
7486
7487 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7488 if( status != PSA_SUCCESS )
7489 {
7490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7491 return;
7492 }
7493 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7494#else
7495
7496 mbedtls_sha256_init( &sha256 );
7497
7498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7499
7500 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7501
7502 /*
7503 * TLSv1.2:
7504 * hash = PRF( master, finished_label,
7505 * Hash( handshake ) )[0.11]
7506 */
7507
7508#if !defined(MBEDTLS_SHA256_ALT)
7509 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7510 sha256.state, sizeof( sha256.state ) );
7511#endif
7512
7513 mbedtls_sha256_finish( &sha256, padbuf );
7514 mbedtls_sha256_free( &sha256 );
7515#endif /* MBEDTLS_USE_PSA_CRYPTO */
7516
7517 ssl->handshake->tls_prf( session->master, 48, sender,
7518 padbuf, 32, buf, len );
7519
7520 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7521
7522 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7523
7524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7525}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007526#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007527
Jerry Yub7ba49e2022-02-17 14:25:53 +08007528
Andrzej Kurek25f27152022-08-17 16:09:31 -04007529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007530static void ssl_calc_finished_tls_sha384(
7531 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7532{
7533 int len = 12;
7534 const char *sender;
7535 unsigned char padbuf[48];
7536#if defined(MBEDTLS_USE_PSA_CRYPTO)
7537 size_t hash_size;
7538 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7539 psa_status_t status;
7540#else
7541 mbedtls_sha512_context sha512;
7542#endif
7543
7544 mbedtls_ssl_session *session = ssl->session_negotiate;
7545 if( !session )
7546 session = ssl->session;
7547
7548 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7549 ? "client finished"
7550 : "server finished";
7551
7552#if defined(MBEDTLS_USE_PSA_CRYPTO)
7553 sha384_psa = psa_hash_operation_init();
7554
7555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7556
7557 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7558 if( status != PSA_SUCCESS )
7559 {
7560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7561 return;
7562 }
7563
7564 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7565 if( status != PSA_SUCCESS )
7566 {
7567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7568 return;
7569 }
7570 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7571#else
7572 mbedtls_sha512_init( &sha512 );
7573
7574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7575
Andrzej Kureka242e832022-08-11 10:03:14 -04007576 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007577
7578 /*
7579 * TLSv1.2:
7580 * hash = PRF( master, finished_label,
7581 * Hash( handshake ) )[0.11]
7582 */
7583
7584#if !defined(MBEDTLS_SHA512_ALT)
7585 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7586 sha512.state, sizeof( sha512.state ) );
7587#endif
7588 mbedtls_sha512_finish( &sha512, padbuf );
7589
7590 mbedtls_sha512_free( &sha512 );
7591#endif
7592
7593 ssl->handshake->tls_prf( session->master, 48, sender,
7594 padbuf, 48, buf, len );
7595
7596 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7597
7598 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7599
7600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7601}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007602#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007603
Jerry Yuaef00152022-02-17 14:27:31 +08007604void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7605{
7606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7607
7608 /*
7609 * Free our handshake params
7610 */
7611 mbedtls_ssl_handshake_free( ssl );
7612 mbedtls_free( ssl->handshake );
7613 ssl->handshake = NULL;
7614
7615 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007616 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007617 */
7618 if( ssl->transform )
7619 {
7620 mbedtls_ssl_transform_free( ssl->transform );
7621 mbedtls_free( ssl->transform );
7622 }
7623 ssl->transform = ssl->transform_negotiate;
7624 ssl->transform_negotiate = NULL;
7625
7626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7627}
7628
Jerry Yu2a9fff52022-02-17 14:28:51 +08007629void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7630{
7631 int resume = ssl->handshake->resume;
7632
7633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7634
7635#if defined(MBEDTLS_SSL_RENEGOTIATION)
7636 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7637 {
7638 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7639 ssl->renego_records_seen = 0;
7640 }
7641#endif
7642
7643 /*
7644 * Free the previous session and switch in the current one
7645 */
7646 if( ssl->session )
7647 {
7648#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7649 /* RFC 7366 3.1: keep the EtM state */
7650 ssl->session_negotiate->encrypt_then_mac =
7651 ssl->session->encrypt_then_mac;
7652#endif
7653
7654 mbedtls_ssl_session_free( ssl->session );
7655 mbedtls_free( ssl->session );
7656 }
7657 ssl->session = ssl->session_negotiate;
7658 ssl->session_negotiate = NULL;
7659
7660 /*
7661 * Add cache entry
7662 */
7663 if( ssl->conf->f_set_cache != NULL &&
7664 ssl->session->id_len != 0 &&
7665 resume == 0 )
7666 {
7667 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7668 ssl->session->id,
7669 ssl->session->id_len,
7670 ssl->session ) != 0 )
7671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7672 }
7673
7674#if defined(MBEDTLS_SSL_PROTO_DTLS)
7675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7676 ssl->handshake->flight != NULL )
7677 {
7678 /* Cancel handshake timer */
7679 mbedtls_ssl_set_timer( ssl, 0 );
7680
7681 /* Keep last flight around in case we need to resend it:
7682 * we need the handshake and transform structures for that */
7683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7684 }
7685 else
7686#endif
7687 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7688
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007689 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007690
7691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7692}
7693
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007694int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7695{
7696 int ret, hash_len;
7697
7698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7699
7700 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7701
7702 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7703
7704 /*
7705 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7706 * may define some other value. Currently (early 2016), no defined
7707 * ciphersuite does this (and this is unlikely to change as activity has
7708 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7709 */
7710 hash_len = 12;
7711
7712#if defined(MBEDTLS_SSL_RENEGOTIATION)
7713 ssl->verify_data_len = hash_len;
7714 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7715#endif
7716
7717 ssl->out_msglen = 4 + hash_len;
7718 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7719 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7720
7721 /*
7722 * In case of session resuming, invert the client and server
7723 * ChangeCipherSpec messages order.
7724 */
7725 if( ssl->handshake->resume != 0 )
7726 {
7727#if defined(MBEDTLS_SSL_CLI_C)
7728 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7729 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7730#endif
7731#if defined(MBEDTLS_SSL_SRV_C)
7732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7733 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7734#endif
7735 }
7736 else
7737 ssl->state++;
7738
7739 /*
7740 * Switch to our negotiated transform and session parameters for outbound
7741 * data.
7742 */
7743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7744
7745#if defined(MBEDTLS_SSL_PROTO_DTLS)
7746 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7747 {
7748 unsigned char i;
7749
7750 /* Remember current epoch settings for resending */
7751 ssl->handshake->alt_transform_out = ssl->transform_out;
7752 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7753 sizeof( ssl->handshake->alt_out_ctr ) );
7754
7755 /* Set sequence_number to zero */
7756 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7757
7758
7759 /* Increment epoch */
7760 for( i = 2; i > 0; i-- )
7761 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7762 break;
7763
7764 /* The loop goes to its end iff the counter is wrapping */
7765 if( i == 0 )
7766 {
7767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7768 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7769 }
7770 }
7771 else
7772#endif /* MBEDTLS_SSL_PROTO_DTLS */
7773 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7774
7775 ssl->transform_out = ssl->transform_negotiate;
7776 ssl->session_out = ssl->session_negotiate;
7777
7778#if defined(MBEDTLS_SSL_PROTO_DTLS)
7779 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7780 mbedtls_ssl_send_flight_completed( ssl );
7781#endif
7782
7783 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7784 {
7785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7786 return( ret );
7787 }
7788
7789#if defined(MBEDTLS_SSL_PROTO_DTLS)
7790 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7791 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7792 {
7793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7794 return( ret );
7795 }
7796#endif
7797
7798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7799
7800 return( 0 );
7801}
7802
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007803#define SSL_MAX_HASH_LEN 12
7804
7805int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7806{
7807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7808 unsigned int hash_len = 12;
7809 unsigned char buf[SSL_MAX_HASH_LEN];
7810
7811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7812
7813 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7814
7815 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7816 {
7817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7818 goto exit;
7819 }
7820
7821 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7822 {
7823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7824 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7825 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7826 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7827 goto exit;
7828 }
7829
7830 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7831 {
7832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7833 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7834 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7835 goto exit;
7836 }
7837
7838 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7839 {
7840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7841 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7842 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7843 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7844 goto exit;
7845 }
7846
7847 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7848 buf, hash_len ) != 0 )
7849 {
7850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7852 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7853 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7854 goto exit;
7855 }
7856
7857#if defined(MBEDTLS_SSL_RENEGOTIATION)
7858 ssl->verify_data_len = hash_len;
7859 memcpy( ssl->peer_verify_data, buf, hash_len );
7860#endif
7861
7862 if( ssl->handshake->resume != 0 )
7863 {
7864#if defined(MBEDTLS_SSL_CLI_C)
7865 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7866 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7867#endif
7868#if defined(MBEDTLS_SSL_SRV_C)
7869 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7870 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7871#endif
7872 }
7873 else
7874 ssl->state++;
7875
7876#if defined(MBEDTLS_SSL_PROTO_DTLS)
7877 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7878 mbedtls_ssl_recv_flight_completed( ssl );
7879#endif
7880
7881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7882
7883exit:
7884 mbedtls_platform_zeroize( buf, hash_len );
7885 return( ret );
7886}
7887
Jerry Yu392112c2022-02-17 14:34:10 +08007888#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7889/*
7890 * Helper to get TLS 1.2 PRF from ciphersuite
7891 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7892 */
7893static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7894{
Jerry Yu392112c2022-02-17 14:34:10 +08007895 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007896 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7897#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007898 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007899 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007900 else
Jerry Yu392112c2022-02-17 14:34:10 +08007901#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007902#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7903 {
7904 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7905 return( tls_prf_sha256 );
7906 }
7907#endif
7908#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7909 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7910 (void) ciphersuite_info;
7911#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007912
Andrzej Kurek894edde2022-09-29 06:31:14 -04007913 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007914}
7915#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007916
7917static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7918{
7919 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007920#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007921 if( tls_prf == tls_prf_sha384 )
7922 {
7923 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7924 }
7925 else
7926#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007927#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007928 if( tls_prf == tls_prf_sha256 )
7929 {
7930 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7931 }
7932 else
7933#endif
7934 return( MBEDTLS_SSL_TLS_PRF_NONE );
7935}
7936
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007937/*
7938 * Populate a transform structure with session keys and all the other
7939 * necessary information.
7940 *
7941 * Parameters:
7942 * - [in/out]: transform: structure to populate
7943 * [in] must be just initialised with mbedtls_ssl_transform_init()
7944 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7945 * - [in] ciphersuite
7946 * - [in] master
7947 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007948 * - [in] tls_prf: pointer to PRF to use for key derivation
7949 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007950 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007951 * - [in] endpoint: client or server
7952 * - [in] ssl: used for:
7953 * - ssl->conf->{f,p}_export_keys
7954 * [in] optionally used for:
7955 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7956 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007957MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007958static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7959 int ciphersuite,
7960 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007961#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007962 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007963#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007964 ssl_tls_prf_t tls_prf,
7965 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007966 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007967 unsigned endpoint,
7968 const mbedtls_ssl_context *ssl )
7969{
7970 int ret = 0;
7971 unsigned char keyblk[256];
7972 unsigned char *key1;
7973 unsigned char *key2;
7974 unsigned char *mac_enc;
7975 unsigned char *mac_dec;
7976 size_t mac_key_len = 0;
7977 size_t iv_copy_len;
7978 size_t keylen;
7979 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007980 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007981#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007982 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007983 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007984#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007985
7986#if defined(MBEDTLS_USE_PSA_CRYPTO)
7987 psa_key_type_t key_type;
7988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7989 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007990 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007991 size_t key_bits;
7992 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7993#endif
7994
7995#if !defined(MBEDTLS_DEBUG_C) && \
7996 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7997 if( ssl->f_export_keys == NULL )
7998 {
7999 ssl = NULL; /* make sure we don't use it except for these cases */
8000 (void) ssl;
8001 }
8002#endif
8003
8004 /*
8005 * Some data just needs copying into the structure
8006 */
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 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008009#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008010 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008011
8012#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8013 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8014#endif
8015
8016#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008017 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008018 {
8019 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8020 * generation separate. This should never happen. */
8021 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8022 }
8023#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8024
8025 /*
8026 * Get various info structures
8027 */
8028 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8029 if( ciphersuite_info == NULL )
8030 {
8031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8032 ciphersuite ) );
8033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8034 }
8035
Neil Armstrongab555e02022-04-04 11:07:59 +02008036 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008037#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008038 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008039#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008040 ciphersuite_info );
8041
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008042 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8043 transform->taglen =
8044 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8045
8046#if defined(MBEDTLS_USE_PSA_CRYPTO)
8047 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8048 transform->taglen,
8049 &alg,
8050 &key_type,
8051 &key_bits ) ) != PSA_SUCCESS )
8052 {
8053 ret = psa_ssl_status_to_mbedtls( status );
8054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8055 goto end;
8056 }
8057#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008058 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8059 if( cipher_info == NULL )
8060 {
8061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8062 ciphersuite_info->cipher ) );
8063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8064 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008066
Neil Armstronge4512952022-03-08 09:08:22 +01008067#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008068 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008069 if( mac_alg == 0 )
8070 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008072 (unsigned) ciphersuite_info->mac ) );
8073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8074 }
8075#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008076 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8077 if( md_info == NULL )
8078 {
8079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8080 (unsigned) ciphersuite_info->mac ) );
8081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8082 }
Neil Armstronge4512952022-03-08 09:08:22 +01008083#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008084
8085#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8086 /* Copy own and peer's CID if the use of the CID
8087 * extension has been negotiated. */
8088 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8089 {
8090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8091
8092 transform->in_cid_len = ssl->own_cid_len;
8093 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8094 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8095 transform->in_cid_len );
8096
8097 transform->out_cid_len = ssl->handshake->peer_cid_len;
8098 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8099 ssl->handshake->peer_cid_len );
8100 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8101 transform->out_cid_len );
8102 }
8103#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8104
8105 /*
8106 * Compute key block using the PRF
8107 */
8108 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8109 if( ret != 0 )
8110 {
8111 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8112 return( ret );
8113 }
8114
8115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8116 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8117 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8118 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8119 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8120
8121 /*
8122 * Determine the appropriate key, IV and MAC length.
8123 */
8124
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008125#if defined(MBEDTLS_USE_PSA_CRYPTO)
8126 keylen = PSA_BITS_TO_BYTES(key_bits);
8127#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008128 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008129#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008130
8131#if defined(MBEDTLS_GCM_C) || \
8132 defined(MBEDTLS_CCM_C) || \
8133 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008134 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008135 {
8136 size_t explicit_ivlen;
8137
8138 transform->maclen = 0;
8139 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008140
8141 /* All modes haves 96-bit IVs, but the length of the static parts vary
8142 * with mode and version:
8143 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8144 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8145 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8146 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8147 * sequence number).
8148 */
8149 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008150
8151 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008152#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008153 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008154#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008155 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8156 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008157#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008158
8159 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008160 transform->fixed_ivlen = 12;
8161 else
8162 transform->fixed_ivlen = 4;
8163
8164 /* Minimum length of encrypted record */
8165 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8166 transform->minlen = explicit_ivlen + transform->taglen;
8167 }
8168 else
8169#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8170#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008171 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8172 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8173 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008174 {
Neil Armstronge4512952022-03-08 09:08:22 +01008175#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008176 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008177#else
8178 size_t block_size = cipher_info->block_size;
8179#endif /* MBEDTLS_USE_PSA_CRYPTO */
8180
8181#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008182 /* Get MAC length */
8183 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8184#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008185 /* Initialize HMAC contexts */
8186 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8187 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8188 {
8189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8190 goto end;
8191 }
8192
8193 /* Get MAC length */
8194 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008195#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008196 transform->maclen = mac_key_len;
8197
8198 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008199#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008200 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008201#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008202 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008203#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008204
8205 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008206 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008207 transform->minlen = transform->maclen;
8208 else
8209 {
8210 /*
8211 * GenericBlockCipher:
8212 * 1. if EtM is in use: one block plus MAC
8213 * otherwise: * first multiple of blocklen greater than maclen
8214 * 2. IV
8215 */
8216#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008217 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008218 {
8219 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008220 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008221 }
8222 else
8223#endif
8224 {
8225 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008226 + block_size
8227 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008228 }
8229
Glenn Strauss07c64162022-03-14 12:34:51 -04008230 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008231 {
8232 transform->minlen += transform->ivlen;
8233 }
8234 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008235 {
8236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8237 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8238 goto end;
8239 }
8240 }
8241 }
8242 else
8243#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8244 {
8245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8247 }
8248
8249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8250 (unsigned) keylen,
8251 (unsigned) transform->minlen,
8252 (unsigned) transform->ivlen,
8253 (unsigned) transform->maclen ) );
8254
8255 /*
8256 * Finally setup the cipher contexts, IVs and MAC secrets.
8257 */
8258#if defined(MBEDTLS_SSL_CLI_C)
8259 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8260 {
8261 key1 = keyblk + mac_key_len * 2;
8262 key2 = keyblk + mac_key_len * 2 + keylen;
8263
8264 mac_enc = keyblk;
8265 mac_dec = keyblk + mac_key_len;
8266
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008267 iv_copy_len = ( transform->fixed_ivlen ) ?
8268 transform->fixed_ivlen : transform->ivlen;
8269 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8270 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8271 iv_copy_len );
8272 }
8273 else
8274#endif /* MBEDTLS_SSL_CLI_C */
8275#if defined(MBEDTLS_SSL_SRV_C)
8276 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8277 {
8278 key1 = keyblk + mac_key_len * 2 + keylen;
8279 key2 = keyblk + mac_key_len * 2;
8280
8281 mac_enc = keyblk + mac_key_len;
8282 mac_dec = keyblk;
8283
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008284 iv_copy_len = ( transform->fixed_ivlen ) ?
8285 transform->fixed_ivlen : transform->ivlen;
8286 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8287 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8288 iv_copy_len );
8289 }
8290 else
8291#endif /* MBEDTLS_SSL_SRV_C */
8292 {
8293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8294 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8295 goto end;
8296 }
8297
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008298 if( ssl != NULL && ssl->f_export_keys != NULL )
8299 {
8300 ssl->f_export_keys( ssl->p_export_keys,
8301 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8302 master, 48,
8303 randbytes + 32,
8304 randbytes,
8305 tls_prf_get_type( tls_prf ) );
8306 }
8307
8308#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008309 transform->psa_alg = alg;
8310
8311 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8312 {
8313 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8314 psa_set_key_algorithm( &attributes, alg );
8315 psa_set_key_type( &attributes, key_type );
8316
8317 if( ( status = psa_import_key( &attributes,
8318 key1,
8319 PSA_BITS_TO_BYTES( key_bits ),
8320 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8321 {
8322 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8323 ret = psa_ssl_status_to_mbedtls( status );
8324 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8325 goto end;
8326 }
8327
8328 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8329
8330 if( ( status = psa_import_key( &attributes,
8331 key2,
8332 PSA_BITS_TO_BYTES( key_bits ),
8333 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8334 {
8335 ret = psa_ssl_status_to_mbedtls( status );
8336 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8337 goto end;
8338 }
8339 }
8340#else
8341 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8342 cipher_info ) ) != 0 )
8343 {
8344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8345 goto end;
8346 }
8347
8348 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8349 cipher_info ) ) != 0 )
8350 {
8351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8352 goto end;
8353 }
8354
8355 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8356 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8357 MBEDTLS_ENCRYPT ) ) != 0 )
8358 {
8359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8360 goto end;
8361 }
8362
8363 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8364 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8365 MBEDTLS_DECRYPT ) ) != 0 )
8366 {
8367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8368 goto end;
8369 }
8370
8371#if defined(MBEDTLS_CIPHER_MODE_CBC)
8372 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8373 {
8374 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8375 MBEDTLS_PADDING_NONE ) ) != 0 )
8376 {
8377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8378 goto end;
8379 }
8380
8381 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8382 MBEDTLS_PADDING_NONE ) ) != 0 )
8383 {
8384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8385 goto end;
8386 }
8387 }
8388#endif /* MBEDTLS_CIPHER_MODE_CBC */
8389#endif /* MBEDTLS_USE_PSA_CRYPTO */
8390
Neil Armstrong29c0c042022-03-17 17:47:28 +01008391#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8392 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8393 For AEAD-based ciphersuites, there is nothing to do here. */
8394 if( mac_key_len != 0 )
8395 {
8396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008397 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008398
8399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008400 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008401 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8402
8403 if( ( status = psa_import_key( &attributes,
8404 mac_enc, mac_key_len,
8405 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8406 {
8407 ret = psa_ssl_status_to_mbedtls( status );
8408 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8409 goto end;
8410 }
8411
Ronald Cronfb39f152022-03-25 14:36:28 +01008412 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008413 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8414#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8415 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8416#endif
8417 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008418 /* mbedtls_ct_hmac() requires the key to be exportable */
8419 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8420 PSA_KEY_USAGE_VERIFY_HASH );
8421 else
8422 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8423
8424 if( ( status = psa_import_key( &attributes,
8425 mac_dec, mac_key_len,
8426 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8427 {
8428 ret = psa_ssl_status_to_mbedtls( status );
8429 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8430 goto end;
8431 }
8432#else
8433 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8434 if( ret != 0 )
8435 goto end;
8436 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8437 if( ret != 0 )
8438 goto end;
8439#endif /* MBEDTLS_USE_PSA_CRYPTO */
8440 }
8441#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8442
8443 ((void) mac_dec);
8444 ((void) mac_enc);
8445
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008446end:
8447 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8448 return( ret );
8449}
8450
Valerio Settia08b1a42022-11-17 15:10:02 +01008451#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8452 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008453int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008454 psa_pake_operation_t *pake_ctx,
8455 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008456 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008457{
8458 psa_status_t status;
8459 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008460 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008461 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8462 * At round two perform a single cycle
8463 */
8464 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008465
Valerio Setti6b3dab02022-11-17 17:14:54 +01008466 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008467 {
8468 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8469 step <= PSA_PAKE_STEP_ZK_PROOF;
8470 ++step )
8471 {
8472 /* Length is stored at the first byte */
8473 size_t length = buf[input_offset];
8474 input_offset += 1;
8475
8476 if( input_offset + length > len )
8477 {
8478 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8479 }
8480
8481 status = psa_pake_input( pake_ctx, step,
8482 buf + input_offset, length );
8483 if( status != PSA_SUCCESS)
8484 {
8485 return psa_ssl_status_to_mbedtls( status );
8486 }
8487
8488 input_offset += length;
8489 }
8490 }
8491
Valerio Setti819de862022-11-17 18:05:19 +01008492 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008493 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008494
Valerio Settia08b1a42022-11-17 15:10:02 +01008495 return( 0 );
8496}
8497
Valerio Setti6b3dab02022-11-17 17:14:54 +01008498int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008499 psa_pake_operation_t *pake_ctx,
8500 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008501 size_t len, size_t *olen,
8502 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008503{
8504 psa_status_t status;
8505 size_t output_offset = 0;
8506 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008507 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008508 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8509 * At round two perform a single cycle
8510 */
8511 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008512
Valerio Setti6b3dab02022-11-17 17:14:54 +01008513 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008514 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008515 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8516 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008517 ++step )
8518 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008519 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008520 * For each step, prepend 1 byte with the length of the data as
8521 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008522 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008523 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008524 buf + output_offset + 1,
8525 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008526 &output_len );
8527 if( status != PSA_SUCCESS )
8528 {
8529 return( psa_ssl_status_to_mbedtls( status ) );
8530 }
8531
Valerio Setti99d88c12022-11-22 16:03:43 +01008532 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008533
8534 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008535 }
8536 }
8537
8538 *olen = output_offset;
8539
8540 return( 0 );
8541}
Valerio Settia08b1a42022-11-17 15:10:02 +01008542#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8543
Jerry Yuee40f9d2022-02-17 14:55:16 +08008544#if defined(MBEDTLS_USE_PSA_CRYPTO)
8545int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8546 unsigned char *hash, size_t *hashlen,
8547 unsigned char *data, size_t data_len,
8548 mbedtls_md_type_t md_alg )
8549{
8550 psa_status_t status;
8551 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008552 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008553
8554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8555
8556 if( ( status = psa_hash_setup( &hash_operation,
8557 hash_alg ) ) != PSA_SUCCESS )
8558 {
8559 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8560 goto exit;
8561 }
8562
8563 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8564 64 ) ) != PSA_SUCCESS )
8565 {
8566 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8567 goto exit;
8568 }
8569
8570 if( ( status = psa_hash_update( &hash_operation,
8571 data, data_len ) ) != PSA_SUCCESS )
8572 {
8573 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8574 goto exit;
8575 }
8576
8577 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8578 hashlen ) ) != PSA_SUCCESS )
8579 {
8580 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8581 goto exit;
8582 }
8583
8584exit:
8585 if( status != PSA_SUCCESS )
8586 {
8587 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8588 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8589 switch( status )
8590 {
8591 case PSA_ERROR_NOT_SUPPORTED:
8592 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8593 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8594 case PSA_ERROR_BUFFER_TOO_SMALL:
8595 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8596 case PSA_ERROR_INSUFFICIENT_MEMORY:
8597 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8598 default:
8599 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8600 }
8601 }
8602 return( 0 );
8603}
8604
8605#else
8606
8607int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8608 unsigned char *hash, size_t *hashlen,
8609 unsigned char *data, size_t data_len,
8610 mbedtls_md_type_t md_alg )
8611{
8612 int ret = 0;
8613 mbedtls_md_context_t ctx;
8614 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8615 *hashlen = mbedtls_md_get_size( md_info );
8616
8617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8618
8619 mbedtls_md_init( &ctx );
8620
8621 /*
8622 * digitally-signed struct {
8623 * opaque client_random[32];
8624 * opaque server_random[32];
8625 * ServerDHParams params;
8626 * };
8627 */
8628 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8629 {
8630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8631 goto exit;
8632 }
8633 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8634 {
8635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8636 goto exit;
8637 }
8638 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8639 {
8640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8641 goto exit;
8642 }
8643 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8644 {
8645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8646 goto exit;
8647 }
8648 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8649 {
8650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8651 goto exit;
8652 }
8653
8654exit:
8655 mbedtls_md_free( &ctx );
8656
8657 if( ret != 0 )
8658 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8659 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8660
8661 return( ret );
8662}
8663#endif /* MBEDTLS_USE_PSA_CRYPTO */
8664
Jerry Yud9d91da2022-02-17 14:57:06 +08008665#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8666
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008667/* Find the preferred hash for a given signature algorithm. */
8668unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8669 mbedtls_ssl_context *ssl,
8670 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008671{
Gabor Mezei078e8032022-04-27 21:17:56 +02008672 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008673 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008674
8675 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008676 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008677
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008678 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008679 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008680 unsigned int hash_alg_received =
8681 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8682 received_sig_algs[i] );
8683 unsigned int sig_alg_received =
8684 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8685 received_sig_algs[i] );
8686
8687 if( sig_alg == sig_alg_received )
8688 {
8689#if defined(MBEDTLS_USE_PSA_CRYPTO)
8690 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8691 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008692 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008693 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008694
Neil Armstrong96eceb82022-06-30 18:05:05 +02008695 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8696 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8697 PSA_ALG_ECDSA( psa_hash_alg ),
8698 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008699 continue;
8700
Neil Armstrong96eceb82022-06-30 18:05:05 +02008701 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008702 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8703 PSA_ALG_RSA_PKCS1V15_SIGN(
8704 psa_hash_alg ),
8705 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008706 continue;
8707 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008708#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008709
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008710 return( hash_alg_received );
8711 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008712 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008713
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008714 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008715}
8716
8717#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8718
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008719/* Serialization of TLS 1.2 sessions:
8720 *
8721 * struct {
8722 * uint64 start_time;
8723 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008724 * uint8 session_id_len; // at most 32
8725 * opaque session_id[32];
8726 * opaque master[48]; // fixed length in the standard
8727 * uint32 verify_result;
8728 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8729 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8730 * uint32 ticket_lifetime;
8731 * uint8 mfl_code; // up to 255 according to standard
8732 * uint8 encrypt_then_mac; // 0 or 1
8733 * } serialized_session_tls12;
8734 *
8735 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008736static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008737 unsigned char *buf,
8738 size_t buf_len )
8739{
8740 unsigned char *p = buf;
8741 size_t used = 0;
8742
8743#if defined(MBEDTLS_HAVE_TIME)
8744 uint64_t start;
8745#endif
8746#if defined(MBEDTLS_X509_CRT_PARSE_C)
8747#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8748 size_t cert_len;
8749#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8750#endif /* MBEDTLS_X509_CRT_PARSE_C */
8751
8752 /*
8753 * Time
8754 */
8755#if defined(MBEDTLS_HAVE_TIME)
8756 used += 8;
8757
8758 if( used <= buf_len )
8759 {
8760 start = (uint64_t) session->start;
8761
8762 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8763 p += 8;
8764 }
8765#endif /* MBEDTLS_HAVE_TIME */
8766
8767 /*
8768 * Basic mandatory fields
8769 */
8770 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008771 + 1 /* id_len */
8772 + sizeof( session->id )
8773 + sizeof( session->master )
8774 + 4; /* verify_result */
8775
8776 if( used <= buf_len )
8777 {
8778 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8779 p += 2;
8780
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008781 *p++ = MBEDTLS_BYTE_0( session->id_len );
8782 memcpy( p, session->id, 32 );
8783 p += 32;
8784
8785 memcpy( p, session->master, 48 );
8786 p += 48;
8787
8788 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8789 p += 4;
8790 }
8791
8792 /*
8793 * Peer's end-entity certificate
8794 */
8795#if defined(MBEDTLS_X509_CRT_PARSE_C)
8796#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8797 if( session->peer_cert == NULL )
8798 cert_len = 0;
8799 else
8800 cert_len = session->peer_cert->raw.len;
8801
8802 used += 3 + cert_len;
8803
8804 if( used <= buf_len )
8805 {
8806 *p++ = MBEDTLS_BYTE_2( cert_len );
8807 *p++ = MBEDTLS_BYTE_1( cert_len );
8808 *p++ = MBEDTLS_BYTE_0( cert_len );
8809
8810 if( session->peer_cert != NULL )
8811 {
8812 memcpy( p, session->peer_cert->raw.p, cert_len );
8813 p += cert_len;
8814 }
8815 }
8816#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8817 if( session->peer_cert_digest != NULL )
8818 {
8819 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8820 if( used <= buf_len )
8821 {
8822 *p++ = (unsigned char) session->peer_cert_digest_type;
8823 *p++ = (unsigned char) session->peer_cert_digest_len;
8824 memcpy( p, session->peer_cert_digest,
8825 session->peer_cert_digest_len );
8826 p += session->peer_cert_digest_len;
8827 }
8828 }
8829 else
8830 {
8831 used += 2;
8832 if( used <= buf_len )
8833 {
8834 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8835 *p++ = 0;
8836 }
8837 }
8838#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8839#endif /* MBEDTLS_X509_CRT_PARSE_C */
8840
8841 /*
8842 * Session ticket if any, plus associated data
8843 */
8844#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8845 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8846
8847 if( used <= buf_len )
8848 {
8849 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8850 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8851 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8852
8853 if( session->ticket != NULL )
8854 {
8855 memcpy( p, session->ticket, session->ticket_len );
8856 p += session->ticket_len;
8857 }
8858
8859 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8860 p += 4;
8861 }
8862#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8863
8864 /*
8865 * Misc extension-related info
8866 */
8867#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8868 used += 1;
8869
8870 if( used <= buf_len )
8871 *p++ = session->mfl_code;
8872#endif
8873
8874#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8875 used += 1;
8876
8877 if( used <= buf_len )
8878 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8879#endif
8880
8881 return( used );
8882}
8883
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008884MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008885static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008886 const unsigned char *buf,
8887 size_t len )
8888{
8889#if defined(MBEDTLS_HAVE_TIME)
8890 uint64_t start;
8891#endif
8892#if defined(MBEDTLS_X509_CRT_PARSE_C)
8893#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8894 size_t cert_len;
8895#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8896#endif /* MBEDTLS_X509_CRT_PARSE_C */
8897
8898 const unsigned char *p = buf;
8899 const unsigned char * const end = buf + len;
8900
8901 /*
8902 * Time
8903 */
8904#if defined(MBEDTLS_HAVE_TIME)
8905 if( 8 > (size_t)( end - p ) )
8906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8907
8908 start = ( (uint64_t) p[0] << 56 ) |
8909 ( (uint64_t) p[1] << 48 ) |
8910 ( (uint64_t) p[2] << 40 ) |
8911 ( (uint64_t) p[3] << 32 ) |
8912 ( (uint64_t) p[4] << 24 ) |
8913 ( (uint64_t) p[5] << 16 ) |
8914 ( (uint64_t) p[6] << 8 ) |
8915 ( (uint64_t) p[7] );
8916 p += 8;
8917
8918 session->start = (time_t) start;
8919#endif /* MBEDTLS_HAVE_TIME */
8920
8921 /*
8922 * Basic mandatory fields
8923 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008924 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8926
8927 session->ciphersuite = ( p[0] << 8 ) | p[1];
8928 p += 2;
8929
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008930 session->id_len = *p++;
8931 memcpy( session->id, p, 32 );
8932 p += 32;
8933
8934 memcpy( session->master, p, 48 );
8935 p += 48;
8936
8937 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8938 ( (uint32_t) p[1] << 16 ) |
8939 ( (uint32_t) p[2] << 8 ) |
8940 ( (uint32_t) p[3] );
8941 p += 4;
8942
8943 /* Immediately clear invalid pointer values that have been read, in case
8944 * we exit early before we replaced them with valid ones. */
8945#if defined(MBEDTLS_X509_CRT_PARSE_C)
8946#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8947 session->peer_cert = NULL;
8948#else
8949 session->peer_cert_digest = NULL;
8950#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8951#endif /* MBEDTLS_X509_CRT_PARSE_C */
8952#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8953 session->ticket = NULL;
8954#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8955
8956 /*
8957 * Peer certificate
8958 */
8959#if defined(MBEDTLS_X509_CRT_PARSE_C)
8960#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8961 /* Deserialize CRT from the end of the ticket. */
8962 if( 3 > (size_t)( end - p ) )
8963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8964
8965 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8966 p += 3;
8967
8968 if( cert_len != 0 )
8969 {
8970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8971
8972 if( cert_len > (size_t)( end - p ) )
8973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8974
8975 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8976
8977 if( session->peer_cert == NULL )
8978 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8979
8980 mbedtls_x509_crt_init( session->peer_cert );
8981
8982 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8983 p, cert_len ) ) != 0 )
8984 {
8985 mbedtls_x509_crt_free( session->peer_cert );
8986 mbedtls_free( session->peer_cert );
8987 session->peer_cert = NULL;
8988 return( ret );
8989 }
8990
8991 p += cert_len;
8992 }
8993#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8994 /* Deserialize CRT digest from the end of the ticket. */
8995 if( 2 > (size_t)( end - p ) )
8996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8997
8998 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8999 session->peer_cert_digest_len = (size_t) *p++;
9000
9001 if( session->peer_cert_digest_len != 0 )
9002 {
9003 const mbedtls_md_info_t *md_info =
9004 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9005 if( md_info == NULL )
9006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9007 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9009
9010 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9012
9013 session->peer_cert_digest =
9014 mbedtls_calloc( 1, session->peer_cert_digest_len );
9015 if( session->peer_cert_digest == NULL )
9016 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9017
9018 memcpy( session->peer_cert_digest, p,
9019 session->peer_cert_digest_len );
9020 p += session->peer_cert_digest_len;
9021 }
9022#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9023#endif /* MBEDTLS_X509_CRT_PARSE_C */
9024
9025 /*
9026 * Session ticket and associated data
9027 */
9028#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9029 if( 3 > (size_t)( end - p ) )
9030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9031
9032 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9033 p += 3;
9034
9035 if( session->ticket_len != 0 )
9036 {
9037 if( session->ticket_len > (size_t)( end - p ) )
9038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9039
9040 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9041 if( session->ticket == NULL )
9042 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9043
9044 memcpy( session->ticket, p, session->ticket_len );
9045 p += session->ticket_len;
9046 }
9047
9048 if( 4 > (size_t)( end - p ) )
9049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9050
9051 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9052 ( (uint32_t) p[1] << 16 ) |
9053 ( (uint32_t) p[2] << 8 ) |
9054 ( (uint32_t) p[3] );
9055 p += 4;
9056#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9057
9058 /*
9059 * Misc extension-related info
9060 */
9061#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9062 if( 1 > (size_t)( end - p ) )
9063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9064
9065 session->mfl_code = *p++;
9066#endif
9067
9068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9069 if( 1 > (size_t)( end - p ) )
9070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9071
9072 session->encrypt_then_mac = *p++;
9073#endif
9074
9075 /* Done, should have consumed entire buffer */
9076 if( p != end )
9077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9078
9079 return( 0 );
9080}
Jerry Yudc7bd172022-02-17 13:44:15 +08009081#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9082
XiaokangQian75d40ef2022-04-20 11:05:24 +00009083int mbedtls_ssl_validate_ciphersuite(
9084 const mbedtls_ssl_context *ssl,
9085 const mbedtls_ssl_ciphersuite_t *suite_info,
9086 mbedtls_ssl_protocol_version min_tls_version,
9087 mbedtls_ssl_protocol_version max_tls_version )
9088{
9089 (void) ssl;
9090
9091 if( suite_info == NULL )
9092 return( -1 );
9093
9094 if( ( suite_info->min_tls_version > max_tls_version ) ||
9095 ( suite_info->max_tls_version < min_tls_version ) )
9096 {
9097 return( -1 );
9098 }
9099
XiaokangQian060d8672022-04-21 09:24:56 +00009100#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009101#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009102#if defined(MBEDTLS_USE_PSA_CRYPTO)
9103 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9104 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9105#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009106 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9107 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009108#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009109 {
9110 return( -1 );
9111 }
9112#endif
9113
9114 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9115#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9116 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9117 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9118 {
9119 return( -1 );
9120 }
9121#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9122#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9123
9124 return( 0 );
9125}
9126
Ronald Crone68ab4f2022-10-05 12:46:29 +02009127#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009128/*
9129 * Function for writing a signature algorithm extension.
9130 *
9131 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9132 * value (TLS 1.3 RFC8446):
9133 * enum {
9134 * ....
9135 * ecdsa_secp256r1_sha256( 0x0403 ),
9136 * ecdsa_secp384r1_sha384( 0x0503 ),
9137 * ecdsa_secp521r1_sha512( 0x0603 ),
9138 * ....
9139 * } SignatureScheme;
9140 *
9141 * struct {
9142 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9143 * } SignatureSchemeList;
9144 *
9145 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9146 * value (TLS 1.2 RFC5246):
9147 * enum {
9148 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9149 * sha512(6), (255)
9150 * } HashAlgorithm;
9151 *
9152 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9153 * SignatureAlgorithm;
9154 *
9155 * struct {
9156 * HashAlgorithm hash;
9157 * SignatureAlgorithm signature;
9158 * } SignatureAndHashAlgorithm;
9159 *
9160 * SignatureAndHashAlgorithm
9161 * supported_signature_algorithms<2..2^16-2>;
9162 *
9163 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9164 * generalization of the TLS 1.2 signature algorithm extension.
9165 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9166 * `SignatureScheme` field of TLS 1.3
9167 *
9168 */
9169int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9170 const unsigned char *end, size_t *out_len )
9171{
9172 unsigned char *p = buf;
9173 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9174 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9175
9176 *out_len = 0;
9177
9178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9179
9180 /* Check if we have space for header and length field:
9181 * - extension_type (2 bytes)
9182 * - extension_data_length (2 bytes)
9183 * - supported_signature_algorithms_length (2 bytes)
9184 */
9185 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9186 p += 6;
9187
9188 /*
9189 * Write supported_signature_algorithms
9190 */
9191 supported_sig_alg = p;
9192 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9193 if( sig_alg == NULL )
9194 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9195
9196 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9197 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9199 *sig_alg,
9200 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009201 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9202 continue;
9203 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9204 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9205 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009207 *sig_alg,
9208 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009209 }
9210
9211 /* Length of supported_signature_algorithms */
9212 supported_sig_alg_len = p - supported_sig_alg;
9213 if( supported_sig_alg_len == 0 )
9214 {
9215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9217 }
9218
XiaokangQianeaf36512022-04-24 09:07:44 +00009219 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009220 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009221 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9222
XiaokangQianeaf36512022-04-24 09:07:44 +00009223 *out_len = p - buf;
9224
9225#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009226 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009227#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009228
XiaokangQianeaf36512022-04-24 09:07:44 +00009229 return( 0 );
9230}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009231#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009232
XiaokangQian40a35232022-05-07 09:02:40 +00009233#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009234/*
9235 * mbedtls_ssl_parse_server_name_ext
9236 *
9237 * Structure of server_name extension:
9238 *
9239 * enum {
9240 * host_name(0), (255)
9241 * } NameType;
9242 * opaque HostName<1..2^16-1>;
9243 *
9244 * struct {
9245 * NameType name_type;
9246 * select (name_type) {
9247 * case host_name: HostName;
9248 * } name;
9249 * } ServerName;
9250 * struct {
9251 * ServerName server_name_list<1..2^16-1>
9252 * } ServerNameList;
9253 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009254MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009255int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9256 const unsigned char *buf,
9257 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009258{
9259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9260 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009261 size_t server_name_list_len, hostname_len;
9262 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009263
XiaokangQianf2a94202022-05-20 06:44:24 +00009264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009265
9266 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009267 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009268 p += 2;
9269
XiaokangQian9b2b7712022-05-17 02:57:00 +00009270 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9271 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009272 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009273 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009274 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009275 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009276 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9277 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009278
9279 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9280 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009281 /* sni_name is intended to be used only during the parsing of the
9282 * ClientHello message (it is reset to NULL before the end of
9283 * the message parsing). Thus it is ok to just point to the
9284 * reception buffer and not make a copy of it.
9285 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009286 ssl->handshake->sni_name = p + 3;
9287 ssl->handshake->sni_name_len = hostname_len;
9288 if( ssl->conf->f_sni == NULL )
9289 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009290 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009291 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009292 if( ret != 0 )
9293 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009294 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009295 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9296 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009297 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9298 }
9299 return( 0 );
9300 }
9301
9302 p += hostname_len + 3;
9303 }
9304
9305 return( 0 );
9306}
9307#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9308
XiaokangQianacb39922022-06-17 10:18:48 +00009309#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009310MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009311int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9312 const unsigned char *buf,
9313 const unsigned char *end )
9314{
9315 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009316 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009317 const unsigned char *protocol_name_list;
9318 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009319 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009320
9321 /* If ALPN not configured, just ignore the extension */
9322 if( ssl->conf->alpn_list == NULL )
9323 return( 0 );
9324
9325 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009326 * RFC7301, section 3.1
9327 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009328 *
XiaokangQianc7403452022-06-23 03:24:12 +00009329 * struct {
9330 * ProtocolName protocol_name_list<2..2^16-1>
9331 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009332 */
9333
XiaokangQianc7403452022-06-23 03:24:12 +00009334 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009335 * protocol_name_list_len 2 bytes
9336 * protocol_name_len 1 bytes
9337 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009338 */
XiaokangQianacb39922022-06-17 10:18:48 +00009339 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9340
XiaokangQianc7403452022-06-23 03:24:12 +00009341 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009342 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009343 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009344 protocol_name_list = p;
9345 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009346
9347 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009348 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009349 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009350 protocol_name_len = *p++;
9351 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9352 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009353 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009354 {
9355 MBEDTLS_SSL_PEND_FATAL_ALERT(
9356 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9357 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009358 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009359 }
9360
9361 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009362 }
9363
9364 /* Use our order of preference */
9365 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9366 {
9367 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009368 p = protocol_name_list;
9369 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009370 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009371 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009372 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009373 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009374 {
9375 ssl->alpn_chosen = *alpn;
9376 return( 0 );
9377 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009378
9379 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009380 }
9381 }
9382
XiaokangQian95d5f542022-06-24 02:29:26 +00009383 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009384 MBEDTLS_SSL_PEND_FATAL_ALERT(
9385 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9386 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9387 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9388}
9389
9390int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9391 unsigned char *buf,
9392 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009393 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009394{
9395 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009396 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009397 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009398
9399 if( ssl->alpn_chosen == NULL )
9400 {
9401 return( 0 );
9402 }
9403
XiaokangQian95d5f542022-06-24 02:29:26 +00009404 protocol_name_len = strlen( ssl->alpn_chosen );
9405 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009406
9407 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9408 /*
9409 * 0 . 1 ext identifier
9410 * 2 . 3 ext length
9411 * 4 . 5 protocol list length
9412 * 6 . 6 protocol name length
9413 * 7 . 7+n protocol name
9414 */
9415 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9416
XiaokangQian95d5f542022-06-24 02:29:26 +00009417 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009418
XiaokangQian95d5f542022-06-24 02:29:26 +00009419 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9420 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009421 /* Note: the length of the chosen protocol has been checked to be less
9422 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9423 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009424 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009425
XiaokangQian95d5f542022-06-24 02:29:26 +00009426 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009427
9428#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9429 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9430#endif
9431
XiaokangQianacb39922022-06-17 10:18:48 +00009432 return ( 0 );
9433}
9434#endif /* MBEDTLS_SSL_ALPN */
9435
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009436#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009437 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009438 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009439 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009440int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9441 const char *hostname )
9442{
9443 /* Initialize to suppress unnecessary compiler warning */
9444 size_t hostname_len = 0;
9445
9446 /* Check if new hostname is valid before
9447 * making any change to current one */
9448 if( hostname != NULL )
9449 {
9450 hostname_len = strlen( hostname );
9451
9452 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9454 }
9455
9456 /* Now it's clear that we will overwrite the old hostname,
9457 * so we can free it safely */
9458 if( session->hostname != NULL )
9459 {
9460 mbedtls_platform_zeroize( session->hostname,
9461 strlen( session->hostname ) );
9462 mbedtls_free( session->hostname );
9463 }
9464
9465 /* Passing NULL as hostname shall clear the old one */
9466 if( hostname == NULL )
9467 {
9468 session->hostname = NULL;
9469 }
9470 else
9471 {
9472 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9473 if( session->hostname == NULL )
9474 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9475
9476 memcpy( session->hostname, hostname, hostname_len );
9477 }
9478
9479 return( 0 );
9480}
Xiaokang Qian03409292022-10-12 02:49:52 +00009481#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9482 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009483 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009484 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#endif /* MBEDTLS_SSL_TLS_C */