blob: f1d286c7d7d7ed7abcdd0420cf0656fff340bffa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Jerry Yu6848a612022-10-27 13:03:26 +0800171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yud25cab02022-10-31 12:48:30 +0800619#if defined(MBEDTLS_DEBUG_C)
620static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800621 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800622 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
623 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
624 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
625 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
626 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
627 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
628 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
629 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
630 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
631 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
632 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
633 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
634 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
635 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
636 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
637 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
638 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
639 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
640 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
641 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
642 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
643 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
644 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
646 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
647 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
648 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
649};
650
Jerry Yuea52ed92022-11-08 21:01:17 +0800651static unsigned int extension_type_table[]={
Jerry Yud25cab02022-10-31 12:48:30 +0800652 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
653 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
654 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
655 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
657 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
658 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
659 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
660 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
661 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
662 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
664 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
665 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
666 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
668 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
669 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
670 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
671 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
672 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
673 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
674 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
675 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
676 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
677 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
678 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
679 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
680};
681
682const char *mbedtls_ssl_get_extension_name( unsigned int extension_type )
683{
684 return( extension_name_table[
685 mbedtls_ssl_get_extension_id( extension_type ) ] );
686}
687
688static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type )
689{
690 switch( hs_msg_type )
691 {
692 case MBEDTLS_SSL_HS_CLIENT_HELLO:
693 return( "ClientHello" );
694 case MBEDTLS_SSL_HS_SERVER_HELLO:
695 return( "ServerHello" );
696 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
697 return( "HelloRetryRequest" );
698 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
699 return( "NewSessionTicket" );
700 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
701 return( "EncryptedExtensions" );
702 case MBEDTLS_SSL_HS_CERTIFICATE:
703 return( "Certificate" );
704 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
705 return( "CertificateRequest" );
706 }
Jerry Yu79aa7212022-11-08 21:30:21 +0800707 return( "Unknown" );
Jerry Yud25cab02022-10-31 12:48:30 +0800708}
709
Jerry Yu79aa7212022-11-08 21:30:21 +0800710void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type, unsigned int extension_type,
713 const char *extra_msg0, const char *extra_msg1 )
Jerry Yud25cab02022-10-31 12:48:30 +0800714{
715 const char *extra_msg;
716 if( extra_msg0 && extra_msg1 )
717 {
718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
720 "%s: %s(%u) extension %s %s.",
721 ssl_tls13_get_hs_msg_name( hs_msg_type ),
722 mbedtls_ssl_get_extension_name( extension_type ),
723 extension_type,
724 extra_msg0, extra_msg1 );
725 return;
726 }
727
728 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
729 if( extra_msg )
730 {
731 mbedtls_debug_print_msg(
732 ssl, level, file, line,
733 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
734 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
735 extra_msg );
736 return;
737 }
738
739 mbedtls_debug_print_msg(
740 ssl, level, file, line,
741 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
742 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
743}
744
745void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
746 int level, const char *file, int line,
747 int hs_msg_type, uint32_t extensions_mask,
748 const char *extra )
749{
750
751 for( unsigned i = 0;
752 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
753 i++ )
754 {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yu97be6a92022-11-09 22:43:31 +0800757 extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra );
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
761#endif /* MBEDTLS_DEBUG_C */
762
Jerry Yuc73c6182022-02-08 20:29:25 +0800763void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
765{
766 ((void) ciphersuite_info);
767
Andrzej Kurek25f27152022-08-17 16:09:31 -0400768#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800769 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
770 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
771 else
772#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400773#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
775 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
776 else
777#endif
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
780 return;
781 }
782}
783
XiaokangQianadab9a62022-07-18 07:41:26 +0000784void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
785 unsigned hs_type,
786 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100787{
788 unsigned char hs_hdr[4];
789
790 /* Build HS header for checksum update. */
791 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
792 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
793 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
794 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
795
796 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
797}
798
799void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
800 unsigned hs_type,
801 unsigned char const *msg,
802 size_t msg_len )
803{
804 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
805 ssl->handshake->update_checksum( ssl, msg, msg_len );
806}
807
Jerry Yuc73c6182022-02-08 20:29:25 +0800808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
809{
810 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#if defined(MBEDTLS_USE_PSA_CRYPTO)
813 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
814 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
815#else
816 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
817#endif
818#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400819#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
822 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
823#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400824 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#endif
826#endif
827}
828
829static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
830 const unsigned char *buf, size_t len )
831{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
834 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
835#else
836 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
837#endif
838#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
842#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400843 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400846#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
847 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
848 (void) ssl;
849 (void) buf;
850 (void) len;
851#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800852}
853
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
856 const unsigned char *buf, size_t len )
857{
858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
860#else
861 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
862#endif
863}
864#endif
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
868 const unsigned char *buf, size_t len )
869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
871 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
872#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400873 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875}
876#endif
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881
Andrzej Kurek25f27152022-08-17 16:09:31 -0400882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884 handshake->fin_sha256_psa = psa_hash_operation_init();
885 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200888 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500893 handshake->fin_sha384_psa = psa_hash_operation_init();
894 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400896 mbedtls_sha512_init( &handshake->fin_sha384 );
897 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900
901 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_DHM_C)
904 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200906#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200910#if defined(MBEDTLS_USE_PSA_CRYPTO)
911 handshake->psa_pake_ctx = psa_pake_operation_init();
912 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
913#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200914 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200916#if defined(MBEDTLS_SSL_CLI_C)
917 handshake->ecjpake_cache = NULL;
918 handshake->ecjpake_cache_len = 0;
919#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200920#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200921
Gilles Peskineeccd8882020-03-10 12:19:08 +0100922#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200923 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200924#endif
925
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
927 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
928#endif
Hanno Becker75173122019-02-06 16:18:31 +0000929
930#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
931 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
932 mbedtls_pk_init( &handshake->peer_pubkey );
933#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934}
935
Hanno Beckera18d1322018-01-03 14:27:32 +0000936void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200939
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
942 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100943#else
944 mbedtls_cipher_init( &transform->cipher_ctx_enc );
945 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100946#endif
947
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
951 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100952#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_md_init( &transform->md_ctx_enc );
954 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000955#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100956#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957}
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962}
963
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200964MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000966{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000968 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200973 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974
975 /*
976 * Either the pointers are now NULL or cleared properly and can be freed.
977 * Now allocate missing structures.
978 */
979 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200981 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Paul Bakker48916f92012-09-16 19:57:18 +0000988
Paul Bakker82788fb2014-10-20 13:59:19 +0200989 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200991 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200992 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500993#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
994 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400995
Andrzej Kurek4a063792020-10-21 15:08:44 +0200996 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
997 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500998#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000999
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001000 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00001001 if( ssl->handshake == NULL ||
1002 ssl->transform_negotiate == NULL ||
1003 ssl->session_negotiate == NULL )
1004 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_free( ssl->handshake );
1008 mbedtls_free( ssl->transform_negotiate );
1009 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010
1011 ssl->handshake = NULL;
1012 ssl->transform_negotiate = NULL;
1013 ssl->session_negotiate = NULL;
1014
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001015 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001016 }
1017
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001020 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001021 ssl_handshake_params_init( ssl->handshake );
1022
Jerry Yud0766ec2022-09-22 10:46:57 +08001023#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1024 defined(MBEDTLS_SSL_SRV_C) && \
1025 defined(MBEDTLS_SSL_SESSION_TICKETS)
1026 ssl->handshake->new_session_tickets_count =
1027 ssl->conf->new_session_tickets_count ;
1028#endif
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1032 {
1033 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001034
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1036 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1037 else
1038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001039
Hanno Becker0f57a652020-02-05 10:37:26 +00001040 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001041 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001042#endif
1043
Brett Warrene0edc842021-08-17 09:53:13 +01001044/*
1045 * curve_list is translated to IANA TLS group identifiers here because
1046 * mbedtls_ssl_conf_curves returns void and so can't return
1047 * any error codes.
1048 */
1049#if defined(MBEDTLS_ECP_C)
1050#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1051 /* Heap allocate and translate curve_list from internal to IANA group ids */
1052 if ( ssl->conf->curve_list != NULL )
1053 {
1054 size_t length;
1055 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1056
1057 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1058 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1059
1060 /* Leave room for zero termination */
1061 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1062 if ( group_list == NULL )
1063 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1064
1065 for( size_t i = 0; i < length; i++ )
1066 {
1067 const mbedtls_ecp_curve_info *info =
1068 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1069 if ( info == NULL )
1070 {
1071 mbedtls_free( group_list );
1072 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1073 }
1074 group_list[i] = info->tls_id;
1075 }
1076
1077 group_list[length] = 0;
1078
1079 ssl->handshake->group_list = group_list;
1080 ssl->handshake->group_list_heap_allocated = 1;
1081 }
1082 else
1083 {
1084 ssl->handshake->group_list = ssl->conf->group_list;
1085 ssl->handshake->group_list_heap_allocated = 0;
1086 }
1087#endif /* MBEDTLS_DEPRECATED_REMOVED */
1088#endif /* MBEDTLS_ECP_C */
1089
Ronald Crone68ab4f2022-10-05 12:46:29 +02001090#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001091#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001093 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1094 signature algorithms IANA identifiers. */
1095 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001096 ssl->conf->sig_hashes != NULL )
1097 {
1098 const int *md;
1099 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001100 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001101 uint16_t *p;
1102
Jerry Yub476a442022-01-21 18:14:45 +08001103#if defined(static_assert)
1104 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1105 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1106 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001107#endif
1108
Jerry Yuf017ee42022-01-12 15:49:48 +08001109 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1110 {
1111 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1112 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001113#if defined(MBEDTLS_ECDSA_C)
1114 sig_algs_len += sizeof( uint16_t );
1115#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001116
Jerry Yub476a442022-01-21 18:14:45 +08001117#if defined(MBEDTLS_RSA_C)
1118 sig_algs_len += sizeof( uint16_t );
1119#endif
1120 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1121 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001122 }
1123
Jerry Yub476a442022-01-21 18:14:45 +08001124 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001125 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1126
Jerry Yub476a442022-01-21 18:14:45 +08001127 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1128 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001129 if( ssl->handshake->sig_algs == NULL )
1130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1131
1132 p = (uint16_t *)ssl->handshake->sig_algs;
1133 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1134 {
1135 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1136 if( hash == MBEDTLS_SSL_HASH_NONE )
1137 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001138#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001139 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1140 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001141#endif
1142#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1144 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001145#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001146 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001147 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001148 ssl->handshake->sig_algs_heap_allocated = 1;
1149 }
1150 else
Jerry Yua69269a2022-01-17 21:06:01 +08001151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001152 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001153 ssl->handshake->sig_algs_heap_allocated = 0;
1154 }
Jerry Yucc539102022-06-27 16:27:35 +08001155#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001156#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001161/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001162MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001163static int ssl_cookie_write_dummy( void *ctx,
1164 unsigned char **p, unsigned char *end,
1165 const unsigned char *cli_id, size_t cli_id_len )
1166{
1167 ((void) ctx);
1168 ((void) p);
1169 ((void) end);
1170 ((void) cli_id);
1171 ((void) cli_id_len);
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001174}
1175
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001176MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001177static int ssl_cookie_check_dummy( void *ctx,
1178 const unsigned char *cookie, size_t cookie_len,
1179 const unsigned char *cli_id, size_t cli_id_len )
1180{
1181 ((void) ctx);
1182 ((void) cookie);
1183 ((void) cookie_len);
1184 ((void) cli_id);
1185 ((void) cli_id_len);
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001188}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001189#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001190
Paul Bakker5121ce52009-01-03 21:22:43 +00001191/*
1192 * Initialize an SSL context
1193 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001194void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1195{
1196 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1197}
1198
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001199MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001200static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1201{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001202 const mbedtls_ssl_config *conf = ssl->conf;
1203
Ronald Cron6f135e12021-12-08 16:57:54 +01001204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001205 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1206 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1208 {
1209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1211 }
1212
Jerry Yu60835a82021-08-04 10:13:52 +08001213 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1214 return( 0 );
1215 }
1216#endif
1217
1218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001219 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1222 return( 0 );
1223 }
1224#endif
1225
Ronald Cron6f135e12021-12-08 16:57:54 +01001226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001227 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001228 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1230 {
1231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1232 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1233 }
1234
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001235 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1236 {
1237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1238 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1239 }
1240
1241
Ronald Crone1d3f062022-02-10 14:50:54 +01001242 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1243 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001244 }
1245#endif
1246
1247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1248 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1249}
1250
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001251MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001252static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1253{
1254 int ret;
1255 ret = ssl_conf_version_check( ssl );
1256 if( ret != 0 )
1257 return( ret );
1258
Jerry Yudef7ae42022-10-30 14:13:19 +08001259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1260 /* RFC 8446 section 4.4.3
1261 *
1262 * If the verification fails, the receiver MUST terminate the handshake with
1263 * a "decrypt_error" alert.
1264 *
1265 * If the client is configured as TLS 1.3 only with optional verify, return
1266 * bad config.
1267 *
1268 */
1269 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1270 (mbedtls_ssl_context *)ssl ) &&
1271 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1272 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1273 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1274 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1275 {
1276 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001277 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001278 "is not available for TLS 1.3 client" ) );
1279 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1280 }
1281#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1282
Jerry Yu60835a82021-08-04 10:13:52 +08001283 /* Space for further checks */
1284
1285 return( 0 );
1286}
1287
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001288/*
1289 * Setup an SSL context
1290 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001291
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001292int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001293 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294{
Janos Follath865b3eb2019-12-16 11:46:15 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001296 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1297 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001299 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001300
Jerry Yu60835a82021-08-04 10:13:52 +08001301 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1302 return( ret );
1303
Paul Bakker62f2dee2012-09-28 07:31:51 +00001304 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001305 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001306 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001307
1308 /* Set to NULL in case of an error condition */
1309 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001310
Darryl Greenb33cc762019-11-28 14:29:44 +00001311#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1312 ssl->in_buf_len = in_buf_len;
1313#endif
1314 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001315 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001318 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001319 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001320 }
1321
Darryl Greenb33cc762019-11-28 14:29:44 +00001322#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1323 ssl->out_buf_len = out_buf_len;
1324#endif
1325 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001326 if( ssl->out_buf == NULL )
1327 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001330 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
1332
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001333 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001334
Johan Pascalb62bb512015-12-03 21:56:45 +01001335#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001336 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001337#endif
1338
Paul Bakker48916f92012-09-16 19:57:18 +00001339 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001340 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001343
1344error:
1345 mbedtls_free( ssl->in_buf );
1346 mbedtls_free( ssl->out_buf );
1347
1348 ssl->conf = NULL;
1349
Darryl Greenb33cc762019-11-28 14:29:44 +00001350#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1351 ssl->in_buf_len = 0;
1352 ssl->out_buf_len = 0;
1353#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001354 ssl->in_buf = NULL;
1355 ssl->out_buf = NULL;
1356
1357 ssl->in_hdr = NULL;
1358 ssl->in_ctr = NULL;
1359 ssl->in_len = NULL;
1360 ssl->in_iv = NULL;
1361 ssl->in_msg = NULL;
1362
1363 ssl->out_hdr = NULL;
1364 ssl->out_ctr = NULL;
1365 ssl->out_len = NULL;
1366 ssl->out_iv = NULL;
1367 ssl->out_msg = NULL;
1368
k-stachowiak9f7798e2018-07-31 16:52:32 +02001369 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370}
1371
1372/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001373 * Reset an initialized and used SSL context for re-use while retaining
1374 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001375 *
1376 * If partial is non-zero, keep data in the input buffer and client ID.
1377 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001378 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001379void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1380 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001381{
Darryl Greenb33cc762019-11-28 14:29:44 +00001382#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1383 size_t in_buf_len = ssl->in_buf_len;
1384 size_t out_buf_len = ssl->out_buf_len;
1385#else
1386 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1388#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001389
Hanno Beckerb0302c42021-08-03 09:39:42 +01001390#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1391 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001392#endif
1393
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001394 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001395 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001396
Hanno Beckerb0302c42021-08-03 09:39:42 +01001397 mbedtls_ssl_reset_in_out_pointers( ssl );
1398
1399 /* Reset incoming message parsing */
1400 ssl->in_offt = NULL;
1401 ssl->nb_zero = 0;
1402 ssl->in_msgtype = 0;
1403 ssl->in_msglen = 0;
1404 ssl->in_hslen = 0;
1405 ssl->keep_current_message = 0;
1406 ssl->transform_in = NULL;
1407
1408#if defined(MBEDTLS_SSL_PROTO_DTLS)
1409 ssl->next_record_offset = 0;
1410 ssl->in_epoch = 0;
1411#endif
1412
1413 /* Keep current datagram if partial == 1 */
1414 if( partial == 0 )
1415 {
1416 ssl->in_left = 0;
1417 memset( ssl->in_buf, 0, in_buf_len );
1418 }
1419
Ronald Cronad8c17b2022-06-10 17:18:09 +02001420 ssl->send_alert = 0;
1421
Hanno Beckerb0302c42021-08-03 09:39:42 +01001422 /* Reset outgoing message writing */
1423 ssl->out_msgtype = 0;
1424 ssl->out_msglen = 0;
1425 ssl->out_left = 0;
1426 memset( ssl->out_buf, 0, out_buf_len );
1427 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1428 ssl->transform_out = NULL;
1429
1430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1431 mbedtls_ssl_dtls_replay_reset( ssl );
1432#endif
1433
XiaokangQian2b01dc32022-01-21 02:53:13 +00001434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435 if( ssl->transform )
1436 {
1437 mbedtls_ssl_transform_free( ssl->transform );
1438 mbedtls_free( ssl->transform );
1439 ssl->transform = NULL;
1440 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1442
XiaokangQian2b01dc32022-01-21 02:53:13 +00001443#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1444 mbedtls_ssl_transform_free( ssl->transform_application );
1445 mbedtls_free( ssl->transform_application );
1446 ssl->transform_application = NULL;
1447
1448 if( ssl->handshake != NULL )
1449 {
1450 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1451 mbedtls_free( ssl->handshake->transform_earlydata );
1452 ssl->handshake->transform_earlydata = NULL;
1453
1454 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1455 mbedtls_free( ssl->handshake->transform_handshake );
1456 ssl->handshake->transform_handshake = NULL;
1457 }
1458
XiaokangQian2b01dc32022-01-21 02:53:13 +00001459#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001460}
1461
1462int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1463{
1464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1465
1466 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1467
XiaokangQian78b1fa72022-01-19 06:56:30 +00001468 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001469
1470 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_RENEGOTIATION)
1472 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001473 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001474
1475 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1477 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001480
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001482 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001483 if( ssl->session )
1484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_ssl_session_free( ssl->session );
1486 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001487 ssl->session = NULL;
1488 }
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001491 ssl->alpn_chosen = NULL;
1492#endif
1493
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001494#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001495 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001496#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001497 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001498#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001499 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001500 {
1501 mbedtls_free( ssl->cli_id );
1502 ssl->cli_id = NULL;
1503 ssl->cli_id_len = 0;
1504 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001505#endif
1506
Paul Bakker48916f92012-09-16 19:57:18 +00001507 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1508 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001509
1510 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001511}
1512
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001513/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001514 * Reset an initialized and used SSL context for re-use while retaining
1515 * all application-set variables, function pointers and data.
1516 */
1517int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1518{
Hanno Becker43aefe22020-02-05 10:44:56 +00001519 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001520}
1521
1522/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 * SSL set accessors
1524 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001525void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001527 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001530void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001532 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001533}
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001536void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001538 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001539}
1540#endif
1541
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001542void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001544 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001545}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001548
Hanno Becker1841b0a2018-08-24 11:13:57 +01001549void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1550 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001551{
1552 ssl->disable_datagram_packing = !allow_packing;
1553}
1554
1555void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1556 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001558 conf->hs_timeout_min = min;
1559 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001560}
1561#endif
1562
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001563void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001565 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566}
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001569void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001570 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001571 void *p_vrfy )
1572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001573 conf->f_vrfy = f_vrfy;
1574 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001577
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001578void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001579 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 void *p_rng )
1581{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001582 conf->f_rng = f_rng;
1583 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001584}
1585
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001586void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001587 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 void *p_dbg )
1589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001590 conf->f_dbg = f_dbg;
1591 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001592}
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001595 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001596 mbedtls_ssl_send_t *f_send,
1597 mbedtls_ssl_recv_t *f_recv,
1598 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001599{
1600 ssl->p_bio = p_bio;
1601 ssl->f_send = f_send;
1602 ssl->f_recv = f_recv;
1603 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001604}
1605
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001606#if defined(MBEDTLS_SSL_PROTO_DTLS)
1607void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1608{
1609 ssl->mtu = mtu;
1610}
1611#endif
1612
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001613void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001614{
1615 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001616}
1617
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001618void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1619 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001620 mbedtls_ssl_set_timer_t *f_set_timer,
1621 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001622{
1623 ssl->p_timer = p_timer;
1624 ssl->f_set_timer = f_set_timer;
1625 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001626
1627 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001628 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001629}
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001632void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001633 void *p_cache,
1634 mbedtls_ssl_cache_get_t *f_get_cache,
1635 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001636{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001637 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001638 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001639 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_CLI_C)
1644int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001645{
Janos Follath865b3eb2019-12-16 11:46:15 +00001646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001647
1648 if( ssl == NULL ||
1649 session == NULL ||
1650 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001651 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654 }
1655
Hanno Beckere810bbc2021-05-14 16:01:05 +01001656 if( ssl->handshake->resume == 1 )
1657 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1658
Jerry Yu21092062022-10-10 21:21:31 +08001659#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1660 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001661 {
Jerry Yu21092062022-10-10 21:21:31 +08001662 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1663 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1664
1665 if( mbedtls_ssl_validate_ciphersuite(
1666 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1667 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1668 {
1669 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1670 session->ciphersuite ) );
1671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1672 }
Jerry Yu40afab62022-10-08 10:42:13 +08001673 }
Jerry Yu21092062022-10-10 21:21:31 +08001674#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001675
Hanno Becker52055ae2019-02-06 14:30:46 +00001676 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1677 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001678 return( ret );
1679
Paul Bakker0a597072012-09-25 21:55:46 +00001680 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001681
1682 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001686void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1687 const int *ciphersuites )
1688{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001689 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690}
1691
Ronald Cron6f135e12021-12-08 16:57:54 +01001692#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001693void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001694 const int kex_modes )
1695{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001696 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001697}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001698
1699#if defined(MBEDTLS_SSL_EARLY_DATA)
1700void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001701 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001702{
1703 conf->early_data_enabled = early_data_enabled;
1704}
1705#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001706#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001709void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001710 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001711{
1712 conf->cert_profile = profile;
1713}
1714
Glenn Strauss36872db2022-01-22 05:06:31 -05001715static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1716{
1717 mbedtls_ssl_key_cert *cur = key_cert, *next;
1718
1719 while( cur != NULL )
1720 {
1721 next = cur->next;
1722 mbedtls_free( cur );
1723 cur = next;
1724 }
1725}
1726
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001727/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001728MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001729static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1730 mbedtls_x509_crt *cert,
1731 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001732{
niisato8ee24222018-06-25 19:05:48 +09001733 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001734
Glenn Strauss36872db2022-01-22 05:06:31 -05001735 if( cert == NULL )
1736 {
1737 /* Free list if cert is null */
1738 ssl_key_cert_free( *head );
1739 *head = NULL;
1740 return( 0 );
1741 }
1742
niisato8ee24222018-06-25 19:05:48 +09001743 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1744 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001746
niisato8ee24222018-06-25 19:05:48 +09001747 new_cert->cert = cert;
1748 new_cert->key = key;
1749 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001750
Glenn Strauss36872db2022-01-22 05:06:31 -05001751 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001752 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001753 {
niisato8ee24222018-06-25 19:05:48 +09001754 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001755 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001756 else
1757 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001758 mbedtls_ssl_key_cert *cur = *head;
1759 while( cur->next != NULL )
1760 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001761 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001762 }
1763
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001764 return( 0 );
1765}
1766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001767int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001768 mbedtls_x509_crt *own_cert,
1769 mbedtls_pk_context *pk_key )
1770{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001771 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001772}
1773
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001774void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001775 mbedtls_x509_crt *ca_chain,
1776 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001778 conf->ca_chain = ca_chain;
1779 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001780
1781#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1782 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1783 * cannot be used together. */
1784 conf->f_ca_cb = NULL;
1785 conf->p_ca_cb = NULL;
1786#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001787}
Hanno Becker5adaad92019-03-27 16:54:37 +00001788
1789#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1790void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001791 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001792 void *p_ca_cb )
1793{
1794 conf->f_ca_cb = f_ca_cb;
1795 conf->p_ca_cb = p_ca_cb;
1796
1797 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1798 * cannot be used together. */
1799 conf->ca_chain = NULL;
1800 conf->ca_crl = NULL;
1801}
1802#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001804
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001805#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001806const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1807 size_t *name_len )
1808{
1809 *name_len = ssl->handshake->sni_name_len;
1810 return( ssl->handshake->sni_name );
1811}
1812
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001813int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1814 mbedtls_x509_crt *own_cert,
1815 mbedtls_pk_context *pk_key )
1816{
1817 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1818 own_cert, pk_key ) );
1819}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001820
1821void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1822 mbedtls_x509_crt *ca_chain,
1823 mbedtls_x509_crl *ca_crl )
1824{
1825 ssl->handshake->sni_ca_chain = ca_chain;
1826 ssl->handshake->sni_ca_crl = ca_crl;
1827}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001828
Glenn Strauss999ef702022-03-11 01:37:23 -05001829#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1830void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1831 const mbedtls_x509_crt *crt)
1832{
1833 ssl->handshake->dn_hints = crt;
1834}
1835#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1836
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001837void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1838 int authmode )
1839{
1840 ssl->handshake->sni_authmode = authmode;
1841}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001842#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1843
Hanno Becker8927c832019-04-03 12:52:50 +01001844#if defined(MBEDTLS_X509_CRT_PARSE_C)
1845void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1846 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1847 void *p_vrfy )
1848{
1849 ssl->f_vrfy = f_vrfy;
1850 ssl->p_vrfy = p_vrfy;
1851}
1852#endif
1853
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001854#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001855/*
1856 * Set EC J-PAKE password for current handshake
1857 */
Valerio Setti02c25b52022-11-15 14:08:42 +01001858#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001859int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1860 const unsigned char *pw,
1861 size_t pw_len )
1862{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001863 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1865 psa_pake_role_t psa_role;
1866 psa_status_t status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001867
Janos Follath8eb64132016-06-03 15:40:57 +01001868 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1870
Neil Armstrongca7d5062022-05-31 14:43:23 +02001871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1872 psa_role = PSA_PAKE_ROLE_SERVER;
1873 else
1874 psa_role = PSA_PAKE_ROLE_CLIENT;
1875
Valerio Settiaca21b72022-11-17 18:17:01 +01001876 /* Empty password is not valid */
1877 if( ( pw == NULL) || ( pw_len == 0 ) )
1878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001879
Valerio Settiaca21b72022-11-17 18:17:01 +01001880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1881 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1882 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001883
Valerio Settiaca21b72022-11-17 18:17:01 +01001884 status = psa_import_key( &attributes, pw, pw_len,
1885 &ssl->handshake->psa_pake_password );
1886 if( status != PSA_SUCCESS )
1887 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001888
1889 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1890 psa_pake_cs_set_primitive( &cipher_suite,
1891 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1892 PSA_ECC_FAMILY_SECP_R1,
1893 256) );
1894 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1895
1896 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1897 if( status != PSA_SUCCESS )
1898 {
1899 psa_destroy_key( ssl->handshake->psa_pake_password );
1900 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1901 }
1902
1903 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1904 if( status != PSA_SUCCESS )
1905 {
1906 psa_destroy_key( ssl->handshake->psa_pake_password );
1907 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1908 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1909 }
1910
Valerio Settiaca21b72022-11-17 18:17:01 +01001911 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1912 ssl->handshake->psa_pake_password );
1913 if( status != PSA_SUCCESS )
Neil Armstrongca7d5062022-05-31 14:43:23 +02001914 {
Valerio Settiaca21b72022-11-17 18:17:01 +01001915 psa_destroy_key( ssl->handshake->psa_pake_password );
1916 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1917 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001918 }
1919
1920 ssl->handshake->psa_pake_ctx_is_ok = 1;
1921
1922 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001923}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001924
1925int mbedtls_ssl_set_hs_ecjpake_password_opaque( mbedtls_ssl_context *ssl,
1926 mbedtls_svc_key_id_t pwd )
1927{
1928 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1929 psa_pake_role_t psa_role;
1930 psa_status_t status;
1931
1932 if( ssl->handshake == NULL || ssl->conf == NULL )
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934
1935 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1936 psa_role = PSA_PAKE_ROLE_SERVER;
1937 else
1938 psa_role = PSA_PAKE_ROLE_CLIENT;
1939
1940 if( mbedtls_svc_key_id_is_null( pwd ) )
1941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1942 ssl->handshake->psa_pake_password = pwd;
1943
1944 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1945 psa_pake_cs_set_primitive( &cipher_suite,
1946 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1947 PSA_ECC_FAMILY_SECP_R1,
1948 256) );
1949 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1950
1951 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1952 if( status != PSA_SUCCESS )
1953 {
1954 psa_destroy_key( ssl->handshake->psa_pake_password );
1955 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1956 }
1957
1958 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1959 if( status != PSA_SUCCESS )
1960 {
1961 psa_destroy_key( ssl->handshake->psa_pake_password );
1962 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1963 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1964 }
1965
1966 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1967 ssl->handshake->psa_pake_password );
1968 if( status != PSA_SUCCESS )
1969 {
1970 psa_destroy_key( ssl->handshake->psa_pake_password );
1971 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1972 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1973 }
1974
1975 ssl->handshake->psa_pake_ctx_is_ok = 1;
1976
1977 return( 0 );
1978}
Valerio Setti02c25b52022-11-15 14:08:42 +01001979#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001980int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1981 const unsigned char *pw,
1982 size_t pw_len )
1983{
1984 mbedtls_ecjpake_role role;
1985
1986 if( ssl->handshake == NULL || ssl->conf == NULL )
1987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1988
1989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1990 role = MBEDTLS_ECJPAKE_SERVER;
1991 else
1992 role = MBEDTLS_ECJPAKE_CLIENT;
1993
1994 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1995 role,
1996 MBEDTLS_MD_SHA256,
1997 MBEDTLS_ECP_DP_SECP256R1,
1998 pw, pw_len ) );
1999}
Valerio Setti02c25b52022-11-15 14:08:42 +01002000#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002001#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002002
Ronald Cron73fe8df2022-10-05 14:31:43 +02002003#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02002004int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002005{
Ronald Crond29e13e2022-10-19 10:33:48 +02002006 if( conf->psk_identity == NULL ||
2007 conf->psk_identity_len == 0 )
2008 {
2009 return( 0 );
2010 }
2011
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002012#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02002013 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002014 return( 1 );
2015#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002016
2017 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002018 return( 1 );
2019
2020 return( 0 );
2021}
2022
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002023static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
2024{
2025 /* Remove reference to existing PSK, if any. */
2026#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002027 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002028 {
2029 /* The maintenance of the PSK key slot is the
2030 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002031 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002032 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002033#endif /* MBEDTLS_USE_PSA_CRYPTO */
2034 if( conf->psk != NULL )
2035 {
2036 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
2037
2038 mbedtls_free( conf->psk );
2039 conf->psk = NULL;
2040 conf->psk_len = 0;
2041 }
2042
2043 /* Remove reference to PSK identity, if any. */
2044 if( conf->psk_identity != NULL )
2045 {
2046 mbedtls_free( conf->psk_identity );
2047 conf->psk_identity = NULL;
2048 conf->psk_identity_len = 0;
2049 }
2050}
2051
Hanno Becker7390c712018-11-15 13:33:04 +00002052/* This function assumes that PSK identity in the SSL config is unset.
2053 * It checks that the provided identity is well-formed and attempts
2054 * to make a copy of it in the SSL config.
2055 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002056MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002057static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2058 unsigned char const *psk_identity,
2059 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002060{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002061 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002062 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002063 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002064 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002065 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002066 {
2067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2068 }
2069
Hanno Becker7390c712018-11-15 13:33:04 +00002070 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2071 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002072 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002073
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002074 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002075 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002076
2077 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002078}
2079
Hanno Becker7390c712018-11-15 13:33:04 +00002080int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2081 const unsigned char *psk, size_t psk_len,
2082 const unsigned char *psk_identity, size_t psk_identity_len )
2083{
Janos Follath865b3eb2019-12-16 11:46:15 +00002084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002085
2086 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002087 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002088 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002089
2090 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002091 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002093 if( psk_len == 0 )
2094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2095 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2097
Hanno Becker7390c712018-11-15 13:33:04 +00002098 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2099 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2100 conf->psk_len = psk_len;
2101 memcpy( conf->psk, psk, conf->psk_len );
2102
2103 /* Check and set PSK Identity */
2104 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2105 if( ret != 0 )
2106 ssl_conf_remove_psk( conf );
2107
2108 return( ret );
2109}
2110
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002111static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2112{
2113#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002114 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002115 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002116 /* The maintenance of the external PSK key slot is the
2117 * user's responsibility. */
2118 if( ssl->handshake->psk_opaque_is_internal )
2119 {
2120 psa_destroy_key( ssl->handshake->psk_opaque );
2121 ssl->handshake->psk_opaque_is_internal = 0;
2122 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002123 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002124 }
Neil Armstronge952a302022-05-03 10:22:14 +02002125#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002126 if( ssl->handshake->psk != NULL )
2127 {
2128 mbedtls_platform_zeroize( ssl->handshake->psk,
2129 ssl->handshake->psk_len );
2130 mbedtls_free( ssl->handshake->psk );
2131 ssl->handshake->psk_len = 0;
2132 }
Neil Armstronge952a302022-05-03 10:22:14 +02002133#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002134}
2135
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002136int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2137 const unsigned char *psk, size_t psk_len )
2138{
Neil Armstrong501c9322022-05-03 09:35:09 +02002139#if defined(MBEDTLS_USE_PSA_CRYPTO)
2140 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002142 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002144#endif /* MBEDTLS_USE_PSA_CRYPTO */
2145
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002146 if( psk == NULL || ssl->handshake == NULL )
2147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2148
2149 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2151
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002152 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002153
Neil Armstrong501c9322022-05-03 09:35:09 +02002154#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002155#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2156 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2157 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002158 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002159 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2160 else
2161 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2162 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2163 }
2164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002165
Jerry Yu568ec252022-07-22 21:27:34 +08002166#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002167 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2168 {
2169 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2170 psa_set_key_usage_flags( &key_attributes,
2171 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2172 }
2173#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2174
Neil Armstrong501c9322022-05-03 09:35:09 +02002175 psa_set_key_algorithm( &key_attributes, alg );
2176 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2177
2178 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2179 if( status != PSA_SUCCESS )
2180 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2181
2182 /* Allow calling psa_destroy_key() on psk remove */
2183 ssl->handshake->psk_opaque_is_internal = 1;
2184 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2185#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002186 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002187 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002188
2189 ssl->handshake->psk_len = psk_len;
2190 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2191
2192 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002193#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002194}
2195
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002196#if defined(MBEDTLS_USE_PSA_CRYPTO)
2197int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002198 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002199 const unsigned char *psk_identity,
2200 size_t psk_identity_len )
2201{
Janos Follath865b3eb2019-12-16 11:46:15 +00002202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002203
2204 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002205 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002206 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002207
Hanno Becker7390c712018-11-15 13:33:04 +00002208 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002209 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002211 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002212
2213 /* Check and set PSK Identity */
2214 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2215 psk_identity_len );
2216 if( ret != 0 )
2217 ssl_conf_remove_psk( conf );
2218
2219 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002220}
2221
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002222int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2223 mbedtls_svc_key_id_t psk )
2224{
2225 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2226 ( ssl->handshake == NULL ) )
2227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2228
2229 ssl_remove_psk( ssl );
2230 ssl->handshake->psk_opaque = psk;
2231 return( 0 );
2232}
2233#endif /* MBEDTLS_USE_PSA_CRYPTO */
2234
Jerry Yu8897c072022-08-12 13:56:53 +08002235#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002236void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2237 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2238 size_t),
2239 void *p_psk )
2240{
2241 conf->f_psk = f_psk;
2242 conf->p_psk = p_psk;
2243}
Jerry Yu8897c072022-08-12 13:56:53 +08002244#endif /* MBEDTLS_SSL_SRV_C */
2245
Ronald Cron73fe8df2022-10-05 14:31:43 +02002246#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002247
2248#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002249static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2250 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002251{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002252#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002253 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002254 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002255#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002256 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002257 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002258 return( MBEDTLS_SSL_MODE_STREAM );
2259}
2260
2261#else /* MBEDTLS_USE_PSA_CRYPTO */
2262
2263static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2264 mbedtls_cipher_mode_t mode )
2265{
2266#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2267 if( mode == MBEDTLS_MODE_CBC )
2268 return( MBEDTLS_SSL_MODE_CBC );
2269#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2270
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002271#if defined(MBEDTLS_GCM_C) || \
2272 defined(MBEDTLS_CCM_C) || \
2273 defined(MBEDTLS_CHACHAPOLY_C)
2274 if( mode == MBEDTLS_MODE_GCM ||
2275 mode == MBEDTLS_MODE_CCM ||
2276 mode == MBEDTLS_MODE_CHACHAPOLY )
2277 return( MBEDTLS_SSL_MODE_AEAD );
2278#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002279
2280 return( MBEDTLS_SSL_MODE_STREAM );
2281}
Gilles Peskine301711e2022-04-26 16:57:05 +02002282#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002283
Gilles Peskinee108d982022-04-26 16:50:40 +02002284static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2285 mbedtls_ssl_mode_t base_mode,
2286 int encrypt_then_mac )
2287{
2288#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2289 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2290 base_mode == MBEDTLS_SSL_MODE_CBC )
2291 {
2292 return( MBEDTLS_SSL_MODE_CBC_ETM );
2293 }
2294#else
2295 (void) encrypt_then_mac;
2296#endif
2297 return( base_mode );
2298}
2299
Neil Armstrongab555e02022-04-04 11:07:59 +02002300mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002301 const mbedtls_ssl_transform *transform )
2302{
Gilles Peskinee108d982022-04-26 16:50:40 +02002303 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002304#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002305 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002306#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002307 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2308#endif
2309 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002310
Gilles Peskinee108d982022-04-26 16:50:40 +02002311 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002312#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002313 encrypt_then_mac = transform->encrypt_then_mac;
2314#endif
2315 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002316}
2317
Neil Armstrongab555e02022-04-04 11:07:59 +02002318mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002319#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002320 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002321#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002322 const mbedtls_ssl_ciphersuite_t *suite )
2323{
Gilles Peskinee108d982022-04-26 16:50:40 +02002324 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2325
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002326#if defined(MBEDTLS_USE_PSA_CRYPTO)
2327 psa_status_t status;
2328 psa_algorithm_t alg;
2329 psa_key_type_t type;
2330 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002331 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2332 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002333 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002334#else
2335 const mbedtls_cipher_info_t *cipher =
2336 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002337 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002338 {
2339 base_mode =
2340 mbedtls_ssl_get_base_mode(
2341 mbedtls_cipher_info_get_mode( cipher ) );
2342 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002343#endif /* MBEDTLS_USE_PSA_CRYPTO */
2344
Gilles Peskinee108d982022-04-26 16:50:40 +02002345#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2346 int encrypt_then_mac = 0;
2347#endif
2348 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002349}
2350
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002351#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002352
2353#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002354/* Serialization of TLS 1.3 sessions:
2355 *
2356 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002357 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002358 * uint64 ticket_received;
2359 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002360 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002361 * } ClientOnlyData;
2362 *
2363 * struct {
2364 * uint8 endpoint;
2365 * uint8 ciphersuite[2];
2366 * uint32 ticket_age_add;
2367 * uint8 ticket_flags;
2368 * opaque resumption_key<0..255>;
2369 * select ( endpoint ) {
2370 * case client: ClientOnlyData;
2371 * case server: uint64 start_time;
2372 * };
2373 * } serialized_session_tls13;
2374 *
2375 */
2376#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002377MBEDTLS_CHECK_RETURN_CRITICAL
2378static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2379 unsigned char *buf,
2380 size_t buf_len,
2381 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002382{
2383 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002384#if defined(MBEDTLS_SSL_CLI_C) && \
2385 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2386 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002387 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002388#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002389 size_t needed = 1 /* endpoint */
2390 + 2 /* ciphersuite */
2391 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002392 + 1 /* ticket_flags */
2393 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002394 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002395
2396 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2398 needed += session->resumption_key_len; /* resumption_key */
2399
Jerry Yu438ddd82022-07-07 06:55:50 +00002400#if defined(MBEDTLS_HAVE_TIME)
2401 needed += 8; /* start_time or ticket_received */
2402#endif
2403
2404#if defined(MBEDTLS_SSL_CLI_C)
2405 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2406 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002407#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002408 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002409 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002410#endif
2411
Jerry Yu438ddd82022-07-07 06:55:50 +00002412 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002413 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002414
2415 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002416 if( session->ticket_len > SIZE_MAX - needed )
2417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002418
Jerry Yue28d9742022-08-18 15:44:03 +08002419 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002420 }
2421#endif /* MBEDTLS_SSL_CLI_C */
2422
Jerry Yue36fdd62022-08-17 21:31:36 +08002423 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002424 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002425 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002426
2427 p[0] = session->endpoint;
2428 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2429 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2430 p[7] = session->ticket_flags;
2431
2432 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002433 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002434 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002435 memcpy( p, session->resumption_key, session->resumption_key_len );
2436 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002437
2438#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2439 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2440 {
2441 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2442 p += 8;
2443 }
2444#endif /* MBEDTLS_HAVE_TIME */
2445
2446#if defined(MBEDTLS_SSL_CLI_C)
2447 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2448 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002449#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2450 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2451 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002452 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002453 {
2454 /* save host name */
2455 memcpy( p, session->hostname, hostname_len );
2456 p += hostname_len;
2457 }
2458#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2459
Jerry Yu438ddd82022-07-07 06:55:50 +00002460#if defined(MBEDTLS_HAVE_TIME)
2461 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2462 p += 8;
2463#endif
2464 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2465 p += 4;
2466
2467 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2468 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002469
2470 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002471 {
2472 memcpy( p, session->ticket, session->ticket_len );
2473 p += session->ticket_len;
2474 }
2475 }
2476#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002477 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002478}
2479
2480MBEDTLS_CHECK_RETURN_CRITICAL
2481static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2482 const unsigned char *buf,
2483 size_t len )
2484{
2485 const unsigned char *p = buf;
2486 const unsigned char *end = buf + len;
2487
2488 if( end - p < 9 )
2489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2490 session->endpoint = p[0];
2491 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2492 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2493 session->ticket_flags = p[7];
2494
2495 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002496 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002497 p += 9;
2498
Jerry Yubc7c1a42022-07-21 22:57:37 +08002499 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2501
Jerry Yubc7c1a42022-07-21 22:57:37 +08002502 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002504 memcpy( session->resumption_key, p, session->resumption_key_len );
2505 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002506
2507#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2508 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2509 {
2510 if( end - p < 8 )
2511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2512 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2513 p += 8;
2514 }
2515#endif /* MBEDTLS_HAVE_TIME */
2516
2517#if defined(MBEDTLS_SSL_CLI_C)
2518 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2519 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002520#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2521 defined(MBEDTLS_SSL_SESSION_TICKETS)
2522 size_t hostname_len;
2523 /* load host name */
2524 if( end - p < 2 )
2525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2526 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2527 p += 2;
2528
2529 if( end - p < ( long int )hostname_len )
2530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2531 if( hostname_len > 0 )
2532 {
2533 session->hostname = mbedtls_calloc( 1, hostname_len );
2534 if( session->hostname == NULL )
2535 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2536 memcpy( session->hostname, p, hostname_len );
2537 p += hostname_len;
2538 }
2539#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2540 MBEDTLS_SSL_SESSION_TICKETS */
2541
Jerry Yu438ddd82022-07-07 06:55:50 +00002542#if defined(MBEDTLS_HAVE_TIME)
2543 if( end - p < 8 )
2544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2545 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2546 p += 8;
2547#endif
2548 if( end - p < 4 )
2549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2550 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2551 p += 4;
2552
2553 if( end - p < 2 )
2554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2555 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2556 p += 2;
2557
2558 if( end - p < ( long int )session->ticket_len )
2559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2560 if( session->ticket_len > 0 )
2561 {
2562 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2563 if( session->ticket == NULL )
2564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2565 memcpy( session->ticket, p, session->ticket_len );
2566 p += session->ticket_len;
2567 }
2568 }
2569#endif /* MBEDTLS_SSL_CLI_C */
2570
2571 return( 0 );
2572
2573}
2574#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002575MBEDTLS_CHECK_RETURN_CRITICAL
2576static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2577 unsigned char *buf,
2578 size_t buf_len,
2579 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002580{
2581 ((void) session);
2582 ((void) buf);
2583 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002584 *olen = 0;
2585 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002586}
Jerry Yu438ddd82022-07-07 06:55:50 +00002587
2588static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2589 unsigned char *buf,
2590 size_t buf_len )
2591{
2592 ((void) session);
2593 ((void) buf);
2594 ((void) buf_len);
2595 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2596}
2597#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002598#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2599
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002600psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002601 size_t taglen,
2602 psa_algorithm_t *alg,
2603 psa_key_type_t *key_type,
2604 size_t *key_size )
2605{
2606 switch ( mbedtls_cipher_type )
2607 {
2608 case MBEDTLS_CIPHER_AES_128_CBC:
2609 *alg = PSA_ALG_CBC_NO_PADDING;
2610 *key_type = PSA_KEY_TYPE_AES;
2611 *key_size = 128;
2612 break;
2613 case MBEDTLS_CIPHER_AES_128_CCM:
2614 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2615 *key_type = PSA_KEY_TYPE_AES;
2616 *key_size = 128;
2617 break;
2618 case MBEDTLS_CIPHER_AES_128_GCM:
2619 *alg = PSA_ALG_GCM;
2620 *key_type = PSA_KEY_TYPE_AES;
2621 *key_size = 128;
2622 break;
2623 case MBEDTLS_CIPHER_AES_192_CCM:
2624 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2625 *key_type = PSA_KEY_TYPE_AES;
2626 *key_size = 192;
2627 break;
2628 case MBEDTLS_CIPHER_AES_192_GCM:
2629 *alg = PSA_ALG_GCM;
2630 *key_type = PSA_KEY_TYPE_AES;
2631 *key_size = 192;
2632 break;
2633 case MBEDTLS_CIPHER_AES_256_CBC:
2634 *alg = PSA_ALG_CBC_NO_PADDING;
2635 *key_type = PSA_KEY_TYPE_AES;
2636 *key_size = 256;
2637 break;
2638 case MBEDTLS_CIPHER_AES_256_CCM:
2639 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2640 *key_type = PSA_KEY_TYPE_AES;
2641 *key_size = 256;
2642 break;
2643 case MBEDTLS_CIPHER_AES_256_GCM:
2644 *alg = PSA_ALG_GCM;
2645 *key_type = PSA_KEY_TYPE_AES;
2646 *key_size = 256;
2647 break;
2648 case MBEDTLS_CIPHER_ARIA_128_CBC:
2649 *alg = PSA_ALG_CBC_NO_PADDING;
2650 *key_type = PSA_KEY_TYPE_ARIA;
2651 *key_size = 128;
2652 break;
2653 case MBEDTLS_CIPHER_ARIA_128_CCM:
2654 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2655 *key_type = PSA_KEY_TYPE_ARIA;
2656 *key_size = 128;
2657 break;
2658 case MBEDTLS_CIPHER_ARIA_128_GCM:
2659 *alg = PSA_ALG_GCM;
2660 *key_type = PSA_KEY_TYPE_ARIA;
2661 *key_size = 128;
2662 break;
2663 case MBEDTLS_CIPHER_ARIA_192_CCM:
2664 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2665 *key_type = PSA_KEY_TYPE_ARIA;
2666 *key_size = 192;
2667 break;
2668 case MBEDTLS_CIPHER_ARIA_192_GCM:
2669 *alg = PSA_ALG_GCM;
2670 *key_type = PSA_KEY_TYPE_ARIA;
2671 *key_size = 192;
2672 break;
2673 case MBEDTLS_CIPHER_ARIA_256_CBC:
2674 *alg = PSA_ALG_CBC_NO_PADDING;
2675 *key_type = PSA_KEY_TYPE_ARIA;
2676 *key_size = 256;
2677 break;
2678 case MBEDTLS_CIPHER_ARIA_256_CCM:
2679 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2680 *key_type = PSA_KEY_TYPE_ARIA;
2681 *key_size = 256;
2682 break;
2683 case MBEDTLS_CIPHER_ARIA_256_GCM:
2684 *alg = PSA_ALG_GCM;
2685 *key_type = PSA_KEY_TYPE_ARIA;
2686 *key_size = 256;
2687 break;
2688 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2689 *alg = PSA_ALG_CBC_NO_PADDING;
2690 *key_type = PSA_KEY_TYPE_CAMELLIA;
2691 *key_size = 128;
2692 break;
2693 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2694 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2695 *key_type = PSA_KEY_TYPE_CAMELLIA;
2696 *key_size = 128;
2697 break;
2698 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2699 *alg = PSA_ALG_GCM;
2700 *key_type = PSA_KEY_TYPE_CAMELLIA;
2701 *key_size = 128;
2702 break;
2703 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2704 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2705 *key_type = PSA_KEY_TYPE_CAMELLIA;
2706 *key_size = 192;
2707 break;
2708 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2709 *alg = PSA_ALG_GCM;
2710 *key_type = PSA_KEY_TYPE_CAMELLIA;
2711 *key_size = 192;
2712 break;
2713 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2714 *alg = PSA_ALG_CBC_NO_PADDING;
2715 *key_type = PSA_KEY_TYPE_CAMELLIA;
2716 *key_size = 256;
2717 break;
2718 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2719 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2720 *key_type = PSA_KEY_TYPE_CAMELLIA;
2721 *key_size = 256;
2722 break;
2723 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2724 *alg = PSA_ALG_GCM;
2725 *key_type = PSA_KEY_TYPE_CAMELLIA;
2726 *key_size = 256;
2727 break;
2728 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2729 *alg = PSA_ALG_CHACHA20_POLY1305;
2730 *key_type = PSA_KEY_TYPE_CHACHA20;
2731 *key_size = 256;
2732 break;
2733 case MBEDTLS_CIPHER_NULL:
2734 *alg = MBEDTLS_SSL_NULL_CIPHER;
2735 *key_type = 0;
2736 *key_size = 0;
2737 break;
2738 default:
2739 return PSA_ERROR_NOT_SUPPORTED;
2740 }
2741
2742 return PSA_SUCCESS;
2743}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002744#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002745
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002746#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002747int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2748 const unsigned char *dhm_P, size_t P_len,
2749 const unsigned char *dhm_G, size_t G_len )
2750{
Janos Follath865b3eb2019-12-16 11:46:15 +00002751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002752
Glenn Strausscee11292021-12-20 01:43:17 -05002753 mbedtls_mpi_free( &conf->dhm_P );
2754 mbedtls_mpi_free( &conf->dhm_G );
2755
Hanno Beckera90658f2017-10-04 15:29:08 +01002756 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2757 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2758 {
2759 mbedtls_mpi_free( &conf->dhm_P );
2760 mbedtls_mpi_free( &conf->dhm_G );
2761 return( ret );
2762 }
2763
2764 return( 0 );
2765}
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002767int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002768{
Janos Follath865b3eb2019-12-16 11:46:15 +00002769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002770
Glenn Strausscee11292021-12-20 01:43:17 -05002771 mbedtls_mpi_free( &conf->dhm_P );
2772 mbedtls_mpi_free( &conf->dhm_G );
2773
Gilles Peskinee5702482021-06-11 21:59:08 +02002774 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2775 &conf->dhm_P ) ) != 0 ||
2776 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2777 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002778 {
2779 mbedtls_mpi_free( &conf->dhm_P );
2780 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002781 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002782 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002783
2784 return( 0 );
2785}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002786#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002787
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002788#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2789/*
2790 * Set the minimum length for Diffie-Hellman parameters
2791 */
2792void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2793 unsigned int bitlen )
2794{
2795 conf->dhm_min_bitlen = bitlen;
2796}
2797#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2798
Ronald Crone68ab4f2022-10-05 12:46:29 +02002799#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002800#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002801/*
2802 * Set allowed/preferred hashes for handshake signatures
2803 */
2804void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2805 const int *hashes )
2806{
2807 conf->sig_hashes = hashes;
2808}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002809#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002810
Jerry Yuf017ee42022-01-12 15:49:48 +08002811/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002812void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2813 const uint16_t* sig_algs )
2814{
Jerry Yuf017ee42022-01-12 15:49:48 +08002815#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2816 conf->sig_hashes = NULL;
2817#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2818 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002819}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002820#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002821
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002822#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002823#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002824/*
2825 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002826 *
2827 * mbedtls_ssl_setup() takes the provided list
2828 * and translates it to a list of IANA TLS group identifiers,
2829 * stored in ssl->handshake->group_list.
2830 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002831 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002832void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002833 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002834{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002835 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002836 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002837}
Brett Warrene0edc842021-08-17 09:53:13 +01002838#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002839#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002840
Brett Warrene0edc842021-08-17 09:53:13 +01002841/*
2842 * Set the allowed groups
2843 */
2844void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2845 const uint16_t *group_list )
2846{
2847#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2848 conf->curve_list = NULL;
2849#endif
2850 conf->group_list = group_list;
2851}
2852
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002853#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002855{
Hanno Becker947194e2017-04-07 13:25:49 +01002856 /* Initialize to suppress unnecessary compiler warning */
2857 size_t hostname_len = 0;
2858
2859 /* Check if new hostname is valid before
2860 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002861 if( hostname != NULL )
2862 {
2863 hostname_len = strlen( hostname );
2864
2865 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2867 }
2868
2869 /* Now it's clear that we will overwrite the old hostname,
2870 * so we can free it safely */
2871
2872 if( ssl->hostname != NULL )
2873 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002874 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002875 mbedtls_free( ssl->hostname );
2876 }
2877
2878 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002879
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002881 {
2882 ssl->hostname = NULL;
2883 }
2884 else
2885 {
2886 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002887 if( ssl->hostname == NULL )
2888 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002889
Hanno Becker947194e2017-04-07 13:25:49 +01002890 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002891
Hanno Becker947194e2017-04-07 13:25:49 +01002892 ssl->hostname[hostname_len] = '\0';
2893 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
2895 return( 0 );
2896}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002897#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002899#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002900void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002902 const unsigned char *, size_t),
2903 void *p_sni )
2904{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002905 conf->f_sni = f_sni;
2906 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002911int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002912{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002913 size_t cur_len, tot_len;
2914 const char **p;
2915
2916 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002917 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2918 * MUST NOT be truncated."
2919 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002920 */
2921 tot_len = 0;
2922 for( p = protos; *p != NULL; p++ )
2923 {
2924 cur_len = strlen( *p );
2925 tot_len += cur_len;
2926
Ronald Cron8216dd32020-04-23 16:41:44 +02002927 if( ( cur_len == 0 ) ||
2928 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2929 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002931 }
2932
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002933 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002934
2935 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002936}
2937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002939{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002940 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002941}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002943
Johan Pascalb62bb512015-12-03 21:56:45 +01002944#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002945void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2946 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002947{
2948 conf->dtls_srtp_mki_support = support_mki_value;
2949}
2950
Ron Eldoref72faf2018-07-12 11:54:20 +03002951int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2952 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002953 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002954{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002955 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002956 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002958 }
Ron Eldor591f1622018-01-22 12:30:04 +02002959
2960 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002961 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002962 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002963 }
Ron Eldor591f1622018-01-22 12:30:04 +02002964
2965 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2966 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002967 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002968}
2969
Ron Eldoref72faf2018-07-12 11:54:20 +03002970int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002971 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002972{
Johan Pascal253d0262020-09-22 13:04:45 +02002973 const mbedtls_ssl_srtp_profile *p;
2974 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002975
Johan Pascal253d0262020-09-22 13:04:45 +02002976 /* check the profiles list: all entry must be valid,
2977 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002978 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2979 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2980 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002981 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002982 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002983 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002984 list_size++;
2985 }
2986 else
2987 {
2988 /* unsupported value, stop parsing and set the size to an error value */
2989 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002990 }
2991 }
2992
Johan Pascal5ef72d22020-10-28 17:05:47 +01002993 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002994 {
Johan Pascal253d0262020-09-22 13:04:45 +02002995 conf->dtls_srtp_profile_list = NULL;
2996 conf->dtls_srtp_profile_list_len = 0;
2997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2998 }
2999
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003000 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003001 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003002
3003 return( 0 );
3004}
3005
Johan Pascal5ef72d22020-10-28 17:05:47 +01003006void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
3007 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01003008{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003009 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3010 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01003011 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003012 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003013 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003014 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01003015 else
3016 {
3017 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01003018 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3019 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01003020 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003021}
Johan Pascalb62bb512015-12-03 21:56:45 +01003022#endif /* MBEDTLS_SSL_DTLS_SRTP */
3023
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303024#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003025void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00003026{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003027 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003028}
3029
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003030void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00003031{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003032 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003033}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303034#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003035
Janos Follath088ce432017-04-10 12:42:31 +01003036#if defined(MBEDTLS_SSL_SRV_C)
3037void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
3038 char cert_req_ca_list )
3039{
3040 conf->cert_req_ca_list = cert_req_ca_list;
3041}
3042#endif
3043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003045void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003046{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003047 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003048}
3049#endif
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003052void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003054 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003055}
3056#endif
3057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003059int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003062 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003065 }
3066
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003067 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003068
3069 return( 0 );
3070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003072
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003073void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003075 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003076}
3077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003079void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003080{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003081 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003082}
3083
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003084void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003085{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003086 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003087}
3088
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003089void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003090 const unsigned char period[8] )
3091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003092 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003097#if defined(MBEDTLS_SSL_CLI_C)
3098void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003099{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003100 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003101}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003102#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003103
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003104#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003105
Jerry Yud0766ec2022-09-22 10:46:57 +08003106#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003107void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3108 uint16_t num_tickets )
3109{
Jerry Yud0766ec2022-09-22 10:46:57 +08003110 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003111}
3112#endif
3113
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003114void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3115 mbedtls_ssl_ticket_write_t *f_ticket_write,
3116 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3117 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003118{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003119 conf->f_ticket_write = f_ticket_write;
3120 conf->f_ticket_parse = f_ticket_parse;
3121 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003122}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003125
Hanno Becker7e6c1782021-06-08 09:24:55 +01003126void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3127 mbedtls_ssl_export_keys_t *f_export_keys,
3128 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003129{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003130 ssl->f_export_keys = f_export_keys;
3131 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003132}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003133
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003134#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003135void mbedtls_ssl_conf_async_private_cb(
3136 mbedtls_ssl_config *conf,
3137 mbedtls_ssl_async_sign_t *f_async_sign,
3138 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3139 mbedtls_ssl_async_resume_t *f_async_resume,
3140 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003141 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003142{
3143 conf->f_async_sign_start = f_async_sign;
3144 conf->f_async_decrypt_start = f_async_decrypt;
3145 conf->f_async_resume = f_async_resume;
3146 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003147 conf->p_async_config_data = async_config_data;
3148}
3149
Gilles Peskine8f97af72018-04-26 11:46:10 +02003150void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3151{
3152 return( conf->p_async_config_data );
3153}
3154
Gilles Peskine1febfef2018-04-30 11:54:39 +02003155void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003156{
3157 if( ssl->handshake == NULL )
3158 return( NULL );
3159 else
3160 return( ssl->handshake->user_async_ctx );
3161}
3162
Gilles Peskine1febfef2018-04-30 11:54:39 +02003163void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003164 void *ctx )
3165{
3166 if( ssl->handshake != NULL )
3167 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003168}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003169#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003170
Paul Bakker5121ce52009-01-03 21:22:43 +00003171/*
3172 * SSL get accessors
3173 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003174uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003175{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003176 if( ssl->session != NULL )
3177 return( ssl->session->verify_result );
3178
3179 if( ssl->session_negotiate != NULL )
3180 return( ssl->session_negotiate->verify_result );
3181
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003182 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003183}
3184
Glenn Strauss8f526902022-01-13 00:04:49 -05003185int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3186{
3187 if( ssl == NULL || ssl->session == NULL )
3188 return( 0 );
3189
3190 return( ssl->session->ciphersuite );
3191}
3192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003194{
Paul Bakker926c8e42013-03-06 10:23:34 +01003195 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003196 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003199}
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003204 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003205 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003206 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003207 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003208 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003209 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003210 default:
3211 return( "unknown (DTLS)" );
3212 }
3213 }
3214#endif
3215
Glenn Strauss60bfe602022-03-14 19:04:24 -04003216 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003217 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003218 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003219 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003220 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003221 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003222 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003223 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003224 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003225}
3226
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003227#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003228size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3229{
David Horstmann95d516f2021-05-04 18:36:56 +01003230 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003231 size_t read_mfl;
3232
3233 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3234 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3235 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3236 {
3237 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3238 }
3239
3240 /* Check if a smaller max length was negotiated */
3241 if( ssl->session_out != NULL )
3242 {
3243 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3244 if( read_mfl < max_len )
3245 {
3246 max_len = read_mfl;
3247 }
3248 }
3249
3250 // During a handshake, use the value being negotiated
3251 if( ssl->session_negotiate != NULL )
3252 {
3253 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3254 if( read_mfl < max_len )
3255 {
3256 max_len = read_mfl;
3257 }
3258 }
3259
3260 return( max_len );
3261}
3262
3263size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003264{
3265 size_t max_len;
3266
3267 /*
3268 * Assume mfl_code is correct since it was checked when set
3269 */
Angus Grattond8213d02016-05-25 20:56:48 +10003270 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003271
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003272 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003273 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003274 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003275 {
Angus Grattond8213d02016-05-25 20:56:48 +10003276 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003277 }
3278
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003279 /* During a handshake, use the value being negotiated */
3280 if( ssl->session_negotiate != NULL &&
3281 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3282 {
3283 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3284 }
3285
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003286 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003287}
3288#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3289
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003290#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003291size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003292{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003293 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3294 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3295 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3296 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3297 return ( 0 );
3298
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003299 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3300 return( ssl->mtu );
3301
3302 if( ssl->mtu == 0 )
3303 return( ssl->handshake->mtu );
3304
3305 return( ssl->mtu < ssl->handshake->mtu ?
3306 ssl->mtu : ssl->handshake->mtu );
3307}
3308#endif /* MBEDTLS_SSL_PROTO_DTLS */
3309
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003310int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3311{
3312 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3313
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003314#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3315 !defined(MBEDTLS_SSL_PROTO_DTLS)
3316 (void) ssl;
3317#endif
3318
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003320 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003321
3322 if( max_len > mfl )
3323 max_len = mfl;
3324#endif
3325
3326#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003327 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003328 {
Hanno Becker89490712020-02-05 10:50:12 +00003329 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003330 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3331 const size_t overhead = (size_t) ret;
3332
3333 if( ret < 0 )
3334 return( ret );
3335
3336 if( mtu <= overhead )
3337 {
3338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3339 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3340 }
3341
3342 if( max_len > mtu - overhead )
3343 max_len = mtu - overhead;
3344 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003345#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003346
Hanno Becker0defedb2018-08-10 12:35:02 +01003347#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3348 !defined(MBEDTLS_SSL_PROTO_DTLS)
3349 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003350#endif
3351
3352 return( (int) max_len );
3353}
3354
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003355int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3356{
3357 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3358
3359#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3360 (void) ssl;
3361#endif
3362
3363#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3364 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3365
3366 if( max_len > mfl )
3367 max_len = mfl;
3368#endif
3369
3370 return( (int) max_len );
3371}
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373#if defined(MBEDTLS_X509_CRT_PARSE_C)
3374const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003375{
3376 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003377 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003378
Hanno Beckere6824572019-02-07 13:18:46 +00003379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003380 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003381#else
3382 return( NULL );
3383#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003388int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3389 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003390{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003391 int ret;
3392
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003393 if( ssl == NULL ||
3394 dst == NULL ||
3395 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003396 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003399 }
3400
Hanno Beckere810bbc2021-05-14 16:01:05 +01003401 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3402 * idempotent: Each session can only be exported once.
3403 *
3404 * (This is in preparation for TLS 1.3 support where we will
3405 * need the ability to export multiple sessions (aka tickets),
3406 * which will be achieved by calling mbedtls_ssl_get_session()
3407 * multiple times until it fails.)
3408 *
3409 * Check whether we have already exported the current session,
3410 * and fail if so.
3411 */
3412 if( ssl->session->exported == 1 )
3413 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3414
3415 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3416 if( ret != 0 )
3417 return( ret );
3418
3419 /* Remember that we've exported the session. */
3420 ssl->session->exported = 1;
3421 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003424
Paul Bakker5121ce52009-01-03 21:22:43 +00003425/*
Hanno Beckera835da52019-05-16 12:39:07 +01003426 * Define ticket header determining Mbed TLS version
3427 * and structure of the ticket.
3428 */
3429
Hanno Becker94ef3b32019-05-16 12:50:45 +01003430/*
Hanno Becker50b59662019-05-28 14:30:45 +01003431 * Define bitflag determining compile-time settings influencing
3432 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003433 */
3434
Hanno Becker50b59662019-05-28 14:30:45 +01003435#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003436#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003437#else
Hanno Becker3e088662019-05-29 11:10:18 +01003438#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003439#endif /* MBEDTLS_HAVE_TIME */
3440
3441#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003442#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003443#else
Hanno Becker3e088662019-05-29 11:10:18 +01003444#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003445#endif /* MBEDTLS_X509_CRT_PARSE_C */
3446
3447#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003448#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003449#else
Hanno Becker3e088662019-05-29 11:10:18 +01003450#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003451#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3452
3453#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003454#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003455#else
Hanno Becker3e088662019-05-29 11:10:18 +01003456#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003457#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3458
Hanno Becker94ef3b32019-05-16 12:50:45 +01003459#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003460#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003461#else
Hanno Becker3e088662019-05-29 11:10:18 +01003462#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003463#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3464
Hanno Becker94ef3b32019-05-16 12:50:45 +01003465#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3466#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3467#else
3468#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3469#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3470
Hanno Becker3e088662019-05-29 11:10:18 +01003471#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3472#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3473#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3474#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003475#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3476#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003477
Hanno Becker50b59662019-05-28 14:30:45 +01003478#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003479 ( (uint16_t) ( \
3480 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3481 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3482 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3483 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003484 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003485 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003486
Hanno Beckerf8787072019-05-16 12:41:07 +01003487static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003488 MBEDTLS_VERSION_MAJOR,
3489 MBEDTLS_VERSION_MINOR,
3490 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003491 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3492 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003493};
Hanno Beckera835da52019-05-16 12:39:07 +01003494
3495/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003496 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003497 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003498 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003499 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003500 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003501 * opaque mbedtls_version[3]; // library version: major, minor, patch
3502 * opaque session_format[2]; // library-version specific 16-bit field
3503 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003504 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003505 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003506 * Note: When updating the format, remember to keep
3507 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003508 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003509 * // In this version, `session_format` determines
3510 * // the setting of those compile-time
3511 * // configuration options which influence
3512 * // the structure of mbedtls_ssl_session.
3513 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003514 * uint8_t minor_ver; // Protocol minor version. Possible values:
3515 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003516 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003517 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003518 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003519 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003520 * serialized_session_tls12 data;
3521 *
3522 * };
3523 *
3524 * } serialized_session;
3525 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003526 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003527
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003528MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003529static int ssl_session_save( const mbedtls_ssl_session *session,
3530 unsigned char omit_header,
3531 unsigned char *buf,
3532 size_t buf_len,
3533 size_t *olen )
3534{
3535 unsigned char *p = buf;
3536 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003537 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003538#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3539 size_t out_len;
3540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3541#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003542 if( session == NULL )
3543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3544
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003545 if( !omit_header )
3546 {
3547 /*
3548 * Add Mbed TLS version identifier
3549 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003550 used += sizeof( ssl_serialized_session_header );
3551
3552 if( used <= buf_len )
3553 {
3554 memcpy( p, ssl_serialized_session_header,
3555 sizeof( ssl_serialized_session_header ) );
3556 p += sizeof( ssl_serialized_session_header );
3557 }
3558 }
3559
3560 /*
3561 * TLS version identifier
3562 */
3563 used += 1;
3564 if( used <= buf_len )
3565 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003566 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003567 }
3568
3569 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003570 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003571 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003572 {
3573#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003574 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003575 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003576 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003577#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3578
Jerry Yu251a12e2022-07-13 15:15:48 +08003579#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3580 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003581 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3582 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003583 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003584 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003585 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003586#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3587
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003588 default:
3589 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3590 }
3591
3592 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003593 if( used > buf_len )
3594 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003595
3596 return( 0 );
3597}
3598
3599/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003600 * Public wrapper for ssl_session_save()
3601 */
3602int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3603 unsigned char *buf,
3604 size_t buf_len,
3605 size_t *olen )
3606{
3607 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3608}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003609
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003610/*
3611 * Deserialize session, see mbedtls_ssl_session_save() for format.
3612 *
3613 * This internal version is wrapped by a public function that cleans up in
3614 * case of error, and has an extra option omit_header.
3615 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003616MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003617static int ssl_session_load( mbedtls_ssl_session *session,
3618 unsigned char omit_header,
3619 const unsigned char *buf,
3620 size_t len )
3621{
3622 const unsigned char *p = buf;
3623 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003624 size_t remaining_len;
3625
3626
3627 if( session == NULL )
3628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629
3630 if( !omit_header )
3631 {
3632 /*
3633 * Check Mbed TLS version identifier
3634 */
3635
3636 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3638
3639 if( memcmp( p, ssl_serialized_session_header,
3640 sizeof( ssl_serialized_session_header ) ) != 0 )
3641 {
3642 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3643 }
3644 p += sizeof( ssl_serialized_session_header );
3645 }
3646
3647 /*
3648 * TLS version identifier
3649 */
3650 if( 1 > (size_t)( end - p ) )
3651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003652 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003653
3654 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003655 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003656 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003657 {
3658#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003659 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003660 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3662
Jerry Yu438ddd82022-07-07 06:55:50 +00003663#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3664 case MBEDTLS_SSL_VERSION_TLS1_3:
3665 return( ssl_tls13_session_load( session, p, remaining_len ) );
3666#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3667
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003668 default:
3669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3670 }
3671}
3672
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003673/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003674 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003675 */
3676int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3677 const unsigned char *buf,
3678 size_t len )
3679{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003680 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003681
3682 if( ret != 0 )
3683 mbedtls_ssl_session_free( session );
3684
3685 return( ret );
3686}
3687
3688/*
Paul Bakker1961b702013-01-25 14:49:24 +01003689 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003690 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003691MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003692static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3693{
3694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3695
Ronald Cron66dbf912022-02-02 15:33:46 +01003696 /*
3697 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003698 * that were written into the output buffer by the previous handshake step,
3699 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003700 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3701 * We proceed to the next handshake step only when all data from the
3702 * previous one have been sent to the peer, thus we make sure that this is
3703 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3704 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3705 * we have to wait before to go ahead.
3706 * In the case of TLS 1.3, handshake step handlers do not send data to the
3707 * peer. Data are only sent here and through
3708 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003709 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003710 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003711 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3712 return( ret );
3713
3714#if defined(MBEDTLS_SSL_PROTO_DTLS)
3715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3716 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3717 {
3718 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3719 return( ret );
3720 }
3721#endif /* MBEDTLS_SSL_PROTO_DTLS */
3722
3723 return( ret );
3724}
3725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003727{
Hanno Becker41934dd2021-08-07 19:13:43 +01003728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003729
Hanno Becker41934dd2021-08-07 19:13:43 +01003730 if( ssl == NULL ||
3731 ssl->conf == NULL ||
3732 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003733 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003734 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003736 }
3737
3738 ret = ssl_prepare_handshake_step( ssl );
3739 if( ret != 0 )
3740 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003741
Jerry Yue7047812021-09-13 19:26:39 +08003742 ret = mbedtls_ssl_handle_pending_alert( ssl );
3743 if( ret != 0 )
3744 goto cleanup;
3745
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003746 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3747 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3748 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003751 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003752 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3754 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003755
Ronald Cron9f0fba32022-02-10 16:45:15 +01003756 switch( ssl->state )
3757 {
3758 case MBEDTLS_SSL_HELLO_REQUEST:
3759 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003760 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003761 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003762
Ronald Cron9f0fba32022-02-10 16:45:15 +01003763 case MBEDTLS_SSL_CLIENT_HELLO:
3764 ret = mbedtls_ssl_write_client_hello( ssl );
3765 break;
3766
3767 default:
3768#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003769 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003770 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3771 else
3772 ret = mbedtls_ssl_handshake_client_step( ssl );
3773#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3774 ret = mbedtls_ssl_handshake_client_step( ssl );
3775#else
3776 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3777#endif
3778 }
Jerry Yub9930e72021-08-06 17:11:51 +08003779 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003782 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003783 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003784#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003785 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003786 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003787#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003788
3789#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3790 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3791 ret = mbedtls_ssl_handshake_server_step( ssl );
3792#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3793 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003794#endif
3795
Jerry Yue7047812021-09-13 19:26:39 +08003796 if( ret != 0 )
3797 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003798 /* handshake_step return error. And it is same
3799 * with alert_reason.
3800 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003801 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003802 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003803 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003804 goto cleanup;
3805 }
3806 }
3807
3808cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003809 return( ret );
3810}
3811
3812/*
3813 * Perform the SSL handshake
3814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003816{
3817 int ret = 0;
3818
Hanno Beckera817ea42020-10-20 15:20:23 +01003819 /* Sanity checks */
3820
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003821 if( ssl == NULL || ssl->conf == NULL )
3822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3823
Hanno Beckera817ea42020-10-20 15:20:23 +01003824#if defined(MBEDTLS_SSL_PROTO_DTLS)
3825 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3826 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3827 {
3828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3829 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3830 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3831 }
3832#endif /* MBEDTLS_SSL_PROTO_DTLS */
3833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003835
Hanno Beckera817ea42020-10-20 15:20:23 +01003836 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003837 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003840
3841 if( ret != 0 )
3842 break;
3843 }
3844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
3847 return( ret );
3848}
3849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#if defined(MBEDTLS_SSL_RENEGOTIATION)
3851#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003852/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003853 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003854 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003855MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003857{
Janos Follath865b3eb2019-12-16 11:46:15 +00003858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003861
3862 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3864 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003865
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003866 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003867 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003869 return( ret );
3870 }
3871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003873
3874 return( 0 );
3875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003877
3878/*
3879 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 * - any side: calling mbedtls_ssl_renegotiate(),
3881 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3882 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003883 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003884 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003886 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003887int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003888{
Janos Follath865b3eb2019-12-16 11:46:15 +00003889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003892
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003893 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3894 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003895
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003896 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3897 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003901 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003902 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003903 ssl->handshake->out_msg_seq = 1;
3904 else
3905 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003906 }
3907#endif
3908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3910 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003915 return( ret );
3916 }
3917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003919
3920 return( 0 );
3921}
3922
3923/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003924 * Renegotiate current connection on client,
3925 * or request renegotiation on server
3926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003928{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003930
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003931 if( ssl == NULL || ssl->conf == NULL )
3932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003935 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003936 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003937 {
Jerry Yu6848a612022-10-27 13:03:26 +08003938 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003942
3943 /* Did we already try/start sending HelloRequest? */
3944 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003946
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003947 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003952 /*
3953 * On client, either start the renegotiation process or,
3954 * if already in progress, continue the handshake
3955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003957 {
Jerry Yu6848a612022-10-27 13:03:26 +08003958 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003960
Hanno Becker40cdaa12020-02-05 10:48:27 +00003961 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003962 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003964 return( ret );
3965 }
3966 }
3967 else
3968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003972 return( ret );
3973 }
3974 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003976
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003977 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003980
Gilles Peskine9b562d52018-04-25 20:32:43 +02003981void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003982{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003983 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3984
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003985 if( handshake == NULL )
3986 return;
3987
Brett Warrene0edc842021-08-17 09:53:13 +01003988#if defined(MBEDTLS_ECP_C)
3989#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3990 if ( ssl->handshake->group_list_heap_allocated )
3991 mbedtls_free( (void*) handshake->group_list );
3992 handshake->group_list = NULL;
3993#endif /* MBEDTLS_DEPRECATED_REMOVED */
3994#endif /* MBEDTLS_ECP_C */
3995
Ronald Crone68ab4f2022-10-05 12:46:29 +02003996#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003997#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3998 if ( ssl->handshake->sig_algs_heap_allocated )
3999 mbedtls_free( (void*) handshake->sig_algs );
4000 handshake->sig_algs = NULL;
4001#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004002#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4003 if( ssl->handshake->certificate_request_context )
4004 {
4005 mbedtls_free( (void*) handshake->certificate_request_context );
4006 }
4007#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004008#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004009
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004010#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4011 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
4012 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02004013 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004014 handshake->async_in_progress = 0;
4015 }
4016#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4017
Andrzej Kurek25f27152022-08-17 16:09:31 -04004018#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004019#if defined(MBEDTLS_USE_PSA_CRYPTO)
4020 psa_hash_abort( &handshake->fin_sha256_psa );
4021#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004022 mbedtls_sha256_free( &handshake->fin_sha256 );
4023#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004024#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004025#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004026#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05004027 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05004028#else
Andrzej Kureka242e832022-08-11 10:03:14 -04004029 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004030#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004031#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033#if defined(MBEDTLS_DHM_C)
4034 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00004035#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004036#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02004038#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004039
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004040#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004041#if defined(MBEDTLS_USE_PSA_CRYPTO)
4042 psa_pake_abort( &handshake->psa_pake_ctx );
4043 psa_destroy_key( handshake->psa_pake_password );
4044 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4045#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004046 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004047#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004048#if defined(MBEDTLS_SSL_CLI_C)
4049 mbedtls_free( handshake->ecjpake_cache );
4050 handshake->ecjpake_cache = NULL;
4051 handshake->ecjpake_cache_len = 0;
4052#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004053#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004054
Janos Follath4ae5c292016-02-10 11:27:43 +00004055#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4056 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004057 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004059#endif
4060
Ronald Cron73fe8df2022-10-05 14:31:43 +02004061#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004062#if defined(MBEDTLS_USE_PSA_CRYPTO)
4063 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4064 {
4065 /* The maintenance of the external PSK key slot is the
4066 * user's responsibility. */
4067 if( ssl->handshake->psk_opaque_is_internal )
4068 {
4069 psa_destroy_key( ssl->handshake->psk_opaque );
4070 ssl->handshake->psk_opaque_is_internal = 0;
4071 }
4072 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4073 }
Neil Armstronge952a302022-05-03 10:22:14 +02004074#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004075 if( handshake->psk != NULL )
4076 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004077 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004078 mbedtls_free( handshake->psk );
4079 }
Neil Armstronge952a302022-05-03 10:22:14 +02004080#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004081#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4084 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004085 /*
4086 * Free only the linked list wrapper, not the keys themselves
4087 * since the belong to the SNI callback
4088 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004089 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004091
Gilles Peskineeccd8882020-03-10 12:19:08 +01004092#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004093 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004094 if( handshake->ecrs_peer_cert != NULL )
4095 {
4096 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4097 mbedtls_free( handshake->ecrs_peer_cert );
4098 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004099#endif
4100
Hanno Becker75173122019-02-06 16:18:31 +00004101#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4102 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4103 mbedtls_pk_free( &handshake->peer_pubkey );
4104#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4105
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004106#if defined(MBEDTLS_SSL_CLI_C) && \
4107 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4108 mbedtls_free( handshake->cookie );
4109#endif /* MBEDTLS_SSL_CLI_C &&
4110 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004111
4112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004113 mbedtls_ssl_flight_free( handshake->flight );
4114 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004115#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004116
Ronald Cronf12b81d2022-03-15 10:42:41 +01004117#if defined(MBEDTLS_ECDH_C) && \
4118 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004119 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004120 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004121#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4122
Ronald Cron6f135e12021-12-08 16:57:54 +01004123#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004124 mbedtls_ssl_transform_free( handshake->transform_handshake );
4125 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004126 mbedtls_free( handshake->transform_earlydata );
4127 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01004128#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004129
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004130
4131#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4132 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4133 * processes datagrams and the fact that a datagram is allowed to have
4134 * several records in it, it is possible that the I/O buffers are not
4135 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004136 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4137 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004138#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004139
Jerry Yuba9c7272021-10-30 11:54:10 +08004140 /* mbedtls_platform_zeroize MUST be last one in this function */
4141 mbedtls_platform_zeroize( handshake,
4142 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004143}
4144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004146{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004147 if( session == NULL )
4148 return;
4149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004151 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004152#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004153
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004154#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004155#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4156 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004157 mbedtls_free( session->hostname );
4158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004160#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004161
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004162 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004163}
4164
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004165#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004166
4167#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4168#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4169#else
4170#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4171#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4172
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004173#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004174
4175#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4176#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4177#else
4178#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4179#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4180
4181#if defined(MBEDTLS_SSL_ALPN)
4182#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4183#else
4184#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4185#endif /* MBEDTLS_SSL_ALPN */
4186
4187#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4188#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4189#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4190#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4191
4192#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4193 ( (uint32_t) ( \
4194 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4195 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4196 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4197 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4198 0u ) )
4199
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004200static unsigned char ssl_serialized_context_header[] = {
4201 MBEDTLS_VERSION_MAJOR,
4202 MBEDTLS_VERSION_MINOR,
4203 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004204 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4205 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4206 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4207 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4208 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004209};
4210
Paul Bakker5121ce52009-01-03 21:22:43 +00004211/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004212 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004213 *
4214 * The format of the serialized data is:
4215 * (in the presentation language of TLS, RFC 8446 section 3)
4216 *
4217 * // header
4218 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004219 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004220 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004221 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004222 * Note: When updating the format, remember to keep these
4223 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004224 *
4225 * // session sub-structure
4226 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4227 * // transform sub-structure
4228 * uint8 random[64]; // ServerHello.random+ClientHello.random
4229 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4230 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4231 * // fields from ssl_context
4232 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4233 * uint64 in_window_top; // DTLS: last validated record seq_num
4234 * uint64 in_window; // DTLS: bitmask for replay protection
4235 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4236 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4237 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4238 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4239 *
4240 * Note that many fields of the ssl_context or sub-structures are not
4241 * serialized, as they fall in one of the following categories:
4242 *
4243 * 1. forced value (eg in_left must be 0)
4244 * 2. pointer to dynamically-allocated memory (eg session, transform)
4245 * 3. value can be re-derived from other data (eg session keys from MS)
4246 * 4. value was temporary (eg content of input buffer)
4247 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004248 */
4249int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4250 unsigned char *buf,
4251 size_t buf_len,
4252 size_t *olen )
4253{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004254 unsigned char *p = buf;
4255 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004256 size_t session_len;
4257 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004258
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004259 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004260 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4261 * this function's documentation.
4262 *
4263 * These are due to assumptions/limitations in the implementation. Some of
4264 * them are likely to stay (no handshake in progress) some might go away
4265 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004266 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004267 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004268 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004269 {
4270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004272 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004273 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004274 {
4275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
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 /* Double-check that sub-structures are indeed ready */
4279 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004280 {
4281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004283 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004284 /* There must be no pending incoming or outgoing data */
4285 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004286 {
4287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004289 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004290 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004291 {
4292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004294 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004295 /* Protocol must be DLTS, not TLS */
4296 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004297 {
4298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004300 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004301 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004302 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004303 {
4304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004306 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004307 /* We must be using an AEAD ciphersuite */
4308 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004309 {
4310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004312 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004313 /* Renegotiation must not be enabled */
4314#if defined(MBEDTLS_SSL_RENEGOTIATION)
4315 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004316 {
4317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004319 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004320#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004321
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004322 /*
4323 * Version and format identifier
4324 */
4325 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004326
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004327 if( used <= buf_len )
4328 {
4329 memcpy( p, ssl_serialized_context_header,
4330 sizeof( ssl_serialized_context_header ) );
4331 p += sizeof( ssl_serialized_context_header );
4332 }
4333
4334 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004335 * Session (length + data)
4336 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004337 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004338 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4339 return( ret );
4340
4341 used += 4 + session_len;
4342 if( used <= buf_len )
4343 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004344 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4345 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004346
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004347 ret = ssl_session_save( ssl->session, 1,
4348 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004349 if( ret != 0 )
4350 return( ret );
4351
4352 p += session_len;
4353 }
4354
4355 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004356 * Transform
4357 */
4358 used += sizeof( ssl->transform->randbytes );
4359 if( used <= buf_len )
4360 {
4361 memcpy( p, ssl->transform->randbytes,
4362 sizeof( ssl->transform->randbytes ) );
4363 p += sizeof( ssl->transform->randbytes );
4364 }
4365
4366#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4367 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4368 if( used <= buf_len )
4369 {
4370 *p++ = ssl->transform->in_cid_len;
4371 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4372 p += ssl->transform->in_cid_len;
4373
4374 *p++ = ssl->transform->out_cid_len;
4375 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4376 p += ssl->transform->out_cid_len;
4377 }
4378#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4379
4380 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004381 * Saved fields from top-level ssl_context structure
4382 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004383 used += 4;
4384 if( used <= buf_len )
4385 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004386 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4387 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004388 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004389
4390#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4391 used += 16;
4392 if( used <= buf_len )
4393 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004394 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4395 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004396
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004397 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4398 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004399 }
4400#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4401
4402#if defined(MBEDTLS_SSL_PROTO_DTLS)
4403 used += 1;
4404 if( used <= buf_len )
4405 {
4406 *p++ = ssl->disable_datagram_packing;
4407 }
4408#endif /* MBEDTLS_SSL_PROTO_DTLS */
4409
Jerry Yuae0b2e22021-10-08 15:21:19 +08004410 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004411 if( used <= buf_len )
4412 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004413 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4414 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004415 }
4416
4417#if defined(MBEDTLS_SSL_PROTO_DTLS)
4418 used += 2;
4419 if( used <= buf_len )
4420 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004421 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4422 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004423 }
4424#endif /* MBEDTLS_SSL_PROTO_DTLS */
4425
4426#if defined(MBEDTLS_SSL_ALPN)
4427 {
4428 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004429 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004430 : 0;
4431
4432 used += 1 + alpn_len;
4433 if( used <= buf_len )
4434 {
4435 *p++ = alpn_len;
4436
4437 if( ssl->alpn_chosen != NULL )
4438 {
4439 memcpy( p, ssl->alpn_chosen, alpn_len );
4440 p += alpn_len;
4441 }
4442 }
4443 }
4444#endif /* MBEDTLS_SSL_ALPN */
4445
4446 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004447 * Done
4448 */
4449 *olen = used;
4450
4451 if( used > buf_len )
4452 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004453
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004454 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4455
Hanno Becker43aefe22020-02-05 10:44:56 +00004456 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004457}
4458
4459/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004460 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004461 *
4462 * This internal version is wrapped by a public function that cleans up in
4463 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004464 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004465MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004466static int ssl_context_load( mbedtls_ssl_context *ssl,
4467 const unsigned char *buf,
4468 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004469{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004470 const unsigned char *p = buf;
4471 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004472 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004474#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004475 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004476#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004477
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004478 /*
4479 * The context should have been freshly setup or reset.
4480 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004481 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004482 * renegotiating, or if the user mistakenly loaded a session first.)
4483 */
4484 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4485 ssl->session != NULL )
4486 {
4487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4488 }
4489
4490 /*
4491 * We can't check that the config matches the initial one, but we can at
4492 * least check it matches the requirements for serializing.
4493 */
4494 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004495 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4496 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004497#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004498 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004499#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004500 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004501 {
4502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4503 }
4504
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004505 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4506
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004507 /*
4508 * Check version identifier
4509 */
4510 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4512
4513 if( memcmp( p, ssl_serialized_context_header,
4514 sizeof( ssl_serialized_context_header ) ) != 0 )
4515 {
4516 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4517 }
4518 p += sizeof( ssl_serialized_context_header );
4519
4520 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004521 * Session
4522 */
4523 if( (size_t)( end - p ) < 4 )
4524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4525
4526 session_len = ( (size_t) p[0] << 24 ) |
4527 ( (size_t) p[1] << 16 ) |
4528 ( (size_t) p[2] << 8 ) |
4529 ( (size_t) p[3] );
4530 p += 4;
4531
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004532 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004533 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004534 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004535 ssl->session_in = ssl->session;
4536 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004537 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004538
4539 if( (size_t)( end - p ) < session_len )
4540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4541
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004542 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004543 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004544 {
4545 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004546 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004547 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004548
4549 p += session_len;
4550
4551 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004552 * Transform
4553 */
4554
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004555 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004556 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004557 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004558 ssl->transform_in = ssl->transform;
4559 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004560 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004561
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004562#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004563 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4564 if( prf_func == NULL )
4565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4566
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004567 /* Read random bytes and populate structure */
4568 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004570
Hanno Beckerbd257552021-03-22 06:59:27 +00004571 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004572 ssl->session->ciphersuite,
4573 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004574#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004575 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004576#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004577 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004578 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004579 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004580 ssl->conf->endpoint,
4581 ssl );
4582 if( ret != 0 )
4583 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004585 p += sizeof( ssl->transform->randbytes );
4586
4587#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4588 /* Read connection IDs and store them */
4589 if( (size_t)( end - p ) < 1 )
4590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4591
4592 ssl->transform->in_cid_len = *p++;
4593
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004594 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4596
4597 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4598 p += ssl->transform->in_cid_len;
4599
4600 ssl->transform->out_cid_len = *p++;
4601
4602 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4604
4605 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4606 p += ssl->transform->out_cid_len;
4607#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4608
4609 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004610 * Saved fields from top-level ssl_context structure
4611 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004612 if( (size_t)( end - p ) < 4 )
4613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4614
4615 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4616 ( (uint32_t) p[1] << 16 ) |
4617 ( (uint32_t) p[2] << 8 ) |
4618 ( (uint32_t) p[3] );
4619 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004620
4621#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4622 if( (size_t)( end - p ) < 16 )
4623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4624
4625 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4626 ( (uint64_t) p[1] << 48 ) |
4627 ( (uint64_t) p[2] << 40 ) |
4628 ( (uint64_t) p[3] << 32 ) |
4629 ( (uint64_t) p[4] << 24 ) |
4630 ( (uint64_t) p[5] << 16 ) |
4631 ( (uint64_t) p[6] << 8 ) |
4632 ( (uint64_t) p[7] );
4633 p += 8;
4634
4635 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4636 ( (uint64_t) p[1] << 48 ) |
4637 ( (uint64_t) p[2] << 40 ) |
4638 ( (uint64_t) p[3] << 32 ) |
4639 ( (uint64_t) p[4] << 24 ) |
4640 ( (uint64_t) p[5] << 16 ) |
4641 ( (uint64_t) p[6] << 8 ) |
4642 ( (uint64_t) p[7] );
4643 p += 8;
4644#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4645
4646#if defined(MBEDTLS_SSL_PROTO_DTLS)
4647 if( (size_t)( end - p ) < 1 )
4648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4649
4650 ssl->disable_datagram_packing = *p++;
4651#endif /* MBEDTLS_SSL_PROTO_DTLS */
4652
Jerry Yud9a94fe2021-09-28 18:58:59 +08004653 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004655 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4656 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004657
4658#if defined(MBEDTLS_SSL_PROTO_DTLS)
4659 if( (size_t)( end - p ) < 2 )
4660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4661
4662 ssl->mtu = ( p[0] << 8 ) | p[1];
4663 p += 2;
4664#endif /* MBEDTLS_SSL_PROTO_DTLS */
4665
4666#if defined(MBEDTLS_SSL_ALPN)
4667 {
4668 uint8_t alpn_len;
4669 const char **cur;
4670
4671 if( (size_t)( end - p ) < 1 )
4672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4673
4674 alpn_len = *p++;
4675
4676 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4677 {
4678 /* alpn_chosen should point to an item in the configured list */
4679 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4680 {
4681 if( strlen( *cur ) == alpn_len &&
4682 memcmp( p, cur, alpn_len ) == 0 )
4683 {
4684 ssl->alpn_chosen = *cur;
4685 break;
4686 }
4687 }
4688 }
4689
4690 /* can only happen on conf mismatch */
4691 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4693
4694 p += alpn_len;
4695 }
4696#endif /* MBEDTLS_SSL_ALPN */
4697
4698 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004699 * Forced fields from top-level ssl_context structure
4700 *
4701 * Most of them already set to the correct value by mbedtls_ssl_init() and
4702 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4703 */
4704 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004705 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004706
Hanno Becker361b10d2019-08-30 10:42:49 +01004707 /* Adjust pointers for header fields of outgoing records to
4708 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004709 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004710
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004711#if defined(MBEDTLS_SSL_PROTO_DTLS)
4712 ssl->in_epoch = 1;
4713#endif
4714
4715 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4716 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004717 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4718 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004719 if( ssl->handshake != NULL )
4720 {
4721 mbedtls_ssl_handshake_free( ssl );
4722 mbedtls_free( ssl->handshake );
4723 ssl->handshake = NULL;
4724 }
4725
4726 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004727 * Done - should have consumed entire buffer
4728 */
4729 if( p != end )
4730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004731
4732 return( 0 );
4733}
4734
4735/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004736 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004737 */
4738int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4739 const unsigned char *buf,
4740 size_t len )
4741{
4742 int ret = ssl_context_load( context, buf, len );
4743
4744 if( ret != 0 )
4745 mbedtls_ssl_free( context );
4746
4747 return( ret );
4748}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004749#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004750
4751/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004752 * Free an SSL context
4753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004755{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004756 if( ssl == NULL )
4757 return;
4758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004760
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004761 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004762 {
sander-visserb8aa2072020-05-06 22:05:13 +02004763#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4764 size_t out_buf_len = ssl->out_buf_len;
4765#else
4766 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4767#endif
4768
Darryl Greenb33cc762019-11-28 14:29:44 +00004769 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004771 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004772 }
4773
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004774 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004775 {
sander-visserb8aa2072020-05-06 22:05:13 +02004776#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4777 size_t in_buf_len = ssl->in_buf_len;
4778#else
4779 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4780#endif
4781
Darryl Greenb33cc762019-11-28 14:29:44 +00004782 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004784 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004785 }
4786
Paul Bakker48916f92012-09-16 19:57:18 +00004787 if( ssl->transform )
4788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 mbedtls_ssl_transform_free( ssl->transform );
4790 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004791 }
4792
4793 if( ssl->handshake )
4794 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004795 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4797 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 mbedtls_free( ssl->handshake );
4800 mbedtls_free( ssl->transform_negotiate );
4801 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004802 }
4803
Ronald Cron6f135e12021-12-08 16:57:54 +01004804#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004805 mbedtls_ssl_transform_free( ssl->transform_application );
4806 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004807#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004808
Paul Bakkerc0463502013-02-14 11:19:38 +01004809 if( ssl->session )
4810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 mbedtls_ssl_session_free( ssl->session );
4812 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004813 }
4814
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004815#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004816 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004818 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004820 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004822
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004823#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004825#endif
4826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004828
Paul Bakker86f04f42013-02-14 11:20:09 +01004829 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004830 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004831}
4832
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004833/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004834 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004835 */
4836void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4837{
4838 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4839}
4840
Gilles Peskineae270bf2021-06-02 00:05:29 +02004841/* The selection should be the same as mbedtls_x509_crt_profile_default in
4842 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004843 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004844 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004845 * about this list.
4846 */
Brett Warrene0edc842021-08-17 09:53:13 +01004847static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004848#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004849 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004850#endif
4851#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004852 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004853#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004854#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004855 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004856#endif
4857#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004858 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004859#endif
4860#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004861 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004862#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004863#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004864 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004865#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004866#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004867 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004868#endif
4869#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004870 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004871#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004872 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004873};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004874
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004875static int ssl_preset_suiteb_ciphersuites[] = {
4876 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4877 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4878 0
4879};
4880
Ronald Crone68ab4f2022-10-05 12:46:29 +02004881#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004882
Jerry Yu909df7b2022-01-22 11:56:27 +08004883/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004884 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004885 * rules SHOULD be upheld.
4886 * - No duplicate entries.
4887 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004888 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004889 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004890 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004891static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004892
Andrzej Kurek25f27152022-08-17 16:09:31 -04004893#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 +08004894 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4895 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004896#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004897 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004898
Andrzej Kurek25f27152022-08-17 16:09:31 -04004899#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 +08004900 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4901 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004902#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004903 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4904
Andrzej Kurek25f27152022-08-17 16:09:31 -04004905#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 +08004906 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4907 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004908#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004909 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004910
Andrzej Kurek25f27152022-08-17 16:09:31 -04004911#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 +08004912 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004913#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 +08004914
Andrzej Kurek25f27152022-08-17 16:09:31 -04004915#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 +08004916 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004917#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 +08004918
Andrzej Kurek25f27152022-08-17 16:09:31 -04004919#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 +08004920 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004921#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 +08004922
Andrzej Kurek25f27152022-08-17 16:09:31 -04004923#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 +08004924 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4925#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4926
Andrzej Kurek25f27152022-08-17 16:09:31 -04004927#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 +08004928 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4929#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4930
Andrzej Kurek25f27152022-08-17 16:09:31 -04004931#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 +08004932 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4933#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4934
Gabor Mezei15b95a62022-05-09 16:37:58 +02004935 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004936};
4937
4938/* NOTICE: see above */
4939#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4940static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004941#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004942#if defined(MBEDTLS_ECDSA_C)
4943 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004944#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004945#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4946 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4947#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004948#if defined(MBEDTLS_RSA_C)
4949 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4950#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004951#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004952#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004953#if defined(MBEDTLS_ECDSA_C)
4954 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004955#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004956#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4957 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4958#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004959#if defined(MBEDTLS_RSA_C)
4960 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4961#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004962#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004963#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004964#if defined(MBEDTLS_ECDSA_C)
4965 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004966#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004967#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4968 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4969#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004970#if defined(MBEDTLS_RSA_C)
4971 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4972#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004973#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004974 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004975};
4976#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4977/* NOTICE: see above */
4978static uint16_t ssl_preset_suiteb_sig_algs[] = {
4979
Andrzej Kurek25f27152022-08-17 16:09:31 -04004980#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 +08004981 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4982 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004983#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004984 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4985
Andrzej Kurek25f27152022-08-17 16:09:31 -04004986#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 +08004987 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4988 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004989#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004990 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004991
Andrzej Kurek25f27152022-08-17 16:09:31 -04004992#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 +08004993 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004994#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 +08004995
Andrzej Kurek25f27152022-08-17 16:09:31 -04004996#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 +08004997 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004998#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004999
Gabor Mezei15b95a62022-05-09 16:37:58 +02005000 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005001};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005002
Jerry Yu909df7b2022-01-22 11:56:27 +08005003/* NOTICE: see above */
5004#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5005static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005006#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005007#if defined(MBEDTLS_ECDSA_C)
5008 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08005009#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005010#if defined(MBEDTLS_RSA_C)
5011 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
5012#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005013#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005014#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005015#if defined(MBEDTLS_ECDSA_C)
5016 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08005017#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005018#if defined(MBEDTLS_RSA_C)
5019 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
5020#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005021#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005022 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005023};
Jerry Yu909df7b2022-01-22 11:56:27 +08005024#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5025
Ronald Crone68ab4f2022-10-05 12:46:29 +02005026#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005027
Brett Warrene0edc842021-08-17 09:53:13 +01005028static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005029#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005030 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005031#endif
5032#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005033 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005034#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005035 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005036};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005037
Ronald Crone68ab4f2022-10-05 12:46:29 +02005038#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005039/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005040 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005041MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005042static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005043{
5044 size_t i, j;
5045 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005046
Gabor Mezei15b95a62022-05-09 16:37:58 +02005047 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005048 {
Jerry Yuf377d642022-01-25 10:43:59 +08005049 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005050 {
Jerry Yuf377d642022-01-25 10:43:59 +08005051 if( sig_algs[i] != sig_algs[j] )
5052 continue;
5053 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5054 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5055 sig_algs[i], j, i );
5056 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005057 }
5058 }
5059 return( ret );
5060}
5061
Ronald Crone68ab4f2022-10-05 12:46:29 +02005062#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005063
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005064/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005065 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005066 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005067int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005068 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005069{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005070#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005072#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005073
Ronald Crone68ab4f2022-10-05 12:46:29 +02005074#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005075 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005076 {
5077 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5078 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5079 }
5080
Jerry Yuf377d642022-01-25 10:43:59 +08005081 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005082 {
5083 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5084 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5085 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005086
5087#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005088 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005089 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005090 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005091 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5092 }
5093
Jerry Yuf377d642022-01-25 10:43:59 +08005094 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005095 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005096 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005097 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5098 }
5099#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005100#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005101
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005102 /* Use the functions here so that they are covered in tests,
5103 * but otherwise access member directly for efficiency */
5104 mbedtls_ssl_conf_endpoint( conf, endpoint );
5105 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005106
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005107 /*
5108 * Things that are common to all presets
5109 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005110#if defined(MBEDTLS_SSL_CLI_C)
5111 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5112 {
5113 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5114#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5115 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5116#endif
5117 }
5118#endif
5119
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005120#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5121 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5122#endif
5123
5124#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5125 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5126#endif
5127
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005128#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005129 conf->f_cookie_write = ssl_cookie_write_dummy;
5130 conf->f_cookie_check = ssl_cookie_check_dummy;
5131#endif
5132
5133#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5134 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5135#endif
5136
Janos Follath088ce432017-04-10 12:42:31 +01005137#if defined(MBEDTLS_SSL_SRV_C)
5138 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005139 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005140#endif
5141
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005142#if defined(MBEDTLS_SSL_PROTO_DTLS)
5143 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5144 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5145#endif
5146
5147#if defined(MBEDTLS_SSL_RENEGOTIATION)
5148 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005149 memset( conf->renego_period, 0x00, 2 );
5150 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005151#endif
5152
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005153#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005154 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5155 {
5156 const unsigned char dhm_p[] =
5157 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5158 const unsigned char dhm_g[] =
5159 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005160
Hanno Beckere2defad2021-07-24 05:59:17 +01005161 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5162 dhm_p, sizeof( dhm_p ),
5163 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5164 {
5165 return( ret );
5166 }
5167 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005168#endif
5169
Ronald Cron6f135e12021-12-08 16:57:54 +01005170#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005171#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005172 mbedtls_ssl_conf_new_session_tickets(
5173 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5174#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005175 /*
5176 * Allow all TLS 1.3 key exchange modes by default.
5177 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005178 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005179#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005180
XiaokangQian4d3a6042022-04-21 13:46:17 +00005181 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5182 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005183#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005184 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005185 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005186#else
XiaokangQian060d8672022-04-21 09:24:56 +00005187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005188#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005189 }
5190 else
5191 {
5192#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5193 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5194 {
5195 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5196 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5197 }
5198 else
5199 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5200 {
5201 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5202 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5203 }
5204#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5205 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5206 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5207#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5208 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5209 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5210#else
5211 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5212#endif
5213 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005214
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005215 /*
5216 * Preset-specific defaults
5217 */
5218 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005219 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005220 /*
5221 * NSA Suite B
5222 */
5223 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005224
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005225 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005226
5227#if defined(MBEDTLS_X509_CRT_PARSE_C)
5228 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005229#endif
5230
Ronald Crone68ab4f2022-10-05 12:46:29 +02005231#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005232#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5233 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5234 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5235 else
5236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5237 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005238#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005239
Brett Warrene0edc842021-08-17 09:53:13 +01005240#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5241 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005242#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005243 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005244 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005245
5246 /*
5247 * Default
5248 */
5249 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005250
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005251 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005252
5253#if defined(MBEDTLS_X509_CRT_PARSE_C)
5254 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5255#endif
5256
Ronald Crone68ab4f2022-10-05 12:46:29 +02005257#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005258#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5259 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5260 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5261 else
5262#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5263 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005264#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005265
Brett Warrene0edc842021-08-17 09:53:13 +01005266#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5267 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005268#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005269 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005270
5271#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5272 conf->dhm_min_bitlen = 1024;
5273#endif
5274 }
5275
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005276 return( 0 );
5277}
5278
5279/*
5280 * Free mbedtls_ssl_config
5281 */
5282void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5283{
5284#if defined(MBEDTLS_DHM_C)
5285 mbedtls_mpi_free( &conf->dhm_P );
5286 mbedtls_mpi_free( &conf->dhm_G );
5287#endif
5288
Ronald Cron73fe8df2022-10-05 14:31:43 +02005289#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005290#if defined(MBEDTLS_USE_PSA_CRYPTO)
5291 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5292 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005293 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5294 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005295#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005296 if( conf->psk != NULL )
5297 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005298 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005299 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005300 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005301 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005302 }
5303
5304 if( conf->psk_identity != NULL )
5305 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005306 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005307 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005308 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005309 conf->psk_identity_len = 0;
5310 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005311#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005312
5313#if defined(MBEDTLS_X509_CRT_PARSE_C)
5314 ssl_key_cert_free( conf->key_cert );
5315#endif
5316
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005317 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005318}
5319
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005320#if defined(MBEDTLS_PK_C) && \
5321 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005322/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327#if defined(MBEDTLS_RSA_C)
5328 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5329 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_ECDSA_C)
5332 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5333 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005336}
5337
Hanno Becker7e5437a2017-04-28 17:15:26 +01005338unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5339{
5340 switch( type ) {
5341 case MBEDTLS_PK_RSA:
5342 return( MBEDTLS_SSL_SIG_RSA );
5343 case MBEDTLS_PK_ECDSA:
5344 case MBEDTLS_PK_ECKEY:
5345 return( MBEDTLS_SSL_SIG_ECDSA );
5346 default:
5347 return( MBEDTLS_SSL_SIG_ANON );
5348 }
5349}
5350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005352{
5353 switch( sig )
5354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355#if defined(MBEDTLS_RSA_C)
5356 case MBEDTLS_SSL_SIG_RSA:
5357 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005358#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359#if defined(MBEDTLS_ECDSA_C)
5360 case MBEDTLS_SSL_SIG_ECDSA:
5361 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005362#endif
5363 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005365 }
5366}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005367#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005368
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005369/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005370 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005373{
5374 switch( hash )
5375 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005376#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 case MBEDTLS_SSL_HASH_MD5:
5378 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005379#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005380#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 case MBEDTLS_SSL_HASH_SHA1:
5382 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005383#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005384#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 case MBEDTLS_SSL_HASH_SHA224:
5386 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005387#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005388#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 case MBEDTLS_SSL_HASH_SHA256:
5390 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005391#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005392#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 case MBEDTLS_SSL_HASH_SHA384:
5394 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005395#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005396#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 case MBEDTLS_SSL_HASH_SHA512:
5398 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005399#endif
5400 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005402 }
5403}
5404
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005405/*
5406 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5407 */
5408unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5409{
5410 switch( md )
5411 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005412#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005413 case MBEDTLS_MD_MD5:
5414 return( MBEDTLS_SSL_HASH_MD5 );
5415#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005416#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005417 case MBEDTLS_MD_SHA1:
5418 return( MBEDTLS_SSL_HASH_SHA1 );
5419#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005420#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005421 case MBEDTLS_MD_SHA224:
5422 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005423#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005424#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005425 case MBEDTLS_MD_SHA256:
5426 return( MBEDTLS_SSL_HASH_SHA256 );
5427#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005428#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005429 case MBEDTLS_MD_SHA384:
5430 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005431#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005432#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005433 case MBEDTLS_MD_SHA512:
5434 return( MBEDTLS_SSL_HASH_SHA512 );
5435#endif
5436 default:
5437 return( MBEDTLS_SSL_HASH_NONE );
5438 }
5439}
5440
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005441/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005442 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005443 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005444 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005445int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005446{
Brett Warrene0edc842021-08-17 09:53:13 +01005447 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005448
Brett Warrene0edc842021-08-17 09:53:13 +01005449 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005450 return( -1 );
5451
Brett Warrene0edc842021-08-17 09:53:13 +01005452 for( ; *group_list != 0; group_list++ )
5453 {
5454 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005455 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005456 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005457
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005458 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005459}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005460
5461#if defined(MBEDTLS_ECP_C)
5462/*
5463 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5464 */
5465int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5466{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005467 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005468 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005469
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005470 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005471 return -1;
5472
5473 uint16_t tls_id = grp_info->tls_id;
5474
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005475 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5476}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005477#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479#if defined(MBEDTLS_X509_CRT_PARSE_C)
5480int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5481 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005482 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005483 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005484{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005485 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005486 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005487 const char *ext_oid;
5488 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005491 {
5492 /* Server part of the key exchange */
5493 switch( ciphersuite->key_exchange )
5494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 case MBEDTLS_KEY_EXCHANGE_RSA:
5496 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005497 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005498 break;
5499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5501 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5502 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5503 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005504 break;
5505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5507 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005508 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005509 break;
5510
5511 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 case MBEDTLS_KEY_EXCHANGE_NONE:
5513 case MBEDTLS_KEY_EXCHANGE_PSK:
5514 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5515 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005516 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005517 usage = 0;
5518 }
5519 }
5520 else
5521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5523 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005524 }
5525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005527 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005528 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005529 ret = -1;
5530 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5535 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005536 }
5537 else
5538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5540 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005541 }
5542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005544 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005545 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005546 ret = -1;
5547 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005548
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005549 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005552
Jerry Yu148165c2021-09-24 23:20:59 +08005553#if defined(MBEDTLS_USE_PSA_CRYPTO)
5554int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5555 const mbedtls_md_type_t md,
5556 unsigned char *dst,
5557 size_t dst_len,
5558 size_t *olen )
5559{
Ronald Cronf6893e12022-01-07 22:09:01 +01005560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5561 psa_hash_operation_t *hash_operation_to_clone;
5562 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5563
Jerry Yu148165c2021-09-24 23:20:59 +08005564 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005565
5566 switch( md )
5567 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005568#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005569 case MBEDTLS_MD_SHA384:
5570 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5571 break;
5572#endif
5573
Andrzej Kurek25f27152022-08-17 16:09:31 -04005574#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005575 case MBEDTLS_MD_SHA256:
5576 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5577 break;
5578#endif
5579
5580 default:
5581 goto exit;
5582 }
5583
5584 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5585 if( status != PSA_SUCCESS )
5586 goto exit;
5587
5588 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5589 if( status != PSA_SUCCESS )
5590 goto exit;
5591
5592exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005593#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5594 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5595 (void) ssl;
5596#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005597 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005598}
5599#else /* MBEDTLS_USE_PSA_CRYPTO */
5600
Andrzej Kurek25f27152022-08-17 16:09:31 -04005601#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005602MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005603static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5604 unsigned char *dst,
5605 size_t dst_len,
5606 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005607{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005608 int ret;
5609 mbedtls_sha512_context sha512;
5610
5611 if( dst_len < 48 )
5612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5613
5614 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005615 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005616
5617 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5618 {
5619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5620 goto exit;
5621 }
5622
5623 *olen = 48;
5624
5625exit:
5626
5627 mbedtls_sha512_free( &sha512 );
5628 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005629}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005630#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005631
Andrzej Kurek25f27152022-08-17 16:09:31 -04005632#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005633MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005634static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5635 unsigned char *dst,
5636 size_t dst_len,
5637 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005638{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005639 int ret;
5640 mbedtls_sha256_context sha256;
5641
5642 if( dst_len < 32 )
5643 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5644
5645 mbedtls_sha256_init( &sha256 );
5646 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005647
Jerry Yu24c0ec32021-09-09 14:21:07 +08005648 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5649 {
5650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5651 goto exit;
5652 }
5653
5654 *olen = 32;
5655
5656exit:
5657
5658 mbedtls_sha256_free( &sha256 );
5659 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005660}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005661#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005662
Jerry Yu000f9762021-09-14 11:12:51 +08005663int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5664 const mbedtls_md_type_t md,
5665 unsigned char *dst,
5666 size_t dst_len,
5667 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005668{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005669 switch( md )
5670 {
5671
Andrzej Kurek25f27152022-08-17 16:09:31 -04005672#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005673 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005674 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005675#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005676
Andrzej Kurek25f27152022-08-17 16:09:31 -04005677#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005678 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005679 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005680#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005681
5682 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005683#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5684 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5685 (void) ssl;
5686 (void) dst;
5687 (void) dst_len;
5688 (void) olen;
5689#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005690 break;
5691 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005693}
XiaokangQian647719a2021-12-07 09:16:29 +00005694
Jerry Yu148165c2021-09-24 23:20:59 +08005695#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005696
Ronald Crone68ab4f2022-10-05 12:46:29 +02005697#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005698/* mbedtls_ssl_parse_sig_alg_ext()
5699 *
5700 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5701 * value (TLS 1.3 RFC8446):
5702 * enum {
5703 * ....
5704 * ecdsa_secp256r1_sha256( 0x0403 ),
5705 * ecdsa_secp384r1_sha384( 0x0503 ),
5706 * ecdsa_secp521r1_sha512( 0x0603 ),
5707 * ....
5708 * } SignatureScheme;
5709 *
5710 * struct {
5711 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5712 * } SignatureSchemeList;
5713 *
5714 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5715 * value (TLS 1.2 RFC5246):
5716 * enum {
5717 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5718 * sha512(6), (255)
5719 * } HashAlgorithm;
5720 *
5721 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5722 * SignatureAlgorithm;
5723 *
5724 * struct {
5725 * HashAlgorithm hash;
5726 * SignatureAlgorithm signature;
5727 * } SignatureAndHashAlgorithm;
5728 *
5729 * SignatureAndHashAlgorithm
5730 * supported_signature_algorithms<2..2^16-2>;
5731 *
5732 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5733 * generalization of the TLS 1.2 signature algorithm extension.
5734 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5735 * `SignatureScheme` field of TLS 1.3
5736 *
5737 */
5738int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5739 const unsigned char *buf,
5740 const unsigned char *end )
5741{
5742 const unsigned char *p = buf;
5743 size_t supported_sig_algs_len = 0;
5744 const unsigned char *supported_sig_algs_end;
5745 uint16_t sig_alg;
5746 uint32_t common_idx = 0;
5747
5748 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5749 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5750 p += 2;
5751
5752 memset( ssl->handshake->received_sig_algs, 0,
5753 sizeof(ssl->handshake->received_sig_algs) );
5754
5755 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5756 supported_sig_algs_end = p + supported_sig_algs_len;
5757 while( p < supported_sig_algs_end )
5758 {
5759 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5760 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5761 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005762 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5763 sig_alg,
5764 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005765#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005766 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005767 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5768 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5769 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005770 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005771 }
5772#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005773
Jerry Yuf3b46b52022-06-19 16:52:27 +08005774 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5775 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005776
5777 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5778 {
5779 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5780 common_idx += 1;
5781 }
5782 }
5783 /* Check that we consumed all the message. */
5784 if( p != end )
5785 {
5786 MBEDTLS_SSL_DEBUG_MSG( 1,
5787 ( "Signature algorithms extension length misaligned" ) );
5788 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5789 MBEDTLS_ERR_SSL_DECODE_ERROR );
5790 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5791 }
5792
5793 if( common_idx == 0 )
5794 {
5795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5796 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5797 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5798 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5799 }
5800
Gabor Mezei15b95a62022-05-09 16:37:58 +02005801 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005802 return( 0 );
5803}
5804
Ronald Crone68ab4f2022-10-05 12:46:29 +02005805#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005806
Jerry Yudc7bd172022-02-17 13:44:15 +08005807#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5808
5809#if defined(MBEDTLS_USE_PSA_CRYPTO)
5810
5811static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5812 mbedtls_svc_key_id_t key,
5813 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005814 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005815 const unsigned char* seed, size_t seed_length,
5816 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005817 const unsigned char* other_secret,
5818 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005819 size_t capacity )
5820{
5821 psa_status_t status;
5822
5823 status = psa_key_derivation_setup( derivation, alg );
5824 if( status != PSA_SUCCESS )
5825 return( status );
5826
5827 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5828 {
5829 status = psa_key_derivation_input_bytes( derivation,
5830 PSA_KEY_DERIVATION_INPUT_SEED,
5831 seed, seed_length );
5832 if( status != PSA_SUCCESS )
5833 return( status );
5834
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005835 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005836 {
5837 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005838 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5839 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005840 if( status != PSA_SUCCESS )
5841 return( status );
5842 }
5843
Jerry Yudc7bd172022-02-17 13:44:15 +08005844 if( mbedtls_svc_key_id_is_null( key ) )
5845 {
5846 status = psa_key_derivation_input_bytes(
5847 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005848 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005849 }
5850 else
5851 {
5852 status = psa_key_derivation_input_key(
5853 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5854 }
5855 if( status != PSA_SUCCESS )
5856 return( status );
5857
5858 status = psa_key_derivation_input_bytes( derivation,
5859 PSA_KEY_DERIVATION_INPUT_LABEL,
5860 label, label_length );
5861 if( status != PSA_SUCCESS )
5862 return( status );
5863 }
5864 else
5865 {
5866 return( PSA_ERROR_NOT_SUPPORTED );
5867 }
5868
5869 status = psa_key_derivation_set_capacity( derivation, capacity );
5870 if( status != PSA_SUCCESS )
5871 return( status );
5872
5873 return( PSA_SUCCESS );
5874}
5875
Andrzej Kurek57d10632022-10-24 10:32:01 -04005876#if defined(PSA_WANT_ALG_SHA_384) || \
5877 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005878MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005879static int tls_prf_generic( mbedtls_md_type_t md_type,
5880 const unsigned char *secret, size_t slen,
5881 const char *label,
5882 const unsigned char *random, size_t rlen,
5883 unsigned char *dstbuf, size_t dlen )
5884{
5885 psa_status_t status;
5886 psa_algorithm_t alg;
5887 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5888 psa_key_derivation_operation_t derivation =
5889 PSA_KEY_DERIVATION_OPERATION_INIT;
5890
5891 if( md_type == MBEDTLS_MD_SHA384 )
5892 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5893 else
5894 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5895
5896 /* Normally a "secret" should be long enough to be impossible to
5897 * find by brute force, and in particular should not be empty. But
5898 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5899 * and for this use case it makes sense to have a 0-length "secret".
5900 * Since the key API doesn't allow importing a key of length 0,
5901 * keep master_key=0, which setup_psa_key_derivation() understands
5902 * to mean a 0-length "secret" input. */
5903 if( slen != 0 )
5904 {
5905 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5906 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5907 psa_set_key_algorithm( &key_attributes, alg );
5908 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5909
5910 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5911 if( status != PSA_SUCCESS )
5912 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5913 }
5914
5915 status = setup_psa_key_derivation( &derivation,
5916 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005917 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005918 random, rlen,
5919 (unsigned char const *) label,
5920 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005921 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005922 dlen );
5923 if( status != PSA_SUCCESS )
5924 {
5925 psa_key_derivation_abort( &derivation );
5926 psa_destroy_key( master_key );
5927 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5928 }
5929
5930 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5931 if( status != PSA_SUCCESS )
5932 {
5933 psa_key_derivation_abort( &derivation );
5934 psa_destroy_key( master_key );
5935 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5936 }
5937
5938 status = psa_key_derivation_abort( &derivation );
5939 if( status != PSA_SUCCESS )
5940 {
5941 psa_destroy_key( master_key );
5942 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5943 }
5944
5945 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5946 status = psa_destroy_key( master_key );
5947 if( status != PSA_SUCCESS )
5948 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5949
5950 return( 0 );
5951}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005952#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005953#else /* MBEDTLS_USE_PSA_CRYPTO */
5954
Andrzej Kurek57d10632022-10-24 10:32:01 -04005955#if defined(MBEDTLS_MD_C) && \
5956 ( defined(MBEDTLS_SHA256_C) || \
5957 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005958MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005959static int tls_prf_generic( mbedtls_md_type_t md_type,
5960 const unsigned char *secret, size_t slen,
5961 const char *label,
5962 const unsigned char *random, size_t rlen,
5963 unsigned char *dstbuf, size_t dlen )
5964{
5965 size_t nb;
5966 size_t i, j, k, md_len;
5967 unsigned char *tmp;
5968 size_t tmp_len = 0;
5969 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5970 const mbedtls_md_info_t *md_info;
5971 mbedtls_md_context_t md_ctx;
5972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5973
5974 mbedtls_md_init( &md_ctx );
5975
5976 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5978
5979 md_len = mbedtls_md_get_size( md_info );
5980
5981 tmp_len = md_len + strlen( label ) + rlen;
5982 tmp = mbedtls_calloc( 1, tmp_len );
5983 if( tmp == NULL )
5984 {
5985 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5986 goto exit;
5987 }
5988
5989 nb = strlen( label );
5990 memcpy( tmp + md_len, label, nb );
5991 memcpy( tmp + md_len + nb, random, rlen );
5992 nb += rlen;
5993
5994 /*
5995 * Compute P_<hash>(secret, label + random)[0..dlen]
5996 */
5997 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5998 goto exit;
5999
6000 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
6001 if( ret != 0 )
6002 goto exit;
6003 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
6004 if( ret != 0 )
6005 goto exit;
6006 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6007 if( ret != 0 )
6008 goto exit;
6009
6010 for( i = 0; i < dlen; i += md_len )
6011 {
6012 ret = mbedtls_md_hmac_reset ( &md_ctx );
6013 if( ret != 0 )
6014 goto exit;
6015 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
6016 if( ret != 0 )
6017 goto exit;
6018 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
6019 if( ret != 0 )
6020 goto exit;
6021
6022 ret = mbedtls_md_hmac_reset ( &md_ctx );
6023 if( ret != 0 )
6024 goto exit;
6025 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
6026 if( ret != 0 )
6027 goto exit;
6028 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6029 if( ret != 0 )
6030 goto exit;
6031
6032 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
6033
6034 for( j = 0; j < k; j++ )
6035 dstbuf[i + j] = h_i[j];
6036 }
6037
6038exit:
6039 mbedtls_md_free( &md_ctx );
6040
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006041 if ( tmp != NULL )
6042 mbedtls_platform_zeroize( tmp, tmp_len );
6043
Jerry Yudc7bd172022-02-17 13:44:15 +08006044 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6045
6046 mbedtls_free( tmp );
6047
6048 return( ret );
6049}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006050#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006051#endif /* MBEDTLS_USE_PSA_CRYPTO */
6052
Andrzej Kurek25f27152022-08-17 16:09:31 -04006053#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006054MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006055static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6056 const char *label,
6057 const unsigned char *random, size_t rlen,
6058 unsigned char *dstbuf, size_t dlen )
6059{
6060 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6061 label, random, rlen, dstbuf, dlen ) );
6062}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006063#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006064
Andrzej Kurek25f27152022-08-17 16:09:31 -04006065#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006066MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006067static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6068 const char *label,
6069 const unsigned char *random, size_t rlen,
6070 unsigned char *dstbuf, size_t dlen )
6071{
6072 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6073 label, random, rlen, dstbuf, dlen ) );
6074}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006075#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006076
Jerry Yuf009d862022-02-17 14:01:37 +08006077/*
6078 * Set appropriate PRF function and other SSL / TLS1.2 functions
6079 *
6080 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006081 * - hash associated with the ciphersuite (only used by TLS 1.2)
6082 *
6083 * Outputs:
6084 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6085 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006086MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006087static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006088 mbedtls_md_type_t hash )
6089{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006090#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006091 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006092 {
6093 handshake->tls_prf = tls_prf_sha384;
6094 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6095 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6096 }
6097 else
6098#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006099#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006100 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006101 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006102 handshake->tls_prf = tls_prf_sha256;
6103 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6104 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6105 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006106#else
Jerry Yuf009d862022-02-17 14:01:37 +08006107 {
Jerry Yuf009d862022-02-17 14:01:37 +08006108 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006109 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6111 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006112#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006113
6114 return( 0 );
6115}
Jerry Yud6ab2352022-02-17 14:03:43 +08006116
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006117/*
6118 * Compute master secret if needed
6119 *
6120 * Parameters:
6121 * [in/out] handshake
6122 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6123 * (PSA-PSK) ciphersuite_info, psk_opaque
6124 * [out] premaster (cleared)
6125 * [out] master
6126 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6127 * debug: conf->f_dbg, conf->p_dbg
6128 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006129 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006130 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006131MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006132static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6133 unsigned char *master,
6134 const mbedtls_ssl_context *ssl )
6135{
6136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6137
6138 /* cf. RFC 5246, Section 8.1:
6139 * "The master secret is always exactly 48 bytes in length." */
6140 size_t const master_secret_len = 48;
6141
6142#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6143 unsigned char session_hash[48];
6144#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6145
6146 /* The label for the KDF used for key expansion.
6147 * This is either "master secret" or "extended master secret"
6148 * depending on whether the Extended Master Secret extension
6149 * is used. */
6150 char const *lbl = "master secret";
6151
Przemek Stekielae4ed302022-04-05 17:15:55 +02006152 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006153 * - If the Extended Master Secret extension is not used,
6154 * this is ClientHello.Random + ServerHello.Random
6155 * (see Sect. 8.1 in RFC 5246).
6156 * - If the Extended Master Secret extension is used,
6157 * this is the transcript of the handshake so far.
6158 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006159 unsigned char const *seed = handshake->randbytes;
6160 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006161
6162#if !defined(MBEDTLS_DEBUG_C) && \
6163 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6164 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6165 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6166 ssl = NULL; /* make sure we don't use it except for those cases */
6167 (void) ssl;
6168#endif
6169
6170 if( handshake->resume != 0 )
6171 {
6172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6173 return( 0 );
6174 }
6175
6176#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6177 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6178 {
6179 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006180 seed = session_hash;
6181 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006182
6183 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006184 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006185 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006186#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006187
Przemek Stekiel99114f32022-04-22 11:20:09 +02006188#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006189 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006190 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006191 {
6192 /* Perform PSK-to-MS expansion in a single step. */
6193 psa_status_t status;
6194 psa_algorithm_t alg;
6195 mbedtls_svc_key_id_t psk;
6196 psa_key_derivation_operation_t derivation =
6197 PSA_KEY_DERIVATION_OPERATION_INIT;
6198 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6199
6200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6201
6202 psk = mbedtls_ssl_get_opaque_psk( ssl );
6203
6204 if( hash_alg == MBEDTLS_MD_SHA384 )
6205 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6206 else
6207 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6208
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006209 size_t other_secret_len = 0;
6210 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006211
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006212 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006213 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006214 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006215 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006216 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006217 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006218 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006219 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006220 other_secret_len = 48;
6221 other_secret = handshake->premaster + 2;
6222 break;
6223 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006224 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006225 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6226 other_secret = handshake->premaster + 2;
6227 break;
6228 default:
6229 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006230 }
6231
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006232 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006233 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006234 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006235 (unsigned char const *) lbl,
6236 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006237 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006238 master_secret_len );
6239 if( status != PSA_SUCCESS )
6240 {
6241 psa_key_derivation_abort( &derivation );
6242 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6243 }
6244
6245 status = psa_key_derivation_output_bytes( &derivation,
6246 master,
6247 master_secret_len );
6248 if( status != PSA_SUCCESS )
6249 {
6250 psa_key_derivation_abort( &derivation );
6251 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6252 }
6253
6254 status = psa_key_derivation_abort( &derivation );
6255 if( status != PSA_SUCCESS )
6256 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6257 }
6258 else
6259#endif
6260 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006261#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6262 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6263 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6264 {
6265 psa_status_t status;
6266 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6267 psa_key_derivation_operation_t derivation =
6268 PSA_KEY_DERIVATION_OPERATION_INIT;
6269
6270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6271
6272 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6273
6274 status = psa_key_derivation_setup( &derivation, alg );
6275 if( status != PSA_SUCCESS )
6276 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6277
6278 status = psa_key_derivation_set_capacity( &derivation,
6279 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6280 if( status != PSA_SUCCESS )
6281 {
6282 psa_key_derivation_abort( &derivation );
6283 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6284 }
6285
6286 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6287 &derivation );
6288 if( status != PSA_SUCCESS )
6289 {
6290 psa_key_derivation_abort( &derivation );
6291 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6292 }
6293
6294 status = psa_key_derivation_output_bytes( &derivation,
6295 handshake->premaster,
6296 handshake->pmslen );
6297 if( status != PSA_SUCCESS )
6298 {
6299 psa_key_derivation_abort( &derivation );
6300 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6301 }
6302
6303 status = psa_key_derivation_abort( &derivation );
6304 if( status != PSA_SUCCESS )
6305 {
6306 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6307 }
6308 }
6309#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006310 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006311 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006312 master,
6313 master_secret_len );
6314 if( ret != 0 )
6315 {
6316 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6317 return( ret );
6318 }
6319
6320 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6321 handshake->premaster,
6322 handshake->pmslen );
6323
6324 mbedtls_platform_zeroize( handshake->premaster,
6325 sizeof(handshake->premaster) );
6326 }
6327
6328 return( 0 );
6329}
6330
Jerry Yud62f87e2022-02-17 14:09:02 +08006331int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6332{
6333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6334 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6335 ssl->handshake->ciphersuite_info;
6336
6337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6338
6339 /* Set PRF, calc_verify and calc_finished function pointers */
6340 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006341 ciphersuite_info->mac );
6342 if( ret != 0 )
6343 {
6344 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6345 return( ret );
6346 }
6347
6348 /* Compute master secret if needed */
6349 ret = ssl_compute_master( ssl->handshake,
6350 ssl->session_negotiate->master,
6351 ssl );
6352 if( ret != 0 )
6353 {
6354 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6355 return( ret );
6356 }
6357
6358 /* Swap the client and server random values:
6359 * - MS derivation wanted client+server (RFC 5246 8.1)
6360 * - key derivation wants server+client (RFC 5246 6.3) */
6361 {
6362 unsigned char tmp[64];
6363 memcpy( tmp, ssl->handshake->randbytes, 64 );
6364 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6365 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6366 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6367 }
6368
6369 /* Populate transform structure */
6370 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6371 ssl->session_negotiate->ciphersuite,
6372 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006373#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006374 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006375#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006376 ssl->handshake->tls_prf,
6377 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006378 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006379 ssl->conf->endpoint,
6380 ssl );
6381 if( ret != 0 )
6382 {
6383 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6384 return( ret );
6385 }
6386
6387 /* We no longer need Server/ClientHello.random values */
6388 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6389 sizeof( ssl->handshake->randbytes ) );
6390
6391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6392
6393 return( 0 );
6394}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006395
Ronald Cron4dcbca92022-03-07 10:21:40 +01006396int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6397{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006398 switch( md )
6399 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006400#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006401 case MBEDTLS_SSL_HASH_SHA384:
6402 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6403 break;
6404#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006405#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006406 case MBEDTLS_SSL_HASH_SHA256:
6407 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6408 break;
6409#endif
6410 default:
6411 return( -1 );
6412 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006413#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6414 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6415 (void) ssl;
6416#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006417 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006418}
6419
Andrzej Kurek25f27152022-08-17 16:09:31 -04006420#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006421void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6422 unsigned char *hash,
6423 size_t *hlen )
6424{
6425#if defined(MBEDTLS_USE_PSA_CRYPTO)
6426 size_t hash_size;
6427 psa_status_t status;
6428 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6429
6430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6431 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6432 if( status != PSA_SUCCESS )
6433 {
6434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6435 return;
6436 }
6437
6438 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6439 if( status != PSA_SUCCESS )
6440 {
6441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6442 return;
6443 }
6444
6445 *hlen = 32;
6446 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6448#else
6449 mbedtls_sha256_context sha256;
6450
6451 mbedtls_sha256_init( &sha256 );
6452
6453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6454
6455 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6456 mbedtls_sha256_finish( &sha256, hash );
6457
6458 *hlen = 32;
6459
6460 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6462
6463 mbedtls_sha256_free( &sha256 );
6464#endif /* MBEDTLS_USE_PSA_CRYPTO */
6465 return;
6466}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006467#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006468
Andrzej Kurek25f27152022-08-17 16:09:31 -04006469#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006470void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6471 unsigned char *hash,
6472 size_t *hlen )
6473{
6474#if defined(MBEDTLS_USE_PSA_CRYPTO)
6475 size_t hash_size;
6476 psa_status_t status;
6477 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6478
6479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6480 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6481 if( status != PSA_SUCCESS )
6482 {
6483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6484 return;
6485 }
6486
6487 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6488 if( status != PSA_SUCCESS )
6489 {
6490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6491 return;
6492 }
6493
6494 *hlen = 48;
6495 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6497#else
6498 mbedtls_sha512_context sha512;
6499
6500 mbedtls_sha512_init( &sha512 );
6501
6502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6503
Andrzej Kureka242e832022-08-11 10:03:14 -04006504 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006505 mbedtls_sha512_finish( &sha512, hash );
6506
6507 *hlen = 48;
6508
6509 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6511
6512 mbedtls_sha512_free( &sha512 );
6513#endif /* MBEDTLS_USE_PSA_CRYPTO */
6514 return;
6515}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006516#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006517
Neil Armstrong80f6f322022-05-03 17:56:38 +02006518#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6519 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006520int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6521{
6522 unsigned char *p = ssl->handshake->premaster;
6523 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6524 const unsigned char *psk = NULL;
6525 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006526 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006527
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006528 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006529 {
6530 /*
6531 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006532 * checked before calling this function.
6533 *
6534 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006535 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006536 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006537 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6538 {
6539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6541 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006542 }
6543
6544 /*
6545 * PMS = struct {
6546 * opaque other_secret<0..2^16-1>;
6547 * opaque psk<0..2^16-1>;
6548 * };
6549 * with "other_secret" depending on the particular key exchange
6550 */
6551#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6552 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6553 {
6554 if( end - p < 2 )
6555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6556
6557 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6558 p += 2;
6559
6560 if( end < p || (size_t)( end - p ) < psk_len )
6561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6562
6563 memset( p, 0, psk_len );
6564 p += psk_len;
6565 }
6566 else
6567#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6568#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6569 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6570 {
6571 /*
6572 * other_secret already set by the ClientKeyExchange message,
6573 * and is 48 bytes long
6574 */
6575 if( end - p < 2 )
6576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6577
6578 *p++ = 0;
6579 *p++ = 48;
6580 p += 48;
6581 }
6582 else
6583#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6584#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6585 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6586 {
6587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6588 size_t len;
6589
6590 /* Write length only when we know the actual value */
6591 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6592 p + 2, end - ( p + 2 ), &len,
6593 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6594 {
6595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6596 return( ret );
6597 }
6598 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6599 p += 2 + len;
6600
6601 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6602 }
6603 else
6604#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006605#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006606 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6607 {
6608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6609 size_t zlen;
6610
6611 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6612 p + 2, end - ( p + 2 ),
6613 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6614 {
6615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6616 return( ret );
6617 }
6618
6619 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6620 p += 2 + zlen;
6621
6622 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6623 MBEDTLS_DEBUG_ECDH_Z );
6624 }
6625 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006626#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006627 {
6628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6630 }
6631
6632 /* opaque psk<0..2^16-1>; */
6633 if( end - p < 2 )
6634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6635
6636 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6637 p += 2;
6638
6639 if( end < p || (size_t)( end - p ) < psk_len )
6640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6641
6642 memcpy( p, psk, psk_len );
6643 p += psk_len;
6644
6645 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6646
6647 return( 0 );
6648}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006649#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006650
6651#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006652MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006653static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6654
6655#if defined(MBEDTLS_SSL_PROTO_DTLS)
6656int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6657{
6658 /* If renegotiation is not enforced, retransmit until we would reach max
6659 * timeout if we were using the usual handshake doubling scheme */
6660 if( ssl->conf->renego_max_records < 0 )
6661 {
6662 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6663 unsigned char doublings = 1;
6664
6665 while( ratio != 0 )
6666 {
6667 ++doublings;
6668 ratio >>= 1;
6669 }
6670
6671 if( ++ssl->renego_records_seen > doublings )
6672 {
6673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6674 return( 0 );
6675 }
6676 }
6677
6678 return( ssl_write_hello_request( ssl ) );
6679}
6680#endif
6681#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006682
Jerry Yud9526692022-02-17 14:23:47 +08006683/*
6684 * Handshake functions
6685 */
6686#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6687/* No certificate support -> dummy functions */
6688int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6689{
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 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6704}
6705
6706int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6707{
6708 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6709 ssl->handshake->ciphersuite_info;
6710
6711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6712
6713 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6714 {
6715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6716 ssl->state++;
6717 return( 0 );
6718 }
6719
6720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6722}
6723
6724#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6725/* Some certificate support -> implement write and parse */
6726
6727int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6728{
6729 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6730 size_t i, n;
6731 const mbedtls_x509_crt *crt;
6732 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6733 ssl->handshake->ciphersuite_info;
6734
6735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6736
6737 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6738 {
6739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6740 ssl->state++;
6741 return( 0 );
6742 }
6743
6744#if defined(MBEDTLS_SSL_CLI_C)
6745 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6746 {
6747 if( ssl->handshake->client_auth == 0 )
6748 {
6749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6750 ssl->state++;
6751 return( 0 );
6752 }
6753 }
6754#endif /* MBEDTLS_SSL_CLI_C */
6755#if defined(MBEDTLS_SSL_SRV_C)
6756 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6757 {
6758 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6759 {
6760 /* Should never happen because we shouldn't have picked the
6761 * ciphersuite if we don't have a certificate. */
6762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6763 }
6764 }
6765#endif
6766
6767 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6768
6769 /*
6770 * 0 . 0 handshake type
6771 * 1 . 3 handshake length
6772 * 4 . 6 length of all certs
6773 * 7 . 9 length of cert. 1
6774 * 10 . n-1 peer certificate
6775 * n . n+2 length of cert. 2
6776 * n+3 . ... upper level cert, etc.
6777 */
6778 i = 7;
6779 crt = mbedtls_ssl_own_cert( ssl );
6780
6781 while( crt != NULL )
6782 {
6783 n = crt->raw.len;
6784 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6785 {
6786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6787 " > %" MBEDTLS_PRINTF_SIZET,
6788 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6789 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6790 }
6791
6792 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6793 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6794 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6795
6796 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6797 i += n; crt = crt->next;
6798 }
6799
6800 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6801 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6802 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6803
6804 ssl->out_msglen = i;
6805 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6806 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6807
6808 ssl->state++;
6809
6810 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6811 {
6812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6813 return( ret );
6814 }
6815
6816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6817
6818 return( ret );
6819}
6820
6821#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6822
6823#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006824MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006825static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6826 unsigned char *crt_buf,
6827 size_t crt_buf_len )
6828{
6829 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6830
6831 if( peer_crt == NULL )
6832 return( -1 );
6833
6834 if( peer_crt->raw.len != crt_buf_len )
6835 return( -1 );
6836
6837 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6838}
6839#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006840MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006841static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6842 unsigned char *crt_buf,
6843 size_t crt_buf_len )
6844{
6845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6846 unsigned char const * const peer_cert_digest =
6847 ssl->session->peer_cert_digest;
6848 mbedtls_md_type_t const peer_cert_digest_type =
6849 ssl->session->peer_cert_digest_type;
6850 mbedtls_md_info_t const * const digest_info =
6851 mbedtls_md_info_from_type( peer_cert_digest_type );
6852 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6853 size_t digest_len;
6854
6855 if( peer_cert_digest == NULL || digest_info == NULL )
6856 return( -1 );
6857
6858 digest_len = mbedtls_md_get_size( digest_info );
6859 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6860 return( -1 );
6861
6862 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6863 if( ret != 0 )
6864 return( -1 );
6865
6866 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6867}
6868#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6869#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6870
6871/*
6872 * Once the certificate message is read, parse it into a cert chain and
6873 * perform basic checks, but leave actual verification to the caller
6874 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006875MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006876static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6877 mbedtls_x509_crt *chain )
6878{
6879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6880#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6881 int crt_cnt=0;
6882#endif
6883 size_t i, n;
6884 uint8_t alert;
6885
6886 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6887 {
6888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6889 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6890 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6891 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6892 }
6893
6894 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6895 {
6896 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6897 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6898 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6899 }
6900
6901 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6902 {
6903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6904 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6905 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6906 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6907 }
6908
6909 i = mbedtls_ssl_hs_hdr_len( ssl );
6910
6911 /*
6912 * Same message structure as in mbedtls_ssl_write_certificate()
6913 */
6914 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6915
6916 if( ssl->in_msg[i] != 0 ||
6917 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6918 {
6919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6920 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6922 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6923 }
6924
6925 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6926 i += 3;
6927
6928 /* Iterate through and parse the CRTs in the provided chain. */
6929 while( i < ssl->in_hslen )
6930 {
6931 /* Check that there's room for the next CRT's length fields. */
6932 if ( i + 3 > ssl->in_hslen ) {
6933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6934 mbedtls_ssl_send_alert_message( ssl,
6935 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6936 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6937 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6938 }
6939 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6940 * anything beyond 2**16 ~ 64K. */
6941 if( ssl->in_msg[i] != 0 )
6942 {
6943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6944 mbedtls_ssl_send_alert_message( ssl,
6945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6946 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6947 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6948 }
6949
6950 /* Read length of the next CRT in the chain. */
6951 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6952 | (unsigned int) ssl->in_msg[i + 2];
6953 i += 3;
6954
6955 if( n < 128 || i + n > ssl->in_hslen )
6956 {
6957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6958 mbedtls_ssl_send_alert_message( ssl,
6959 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6960 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6961 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6962 }
6963
6964 /* Check if we're handling the first CRT in the chain. */
6965#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6966 if( crt_cnt++ == 0 &&
6967 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6968 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6969 {
6970 /* During client-side renegotiation, check that the server's
6971 * end-CRTs hasn't changed compared to the initial handshake,
6972 * mitigating the triple handshake attack. On success, reuse
6973 * the original end-CRT instead of parsing it again. */
6974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6975 if( ssl_check_peer_crt_unchanged( ssl,
6976 &ssl->in_msg[i],
6977 n ) != 0 )
6978 {
6979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6980 mbedtls_ssl_send_alert_message( ssl,
6981 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6982 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6983 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6984 }
6985
6986 /* Now we can safely free the original chain. */
6987 ssl_clear_peer_cert( ssl->session );
6988 }
6989#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6990
6991 /* Parse the next certificate in the chain. */
6992#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6993 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6994#else
6995 /* If we don't need to store the CRT chain permanently, parse
6996 * it in-place from the input buffer instead of making a copy. */
6997 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6998#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6999 switch( ret )
7000 {
7001 case 0: /*ok*/
7002 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7003 /* Ignore certificate with an unknown algorithm: maybe a
7004 prior certificate was already trusted. */
7005 break;
7006
7007 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7008 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7009 goto crt_parse_der_failed;
7010
7011 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7012 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7013 goto crt_parse_der_failed;
7014
7015 default:
7016 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7017 crt_parse_der_failed:
7018 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
7019 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7020 return( ret );
7021 }
7022
7023 i += n;
7024 }
7025
7026 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
7027 return( 0 );
7028}
7029
7030#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007031MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007032static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7033{
7034 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7035 return( -1 );
7036
7037 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7038 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7039 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7040 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7041 {
Ronald Cron19385882022-06-15 16:26:13 +02007042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007043 return( 0 );
7044 }
7045 return( -1 );
7046}
7047#endif /* MBEDTLS_SSL_SRV_C */
7048
7049/* Check if a certificate message is expected.
7050 * Return either
7051 * - SSL_CERTIFICATE_EXPECTED, or
7052 * - SSL_CERTIFICATE_SKIP
7053 * indicating whether a Certificate message is expected or not.
7054 */
7055#define SSL_CERTIFICATE_EXPECTED 0
7056#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007057MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007058static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7059 int authmode )
7060{
7061 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7062 ssl->handshake->ciphersuite_info;
7063
7064 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7065 return( SSL_CERTIFICATE_SKIP );
7066
7067#if defined(MBEDTLS_SSL_SRV_C)
7068 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7069 {
7070 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7071 return( SSL_CERTIFICATE_SKIP );
7072
7073 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7074 {
7075 ssl->session_negotiate->verify_result =
7076 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7077 return( SSL_CERTIFICATE_SKIP );
7078 }
7079 }
7080#else
7081 ((void) authmode);
7082#endif /* MBEDTLS_SSL_SRV_C */
7083
7084 return( SSL_CERTIFICATE_EXPECTED );
7085}
7086
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007087MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007088static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7089 int authmode,
7090 mbedtls_x509_crt *chain,
7091 void *rs_ctx )
7092{
7093 int ret = 0;
7094 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7095 ssl->handshake->ciphersuite_info;
7096 int have_ca_chain = 0;
7097
7098 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7099 void *p_vrfy;
7100
7101 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7102 return( 0 );
7103
7104 if( ssl->f_vrfy != NULL )
7105 {
7106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7107 f_vrfy = ssl->f_vrfy;
7108 p_vrfy = ssl->p_vrfy;
7109 }
7110 else
7111 {
7112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7113 f_vrfy = ssl->conf->f_vrfy;
7114 p_vrfy = ssl->conf->p_vrfy;
7115 }
7116
7117 /*
7118 * Main check: verify certificate
7119 */
7120#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7121 if( ssl->conf->f_ca_cb != NULL )
7122 {
7123 ((void) rs_ctx);
7124 have_ca_chain = 1;
7125
7126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7127 ret = mbedtls_x509_crt_verify_with_ca_cb(
7128 chain,
7129 ssl->conf->f_ca_cb,
7130 ssl->conf->p_ca_cb,
7131 ssl->conf->cert_profile,
7132 ssl->hostname,
7133 &ssl->session_negotiate->verify_result,
7134 f_vrfy, p_vrfy );
7135 }
7136 else
7137#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7138 {
7139 mbedtls_x509_crt *ca_chain;
7140 mbedtls_x509_crl *ca_crl;
7141
7142#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7143 if( ssl->handshake->sni_ca_chain != NULL )
7144 {
7145 ca_chain = ssl->handshake->sni_ca_chain;
7146 ca_crl = ssl->handshake->sni_ca_crl;
7147 }
7148 else
7149#endif
7150 {
7151 ca_chain = ssl->conf->ca_chain;
7152 ca_crl = ssl->conf->ca_crl;
7153 }
7154
7155 if( ca_chain != NULL )
7156 have_ca_chain = 1;
7157
7158 ret = mbedtls_x509_crt_verify_restartable(
7159 chain,
7160 ca_chain, ca_crl,
7161 ssl->conf->cert_profile,
7162 ssl->hostname,
7163 &ssl->session_negotiate->verify_result,
7164 f_vrfy, p_vrfy, rs_ctx );
7165 }
7166
7167 if( ret != 0 )
7168 {
7169 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7170 }
7171
7172#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7173 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7174 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7175#endif
7176
7177 /*
7178 * Secondary checks: always done, but change 'ret' only if it was 0
7179 */
7180
7181#if defined(MBEDTLS_ECP_C)
7182 {
7183 const mbedtls_pk_context *pk = &chain->pk;
7184
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007185 /* If certificate uses an EC key, make sure the curve is OK.
7186 * This is a public key, so it can't be opaque, so can_do() is a good
7187 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007188 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007189 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007190 /* and in the unlikely case the above assumption no longer holds
7191 * we are making sure that pk_ec() here does not return a NULL
7192 */
7193 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7194 if( ec == NULL )
7195 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7198 }
Jerry Yud9526692022-02-17 14:23:47 +08007199
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007200 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7201 {
7202 ssl->session_negotiate->verify_result |=
7203 MBEDTLS_X509_BADCERT_BAD_KEY;
7204
7205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7206 if( ret == 0 )
7207 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7208 }
Jerry Yud9526692022-02-17 14:23:47 +08007209 }
7210 }
7211#endif /* MBEDTLS_ECP_C */
7212
7213 if( mbedtls_ssl_check_cert_usage( chain,
7214 ciphersuite_info,
7215 ! ssl->conf->endpoint,
7216 &ssl->session_negotiate->verify_result ) != 0 )
7217 {
7218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7219 if( ret == 0 )
7220 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7221 }
7222
7223 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7224 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7225 * with details encoded in the verification flags. All other kinds
7226 * of error codes, including those from the user provided f_vrfy
7227 * functions, are treated as fatal and lead to a failure of
7228 * ssl_parse_certificate even if verification was optional. */
7229 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7230 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7231 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7232 {
7233 ret = 0;
7234 }
7235
7236 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7237 {
7238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7239 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7240 }
7241
7242 if( ret != 0 )
7243 {
7244 uint8_t alert;
7245
7246 /* The certificate may have been rejected for several reasons.
7247 Pick one and send the corresponding alert. Which alert to send
7248 may be a subject of debate in some cases. */
7249 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7250 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7251 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7252 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7253 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7254 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7256 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7258 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7260 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7261 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7262 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7263 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7264 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7265 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7266 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7267 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7268 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7269 else
7270 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7271 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7272 alert );
7273 }
7274
7275#if defined(MBEDTLS_DEBUG_C)
7276 if( ssl->session_negotiate->verify_result != 0 )
7277 {
7278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7279 (unsigned int) ssl->session_negotiate->verify_result ) );
7280 }
7281 else
7282 {
7283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7284 }
7285#endif /* MBEDTLS_DEBUG_C */
7286
7287 return( ret );
7288}
7289
7290#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007291MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007292static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7293 unsigned char *start, size_t len )
7294{
7295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7296 /* Remember digest of the peer's end-CRT. */
7297 ssl->session_negotiate->peer_cert_digest =
7298 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7299 if( ssl->session_negotiate->peer_cert_digest == NULL )
7300 {
7301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7302 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7303 mbedtls_ssl_send_alert_message( ssl,
7304 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7305 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7306
7307 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7308 }
7309
7310 ret = mbedtls_md( mbedtls_md_info_from_type(
7311 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7312 start, len,
7313 ssl->session_negotiate->peer_cert_digest );
7314
7315 ssl->session_negotiate->peer_cert_digest_type =
7316 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7317 ssl->session_negotiate->peer_cert_digest_len =
7318 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7319
7320 return( ret );
7321}
7322
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007323MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007324static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7325 unsigned char *start, size_t len )
7326{
7327 unsigned char *end = start + len;
7328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7329
7330 /* Make a copy of the peer's raw public key. */
7331 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7332 ret = mbedtls_pk_parse_subpubkey( &start, end,
7333 &ssl->handshake->peer_pubkey );
7334 if( ret != 0 )
7335 {
7336 /* We should have parsed the public key before. */
7337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7338 }
7339
7340 return( 0 );
7341}
7342#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7343
7344int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7345{
7346 int ret = 0;
7347 int crt_expected;
7348#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7349 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7350 ? ssl->handshake->sni_authmode
7351 : ssl->conf->authmode;
7352#else
7353 const int authmode = ssl->conf->authmode;
7354#endif
7355 void *rs_ctx = NULL;
7356 mbedtls_x509_crt *chain = NULL;
7357
7358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7359
7360 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7361 if( crt_expected == SSL_CERTIFICATE_SKIP )
7362 {
7363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7364 goto exit;
7365 }
7366
7367#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7368 if( ssl->handshake->ecrs_enabled &&
7369 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7370 {
7371 chain = ssl->handshake->ecrs_peer_cert;
7372 ssl->handshake->ecrs_peer_cert = NULL;
7373 goto crt_verify;
7374 }
7375#endif
7376
7377 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7378 {
7379 /* mbedtls_ssl_read_record may have sent an alert already. We
7380 let it decide whether to alert. */
7381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7382 goto exit;
7383 }
7384
7385#if defined(MBEDTLS_SSL_SRV_C)
7386 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7387 {
7388 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7389
7390 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7391 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7392
7393 goto exit;
7394 }
7395#endif /* MBEDTLS_SSL_SRV_C */
7396
7397 /* Clear existing peer CRT structure in case we tried to
7398 * reuse a session but it failed, and allocate a new one. */
7399 ssl_clear_peer_cert( ssl->session_negotiate );
7400
7401 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7402 if( chain == NULL )
7403 {
7404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7405 sizeof( mbedtls_x509_crt ) ) );
7406 mbedtls_ssl_send_alert_message( ssl,
7407 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7408 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7409
7410 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7411 goto exit;
7412 }
7413 mbedtls_x509_crt_init( chain );
7414
7415 ret = ssl_parse_certificate_chain( ssl, chain );
7416 if( ret != 0 )
7417 goto exit;
7418
7419#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7420 if( ssl->handshake->ecrs_enabled)
7421 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7422
7423crt_verify:
7424 if( ssl->handshake->ecrs_enabled)
7425 rs_ctx = &ssl->handshake->ecrs_ctx;
7426#endif
7427
7428 ret = ssl_parse_certificate_verify( ssl, authmode,
7429 chain, rs_ctx );
7430 if( ret != 0 )
7431 goto exit;
7432
7433#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7434 {
7435 unsigned char *crt_start, *pk_start;
7436 size_t crt_len, pk_len;
7437
7438 /* We parse the CRT chain without copying, so
7439 * these pointers point into the input buffer,
7440 * and are hence still valid after freeing the
7441 * CRT chain. */
7442
7443 crt_start = chain->raw.p;
7444 crt_len = chain->raw.len;
7445
7446 pk_start = chain->pk_raw.p;
7447 pk_len = chain->pk_raw.len;
7448
7449 /* Free the CRT structures before computing
7450 * digest and copying the peer's public key. */
7451 mbedtls_x509_crt_free( chain );
7452 mbedtls_free( chain );
7453 chain = NULL;
7454
7455 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7456 if( ret != 0 )
7457 goto exit;
7458
7459 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7460 if( ret != 0 )
7461 goto exit;
7462 }
7463#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7464 /* Pass ownership to session structure. */
7465 ssl->session_negotiate->peer_cert = chain;
7466 chain = NULL;
7467#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7468
7469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7470
7471exit:
7472
7473 if( ret == 0 )
7474 ssl->state++;
7475
7476#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7477 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7478 {
7479 ssl->handshake->ecrs_peer_cert = chain;
7480 chain = NULL;
7481 }
7482#endif
7483
7484 if( chain != NULL )
7485 {
7486 mbedtls_x509_crt_free( chain );
7487 mbedtls_free( chain );
7488 }
7489
7490 return( ret );
7491}
7492#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7493
Andrzej Kurek25f27152022-08-17 16:09:31 -04007494#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007495static void ssl_calc_finished_tls_sha256(
7496 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7497{
7498 int len = 12;
7499 const char *sender;
7500 unsigned char padbuf[32];
7501#if defined(MBEDTLS_USE_PSA_CRYPTO)
7502 size_t hash_size;
7503 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7504 psa_status_t status;
7505#else
7506 mbedtls_sha256_context sha256;
7507#endif
7508
7509 mbedtls_ssl_session *session = ssl->session_negotiate;
7510 if( !session )
7511 session = ssl->session;
7512
7513 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7514 ? "client finished"
7515 : "server finished";
7516
7517#if defined(MBEDTLS_USE_PSA_CRYPTO)
7518 sha256_psa = psa_hash_operation_init();
7519
7520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7521
7522 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7523 if( status != PSA_SUCCESS )
7524 {
7525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7526 return;
7527 }
7528
7529 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7530 if( status != PSA_SUCCESS )
7531 {
7532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7533 return;
7534 }
7535 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7536#else
7537
7538 mbedtls_sha256_init( &sha256 );
7539
7540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7541
7542 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7543
7544 /*
7545 * TLSv1.2:
7546 * hash = PRF( master, finished_label,
7547 * Hash( handshake ) )[0.11]
7548 */
7549
7550#if !defined(MBEDTLS_SHA256_ALT)
7551 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7552 sha256.state, sizeof( sha256.state ) );
7553#endif
7554
7555 mbedtls_sha256_finish( &sha256, padbuf );
7556 mbedtls_sha256_free( &sha256 );
7557#endif /* MBEDTLS_USE_PSA_CRYPTO */
7558
7559 ssl->handshake->tls_prf( session->master, 48, sender,
7560 padbuf, 32, buf, len );
7561
7562 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7563
7564 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7565
7566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7567}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007568#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007569
Jerry Yub7ba49e2022-02-17 14:25:53 +08007570
Andrzej Kurek25f27152022-08-17 16:09:31 -04007571#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007572static void ssl_calc_finished_tls_sha384(
7573 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7574{
7575 int len = 12;
7576 const char *sender;
7577 unsigned char padbuf[48];
7578#if defined(MBEDTLS_USE_PSA_CRYPTO)
7579 size_t hash_size;
7580 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7581 psa_status_t status;
7582#else
7583 mbedtls_sha512_context sha512;
7584#endif
7585
7586 mbedtls_ssl_session *session = ssl->session_negotiate;
7587 if( !session )
7588 session = ssl->session;
7589
7590 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7591 ? "client finished"
7592 : "server finished";
7593
7594#if defined(MBEDTLS_USE_PSA_CRYPTO)
7595 sha384_psa = psa_hash_operation_init();
7596
7597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7598
7599 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7600 if( status != PSA_SUCCESS )
7601 {
7602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7603 return;
7604 }
7605
7606 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7607 if( status != PSA_SUCCESS )
7608 {
7609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7610 return;
7611 }
7612 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7613#else
7614 mbedtls_sha512_init( &sha512 );
7615
7616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7617
Andrzej Kureka242e832022-08-11 10:03:14 -04007618 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007619
7620 /*
7621 * TLSv1.2:
7622 * hash = PRF( master, finished_label,
7623 * Hash( handshake ) )[0.11]
7624 */
7625
7626#if !defined(MBEDTLS_SHA512_ALT)
7627 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7628 sha512.state, sizeof( sha512.state ) );
7629#endif
7630 mbedtls_sha512_finish( &sha512, padbuf );
7631
7632 mbedtls_sha512_free( &sha512 );
7633#endif
7634
7635 ssl->handshake->tls_prf( session->master, 48, sender,
7636 padbuf, 48, buf, len );
7637
7638 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7639
7640 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7641
7642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7643}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007644#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007645
Jerry Yuaef00152022-02-17 14:27:31 +08007646void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7647{
7648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7649
7650 /*
7651 * Free our handshake params
7652 */
7653 mbedtls_ssl_handshake_free( ssl );
7654 mbedtls_free( ssl->handshake );
7655 ssl->handshake = NULL;
7656
7657 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007658 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007659 */
7660 if( ssl->transform )
7661 {
7662 mbedtls_ssl_transform_free( ssl->transform );
7663 mbedtls_free( ssl->transform );
7664 }
7665 ssl->transform = ssl->transform_negotiate;
7666 ssl->transform_negotiate = NULL;
7667
7668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7669}
7670
Jerry Yu2a9fff52022-02-17 14:28:51 +08007671void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7672{
7673 int resume = ssl->handshake->resume;
7674
7675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7676
7677#if defined(MBEDTLS_SSL_RENEGOTIATION)
7678 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7679 {
7680 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7681 ssl->renego_records_seen = 0;
7682 }
7683#endif
7684
7685 /*
7686 * Free the previous session and switch in the current one
7687 */
7688 if( ssl->session )
7689 {
7690#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7691 /* RFC 7366 3.1: keep the EtM state */
7692 ssl->session_negotiate->encrypt_then_mac =
7693 ssl->session->encrypt_then_mac;
7694#endif
7695
7696 mbedtls_ssl_session_free( ssl->session );
7697 mbedtls_free( ssl->session );
7698 }
7699 ssl->session = ssl->session_negotiate;
7700 ssl->session_negotiate = NULL;
7701
7702 /*
7703 * Add cache entry
7704 */
7705 if( ssl->conf->f_set_cache != NULL &&
7706 ssl->session->id_len != 0 &&
7707 resume == 0 )
7708 {
7709 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7710 ssl->session->id,
7711 ssl->session->id_len,
7712 ssl->session ) != 0 )
7713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7714 }
7715
7716#if defined(MBEDTLS_SSL_PROTO_DTLS)
7717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7718 ssl->handshake->flight != NULL )
7719 {
7720 /* Cancel handshake timer */
7721 mbedtls_ssl_set_timer( ssl, 0 );
7722
7723 /* Keep last flight around in case we need to resend it:
7724 * we need the handshake and transform structures for that */
7725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7726 }
7727 else
7728#endif
7729 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7730
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007731 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007732
7733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7734}
7735
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007736int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7737{
7738 int ret, hash_len;
7739
7740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7741
7742 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7743
7744 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7745
7746 /*
7747 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7748 * may define some other value. Currently (early 2016), no defined
7749 * ciphersuite does this (and this is unlikely to change as activity has
7750 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7751 */
7752 hash_len = 12;
7753
7754#if defined(MBEDTLS_SSL_RENEGOTIATION)
7755 ssl->verify_data_len = hash_len;
7756 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7757#endif
7758
7759 ssl->out_msglen = 4 + hash_len;
7760 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7761 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7762
7763 /*
7764 * In case of session resuming, invert the client and server
7765 * ChangeCipherSpec messages order.
7766 */
7767 if( ssl->handshake->resume != 0 )
7768 {
7769#if defined(MBEDTLS_SSL_CLI_C)
7770 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7771 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7772#endif
7773#if defined(MBEDTLS_SSL_SRV_C)
7774 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7775 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7776#endif
7777 }
7778 else
7779 ssl->state++;
7780
7781 /*
7782 * Switch to our negotiated transform and session parameters for outbound
7783 * data.
7784 */
7785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7786
7787#if defined(MBEDTLS_SSL_PROTO_DTLS)
7788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7789 {
7790 unsigned char i;
7791
7792 /* Remember current epoch settings for resending */
7793 ssl->handshake->alt_transform_out = ssl->transform_out;
7794 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7795 sizeof( ssl->handshake->alt_out_ctr ) );
7796
7797 /* Set sequence_number to zero */
7798 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7799
7800
7801 /* Increment epoch */
7802 for( i = 2; i > 0; i-- )
7803 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7804 break;
7805
7806 /* The loop goes to its end iff the counter is wrapping */
7807 if( i == 0 )
7808 {
7809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7810 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7811 }
7812 }
7813 else
7814#endif /* MBEDTLS_SSL_PROTO_DTLS */
7815 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7816
7817 ssl->transform_out = ssl->transform_negotiate;
7818 ssl->session_out = ssl->session_negotiate;
7819
7820#if defined(MBEDTLS_SSL_PROTO_DTLS)
7821 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7822 mbedtls_ssl_send_flight_completed( ssl );
7823#endif
7824
7825 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7826 {
7827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7828 return( ret );
7829 }
7830
7831#if defined(MBEDTLS_SSL_PROTO_DTLS)
7832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7833 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7834 {
7835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7836 return( ret );
7837 }
7838#endif
7839
7840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7841
7842 return( 0 );
7843}
7844
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007845#define SSL_MAX_HASH_LEN 12
7846
7847int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7848{
7849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7850 unsigned int hash_len = 12;
7851 unsigned char buf[SSL_MAX_HASH_LEN];
7852
7853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7854
7855 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7856
7857 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7858 {
7859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7860 goto exit;
7861 }
7862
7863 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7864 {
7865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7866 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7867 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7868 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7869 goto exit;
7870 }
7871
7872 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7873 {
7874 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7875 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7876 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7877 goto exit;
7878 }
7879
7880 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7881 {
7882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7883 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7884 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7885 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7886 goto exit;
7887 }
7888
7889 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7890 buf, hash_len ) != 0 )
7891 {
7892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7894 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7895 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7896 goto exit;
7897 }
7898
7899#if defined(MBEDTLS_SSL_RENEGOTIATION)
7900 ssl->verify_data_len = hash_len;
7901 memcpy( ssl->peer_verify_data, buf, hash_len );
7902#endif
7903
7904 if( ssl->handshake->resume != 0 )
7905 {
7906#if defined(MBEDTLS_SSL_CLI_C)
7907 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7908 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7909#endif
7910#if defined(MBEDTLS_SSL_SRV_C)
7911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7912 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7913#endif
7914 }
7915 else
7916 ssl->state++;
7917
7918#if defined(MBEDTLS_SSL_PROTO_DTLS)
7919 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7920 mbedtls_ssl_recv_flight_completed( ssl );
7921#endif
7922
7923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7924
7925exit:
7926 mbedtls_platform_zeroize( buf, hash_len );
7927 return( ret );
7928}
7929
Jerry Yu392112c2022-02-17 14:34:10 +08007930#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7931/*
7932 * Helper to get TLS 1.2 PRF from ciphersuite
7933 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7934 */
7935static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7936{
Jerry Yu392112c2022-02-17 14:34:10 +08007937 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007938 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7939#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007940 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007941 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007942 else
Jerry Yu392112c2022-02-17 14:34:10 +08007943#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007944#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7945 {
7946 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7947 return( tls_prf_sha256 );
7948 }
7949#endif
7950#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7951 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7952 (void) ciphersuite_info;
7953#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007954
Andrzej Kurek894edde2022-09-29 06:31:14 -04007955 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007956}
7957#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007958
7959static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7960{
7961 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007962#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007963 if( tls_prf == tls_prf_sha384 )
7964 {
7965 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7966 }
7967 else
7968#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007969#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007970 if( tls_prf == tls_prf_sha256 )
7971 {
7972 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7973 }
7974 else
7975#endif
7976 return( MBEDTLS_SSL_TLS_PRF_NONE );
7977}
7978
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007979/*
7980 * Populate a transform structure with session keys and all the other
7981 * necessary information.
7982 *
7983 * Parameters:
7984 * - [in/out]: transform: structure to populate
7985 * [in] must be just initialised with mbedtls_ssl_transform_init()
7986 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7987 * - [in] ciphersuite
7988 * - [in] master
7989 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007990 * - [in] tls_prf: pointer to PRF to use for key derivation
7991 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007992 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007993 * - [in] endpoint: client or server
7994 * - [in] ssl: used for:
7995 * - ssl->conf->{f,p}_export_keys
7996 * [in] optionally used for:
7997 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7998 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007999MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008000static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
8001 int ciphersuite,
8002 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008003#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008004 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008005#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008006 ssl_tls_prf_t tls_prf,
8007 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04008008 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008009 unsigned endpoint,
8010 const mbedtls_ssl_context *ssl )
8011{
8012 int ret = 0;
8013 unsigned char keyblk[256];
8014 unsigned char *key1;
8015 unsigned char *key2;
8016 unsigned char *mac_enc;
8017 unsigned char *mac_dec;
8018 size_t mac_key_len = 0;
8019 size_t iv_copy_len;
8020 size_t keylen;
8021 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008022 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008023#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008024 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008025 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008026#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008027
8028#if defined(MBEDTLS_USE_PSA_CRYPTO)
8029 psa_key_type_t key_type;
8030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8031 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008032 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008033 size_t key_bits;
8034 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8035#endif
8036
8037#if !defined(MBEDTLS_DEBUG_C) && \
8038 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8039 if( ssl->f_export_keys == NULL )
8040 {
8041 ssl = NULL; /* make sure we don't use it except for these cases */
8042 (void) ssl;
8043 }
8044#endif
8045
8046 /*
8047 * Some data just needs copying into the structure
8048 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008049#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008050 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008051#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008052 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008053
8054#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8055 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8056#endif
8057
8058#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008059 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008060 {
8061 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8062 * generation separate. This should never happen. */
8063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8064 }
8065#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8066
8067 /*
8068 * Get various info structures
8069 */
8070 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8071 if( ciphersuite_info == NULL )
8072 {
8073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8074 ciphersuite ) );
8075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8076 }
8077
Neil Armstrongab555e02022-04-04 11:07:59 +02008078 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008079#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008080 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008081#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008082 ciphersuite_info );
8083
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008084 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8085 transform->taglen =
8086 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8087
8088#if defined(MBEDTLS_USE_PSA_CRYPTO)
8089 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8090 transform->taglen,
8091 &alg,
8092 &key_type,
8093 &key_bits ) ) != PSA_SUCCESS )
8094 {
8095 ret = psa_ssl_status_to_mbedtls( status );
8096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8097 goto end;
8098 }
8099#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008100 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8101 if( cipher_info == NULL )
8102 {
8103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8104 ciphersuite_info->cipher ) );
8105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8106 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008107#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008108
Neil Armstronge4512952022-03-08 09:08:22 +01008109#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008110 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008111 if( mac_alg == 0 )
8112 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008114 (unsigned) ciphersuite_info->mac ) );
8115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8116 }
8117#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008118 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8119 if( md_info == NULL )
8120 {
8121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8122 (unsigned) ciphersuite_info->mac ) );
8123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8124 }
Neil Armstronge4512952022-03-08 09:08:22 +01008125#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008126
8127#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8128 /* Copy own and peer's CID if the use of the CID
8129 * extension has been negotiated. */
8130 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8131 {
8132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8133
8134 transform->in_cid_len = ssl->own_cid_len;
8135 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8136 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8137 transform->in_cid_len );
8138
8139 transform->out_cid_len = ssl->handshake->peer_cid_len;
8140 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8141 ssl->handshake->peer_cid_len );
8142 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8143 transform->out_cid_len );
8144 }
8145#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8146
8147 /*
8148 * Compute key block using the PRF
8149 */
8150 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8151 if( ret != 0 )
8152 {
8153 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8154 return( ret );
8155 }
8156
8157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8158 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8159 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8160 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8161 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8162
8163 /*
8164 * Determine the appropriate key, IV and MAC length.
8165 */
8166
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008167#if defined(MBEDTLS_USE_PSA_CRYPTO)
8168 keylen = PSA_BITS_TO_BYTES(key_bits);
8169#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008170 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008171#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008172
8173#if defined(MBEDTLS_GCM_C) || \
8174 defined(MBEDTLS_CCM_C) || \
8175 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008176 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008177 {
8178 size_t explicit_ivlen;
8179
8180 transform->maclen = 0;
8181 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008182
8183 /* All modes haves 96-bit IVs, but the length of the static parts vary
8184 * with mode and version:
8185 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8186 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8187 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8188 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8189 * sequence number).
8190 */
8191 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008192
8193 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008194#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008195 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008196#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008197 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8198 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008199#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008200
8201 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008202 transform->fixed_ivlen = 12;
8203 else
8204 transform->fixed_ivlen = 4;
8205
8206 /* Minimum length of encrypted record */
8207 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8208 transform->minlen = explicit_ivlen + transform->taglen;
8209 }
8210 else
8211#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8212#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008213 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8214 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8215 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008216 {
Neil Armstronge4512952022-03-08 09:08:22 +01008217#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008218 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008219#else
8220 size_t block_size = cipher_info->block_size;
8221#endif /* MBEDTLS_USE_PSA_CRYPTO */
8222
8223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008224 /* Get MAC length */
8225 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8226#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008227 /* Initialize HMAC contexts */
8228 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8229 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8230 {
8231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8232 goto end;
8233 }
8234
8235 /* Get MAC length */
8236 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008237#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238 transform->maclen = mac_key_len;
8239
8240 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008241#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008242 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008243#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008244 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008245#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008246
8247 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008248 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008249 transform->minlen = transform->maclen;
8250 else
8251 {
8252 /*
8253 * GenericBlockCipher:
8254 * 1. if EtM is in use: one block plus MAC
8255 * otherwise: * first multiple of blocklen greater than maclen
8256 * 2. IV
8257 */
8258#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008259 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008260 {
8261 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008262 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008263 }
8264 else
8265#endif
8266 {
8267 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008268 + block_size
8269 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008270 }
8271
Glenn Strauss07c64162022-03-14 12:34:51 -04008272 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008273 {
8274 transform->minlen += transform->ivlen;
8275 }
8276 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008277 {
8278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8279 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8280 goto end;
8281 }
8282 }
8283 }
8284 else
8285#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8286 {
8287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8289 }
8290
8291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8292 (unsigned) keylen,
8293 (unsigned) transform->minlen,
8294 (unsigned) transform->ivlen,
8295 (unsigned) transform->maclen ) );
8296
8297 /*
8298 * Finally setup the cipher contexts, IVs and MAC secrets.
8299 */
8300#if defined(MBEDTLS_SSL_CLI_C)
8301 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8302 {
8303 key1 = keyblk + mac_key_len * 2;
8304 key2 = keyblk + mac_key_len * 2 + keylen;
8305
8306 mac_enc = keyblk;
8307 mac_dec = keyblk + mac_key_len;
8308
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008309 iv_copy_len = ( transform->fixed_ivlen ) ?
8310 transform->fixed_ivlen : transform->ivlen;
8311 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8312 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8313 iv_copy_len );
8314 }
8315 else
8316#endif /* MBEDTLS_SSL_CLI_C */
8317#if defined(MBEDTLS_SSL_SRV_C)
8318 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8319 {
8320 key1 = keyblk + mac_key_len * 2 + keylen;
8321 key2 = keyblk + mac_key_len * 2;
8322
8323 mac_enc = keyblk + mac_key_len;
8324 mac_dec = keyblk;
8325
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008326 iv_copy_len = ( transform->fixed_ivlen ) ?
8327 transform->fixed_ivlen : transform->ivlen;
8328 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8329 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8330 iv_copy_len );
8331 }
8332 else
8333#endif /* MBEDTLS_SSL_SRV_C */
8334 {
8335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8336 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8337 goto end;
8338 }
8339
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340 if( ssl != NULL && ssl->f_export_keys != NULL )
8341 {
8342 ssl->f_export_keys( ssl->p_export_keys,
8343 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8344 master, 48,
8345 randbytes + 32,
8346 randbytes,
8347 tls_prf_get_type( tls_prf ) );
8348 }
8349
8350#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008351 transform->psa_alg = alg;
8352
8353 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8354 {
8355 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8356 psa_set_key_algorithm( &attributes, alg );
8357 psa_set_key_type( &attributes, key_type );
8358
8359 if( ( status = psa_import_key( &attributes,
8360 key1,
8361 PSA_BITS_TO_BYTES( key_bits ),
8362 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8363 {
8364 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8365 ret = psa_ssl_status_to_mbedtls( status );
8366 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8367 goto end;
8368 }
8369
8370 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8371
8372 if( ( status = psa_import_key( &attributes,
8373 key2,
8374 PSA_BITS_TO_BYTES( key_bits ),
8375 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8376 {
8377 ret = psa_ssl_status_to_mbedtls( status );
8378 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8379 goto end;
8380 }
8381 }
8382#else
8383 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8384 cipher_info ) ) != 0 )
8385 {
8386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8387 goto end;
8388 }
8389
8390 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8391 cipher_info ) ) != 0 )
8392 {
8393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8394 goto end;
8395 }
8396
8397 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8398 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8399 MBEDTLS_ENCRYPT ) ) != 0 )
8400 {
8401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8402 goto end;
8403 }
8404
8405 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8406 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8407 MBEDTLS_DECRYPT ) ) != 0 )
8408 {
8409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8410 goto end;
8411 }
8412
8413#if defined(MBEDTLS_CIPHER_MODE_CBC)
8414 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8415 {
8416 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8417 MBEDTLS_PADDING_NONE ) ) != 0 )
8418 {
8419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8420 goto end;
8421 }
8422
8423 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8424 MBEDTLS_PADDING_NONE ) ) != 0 )
8425 {
8426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8427 goto end;
8428 }
8429 }
8430#endif /* MBEDTLS_CIPHER_MODE_CBC */
8431#endif /* MBEDTLS_USE_PSA_CRYPTO */
8432
Neil Armstrong29c0c042022-03-17 17:47:28 +01008433#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8434 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8435 For AEAD-based ciphersuites, there is nothing to do here. */
8436 if( mac_key_len != 0 )
8437 {
8438#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008439 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008440
8441 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008442 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008443 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8444
8445 if( ( status = psa_import_key( &attributes,
8446 mac_enc, mac_key_len,
8447 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8448 {
8449 ret = psa_ssl_status_to_mbedtls( status );
8450 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8451 goto end;
8452 }
8453
Ronald Cronfb39f152022-03-25 14:36:28 +01008454 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008455 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8456#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8457 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8458#endif
8459 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008460 /* mbedtls_ct_hmac() requires the key to be exportable */
8461 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8462 PSA_KEY_USAGE_VERIFY_HASH );
8463 else
8464 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8465
8466 if( ( status = psa_import_key( &attributes,
8467 mac_dec, mac_key_len,
8468 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8469 {
8470 ret = psa_ssl_status_to_mbedtls( status );
8471 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8472 goto end;
8473 }
8474#else
8475 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8476 if( ret != 0 )
8477 goto end;
8478 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8479 if( ret != 0 )
8480 goto end;
8481#endif /* MBEDTLS_USE_PSA_CRYPTO */
8482 }
8483#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8484
8485 ((void) mac_dec);
8486 ((void) mac_enc);
8487
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008488end:
8489 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8490 return( ret );
8491}
8492
Valerio Settia08b1a42022-11-17 15:10:02 +01008493#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8494 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008495int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008496 psa_pake_operation_t *pake_ctx,
8497 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008498 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008499{
8500 psa_status_t status;
8501 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008502 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008503 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8504 * At round two perform a single cycle
8505 */
8506 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008507
Valerio Setti6b3dab02022-11-17 17:14:54 +01008508 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008509 {
8510 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8511 step <= PSA_PAKE_STEP_ZK_PROOF;
8512 ++step )
8513 {
8514 /* Length is stored at the first byte */
8515 size_t length = buf[input_offset];
8516 input_offset += 1;
8517
8518 if( input_offset + length > len )
8519 {
8520 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8521 }
8522
8523 status = psa_pake_input( pake_ctx, step,
8524 buf + input_offset, length );
8525 if( status != PSA_SUCCESS)
8526 {
8527 return psa_ssl_status_to_mbedtls( status );
8528 }
8529
8530 input_offset += length;
8531 }
8532 }
8533
Valerio Setti819de862022-11-17 18:05:19 +01008534 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008535 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008536
Valerio Settia08b1a42022-11-17 15:10:02 +01008537 return( 0 );
8538}
8539
Valerio Setti6b3dab02022-11-17 17:14:54 +01008540int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008541 psa_pake_operation_t *pake_ctx,
8542 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008543 size_t len, size_t *olen,
8544 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008545{
8546 psa_status_t status;
8547 size_t output_offset = 0;
8548 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008549 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008550 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8551 * At round two perform a single cycle
8552 */
8553 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008554
Valerio Setti6b3dab02022-11-17 17:14:54 +01008555 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008556 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008557 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8558 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008559 ++step )
8560 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008561 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008562 * For each step, prepend 1 byte with the length of the data as
8563 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008564 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008565 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008566 buf + output_offset + 1,
8567 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008568 &output_len );
8569 if( status != PSA_SUCCESS )
8570 {
8571 return( psa_ssl_status_to_mbedtls( status ) );
8572 }
8573
Valerio Setti99d88c12022-11-22 16:03:43 +01008574 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008575
8576 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008577 }
8578 }
8579
8580 *olen = output_offset;
8581
8582 return( 0 );
8583}
Valerio Settia08b1a42022-11-17 15:10:02 +01008584#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8585
Jerry Yuee40f9d2022-02-17 14:55:16 +08008586#if defined(MBEDTLS_USE_PSA_CRYPTO)
8587int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8588 unsigned char *hash, size_t *hashlen,
8589 unsigned char *data, size_t data_len,
8590 mbedtls_md_type_t md_alg )
8591{
8592 psa_status_t status;
8593 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008594 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008595
8596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8597
8598 if( ( status = psa_hash_setup( &hash_operation,
8599 hash_alg ) ) != PSA_SUCCESS )
8600 {
8601 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8602 goto exit;
8603 }
8604
8605 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8606 64 ) ) != PSA_SUCCESS )
8607 {
8608 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8609 goto exit;
8610 }
8611
8612 if( ( status = psa_hash_update( &hash_operation,
8613 data, data_len ) ) != PSA_SUCCESS )
8614 {
8615 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8616 goto exit;
8617 }
8618
8619 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8620 hashlen ) ) != PSA_SUCCESS )
8621 {
8622 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8623 goto exit;
8624 }
8625
8626exit:
8627 if( status != PSA_SUCCESS )
8628 {
8629 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8630 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8631 switch( status )
8632 {
8633 case PSA_ERROR_NOT_SUPPORTED:
8634 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8635 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8636 case PSA_ERROR_BUFFER_TOO_SMALL:
8637 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8638 case PSA_ERROR_INSUFFICIENT_MEMORY:
8639 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8640 default:
8641 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8642 }
8643 }
8644 return( 0 );
8645}
8646
8647#else
8648
8649int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8650 unsigned char *hash, size_t *hashlen,
8651 unsigned char *data, size_t data_len,
8652 mbedtls_md_type_t md_alg )
8653{
8654 int ret = 0;
8655 mbedtls_md_context_t ctx;
8656 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8657 *hashlen = mbedtls_md_get_size( md_info );
8658
8659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8660
8661 mbedtls_md_init( &ctx );
8662
8663 /*
8664 * digitally-signed struct {
8665 * opaque client_random[32];
8666 * opaque server_random[32];
8667 * ServerDHParams params;
8668 * };
8669 */
8670 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8671 {
8672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8673 goto exit;
8674 }
8675 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8676 {
8677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8678 goto exit;
8679 }
8680 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8681 {
8682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8683 goto exit;
8684 }
8685 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8686 {
8687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8688 goto exit;
8689 }
8690 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8691 {
8692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8693 goto exit;
8694 }
8695
8696exit:
8697 mbedtls_md_free( &ctx );
8698
8699 if( ret != 0 )
8700 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8701 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8702
8703 return( ret );
8704}
8705#endif /* MBEDTLS_USE_PSA_CRYPTO */
8706
Jerry Yud9d91da2022-02-17 14:57:06 +08008707#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8708
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008709/* Find the preferred hash for a given signature algorithm. */
8710unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8711 mbedtls_ssl_context *ssl,
8712 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008713{
Gabor Mezei078e8032022-04-27 21:17:56 +02008714 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008715 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008716
8717 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008718 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008719
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008720 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008721 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008722 unsigned int hash_alg_received =
8723 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8724 received_sig_algs[i] );
8725 unsigned int sig_alg_received =
8726 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8727 received_sig_algs[i] );
8728
8729 if( sig_alg == sig_alg_received )
8730 {
8731#if defined(MBEDTLS_USE_PSA_CRYPTO)
8732 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8733 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008734 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008735 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008736
Neil Armstrong96eceb82022-06-30 18:05:05 +02008737 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8738 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8739 PSA_ALG_ECDSA( psa_hash_alg ),
8740 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008741 continue;
8742
Neil Armstrong96eceb82022-06-30 18:05:05 +02008743 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008744 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8745 PSA_ALG_RSA_PKCS1V15_SIGN(
8746 psa_hash_alg ),
8747 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008748 continue;
8749 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008750#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008751
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008752 return( hash_alg_received );
8753 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008754 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008755
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008756 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008757}
8758
8759#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8760
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008761/* Serialization of TLS 1.2 sessions:
8762 *
8763 * struct {
8764 * uint64 start_time;
8765 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008766 * uint8 session_id_len; // at most 32
8767 * opaque session_id[32];
8768 * opaque master[48]; // fixed length in the standard
8769 * uint32 verify_result;
8770 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8771 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8772 * uint32 ticket_lifetime;
8773 * uint8 mfl_code; // up to 255 according to standard
8774 * uint8 encrypt_then_mac; // 0 or 1
8775 * } serialized_session_tls12;
8776 *
8777 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008778static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008779 unsigned char *buf,
8780 size_t buf_len )
8781{
8782 unsigned char *p = buf;
8783 size_t used = 0;
8784
8785#if defined(MBEDTLS_HAVE_TIME)
8786 uint64_t start;
8787#endif
8788#if defined(MBEDTLS_X509_CRT_PARSE_C)
8789#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8790 size_t cert_len;
8791#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8792#endif /* MBEDTLS_X509_CRT_PARSE_C */
8793
8794 /*
8795 * Time
8796 */
8797#if defined(MBEDTLS_HAVE_TIME)
8798 used += 8;
8799
8800 if( used <= buf_len )
8801 {
8802 start = (uint64_t) session->start;
8803
8804 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8805 p += 8;
8806 }
8807#endif /* MBEDTLS_HAVE_TIME */
8808
8809 /*
8810 * Basic mandatory fields
8811 */
8812 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008813 + 1 /* id_len */
8814 + sizeof( session->id )
8815 + sizeof( session->master )
8816 + 4; /* verify_result */
8817
8818 if( used <= buf_len )
8819 {
8820 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8821 p += 2;
8822
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008823 *p++ = MBEDTLS_BYTE_0( session->id_len );
8824 memcpy( p, session->id, 32 );
8825 p += 32;
8826
8827 memcpy( p, session->master, 48 );
8828 p += 48;
8829
8830 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8831 p += 4;
8832 }
8833
8834 /*
8835 * Peer's end-entity certificate
8836 */
8837#if defined(MBEDTLS_X509_CRT_PARSE_C)
8838#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8839 if( session->peer_cert == NULL )
8840 cert_len = 0;
8841 else
8842 cert_len = session->peer_cert->raw.len;
8843
8844 used += 3 + cert_len;
8845
8846 if( used <= buf_len )
8847 {
8848 *p++ = MBEDTLS_BYTE_2( cert_len );
8849 *p++ = MBEDTLS_BYTE_1( cert_len );
8850 *p++ = MBEDTLS_BYTE_0( cert_len );
8851
8852 if( session->peer_cert != NULL )
8853 {
8854 memcpy( p, session->peer_cert->raw.p, cert_len );
8855 p += cert_len;
8856 }
8857 }
8858#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8859 if( session->peer_cert_digest != NULL )
8860 {
8861 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8862 if( used <= buf_len )
8863 {
8864 *p++ = (unsigned char) session->peer_cert_digest_type;
8865 *p++ = (unsigned char) session->peer_cert_digest_len;
8866 memcpy( p, session->peer_cert_digest,
8867 session->peer_cert_digest_len );
8868 p += session->peer_cert_digest_len;
8869 }
8870 }
8871 else
8872 {
8873 used += 2;
8874 if( used <= buf_len )
8875 {
8876 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8877 *p++ = 0;
8878 }
8879 }
8880#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8881#endif /* MBEDTLS_X509_CRT_PARSE_C */
8882
8883 /*
8884 * Session ticket if any, plus associated data
8885 */
8886#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8887 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8888
8889 if( used <= buf_len )
8890 {
8891 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8892 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8893 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8894
8895 if( session->ticket != NULL )
8896 {
8897 memcpy( p, session->ticket, session->ticket_len );
8898 p += session->ticket_len;
8899 }
8900
8901 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8902 p += 4;
8903 }
8904#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8905
8906 /*
8907 * Misc extension-related info
8908 */
8909#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8910 used += 1;
8911
8912 if( used <= buf_len )
8913 *p++ = session->mfl_code;
8914#endif
8915
8916#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8917 used += 1;
8918
8919 if( used <= buf_len )
8920 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8921#endif
8922
8923 return( used );
8924}
8925
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008926MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008927static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008928 const unsigned char *buf,
8929 size_t len )
8930{
8931#if defined(MBEDTLS_HAVE_TIME)
8932 uint64_t start;
8933#endif
8934#if defined(MBEDTLS_X509_CRT_PARSE_C)
8935#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8936 size_t cert_len;
8937#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8938#endif /* MBEDTLS_X509_CRT_PARSE_C */
8939
8940 const unsigned char *p = buf;
8941 const unsigned char * const end = buf + len;
8942
8943 /*
8944 * Time
8945 */
8946#if defined(MBEDTLS_HAVE_TIME)
8947 if( 8 > (size_t)( end - p ) )
8948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8949
8950 start = ( (uint64_t) p[0] << 56 ) |
8951 ( (uint64_t) p[1] << 48 ) |
8952 ( (uint64_t) p[2] << 40 ) |
8953 ( (uint64_t) p[3] << 32 ) |
8954 ( (uint64_t) p[4] << 24 ) |
8955 ( (uint64_t) p[5] << 16 ) |
8956 ( (uint64_t) p[6] << 8 ) |
8957 ( (uint64_t) p[7] );
8958 p += 8;
8959
8960 session->start = (time_t) start;
8961#endif /* MBEDTLS_HAVE_TIME */
8962
8963 /*
8964 * Basic mandatory fields
8965 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008966 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8968
8969 session->ciphersuite = ( p[0] << 8 ) | p[1];
8970 p += 2;
8971
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008972 session->id_len = *p++;
8973 memcpy( session->id, p, 32 );
8974 p += 32;
8975
8976 memcpy( session->master, p, 48 );
8977 p += 48;
8978
8979 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8980 ( (uint32_t) p[1] << 16 ) |
8981 ( (uint32_t) p[2] << 8 ) |
8982 ( (uint32_t) p[3] );
8983 p += 4;
8984
8985 /* Immediately clear invalid pointer values that have been read, in case
8986 * we exit early before we replaced them with valid ones. */
8987#if defined(MBEDTLS_X509_CRT_PARSE_C)
8988#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8989 session->peer_cert = NULL;
8990#else
8991 session->peer_cert_digest = NULL;
8992#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8993#endif /* MBEDTLS_X509_CRT_PARSE_C */
8994#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8995 session->ticket = NULL;
8996#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8997
8998 /*
8999 * Peer certificate
9000 */
9001#if defined(MBEDTLS_X509_CRT_PARSE_C)
9002#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9003 /* Deserialize CRT from the end of the ticket. */
9004 if( 3 > (size_t)( end - p ) )
9005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9006
9007 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9008 p += 3;
9009
9010 if( cert_len != 0 )
9011 {
9012 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9013
9014 if( cert_len > (size_t)( end - p ) )
9015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9016
9017 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9018
9019 if( session->peer_cert == NULL )
9020 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9021
9022 mbedtls_x509_crt_init( session->peer_cert );
9023
9024 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9025 p, cert_len ) ) != 0 )
9026 {
9027 mbedtls_x509_crt_free( session->peer_cert );
9028 mbedtls_free( session->peer_cert );
9029 session->peer_cert = NULL;
9030 return( ret );
9031 }
9032
9033 p += cert_len;
9034 }
9035#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9036 /* Deserialize CRT digest from the end of the ticket. */
9037 if( 2 > (size_t)( end - p ) )
9038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9039
9040 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9041 session->peer_cert_digest_len = (size_t) *p++;
9042
9043 if( session->peer_cert_digest_len != 0 )
9044 {
9045 const mbedtls_md_info_t *md_info =
9046 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9047 if( md_info == NULL )
9048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9049 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9051
9052 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9054
9055 session->peer_cert_digest =
9056 mbedtls_calloc( 1, session->peer_cert_digest_len );
9057 if( session->peer_cert_digest == NULL )
9058 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9059
9060 memcpy( session->peer_cert_digest, p,
9061 session->peer_cert_digest_len );
9062 p += session->peer_cert_digest_len;
9063 }
9064#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9065#endif /* MBEDTLS_X509_CRT_PARSE_C */
9066
9067 /*
9068 * Session ticket and associated data
9069 */
9070#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9071 if( 3 > (size_t)( end - p ) )
9072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9073
9074 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9075 p += 3;
9076
9077 if( session->ticket_len != 0 )
9078 {
9079 if( session->ticket_len > (size_t)( end - p ) )
9080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9081
9082 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9083 if( session->ticket == NULL )
9084 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9085
9086 memcpy( session->ticket, p, session->ticket_len );
9087 p += session->ticket_len;
9088 }
9089
9090 if( 4 > (size_t)( end - p ) )
9091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9092
9093 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9094 ( (uint32_t) p[1] << 16 ) |
9095 ( (uint32_t) p[2] << 8 ) |
9096 ( (uint32_t) p[3] );
9097 p += 4;
9098#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9099
9100 /*
9101 * Misc extension-related info
9102 */
9103#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9104 if( 1 > (size_t)( end - p ) )
9105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9106
9107 session->mfl_code = *p++;
9108#endif
9109
9110#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9111 if( 1 > (size_t)( end - p ) )
9112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9113
9114 session->encrypt_then_mac = *p++;
9115#endif
9116
9117 /* Done, should have consumed entire buffer */
9118 if( p != end )
9119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9120
9121 return( 0 );
9122}
Jerry Yudc7bd172022-02-17 13:44:15 +08009123#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9124
XiaokangQian75d40ef2022-04-20 11:05:24 +00009125int mbedtls_ssl_validate_ciphersuite(
9126 const mbedtls_ssl_context *ssl,
9127 const mbedtls_ssl_ciphersuite_t *suite_info,
9128 mbedtls_ssl_protocol_version min_tls_version,
9129 mbedtls_ssl_protocol_version max_tls_version )
9130{
9131 (void) ssl;
9132
9133 if( suite_info == NULL )
9134 return( -1 );
9135
9136 if( ( suite_info->min_tls_version > max_tls_version ) ||
9137 ( suite_info->max_tls_version < min_tls_version ) )
9138 {
9139 return( -1 );
9140 }
9141
XiaokangQian060d8672022-04-21 09:24:56 +00009142#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009143#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009144#if defined(MBEDTLS_USE_PSA_CRYPTO)
9145 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9146 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9147#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009148 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9149 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009150#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009151 {
9152 return( -1 );
9153 }
9154#endif
9155
9156 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9157#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9158 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9159 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9160 {
9161 return( -1 );
9162 }
9163#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9165
9166 return( 0 );
9167}
9168
Ronald Crone68ab4f2022-10-05 12:46:29 +02009169#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009170/*
9171 * Function for writing a signature algorithm extension.
9172 *
9173 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9174 * value (TLS 1.3 RFC8446):
9175 * enum {
9176 * ....
9177 * ecdsa_secp256r1_sha256( 0x0403 ),
9178 * ecdsa_secp384r1_sha384( 0x0503 ),
9179 * ecdsa_secp521r1_sha512( 0x0603 ),
9180 * ....
9181 * } SignatureScheme;
9182 *
9183 * struct {
9184 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9185 * } SignatureSchemeList;
9186 *
9187 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9188 * value (TLS 1.2 RFC5246):
9189 * enum {
9190 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9191 * sha512(6), (255)
9192 * } HashAlgorithm;
9193 *
9194 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9195 * SignatureAlgorithm;
9196 *
9197 * struct {
9198 * HashAlgorithm hash;
9199 * SignatureAlgorithm signature;
9200 * } SignatureAndHashAlgorithm;
9201 *
9202 * SignatureAndHashAlgorithm
9203 * supported_signature_algorithms<2..2^16-2>;
9204 *
9205 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9206 * generalization of the TLS 1.2 signature algorithm extension.
9207 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9208 * `SignatureScheme` field of TLS 1.3
9209 *
9210 */
9211int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9212 const unsigned char *end, size_t *out_len )
9213{
9214 unsigned char *p = buf;
9215 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9216 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9217
9218 *out_len = 0;
9219
9220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9221
9222 /* Check if we have space for header and length field:
9223 * - extension_type (2 bytes)
9224 * - extension_data_length (2 bytes)
9225 * - supported_signature_algorithms_length (2 bytes)
9226 */
9227 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9228 p += 6;
9229
9230 /*
9231 * Write supported_signature_algorithms
9232 */
9233 supported_sig_alg = p;
9234 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9235 if( sig_alg == NULL )
9236 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9237
9238 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9239 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9241 *sig_alg,
9242 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009243 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9244 continue;
9245 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9246 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9247 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009249 *sig_alg,
9250 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009251 }
9252
9253 /* Length of supported_signature_algorithms */
9254 supported_sig_alg_len = p - supported_sig_alg;
9255 if( supported_sig_alg_len == 0 )
9256 {
9257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9258 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9259 }
9260
XiaokangQianeaf36512022-04-24 09:07:44 +00009261 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009262 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009263 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9264
XiaokangQianeaf36512022-04-24 09:07:44 +00009265 *out_len = p - buf;
9266
9267#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009268 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009269#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009270
XiaokangQianeaf36512022-04-24 09:07:44 +00009271 return( 0 );
9272}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009273#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009274
XiaokangQian40a35232022-05-07 09:02:40 +00009275#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009276/*
9277 * mbedtls_ssl_parse_server_name_ext
9278 *
9279 * Structure of server_name extension:
9280 *
9281 * enum {
9282 * host_name(0), (255)
9283 * } NameType;
9284 * opaque HostName<1..2^16-1>;
9285 *
9286 * struct {
9287 * NameType name_type;
9288 * select (name_type) {
9289 * case host_name: HostName;
9290 * } name;
9291 * } ServerName;
9292 * struct {
9293 * ServerName server_name_list<1..2^16-1>
9294 * } ServerNameList;
9295 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009296MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009297int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9298 const unsigned char *buf,
9299 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009300{
9301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9302 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009303 size_t server_name_list_len, hostname_len;
9304 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009305
XiaokangQianf2a94202022-05-20 06:44:24 +00009306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009307
9308 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009309 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009310 p += 2;
9311
XiaokangQian9b2b7712022-05-17 02:57:00 +00009312 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9313 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009314 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009315 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009316 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009317 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009318 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9319 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009320
9321 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9322 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009323 /* sni_name is intended to be used only during the parsing of the
9324 * ClientHello message (it is reset to NULL before the end of
9325 * the message parsing). Thus it is ok to just point to the
9326 * reception buffer and not make a copy of it.
9327 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009328 ssl->handshake->sni_name = p + 3;
9329 ssl->handshake->sni_name_len = hostname_len;
9330 if( ssl->conf->f_sni == NULL )
9331 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009332 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009333 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009334 if( ret != 0 )
9335 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009337 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9338 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009339 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9340 }
9341 return( 0 );
9342 }
9343
9344 p += hostname_len + 3;
9345 }
9346
9347 return( 0 );
9348}
9349#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9350
XiaokangQianacb39922022-06-17 10:18:48 +00009351#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009352MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009353int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9354 const unsigned char *buf,
9355 const unsigned char *end )
9356{
9357 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009358 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009359 const unsigned char *protocol_name_list;
9360 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009361 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009362
9363 /* If ALPN not configured, just ignore the extension */
9364 if( ssl->conf->alpn_list == NULL )
9365 return( 0 );
9366
9367 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009368 * RFC7301, section 3.1
9369 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009370 *
XiaokangQianc7403452022-06-23 03:24:12 +00009371 * struct {
9372 * ProtocolName protocol_name_list<2..2^16-1>
9373 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009374 */
9375
XiaokangQianc7403452022-06-23 03:24:12 +00009376 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009377 * protocol_name_list_len 2 bytes
9378 * protocol_name_len 1 bytes
9379 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009380 */
XiaokangQianacb39922022-06-17 10:18:48 +00009381 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9382
XiaokangQianc7403452022-06-23 03:24:12 +00009383 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009384 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009386 protocol_name_list = p;
9387 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009388
9389 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009390 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009391 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009392 protocol_name_len = *p++;
9393 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9394 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009395 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009396 {
9397 MBEDTLS_SSL_PEND_FATAL_ALERT(
9398 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9399 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009400 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009401 }
9402
9403 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009404 }
9405
9406 /* Use our order of preference */
9407 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9408 {
9409 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009410 p = protocol_name_list;
9411 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009412 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009413 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009414 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009415 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009416 {
9417 ssl->alpn_chosen = *alpn;
9418 return( 0 );
9419 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009420
9421 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009422 }
9423 }
9424
XiaokangQian95d5f542022-06-24 02:29:26 +00009425 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009426 MBEDTLS_SSL_PEND_FATAL_ALERT(
9427 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9428 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9429 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9430}
9431
9432int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9433 unsigned char *buf,
9434 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009435 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009436{
9437 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009438 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009439 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009440
9441 if( ssl->alpn_chosen == NULL )
9442 {
9443 return( 0 );
9444 }
9445
XiaokangQian95d5f542022-06-24 02:29:26 +00009446 protocol_name_len = strlen( ssl->alpn_chosen );
9447 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009448
9449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9450 /*
9451 * 0 . 1 ext identifier
9452 * 2 . 3 ext length
9453 * 4 . 5 protocol list length
9454 * 6 . 6 protocol name length
9455 * 7 . 7+n protocol name
9456 */
9457 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9458
XiaokangQian95d5f542022-06-24 02:29:26 +00009459 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009460
XiaokangQian95d5f542022-06-24 02:29:26 +00009461 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9462 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009463 /* Note: the length of the chosen protocol has been checked to be less
9464 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9465 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009466 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009467
XiaokangQian95d5f542022-06-24 02:29:26 +00009468 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009469
9470#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9471 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9472#endif
9473
XiaokangQianacb39922022-06-17 10:18:48 +00009474 return ( 0 );
9475}
9476#endif /* MBEDTLS_SSL_ALPN */
9477
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009478#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009479 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009480 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009481 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009482int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9483 const char *hostname )
9484{
9485 /* Initialize to suppress unnecessary compiler warning */
9486 size_t hostname_len = 0;
9487
9488 /* Check if new hostname is valid before
9489 * making any change to current one */
9490 if( hostname != NULL )
9491 {
9492 hostname_len = strlen( hostname );
9493
9494 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9496 }
9497
9498 /* Now it's clear that we will overwrite the old hostname,
9499 * so we can free it safely */
9500 if( session->hostname != NULL )
9501 {
9502 mbedtls_platform_zeroize( session->hostname,
9503 strlen( session->hostname ) );
9504 mbedtls_free( session->hostname );
9505 }
9506
9507 /* Passing NULL as hostname shall clear the old one */
9508 if( hostname == NULL )
9509 {
9510 session->hostname = NULL;
9511 }
9512 else
9513 {
9514 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9515 if( session->hostname == NULL )
9516 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9517
9518 memcpy( session->hostname, hostname, hostname_len );
9519 }
9520
9521 return( 0 );
9522}
Xiaokang Qian03409292022-10-12 02:49:52 +00009523#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9524 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009525 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009526 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528#endif /* MBEDTLS_SSL_TLS_C */