blob: 173ae2591028e4a8488b73db96ee3366a9e5ed96 [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
Andrzej Kurek25f27152022-08-17 16:09:31 -040049#include "legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200238
吴敬辉0b716112021-11-29 10:46:35 +0800239#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
240 dst->ticket = NULL;
241#endif
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000244
245#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246 if( src->peer_cert != NULL )
247 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200249
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200250 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200251 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200252 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200257 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 dst->peer_cert = NULL;
261 return( ret );
262 }
263 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000264#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000265 if( src->peer_cert_digest != NULL )
266 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000267 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000268 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000269 if( dst->peer_cert_digest == NULL )
270 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
271
272 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
273 src->peer_cert_digest_len );
274 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000275 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000276 }
277#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200280
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200281#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200282 if( src->ticket != NULL )
283 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200284 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200285 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200286 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
288 memcpy( dst->ticket, src->ticket, src->ticket_len );
289 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200290#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
292 return( 0 );
293}
294
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500295#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200296MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500297static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
298{
299 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
300 if( resized_buffer == NULL )
301 return -1;
302
303 /* We want to copy len_new bytes when downsizing the buffer, and
304 * len_old bytes when upsizing, so we choose the smaller of two sizes,
305 * to fit one buffer into another. Size checks, ensuring that no data is
306 * lost, are done outside of this function. */
307 memcpy( resized_buffer, *buffer,
308 ( len_new < *len_old ) ? len_new : *len_old );
309 mbedtls_platform_zeroize( *buffer, *len_old );
310 mbedtls_free( *buffer );
311
312 *buffer = resized_buffer;
313 *len_old = len_new;
314
315 return 0;
316}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200317
318static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500319 size_t in_buf_new_len,
320 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200321{
322 int modified = 0;
323 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
324 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
325 if( ssl->in_buf != NULL )
326 {
327 written_in = ssl->in_msg - ssl->in_buf;
328 iv_offset_in = ssl->in_iv - ssl->in_buf;
329 len_offset_in = ssl->in_len - ssl->in_buf;
330 if( downsizing ?
331 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
332 ssl->in_buf_len < in_buf_new_len )
333 {
334 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
335 {
336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
337 }
338 else
339 {
Paul Elliottb7449902021-03-10 18:14:58 +0000340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
341 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200342 modified = 1;
343 }
344 }
345 }
346
347 if( ssl->out_buf != NULL )
348 {
349 written_out = ssl->out_msg - ssl->out_buf;
350 iv_offset_out = ssl->out_iv - ssl->out_buf;
351 len_offset_out = ssl->out_len - ssl->out_buf;
352 if( downsizing ?
353 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
354 ssl->out_buf_len < out_buf_new_len )
355 {
356 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
357 {
358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
359 }
360 else
361 {
Paul Elliottb7449902021-03-10 18:14:58 +0000362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
363 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200364 modified = 1;
365 }
366 }
367 }
368 if( modified )
369 {
370 /* Update pointers here to avoid doing it twice. */
371 mbedtls_ssl_reset_in_out_pointers( ssl );
372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
388typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen );
392
393static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800403static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400411 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl );
414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800417static int tls_prf_sha256( const unsigned char *secret, size_t slen,
418 const char *label,
419 const unsigned char *random, size_t rlen,
420 unsigned char *dstbuf, size_t dlen );
421static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
422static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
423
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800425
Andrzej Kurek25f27152022-08-17 16:09:31 -0400426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200427MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800428static int tls_prf_sha384( const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen );
432
433static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
434static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800436
Jerry Yu438ddd82022-07-07 06:55:50 +0000437static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800438 unsigned char *buf,
439 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800440
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200441MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000442static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800443 const unsigned char *buf,
444 size_t len );
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
Jerry Yu53d23e22022-02-09 16:25:09 +0800447static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
448
Andrzej Kurek25f27152022-08-17 16:09:31 -0400449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800450static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800452
Andrzej Kurek25f27152022-08-17 16:09:31 -0400453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800454static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000456
Ron Eldor51d3ab52019-05-12 14:54:30 +0300457int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen )
462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
465 switch( prf )
466 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300469 case MBEDTLS_SSL_TLS_PRF_SHA384:
470 tls_prf = tls_prf_sha384;
471 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400472#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400473#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300474 case MBEDTLS_SSL_TLS_PRF_SHA256:
475 tls_prf = tls_prf_sha256;
476 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400477#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300478#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300479 default:
480 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
481 }
482
483 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
484}
485
Jerry Yuc73c6182022-02-08 20:29:25 +0800486#if defined(MBEDTLS_X509_CRT_PARSE_C)
487static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
488{
489#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
490 if( session->peer_cert != NULL )
491 {
492 mbedtls_x509_crt_free( session->peer_cert );
493 mbedtls_free( session->peer_cert );
494 session->peer_cert = NULL;
495 }
496#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
497 if( session->peer_cert_digest != NULL )
498 {
499 /* Zeroization is not necessary. */
500 mbedtls_free( session->peer_cert_digest );
501 session->peer_cert_digest = NULL;
502 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
503 session->peer_cert_digest_len = 0;
504 }
505#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
506}
507#endif /* MBEDTLS_X509_CRT_PARSE_C */
508
509void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
510 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
511{
512 ((void) ciphersuite_info);
513
Andrzej Kurek25f27152022-08-17 16:09:31 -0400514#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800515 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
516 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
517 else
518#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400519#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800520 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
521 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
522 else
523#endif
524 {
525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
526 return;
527 }
528}
529
XiaokangQianadab9a62022-07-18 07:41:26 +0000530void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
531 unsigned hs_type,
532 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100533{
534 unsigned char hs_hdr[4];
535
536 /* Build HS header for checksum update. */
537 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
538 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
539 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
540 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
541
542 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
543}
544
545void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
546 unsigned hs_type,
547 unsigned char const *msg,
548 size_t msg_len )
549{
550 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
551 ssl->handshake->update_checksum( ssl, msg, msg_len );
552}
553
Jerry Yuc73c6182022-02-08 20:29:25 +0800554void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
555{
556 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400557#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800558#if defined(MBEDTLS_USE_PSA_CRYPTO)
559 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
560 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
561#else
562 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
563#endif
564#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400565#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800566#if defined(MBEDTLS_USE_PSA_CRYPTO)
567 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
568 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
569#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400570 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800571#endif
572#endif
573}
574
575static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
576 const unsigned char *buf, size_t len )
577{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400578#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800579#if defined(MBEDTLS_USE_PSA_CRYPTO)
580 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
581#else
582 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
583#endif
584#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400585#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800586#if defined(MBEDTLS_USE_PSA_CRYPTO)
587 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
588#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400589 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800590#endif
591#endif
592}
593
Andrzej Kurek25f27152022-08-17 16:09:31 -0400594#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800595static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
596 const unsigned char *buf, size_t len )
597{
598#if defined(MBEDTLS_USE_PSA_CRYPTO)
599 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
600#else
601 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
602#endif
603}
604#endif
605
Andrzej Kurek25f27152022-08-17 16:09:31 -0400606#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800607static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
608 const unsigned char *buf, size_t len )
609{
610#if defined(MBEDTLS_USE_PSA_CRYPTO)
611 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
612#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400613 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800614#endif
615}
616#endif
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200621
Andrzej Kurek25f27152022-08-17 16:09:31 -0400622#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500623#if defined(MBEDTLS_USE_PSA_CRYPTO)
624 handshake->fin_sha256_psa = psa_hash_operation_init();
625 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
626#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200628 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200629#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500630#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400631#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500632#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500633 handshake->fin_sha384_psa = psa_hash_operation_init();
634 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500635#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400636 mbedtls_sha512_init( &handshake->fin_sha384 );
637 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200638#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500639#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640
641 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_DHM_C)
644 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200645#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200646#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200649#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200650 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200651#if defined(MBEDTLS_SSL_CLI_C)
652 handshake->ecjpake_cache = NULL;
653 handshake->ecjpake_cache_len = 0;
654#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200655#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200656
Gilles Peskineeccd8882020-03-10 12:19:08 +0100657#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200658 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200659#endif
660
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200661#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
662 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
663#endif
Hanno Becker75173122019-02-06 16:18:31 +0000664
665#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
666 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
667 mbedtls_pk_init( &handshake->peer_pubkey );
668#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200669}
670
Hanno Beckera18d1322018-01-03 14:27:32 +0000671void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200672{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200674
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100675#if defined(MBEDTLS_USE_PSA_CRYPTO)
676 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
677 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100678#else
679 mbedtls_cipher_init( &transform->cipher_ctx_enc );
680 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100681#endif
682
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000683#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100684#if defined(MBEDTLS_USE_PSA_CRYPTO)
685 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
686 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100687#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_md_init( &transform->md_ctx_enc );
689 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000690#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100691#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200692}
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200695{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200697}
698
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200699MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000701{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200702 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000703 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200705 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200707 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200708 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200709
710 /*
711 * Either the pointers are now NULL or cleared properly and can be freed.
712 * Now allocate missing structures.
713 */
714 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200715 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200716 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200717 }
Paul Bakker48916f92012-09-16 19:57:18 +0000718
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200719 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200720 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200721 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200722 }
Paul Bakker48916f92012-09-16 19:57:18 +0000723
Paul Bakker82788fb2014-10-20 13:59:19 +0200724 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200725 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200726 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200727 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500728#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
729 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400730
Andrzej Kurek4a063792020-10-21 15:08:44 +0200731 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
732 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500733#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000734
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200735 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000736 if( ssl->handshake == NULL ||
737 ssl->transform_negotiate == NULL ||
738 ssl->session_negotiate == NULL )
739 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_free( ssl->handshake );
743 mbedtls_free( ssl->transform_negotiate );
744 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200745
746 ssl->handshake = NULL;
747 ssl->transform_negotiate = NULL;
748 ssl->session_negotiate = NULL;
749
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200750 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000751 }
752
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200753 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000755 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200756 ssl_handshake_params_init( ssl->handshake );
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200759 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
760 {
761 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200762
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200763 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
764 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
765 else
766 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200767
Hanno Becker0f57a652020-02-05 10:37:26 +0000768 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200769 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200770#endif
771
Brett Warrene0edc842021-08-17 09:53:13 +0100772/*
773 * curve_list is translated to IANA TLS group identifiers here because
774 * mbedtls_ssl_conf_curves returns void and so can't return
775 * any error codes.
776 */
777#if defined(MBEDTLS_ECP_C)
778#if !defined(MBEDTLS_DEPRECATED_REMOVED)
779 /* Heap allocate and translate curve_list from internal to IANA group ids */
780 if ( ssl->conf->curve_list != NULL )
781 {
782 size_t length;
783 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
784
785 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
786 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
787
788 /* Leave room for zero termination */
789 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
790 if ( group_list == NULL )
791 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
792
793 for( size_t i = 0; i < length; i++ )
794 {
795 const mbedtls_ecp_curve_info *info =
796 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
797 if ( info == NULL )
798 {
799 mbedtls_free( group_list );
800 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
801 }
802 group_list[i] = info->tls_id;
803 }
804
805 group_list[length] = 0;
806
807 ssl->handshake->group_list = group_list;
808 ssl->handshake->group_list_heap_allocated = 1;
809 }
810 else
811 {
812 ssl->handshake->group_list = ssl->conf->group_list;
813 ssl->handshake->group_list_heap_allocated = 0;
814 }
815#endif /* MBEDTLS_DEPRECATED_REMOVED */
816#endif /* MBEDTLS_ECP_C */
817
Jerry Yuf017ee42022-01-12 15:49:48 +0800818#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
819#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800820#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800821 /* Heap allocate and translate sig_hashes from internal hash identifiers to
822 signature algorithms IANA identifiers. */
823 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800824 ssl->conf->sig_hashes != NULL )
825 {
826 const int *md;
827 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800828 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800829 uint16_t *p;
830
Jerry Yub476a442022-01-21 18:14:45 +0800831#if defined(static_assert)
832 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
833 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
834 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800835#endif
836
Jerry Yuf017ee42022-01-12 15:49:48 +0800837 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
838 {
839 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
840 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800841#if defined(MBEDTLS_ECDSA_C)
842 sig_algs_len += sizeof( uint16_t );
843#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800844
Jerry Yub476a442022-01-21 18:14:45 +0800845#if defined(MBEDTLS_RSA_C)
846 sig_algs_len += sizeof( uint16_t );
847#endif
848 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
849 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800850 }
851
Jerry Yub476a442022-01-21 18:14:45 +0800852 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800853 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
854
Jerry Yub476a442022-01-21 18:14:45 +0800855 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
856 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800857 if( ssl->handshake->sig_algs == NULL )
858 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
859
860 p = (uint16_t *)ssl->handshake->sig_algs;
861 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
862 {
863 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
864 if( hash == MBEDTLS_SSL_HASH_NONE )
865 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800866#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800867 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
868 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800869#endif
870#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800871 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
872 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800873#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800874 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200875 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800876 ssl->handshake->sig_algs_heap_allocated = 1;
877 }
878 else
Jerry Yua69269a2022-01-17 21:06:01 +0800879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800880 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800881 ssl->handshake->sig_algs_heap_allocated = 0;
882 }
Jerry Yucc539102022-06-27 16:27:35 +0800883#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800884#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000885 return( 0 );
886}
887
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200888#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200889/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200890MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200891static int ssl_cookie_write_dummy( void *ctx,
892 unsigned char **p, unsigned char *end,
893 const unsigned char *cli_id, size_t cli_id_len )
894{
895 ((void) ctx);
896 ((void) p);
897 ((void) end);
898 ((void) cli_id);
899 ((void) cli_id_len);
900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200902}
903
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200904MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200905static int ssl_cookie_check_dummy( void *ctx,
906 const unsigned char *cookie, size_t cookie_len,
907 const unsigned char *cli_id, size_t cli_id_len )
908{
909 ((void) ctx);
910 ((void) cookie);
911 ((void) cookie_len);
912 ((void) cli_id);
913 ((void) cli_id_len);
914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200916}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200917#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200918
Paul Bakker5121ce52009-01-03 21:22:43 +0000919/*
920 * Initialize an SSL context
921 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200922void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
923{
924 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
925}
926
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200927MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800928static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
929{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100930 const mbedtls_ssl_config *conf = ssl->conf;
931
Ronald Cron6f135e12021-12-08 16:57:54 +0100932#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100933 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
934 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000935 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
936 {
937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
938 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
939 }
940
Jerry Yu60835a82021-08-04 10:13:52 +0800941 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
942 return( 0 );
943 }
944#endif
945
946#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100947 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800948 {
949 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
950 return( 0 );
951 }
952#endif
953
Ronald Cron6f135e12021-12-08 16:57:54 +0100954#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100955 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800956 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
958 {
959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
960 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
961 }
962
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000963 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
967 }
968
969
Ronald Crone1d3f062022-02-10 14:50:54 +0100970 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
971 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800972 }
973#endif
974
975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
976 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
977}
978
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200979MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800980static int ssl_conf_check(const mbedtls_ssl_context *ssl)
981{
982 int ret;
983 ret = ssl_conf_version_check( ssl );
984 if( ret != 0 )
985 return( ret );
986
987 /* Space for further checks */
988
989 return( 0 );
990}
991
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200992/*
993 * Setup an SSL context
994 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100995
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200996int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200997 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998{
Janos Follath865b3eb2019-12-16 11:46:15 +0000999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001000 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1001 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001003 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001004
Jerry Yu60835a82021-08-04 10:13:52 +08001005 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1006 return( ret );
1007
Paul Bakker62f2dee2012-09-28 07:31:51 +00001008 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001009 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001010 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001011
1012 /* Set to NULL in case of an error condition */
1013 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001014
Darryl Greenb33cc762019-11-28 14:29:44 +00001015#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1016 ssl->in_buf_len = in_buf_len;
1017#endif
1018 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001019 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001022 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001023 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001024 }
1025
Darryl Greenb33cc762019-11-28 14:29:44 +00001026#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1027 ssl->out_buf_len = out_buf_len;
1028#endif
1029 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001030 if( ssl->out_buf == NULL )
1031 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001033 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001034 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 }
1036
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001037 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001038
Johan Pascalb62bb512015-12-03 21:56:45 +01001039#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001040 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001041#endif
1042
Paul Bakker48916f92012-09-16 19:57:18 +00001043 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001044 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
1046 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001047
1048error:
1049 mbedtls_free( ssl->in_buf );
1050 mbedtls_free( ssl->out_buf );
1051
1052 ssl->conf = NULL;
1053
Darryl Greenb33cc762019-11-28 14:29:44 +00001054#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1055 ssl->in_buf_len = 0;
1056 ssl->out_buf_len = 0;
1057#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001058 ssl->in_buf = NULL;
1059 ssl->out_buf = NULL;
1060
1061 ssl->in_hdr = NULL;
1062 ssl->in_ctr = NULL;
1063 ssl->in_len = NULL;
1064 ssl->in_iv = NULL;
1065 ssl->in_msg = NULL;
1066
1067 ssl->out_hdr = NULL;
1068 ssl->out_ctr = NULL;
1069 ssl->out_len = NULL;
1070 ssl->out_iv = NULL;
1071 ssl->out_msg = NULL;
1072
k-stachowiak9f7798e2018-07-31 16:52:32 +02001073 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074}
1075
1076/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001077 * Reset an initialized and used SSL context for re-use while retaining
1078 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001079 *
1080 * If partial is non-zero, keep data in the input buffer and client ID.
1081 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001082 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001083void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1084 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001085{
Darryl Greenb33cc762019-11-28 14:29:44 +00001086#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1087 size_t in_buf_len = ssl->in_buf_len;
1088 size_t out_buf_len = ssl->out_buf_len;
1089#else
1090 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1091 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1092#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001093
Hanno Beckerb0302c42021-08-03 09:39:42 +01001094#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1095 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001096#endif
1097
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001098 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001099 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001100
Hanno Beckerb0302c42021-08-03 09:39:42 +01001101 mbedtls_ssl_reset_in_out_pointers( ssl );
1102
1103 /* Reset incoming message parsing */
1104 ssl->in_offt = NULL;
1105 ssl->nb_zero = 0;
1106 ssl->in_msgtype = 0;
1107 ssl->in_msglen = 0;
1108 ssl->in_hslen = 0;
1109 ssl->keep_current_message = 0;
1110 ssl->transform_in = NULL;
1111
1112#if defined(MBEDTLS_SSL_PROTO_DTLS)
1113 ssl->next_record_offset = 0;
1114 ssl->in_epoch = 0;
1115#endif
1116
1117 /* Keep current datagram if partial == 1 */
1118 if( partial == 0 )
1119 {
1120 ssl->in_left = 0;
1121 memset( ssl->in_buf, 0, in_buf_len );
1122 }
1123
Ronald Cronad8c17b2022-06-10 17:18:09 +02001124 ssl->send_alert = 0;
1125
Hanno Beckerb0302c42021-08-03 09:39:42 +01001126 /* Reset outgoing message writing */
1127 ssl->out_msgtype = 0;
1128 ssl->out_msglen = 0;
1129 ssl->out_left = 0;
1130 memset( ssl->out_buf, 0, out_buf_len );
1131 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1132 ssl->transform_out = NULL;
1133
1134#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1135 mbedtls_ssl_dtls_replay_reset( ssl );
1136#endif
1137
XiaokangQian2b01dc32022-01-21 02:53:13 +00001138#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001139 if( ssl->transform )
1140 {
1141 mbedtls_ssl_transform_free( ssl->transform );
1142 mbedtls_free( ssl->transform );
1143 ssl->transform = NULL;
1144 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1146
XiaokangQian2b01dc32022-01-21 02:53:13 +00001147#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1148 mbedtls_ssl_transform_free( ssl->transform_application );
1149 mbedtls_free( ssl->transform_application );
1150 ssl->transform_application = NULL;
1151
1152 if( ssl->handshake != NULL )
1153 {
1154 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1155 mbedtls_free( ssl->handshake->transform_earlydata );
1156 ssl->handshake->transform_earlydata = NULL;
1157
1158 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1159 mbedtls_free( ssl->handshake->transform_handshake );
1160 ssl->handshake->transform_handshake = NULL;
1161 }
1162
XiaokangQian2b01dc32022-01-21 02:53:13 +00001163#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001164}
1165
1166int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1167{
1168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1169
1170 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1171
XiaokangQian78b1fa72022-01-19 06:56:30 +00001172 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001173
1174 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175#if defined(MBEDTLS_SSL_RENEGOTIATION)
1176 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001177 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001178
1179 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1181 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001184
Hanno Beckerb0302c42021-08-03 09:39:42 +01001185 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001186 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001187 if( ssl->session )
1188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_ssl_session_free( ssl->session );
1190 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001191 ssl->session = NULL;
1192 }
1193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001195 ssl->alpn_chosen = NULL;
1196#endif
1197
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001198#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001199#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001200 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001201#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001202 {
1203 mbedtls_free( ssl->cli_id );
1204 ssl->cli_id = NULL;
1205 ssl->cli_id_len = 0;
1206 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001207#endif
1208
Paul Bakker48916f92012-09-16 19:57:18 +00001209 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1210 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001211
1212 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001213}
1214
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001215/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001216 * Reset an initialized and used SSL context for re-use while retaining
1217 * all application-set variables, function pointers and data.
1218 */
1219int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1220{
Hanno Becker43aefe22020-02-05 10:44:56 +00001221 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001222}
1223
1224/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 * SSL set accessors
1226 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001227void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001228{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001229 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001230}
1231
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001232void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001233{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001234 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001235}
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001238void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001240 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001241}
1242#endif
1243
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001244void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001246 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001247}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001250
Hanno Becker1841b0a2018-08-24 11:13:57 +01001251void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1252 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001253{
1254 ssl->disable_datagram_packing = !allow_packing;
1255}
1256
1257void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1258 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001259{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001260 conf->hs_timeout_min = min;
1261 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001262}
1263#endif
1264
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001265void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001267 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001268}
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001271void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001272 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001273 void *p_vrfy )
1274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001275 conf->f_vrfy = f_vrfy;
1276 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001279
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001280void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001281 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 void *p_rng )
1283{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001284 conf->f_rng = f_rng;
1285 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001286}
1287
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001288void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001289 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 void *p_dbg )
1291{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001292 conf->f_dbg = f_dbg;
1293 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001294}
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001297 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001298 mbedtls_ssl_send_t *f_send,
1299 mbedtls_ssl_recv_t *f_recv,
1300 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001301{
1302 ssl->p_bio = p_bio;
1303 ssl->f_send = f_send;
1304 ssl->f_recv = f_recv;
1305 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001306}
1307
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001308#if defined(MBEDTLS_SSL_PROTO_DTLS)
1309void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1310{
1311 ssl->mtu = mtu;
1312}
1313#endif
1314
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001315void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001316{
1317 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001318}
1319
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001320void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1321 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001322 mbedtls_ssl_set_timer_t *f_set_timer,
1323 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001324{
1325 ssl->p_timer = p_timer;
1326 ssl->f_set_timer = f_set_timer;
1327 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001328
1329 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001330 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001331}
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001334void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001335 void *p_cache,
1336 mbedtls_ssl_cache_get_t *f_get_cache,
1337 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001338{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001339 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001340 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001341 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_SSL_CLI_C)
1346int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347{
Janos Follath865b3eb2019-12-16 11:46:15 +00001348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001349
1350 if( ssl == NULL ||
1351 session == NULL ||
1352 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001353 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001356 }
1357
Hanno Beckere810bbc2021-05-14 16:01:05 +01001358 if( ssl->handshake->resume == 1 )
1359 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1360
Hanno Becker52055ae2019-02-06 14:30:46 +00001361 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1362 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001363 return( ret );
1364
Paul Bakker0a597072012-09-25 21:55:46 +00001365 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001366
1367 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001371void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1372 const int *ciphersuites )
1373{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001374 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001375}
1376
Ronald Cron6f135e12021-12-08 16:57:54 +01001377#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001378void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001379 const int kex_modes )
1380{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001381 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001382}
Ronald Cron6f135e12021-12-08 16:57:54 +01001383#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001386void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001387 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001388{
1389 conf->cert_profile = profile;
1390}
1391
Glenn Strauss36872db2022-01-22 05:06:31 -05001392static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1393{
1394 mbedtls_ssl_key_cert *cur = key_cert, *next;
1395
1396 while( cur != NULL )
1397 {
1398 next = cur->next;
1399 mbedtls_free( cur );
1400 cur = next;
1401 }
1402}
1403
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001404/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001405MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001406static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1407 mbedtls_x509_crt *cert,
1408 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001409{
niisato8ee24222018-06-25 19:05:48 +09001410 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001411
Glenn Strauss36872db2022-01-22 05:06:31 -05001412 if( cert == NULL )
1413 {
1414 /* Free list if cert is null */
1415 ssl_key_cert_free( *head );
1416 *head = NULL;
1417 return( 0 );
1418 }
1419
niisato8ee24222018-06-25 19:05:48 +09001420 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1421 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001422 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001423
niisato8ee24222018-06-25 19:05:48 +09001424 new_cert->cert = cert;
1425 new_cert->key = key;
1426 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001427
Glenn Strauss36872db2022-01-22 05:06:31 -05001428 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001429 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001430 {
niisato8ee24222018-06-25 19:05:48 +09001431 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001432 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001433 else
1434 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001435 mbedtls_ssl_key_cert *cur = *head;
1436 while( cur->next != NULL )
1437 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001438 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001439 }
1440
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001441 return( 0 );
1442}
1443
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001444int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001445 mbedtls_x509_crt *own_cert,
1446 mbedtls_pk_context *pk_key )
1447{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001448 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001449}
1450
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001451void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001452 mbedtls_x509_crt *ca_chain,
1453 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001454{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001455 conf->ca_chain = ca_chain;
1456 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001457
1458#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1459 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1460 * cannot be used together. */
1461 conf->f_ca_cb = NULL;
1462 conf->p_ca_cb = NULL;
1463#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001464}
Hanno Becker5adaad92019-03-27 16:54:37 +00001465
1466#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1467void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001468 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001469 void *p_ca_cb )
1470{
1471 conf->f_ca_cb = f_ca_cb;
1472 conf->p_ca_cb = p_ca_cb;
1473
1474 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1475 * cannot be used together. */
1476 conf->ca_chain = NULL;
1477 conf->ca_crl = NULL;
1478}
1479#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001481
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001482#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001483const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1484 size_t *name_len )
1485{
1486 *name_len = ssl->handshake->sni_name_len;
1487 return( ssl->handshake->sni_name );
1488}
1489
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001490int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1491 mbedtls_x509_crt *own_cert,
1492 mbedtls_pk_context *pk_key )
1493{
1494 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1495 own_cert, pk_key ) );
1496}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001497
1498void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1499 mbedtls_x509_crt *ca_chain,
1500 mbedtls_x509_crl *ca_crl )
1501{
1502 ssl->handshake->sni_ca_chain = ca_chain;
1503 ssl->handshake->sni_ca_crl = ca_crl;
1504}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001505
Glenn Strauss999ef702022-03-11 01:37:23 -05001506#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1507void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1508 const mbedtls_x509_crt *crt)
1509{
1510 ssl->handshake->dn_hints = crt;
1511}
1512#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1513
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001514void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1515 int authmode )
1516{
1517 ssl->handshake->sni_authmode = authmode;
1518}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001519#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1520
Hanno Becker8927c832019-04-03 12:52:50 +01001521#if defined(MBEDTLS_X509_CRT_PARSE_C)
1522void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1523 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1524 void *p_vrfy )
1525{
1526 ssl->f_vrfy = f_vrfy;
1527 ssl->p_vrfy = p_vrfy;
1528}
1529#endif
1530
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001531#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001532/*
1533 * Set EC J-PAKE password for current handshake
1534 */
1535int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1536 const unsigned char *pw,
1537 size_t pw_len )
1538{
1539 mbedtls_ecjpake_role role;
1540
Janos Follath8eb64132016-06-03 15:40:57 +01001541 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1543
1544 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1545 role = MBEDTLS_ECJPAKE_SERVER;
1546 else
1547 role = MBEDTLS_ECJPAKE_CLIENT;
1548
1549 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1550 role,
1551 MBEDTLS_MD_SHA256,
1552 MBEDTLS_ECP_DP_SECP256R1,
1553 pw, pw_len ) );
1554}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001555#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001556
Gilles Peskineeccd8882020-03-10 12:19:08 +01001557#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001558
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001559MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001560static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1561{
1562#if defined(MBEDTLS_USE_PSA_CRYPTO)
1563 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1564 return( 1 );
1565#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001566 if( conf->psk != NULL )
1567 return( 1 );
1568
1569 return( 0 );
1570}
1571
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001572static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1573{
1574 /* Remove reference to existing PSK, if any. */
1575#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001576 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001577 {
1578 /* The maintenance of the PSK key slot is the
1579 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001580 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001581 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001582#endif /* MBEDTLS_USE_PSA_CRYPTO */
1583 if( conf->psk != NULL )
1584 {
1585 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1586
1587 mbedtls_free( conf->psk );
1588 conf->psk = NULL;
1589 conf->psk_len = 0;
1590 }
1591
1592 /* Remove reference to PSK identity, if any. */
1593 if( conf->psk_identity != NULL )
1594 {
1595 mbedtls_free( conf->psk_identity );
1596 conf->psk_identity = NULL;
1597 conf->psk_identity_len = 0;
1598 }
1599}
1600
Hanno Becker7390c712018-11-15 13:33:04 +00001601/* This function assumes that PSK identity in the SSL config is unset.
1602 * It checks that the provided identity is well-formed and attempts
1603 * to make a copy of it in the SSL config.
1604 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001605MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001606static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1607 unsigned char const *psk_identity,
1608 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001609{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001610 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001611 if( psk_identity == NULL ||
1612 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001613 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001614 {
1615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1616 }
1617
Hanno Becker7390c712018-11-15 13:33:04 +00001618 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1619 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001620 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001621
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001622 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001623 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001624
1625 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001626}
1627
Hanno Becker7390c712018-11-15 13:33:04 +00001628int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1629 const unsigned char *psk, size_t psk_len,
1630 const unsigned char *psk_identity, size_t psk_identity_len )
1631{
Janos Follath865b3eb2019-12-16 11:46:15 +00001632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001633
1634 /* We currently only support one PSK, raw or opaque. */
1635 if( ssl_conf_psk_is_configured( conf ) )
1636 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001637
1638 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001639 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001641 if( psk_len == 0 )
1642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1643 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1645
Hanno Becker7390c712018-11-15 13:33:04 +00001646 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1647 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1648 conf->psk_len = psk_len;
1649 memcpy( conf->psk, psk, conf->psk_len );
1650
1651 /* Check and set PSK Identity */
1652 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1653 if( ret != 0 )
1654 ssl_conf_remove_psk( conf );
1655
1656 return( ret );
1657}
1658
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001659static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1660{
1661#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001662 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001663 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001664 /* The maintenance of the external PSK key slot is the
1665 * user's responsibility. */
1666 if( ssl->handshake->psk_opaque_is_internal )
1667 {
1668 psa_destroy_key( ssl->handshake->psk_opaque );
1669 ssl->handshake->psk_opaque_is_internal = 0;
1670 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001671 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001672 }
Neil Armstronge952a302022-05-03 10:22:14 +02001673#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001674 if( ssl->handshake->psk != NULL )
1675 {
1676 mbedtls_platform_zeroize( ssl->handshake->psk,
1677 ssl->handshake->psk_len );
1678 mbedtls_free( ssl->handshake->psk );
1679 ssl->handshake->psk_len = 0;
1680 }
Neil Armstronge952a302022-05-03 10:22:14 +02001681#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001682}
1683
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001684int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1685 const unsigned char *psk, size_t psk_len )
1686{
Neil Armstrong501c9322022-05-03 09:35:09 +02001687#if defined(MBEDTLS_USE_PSA_CRYPTO)
1688 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001690 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001692#endif /* MBEDTLS_USE_PSA_CRYPTO */
1693
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001694 if( psk == NULL || ssl->handshake == NULL )
1695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1696
1697 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1698 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1699
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001700 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001701
Neil Armstrong501c9322022-05-03 09:35:09 +02001702#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001703#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1704 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1705 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001706 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001707 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1708 else
1709 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1711 }
1712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001713
Jerry Yu568ec252022-07-22 21:27:34 +08001714#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001715 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1716 {
1717 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1718 psa_set_key_usage_flags( &key_attributes,
1719 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1720 }
1721#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1722
Neil Armstrong501c9322022-05-03 09:35:09 +02001723 psa_set_key_algorithm( &key_attributes, alg );
1724 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1725
1726 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1727 if( status != PSA_SUCCESS )
1728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1729
1730 /* Allow calling psa_destroy_key() on psk remove */
1731 ssl->handshake->psk_opaque_is_internal = 1;
1732 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1733#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001734 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001735 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001736
1737 ssl->handshake->psk_len = psk_len;
1738 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1739
1740 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001741#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001742}
1743
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001744#if defined(MBEDTLS_USE_PSA_CRYPTO)
1745int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001746 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001747 const unsigned char *psk_identity,
1748 size_t psk_identity_len )
1749{
Janos Follath865b3eb2019-12-16 11:46:15 +00001750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001751
1752 /* We currently only support one PSK, raw or opaque. */
1753 if( ssl_conf_psk_is_configured( conf ) )
1754 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001755
Hanno Becker7390c712018-11-15 13:33:04 +00001756 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001757 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001759 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001760
1761 /* Check and set PSK Identity */
1762 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1763 psk_identity_len );
1764 if( ret != 0 )
1765 ssl_conf_remove_psk( conf );
1766
1767 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001768}
1769
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001770int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1771 mbedtls_svc_key_id_t psk )
1772{
1773 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1774 ( ssl->handshake == NULL ) )
1775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1776
1777 ssl_remove_psk( ssl );
1778 ssl->handshake->psk_opaque = psk;
1779 return( 0 );
1780}
1781#endif /* MBEDTLS_USE_PSA_CRYPTO */
1782
1783void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1784 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1785 size_t),
1786 void *p_psk )
1787{
1788 conf->f_psk = f_psk;
1789 conf->p_psk = p_psk;
1790}
1791#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1792
1793#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001794static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1795 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001796{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001797#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001798 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001799 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001800#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001801 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001802 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001803 return( MBEDTLS_SSL_MODE_STREAM );
1804}
1805
1806#else /* MBEDTLS_USE_PSA_CRYPTO */
1807
1808static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1809 mbedtls_cipher_mode_t mode )
1810{
1811#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1812 if( mode == MBEDTLS_MODE_CBC )
1813 return( MBEDTLS_SSL_MODE_CBC );
1814#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1815
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001816#if defined(MBEDTLS_GCM_C) || \
1817 defined(MBEDTLS_CCM_C) || \
1818 defined(MBEDTLS_CHACHAPOLY_C)
1819 if( mode == MBEDTLS_MODE_GCM ||
1820 mode == MBEDTLS_MODE_CCM ||
1821 mode == MBEDTLS_MODE_CHACHAPOLY )
1822 return( MBEDTLS_SSL_MODE_AEAD );
1823#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001824
1825 return( MBEDTLS_SSL_MODE_STREAM );
1826}
Gilles Peskine301711e2022-04-26 16:57:05 +02001827#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001828
Gilles Peskinee108d982022-04-26 16:50:40 +02001829static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1830 mbedtls_ssl_mode_t base_mode,
1831 int encrypt_then_mac )
1832{
1833#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1834 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1835 base_mode == MBEDTLS_SSL_MODE_CBC )
1836 {
1837 return( MBEDTLS_SSL_MODE_CBC_ETM );
1838 }
1839#else
1840 (void) encrypt_then_mac;
1841#endif
1842 return( base_mode );
1843}
1844
Neil Armstrongab555e02022-04-04 11:07:59 +02001845mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001846 const mbedtls_ssl_transform *transform )
1847{
Gilles Peskinee108d982022-04-26 16:50:40 +02001848 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001849#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001850 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001851#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001852 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1853#endif
1854 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001855
Gilles Peskinee108d982022-04-26 16:50:40 +02001856 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001857#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001858 encrypt_then_mac = transform->encrypt_then_mac;
1859#endif
1860 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001861}
1862
Neil Armstrongab555e02022-04-04 11:07:59 +02001863mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001864#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001865 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001866#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001867 const mbedtls_ssl_ciphersuite_t *suite )
1868{
Gilles Peskinee108d982022-04-26 16:50:40 +02001869 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1870
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001871#if defined(MBEDTLS_USE_PSA_CRYPTO)
1872 psa_status_t status;
1873 psa_algorithm_t alg;
1874 psa_key_type_t type;
1875 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001876 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1877 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001878 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001879#else
1880 const mbedtls_cipher_info_t *cipher =
1881 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001882 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001883 {
1884 base_mode =
1885 mbedtls_ssl_get_base_mode(
1886 mbedtls_cipher_info_get_mode( cipher ) );
1887 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001888#endif /* MBEDTLS_USE_PSA_CRYPTO */
1889
Gilles Peskinee108d982022-04-26 16:50:40 +02001890#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1891 int encrypt_then_mac = 0;
1892#endif
1893 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894}
1895
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001896#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001897
1898#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001899/* Serialization of TLS 1.3 sessions:
1900 *
1901 * struct {
1902 * uint64 ticket_received;
1903 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001904 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001905 * } ClientOnlyData;
1906 *
1907 * struct {
1908 * uint8 endpoint;
1909 * uint8 ciphersuite[2];
1910 * uint32 ticket_age_add;
1911 * uint8 ticket_flags;
1912 * opaque resumption_key<0..255>;
1913 * select ( endpoint ) {
1914 * case client: ClientOnlyData;
1915 * case server: uint64 start_time;
1916 * };
1917 * } serialized_session_tls13;
1918 *
1919 */
1920#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001921MBEDTLS_CHECK_RETURN_CRITICAL
1922static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1923 unsigned char *buf,
1924 size_t buf_len,
1925 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001926{
1927 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001928 size_t needed = 1 /* endpoint */
1929 + 2 /* ciphersuite */
1930 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001931 + 1 /* ticket_flags */
1932 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001933 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001934
1935 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1937 needed += session->resumption_key_len; /* resumption_key */
1938
Jerry Yu438ddd82022-07-07 06:55:50 +00001939#if defined(MBEDTLS_HAVE_TIME)
1940 needed += 8; /* start_time or ticket_received */
1941#endif
1942
1943#if defined(MBEDTLS_SSL_CLI_C)
1944 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1945 {
1946 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001947 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001948
1949 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001950 if( session->ticket_len > SIZE_MAX - needed )
1951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001952
Jerry Yue28d9742022-08-18 15:44:03 +08001953 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001954 }
1955#endif /* MBEDTLS_SSL_CLI_C */
1956
Jerry Yue36fdd62022-08-17 21:31:36 +08001957 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001958 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001959 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001960
1961 p[0] = session->endpoint;
1962 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1963 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1964 p[7] = session->ticket_flags;
1965
1966 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001967 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001968 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001969 memcpy( p, session->resumption_key, session->resumption_key_len );
1970 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001971
1972#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1973 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1974 {
1975 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1976 p += 8;
1977 }
1978#endif /* MBEDTLS_HAVE_TIME */
1979
1980#if defined(MBEDTLS_SSL_CLI_C)
1981 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1982 {
1983#if defined(MBEDTLS_HAVE_TIME)
1984 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
1985 p += 8;
1986#endif
1987 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
1988 p += 4;
1989
1990 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
1991 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08001992
1993 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00001994 {
1995 memcpy( p, session->ticket, session->ticket_len );
1996 p += session->ticket_len;
1997 }
1998 }
1999#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002000 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002001}
2002
2003MBEDTLS_CHECK_RETURN_CRITICAL
2004static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2005 const unsigned char *buf,
2006 size_t len )
2007{
2008 const unsigned char *p = buf;
2009 const unsigned char *end = buf + len;
2010
2011 if( end - p < 9 )
2012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2013 session->endpoint = p[0];
2014 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2015 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2016 session->ticket_flags = p[7];
2017
2018 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002019 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002020 p += 9;
2021
Jerry Yubc7c1a42022-07-21 22:57:37 +08002022 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2024
Jerry Yubc7c1a42022-07-21 22:57:37 +08002025 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002027 memcpy( session->resumption_key, p, session->resumption_key_len );
2028 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002029
2030#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2031 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2032 {
2033 if( end - p < 8 )
2034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2035 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2036 p += 8;
2037 }
2038#endif /* MBEDTLS_HAVE_TIME */
2039
2040#if defined(MBEDTLS_SSL_CLI_C)
2041 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2042 {
2043#if defined(MBEDTLS_HAVE_TIME)
2044 if( end - p < 8 )
2045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2046 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2047 p += 8;
2048#endif
2049 if( end - p < 4 )
2050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2051 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2052 p += 4;
2053
2054 if( end - p < 2 )
2055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2056 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2057 p += 2;
2058
2059 if( end - p < ( long int )session->ticket_len )
2060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2061 if( session->ticket_len > 0 )
2062 {
2063 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2064 if( session->ticket == NULL )
2065 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2066 memcpy( session->ticket, p, session->ticket_len );
2067 p += session->ticket_len;
2068 }
2069 }
2070#endif /* MBEDTLS_SSL_CLI_C */
2071
2072 return( 0 );
2073
2074}
2075#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002076MBEDTLS_CHECK_RETURN_CRITICAL
2077static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2078 unsigned char *buf,
2079 size_t buf_len,
2080 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002081{
2082 ((void) session);
2083 ((void) buf);
2084 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002085 *olen = 0;
2086 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002087}
Jerry Yu438ddd82022-07-07 06:55:50 +00002088
2089static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2090 unsigned char *buf,
2091 size_t buf_len )
2092{
2093 ((void) session);
2094 ((void) buf);
2095 ((void) buf_len);
2096 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2097}
2098#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002099#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2100
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002101psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002102 size_t taglen,
2103 psa_algorithm_t *alg,
2104 psa_key_type_t *key_type,
2105 size_t *key_size )
2106{
2107 switch ( mbedtls_cipher_type )
2108 {
2109 case MBEDTLS_CIPHER_AES_128_CBC:
2110 *alg = PSA_ALG_CBC_NO_PADDING;
2111 *key_type = PSA_KEY_TYPE_AES;
2112 *key_size = 128;
2113 break;
2114 case MBEDTLS_CIPHER_AES_128_CCM:
2115 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2116 *key_type = PSA_KEY_TYPE_AES;
2117 *key_size = 128;
2118 break;
2119 case MBEDTLS_CIPHER_AES_128_GCM:
2120 *alg = PSA_ALG_GCM;
2121 *key_type = PSA_KEY_TYPE_AES;
2122 *key_size = 128;
2123 break;
2124 case MBEDTLS_CIPHER_AES_192_CCM:
2125 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2126 *key_type = PSA_KEY_TYPE_AES;
2127 *key_size = 192;
2128 break;
2129 case MBEDTLS_CIPHER_AES_192_GCM:
2130 *alg = PSA_ALG_GCM;
2131 *key_type = PSA_KEY_TYPE_AES;
2132 *key_size = 192;
2133 break;
2134 case MBEDTLS_CIPHER_AES_256_CBC:
2135 *alg = PSA_ALG_CBC_NO_PADDING;
2136 *key_type = PSA_KEY_TYPE_AES;
2137 *key_size = 256;
2138 break;
2139 case MBEDTLS_CIPHER_AES_256_CCM:
2140 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2141 *key_type = PSA_KEY_TYPE_AES;
2142 *key_size = 256;
2143 break;
2144 case MBEDTLS_CIPHER_AES_256_GCM:
2145 *alg = PSA_ALG_GCM;
2146 *key_type = PSA_KEY_TYPE_AES;
2147 *key_size = 256;
2148 break;
2149 case MBEDTLS_CIPHER_ARIA_128_CBC:
2150 *alg = PSA_ALG_CBC_NO_PADDING;
2151 *key_type = PSA_KEY_TYPE_ARIA;
2152 *key_size = 128;
2153 break;
2154 case MBEDTLS_CIPHER_ARIA_128_CCM:
2155 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2156 *key_type = PSA_KEY_TYPE_ARIA;
2157 *key_size = 128;
2158 break;
2159 case MBEDTLS_CIPHER_ARIA_128_GCM:
2160 *alg = PSA_ALG_GCM;
2161 *key_type = PSA_KEY_TYPE_ARIA;
2162 *key_size = 128;
2163 break;
2164 case MBEDTLS_CIPHER_ARIA_192_CCM:
2165 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2166 *key_type = PSA_KEY_TYPE_ARIA;
2167 *key_size = 192;
2168 break;
2169 case MBEDTLS_CIPHER_ARIA_192_GCM:
2170 *alg = PSA_ALG_GCM;
2171 *key_type = PSA_KEY_TYPE_ARIA;
2172 *key_size = 192;
2173 break;
2174 case MBEDTLS_CIPHER_ARIA_256_CBC:
2175 *alg = PSA_ALG_CBC_NO_PADDING;
2176 *key_type = PSA_KEY_TYPE_ARIA;
2177 *key_size = 256;
2178 break;
2179 case MBEDTLS_CIPHER_ARIA_256_CCM:
2180 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2181 *key_type = PSA_KEY_TYPE_ARIA;
2182 *key_size = 256;
2183 break;
2184 case MBEDTLS_CIPHER_ARIA_256_GCM:
2185 *alg = PSA_ALG_GCM;
2186 *key_type = PSA_KEY_TYPE_ARIA;
2187 *key_size = 256;
2188 break;
2189 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2190 *alg = PSA_ALG_CBC_NO_PADDING;
2191 *key_type = PSA_KEY_TYPE_CAMELLIA;
2192 *key_size = 128;
2193 break;
2194 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2195 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2196 *key_type = PSA_KEY_TYPE_CAMELLIA;
2197 *key_size = 128;
2198 break;
2199 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2200 *alg = PSA_ALG_GCM;
2201 *key_type = PSA_KEY_TYPE_CAMELLIA;
2202 *key_size = 128;
2203 break;
2204 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2205 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2206 *key_type = PSA_KEY_TYPE_CAMELLIA;
2207 *key_size = 192;
2208 break;
2209 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2210 *alg = PSA_ALG_GCM;
2211 *key_type = PSA_KEY_TYPE_CAMELLIA;
2212 *key_size = 192;
2213 break;
2214 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2215 *alg = PSA_ALG_CBC_NO_PADDING;
2216 *key_type = PSA_KEY_TYPE_CAMELLIA;
2217 *key_size = 256;
2218 break;
2219 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2220 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2221 *key_type = PSA_KEY_TYPE_CAMELLIA;
2222 *key_size = 256;
2223 break;
2224 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2225 *alg = PSA_ALG_GCM;
2226 *key_type = PSA_KEY_TYPE_CAMELLIA;
2227 *key_size = 256;
2228 break;
2229 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2230 *alg = PSA_ALG_CHACHA20_POLY1305;
2231 *key_type = PSA_KEY_TYPE_CHACHA20;
2232 *key_size = 256;
2233 break;
2234 case MBEDTLS_CIPHER_NULL:
2235 *alg = MBEDTLS_SSL_NULL_CIPHER;
2236 *key_type = 0;
2237 *key_size = 0;
2238 break;
2239 default:
2240 return PSA_ERROR_NOT_SUPPORTED;
2241 }
2242
2243 return PSA_SUCCESS;
2244}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002245#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002246
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002247#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002248int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2249 const unsigned char *dhm_P, size_t P_len,
2250 const unsigned char *dhm_G, size_t G_len )
2251{
Janos Follath865b3eb2019-12-16 11:46:15 +00002252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002253
Glenn Strausscee11292021-12-20 01:43:17 -05002254 mbedtls_mpi_free( &conf->dhm_P );
2255 mbedtls_mpi_free( &conf->dhm_G );
2256
Hanno Beckera90658f2017-10-04 15:29:08 +01002257 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2258 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2259 {
2260 mbedtls_mpi_free( &conf->dhm_P );
2261 mbedtls_mpi_free( &conf->dhm_G );
2262 return( ret );
2263 }
2264
2265 return( 0 );
2266}
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002268int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002269{
Janos Follath865b3eb2019-12-16 11:46:15 +00002270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002271
Glenn Strausscee11292021-12-20 01:43:17 -05002272 mbedtls_mpi_free( &conf->dhm_P );
2273 mbedtls_mpi_free( &conf->dhm_G );
2274
Gilles Peskinee5702482021-06-11 21:59:08 +02002275 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2276 &conf->dhm_P ) ) != 0 ||
2277 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2278 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002279 {
2280 mbedtls_mpi_free( &conf->dhm_P );
2281 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002282 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002283 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002284
2285 return( 0 );
2286}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002287#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002288
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002289#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2290/*
2291 * Set the minimum length for Diffie-Hellman parameters
2292 */
2293void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2294 unsigned int bitlen )
2295{
2296 conf->dhm_min_bitlen = bitlen;
2297}
2298#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2299
Gilles Peskineeccd8882020-03-10 12:19:08 +01002300#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002301#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002302/*
2303 * Set allowed/preferred hashes for handshake signatures
2304 */
2305void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2306 const int *hashes )
2307{
2308 conf->sig_hashes = hashes;
2309}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002310#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002311
Jerry Yuf017ee42022-01-12 15:49:48 +08002312/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002313void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2314 const uint16_t* sig_algs )
2315{
Jerry Yuf017ee42022-01-12 15:49:48 +08002316#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2317 conf->sig_hashes = NULL;
2318#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2319 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002320}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002321#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002322
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002323#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002324#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002325/*
2326 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002327 *
2328 * mbedtls_ssl_setup() takes the provided list
2329 * and translates it to a list of IANA TLS group identifiers,
2330 * stored in ssl->handshake->group_list.
2331 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002332 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002333void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002334 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002335{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002336 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002337 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002338}
Brett Warrene0edc842021-08-17 09:53:13 +01002339#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002340#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002341
Brett Warrene0edc842021-08-17 09:53:13 +01002342/*
2343 * Set the allowed groups
2344 */
2345void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2346 const uint16_t *group_list )
2347{
2348#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2349 conf->curve_list = NULL;
2350#endif
2351 conf->group_list = group_list;
2352}
2353
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002354#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002356{
Hanno Becker947194e2017-04-07 13:25:49 +01002357 /* Initialize to suppress unnecessary compiler warning */
2358 size_t hostname_len = 0;
2359
2360 /* Check if new hostname is valid before
2361 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002362 if( hostname != NULL )
2363 {
2364 hostname_len = strlen( hostname );
2365
2366 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2368 }
2369
2370 /* Now it's clear that we will overwrite the old hostname,
2371 * so we can free it safely */
2372
2373 if( ssl->hostname != NULL )
2374 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002375 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002376 mbedtls_free( ssl->hostname );
2377 }
2378
2379 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002380
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002382 {
2383 ssl->hostname = NULL;
2384 }
2385 else
2386 {
2387 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002388 if( ssl->hostname == NULL )
2389 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002390
Hanno Becker947194e2017-04-07 13:25:49 +01002391 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002392
Hanno Becker947194e2017-04-07 13:25:49 +01002393 ssl->hostname[hostname_len] = '\0';
2394 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
2396 return( 0 );
2397}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002398#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002400#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002401void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002403 const unsigned char *, size_t),
2404 void *p_sni )
2405{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002406 conf->f_sni = f_sni;
2407 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002412int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002413{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002414 size_t cur_len, tot_len;
2415 const char **p;
2416
2417 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002418 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2419 * MUST NOT be truncated."
2420 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002421 */
2422 tot_len = 0;
2423 for( p = protos; *p != NULL; p++ )
2424 {
2425 cur_len = strlen( *p );
2426 tot_len += cur_len;
2427
Ronald Cron8216dd32020-04-23 16:41:44 +02002428 if( ( cur_len == 0 ) ||
2429 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2430 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002432 }
2433
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002434 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002435
2436 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002437}
2438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002440{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002441 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002444
Johan Pascalb62bb512015-12-03 21:56:45 +01002445#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002446void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2447 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002448{
2449 conf->dtls_srtp_mki_support = support_mki_value;
2450}
2451
Ron Eldoref72faf2018-07-12 11:54:20 +03002452int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2453 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002454 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002455{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002456 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002457 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002459 }
Ron Eldor591f1622018-01-22 12:30:04 +02002460
2461 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002462 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002463 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002464 }
Ron Eldor591f1622018-01-22 12:30:04 +02002465
2466 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2467 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002468 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002469}
2470
Ron Eldoref72faf2018-07-12 11:54:20 +03002471int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002472 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002473{
Johan Pascal253d0262020-09-22 13:04:45 +02002474 const mbedtls_ssl_srtp_profile *p;
2475 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002476
Johan Pascal253d0262020-09-22 13:04:45 +02002477 /* check the profiles list: all entry must be valid,
2478 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002479 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2480 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2481 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002482 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002483 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002484 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002485 list_size++;
2486 }
2487 else
2488 {
2489 /* unsupported value, stop parsing and set the size to an error value */
2490 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002491 }
2492 }
2493
Johan Pascal5ef72d22020-10-28 17:05:47 +01002494 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002495 {
Johan Pascal253d0262020-09-22 13:04:45 +02002496 conf->dtls_srtp_profile_list = NULL;
2497 conf->dtls_srtp_profile_list_len = 0;
2498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2499 }
2500
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002501 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002502 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002503
2504 return( 0 );
2505}
2506
Johan Pascal5ef72d22020-10-28 17:05:47 +01002507void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2508 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002509{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002510 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2511 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002512 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002513 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002514 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002515 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002516 else
2517 {
2518 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002519 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2520 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002521 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002522}
Johan Pascalb62bb512015-12-03 21:56:45 +01002523#endif /* MBEDTLS_SSL_DTLS_SRTP */
2524
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302525#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002526void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002527{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002528 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002529}
2530
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002531void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002532{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002533 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002534}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302535#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002536
Janos Follath088ce432017-04-10 12:42:31 +01002537#if defined(MBEDTLS_SSL_SRV_C)
2538void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2539 char cert_req_ca_list )
2540{
2541 conf->cert_req_ca_list = cert_req_ca_list;
2542}
2543#endif
2544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002546void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002547{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002548 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002549}
2550#endif
2551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002553void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002554{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002555 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002556}
2557#endif
2558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002560int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002563 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002566 }
2567
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002568 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002569
2570 return( 0 );
2571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002573
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002574void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002576 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002577}
2578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002580void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002581{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002582 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002583}
2584
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002585void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002587 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002588}
2589
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002590void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002591 const unsigned char period[8] )
2592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002593 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002598#if defined(MBEDTLS_SSL_CLI_C)
2599void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002600{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002601 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002602}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002603#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002604
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002605#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002606void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2607 mbedtls_ssl_ticket_write_t *f_ticket_write,
2608 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2609 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002610{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002611 conf->f_ticket_write = f_ticket_write;
2612 conf->f_ticket_parse = f_ticket_parse;
2613 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002614}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002617
Hanno Becker7e6c1782021-06-08 09:24:55 +01002618void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2619 mbedtls_ssl_export_keys_t *f_export_keys,
2620 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002621{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002622 ssl->f_export_keys = f_export_keys;
2623 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002624}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002625
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002626#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002627void mbedtls_ssl_conf_async_private_cb(
2628 mbedtls_ssl_config *conf,
2629 mbedtls_ssl_async_sign_t *f_async_sign,
2630 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2631 mbedtls_ssl_async_resume_t *f_async_resume,
2632 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002633 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002634{
2635 conf->f_async_sign_start = f_async_sign;
2636 conf->f_async_decrypt_start = f_async_decrypt;
2637 conf->f_async_resume = f_async_resume;
2638 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002639 conf->p_async_config_data = async_config_data;
2640}
2641
Gilles Peskine8f97af72018-04-26 11:46:10 +02002642void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2643{
2644 return( conf->p_async_config_data );
2645}
2646
Gilles Peskine1febfef2018-04-30 11:54:39 +02002647void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002648{
2649 if( ssl->handshake == NULL )
2650 return( NULL );
2651 else
2652 return( ssl->handshake->user_async_ctx );
2653}
2654
Gilles Peskine1febfef2018-04-30 11:54:39 +02002655void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002656 void *ctx )
2657{
2658 if( ssl->handshake != NULL )
2659 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002660}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002661#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002662
Paul Bakker5121ce52009-01-03 21:22:43 +00002663/*
2664 * SSL get accessors
2665 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002666uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002667{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002668 if( ssl->session != NULL )
2669 return( ssl->session->verify_result );
2670
2671 if( ssl->session_negotiate != NULL )
2672 return( ssl->session_negotiate->verify_result );
2673
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002674 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675}
2676
Glenn Strauss8f526902022-01-13 00:04:49 -05002677int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2678{
2679 if( ssl == NULL || ssl->session == NULL )
2680 return( 0 );
2681
2682 return( ssl->session->ciphersuite );
2683}
2684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002686{
Paul Bakker926c8e42013-03-06 10:23:34 +01002687 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002688 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002691}
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002696 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002697 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002698 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002699 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002700 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002701 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002702 default:
2703 return( "unknown (DTLS)" );
2704 }
2705 }
2706#endif
2707
Glenn Strauss60bfe602022-03-14 19:04:24 -04002708 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002709 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002710 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002711 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002712 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002713 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002714 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002715 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002716 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002717}
2718
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002719#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002720size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2721{
David Horstmann95d516f2021-05-04 18:36:56 +01002722 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002723 size_t read_mfl;
2724
2725 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2727 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2728 {
2729 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2730 }
2731
2732 /* Check if a smaller max length was negotiated */
2733 if( ssl->session_out != NULL )
2734 {
2735 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2736 if( read_mfl < max_len )
2737 {
2738 max_len = read_mfl;
2739 }
2740 }
2741
2742 // During a handshake, use the value being negotiated
2743 if( ssl->session_negotiate != NULL )
2744 {
2745 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2746 if( read_mfl < max_len )
2747 {
2748 max_len = read_mfl;
2749 }
2750 }
2751
2752 return( max_len );
2753}
2754
2755size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002756{
2757 size_t max_len;
2758
2759 /*
2760 * Assume mfl_code is correct since it was checked when set
2761 */
Angus Grattond8213d02016-05-25 20:56:48 +10002762 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002763
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002764 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002765 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002766 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002767 {
Angus Grattond8213d02016-05-25 20:56:48 +10002768 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002769 }
2770
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002771 /* During a handshake, use the value being negotiated */
2772 if( ssl->session_negotiate != NULL &&
2773 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2774 {
2775 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2776 }
2777
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002778 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002779}
2780#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2781
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002783size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002784{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002785 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2786 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2787 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2788 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2789 return ( 0 );
2790
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002791 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2792 return( ssl->mtu );
2793
2794 if( ssl->mtu == 0 )
2795 return( ssl->handshake->mtu );
2796
2797 return( ssl->mtu < ssl->handshake->mtu ?
2798 ssl->mtu : ssl->handshake->mtu );
2799}
2800#endif /* MBEDTLS_SSL_PROTO_DTLS */
2801
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002802int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2803{
2804 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2805
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002806#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2807 !defined(MBEDTLS_SSL_PROTO_DTLS)
2808 (void) ssl;
2809#endif
2810
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002811#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002812 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002813
2814 if( max_len > mfl )
2815 max_len = mfl;
2816#endif
2817
2818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002819 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002820 {
Hanno Becker89490712020-02-05 10:50:12 +00002821 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002822 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2823 const size_t overhead = (size_t) ret;
2824
2825 if( ret < 0 )
2826 return( ret );
2827
2828 if( mtu <= overhead )
2829 {
2830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2831 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2832 }
2833
2834 if( max_len > mtu - overhead )
2835 max_len = mtu - overhead;
2836 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002837#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002838
Hanno Becker0defedb2018-08-10 12:35:02 +01002839#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2840 !defined(MBEDTLS_SSL_PROTO_DTLS)
2841 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002842#endif
2843
2844 return( (int) max_len );
2845}
2846
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002847int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2848{
2849 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2850
2851#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2852 (void) ssl;
2853#endif
2854
2855#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2856 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2857
2858 if( max_len > mfl )
2859 max_len = mfl;
2860#endif
2861
2862 return( (int) max_len );
2863}
2864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865#if defined(MBEDTLS_X509_CRT_PARSE_C)
2866const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002867{
2868 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002869 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002870
Hanno Beckere6824572019-02-07 13:18:46 +00002871#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002872 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002873#else
2874 return( NULL );
2875#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002880int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2881 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002882{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002883 int ret;
2884
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002885 if( ssl == NULL ||
2886 dst == NULL ||
2887 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002888 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002891 }
2892
Hanno Beckere810bbc2021-05-14 16:01:05 +01002893 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2894 * idempotent: Each session can only be exported once.
2895 *
2896 * (This is in preparation for TLS 1.3 support where we will
2897 * need the ability to export multiple sessions (aka tickets),
2898 * which will be achieved by calling mbedtls_ssl_get_session()
2899 * multiple times until it fails.)
2900 *
2901 * Check whether we have already exported the current session,
2902 * and fail if so.
2903 */
2904 if( ssl->session->exported == 1 )
2905 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2906
2907 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2908 if( ret != 0 )
2909 return( ret );
2910
2911 /* Remember that we've exported the session. */
2912 ssl->session->exported = 1;
2913 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002916
Paul Bakker5121ce52009-01-03 21:22:43 +00002917/*
Hanno Beckera835da52019-05-16 12:39:07 +01002918 * Define ticket header determining Mbed TLS version
2919 * and structure of the ticket.
2920 */
2921
Hanno Becker94ef3b32019-05-16 12:50:45 +01002922/*
Hanno Becker50b59662019-05-28 14:30:45 +01002923 * Define bitflag determining compile-time settings influencing
2924 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002925 */
2926
Hanno Becker50b59662019-05-28 14:30:45 +01002927#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002928#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002929#else
Hanno Becker3e088662019-05-29 11:10:18 +01002930#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002931#endif /* MBEDTLS_HAVE_TIME */
2932
2933#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002934#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002935#else
Hanno Becker3e088662019-05-29 11:10:18 +01002936#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002937#endif /* MBEDTLS_X509_CRT_PARSE_C */
2938
2939#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002940#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002941#else
Hanno Becker3e088662019-05-29 11:10:18 +01002942#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002943#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2944
2945#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002946#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002947#else
Hanno Becker3e088662019-05-29 11:10:18 +01002948#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002949#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2950
Hanno Becker94ef3b32019-05-16 12:50:45 +01002951#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002952#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002953#else
Hanno Becker3e088662019-05-29 11:10:18 +01002954#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002955#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2956
Hanno Becker94ef3b32019-05-16 12:50:45 +01002957#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2958#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2959#else
2960#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2961#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2962
Hanno Becker3e088662019-05-29 11:10:18 +01002963#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2964#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2965#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2966#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002967#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2968#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002969
Hanno Becker50b59662019-05-28 14:30:45 +01002970#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002971 ( (uint16_t) ( \
2972 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2973 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2974 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2975 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002976 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002977 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002978
Hanno Beckerf8787072019-05-16 12:41:07 +01002979static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002980 MBEDTLS_VERSION_MAJOR,
2981 MBEDTLS_VERSION_MINOR,
2982 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002983 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2984 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002985};
Hanno Beckera835da52019-05-16 12:39:07 +01002986
2987/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002988 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002989 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002990 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002991 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002992 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002993 * opaque mbedtls_version[3]; // library version: major, minor, patch
2994 * opaque session_format[2]; // library-version specific 16-bit field
2995 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002996 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002997 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002998 * Note: When updating the format, remember to keep
2999 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003000 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003001 * // In this version, `session_format` determines
3002 * // the setting of those compile-time
3003 * // configuration options which influence
3004 * // the structure of mbedtls_ssl_session.
3005 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003006 * uint8_t minor_ver; // Protocol minor version. Possible values:
3007 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003008 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003009 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003010 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003011 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003012 * serialized_session_tls12 data;
3013 *
3014 * };
3015 *
3016 * } serialized_session;
3017 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003018 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003019
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003020MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003021static int ssl_session_save( const mbedtls_ssl_session *session,
3022 unsigned char omit_header,
3023 unsigned char *buf,
3024 size_t buf_len,
3025 size_t *olen )
3026{
3027 unsigned char *p = buf;
3028 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003029 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003030#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3031 size_t out_len;
3032 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3033#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003034 if( session == NULL )
3035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3036
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003037 if( !omit_header )
3038 {
3039 /*
3040 * Add Mbed TLS version identifier
3041 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003042 used += sizeof( ssl_serialized_session_header );
3043
3044 if( used <= buf_len )
3045 {
3046 memcpy( p, ssl_serialized_session_header,
3047 sizeof( ssl_serialized_session_header ) );
3048 p += sizeof( ssl_serialized_session_header );
3049 }
3050 }
3051
3052 /*
3053 * TLS version identifier
3054 */
3055 used += 1;
3056 if( used <= buf_len )
3057 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003058 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003059 }
3060
3061 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003062 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003063 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003064 {
3065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003066 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003067 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003068 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003069#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3070
Jerry Yu251a12e2022-07-13 15:15:48 +08003071#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3072 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003073 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3074 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003075 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003076 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003077 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003078#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3079
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003080 default:
3081 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3082 }
3083
3084 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003085 if( used > buf_len )
3086 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003087
3088 return( 0 );
3089}
3090
3091/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003092 * Public wrapper for ssl_session_save()
3093 */
3094int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3095 unsigned char *buf,
3096 size_t buf_len,
3097 size_t *olen )
3098{
3099 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3100}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003101
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003102/*
3103 * Deserialize session, see mbedtls_ssl_session_save() for format.
3104 *
3105 * This internal version is wrapped by a public function that cleans up in
3106 * case of error, and has an extra option omit_header.
3107 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003108MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003109static int ssl_session_load( mbedtls_ssl_session *session,
3110 unsigned char omit_header,
3111 const unsigned char *buf,
3112 size_t len )
3113{
3114 const unsigned char *p = buf;
3115 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003116 size_t remaining_len;
3117
3118
3119 if( session == NULL )
3120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003121
3122 if( !omit_header )
3123 {
3124 /*
3125 * Check Mbed TLS version identifier
3126 */
3127
3128 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3130
3131 if( memcmp( p, ssl_serialized_session_header,
3132 sizeof( ssl_serialized_session_header ) ) != 0 )
3133 {
3134 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3135 }
3136 p += sizeof( ssl_serialized_session_header );
3137 }
3138
3139 /*
3140 * TLS version identifier
3141 */
3142 if( 1 > (size_t)( end - p ) )
3143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003144 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003145
3146 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003147 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003148 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003149 {
3150#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003151 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003152 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3154
Jerry Yu438ddd82022-07-07 06:55:50 +00003155#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3156 case MBEDTLS_SSL_VERSION_TLS1_3:
3157 return( ssl_tls13_session_load( session, p, remaining_len ) );
3158#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3159
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003160 default:
3161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3162 }
3163}
3164
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003165/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003166 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003167 */
3168int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3169 const unsigned char *buf,
3170 size_t len )
3171{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003172 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003173
3174 if( ret != 0 )
3175 mbedtls_ssl_session_free( session );
3176
3177 return( ret );
3178}
3179
3180/*
Paul Bakker1961b702013-01-25 14:49:24 +01003181 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003183MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003184static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3185{
3186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3187
Ronald Cron66dbf912022-02-02 15:33:46 +01003188 /*
3189 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003190 * that were written into the output buffer by the previous handshake step,
3191 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003192 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3193 * We proceed to the next handshake step only when all data from the
3194 * previous one have been sent to the peer, thus we make sure that this is
3195 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3196 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3197 * we have to wait before to go ahead.
3198 * In the case of TLS 1.3, handshake step handlers do not send data to the
3199 * peer. Data are only sent here and through
3200 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003201 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003202 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003203 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3204 return( ret );
3205
3206#if defined(MBEDTLS_SSL_PROTO_DTLS)
3207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3208 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3209 {
3210 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3211 return( ret );
3212 }
3213#endif /* MBEDTLS_SSL_PROTO_DTLS */
3214
3215 return( ret );
3216}
3217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219{
Hanno Becker41934dd2021-08-07 19:13:43 +01003220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Hanno Becker41934dd2021-08-07 19:13:43 +01003222 if( ssl == NULL ||
3223 ssl->conf == NULL ||
3224 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003225 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003226 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003228 }
3229
3230 ret = ssl_prepare_handshake_step( ssl );
3231 if( ret != 0 )
3232 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003233
Jerry Yue7047812021-09-13 19:26:39 +08003234 ret = mbedtls_ssl_handle_pending_alert( ssl );
3235 if( ret != 0 )
3236 goto cleanup;
3237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003239 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003240 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3242 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003243
Ronald Cron9f0fba32022-02-10 16:45:15 +01003244 switch( ssl->state )
3245 {
3246 case MBEDTLS_SSL_HELLO_REQUEST:
3247 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3248 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003249
Ronald Cron9f0fba32022-02-10 16:45:15 +01003250 case MBEDTLS_SSL_CLIENT_HELLO:
3251 ret = mbedtls_ssl_write_client_hello( ssl );
3252 break;
3253
3254 default:
3255#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003256 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003257 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3258 else
3259 ret = mbedtls_ssl_handshake_client_step( ssl );
3260#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3261 ret = mbedtls_ssl_handshake_client_step( ssl );
3262#else
3263 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3264#endif
3265 }
Jerry Yub9930e72021-08-06 17:11:51 +08003266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003269 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003270 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003271#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003272 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003273 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003274#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003275
3276#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3277 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3278 ret = mbedtls_ssl_handshake_server_step( ssl );
3279#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003281#endif
3282
Jerry Yue7047812021-09-13 19:26:39 +08003283 if( ret != 0 )
3284 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003285 /* handshake_step return error. And it is same
3286 * with alert_reason.
3287 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003288 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003289 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003290 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003291 goto cleanup;
3292 }
3293 }
3294
3295cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003296 return( ret );
3297}
3298
3299/*
3300 * Perform the SSL handshake
3301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003303{
3304 int ret = 0;
3305
Hanno Beckera817ea42020-10-20 15:20:23 +01003306 /* Sanity checks */
3307
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003308 if( ssl == NULL || ssl->conf == NULL )
3309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3310
Hanno Beckera817ea42020-10-20 15:20:23 +01003311#if defined(MBEDTLS_SSL_PROTO_DTLS)
3312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3313 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3314 {
3315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3316 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3318 }
3319#endif /* MBEDTLS_SSL_PROTO_DTLS */
3320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003322
Hanno Beckera817ea42020-10-20 15:20:23 +01003323 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003324 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003327
3328 if( ret != 0 )
3329 break;
3330 }
3331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
3334 return( ret );
3335}
3336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337#if defined(MBEDTLS_SSL_RENEGOTIATION)
3338#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003339/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003340 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003341 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003342MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003344{
Janos Follath865b3eb2019-12-16 11:46:15 +00003345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003348
3349 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3351 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003352
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003353 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003354 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003356 return( ret );
3357 }
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003360
3361 return( 0 );
3362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003364
3365/*
3366 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 * - any side: calling mbedtls_ssl_renegotiate(),
3368 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3369 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003370 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003371 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003373 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003374int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003375{
Janos Follath865b3eb2019-12-16 11:46:15 +00003376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003379
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003380 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3381 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003382
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003383 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3384 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003386 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003389 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003390 ssl->handshake->out_msg_seq = 1;
3391 else
3392 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003393 }
3394#endif
3395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3397 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003402 return( ret );
3403 }
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003406
3407 return( 0 );
3408}
3409
3410/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003411 * Renegotiate current connection on client,
3412 * or request renegotiation on server
3413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003417
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003418 if( ssl == NULL || ssl->conf == NULL )
3419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003422 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003423 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003424 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003425 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003429
3430 /* Did we already try/start sending HelloRequest? */
3431 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003433
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003434 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003435 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003439 /*
3440 * On client, either start the renegotiation process or,
3441 * if already in progress, continue the handshake
3442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003444 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003445 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003447
Hanno Becker40cdaa12020-02-05 10:48:27 +00003448 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003449 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003451 return( ret );
3452 }
3453 }
3454 else
3455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003459 return( ret );
3460 }
3461 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003463
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003464 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003465}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003467
Gilles Peskine9b562d52018-04-25 20:32:43 +02003468void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003469{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003470 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3471
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003472 if( handshake == NULL )
3473 return;
3474
Brett Warrene0edc842021-08-17 09:53:13 +01003475#if defined(MBEDTLS_ECP_C)
3476#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3477 if ( ssl->handshake->group_list_heap_allocated )
3478 mbedtls_free( (void*) handshake->group_list );
3479 handshake->group_list = NULL;
3480#endif /* MBEDTLS_DEPRECATED_REMOVED */
3481#endif /* MBEDTLS_ECP_C */
3482
Jerry Yuf017ee42022-01-12 15:49:48 +08003483#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3484#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3485 if ( ssl->handshake->sig_algs_heap_allocated )
3486 mbedtls_free( (void*) handshake->sig_algs );
3487 handshake->sig_algs = NULL;
3488#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003489#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3490 if( ssl->handshake->certificate_request_context )
3491 {
3492 mbedtls_free( (void*) handshake->certificate_request_context );
3493 }
3494#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003495#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3496
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003497#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3498 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3499 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003500 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003501 handshake->async_in_progress = 0;
3502 }
3503#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3504
Andrzej Kurek25f27152022-08-17 16:09:31 -04003505#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003506#if defined(MBEDTLS_USE_PSA_CRYPTO)
3507 psa_hash_abort( &handshake->fin_sha256_psa );
3508#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003509 mbedtls_sha256_free( &handshake->fin_sha256 );
3510#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003511#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003512#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003513#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003514 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003515#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003516 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003517#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003518#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520#if defined(MBEDTLS_DHM_C)
3521 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003522#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003523#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003525#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003526#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003527 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003528#if defined(MBEDTLS_SSL_CLI_C)
3529 mbedtls_free( handshake->ecjpake_cache );
3530 handshake->ecjpake_cache = NULL;
3531 handshake->ecjpake_cache_len = 0;
3532#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003533#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003534
Janos Follath4ae5c292016-02-10 11:27:43 +00003535#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3536 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003537 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003539#endif
3540
Gilles Peskineeccd8882020-03-10 12:19:08 +01003541#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003542#if defined(MBEDTLS_USE_PSA_CRYPTO)
3543 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3544 {
3545 /* The maintenance of the external PSK key slot is the
3546 * user's responsibility. */
3547 if( ssl->handshake->psk_opaque_is_internal )
3548 {
3549 psa_destroy_key( ssl->handshake->psk_opaque );
3550 ssl->handshake->psk_opaque_is_internal = 0;
3551 }
3552 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3553 }
Neil Armstronge952a302022-05-03 10:22:14 +02003554#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003555 if( handshake->psk != NULL )
3556 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003557 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003558 mbedtls_free( handshake->psk );
3559 }
Neil Armstronge952a302022-05-03 10:22:14 +02003560#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003561#endif
3562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3564 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003565 /*
3566 * Free only the linked list wrapper, not the keys themselves
3567 * since the belong to the SNI callback
3568 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003569 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003571
Gilles Peskineeccd8882020-03-10 12:19:08 +01003572#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003573 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003574 if( handshake->ecrs_peer_cert != NULL )
3575 {
3576 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3577 mbedtls_free( handshake->ecrs_peer_cert );
3578 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003579#endif
3580
Hanno Becker75173122019-02-06 16:18:31 +00003581#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3582 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3583 mbedtls_pk_free( &handshake->peer_pubkey );
3584#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3585
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003586#if defined(MBEDTLS_SSL_CLI_C) && \
3587 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3588 mbedtls_free( handshake->cookie );
3589#endif /* MBEDTLS_SSL_CLI_C &&
3590 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003591
3592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003593 mbedtls_ssl_flight_free( handshake->flight );
3594 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003595#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003596
Ronald Cronf12b81d2022-03-15 10:42:41 +01003597#if defined(MBEDTLS_ECDH_C) && \
3598 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003599 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003600 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003601#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3602
Ronald Cron6f135e12021-12-08 16:57:54 +01003603#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003604 mbedtls_ssl_transform_free( handshake->transform_handshake );
3605 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003606 mbedtls_free( handshake->transform_earlydata );
3607 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003608#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003609
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003610
3611#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3612 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3613 * processes datagrams and the fact that a datagram is allowed to have
3614 * several records in it, it is possible that the I/O buffers are not
3615 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003616 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3617 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003618#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003619
Jerry Yuba9c7272021-10-30 11:54:10 +08003620 /* mbedtls_platform_zeroize MUST be last one in this function */
3621 mbedtls_platform_zeroize( handshake,
3622 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003623}
3624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003626{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003627 if( session == NULL )
3628 return;
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003631 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003632#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003633
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003634#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003636#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003637
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003638 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003639}
3640
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003641#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003642
3643#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3644#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3645#else
3646#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3647#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3648
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003649#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003650
3651#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3652#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3653#else
3654#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3655#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3656
3657#if defined(MBEDTLS_SSL_ALPN)
3658#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3659#else
3660#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3661#endif /* MBEDTLS_SSL_ALPN */
3662
3663#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3664#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3665#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3666#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3667
3668#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3669 ( (uint32_t) ( \
3670 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3671 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3672 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3673 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3674 0u ) )
3675
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003676static unsigned char ssl_serialized_context_header[] = {
3677 MBEDTLS_VERSION_MAJOR,
3678 MBEDTLS_VERSION_MINOR,
3679 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003680 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3681 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3682 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3683 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3684 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003685};
3686
Paul Bakker5121ce52009-01-03 21:22:43 +00003687/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003688 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003689 *
3690 * The format of the serialized data is:
3691 * (in the presentation language of TLS, RFC 8446 section 3)
3692 *
3693 * // header
3694 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003695 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003696 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003697 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003698 * Note: When updating the format, remember to keep these
3699 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003700 *
3701 * // session sub-structure
3702 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3703 * // transform sub-structure
3704 * uint8 random[64]; // ServerHello.random+ClientHello.random
3705 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3706 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3707 * // fields from ssl_context
3708 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3709 * uint64 in_window_top; // DTLS: last validated record seq_num
3710 * uint64 in_window; // DTLS: bitmask for replay protection
3711 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3712 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3713 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3714 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3715 *
3716 * Note that many fields of the ssl_context or sub-structures are not
3717 * serialized, as they fall in one of the following categories:
3718 *
3719 * 1. forced value (eg in_left must be 0)
3720 * 2. pointer to dynamically-allocated memory (eg session, transform)
3721 * 3. value can be re-derived from other data (eg session keys from MS)
3722 * 4. value was temporary (eg content of input buffer)
3723 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003724 */
3725int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3726 unsigned char *buf,
3727 size_t buf_len,
3728 size_t *olen )
3729{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003730 unsigned char *p = buf;
3731 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003732 size_t session_len;
3733 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003734
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003735 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003736 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3737 * this function's documentation.
3738 *
3739 * These are due to assumptions/limitations in the implementation. Some of
3740 * them are likely to stay (no handshake in progress) some might go away
3741 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003742 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003743 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003744 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003748 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003749 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003750 {
3751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003753 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003754 /* Double-check that sub-structures are indeed ready */
3755 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003756 {
3757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003759 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003760 /* There must be no pending incoming or outgoing data */
3761 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003762 {
3763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003765 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003766 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003767 {
3768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003770 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003771 /* Protocol must be DLTS, not TLS */
3772 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003773 {
3774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003776 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003777 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003778 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003779 {
3780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003782 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003783 /* We must be using an AEAD ciphersuite */
3784 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003785 {
3786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003788 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003789 /* Renegotiation must not be enabled */
3790#if defined(MBEDTLS_SSL_RENEGOTIATION)
3791 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003792 {
3793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003795 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003796#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003797
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003798 /*
3799 * Version and format identifier
3800 */
3801 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003802
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003803 if( used <= buf_len )
3804 {
3805 memcpy( p, ssl_serialized_context_header,
3806 sizeof( ssl_serialized_context_header ) );
3807 p += sizeof( ssl_serialized_context_header );
3808 }
3809
3810 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003811 * Session (length + data)
3812 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003813 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003814 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3815 return( ret );
3816
3817 used += 4 + session_len;
3818 if( used <= buf_len )
3819 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003820 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3821 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003822
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003823 ret = ssl_session_save( ssl->session, 1,
3824 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003825 if( ret != 0 )
3826 return( ret );
3827
3828 p += session_len;
3829 }
3830
3831 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003832 * Transform
3833 */
3834 used += sizeof( ssl->transform->randbytes );
3835 if( used <= buf_len )
3836 {
3837 memcpy( p, ssl->transform->randbytes,
3838 sizeof( ssl->transform->randbytes ) );
3839 p += sizeof( ssl->transform->randbytes );
3840 }
3841
3842#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3843 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3844 if( used <= buf_len )
3845 {
3846 *p++ = ssl->transform->in_cid_len;
3847 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3848 p += ssl->transform->in_cid_len;
3849
3850 *p++ = ssl->transform->out_cid_len;
3851 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3852 p += ssl->transform->out_cid_len;
3853 }
3854#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3855
3856 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003857 * Saved fields from top-level ssl_context structure
3858 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003859 used += 4;
3860 if( used <= buf_len )
3861 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003862 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3863 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003864 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003865
3866#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3867 used += 16;
3868 if( used <= buf_len )
3869 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003870 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3871 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003872
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003873 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3874 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003875 }
3876#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3877
3878#if defined(MBEDTLS_SSL_PROTO_DTLS)
3879 used += 1;
3880 if( used <= buf_len )
3881 {
3882 *p++ = ssl->disable_datagram_packing;
3883 }
3884#endif /* MBEDTLS_SSL_PROTO_DTLS */
3885
Jerry Yuae0b2e22021-10-08 15:21:19 +08003886 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003887 if( used <= buf_len )
3888 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003889 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3890 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003891 }
3892
3893#if defined(MBEDTLS_SSL_PROTO_DTLS)
3894 used += 2;
3895 if( used <= buf_len )
3896 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003897 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3898 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003899 }
3900#endif /* MBEDTLS_SSL_PROTO_DTLS */
3901
3902#if defined(MBEDTLS_SSL_ALPN)
3903 {
3904 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003905 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003906 : 0;
3907
3908 used += 1 + alpn_len;
3909 if( used <= buf_len )
3910 {
3911 *p++ = alpn_len;
3912
3913 if( ssl->alpn_chosen != NULL )
3914 {
3915 memcpy( p, ssl->alpn_chosen, alpn_len );
3916 p += alpn_len;
3917 }
3918 }
3919 }
3920#endif /* MBEDTLS_SSL_ALPN */
3921
3922 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003923 * Done
3924 */
3925 *olen = used;
3926
3927 if( used > buf_len )
3928 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003929
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003930 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3931
Hanno Becker43aefe22020-02-05 10:44:56 +00003932 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003933}
3934
3935/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003936 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003937 *
3938 * This internal version is wrapped by a public function that cleans up in
3939 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003940 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003941MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003942static int ssl_context_load( mbedtls_ssl_context *ssl,
3943 const unsigned char *buf,
3944 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003945{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003946 const unsigned char *p = buf;
3947 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003948 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003950
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003951 /*
3952 * The context should have been freshly setup or reset.
3953 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003954 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003955 * renegotiating, or if the user mistakenly loaded a session first.)
3956 */
3957 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3958 ssl->session != NULL )
3959 {
3960 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3961 }
3962
3963 /*
3964 * We can't check that the config matches the initial one, but we can at
3965 * least check it matches the requirements for serializing.
3966 */
3967 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003968 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3969 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003970#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003971 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003972#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003973 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003974 {
3975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3976 }
3977
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003978 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3979
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003980 /*
3981 * Check version identifier
3982 */
3983 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3985
3986 if( memcmp( p, ssl_serialized_context_header,
3987 sizeof( ssl_serialized_context_header ) ) != 0 )
3988 {
3989 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3990 }
3991 p += sizeof( ssl_serialized_context_header );
3992
3993 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003994 * Session
3995 */
3996 if( (size_t)( end - p ) < 4 )
3997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3998
3999 session_len = ( (size_t) p[0] << 24 ) |
4000 ( (size_t) p[1] << 16 ) |
4001 ( (size_t) p[2] << 8 ) |
4002 ( (size_t) p[3] );
4003 p += 4;
4004
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004005 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004006 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004007 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004008 ssl->session_in = ssl->session;
4009 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004010 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004011
4012 if( (size_t)( end - p ) < session_len )
4013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4014
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004015 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004016 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004017 {
4018 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004019 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004020 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004021
4022 p += session_len;
4023
4024 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004025 * Transform
4026 */
4027
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004028 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004029 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004030 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004031 ssl->transform_in = ssl->transform;
4032 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004033 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004034
4035 /* Read random bytes and populate structure */
4036 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004039 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004040 ssl->session->ciphersuite,
4041 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004042#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004043 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004044#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004045 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4046 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004047 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004048 ssl->conf->endpoint,
4049 ssl );
4050 if( ret != 0 )
4051 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004053 p += sizeof( ssl->transform->randbytes );
4054
4055#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4056 /* Read connection IDs and store them */
4057 if( (size_t)( end - p ) < 1 )
4058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4059
4060 ssl->transform->in_cid_len = *p++;
4061
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004062 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4064
4065 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4066 p += ssl->transform->in_cid_len;
4067
4068 ssl->transform->out_cid_len = *p++;
4069
4070 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4072
4073 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4074 p += ssl->transform->out_cid_len;
4075#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4076
4077 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004078 * Saved fields from top-level ssl_context structure
4079 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004080 if( (size_t)( end - p ) < 4 )
4081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4082
4083 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4084 ( (uint32_t) p[1] << 16 ) |
4085 ( (uint32_t) p[2] << 8 ) |
4086 ( (uint32_t) p[3] );
4087 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004088
4089#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4090 if( (size_t)( end - p ) < 16 )
4091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4092
4093 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4094 ( (uint64_t) p[1] << 48 ) |
4095 ( (uint64_t) p[2] << 40 ) |
4096 ( (uint64_t) p[3] << 32 ) |
4097 ( (uint64_t) p[4] << 24 ) |
4098 ( (uint64_t) p[5] << 16 ) |
4099 ( (uint64_t) p[6] << 8 ) |
4100 ( (uint64_t) p[7] );
4101 p += 8;
4102
4103 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4104 ( (uint64_t) p[1] << 48 ) |
4105 ( (uint64_t) p[2] << 40 ) |
4106 ( (uint64_t) p[3] << 32 ) |
4107 ( (uint64_t) p[4] << 24 ) |
4108 ( (uint64_t) p[5] << 16 ) |
4109 ( (uint64_t) p[6] << 8 ) |
4110 ( (uint64_t) p[7] );
4111 p += 8;
4112#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4113
4114#if defined(MBEDTLS_SSL_PROTO_DTLS)
4115 if( (size_t)( end - p ) < 1 )
4116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4117
4118 ssl->disable_datagram_packing = *p++;
4119#endif /* MBEDTLS_SSL_PROTO_DTLS */
4120
Jerry Yud9a94fe2021-09-28 18:58:59 +08004121 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004123 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4124 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004125
4126#if defined(MBEDTLS_SSL_PROTO_DTLS)
4127 if( (size_t)( end - p ) < 2 )
4128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4129
4130 ssl->mtu = ( p[0] << 8 ) | p[1];
4131 p += 2;
4132#endif /* MBEDTLS_SSL_PROTO_DTLS */
4133
4134#if defined(MBEDTLS_SSL_ALPN)
4135 {
4136 uint8_t alpn_len;
4137 const char **cur;
4138
4139 if( (size_t)( end - p ) < 1 )
4140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4141
4142 alpn_len = *p++;
4143
4144 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4145 {
4146 /* alpn_chosen should point to an item in the configured list */
4147 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4148 {
4149 if( strlen( *cur ) == alpn_len &&
4150 memcmp( p, cur, alpn_len ) == 0 )
4151 {
4152 ssl->alpn_chosen = *cur;
4153 break;
4154 }
4155 }
4156 }
4157
4158 /* can only happen on conf mismatch */
4159 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4161
4162 p += alpn_len;
4163 }
4164#endif /* MBEDTLS_SSL_ALPN */
4165
4166 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004167 * Forced fields from top-level ssl_context structure
4168 *
4169 * Most of them already set to the correct value by mbedtls_ssl_init() and
4170 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4171 */
4172 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004173 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004174
Hanno Becker361b10d2019-08-30 10:42:49 +01004175 /* Adjust pointers for header fields of outgoing records to
4176 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004177 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004178
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004179#if defined(MBEDTLS_SSL_PROTO_DTLS)
4180 ssl->in_epoch = 1;
4181#endif
4182
4183 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4184 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004185 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4186 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004187 if( ssl->handshake != NULL )
4188 {
4189 mbedtls_ssl_handshake_free( ssl );
4190 mbedtls_free( ssl->handshake );
4191 ssl->handshake = NULL;
4192 }
4193
4194 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004195 * Done - should have consumed entire buffer
4196 */
4197 if( p != end )
4198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004199
4200 return( 0 );
4201}
4202
4203/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004204 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004205 */
4206int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4207 const unsigned char *buf,
4208 size_t len )
4209{
4210 int ret = ssl_context_load( context, buf, len );
4211
4212 if( ret != 0 )
4213 mbedtls_ssl_free( context );
4214
4215 return( ret );
4216}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004217#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004218
4219/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004220 * Free an SSL context
4221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004223{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004224 if( ssl == NULL )
4225 return;
4226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004228
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004229 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004230 {
sander-visserb8aa2072020-05-06 22:05:13 +02004231#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4232 size_t out_buf_len = ssl->out_buf_len;
4233#else
4234 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4235#endif
4236
Darryl Greenb33cc762019-11-28 14:29:44 +00004237 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004239 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004240 }
4241
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004242 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 {
sander-visserb8aa2072020-05-06 22:05:13 +02004244#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4245 size_t in_buf_len = ssl->in_buf_len;
4246#else
4247 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4248#endif
4249
Darryl Greenb33cc762019-11-28 14:29:44 +00004250 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004252 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004253 }
4254
Paul Bakker48916f92012-09-16 19:57:18 +00004255 if( ssl->transform )
4256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 mbedtls_ssl_transform_free( ssl->transform );
4258 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004259 }
4260
4261 if( ssl->handshake )
4262 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004263 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4265 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 mbedtls_free( ssl->handshake );
4268 mbedtls_free( ssl->transform_negotiate );
4269 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004270 }
4271
Ronald Cron6f135e12021-12-08 16:57:54 +01004272#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004273 mbedtls_ssl_transform_free( ssl->transform_application );
4274 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004275#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004276
Paul Bakkerc0463502013-02-14 11:19:38 +01004277 if( ssl->session )
4278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 mbedtls_ssl_session_free( ssl->session );
4280 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004281 }
4282
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004283#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004284 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004285 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004286 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004288 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004290
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004291#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004293#endif
4294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004296
Paul Bakker86f04f42013-02-14 11:20:09 +01004297 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004298 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004299}
4300
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004301/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004302 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004303 */
4304void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4305{
4306 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4307}
4308
Gilles Peskineae270bf2021-06-02 00:05:29 +02004309/* The selection should be the same as mbedtls_x509_crt_profile_default in
4310 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004311 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004312 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004313 * about this list.
4314 */
Brett Warrene0edc842021-08-17 09:53:13 +01004315static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004316#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004317 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004318#endif
4319#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004320 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004321#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004322#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004323 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004324#endif
4325#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004326 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004327#endif
4328#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004329 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004330#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004331#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004332 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004333#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004334#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004335 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004336#endif
4337#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004338 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004339#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004340 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004341};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004342
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004343static int ssl_preset_suiteb_ciphersuites[] = {
4344 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4345 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4346 0
4347};
4348
Gilles Peskineeccd8882020-03-10 12:19:08 +01004349#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004350
Jerry Yu909df7b2022-01-22 11:56:27 +08004351/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004352 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004353 * rules SHOULD be upheld.
4354 * - No duplicate entries.
4355 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004356 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004357 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004358 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004359static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004360
Andrzej Kurek25f27152022-08-17 16:09:31 -04004361#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 +08004362 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4363 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004364#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004365 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004366
Andrzej Kurek25f27152022-08-17 16:09:31 -04004367#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 +08004368 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4369 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004370#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004371 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4372
Andrzej Kurek25f27152022-08-17 16:09:31 -04004373#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 +08004374 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4375 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004376#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004377 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004378
Andrzej Kurek25f27152022-08-17 16:09:31 -04004379#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 +08004380 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004381#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 +08004382
Andrzej Kurek25f27152022-08-17 16:09:31 -04004383#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 +08004384 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004385#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 +08004386
Andrzej Kurek25f27152022-08-17 16:09:31 -04004387#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 +08004388 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004389#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 +08004390
Andrzej Kurek25f27152022-08-17 16:09:31 -04004391#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 +08004392 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4393#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4394
Andrzej Kurek25f27152022-08-17 16:09:31 -04004395#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 +08004396 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4397#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4398
Andrzej Kurek25f27152022-08-17 16:09:31 -04004399#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 +08004400 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4401#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4402
Gabor Mezei15b95a62022-05-09 16:37:58 +02004403 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004404};
4405
4406/* NOTICE: see above */
4407#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4408static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004409#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004410#if defined(MBEDTLS_ECDSA_C)
4411 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004412#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004413#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4414 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4415#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004416#if defined(MBEDTLS_RSA_C)
4417 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4418#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004419#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004420#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004421#if defined(MBEDTLS_ECDSA_C)
4422 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004423#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004424#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4425 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4426#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004427#if defined(MBEDTLS_RSA_C)
4428 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4429#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004430#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004431#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004432#if defined(MBEDTLS_ECDSA_C)
4433 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004434#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004435#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4436 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4437#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004438#if defined(MBEDTLS_RSA_C)
4439 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4440#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004441#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004442 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004443};
4444#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4445/* NOTICE: see above */
4446static uint16_t ssl_preset_suiteb_sig_algs[] = {
4447
Andrzej Kurek25f27152022-08-17 16:09:31 -04004448#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 +08004449 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4450 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004451#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004452 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4453
Andrzej Kurek25f27152022-08-17 16:09:31 -04004454#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 +08004455 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4456 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004457#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004458 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004459
Andrzej Kurek25f27152022-08-17 16:09:31 -04004460#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 +08004461 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004462#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 +08004463
Andrzej Kurek25f27152022-08-17 16:09:31 -04004464#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 +08004465 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004466#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004467
Gabor Mezei15b95a62022-05-09 16:37:58 +02004468 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004469};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004470
Jerry Yu909df7b2022-01-22 11:56:27 +08004471/* NOTICE: see above */
4472#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4473static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004474#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004475#if defined(MBEDTLS_ECDSA_C)
4476 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004477#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004478#if defined(MBEDTLS_RSA_C)
4479 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4480#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004481#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004482#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004483#if defined(MBEDTLS_ECDSA_C)
4484 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004485#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004486#if defined(MBEDTLS_RSA_C)
4487 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4488#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004489#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004490 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004491};
Jerry Yu909df7b2022-01-22 11:56:27 +08004492#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4493
Jerry Yu1a8b4812022-01-20 17:56:50 +08004494#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004495
Brett Warrene0edc842021-08-17 09:53:13 +01004496static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004497#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004498 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004499#endif
4500#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004501 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004502#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004503 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004504};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004505
Jerry Yu1a8b4812022-01-20 17:56:50 +08004506#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004507/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004508 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004509MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004510static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004511{
4512 size_t i, j;
4513 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004514
Gabor Mezei15b95a62022-05-09 16:37:58 +02004515 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004516 {
Jerry Yuf377d642022-01-25 10:43:59 +08004517 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004518 {
Jerry Yuf377d642022-01-25 10:43:59 +08004519 if( sig_algs[i] != sig_algs[j] )
4520 continue;
4521 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4522 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4523 sig_algs[i], j, i );
4524 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004525 }
4526 }
4527 return( ret );
4528}
4529
4530#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4531
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004532/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004533 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004534 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004535int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004536 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004537{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004538#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004540#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004541
Jerry Yu1a8b4812022-01-20 17:56:50 +08004542#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004543 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004544 {
4545 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4546 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4547 }
4548
Jerry Yuf377d642022-01-25 10:43:59 +08004549 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004550 {
4551 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4552 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4553 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004554
4555#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004556 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004557 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004558 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004559 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4560 }
4561
Jerry Yuf377d642022-01-25 10:43:59 +08004562 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004563 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004564 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004565 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4566 }
4567#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004568#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4569
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004570 /* Use the functions here so that they are covered in tests,
4571 * but otherwise access member directly for efficiency */
4572 mbedtls_ssl_conf_endpoint( conf, endpoint );
4573 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004574
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004575 /*
4576 * Things that are common to all presets
4577 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004578#if defined(MBEDTLS_SSL_CLI_C)
4579 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4580 {
4581 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4582#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4583 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4584#endif
4585 }
4586#endif
4587
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004588#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4589 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4590#endif
4591
4592#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4593 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4594#endif
4595
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004596#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004597 conf->f_cookie_write = ssl_cookie_write_dummy;
4598 conf->f_cookie_check = ssl_cookie_check_dummy;
4599#endif
4600
4601#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4602 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4603#endif
4604
Janos Follath088ce432017-04-10 12:42:31 +01004605#if defined(MBEDTLS_SSL_SRV_C)
4606 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004607 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004608#endif
4609
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004610#if defined(MBEDTLS_SSL_PROTO_DTLS)
4611 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4612 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4613#endif
4614
4615#if defined(MBEDTLS_SSL_RENEGOTIATION)
4616 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004617 memset( conf->renego_period, 0x00, 2 );
4618 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004619#endif
4620
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004621#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004622 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4623 {
4624 const unsigned char dhm_p[] =
4625 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4626 const unsigned char dhm_g[] =
4627 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004628
Hanno Beckere2defad2021-07-24 05:59:17 +01004629 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4630 dhm_p, sizeof( dhm_p ),
4631 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4632 {
4633 return( ret );
4634 }
4635 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004636#endif
4637
Ronald Cron6f135e12021-12-08 16:57:54 +01004638#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004639 /*
4640 * Allow all TLS 1.3 key exchange modes by default.
4641 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004642 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004643#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004644
XiaokangQian4d3a6042022-04-21 13:46:17 +00004645 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4646 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004647#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004648 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004649 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004650#else
XiaokangQian060d8672022-04-21 09:24:56 +00004651 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004652#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004653 }
4654 else
4655 {
4656#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4657 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4658 {
4659 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4660 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4661 }
4662 else
4663 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4664 {
4665 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4666 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4667 }
4668#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4669 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4670 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4671#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4672 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4673 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4674#else
4675 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4676#endif
4677 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004678
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004679 /*
4680 * Preset-specific defaults
4681 */
4682 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004683 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004684 /*
4685 * NSA Suite B
4686 */
4687 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004688
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004689 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004690
4691#if defined(MBEDTLS_X509_CRT_PARSE_C)
4692 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004693#endif
4694
Gilles Peskineeccd8882020-03-10 12:19:08 +01004695#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004696#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4697 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4698 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4699 else
4700#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4701 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004702#endif
4703
Brett Warrene0edc842021-08-17 09:53:13 +01004704#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4705 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004706#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004707 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004708 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004709
4710 /*
4711 * Default
4712 */
4713 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004714
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004715 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004716
4717#if defined(MBEDTLS_X509_CRT_PARSE_C)
4718 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4719#endif
4720
Gilles Peskineeccd8882020-03-10 12:19:08 +01004721#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004722#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4723 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4724 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4725 else
4726#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4727 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004728#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004729
Brett Warrene0edc842021-08-17 09:53:13 +01004730#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4731 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004732#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004733 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004734
4735#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4736 conf->dhm_min_bitlen = 1024;
4737#endif
4738 }
4739
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004740 return( 0 );
4741}
4742
4743/*
4744 * Free mbedtls_ssl_config
4745 */
4746void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4747{
4748#if defined(MBEDTLS_DHM_C)
4749 mbedtls_mpi_free( &conf->dhm_P );
4750 mbedtls_mpi_free( &conf->dhm_G );
4751#endif
4752
Gilles Peskineeccd8882020-03-10 12:19:08 +01004753#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004754#if defined(MBEDTLS_USE_PSA_CRYPTO)
4755 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4756 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004757 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4758 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004759#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004760 if( conf->psk != NULL )
4761 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004762 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004763 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004764 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004765 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004766 }
4767
4768 if( conf->psk_identity != NULL )
4769 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004770 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004771 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004772 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004773 conf->psk_identity_len = 0;
4774 }
4775#endif
4776
4777#if defined(MBEDTLS_X509_CRT_PARSE_C)
4778 ssl_key_cert_free( conf->key_cert );
4779#endif
4780
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004781 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004782}
4783
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004784#if defined(MBEDTLS_PK_C) && \
4785 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004786/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004790{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791#if defined(MBEDTLS_RSA_C)
4792 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4793 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795#if defined(MBEDTLS_ECDSA_C)
4796 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4797 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004800}
4801
Hanno Becker7e5437a2017-04-28 17:15:26 +01004802unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4803{
4804 switch( type ) {
4805 case MBEDTLS_PK_RSA:
4806 return( MBEDTLS_SSL_SIG_RSA );
4807 case MBEDTLS_PK_ECDSA:
4808 case MBEDTLS_PK_ECKEY:
4809 return( MBEDTLS_SSL_SIG_ECDSA );
4810 default:
4811 return( MBEDTLS_SSL_SIG_ANON );
4812 }
4813}
4814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004816{
4817 switch( sig )
4818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819#if defined(MBEDTLS_RSA_C)
4820 case MBEDTLS_SSL_SIG_RSA:
4821 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823#if defined(MBEDTLS_ECDSA_C)
4824 case MBEDTLS_SSL_SIG_ECDSA:
4825 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004826#endif
4827 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004829 }
4830}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004831#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004832
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004833/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004834 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004837{
4838 switch( hash )
4839 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004840#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 case MBEDTLS_SSL_HASH_MD5:
4842 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004843#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004844#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 case MBEDTLS_SSL_HASH_SHA1:
4846 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004847#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004848#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 case MBEDTLS_SSL_HASH_SHA224:
4850 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004851#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004852#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 case MBEDTLS_SSL_HASH_SHA256:
4854 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004855#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004856#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 case MBEDTLS_SSL_HASH_SHA384:
4858 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004859#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004860#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 case MBEDTLS_SSL_HASH_SHA512:
4862 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004863#endif
4864 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004866 }
4867}
4868
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004869/*
4870 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4871 */
4872unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4873{
4874 switch( md )
4875 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004876#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004877 case MBEDTLS_MD_MD5:
4878 return( MBEDTLS_SSL_HASH_MD5 );
4879#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004880#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004881 case MBEDTLS_MD_SHA1:
4882 return( MBEDTLS_SSL_HASH_SHA1 );
4883#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004884#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004885 case MBEDTLS_MD_SHA224:
4886 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004887#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004888#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004889 case MBEDTLS_MD_SHA256:
4890 return( MBEDTLS_SSL_HASH_SHA256 );
4891#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004892#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004893 case MBEDTLS_MD_SHA384:
4894 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004895#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004896#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004897 case MBEDTLS_MD_SHA512:
4898 return( MBEDTLS_SSL_HASH_SHA512 );
4899#endif
4900 default:
4901 return( MBEDTLS_SSL_HASH_NONE );
4902 }
4903}
4904
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004905/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004906 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004907 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004908 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004909int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004910{
Brett Warrene0edc842021-08-17 09:53:13 +01004911 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004912
Brett Warrene0edc842021-08-17 09:53:13 +01004913 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004914 return( -1 );
4915
Brett Warrene0edc842021-08-17 09:53:13 +01004916 for( ; *group_list != 0; group_list++ )
4917 {
4918 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004919 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004920 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004921
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004922 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004923}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004924
4925#if defined(MBEDTLS_ECP_C)
4926/*
4927 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4928 */
4929int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4930{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004931 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004932 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004933
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004934 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004935 return -1;
4936
4937 uint16_t tls_id = grp_info->tls_id;
4938
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004939 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4940}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004941#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943#if defined(MBEDTLS_X509_CRT_PARSE_C)
4944int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4945 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004946 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004947 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004948{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004949 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004950 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004951 const char *ext_oid;
4952 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004955 {
4956 /* Server part of the key exchange */
4957 switch( ciphersuite->key_exchange )
4958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 case MBEDTLS_KEY_EXCHANGE_RSA:
4960 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004961 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004962 break;
4963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4965 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4966 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4967 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004968 break;
4969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4971 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004972 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004973 break;
4974
4975 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 case MBEDTLS_KEY_EXCHANGE_NONE:
4977 case MBEDTLS_KEY_EXCHANGE_PSK:
4978 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4979 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004980 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004981 usage = 0;
4982 }
4983 }
4984 else
4985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4987 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004988 }
4989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004991 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004992 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004993 ret = -1;
4994 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4999 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005000 }
5001 else
5002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5004 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005005 }
5006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005008 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005009 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005010 ret = -1;
5011 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005012
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005013 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005014}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005016
Jerry Yu148165c2021-09-24 23:20:59 +08005017#if defined(MBEDTLS_USE_PSA_CRYPTO)
5018int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5019 const mbedtls_md_type_t md,
5020 unsigned char *dst,
5021 size_t dst_len,
5022 size_t *olen )
5023{
Ronald Cronf6893e12022-01-07 22:09:01 +01005024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5025 psa_hash_operation_t *hash_operation_to_clone;
5026 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5027
Jerry Yu148165c2021-09-24 23:20:59 +08005028 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005029
5030 switch( md )
5031 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005032#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005033 case MBEDTLS_MD_SHA384:
5034 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5035 break;
5036#endif
5037
Andrzej Kurek25f27152022-08-17 16:09:31 -04005038#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005039 case MBEDTLS_MD_SHA256:
5040 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5041 break;
5042#endif
5043
5044 default:
5045 goto exit;
5046 }
5047
5048 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5049 if( status != PSA_SUCCESS )
5050 goto exit;
5051
5052 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5053 if( status != PSA_SUCCESS )
5054 goto exit;
5055
5056exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005057 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005058}
5059#else /* MBEDTLS_USE_PSA_CRYPTO */
5060
Andrzej Kurek25f27152022-08-17 16:09:31 -04005061#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005062MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005063static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5064 unsigned char *dst,
5065 size_t dst_len,
5066 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005067{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005068 int ret;
5069 mbedtls_sha512_context sha512;
5070
5071 if( dst_len < 48 )
5072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5073
5074 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005075 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005076
5077 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5078 {
5079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5080 goto exit;
5081 }
5082
5083 *olen = 48;
5084
5085exit:
5086
5087 mbedtls_sha512_free( &sha512 );
5088 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005089}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005090#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005091
Andrzej Kurek25f27152022-08-17 16:09:31 -04005092#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005093MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005094static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5095 unsigned char *dst,
5096 size_t dst_len,
5097 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005098{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005099 int ret;
5100 mbedtls_sha256_context sha256;
5101
5102 if( dst_len < 32 )
5103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5104
5105 mbedtls_sha256_init( &sha256 );
5106 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005107
Jerry Yu24c0ec32021-09-09 14:21:07 +08005108 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5109 {
5110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5111 goto exit;
5112 }
5113
5114 *olen = 32;
5115
5116exit:
5117
5118 mbedtls_sha256_free( &sha256 );
5119 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005120}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005121#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005122
Jerry Yu000f9762021-09-14 11:12:51 +08005123int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5124 const mbedtls_md_type_t md,
5125 unsigned char *dst,
5126 size_t dst_len,
5127 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005128{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005129 switch( md )
5130 {
5131
Andrzej Kurek25f27152022-08-17 16:09:31 -04005132#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005133 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005134 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005135#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005136
Andrzej Kurek25f27152022-08-17 16:09:31 -04005137#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005138 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005139 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005140#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005141
5142 default:
5143 break;
5144 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005146}
XiaokangQian647719a2021-12-07 09:16:29 +00005147
Jerry Yu148165c2021-09-24 23:20:59 +08005148#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005149
Gabor Mezei078e8032022-04-27 21:17:56 +02005150#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5151/* mbedtls_ssl_parse_sig_alg_ext()
5152 *
5153 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5154 * value (TLS 1.3 RFC8446):
5155 * enum {
5156 * ....
5157 * ecdsa_secp256r1_sha256( 0x0403 ),
5158 * ecdsa_secp384r1_sha384( 0x0503 ),
5159 * ecdsa_secp521r1_sha512( 0x0603 ),
5160 * ....
5161 * } SignatureScheme;
5162 *
5163 * struct {
5164 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5165 * } SignatureSchemeList;
5166 *
5167 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5168 * value (TLS 1.2 RFC5246):
5169 * enum {
5170 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5171 * sha512(6), (255)
5172 * } HashAlgorithm;
5173 *
5174 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5175 * SignatureAlgorithm;
5176 *
5177 * struct {
5178 * HashAlgorithm hash;
5179 * SignatureAlgorithm signature;
5180 * } SignatureAndHashAlgorithm;
5181 *
5182 * SignatureAndHashAlgorithm
5183 * supported_signature_algorithms<2..2^16-2>;
5184 *
5185 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5186 * generalization of the TLS 1.2 signature algorithm extension.
5187 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5188 * `SignatureScheme` field of TLS 1.3
5189 *
5190 */
5191int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5192 const unsigned char *buf,
5193 const unsigned char *end )
5194{
5195 const unsigned char *p = buf;
5196 size_t supported_sig_algs_len = 0;
5197 const unsigned char *supported_sig_algs_end;
5198 uint16_t sig_alg;
5199 uint32_t common_idx = 0;
5200
5201 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5202 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5203 p += 2;
5204
5205 memset( ssl->handshake->received_sig_algs, 0,
5206 sizeof(ssl->handshake->received_sig_algs) );
5207
5208 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5209 supported_sig_algs_end = p + supported_sig_algs_len;
5210 while( p < supported_sig_algs_end )
5211 {
5212 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5213 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5214 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005215 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5216 sig_alg,
5217 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005219 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005220 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5221 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5222 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005223 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005224 }
5225#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005226
Jerry Yuf3b46b52022-06-19 16:52:27 +08005227 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5228 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005229
5230 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5231 {
5232 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5233 common_idx += 1;
5234 }
5235 }
5236 /* Check that we consumed all the message. */
5237 if( p != end )
5238 {
5239 MBEDTLS_SSL_DEBUG_MSG( 1,
5240 ( "Signature algorithms extension length misaligned" ) );
5241 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5242 MBEDTLS_ERR_SSL_DECODE_ERROR );
5243 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5244 }
5245
5246 if( common_idx == 0 )
5247 {
5248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5249 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5250 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5251 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5252 }
5253
Gabor Mezei15b95a62022-05-09 16:37:58 +02005254 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005255 return( 0 );
5256}
5257
5258#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5259
Jerry Yudc7bd172022-02-17 13:44:15 +08005260#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5261
5262#if defined(MBEDTLS_USE_PSA_CRYPTO)
5263
5264static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5265 mbedtls_svc_key_id_t key,
5266 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005267 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005268 const unsigned char* seed, size_t seed_length,
5269 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005270 const unsigned char* other_secret,
5271 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005272 size_t capacity )
5273{
5274 psa_status_t status;
5275
5276 status = psa_key_derivation_setup( derivation, alg );
5277 if( status != PSA_SUCCESS )
5278 return( status );
5279
5280 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5281 {
5282 status = psa_key_derivation_input_bytes( derivation,
5283 PSA_KEY_DERIVATION_INPUT_SEED,
5284 seed, seed_length );
5285 if( status != PSA_SUCCESS )
5286 return( status );
5287
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005288 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005289 {
5290 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005291 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5292 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005293 if( status != PSA_SUCCESS )
5294 return( status );
5295 }
5296
Jerry Yudc7bd172022-02-17 13:44:15 +08005297 if( mbedtls_svc_key_id_is_null( key ) )
5298 {
5299 status = psa_key_derivation_input_bytes(
5300 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005301 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005302 }
5303 else
5304 {
5305 status = psa_key_derivation_input_key(
5306 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5307 }
5308 if( status != PSA_SUCCESS )
5309 return( status );
5310
5311 status = psa_key_derivation_input_bytes( derivation,
5312 PSA_KEY_DERIVATION_INPUT_LABEL,
5313 label, label_length );
5314 if( status != PSA_SUCCESS )
5315 return( status );
5316 }
5317 else
5318 {
5319 return( PSA_ERROR_NOT_SUPPORTED );
5320 }
5321
5322 status = psa_key_derivation_set_capacity( derivation, capacity );
5323 if( status != PSA_SUCCESS )
5324 return( status );
5325
5326 return( PSA_SUCCESS );
5327}
5328
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005329MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005330static int tls_prf_generic( mbedtls_md_type_t md_type,
5331 const unsigned char *secret, size_t slen,
5332 const char *label,
5333 const unsigned char *random, size_t rlen,
5334 unsigned char *dstbuf, size_t dlen )
5335{
5336 psa_status_t status;
5337 psa_algorithm_t alg;
5338 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5339 psa_key_derivation_operation_t derivation =
5340 PSA_KEY_DERIVATION_OPERATION_INIT;
5341
5342 if( md_type == MBEDTLS_MD_SHA384 )
5343 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5344 else
5345 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5346
5347 /* Normally a "secret" should be long enough to be impossible to
5348 * find by brute force, and in particular should not be empty. But
5349 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5350 * and for this use case it makes sense to have a 0-length "secret".
5351 * Since the key API doesn't allow importing a key of length 0,
5352 * keep master_key=0, which setup_psa_key_derivation() understands
5353 * to mean a 0-length "secret" input. */
5354 if( slen != 0 )
5355 {
5356 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5357 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5358 psa_set_key_algorithm( &key_attributes, alg );
5359 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5360
5361 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5362 if( status != PSA_SUCCESS )
5363 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5364 }
5365
5366 status = setup_psa_key_derivation( &derivation,
5367 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005368 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005369 random, rlen,
5370 (unsigned char const *) label,
5371 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005372 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005373 dlen );
5374 if( status != PSA_SUCCESS )
5375 {
5376 psa_key_derivation_abort( &derivation );
5377 psa_destroy_key( master_key );
5378 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5379 }
5380
5381 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5382 if( status != PSA_SUCCESS )
5383 {
5384 psa_key_derivation_abort( &derivation );
5385 psa_destroy_key( master_key );
5386 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5387 }
5388
5389 status = psa_key_derivation_abort( &derivation );
5390 if( status != PSA_SUCCESS )
5391 {
5392 psa_destroy_key( master_key );
5393 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5394 }
5395
5396 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5397 status = psa_destroy_key( master_key );
5398 if( status != PSA_SUCCESS )
5399 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5400
5401 return( 0 );
5402}
5403
5404#else /* MBEDTLS_USE_PSA_CRYPTO */
5405
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005406MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005407static int tls_prf_generic( mbedtls_md_type_t md_type,
5408 const unsigned char *secret, size_t slen,
5409 const char *label,
5410 const unsigned char *random, size_t rlen,
5411 unsigned char *dstbuf, size_t dlen )
5412{
5413 size_t nb;
5414 size_t i, j, k, md_len;
5415 unsigned char *tmp;
5416 size_t tmp_len = 0;
5417 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5418 const mbedtls_md_info_t *md_info;
5419 mbedtls_md_context_t md_ctx;
5420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5421
5422 mbedtls_md_init( &md_ctx );
5423
5424 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5426
5427 md_len = mbedtls_md_get_size( md_info );
5428
5429 tmp_len = md_len + strlen( label ) + rlen;
5430 tmp = mbedtls_calloc( 1, tmp_len );
5431 if( tmp == NULL )
5432 {
5433 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5434 goto exit;
5435 }
5436
5437 nb = strlen( label );
5438 memcpy( tmp + md_len, label, nb );
5439 memcpy( tmp + md_len + nb, random, rlen );
5440 nb += rlen;
5441
5442 /*
5443 * Compute P_<hash>(secret, label + random)[0..dlen]
5444 */
5445 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5446 goto exit;
5447
5448 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5449 if( ret != 0 )
5450 goto exit;
5451 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5452 if( ret != 0 )
5453 goto exit;
5454 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5455 if( ret != 0 )
5456 goto exit;
5457
5458 for( i = 0; i < dlen; i += md_len )
5459 {
5460 ret = mbedtls_md_hmac_reset ( &md_ctx );
5461 if( ret != 0 )
5462 goto exit;
5463 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5464 if( ret != 0 )
5465 goto exit;
5466 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5467 if( ret != 0 )
5468 goto exit;
5469
5470 ret = mbedtls_md_hmac_reset ( &md_ctx );
5471 if( ret != 0 )
5472 goto exit;
5473 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5474 if( ret != 0 )
5475 goto exit;
5476 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5477 if( ret != 0 )
5478 goto exit;
5479
5480 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5481
5482 for( j = 0; j < k; j++ )
5483 dstbuf[i + j] = h_i[j];
5484 }
5485
5486exit:
5487 mbedtls_md_free( &md_ctx );
5488
5489 mbedtls_platform_zeroize( tmp, tmp_len );
5490 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5491
5492 mbedtls_free( tmp );
5493
5494 return( ret );
5495}
5496#endif /* MBEDTLS_USE_PSA_CRYPTO */
5497
Andrzej Kurek25f27152022-08-17 16:09:31 -04005498#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005499MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005500static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5501 const char *label,
5502 const unsigned char *random, size_t rlen,
5503 unsigned char *dstbuf, size_t dlen )
5504{
5505 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5506 label, random, rlen, dstbuf, dlen ) );
5507}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005508#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005509
Andrzej Kurek25f27152022-08-17 16:09:31 -04005510#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005511MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005512static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5513 const char *label,
5514 const unsigned char *random, size_t rlen,
5515 unsigned char *dstbuf, size_t dlen )
5516{
5517 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5518 label, random, rlen, dstbuf, dlen ) );
5519}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005520#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005521
Jerry Yuf009d862022-02-17 14:01:37 +08005522/*
5523 * Set appropriate PRF function and other SSL / TLS1.2 functions
5524 *
5525 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005526 * - hash associated with the ciphersuite (only used by TLS 1.2)
5527 *
5528 * Outputs:
5529 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5530 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005531MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005532static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005533 mbedtls_md_type_t hash )
5534{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005535#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005536 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005537 {
5538 handshake->tls_prf = tls_prf_sha384;
5539 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5540 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5541 }
5542 else
5543#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005544#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005545 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005546 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005547 handshake->tls_prf = tls_prf_sha256;
5548 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5549 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5550 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005551#else
Jerry Yuf009d862022-02-17 14:01:37 +08005552 {
Jerry Yuf009d862022-02-17 14:01:37 +08005553 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005554 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5556 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005557#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005558
5559 return( 0 );
5560}
Jerry Yud6ab2352022-02-17 14:03:43 +08005561
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005562/*
5563 * Compute master secret if needed
5564 *
5565 * Parameters:
5566 * [in/out] handshake
5567 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5568 * (PSA-PSK) ciphersuite_info, psk_opaque
5569 * [out] premaster (cleared)
5570 * [out] master
5571 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5572 * debug: conf->f_dbg, conf->p_dbg
5573 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005574 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005575 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005576MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005577static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5578 unsigned char *master,
5579 const mbedtls_ssl_context *ssl )
5580{
5581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5582
5583 /* cf. RFC 5246, Section 8.1:
5584 * "The master secret is always exactly 48 bytes in length." */
5585 size_t const master_secret_len = 48;
5586
5587#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5588 unsigned char session_hash[48];
5589#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5590
5591 /* The label for the KDF used for key expansion.
5592 * This is either "master secret" or "extended master secret"
5593 * depending on whether the Extended Master Secret extension
5594 * is used. */
5595 char const *lbl = "master secret";
5596
Przemek Stekielae4ed302022-04-05 17:15:55 +02005597 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005598 * - If the Extended Master Secret extension is not used,
5599 * this is ClientHello.Random + ServerHello.Random
5600 * (see Sect. 8.1 in RFC 5246).
5601 * - If the Extended Master Secret extension is used,
5602 * this is the transcript of the handshake so far.
5603 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005604 unsigned char const *seed = handshake->randbytes;
5605 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005606
5607#if !defined(MBEDTLS_DEBUG_C) && \
5608 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5609 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5610 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5611 ssl = NULL; /* make sure we don't use it except for those cases */
5612 (void) ssl;
5613#endif
5614
5615 if( handshake->resume != 0 )
5616 {
5617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5618 return( 0 );
5619 }
5620
5621#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5622 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5623 {
5624 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005625 seed = session_hash;
5626 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005627
5628 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005629 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005630 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005631#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005632
Przemek Stekiel99114f32022-04-22 11:20:09 +02005633#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005634 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005635 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005636 {
5637 /* Perform PSK-to-MS expansion in a single step. */
5638 psa_status_t status;
5639 psa_algorithm_t alg;
5640 mbedtls_svc_key_id_t psk;
5641 psa_key_derivation_operation_t derivation =
5642 PSA_KEY_DERIVATION_OPERATION_INIT;
5643 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5644
5645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5646
5647 psk = mbedtls_ssl_get_opaque_psk( ssl );
5648
5649 if( hash_alg == MBEDTLS_MD_SHA384 )
5650 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5651 else
5652 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5653
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005654 size_t other_secret_len = 0;
5655 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005656
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005657 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005658 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005659 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005660 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005661 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005662 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005663 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005664 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005665 other_secret_len = 48;
5666 other_secret = handshake->premaster + 2;
5667 break;
5668 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005669 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005670 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5671 other_secret = handshake->premaster + 2;
5672 break;
5673 default:
5674 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005675 }
5676
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005677 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005678 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005679 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005680 (unsigned char const *) lbl,
5681 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005682 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005683 master_secret_len );
5684 if( status != PSA_SUCCESS )
5685 {
5686 psa_key_derivation_abort( &derivation );
5687 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5688 }
5689
5690 status = psa_key_derivation_output_bytes( &derivation,
5691 master,
5692 master_secret_len );
5693 if( status != PSA_SUCCESS )
5694 {
5695 psa_key_derivation_abort( &derivation );
5696 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5697 }
5698
5699 status = psa_key_derivation_abort( &derivation );
5700 if( status != PSA_SUCCESS )
5701 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5702 }
5703 else
5704#endif
5705 {
5706 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005707 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005708 master,
5709 master_secret_len );
5710 if( ret != 0 )
5711 {
5712 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5713 return( ret );
5714 }
5715
5716 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5717 handshake->premaster,
5718 handshake->pmslen );
5719
5720 mbedtls_platform_zeroize( handshake->premaster,
5721 sizeof(handshake->premaster) );
5722 }
5723
5724 return( 0 );
5725}
5726
Jerry Yud62f87e2022-02-17 14:09:02 +08005727int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5728{
5729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5730 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5731 ssl->handshake->ciphersuite_info;
5732
5733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5734
5735 /* Set PRF, calc_verify and calc_finished function pointers */
5736 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005737 ciphersuite_info->mac );
5738 if( ret != 0 )
5739 {
5740 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5741 return( ret );
5742 }
5743
5744 /* Compute master secret if needed */
5745 ret = ssl_compute_master( ssl->handshake,
5746 ssl->session_negotiate->master,
5747 ssl );
5748 if( ret != 0 )
5749 {
5750 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5751 return( ret );
5752 }
5753
5754 /* Swap the client and server random values:
5755 * - MS derivation wanted client+server (RFC 5246 8.1)
5756 * - key derivation wants server+client (RFC 5246 6.3) */
5757 {
5758 unsigned char tmp[64];
5759 memcpy( tmp, ssl->handshake->randbytes, 64 );
5760 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5761 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5762 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5763 }
5764
5765 /* Populate transform structure */
5766 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5767 ssl->session_negotiate->ciphersuite,
5768 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005769#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005770 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005771#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005772 ssl->handshake->tls_prf,
5773 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005774 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005775 ssl->conf->endpoint,
5776 ssl );
5777 if( ret != 0 )
5778 {
5779 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5780 return( ret );
5781 }
5782
5783 /* We no longer need Server/ClientHello.random values */
5784 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5785 sizeof( ssl->handshake->randbytes ) );
5786
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5788
5789 return( 0 );
5790}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005791
Ronald Cron4dcbca92022-03-07 10:21:40 +01005792int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5793{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005794 switch( md )
5795 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005796#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005797 case MBEDTLS_SSL_HASH_SHA384:
5798 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5799 break;
5800#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005801#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005802 case MBEDTLS_SSL_HASH_SHA256:
5803 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5804 break;
5805#endif
5806 default:
5807 return( -1 );
5808 }
5809
Ronald Cronc2f13a02022-03-07 10:25:24 +01005810 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005811}
5812
Andrzej Kurek25f27152022-08-17 16:09:31 -04005813#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005814void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5815 unsigned char *hash,
5816 size_t *hlen )
5817{
5818#if defined(MBEDTLS_USE_PSA_CRYPTO)
5819 size_t hash_size;
5820 psa_status_t status;
5821 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5822
5823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5824 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5825 if( status != PSA_SUCCESS )
5826 {
5827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5828 return;
5829 }
5830
5831 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5832 if( status != PSA_SUCCESS )
5833 {
5834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5835 return;
5836 }
5837
5838 *hlen = 32;
5839 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5841#else
5842 mbedtls_sha256_context sha256;
5843
5844 mbedtls_sha256_init( &sha256 );
5845
5846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5847
5848 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5849 mbedtls_sha256_finish( &sha256, hash );
5850
5851 *hlen = 32;
5852
5853 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5855
5856 mbedtls_sha256_free( &sha256 );
5857#endif /* MBEDTLS_USE_PSA_CRYPTO */
5858 return;
5859}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005860#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005861
Andrzej Kurek25f27152022-08-17 16:09:31 -04005862#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005863void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5864 unsigned char *hash,
5865 size_t *hlen )
5866{
5867#if defined(MBEDTLS_USE_PSA_CRYPTO)
5868 size_t hash_size;
5869 psa_status_t status;
5870 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5871
5872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5873 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5874 if( status != PSA_SUCCESS )
5875 {
5876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5877 return;
5878 }
5879
5880 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5881 if( status != PSA_SUCCESS )
5882 {
5883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5884 return;
5885 }
5886
5887 *hlen = 48;
5888 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5890#else
5891 mbedtls_sha512_context sha512;
5892
5893 mbedtls_sha512_init( &sha512 );
5894
5895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5896
Andrzej Kureka242e832022-08-11 10:03:14 -04005897 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005898 mbedtls_sha512_finish( &sha512, hash );
5899
5900 *hlen = 48;
5901
5902 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5904
5905 mbedtls_sha512_free( &sha512 );
5906#endif /* MBEDTLS_USE_PSA_CRYPTO */
5907 return;
5908}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005909#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005910
Neil Armstrong80f6f322022-05-03 17:56:38 +02005911#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5912 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005913int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5914{
5915 unsigned char *p = ssl->handshake->premaster;
5916 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5917 const unsigned char *psk = NULL;
5918 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005919 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005920
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005921 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005922 {
5923 /*
5924 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005925 * checked before calling this function.
5926 *
5927 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005928 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005929 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005930 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5931 {
5932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5934 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005935 }
5936
5937 /*
5938 * PMS = struct {
5939 * opaque other_secret<0..2^16-1>;
5940 * opaque psk<0..2^16-1>;
5941 * };
5942 * with "other_secret" depending on the particular key exchange
5943 */
5944#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5945 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5946 {
5947 if( end - p < 2 )
5948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5949
5950 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5951 p += 2;
5952
5953 if( end < p || (size_t)( end - p ) < psk_len )
5954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5955
5956 memset( p, 0, psk_len );
5957 p += psk_len;
5958 }
5959 else
5960#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5961#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5962 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5963 {
5964 /*
5965 * other_secret already set by the ClientKeyExchange message,
5966 * and is 48 bytes long
5967 */
5968 if( end - p < 2 )
5969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5970
5971 *p++ = 0;
5972 *p++ = 48;
5973 p += 48;
5974 }
5975 else
5976#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5977#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5978 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5979 {
5980 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5981 size_t len;
5982
5983 /* Write length only when we know the actual value */
5984 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5985 p + 2, end - ( p + 2 ), &len,
5986 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5987 {
5988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5989 return( ret );
5990 }
5991 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5992 p += 2 + len;
5993
5994 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5995 }
5996 else
5997#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02005998#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005999 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6000 {
6001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6002 size_t zlen;
6003
6004 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6005 p + 2, end - ( p + 2 ),
6006 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6007 {
6008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6009 return( ret );
6010 }
6011
6012 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6013 p += 2 + zlen;
6014
6015 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6016 MBEDTLS_DEBUG_ECDH_Z );
6017 }
6018 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006019#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006020 {
6021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6023 }
6024
6025 /* opaque psk<0..2^16-1>; */
6026 if( end - p < 2 )
6027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6028
6029 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6030 p += 2;
6031
6032 if( end < p || (size_t)( end - p ) < psk_len )
6033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6034
6035 memcpy( p, psk, psk_len );
6036 p += psk_len;
6037
6038 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6039
6040 return( 0 );
6041}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006042#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006043
6044#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006045MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006046static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6047
6048#if defined(MBEDTLS_SSL_PROTO_DTLS)
6049int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6050{
6051 /* If renegotiation is not enforced, retransmit until we would reach max
6052 * timeout if we were using the usual handshake doubling scheme */
6053 if( ssl->conf->renego_max_records < 0 )
6054 {
6055 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6056 unsigned char doublings = 1;
6057
6058 while( ratio != 0 )
6059 {
6060 ++doublings;
6061 ratio >>= 1;
6062 }
6063
6064 if( ++ssl->renego_records_seen > doublings )
6065 {
6066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6067 return( 0 );
6068 }
6069 }
6070
6071 return( ssl_write_hello_request( ssl ) );
6072}
6073#endif
6074#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006075
Jerry Yud9526692022-02-17 14:23:47 +08006076/*
6077 * Handshake functions
6078 */
6079#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6080/* No certificate support -> dummy functions */
6081int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6082{
6083 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6084 ssl->handshake->ciphersuite_info;
6085
6086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6087
6088 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6089 {
6090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6091 ssl->state++;
6092 return( 0 );
6093 }
6094
6095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6097}
6098
6099int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6100{
6101 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6102 ssl->handshake->ciphersuite_info;
6103
6104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6105
6106 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6107 {
6108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6109 ssl->state++;
6110 return( 0 );
6111 }
6112
6113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6115}
6116
6117#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6118/* Some certificate support -> implement write and parse */
6119
6120int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6121{
6122 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6123 size_t i, n;
6124 const mbedtls_x509_crt *crt;
6125 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6126 ssl->handshake->ciphersuite_info;
6127
6128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6129
6130 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6131 {
6132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6133 ssl->state++;
6134 return( 0 );
6135 }
6136
6137#if defined(MBEDTLS_SSL_CLI_C)
6138 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6139 {
6140 if( ssl->handshake->client_auth == 0 )
6141 {
6142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6143 ssl->state++;
6144 return( 0 );
6145 }
6146 }
6147#endif /* MBEDTLS_SSL_CLI_C */
6148#if defined(MBEDTLS_SSL_SRV_C)
6149 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6150 {
6151 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6152 {
6153 /* Should never happen because we shouldn't have picked the
6154 * ciphersuite if we don't have a certificate. */
6155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6156 }
6157 }
6158#endif
6159
6160 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6161
6162 /*
6163 * 0 . 0 handshake type
6164 * 1 . 3 handshake length
6165 * 4 . 6 length of all certs
6166 * 7 . 9 length of cert. 1
6167 * 10 . n-1 peer certificate
6168 * n . n+2 length of cert. 2
6169 * n+3 . ... upper level cert, etc.
6170 */
6171 i = 7;
6172 crt = mbedtls_ssl_own_cert( ssl );
6173
6174 while( crt != NULL )
6175 {
6176 n = crt->raw.len;
6177 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6178 {
6179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6180 " > %" MBEDTLS_PRINTF_SIZET,
6181 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6182 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6183 }
6184
6185 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6186 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6187 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6188
6189 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6190 i += n; crt = crt->next;
6191 }
6192
6193 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6194 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6195 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6196
6197 ssl->out_msglen = i;
6198 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6199 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6200
6201 ssl->state++;
6202
6203 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6204 {
6205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6206 return( ret );
6207 }
6208
6209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6210
6211 return( ret );
6212}
6213
6214#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6215
6216#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006217MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006218static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6219 unsigned char *crt_buf,
6220 size_t crt_buf_len )
6221{
6222 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6223
6224 if( peer_crt == NULL )
6225 return( -1 );
6226
6227 if( peer_crt->raw.len != crt_buf_len )
6228 return( -1 );
6229
6230 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6231}
6232#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006233MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006234static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6235 unsigned char *crt_buf,
6236 size_t crt_buf_len )
6237{
6238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6239 unsigned char const * const peer_cert_digest =
6240 ssl->session->peer_cert_digest;
6241 mbedtls_md_type_t const peer_cert_digest_type =
6242 ssl->session->peer_cert_digest_type;
6243 mbedtls_md_info_t const * const digest_info =
6244 mbedtls_md_info_from_type( peer_cert_digest_type );
6245 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6246 size_t digest_len;
6247
6248 if( peer_cert_digest == NULL || digest_info == NULL )
6249 return( -1 );
6250
6251 digest_len = mbedtls_md_get_size( digest_info );
6252 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6253 return( -1 );
6254
6255 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6256 if( ret != 0 )
6257 return( -1 );
6258
6259 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6260}
6261#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6262#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6263
6264/*
6265 * Once the certificate message is read, parse it into a cert chain and
6266 * perform basic checks, but leave actual verification to the caller
6267 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006268MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006269static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6270 mbedtls_x509_crt *chain )
6271{
6272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6273#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6274 int crt_cnt=0;
6275#endif
6276 size_t i, n;
6277 uint8_t alert;
6278
6279 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6280 {
6281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6283 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6284 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6285 }
6286
6287 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6288 {
6289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6290 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6291 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6292 }
6293
6294 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6295 {
6296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6297 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6298 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6299 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6300 }
6301
6302 i = mbedtls_ssl_hs_hdr_len( ssl );
6303
6304 /*
6305 * Same message structure as in mbedtls_ssl_write_certificate()
6306 */
6307 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6308
6309 if( ssl->in_msg[i] != 0 ||
6310 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6311 {
6312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6313 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6314 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6315 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6316 }
6317
6318 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6319 i += 3;
6320
6321 /* Iterate through and parse the CRTs in the provided chain. */
6322 while( i < ssl->in_hslen )
6323 {
6324 /* Check that there's room for the next CRT's length fields. */
6325 if ( i + 3 > ssl->in_hslen ) {
6326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6327 mbedtls_ssl_send_alert_message( ssl,
6328 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6330 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6331 }
6332 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6333 * anything beyond 2**16 ~ 64K. */
6334 if( ssl->in_msg[i] != 0 )
6335 {
6336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6337 mbedtls_ssl_send_alert_message( ssl,
6338 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6339 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6340 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6341 }
6342
6343 /* Read length of the next CRT in the chain. */
6344 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6345 | (unsigned int) ssl->in_msg[i + 2];
6346 i += 3;
6347
6348 if( n < 128 || i + n > ssl->in_hslen )
6349 {
6350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6351 mbedtls_ssl_send_alert_message( ssl,
6352 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6353 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6354 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6355 }
6356
6357 /* Check if we're handling the first CRT in the chain. */
6358#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6359 if( crt_cnt++ == 0 &&
6360 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6361 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6362 {
6363 /* During client-side renegotiation, check that the server's
6364 * end-CRTs hasn't changed compared to the initial handshake,
6365 * mitigating the triple handshake attack. On success, reuse
6366 * the original end-CRT instead of parsing it again. */
6367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6368 if( ssl_check_peer_crt_unchanged( ssl,
6369 &ssl->in_msg[i],
6370 n ) != 0 )
6371 {
6372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6373 mbedtls_ssl_send_alert_message( ssl,
6374 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6375 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6376 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6377 }
6378
6379 /* Now we can safely free the original chain. */
6380 ssl_clear_peer_cert( ssl->session );
6381 }
6382#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6383
6384 /* Parse the next certificate in the chain. */
6385#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6386 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6387#else
6388 /* If we don't need to store the CRT chain permanently, parse
6389 * it in-place from the input buffer instead of making a copy. */
6390 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6391#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6392 switch( ret )
6393 {
6394 case 0: /*ok*/
6395 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6396 /* Ignore certificate with an unknown algorithm: maybe a
6397 prior certificate was already trusted. */
6398 break;
6399
6400 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6401 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6402 goto crt_parse_der_failed;
6403
6404 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6405 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6406 goto crt_parse_der_failed;
6407
6408 default:
6409 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6410 crt_parse_der_failed:
6411 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6412 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6413 return( ret );
6414 }
6415
6416 i += n;
6417 }
6418
6419 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6420 return( 0 );
6421}
6422
6423#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006424MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006425static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6426{
6427 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6428 return( -1 );
6429
6430 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6431 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6432 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6433 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6434 {
Ronald Cron19385882022-06-15 16:26:13 +02006435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006436 return( 0 );
6437 }
6438 return( -1 );
6439}
6440#endif /* MBEDTLS_SSL_SRV_C */
6441
6442/* Check if a certificate message is expected.
6443 * Return either
6444 * - SSL_CERTIFICATE_EXPECTED, or
6445 * - SSL_CERTIFICATE_SKIP
6446 * indicating whether a Certificate message is expected or not.
6447 */
6448#define SSL_CERTIFICATE_EXPECTED 0
6449#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006450MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006451static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6452 int authmode )
6453{
6454 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6455 ssl->handshake->ciphersuite_info;
6456
6457 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6458 return( SSL_CERTIFICATE_SKIP );
6459
6460#if defined(MBEDTLS_SSL_SRV_C)
6461 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6462 {
6463 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6464 return( SSL_CERTIFICATE_SKIP );
6465
6466 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6467 {
6468 ssl->session_negotiate->verify_result =
6469 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6470 return( SSL_CERTIFICATE_SKIP );
6471 }
6472 }
6473#else
6474 ((void) authmode);
6475#endif /* MBEDTLS_SSL_SRV_C */
6476
6477 return( SSL_CERTIFICATE_EXPECTED );
6478}
6479
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006480MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006481static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6482 int authmode,
6483 mbedtls_x509_crt *chain,
6484 void *rs_ctx )
6485{
6486 int ret = 0;
6487 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6488 ssl->handshake->ciphersuite_info;
6489 int have_ca_chain = 0;
6490
6491 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6492 void *p_vrfy;
6493
6494 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6495 return( 0 );
6496
6497 if( ssl->f_vrfy != NULL )
6498 {
6499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6500 f_vrfy = ssl->f_vrfy;
6501 p_vrfy = ssl->p_vrfy;
6502 }
6503 else
6504 {
6505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6506 f_vrfy = ssl->conf->f_vrfy;
6507 p_vrfy = ssl->conf->p_vrfy;
6508 }
6509
6510 /*
6511 * Main check: verify certificate
6512 */
6513#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6514 if( ssl->conf->f_ca_cb != NULL )
6515 {
6516 ((void) rs_ctx);
6517 have_ca_chain = 1;
6518
6519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6520 ret = mbedtls_x509_crt_verify_with_ca_cb(
6521 chain,
6522 ssl->conf->f_ca_cb,
6523 ssl->conf->p_ca_cb,
6524 ssl->conf->cert_profile,
6525 ssl->hostname,
6526 &ssl->session_negotiate->verify_result,
6527 f_vrfy, p_vrfy );
6528 }
6529 else
6530#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6531 {
6532 mbedtls_x509_crt *ca_chain;
6533 mbedtls_x509_crl *ca_crl;
6534
6535#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6536 if( ssl->handshake->sni_ca_chain != NULL )
6537 {
6538 ca_chain = ssl->handshake->sni_ca_chain;
6539 ca_crl = ssl->handshake->sni_ca_crl;
6540 }
6541 else
6542#endif
6543 {
6544 ca_chain = ssl->conf->ca_chain;
6545 ca_crl = ssl->conf->ca_crl;
6546 }
6547
6548 if( ca_chain != NULL )
6549 have_ca_chain = 1;
6550
6551 ret = mbedtls_x509_crt_verify_restartable(
6552 chain,
6553 ca_chain, ca_crl,
6554 ssl->conf->cert_profile,
6555 ssl->hostname,
6556 &ssl->session_negotiate->verify_result,
6557 f_vrfy, p_vrfy, rs_ctx );
6558 }
6559
6560 if( ret != 0 )
6561 {
6562 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6563 }
6564
6565#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6566 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6567 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6568#endif
6569
6570 /*
6571 * Secondary checks: always done, but change 'ret' only if it was 0
6572 */
6573
6574#if defined(MBEDTLS_ECP_C)
6575 {
6576 const mbedtls_pk_context *pk = &chain->pk;
6577
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006578 /* If certificate uses an EC key, make sure the curve is OK.
6579 * This is a public key, so it can't be opaque, so can_do() is a good
6580 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006581 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006582 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006583 /* and in the unlikely case the above assumption no longer holds
6584 * we are making sure that pk_ec() here does not return a NULL
6585 */
6586 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6587 if( ec == NULL )
6588 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6591 }
Jerry Yud9526692022-02-17 14:23:47 +08006592
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006593 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6594 {
6595 ssl->session_negotiate->verify_result |=
6596 MBEDTLS_X509_BADCERT_BAD_KEY;
6597
6598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6599 if( ret == 0 )
6600 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6601 }
Jerry Yud9526692022-02-17 14:23:47 +08006602 }
6603 }
6604#endif /* MBEDTLS_ECP_C */
6605
6606 if( mbedtls_ssl_check_cert_usage( chain,
6607 ciphersuite_info,
6608 ! ssl->conf->endpoint,
6609 &ssl->session_negotiate->verify_result ) != 0 )
6610 {
6611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6612 if( ret == 0 )
6613 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6614 }
6615
6616 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6617 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6618 * with details encoded in the verification flags. All other kinds
6619 * of error codes, including those from the user provided f_vrfy
6620 * functions, are treated as fatal and lead to a failure of
6621 * ssl_parse_certificate even if verification was optional. */
6622 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6623 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6624 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6625 {
6626 ret = 0;
6627 }
6628
6629 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6630 {
6631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6632 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6633 }
6634
6635 if( ret != 0 )
6636 {
6637 uint8_t alert;
6638
6639 /* The certificate may have been rejected for several reasons.
6640 Pick one and send the corresponding alert. Which alert to send
6641 may be a subject of debate in some cases. */
6642 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6643 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6644 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6645 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6646 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6647 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6648 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6649 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6650 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6651 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6652 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6653 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6654 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6655 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6656 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6657 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6658 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6659 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6660 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6661 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6662 else
6663 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6664 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6665 alert );
6666 }
6667
6668#if defined(MBEDTLS_DEBUG_C)
6669 if( ssl->session_negotiate->verify_result != 0 )
6670 {
6671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6672 (unsigned int) ssl->session_negotiate->verify_result ) );
6673 }
6674 else
6675 {
6676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6677 }
6678#endif /* MBEDTLS_DEBUG_C */
6679
6680 return( ret );
6681}
6682
6683#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006684MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006685static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6686 unsigned char *start, size_t len )
6687{
6688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6689 /* Remember digest of the peer's end-CRT. */
6690 ssl->session_negotiate->peer_cert_digest =
6691 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6692 if( ssl->session_negotiate->peer_cert_digest == NULL )
6693 {
6694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6695 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6696 mbedtls_ssl_send_alert_message( ssl,
6697 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6698 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6699
6700 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6701 }
6702
6703 ret = mbedtls_md( mbedtls_md_info_from_type(
6704 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6705 start, len,
6706 ssl->session_negotiate->peer_cert_digest );
6707
6708 ssl->session_negotiate->peer_cert_digest_type =
6709 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6710 ssl->session_negotiate->peer_cert_digest_len =
6711 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6712
6713 return( ret );
6714}
6715
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006716MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006717static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6718 unsigned char *start, size_t len )
6719{
6720 unsigned char *end = start + len;
6721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6722
6723 /* Make a copy of the peer's raw public key. */
6724 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6725 ret = mbedtls_pk_parse_subpubkey( &start, end,
6726 &ssl->handshake->peer_pubkey );
6727 if( ret != 0 )
6728 {
6729 /* We should have parsed the public key before. */
6730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6731 }
6732
6733 return( 0 );
6734}
6735#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6736
6737int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6738{
6739 int ret = 0;
6740 int crt_expected;
6741#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6742 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6743 ? ssl->handshake->sni_authmode
6744 : ssl->conf->authmode;
6745#else
6746 const int authmode = ssl->conf->authmode;
6747#endif
6748 void *rs_ctx = NULL;
6749 mbedtls_x509_crt *chain = NULL;
6750
6751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6752
6753 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6754 if( crt_expected == SSL_CERTIFICATE_SKIP )
6755 {
6756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6757 goto exit;
6758 }
6759
6760#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6761 if( ssl->handshake->ecrs_enabled &&
6762 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6763 {
6764 chain = ssl->handshake->ecrs_peer_cert;
6765 ssl->handshake->ecrs_peer_cert = NULL;
6766 goto crt_verify;
6767 }
6768#endif
6769
6770 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6771 {
6772 /* mbedtls_ssl_read_record may have sent an alert already. We
6773 let it decide whether to alert. */
6774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6775 goto exit;
6776 }
6777
6778#if defined(MBEDTLS_SSL_SRV_C)
6779 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6780 {
6781 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6782
6783 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6784 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6785
6786 goto exit;
6787 }
6788#endif /* MBEDTLS_SSL_SRV_C */
6789
6790 /* Clear existing peer CRT structure in case we tried to
6791 * reuse a session but it failed, and allocate a new one. */
6792 ssl_clear_peer_cert( ssl->session_negotiate );
6793
6794 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6795 if( chain == NULL )
6796 {
6797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6798 sizeof( mbedtls_x509_crt ) ) );
6799 mbedtls_ssl_send_alert_message( ssl,
6800 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6801 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6802
6803 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6804 goto exit;
6805 }
6806 mbedtls_x509_crt_init( chain );
6807
6808 ret = ssl_parse_certificate_chain( ssl, chain );
6809 if( ret != 0 )
6810 goto exit;
6811
6812#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6813 if( ssl->handshake->ecrs_enabled)
6814 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6815
6816crt_verify:
6817 if( ssl->handshake->ecrs_enabled)
6818 rs_ctx = &ssl->handshake->ecrs_ctx;
6819#endif
6820
6821 ret = ssl_parse_certificate_verify( ssl, authmode,
6822 chain, rs_ctx );
6823 if( ret != 0 )
6824 goto exit;
6825
6826#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6827 {
6828 unsigned char *crt_start, *pk_start;
6829 size_t crt_len, pk_len;
6830
6831 /* We parse the CRT chain without copying, so
6832 * these pointers point into the input buffer,
6833 * and are hence still valid after freeing the
6834 * CRT chain. */
6835
6836 crt_start = chain->raw.p;
6837 crt_len = chain->raw.len;
6838
6839 pk_start = chain->pk_raw.p;
6840 pk_len = chain->pk_raw.len;
6841
6842 /* Free the CRT structures before computing
6843 * digest and copying the peer's public key. */
6844 mbedtls_x509_crt_free( chain );
6845 mbedtls_free( chain );
6846 chain = NULL;
6847
6848 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6849 if( ret != 0 )
6850 goto exit;
6851
6852 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6853 if( ret != 0 )
6854 goto exit;
6855 }
6856#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6857 /* Pass ownership to session structure. */
6858 ssl->session_negotiate->peer_cert = chain;
6859 chain = NULL;
6860#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6861
6862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6863
6864exit:
6865
6866 if( ret == 0 )
6867 ssl->state++;
6868
6869#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6870 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6871 {
6872 ssl->handshake->ecrs_peer_cert = chain;
6873 chain = NULL;
6874 }
6875#endif
6876
6877 if( chain != NULL )
6878 {
6879 mbedtls_x509_crt_free( chain );
6880 mbedtls_free( chain );
6881 }
6882
6883 return( ret );
6884}
6885#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6886
Andrzej Kurek25f27152022-08-17 16:09:31 -04006887#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006888static void ssl_calc_finished_tls_sha256(
6889 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6890{
6891 int len = 12;
6892 const char *sender;
6893 unsigned char padbuf[32];
6894#if defined(MBEDTLS_USE_PSA_CRYPTO)
6895 size_t hash_size;
6896 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6897 psa_status_t status;
6898#else
6899 mbedtls_sha256_context sha256;
6900#endif
6901
6902 mbedtls_ssl_session *session = ssl->session_negotiate;
6903 if( !session )
6904 session = ssl->session;
6905
6906 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6907 ? "client finished"
6908 : "server finished";
6909
6910#if defined(MBEDTLS_USE_PSA_CRYPTO)
6911 sha256_psa = psa_hash_operation_init();
6912
6913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6914
6915 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6916 if( status != PSA_SUCCESS )
6917 {
6918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6919 return;
6920 }
6921
6922 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6923 if( status != PSA_SUCCESS )
6924 {
6925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6926 return;
6927 }
6928 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6929#else
6930
6931 mbedtls_sha256_init( &sha256 );
6932
6933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6934
6935 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6936
6937 /*
6938 * TLSv1.2:
6939 * hash = PRF( master, finished_label,
6940 * Hash( handshake ) )[0.11]
6941 */
6942
6943#if !defined(MBEDTLS_SHA256_ALT)
6944 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6945 sha256.state, sizeof( sha256.state ) );
6946#endif
6947
6948 mbedtls_sha256_finish( &sha256, padbuf );
6949 mbedtls_sha256_free( &sha256 );
6950#endif /* MBEDTLS_USE_PSA_CRYPTO */
6951
6952 ssl->handshake->tls_prf( session->master, 48, sender,
6953 padbuf, 32, buf, len );
6954
6955 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6956
6957 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6958
6959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6960}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006961#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08006962
Jerry Yub7ba49e2022-02-17 14:25:53 +08006963
Andrzej Kurek25f27152022-08-17 16:09:31 -04006964#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08006965static void ssl_calc_finished_tls_sha384(
6966 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6967{
6968 int len = 12;
6969 const char *sender;
6970 unsigned char padbuf[48];
6971#if defined(MBEDTLS_USE_PSA_CRYPTO)
6972 size_t hash_size;
6973 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6974 psa_status_t status;
6975#else
6976 mbedtls_sha512_context sha512;
6977#endif
6978
6979 mbedtls_ssl_session *session = ssl->session_negotiate;
6980 if( !session )
6981 session = ssl->session;
6982
6983 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6984 ? "client finished"
6985 : "server finished";
6986
6987#if defined(MBEDTLS_USE_PSA_CRYPTO)
6988 sha384_psa = psa_hash_operation_init();
6989
6990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6991
6992 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6993 if( status != PSA_SUCCESS )
6994 {
6995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6996 return;
6997 }
6998
6999 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7000 if( status != PSA_SUCCESS )
7001 {
7002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7003 return;
7004 }
7005 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7006#else
7007 mbedtls_sha512_init( &sha512 );
7008
7009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7010
Andrzej Kureka242e832022-08-11 10:03:14 -04007011 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007012
7013 /*
7014 * TLSv1.2:
7015 * hash = PRF( master, finished_label,
7016 * Hash( handshake ) )[0.11]
7017 */
7018
7019#if !defined(MBEDTLS_SHA512_ALT)
7020 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7021 sha512.state, sizeof( sha512.state ) );
7022#endif
7023 mbedtls_sha512_finish( &sha512, padbuf );
7024
7025 mbedtls_sha512_free( &sha512 );
7026#endif
7027
7028 ssl->handshake->tls_prf( session->master, 48, sender,
7029 padbuf, 48, buf, len );
7030
7031 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7032
7033 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7034
7035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7036}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007037#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007038
Jerry Yuaef00152022-02-17 14:27:31 +08007039void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7040{
7041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7042
7043 /*
7044 * Free our handshake params
7045 */
7046 mbedtls_ssl_handshake_free( ssl );
7047 mbedtls_free( ssl->handshake );
7048 ssl->handshake = NULL;
7049
7050 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007051 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007052 */
7053 if( ssl->transform )
7054 {
7055 mbedtls_ssl_transform_free( ssl->transform );
7056 mbedtls_free( ssl->transform );
7057 }
7058 ssl->transform = ssl->transform_negotiate;
7059 ssl->transform_negotiate = NULL;
7060
7061 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7062}
7063
Jerry Yu2a9fff52022-02-17 14:28:51 +08007064void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7065{
7066 int resume = ssl->handshake->resume;
7067
7068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7069
7070#if defined(MBEDTLS_SSL_RENEGOTIATION)
7071 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7072 {
7073 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7074 ssl->renego_records_seen = 0;
7075 }
7076#endif
7077
7078 /*
7079 * Free the previous session and switch in the current one
7080 */
7081 if( ssl->session )
7082 {
7083#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7084 /* RFC 7366 3.1: keep the EtM state */
7085 ssl->session_negotiate->encrypt_then_mac =
7086 ssl->session->encrypt_then_mac;
7087#endif
7088
7089 mbedtls_ssl_session_free( ssl->session );
7090 mbedtls_free( ssl->session );
7091 }
7092 ssl->session = ssl->session_negotiate;
7093 ssl->session_negotiate = NULL;
7094
7095 /*
7096 * Add cache entry
7097 */
7098 if( ssl->conf->f_set_cache != NULL &&
7099 ssl->session->id_len != 0 &&
7100 resume == 0 )
7101 {
7102 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7103 ssl->session->id,
7104 ssl->session->id_len,
7105 ssl->session ) != 0 )
7106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7107 }
7108
7109#if defined(MBEDTLS_SSL_PROTO_DTLS)
7110 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7111 ssl->handshake->flight != NULL )
7112 {
7113 /* Cancel handshake timer */
7114 mbedtls_ssl_set_timer( ssl, 0 );
7115
7116 /* Keep last flight around in case we need to resend it:
7117 * we need the handshake and transform structures for that */
7118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7119 }
7120 else
7121#endif
7122 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7123
7124 ssl->state++;
7125
7126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7127}
7128
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007129int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7130{
7131 int ret, hash_len;
7132
7133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7134
7135 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7136
7137 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7138
7139 /*
7140 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7141 * may define some other value. Currently (early 2016), no defined
7142 * ciphersuite does this (and this is unlikely to change as activity has
7143 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7144 */
7145 hash_len = 12;
7146
7147#if defined(MBEDTLS_SSL_RENEGOTIATION)
7148 ssl->verify_data_len = hash_len;
7149 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7150#endif
7151
7152 ssl->out_msglen = 4 + hash_len;
7153 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7154 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7155
7156 /*
7157 * In case of session resuming, invert the client and server
7158 * ChangeCipherSpec messages order.
7159 */
7160 if( ssl->handshake->resume != 0 )
7161 {
7162#if defined(MBEDTLS_SSL_CLI_C)
7163 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7164 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7165#endif
7166#if defined(MBEDTLS_SSL_SRV_C)
7167 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7168 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7169#endif
7170 }
7171 else
7172 ssl->state++;
7173
7174 /*
7175 * Switch to our negotiated transform and session parameters for outbound
7176 * data.
7177 */
7178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7179
7180#if defined(MBEDTLS_SSL_PROTO_DTLS)
7181 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7182 {
7183 unsigned char i;
7184
7185 /* Remember current epoch settings for resending */
7186 ssl->handshake->alt_transform_out = ssl->transform_out;
7187 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7188 sizeof( ssl->handshake->alt_out_ctr ) );
7189
7190 /* Set sequence_number to zero */
7191 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7192
7193
7194 /* Increment epoch */
7195 for( i = 2; i > 0; i-- )
7196 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7197 break;
7198
7199 /* The loop goes to its end iff the counter is wrapping */
7200 if( i == 0 )
7201 {
7202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7203 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7204 }
7205 }
7206 else
7207#endif /* MBEDTLS_SSL_PROTO_DTLS */
7208 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7209
7210 ssl->transform_out = ssl->transform_negotiate;
7211 ssl->session_out = ssl->session_negotiate;
7212
7213#if defined(MBEDTLS_SSL_PROTO_DTLS)
7214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7215 mbedtls_ssl_send_flight_completed( ssl );
7216#endif
7217
7218 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7219 {
7220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7221 return( ret );
7222 }
7223
7224#if defined(MBEDTLS_SSL_PROTO_DTLS)
7225 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7226 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7227 {
7228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7229 return( ret );
7230 }
7231#endif
7232
7233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7234
7235 return( 0 );
7236}
7237
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007238#define SSL_MAX_HASH_LEN 12
7239
7240int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7241{
7242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7243 unsigned int hash_len = 12;
7244 unsigned char buf[SSL_MAX_HASH_LEN];
7245
7246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7247
7248 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7249
7250 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7251 {
7252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7253 goto exit;
7254 }
7255
7256 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7257 {
7258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7259 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7260 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7261 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7262 goto exit;
7263 }
7264
7265 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7266 {
7267 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7268 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7269 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7270 goto exit;
7271 }
7272
7273 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7274 {
7275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7276 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7277 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7278 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7279 goto exit;
7280 }
7281
7282 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7283 buf, hash_len ) != 0 )
7284 {
7285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7286 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7287 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7288 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7289 goto exit;
7290 }
7291
7292#if defined(MBEDTLS_SSL_RENEGOTIATION)
7293 ssl->verify_data_len = hash_len;
7294 memcpy( ssl->peer_verify_data, buf, hash_len );
7295#endif
7296
7297 if( ssl->handshake->resume != 0 )
7298 {
7299#if defined(MBEDTLS_SSL_CLI_C)
7300 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7301 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7302#endif
7303#if defined(MBEDTLS_SSL_SRV_C)
7304 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7305 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7306#endif
7307 }
7308 else
7309 ssl->state++;
7310
7311#if defined(MBEDTLS_SSL_PROTO_DTLS)
7312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7313 mbedtls_ssl_recv_flight_completed( ssl );
7314#endif
7315
7316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7317
7318exit:
7319 mbedtls_platform_zeroize( buf, hash_len );
7320 return( ret );
7321}
7322
Jerry Yu392112c2022-02-17 14:34:10 +08007323#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7324/*
7325 * Helper to get TLS 1.2 PRF from ciphersuite
7326 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7327 */
7328static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7329{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007330#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007331 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7332 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7333
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007334 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007335 return( tls_prf_sha384 );
7336#else
7337 (void) ciphersuite_id;
7338#endif
7339 return( tls_prf_sha256 );
7340}
7341#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007342
7343static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7344{
7345 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007346#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007347 if( tls_prf == tls_prf_sha384 )
7348 {
7349 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7350 }
7351 else
7352#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007353#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007354 if( tls_prf == tls_prf_sha256 )
7355 {
7356 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7357 }
7358 else
7359#endif
7360 return( MBEDTLS_SSL_TLS_PRF_NONE );
7361}
7362
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007363/*
7364 * Populate a transform structure with session keys and all the other
7365 * necessary information.
7366 *
7367 * Parameters:
7368 * - [in/out]: transform: structure to populate
7369 * [in] must be just initialised with mbedtls_ssl_transform_init()
7370 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7371 * - [in] ciphersuite
7372 * - [in] master
7373 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007374 * - [in] tls_prf: pointer to PRF to use for key derivation
7375 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007376 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007377 * - [in] endpoint: client or server
7378 * - [in] ssl: used for:
7379 * - ssl->conf->{f,p}_export_keys
7380 * [in] optionally used for:
7381 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7382 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007383MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007384static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7385 int ciphersuite,
7386 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007387#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007388 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007389#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007390 ssl_tls_prf_t tls_prf,
7391 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007392 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007393 unsigned endpoint,
7394 const mbedtls_ssl_context *ssl )
7395{
7396 int ret = 0;
7397 unsigned char keyblk[256];
7398 unsigned char *key1;
7399 unsigned char *key2;
7400 unsigned char *mac_enc;
7401 unsigned char *mac_dec;
7402 size_t mac_key_len = 0;
7403 size_t iv_copy_len;
7404 size_t keylen;
7405 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007406 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007407#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007408 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007409 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007410#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007411
7412#if defined(MBEDTLS_USE_PSA_CRYPTO)
7413 psa_key_type_t key_type;
7414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7415 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007416 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007417 size_t key_bits;
7418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7419#endif
7420
7421#if !defined(MBEDTLS_DEBUG_C) && \
7422 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7423 if( ssl->f_export_keys == NULL )
7424 {
7425 ssl = NULL; /* make sure we don't use it except for these cases */
7426 (void) ssl;
7427 }
7428#endif
7429
7430 /*
7431 * Some data just needs copying into the structure
7432 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007433#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007434 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007435#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007436 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007437
7438#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7439 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7440#endif
7441
7442#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007443 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007444 {
7445 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7446 * generation separate. This should never happen. */
7447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7448 }
7449#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7450
7451 /*
7452 * Get various info structures
7453 */
7454 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7455 if( ciphersuite_info == NULL )
7456 {
7457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7458 ciphersuite ) );
7459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7460 }
7461
Neil Armstrongab555e02022-04-04 11:07:59 +02007462 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007463#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007464 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007465#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007466 ciphersuite_info );
7467
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007468 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7469 transform->taglen =
7470 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7471
7472#if defined(MBEDTLS_USE_PSA_CRYPTO)
7473 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7474 transform->taglen,
7475 &alg,
7476 &key_type,
7477 &key_bits ) ) != PSA_SUCCESS )
7478 {
7479 ret = psa_ssl_status_to_mbedtls( status );
7480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7481 goto end;
7482 }
7483#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007484 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7485 if( cipher_info == NULL )
7486 {
7487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7488 ciphersuite_info->cipher ) );
7489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7490 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007491#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007492
Neil Armstronge4512952022-03-08 09:08:22 +01007493#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007494 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007495 if( mac_alg == 0 )
7496 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007498 (unsigned) ciphersuite_info->mac ) );
7499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7500 }
7501#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007502 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7503 if( md_info == NULL )
7504 {
7505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7506 (unsigned) ciphersuite_info->mac ) );
7507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7508 }
Neil Armstronge4512952022-03-08 09:08:22 +01007509#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007510
7511#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7512 /* Copy own and peer's CID if the use of the CID
7513 * extension has been negotiated. */
7514 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7515 {
7516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7517
7518 transform->in_cid_len = ssl->own_cid_len;
7519 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7520 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7521 transform->in_cid_len );
7522
7523 transform->out_cid_len = ssl->handshake->peer_cid_len;
7524 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7525 ssl->handshake->peer_cid_len );
7526 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7527 transform->out_cid_len );
7528 }
7529#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7530
7531 /*
7532 * Compute key block using the PRF
7533 */
7534 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7535 if( ret != 0 )
7536 {
7537 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7538 return( ret );
7539 }
7540
7541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7542 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7543 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7544 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7545 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7546
7547 /*
7548 * Determine the appropriate key, IV and MAC length.
7549 */
7550
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007551#if defined(MBEDTLS_USE_PSA_CRYPTO)
7552 keylen = PSA_BITS_TO_BYTES(key_bits);
7553#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007554 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007555#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007556
7557#if defined(MBEDTLS_GCM_C) || \
7558 defined(MBEDTLS_CCM_C) || \
7559 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007560 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007561 {
7562 size_t explicit_ivlen;
7563
7564 transform->maclen = 0;
7565 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007566
7567 /* All modes haves 96-bit IVs, but the length of the static parts vary
7568 * with mode and version:
7569 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7570 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7571 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7572 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7573 * sequence number).
7574 */
7575 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007576#if defined(MBEDTLS_USE_PSA_CRYPTO)
7577 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7578#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007579 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007580#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007581 transform->fixed_ivlen = 12;
7582 else
7583 transform->fixed_ivlen = 4;
7584
7585 /* Minimum length of encrypted record */
7586 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7587 transform->minlen = explicit_ivlen + transform->taglen;
7588 }
7589 else
7590#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7591#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007592 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7593 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7594 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007595 {
Neil Armstronge4512952022-03-08 09:08:22 +01007596#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007597 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007598#else
7599 size_t block_size = cipher_info->block_size;
7600#endif /* MBEDTLS_USE_PSA_CRYPTO */
7601
7602#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007603 /* Get MAC length */
7604 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7605#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007606 /* Initialize HMAC contexts */
7607 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7608 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7609 {
7610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7611 goto end;
7612 }
7613
7614 /* Get MAC length */
7615 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007616#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007617 transform->maclen = mac_key_len;
7618
7619 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007620#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007621 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007622#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007624#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007625
7626 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007627 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007628 transform->minlen = transform->maclen;
7629 else
7630 {
7631 /*
7632 * GenericBlockCipher:
7633 * 1. if EtM is in use: one block plus MAC
7634 * otherwise: * first multiple of blocklen greater than maclen
7635 * 2. IV
7636 */
7637#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007638 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007639 {
7640 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007641 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007642 }
7643 else
7644#endif
7645 {
7646 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007647 + block_size
7648 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007649 }
7650
Glenn Strauss07c64162022-03-14 12:34:51 -04007651 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007652 {
7653 transform->minlen += transform->ivlen;
7654 }
7655 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007656 {
7657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7658 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7659 goto end;
7660 }
7661 }
7662 }
7663 else
7664#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7665 {
7666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7667 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7668 }
7669
7670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7671 (unsigned) keylen,
7672 (unsigned) transform->minlen,
7673 (unsigned) transform->ivlen,
7674 (unsigned) transform->maclen ) );
7675
7676 /*
7677 * Finally setup the cipher contexts, IVs and MAC secrets.
7678 */
7679#if defined(MBEDTLS_SSL_CLI_C)
7680 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7681 {
7682 key1 = keyblk + mac_key_len * 2;
7683 key2 = keyblk + mac_key_len * 2 + keylen;
7684
7685 mac_enc = keyblk;
7686 mac_dec = keyblk + mac_key_len;
7687
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007688 iv_copy_len = ( transform->fixed_ivlen ) ?
7689 transform->fixed_ivlen : transform->ivlen;
7690 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7691 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7692 iv_copy_len );
7693 }
7694 else
7695#endif /* MBEDTLS_SSL_CLI_C */
7696#if defined(MBEDTLS_SSL_SRV_C)
7697 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7698 {
7699 key1 = keyblk + mac_key_len * 2 + keylen;
7700 key2 = keyblk + mac_key_len * 2;
7701
7702 mac_enc = keyblk + mac_key_len;
7703 mac_dec = keyblk;
7704
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007705 iv_copy_len = ( transform->fixed_ivlen ) ?
7706 transform->fixed_ivlen : transform->ivlen;
7707 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7708 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7709 iv_copy_len );
7710 }
7711 else
7712#endif /* MBEDTLS_SSL_SRV_C */
7713 {
7714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7715 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7716 goto end;
7717 }
7718
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007719 if( ssl != NULL && ssl->f_export_keys != NULL )
7720 {
7721 ssl->f_export_keys( ssl->p_export_keys,
7722 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7723 master, 48,
7724 randbytes + 32,
7725 randbytes,
7726 tls_prf_get_type( tls_prf ) );
7727 }
7728
7729#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007730 transform->psa_alg = alg;
7731
7732 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7733 {
7734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7735 psa_set_key_algorithm( &attributes, alg );
7736 psa_set_key_type( &attributes, key_type );
7737
7738 if( ( status = psa_import_key( &attributes,
7739 key1,
7740 PSA_BITS_TO_BYTES( key_bits ),
7741 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7742 {
7743 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7744 ret = psa_ssl_status_to_mbedtls( status );
7745 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7746 goto end;
7747 }
7748
7749 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7750
7751 if( ( status = psa_import_key( &attributes,
7752 key2,
7753 PSA_BITS_TO_BYTES( key_bits ),
7754 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7755 {
7756 ret = psa_ssl_status_to_mbedtls( status );
7757 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7758 goto end;
7759 }
7760 }
7761#else
7762 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7763 cipher_info ) ) != 0 )
7764 {
7765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7766 goto end;
7767 }
7768
7769 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7770 cipher_info ) ) != 0 )
7771 {
7772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7773 goto end;
7774 }
7775
7776 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7777 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7778 MBEDTLS_ENCRYPT ) ) != 0 )
7779 {
7780 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7781 goto end;
7782 }
7783
7784 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7785 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7786 MBEDTLS_DECRYPT ) ) != 0 )
7787 {
7788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7789 goto end;
7790 }
7791
7792#if defined(MBEDTLS_CIPHER_MODE_CBC)
7793 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7794 {
7795 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7796 MBEDTLS_PADDING_NONE ) ) != 0 )
7797 {
7798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7799 goto end;
7800 }
7801
7802 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7803 MBEDTLS_PADDING_NONE ) ) != 0 )
7804 {
7805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7806 goto end;
7807 }
7808 }
7809#endif /* MBEDTLS_CIPHER_MODE_CBC */
7810#endif /* MBEDTLS_USE_PSA_CRYPTO */
7811
Neil Armstrong29c0c042022-03-17 17:47:28 +01007812#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7813 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7814 For AEAD-based ciphersuites, there is nothing to do here. */
7815 if( mac_key_len != 0 )
7816 {
7817#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007818 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007819
7820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007821 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007822 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7823
7824 if( ( status = psa_import_key( &attributes,
7825 mac_enc, mac_key_len,
7826 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7827 {
7828 ret = psa_ssl_status_to_mbedtls( status );
7829 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7830 goto end;
7831 }
7832
Ronald Cronfb39f152022-03-25 14:36:28 +01007833 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007834 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7835#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7836 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7837#endif
7838 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007839 /* mbedtls_ct_hmac() requires the key to be exportable */
7840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7841 PSA_KEY_USAGE_VERIFY_HASH );
7842 else
7843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7844
7845 if( ( status = psa_import_key( &attributes,
7846 mac_dec, mac_key_len,
7847 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7848 {
7849 ret = psa_ssl_status_to_mbedtls( status );
7850 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7851 goto end;
7852 }
7853#else
7854 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7855 if( ret != 0 )
7856 goto end;
7857 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7858 if( ret != 0 )
7859 goto end;
7860#endif /* MBEDTLS_USE_PSA_CRYPTO */
7861 }
7862#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7863
7864 ((void) mac_dec);
7865 ((void) mac_enc);
7866
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007867end:
7868 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7869 return( ret );
7870}
7871
Jerry Yuee40f9d2022-02-17 14:55:16 +08007872#if defined(MBEDTLS_USE_PSA_CRYPTO)
7873int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7874 unsigned char *hash, size_t *hashlen,
7875 unsigned char *data, size_t data_len,
7876 mbedtls_md_type_t md_alg )
7877{
7878 psa_status_t status;
7879 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007880 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007881
7882 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7883
7884 if( ( status = psa_hash_setup( &hash_operation,
7885 hash_alg ) ) != PSA_SUCCESS )
7886 {
7887 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7888 goto exit;
7889 }
7890
7891 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7892 64 ) ) != PSA_SUCCESS )
7893 {
7894 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7895 goto exit;
7896 }
7897
7898 if( ( status = psa_hash_update( &hash_operation,
7899 data, data_len ) ) != PSA_SUCCESS )
7900 {
7901 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7902 goto exit;
7903 }
7904
7905 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7906 hashlen ) ) != PSA_SUCCESS )
7907 {
7908 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7909 goto exit;
7910 }
7911
7912exit:
7913 if( status != PSA_SUCCESS )
7914 {
7915 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7916 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7917 switch( status )
7918 {
7919 case PSA_ERROR_NOT_SUPPORTED:
7920 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7921 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7922 case PSA_ERROR_BUFFER_TOO_SMALL:
7923 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7924 case PSA_ERROR_INSUFFICIENT_MEMORY:
7925 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7926 default:
7927 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7928 }
7929 }
7930 return( 0 );
7931}
7932
7933#else
7934
7935int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7936 unsigned char *hash, size_t *hashlen,
7937 unsigned char *data, size_t data_len,
7938 mbedtls_md_type_t md_alg )
7939{
7940 int ret = 0;
7941 mbedtls_md_context_t ctx;
7942 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7943 *hashlen = mbedtls_md_get_size( md_info );
7944
7945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7946
7947 mbedtls_md_init( &ctx );
7948
7949 /*
7950 * digitally-signed struct {
7951 * opaque client_random[32];
7952 * opaque server_random[32];
7953 * ServerDHParams params;
7954 * };
7955 */
7956 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7957 {
7958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7959 goto exit;
7960 }
7961 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7962 {
7963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7964 goto exit;
7965 }
7966 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7967 {
7968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7969 goto exit;
7970 }
7971 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7972 {
7973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7974 goto exit;
7975 }
7976 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7977 {
7978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7979 goto exit;
7980 }
7981
7982exit:
7983 mbedtls_md_free( &ctx );
7984
7985 if( ret != 0 )
7986 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7987 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7988
7989 return( ret );
7990}
7991#endif /* MBEDTLS_USE_PSA_CRYPTO */
7992
Jerry Yud9d91da2022-02-17 14:57:06 +08007993#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7994
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007995/* Find the preferred hash for a given signature algorithm. */
7996unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
7997 mbedtls_ssl_context *ssl,
7998 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08007999{
Gabor Mezei078e8032022-04-27 21:17:56 +02008000 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008001 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008002
8003 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008004 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008005
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008006 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008007 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008008 unsigned int hash_alg_received =
8009 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8010 received_sig_algs[i] );
8011 unsigned int sig_alg_received =
8012 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8013 received_sig_algs[i] );
8014
8015 if( sig_alg == sig_alg_received )
8016 {
8017#if defined(MBEDTLS_USE_PSA_CRYPTO)
8018 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8019 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008020 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008021 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008022
Neil Armstrong96eceb82022-06-30 18:05:05 +02008023 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8024 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8025 PSA_ALG_ECDSA( psa_hash_alg ),
8026 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008027 continue;
8028
Neil Armstrong96eceb82022-06-30 18:05:05 +02008029 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008030 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8031 PSA_ALG_RSA_PKCS1V15_SIGN(
8032 psa_hash_alg ),
8033 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008034 continue;
8035 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008036#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008037
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008038 return( hash_alg_received );
8039 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008040 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008041
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008042 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008043}
8044
8045#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8046
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008047/* Serialization of TLS 1.2 sessions:
8048 *
8049 * struct {
8050 * uint64 start_time;
8051 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008052 * uint8 session_id_len; // at most 32
8053 * opaque session_id[32];
8054 * opaque master[48]; // fixed length in the standard
8055 * uint32 verify_result;
8056 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8057 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8058 * uint32 ticket_lifetime;
8059 * uint8 mfl_code; // up to 255 according to standard
8060 * uint8 encrypt_then_mac; // 0 or 1
8061 * } serialized_session_tls12;
8062 *
8063 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008064static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008065 unsigned char *buf,
8066 size_t buf_len )
8067{
8068 unsigned char *p = buf;
8069 size_t used = 0;
8070
8071#if defined(MBEDTLS_HAVE_TIME)
8072 uint64_t start;
8073#endif
8074#if defined(MBEDTLS_X509_CRT_PARSE_C)
8075#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8076 size_t cert_len;
8077#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8078#endif /* MBEDTLS_X509_CRT_PARSE_C */
8079
8080 /*
8081 * Time
8082 */
8083#if defined(MBEDTLS_HAVE_TIME)
8084 used += 8;
8085
8086 if( used <= buf_len )
8087 {
8088 start = (uint64_t) session->start;
8089
8090 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8091 p += 8;
8092 }
8093#endif /* MBEDTLS_HAVE_TIME */
8094
8095 /*
8096 * Basic mandatory fields
8097 */
8098 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008099 + 1 /* id_len */
8100 + sizeof( session->id )
8101 + sizeof( session->master )
8102 + 4; /* verify_result */
8103
8104 if( used <= buf_len )
8105 {
8106 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8107 p += 2;
8108
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008109 *p++ = MBEDTLS_BYTE_0( session->id_len );
8110 memcpy( p, session->id, 32 );
8111 p += 32;
8112
8113 memcpy( p, session->master, 48 );
8114 p += 48;
8115
8116 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8117 p += 4;
8118 }
8119
8120 /*
8121 * Peer's end-entity certificate
8122 */
8123#if defined(MBEDTLS_X509_CRT_PARSE_C)
8124#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8125 if( session->peer_cert == NULL )
8126 cert_len = 0;
8127 else
8128 cert_len = session->peer_cert->raw.len;
8129
8130 used += 3 + cert_len;
8131
8132 if( used <= buf_len )
8133 {
8134 *p++ = MBEDTLS_BYTE_2( cert_len );
8135 *p++ = MBEDTLS_BYTE_1( cert_len );
8136 *p++ = MBEDTLS_BYTE_0( cert_len );
8137
8138 if( session->peer_cert != NULL )
8139 {
8140 memcpy( p, session->peer_cert->raw.p, cert_len );
8141 p += cert_len;
8142 }
8143 }
8144#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8145 if( session->peer_cert_digest != NULL )
8146 {
8147 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8148 if( used <= buf_len )
8149 {
8150 *p++ = (unsigned char) session->peer_cert_digest_type;
8151 *p++ = (unsigned char) session->peer_cert_digest_len;
8152 memcpy( p, session->peer_cert_digest,
8153 session->peer_cert_digest_len );
8154 p += session->peer_cert_digest_len;
8155 }
8156 }
8157 else
8158 {
8159 used += 2;
8160 if( used <= buf_len )
8161 {
8162 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8163 *p++ = 0;
8164 }
8165 }
8166#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8167#endif /* MBEDTLS_X509_CRT_PARSE_C */
8168
8169 /*
8170 * Session ticket if any, plus associated data
8171 */
8172#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8173 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8174
8175 if( used <= buf_len )
8176 {
8177 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8178 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8179 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8180
8181 if( session->ticket != NULL )
8182 {
8183 memcpy( p, session->ticket, session->ticket_len );
8184 p += session->ticket_len;
8185 }
8186
8187 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8188 p += 4;
8189 }
8190#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8191
8192 /*
8193 * Misc extension-related info
8194 */
8195#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8196 used += 1;
8197
8198 if( used <= buf_len )
8199 *p++ = session->mfl_code;
8200#endif
8201
8202#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8203 used += 1;
8204
8205 if( used <= buf_len )
8206 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8207#endif
8208
8209 return( used );
8210}
8211
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008212MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008213static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008214 const unsigned char *buf,
8215 size_t len )
8216{
8217#if defined(MBEDTLS_HAVE_TIME)
8218 uint64_t start;
8219#endif
8220#if defined(MBEDTLS_X509_CRT_PARSE_C)
8221#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8222 size_t cert_len;
8223#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8224#endif /* MBEDTLS_X509_CRT_PARSE_C */
8225
8226 const unsigned char *p = buf;
8227 const unsigned char * const end = buf + len;
8228
8229 /*
8230 * Time
8231 */
8232#if defined(MBEDTLS_HAVE_TIME)
8233 if( 8 > (size_t)( end - p ) )
8234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8235
8236 start = ( (uint64_t) p[0] << 56 ) |
8237 ( (uint64_t) p[1] << 48 ) |
8238 ( (uint64_t) p[2] << 40 ) |
8239 ( (uint64_t) p[3] << 32 ) |
8240 ( (uint64_t) p[4] << 24 ) |
8241 ( (uint64_t) p[5] << 16 ) |
8242 ( (uint64_t) p[6] << 8 ) |
8243 ( (uint64_t) p[7] );
8244 p += 8;
8245
8246 session->start = (time_t) start;
8247#endif /* MBEDTLS_HAVE_TIME */
8248
8249 /*
8250 * Basic mandatory fields
8251 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008252 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8254
8255 session->ciphersuite = ( p[0] << 8 ) | p[1];
8256 p += 2;
8257
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008258 session->id_len = *p++;
8259 memcpy( session->id, p, 32 );
8260 p += 32;
8261
8262 memcpy( session->master, p, 48 );
8263 p += 48;
8264
8265 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8266 ( (uint32_t) p[1] << 16 ) |
8267 ( (uint32_t) p[2] << 8 ) |
8268 ( (uint32_t) p[3] );
8269 p += 4;
8270
8271 /* Immediately clear invalid pointer values that have been read, in case
8272 * we exit early before we replaced them with valid ones. */
8273#if defined(MBEDTLS_X509_CRT_PARSE_C)
8274#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8275 session->peer_cert = NULL;
8276#else
8277 session->peer_cert_digest = NULL;
8278#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8279#endif /* MBEDTLS_X509_CRT_PARSE_C */
8280#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8281 session->ticket = NULL;
8282#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8283
8284 /*
8285 * Peer certificate
8286 */
8287#if defined(MBEDTLS_X509_CRT_PARSE_C)
8288#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8289 /* Deserialize CRT from the end of the ticket. */
8290 if( 3 > (size_t)( end - p ) )
8291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8292
8293 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8294 p += 3;
8295
8296 if( cert_len != 0 )
8297 {
8298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8299
8300 if( cert_len > (size_t)( end - p ) )
8301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8302
8303 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8304
8305 if( session->peer_cert == NULL )
8306 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8307
8308 mbedtls_x509_crt_init( session->peer_cert );
8309
8310 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8311 p, cert_len ) ) != 0 )
8312 {
8313 mbedtls_x509_crt_free( session->peer_cert );
8314 mbedtls_free( session->peer_cert );
8315 session->peer_cert = NULL;
8316 return( ret );
8317 }
8318
8319 p += cert_len;
8320 }
8321#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8322 /* Deserialize CRT digest from the end of the ticket. */
8323 if( 2 > (size_t)( end - p ) )
8324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8325
8326 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8327 session->peer_cert_digest_len = (size_t) *p++;
8328
8329 if( session->peer_cert_digest_len != 0 )
8330 {
8331 const mbedtls_md_info_t *md_info =
8332 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8333 if( md_info == NULL )
8334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8335 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8337
8338 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8340
8341 session->peer_cert_digest =
8342 mbedtls_calloc( 1, session->peer_cert_digest_len );
8343 if( session->peer_cert_digest == NULL )
8344 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8345
8346 memcpy( session->peer_cert_digest, p,
8347 session->peer_cert_digest_len );
8348 p += session->peer_cert_digest_len;
8349 }
8350#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8351#endif /* MBEDTLS_X509_CRT_PARSE_C */
8352
8353 /*
8354 * Session ticket and associated data
8355 */
8356#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8357 if( 3 > (size_t)( end - p ) )
8358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8359
8360 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8361 p += 3;
8362
8363 if( session->ticket_len != 0 )
8364 {
8365 if( session->ticket_len > (size_t)( end - p ) )
8366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8367
8368 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8369 if( session->ticket == NULL )
8370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8371
8372 memcpy( session->ticket, p, session->ticket_len );
8373 p += session->ticket_len;
8374 }
8375
8376 if( 4 > (size_t)( end - p ) )
8377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8378
8379 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8380 ( (uint32_t) p[1] << 16 ) |
8381 ( (uint32_t) p[2] << 8 ) |
8382 ( (uint32_t) p[3] );
8383 p += 4;
8384#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8385
8386 /*
8387 * Misc extension-related info
8388 */
8389#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8390 if( 1 > (size_t)( end - p ) )
8391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8392
8393 session->mfl_code = *p++;
8394#endif
8395
8396#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8397 if( 1 > (size_t)( end - p ) )
8398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8399
8400 session->encrypt_then_mac = *p++;
8401#endif
8402
8403 /* Done, should have consumed entire buffer */
8404 if( p != end )
8405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8406
8407 return( 0 );
8408}
Jerry Yudc7bd172022-02-17 13:44:15 +08008409#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8410
XiaokangQian75d40ef2022-04-20 11:05:24 +00008411int mbedtls_ssl_validate_ciphersuite(
8412 const mbedtls_ssl_context *ssl,
8413 const mbedtls_ssl_ciphersuite_t *suite_info,
8414 mbedtls_ssl_protocol_version min_tls_version,
8415 mbedtls_ssl_protocol_version max_tls_version )
8416{
8417 (void) ssl;
8418
8419 if( suite_info == NULL )
8420 return( -1 );
8421
8422 if( ( suite_info->min_tls_version > max_tls_version ) ||
8423 ( suite_info->max_tls_version < min_tls_version ) )
8424 {
8425 return( -1 );
8426 }
8427
XiaokangQian060d8672022-04-21 09:24:56 +00008428#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008429#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8430 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8431 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8432 {
8433 return( -1 );
8434 }
8435#endif
8436
8437 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8438#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8439 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8440 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8441 {
8442 return( -1 );
8443 }
8444#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8446
8447 return( 0 );
8448}
8449
XiaokangQianeaf36512022-04-24 09:07:44 +00008450#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8451/*
8452 * Function for writing a signature algorithm extension.
8453 *
8454 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8455 * value (TLS 1.3 RFC8446):
8456 * enum {
8457 * ....
8458 * ecdsa_secp256r1_sha256( 0x0403 ),
8459 * ecdsa_secp384r1_sha384( 0x0503 ),
8460 * ecdsa_secp521r1_sha512( 0x0603 ),
8461 * ....
8462 * } SignatureScheme;
8463 *
8464 * struct {
8465 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8466 * } SignatureSchemeList;
8467 *
8468 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8469 * value (TLS 1.2 RFC5246):
8470 * enum {
8471 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8472 * sha512(6), (255)
8473 * } HashAlgorithm;
8474 *
8475 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8476 * SignatureAlgorithm;
8477 *
8478 * struct {
8479 * HashAlgorithm hash;
8480 * SignatureAlgorithm signature;
8481 * } SignatureAndHashAlgorithm;
8482 *
8483 * SignatureAndHashAlgorithm
8484 * supported_signature_algorithms<2..2^16-2>;
8485 *
8486 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8487 * generalization of the TLS 1.2 signature algorithm extension.
8488 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8489 * `SignatureScheme` field of TLS 1.3
8490 *
8491 */
8492int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8493 const unsigned char *end, size_t *out_len )
8494{
8495 unsigned char *p = buf;
8496 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8497 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8498
8499 *out_len = 0;
8500
8501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8502
8503 /* Check if we have space for header and length field:
8504 * - extension_type (2 bytes)
8505 * - extension_data_length (2 bytes)
8506 * - supported_signature_algorithms_length (2 bytes)
8507 */
8508 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8509 p += 6;
8510
8511 /*
8512 * Write supported_signature_algorithms
8513 */
8514 supported_sig_alg = p;
8515 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8516 if( sig_alg == NULL )
8517 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8518
8519 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8520 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8522 *sig_alg,
8523 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008524 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8525 continue;
8526 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8527 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8528 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008530 *sig_alg,
8531 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008532 }
8533
8534 /* Length of supported_signature_algorithms */
8535 supported_sig_alg_len = p - supported_sig_alg;
8536 if( supported_sig_alg_len == 0 )
8537 {
8538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8540 }
8541
XiaokangQianeaf36512022-04-24 09:07:44 +00008542 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008543 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008544 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8545
XiaokangQianeaf36512022-04-24 09:07:44 +00008546 *out_len = p - buf;
8547
8548#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8549 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8550#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8551 return( 0 );
8552}
8553#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8554
XiaokangQian40a35232022-05-07 09:02:40 +00008555#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008556/*
8557 * mbedtls_ssl_parse_server_name_ext
8558 *
8559 * Structure of server_name extension:
8560 *
8561 * enum {
8562 * host_name(0), (255)
8563 * } NameType;
8564 * opaque HostName<1..2^16-1>;
8565 *
8566 * struct {
8567 * NameType name_type;
8568 * select (name_type) {
8569 * case host_name: HostName;
8570 * } name;
8571 * } ServerName;
8572 * struct {
8573 * ServerName server_name_list<1..2^16-1>
8574 * } ServerNameList;
8575 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008576MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008577int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8578 const unsigned char *buf,
8579 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008580{
8581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8582 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008583 size_t server_name_list_len, hostname_len;
8584 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008585
XiaokangQianf2a94202022-05-20 06:44:24 +00008586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008587
8588 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008589 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008590 p += 2;
8591
XiaokangQian9b2b7712022-05-17 02:57:00 +00008592 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8593 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008594 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008595 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008596 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008597 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008598 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8599 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008600
8601 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8602 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008603 /* sni_name is intended to be used only during the parsing of the
8604 * ClientHello message (it is reset to NULL before the end of
8605 * the message parsing). Thus it is ok to just point to the
8606 * reception buffer and not make a copy of it.
8607 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008608 ssl->handshake->sni_name = p + 3;
8609 ssl->handshake->sni_name_len = hostname_len;
8610 if( ssl->conf->f_sni == NULL )
8611 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008612 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008613 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008614 if( ret != 0 )
8615 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008616 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008617 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8618 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008619 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8620 }
8621 return( 0 );
8622 }
8623
8624 p += hostname_len + 3;
8625 }
8626
8627 return( 0 );
8628}
8629#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8630
XiaokangQianacb39922022-06-17 10:18:48 +00008631#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008632MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008633int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8634 const unsigned char *buf,
8635 const unsigned char *end )
8636{
8637 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008638 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008639 const unsigned char *protocol_name_list;
8640 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008641 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008642
8643 /* If ALPN not configured, just ignore the extension */
8644 if( ssl->conf->alpn_list == NULL )
8645 return( 0 );
8646
8647 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008648 * RFC7301, section 3.1
8649 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008650 *
XiaokangQianc7403452022-06-23 03:24:12 +00008651 * struct {
8652 * ProtocolName protocol_name_list<2..2^16-1>
8653 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008654 */
8655
XiaokangQianc7403452022-06-23 03:24:12 +00008656 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008657 * protocol_name_list_len 2 bytes
8658 * protocol_name_len 1 bytes
8659 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008660 */
XiaokangQianacb39922022-06-17 10:18:48 +00008661 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8662
XiaokangQianc7403452022-06-23 03:24:12 +00008663 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008664 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008665 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008666 protocol_name_list = p;
8667 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008668
8669 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008670 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008671 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008672 protocol_name_len = *p++;
8673 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8674 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008675 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008676 {
8677 MBEDTLS_SSL_PEND_FATAL_ALERT(
8678 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8679 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008680 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008681 }
8682
8683 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008684 }
8685
8686 /* Use our order of preference */
8687 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8688 {
8689 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008690 p = protocol_name_list;
8691 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008692 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008693 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008694 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008695 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008696 {
8697 ssl->alpn_chosen = *alpn;
8698 return( 0 );
8699 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008700
8701 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008702 }
8703 }
8704
XiaokangQian95d5f542022-06-24 02:29:26 +00008705 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008706 MBEDTLS_SSL_PEND_FATAL_ALERT(
8707 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8708 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8709 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8710}
8711
8712int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8713 unsigned char *buf,
8714 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008715 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008716{
8717 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008718 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008719 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008720
8721 if( ssl->alpn_chosen == NULL )
8722 {
8723 return( 0 );
8724 }
8725
XiaokangQian95d5f542022-06-24 02:29:26 +00008726 protocol_name_len = strlen( ssl->alpn_chosen );
8727 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008728
8729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8730 /*
8731 * 0 . 1 ext identifier
8732 * 2 . 3 ext length
8733 * 4 . 5 protocol list length
8734 * 6 . 6 protocol name length
8735 * 7 . 7+n protocol name
8736 */
8737 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8738
XiaokangQian95d5f542022-06-24 02:29:26 +00008739 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008740
XiaokangQian95d5f542022-06-24 02:29:26 +00008741 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8742 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008743 /* Note: the length of the chosen protocol has been checked to be less
8744 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8745 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008746 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008747
XiaokangQian95d5f542022-06-24 02:29:26 +00008748 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008749 return ( 0 );
8750}
8751#endif /* MBEDTLS_SSL_ALPN */
8752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753#endif /* MBEDTLS_SSL_TLS_C */