blob: 075a65b647dd67df10a28aace4503d4f71082405 [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#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020057#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254 if( src->peer_cert != NULL )
255 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200257
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200258 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200265 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 dst->peer_cert = NULL;
269 return( ret );
270 }
271 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 if( src->peer_cert_digest != NULL )
274 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 if( dst->peer_cert_digest == NULL )
278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
279
280 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
281 src->peer_cert_digest_len );
282 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000283 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 }
285#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200289#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290 if( src->ticket != NULL )
291 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200293 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 memcpy( dst->ticket, src->ticket, src->ticket_len );
297 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
300 return( 0 );
301}
302
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500303#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
306{
307 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
308 if( resized_buffer == NULL )
309 return -1;
310
311 /* We want to copy len_new bytes when downsizing the buffer, and
312 * len_old bytes when upsizing, so we choose the smaller of two sizes,
313 * to fit one buffer into another. Size checks, ensuring that no data is
314 * lost, are done outside of this function. */
315 memcpy( resized_buffer, *buffer,
316 ( len_new < *len_old ) ? len_new : *len_old );
317 mbedtls_platform_zeroize( *buffer, *len_old );
318 mbedtls_free( *buffer );
319
320 *buffer = resized_buffer;
321 *len_old = len_new;
322
323 return 0;
324}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200325
326static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500327 size_t in_buf_new_len,
328 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200329{
330 int modified = 0;
331 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
332 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
333 if( ssl->in_buf != NULL )
334 {
335 written_in = ssl->in_msg - ssl->in_buf;
336 iv_offset_in = ssl->in_iv - ssl->in_buf;
337 len_offset_in = ssl->in_len - ssl->in_buf;
338 if( downsizing ?
339 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
340 ssl->in_buf_len < in_buf_new_len )
341 {
342 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
343 {
344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
345 }
346 else
347 {
Paul Elliottb7449902021-03-10 18:14:58 +0000348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
349 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 modified = 1;
351 }
352 }
353 }
354
355 if( ssl->out_buf != NULL )
356 {
357 written_out = ssl->out_msg - ssl->out_buf;
358 iv_offset_out = ssl->out_iv - ssl->out_buf;
359 len_offset_out = ssl->out_len - ssl->out_buf;
360 if( downsizing ?
361 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
362 ssl->out_buf_len < out_buf_new_len )
363 {
364 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
365 {
366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
367 }
368 else
369 {
Paul Elliottb7449902021-03-10 18:14:58 +0000370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
371 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 modified = 1;
373 }
374 }
375 }
376 if( modified )
377 {
378 /* Update pointers here to avoid doing it twice. */
379 mbedtls_ssl_reset_in_out_pointers( ssl );
380 /* Fields below might not be properly updated with record
381 * splitting or with CID, so they are manually updated here. */
382 ssl->out_msg = ssl->out_buf + written_out;
383 ssl->out_len = ssl->out_buf + len_offset_out;
384 ssl->out_iv = ssl->out_buf + iv_offset_out;
385
386 ssl->in_msg = ssl->in_buf + written_in;
387 ssl->in_len = ssl->in_buf + len_offset_in;
388 ssl->in_iv = ssl->in_buf + iv_offset_in;
389 }
390}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500391#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
392
Jerry Yudb8c48a2022-01-27 14:54:54 +0800393#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
396typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
397 const char *label,
398 const unsigned char *random, size_t rlen,
399 unsigned char *dstbuf, size_t dlen );
400
401static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
402
403#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
404
405/* Type for the TLS PRF */
406typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
407 const unsigned char *, size_t,
408 unsigned char *, size_t);
409
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200410MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800411static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
412 int ciphersuite,
413 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200414#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800415 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200416#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800417 ssl_tls_prf_t tls_prf,
418 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400419 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800420 unsigned endpoint,
421 const mbedtls_ssl_context *ssl );
422
Andrzej Kurek25f27152022-08-17 16:09:31 -0400423#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200424MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800425static int tls_prf_sha256( const unsigned char *secret, size_t slen,
426 const char *label,
427 const unsigned char *random, size_t rlen,
428 unsigned char *dstbuf, size_t dlen );
429static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
430static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
431
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400432#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800433
Andrzej Kurek25f27152022-08-17 16:09:31 -0400434#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200435MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800436static int tls_prf_sha384( const unsigned char *secret, size_t slen,
437 const char *label,
438 const unsigned char *random, size_t rlen,
439 unsigned char *dstbuf, size_t dlen );
440
441static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
442static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400443#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800444
Jerry Yu438ddd82022-07-07 06:55:50 +0000445static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800446 unsigned char *buf,
447 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800448
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200449MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000450static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800451 const unsigned char *buf,
452 size_t len );
453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
454
Jerry Yu53d23e22022-02-09 16:25:09 +0800455static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
456
Andrzej Kurek25f27152022-08-17 16:09:31 -0400457#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800458static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400459#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800460
Andrzej Kurek25f27152022-08-17 16:09:31 -0400461#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800462static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400463#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000464
Ron Eldor51d3ab52019-05-12 14:54:30 +0300465int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
466 const unsigned char *secret, size_t slen,
467 const char *label,
468 const unsigned char *random, size_t rlen,
469 unsigned char *dstbuf, size_t dlen )
470{
471 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
472
473 switch( prf )
474 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400476#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300477 case MBEDTLS_SSL_TLS_PRF_SHA384:
478 tls_prf = tls_prf_sha384;
479 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400480#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400481#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482 case MBEDTLS_SSL_TLS_PRF_SHA256:
483 tls_prf = tls_prf_sha256;
484 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400485#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487 default:
488 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
489 }
490
491 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
492}
493
Jerry Yuc73c6182022-02-08 20:29:25 +0800494#if defined(MBEDTLS_X509_CRT_PARSE_C)
495static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
496{
497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
498 if( session->peer_cert != NULL )
499 {
500 mbedtls_x509_crt_free( session->peer_cert );
501 mbedtls_free( session->peer_cert );
502 session->peer_cert = NULL;
503 }
504#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
505 if( session->peer_cert_digest != NULL )
506 {
507 /* Zeroization is not necessary. */
508 mbedtls_free( session->peer_cert_digest );
509 session->peer_cert_digest = NULL;
510 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
511 session->peer_cert_digest_len = 0;
512 }
513#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
514}
515#endif /* MBEDTLS_X509_CRT_PARSE_C */
516
517void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
519{
520 ((void) ciphersuite_info);
521
Andrzej Kurek25f27152022-08-17 16:09:31 -0400522#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800523 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
524 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
525 else
526#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400527#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800528 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
529 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
530 else
531#endif
532 {
533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
534 return;
535 }
536}
537
XiaokangQianadab9a62022-07-18 07:41:26 +0000538void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
539 unsigned hs_type,
540 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100541{
542 unsigned char hs_hdr[4];
543
544 /* Build HS header for checksum update. */
545 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
546 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
547 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
548 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
549
550 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
551}
552
553void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
554 unsigned hs_type,
555 unsigned char const *msg,
556 size_t msg_len )
557{
558 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
559 ssl->handshake->update_checksum( ssl, msg, msg_len );
560}
561
Jerry Yuc73c6182022-02-08 20:29:25 +0800562void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
563{
564 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400565#if defined(MBEDTLS_HAS_ALG_SHA_256_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_sha256_psa );
568 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
569#else
570 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
571#endif
572#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400573#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800574#if defined(MBEDTLS_USE_PSA_CRYPTO)
575 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
576 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
577#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400578 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800579#endif
580#endif
581}
582
583static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
584 const unsigned char *buf, size_t len )
585{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400586#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800587#if defined(MBEDTLS_USE_PSA_CRYPTO)
588 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
589#else
590 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
591#endif
592#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
596#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400597 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800598#endif
599#endif
600}
601
Andrzej Kurek25f27152022-08-17 16:09:31 -0400602#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800603static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
604 const unsigned char *buf, size_t len )
605{
606#if defined(MBEDTLS_USE_PSA_CRYPTO)
607 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
608#else
609 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
610#endif
611}
612#endif
613
Andrzej Kurek25f27152022-08-17 16:09:31 -0400614#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800615static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
616 const unsigned char *buf, size_t len )
617{
618#if defined(MBEDTLS_USE_PSA_CRYPTO)
619 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
620#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400621 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800622#endif
623}
624#endif
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200629
Andrzej Kurek25f27152022-08-17 16:09:31 -0400630#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 handshake->fin_sha256_psa = psa_hash_operation_init();
633 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
634#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200636 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200637#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500638#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400639#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500641 handshake->fin_sha384_psa = psa_hash_operation_init();
642 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500643#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400644 mbedtls_sha512_init( &handshake->fin_sha384 );
645 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200646#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500647#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648
649 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_DHM_C)
652 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200653#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200654#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200656#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200657#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200658 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200659#if defined(MBEDTLS_SSL_CLI_C)
660 handshake->ecjpake_cache = NULL;
661 handshake->ecjpake_cache_len = 0;
662#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200663#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200664
Gilles Peskineeccd8882020-03-10 12:19:08 +0100665#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200666 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200667#endif
668
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
670 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
671#endif
Hanno Becker75173122019-02-06 16:18:31 +0000672
673#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
674 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
675 mbedtls_pk_init( &handshake->peer_pubkey );
676#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200677}
678
Hanno Beckera18d1322018-01-03 14:27:32 +0000679void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200680{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200682
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100683#if defined(MBEDTLS_USE_PSA_CRYPTO)
684 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
685 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100686#else
687 mbedtls_cipher_init( &transform->cipher_ctx_enc );
688 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100689#endif
690
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000691#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100692#if defined(MBEDTLS_USE_PSA_CRYPTO)
693 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
694 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100695#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_init( &transform->md_ctx_enc );
697 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000698#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100699#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200700}
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200705}
706
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200707MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000709{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200710 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000711 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200715 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200716 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717
718 /*
719 * Either the pointers are now NULL or cleared properly and can be freed.
720 * Now allocate missing structures.
721 */
722 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200723 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200724 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200725 }
Paul Bakker48916f92012-09-16 19:57:18 +0000726
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200728 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200729 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200730 }
Paul Bakker48916f92012-09-16 19:57:18 +0000731
Paul Bakker82788fb2014-10-20 13:59:19 +0200732 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200733 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200734 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200735 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500736#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
737 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400738
Andrzej Kurek4a063792020-10-21 15:08:44 +0200739 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
740 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500741#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000742
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200743 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000744 if( ssl->handshake == NULL ||
745 ssl->transform_negotiate == NULL ||
746 ssl->session_negotiate == NULL )
747 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_free( ssl->handshake );
751 mbedtls_free( ssl->transform_negotiate );
752 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200753
754 ssl->handshake = NULL;
755 ssl->transform_negotiate = NULL;
756 ssl->session_negotiate = NULL;
757
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000759 }
760
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000763 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200764 ssl_handshake_params_init( ssl->handshake );
765
Jerry Yud0766ec2022-09-22 10:46:57 +0800766#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
767 defined(MBEDTLS_SSL_SRV_C) && \
768 defined(MBEDTLS_SSL_SESSION_TICKETS)
769 ssl->handshake->new_session_tickets_count =
770 ssl->conf->new_session_tickets_count ;
771#endif
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
775 {
776 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200777
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200778 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
779 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
780 else
781 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200782
Hanno Becker0f57a652020-02-05 10:37:26 +0000783 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200784 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200785#endif
786
Brett Warrene0edc842021-08-17 09:53:13 +0100787/*
788 * curve_list is translated to IANA TLS group identifiers here because
789 * mbedtls_ssl_conf_curves returns void and so can't return
790 * any error codes.
791 */
792#if defined(MBEDTLS_ECP_C)
793#if !defined(MBEDTLS_DEPRECATED_REMOVED)
794 /* Heap allocate and translate curve_list from internal to IANA group ids */
795 if ( ssl->conf->curve_list != NULL )
796 {
797 size_t length;
798 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
799
800 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
801 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
802
803 /* Leave room for zero termination */
804 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
805 if ( group_list == NULL )
806 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
807
808 for( size_t i = 0; i < length; i++ )
809 {
810 const mbedtls_ecp_curve_info *info =
811 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
812 if ( info == NULL )
813 {
814 mbedtls_free( group_list );
815 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
816 }
817 group_list[i] = info->tls_id;
818 }
819
820 group_list[length] = 0;
821
822 ssl->handshake->group_list = group_list;
823 ssl->handshake->group_list_heap_allocated = 1;
824 }
825 else
826 {
827 ssl->handshake->group_list = ssl->conf->group_list;
828 ssl->handshake->group_list_heap_allocated = 0;
829 }
830#endif /* MBEDTLS_DEPRECATED_REMOVED */
831#endif /* MBEDTLS_ECP_C */
832
Jerry Yuf017ee42022-01-12 15:49:48 +0800833#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
834#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800835#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800836 /* Heap allocate and translate sig_hashes from internal hash identifiers to
837 signature algorithms IANA identifiers. */
838 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800839 ssl->conf->sig_hashes != NULL )
840 {
841 const int *md;
842 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800843 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800844 uint16_t *p;
845
Jerry Yub476a442022-01-21 18:14:45 +0800846#if defined(static_assert)
847 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
848 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
849 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800850#endif
851
Jerry Yuf017ee42022-01-12 15:49:48 +0800852 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
853 {
854 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
855 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800856#if defined(MBEDTLS_ECDSA_C)
857 sig_algs_len += sizeof( uint16_t );
858#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800859
Jerry Yub476a442022-01-21 18:14:45 +0800860#if defined(MBEDTLS_RSA_C)
861 sig_algs_len += sizeof( uint16_t );
862#endif
863 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
864 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 }
866
Jerry Yub476a442022-01-21 18:14:45 +0800867 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800868 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
869
Jerry Yub476a442022-01-21 18:14:45 +0800870 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
871 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800872 if( ssl->handshake->sig_algs == NULL )
873 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
874
875 p = (uint16_t *)ssl->handshake->sig_algs;
876 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
877 {
878 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
879 if( hash == MBEDTLS_SSL_HASH_NONE )
880 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800881#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
883 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800884#endif
885#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800886 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
887 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800888#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800889 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200890 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800891 ssl->handshake->sig_algs_heap_allocated = 1;
892 }
893 else
Jerry Yua69269a2022-01-17 21:06:01 +0800894#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800896 ssl->handshake->sig_algs_heap_allocated = 0;
897 }
Jerry Yucc539102022-06-27 16:27:35 +0800898#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800899#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000900 return( 0 );
901}
902
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200903#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200904/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200905MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200906static int ssl_cookie_write_dummy( void *ctx,
907 unsigned char **p, unsigned char *end,
908 const unsigned char *cli_id, size_t cli_id_len )
909{
910 ((void) ctx);
911 ((void) p);
912 ((void) end);
913 ((void) cli_id);
914 ((void) cli_id_len);
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917}
918
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200919MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200920static int ssl_cookie_check_dummy( void *ctx,
921 const unsigned char *cookie, size_t cookie_len,
922 const unsigned char *cli_id, size_t cli_id_len )
923{
924 ((void) ctx);
925 ((void) cookie);
926 ((void) cookie_len);
927 ((void) cli_id);
928 ((void) cli_id_len);
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200931}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200932#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200933
Paul Bakker5121ce52009-01-03 21:22:43 +0000934/*
935 * Initialize an SSL context
936 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200937void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
938{
939 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
940}
941
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200942MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800943static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
944{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100945 const mbedtls_ssl_config *conf = ssl->conf;
946
Ronald Cron6f135e12021-12-08 16:57:54 +0100947#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100948 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
949 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000950 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
951 {
952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
953 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
954 }
955
Jerry Yu60835a82021-08-04 10:13:52 +0800956 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
957 return( 0 );
958 }
959#endif
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100962 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800963 {
964 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
965 return( 0 );
966 }
967#endif
968
Ronald Cron6f135e12021-12-08 16:57:54 +0100969#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100970 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800971 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000972 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
973 {
974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
975 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
976 }
977
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000978 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
981 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
982 }
983
984
Ronald Crone1d3f062022-02-10 14:50:54 +0100985 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
986 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800987 }
988#endif
989
990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
991 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
992}
993
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200994MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800995static int ssl_conf_check(const mbedtls_ssl_context *ssl)
996{
997 int ret;
998 ret = ssl_conf_version_check( ssl );
999 if( ret != 0 )
1000 return( ret );
1001
1002 /* Space for further checks */
1003
1004 return( 0 );
1005}
1006
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001007/*
1008 * Setup an SSL context
1009 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001010
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001011int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001012 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013{
Janos Follath865b3eb2019-12-16 11:46:15 +00001014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001015 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1016 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001018 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001019
Jerry Yu60835a82021-08-04 10:13:52 +08001020 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1021 return( ret );
1022
Paul Bakker62f2dee2012-09-28 07:31:51 +00001023 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001024 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001025 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001026
1027 /* Set to NULL in case of an error condition */
1028 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001029
Darryl Greenb33cc762019-11-28 14:29:44 +00001030#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1031 ssl->in_buf_len = in_buf_len;
1032#endif
1033 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001034 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001037 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001038 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001039 }
1040
Darryl Greenb33cc762019-11-28 14:29:44 +00001041#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1042 ssl->out_buf_len = out_buf_len;
1043#endif
1044 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001045 if( ssl->out_buf == NULL )
1046 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001048 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001049 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
1051
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001052 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001053
Johan Pascalb62bb512015-12-03 21:56:45 +01001054#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001055 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001056#endif
1057
Paul Bakker48916f92012-09-16 19:57:18 +00001058 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001059 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
1061 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001062
1063error:
1064 mbedtls_free( ssl->in_buf );
1065 mbedtls_free( ssl->out_buf );
1066
1067 ssl->conf = NULL;
1068
Darryl Greenb33cc762019-11-28 14:29:44 +00001069#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1070 ssl->in_buf_len = 0;
1071 ssl->out_buf_len = 0;
1072#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001073 ssl->in_buf = NULL;
1074 ssl->out_buf = NULL;
1075
1076 ssl->in_hdr = NULL;
1077 ssl->in_ctr = NULL;
1078 ssl->in_len = NULL;
1079 ssl->in_iv = NULL;
1080 ssl->in_msg = NULL;
1081
1082 ssl->out_hdr = NULL;
1083 ssl->out_ctr = NULL;
1084 ssl->out_len = NULL;
1085 ssl->out_iv = NULL;
1086 ssl->out_msg = NULL;
1087
k-stachowiak9f7798e2018-07-31 16:52:32 +02001088 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089}
1090
1091/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001092 * Reset an initialized and used SSL context for re-use while retaining
1093 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001094 *
1095 * If partial is non-zero, keep data in the input buffer and client ID.
1096 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001097 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001098void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1099 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001100{
Darryl Greenb33cc762019-11-28 14:29:44 +00001101#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1102 size_t in_buf_len = ssl->in_buf_len;
1103 size_t out_buf_len = ssl->out_buf_len;
1104#else
1105 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1106 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1107#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001108
Hanno Beckerb0302c42021-08-03 09:39:42 +01001109#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1110 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001111#endif
1112
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001113 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001114 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001115
Hanno Beckerb0302c42021-08-03 09:39:42 +01001116 mbedtls_ssl_reset_in_out_pointers( ssl );
1117
1118 /* Reset incoming message parsing */
1119 ssl->in_offt = NULL;
1120 ssl->nb_zero = 0;
1121 ssl->in_msgtype = 0;
1122 ssl->in_msglen = 0;
1123 ssl->in_hslen = 0;
1124 ssl->keep_current_message = 0;
1125 ssl->transform_in = NULL;
1126
1127#if defined(MBEDTLS_SSL_PROTO_DTLS)
1128 ssl->next_record_offset = 0;
1129 ssl->in_epoch = 0;
1130#endif
1131
1132 /* Keep current datagram if partial == 1 */
1133 if( partial == 0 )
1134 {
1135 ssl->in_left = 0;
1136 memset( ssl->in_buf, 0, in_buf_len );
1137 }
1138
Ronald Cronad8c17b2022-06-10 17:18:09 +02001139 ssl->send_alert = 0;
1140
Hanno Beckerb0302c42021-08-03 09:39:42 +01001141 /* Reset outgoing message writing */
1142 ssl->out_msgtype = 0;
1143 ssl->out_msglen = 0;
1144 ssl->out_left = 0;
1145 memset( ssl->out_buf, 0, out_buf_len );
1146 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1147 ssl->transform_out = NULL;
1148
1149#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1150 mbedtls_ssl_dtls_replay_reset( ssl );
1151#endif
1152
XiaokangQian2b01dc32022-01-21 02:53:13 +00001153#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001154 if( ssl->transform )
1155 {
1156 mbedtls_ssl_transform_free( ssl->transform );
1157 mbedtls_free( ssl->transform );
1158 ssl->transform = NULL;
1159 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001160#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1161
XiaokangQian2b01dc32022-01-21 02:53:13 +00001162#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1163 mbedtls_ssl_transform_free( ssl->transform_application );
1164 mbedtls_free( ssl->transform_application );
1165 ssl->transform_application = NULL;
1166
1167 if( ssl->handshake != NULL )
1168 {
1169 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1170 mbedtls_free( ssl->handshake->transform_earlydata );
1171 ssl->handshake->transform_earlydata = NULL;
1172
1173 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1174 mbedtls_free( ssl->handshake->transform_handshake );
1175 ssl->handshake->transform_handshake = NULL;
1176 }
1177
XiaokangQian2b01dc32022-01-21 02:53:13 +00001178#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001179}
1180
1181int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1182{
1183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1184
1185 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1186
XiaokangQian78b1fa72022-01-19 06:56:30 +00001187 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001188
1189 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_RENEGOTIATION)
1191 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001192 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001193
1194 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1196 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001199
Hanno Beckerb0302c42021-08-03 09:39:42 +01001200 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001201 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001202 if( ssl->session )
1203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_ssl_session_free( ssl->session );
1205 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001206 ssl->session = NULL;
1207 }
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001210 ssl->alpn_chosen = NULL;
1211#endif
1212
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001213#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001214 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001215#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001216 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001217#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001218 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001219 {
1220 mbedtls_free( ssl->cli_id );
1221 ssl->cli_id = NULL;
1222 ssl->cli_id_len = 0;
1223 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001224#endif
1225
Paul Bakker48916f92012-09-16 19:57:18 +00001226 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1227 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001228
1229 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001230}
1231
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001232/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001233 * Reset an initialized and used SSL context for re-use while retaining
1234 * all application-set variables, function pointers and data.
1235 */
1236int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1237{
Hanno Becker43aefe22020-02-05 10:44:56 +00001238 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001239}
1240
1241/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 * SSL set accessors
1243 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001244void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001246 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001247}
1248
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001249void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001250{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001251 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001252}
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001255void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001257 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001258}
1259#endif
1260
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001261void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001262{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001263 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001264}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001267
Hanno Becker1841b0a2018-08-24 11:13:57 +01001268void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1269 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001270{
1271 ssl->disable_datagram_packing = !allow_packing;
1272}
1273
1274void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1275 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001276{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001277 conf->hs_timeout_min = min;
1278 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001279}
1280#endif
1281
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001282void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001284 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001285}
1286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001288void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001289 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001290 void *p_vrfy )
1291{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001292 conf->f_vrfy = f_vrfy;
1293 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001296
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001298 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 void *p_rng )
1300{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001301 conf->f_rng = f_rng;
1302 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001303}
1304
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001305void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001306 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 void *p_dbg )
1308{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001309 conf->f_dbg = f_dbg;
1310 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001311}
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001314 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001315 mbedtls_ssl_send_t *f_send,
1316 mbedtls_ssl_recv_t *f_recv,
1317 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001318{
1319 ssl->p_bio = p_bio;
1320 ssl->f_send = f_send;
1321 ssl->f_recv = f_recv;
1322 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001323}
1324
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001325#if defined(MBEDTLS_SSL_PROTO_DTLS)
1326void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1327{
1328 ssl->mtu = mtu;
1329}
1330#endif
1331
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001332void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001333{
1334 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001335}
1336
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001337void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1338 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001339 mbedtls_ssl_set_timer_t *f_set_timer,
1340 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001341{
1342 ssl->p_timer = p_timer;
1343 ssl->f_set_timer = f_set_timer;
1344 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001345
1346 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001347 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001348}
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001351void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001352 void *p_cache,
1353 mbedtls_ssl_cache_get_t *f_get_cache,
1354 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001355{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001356 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001357 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001358 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_SSL_CLI_C)
1363int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364{
Janos Follath865b3eb2019-12-16 11:46:15 +00001365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001366
1367 if( ssl == NULL ||
1368 session == NULL ||
1369 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001370 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001373 }
1374
Hanno Beckere810bbc2021-05-14 16:01:05 +01001375 if( ssl->handshake->resume == 1 )
1376 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1377
Hanno Becker52055ae2019-02-06 14:30:46 +00001378 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1379 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001380 return( ret );
1381
Paul Bakker0a597072012-09-25 21:55:46 +00001382 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001383
1384 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001388void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1389 const int *ciphersuites )
1390{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001391 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001392}
1393
Ronald Cron6f135e12021-12-08 16:57:54 +01001394#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001395void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001396 const int kex_modes )
1397{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001398 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001399}
Ronald Cron6f135e12021-12-08 16:57:54 +01001400#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001403void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001404 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001405{
1406 conf->cert_profile = profile;
1407}
1408
Glenn Strauss36872db2022-01-22 05:06:31 -05001409static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1410{
1411 mbedtls_ssl_key_cert *cur = key_cert, *next;
1412
1413 while( cur != NULL )
1414 {
1415 next = cur->next;
1416 mbedtls_free( cur );
1417 cur = next;
1418 }
1419}
1420
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001421/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001422MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001423static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1424 mbedtls_x509_crt *cert,
1425 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001426{
niisato8ee24222018-06-25 19:05:48 +09001427 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001428
Glenn Strauss36872db2022-01-22 05:06:31 -05001429 if( cert == NULL )
1430 {
1431 /* Free list if cert is null */
1432 ssl_key_cert_free( *head );
1433 *head = NULL;
1434 return( 0 );
1435 }
1436
niisato8ee24222018-06-25 19:05:48 +09001437 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1438 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001439 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001440
niisato8ee24222018-06-25 19:05:48 +09001441 new_cert->cert = cert;
1442 new_cert->key = key;
1443 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001444
Glenn Strauss36872db2022-01-22 05:06:31 -05001445 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001446 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001447 {
niisato8ee24222018-06-25 19:05:48 +09001448 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001449 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001450 else
1451 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001452 mbedtls_ssl_key_cert *cur = *head;
1453 while( cur->next != NULL )
1454 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001455 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001456 }
1457
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001458 return( 0 );
1459}
1460
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001461int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001462 mbedtls_x509_crt *own_cert,
1463 mbedtls_pk_context *pk_key )
1464{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001465 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001466}
1467
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001468void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001469 mbedtls_x509_crt *ca_chain,
1470 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001471{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001472 conf->ca_chain = ca_chain;
1473 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001474
1475#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1476 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1477 * cannot be used together. */
1478 conf->f_ca_cb = NULL;
1479 conf->p_ca_cb = NULL;
1480#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001481}
Hanno Becker5adaad92019-03-27 16:54:37 +00001482
1483#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1484void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001485 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001486 void *p_ca_cb )
1487{
1488 conf->f_ca_cb = f_ca_cb;
1489 conf->p_ca_cb = p_ca_cb;
1490
1491 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1492 * cannot be used together. */
1493 conf->ca_chain = NULL;
1494 conf->ca_crl = NULL;
1495}
1496#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001498
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001499#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001500const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1501 size_t *name_len )
1502{
1503 *name_len = ssl->handshake->sni_name_len;
1504 return( ssl->handshake->sni_name );
1505}
1506
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001507int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1508 mbedtls_x509_crt *own_cert,
1509 mbedtls_pk_context *pk_key )
1510{
1511 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1512 own_cert, pk_key ) );
1513}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001514
1515void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1516 mbedtls_x509_crt *ca_chain,
1517 mbedtls_x509_crl *ca_crl )
1518{
1519 ssl->handshake->sni_ca_chain = ca_chain;
1520 ssl->handshake->sni_ca_crl = ca_crl;
1521}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001522
Glenn Strauss999ef702022-03-11 01:37:23 -05001523#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1524void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1525 const mbedtls_x509_crt *crt)
1526{
1527 ssl->handshake->dn_hints = crt;
1528}
1529#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1530
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001531void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1532 int authmode )
1533{
1534 ssl->handshake->sni_authmode = authmode;
1535}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001536#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1537
Hanno Becker8927c832019-04-03 12:52:50 +01001538#if defined(MBEDTLS_X509_CRT_PARSE_C)
1539void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1540 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1541 void *p_vrfy )
1542{
1543 ssl->f_vrfy = f_vrfy;
1544 ssl->p_vrfy = p_vrfy;
1545}
1546#endif
1547
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001548#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001549/*
1550 * Set EC J-PAKE password for current handshake
1551 */
1552int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1553 const unsigned char *pw,
1554 size_t pw_len )
1555{
1556 mbedtls_ecjpake_role role;
1557
Janos Follath8eb64132016-06-03 15:40:57 +01001558 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1560
1561 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1562 role = MBEDTLS_ECJPAKE_SERVER;
1563 else
1564 role = MBEDTLS_ECJPAKE_CLIENT;
1565
1566 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1567 role,
1568 MBEDTLS_MD_SHA256,
1569 MBEDTLS_ECP_DP_SECP256R1,
1570 pw, pw_len ) );
1571}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001572#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001573
Gilles Peskineeccd8882020-03-10 12:19:08 +01001574#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001575
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001576MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001577static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1578{
1579#if defined(MBEDTLS_USE_PSA_CRYPTO)
1580 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1581 return( 1 );
1582#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001583 if( conf->psk != NULL )
1584 return( 1 );
1585
1586 return( 0 );
1587}
1588
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001589static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1590{
1591 /* Remove reference to existing PSK, if any. */
1592#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001593 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001594 {
1595 /* The maintenance of the PSK key slot is the
1596 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001597 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001598 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001599#endif /* MBEDTLS_USE_PSA_CRYPTO */
1600 if( conf->psk != NULL )
1601 {
1602 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1603
1604 mbedtls_free( conf->psk );
1605 conf->psk = NULL;
1606 conf->psk_len = 0;
1607 }
1608
1609 /* Remove reference to PSK identity, if any. */
1610 if( conf->psk_identity != NULL )
1611 {
1612 mbedtls_free( conf->psk_identity );
1613 conf->psk_identity = NULL;
1614 conf->psk_identity_len = 0;
1615 }
1616}
1617
Hanno Becker7390c712018-11-15 13:33:04 +00001618/* This function assumes that PSK identity in the SSL config is unset.
1619 * It checks that the provided identity is well-formed and attempts
1620 * to make a copy of it in the SSL config.
1621 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001622MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001623static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1624 unsigned char const *psk_identity,
1625 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001626{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001627 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001628 if( psk_identity == NULL ||
1629 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001630 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001631 {
1632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1633 }
1634
Hanno Becker7390c712018-11-15 13:33:04 +00001635 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1636 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001637 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001638
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001639 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001640 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001641
1642 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001643}
1644
Hanno Becker7390c712018-11-15 13:33:04 +00001645int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1646 const unsigned char *psk, size_t psk_len,
1647 const unsigned char *psk_identity, size_t psk_identity_len )
1648{
Janos Follath865b3eb2019-12-16 11:46:15 +00001649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001650
1651 /* We currently only support one PSK, raw or opaque. */
1652 if( ssl_conf_psk_is_configured( conf ) )
1653 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001654
1655 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001656 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001658 if( psk_len == 0 )
1659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1660 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1662
Hanno Becker7390c712018-11-15 13:33:04 +00001663 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1664 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1665 conf->psk_len = psk_len;
1666 memcpy( conf->psk, psk, conf->psk_len );
1667
1668 /* Check and set PSK Identity */
1669 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1670 if( ret != 0 )
1671 ssl_conf_remove_psk( conf );
1672
1673 return( ret );
1674}
1675
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001676static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1677{
1678#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001679 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001680 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001681 /* The maintenance of the external PSK key slot is the
1682 * user's responsibility. */
1683 if( ssl->handshake->psk_opaque_is_internal )
1684 {
1685 psa_destroy_key( ssl->handshake->psk_opaque );
1686 ssl->handshake->psk_opaque_is_internal = 0;
1687 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001688 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001689 }
Neil Armstronge952a302022-05-03 10:22:14 +02001690#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001691 if( ssl->handshake->psk != NULL )
1692 {
1693 mbedtls_platform_zeroize( ssl->handshake->psk,
1694 ssl->handshake->psk_len );
1695 mbedtls_free( ssl->handshake->psk );
1696 ssl->handshake->psk_len = 0;
1697 }
Neil Armstronge952a302022-05-03 10:22:14 +02001698#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001699}
1700
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001701int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1702 const unsigned char *psk, size_t psk_len )
1703{
Neil Armstrong501c9322022-05-03 09:35:09 +02001704#if defined(MBEDTLS_USE_PSA_CRYPTO)
1705 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001707 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001709#endif /* MBEDTLS_USE_PSA_CRYPTO */
1710
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001711 if( psk == NULL || ssl->handshake == NULL )
1712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1713
1714 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1716
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001717 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001718
Neil Armstrong501c9322022-05-03 09:35:09 +02001719#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001720#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1721 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1722 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001723 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001724 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1725 else
1726 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1727 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1728 }
1729#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001730
Jerry Yu568ec252022-07-22 21:27:34 +08001731#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001732 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1733 {
1734 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1735 psa_set_key_usage_flags( &key_attributes,
1736 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1737 }
1738#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1739
Neil Armstrong501c9322022-05-03 09:35:09 +02001740 psa_set_key_algorithm( &key_attributes, alg );
1741 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1742
1743 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1744 if( status != PSA_SUCCESS )
1745 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1746
1747 /* Allow calling psa_destroy_key() on psk remove */
1748 ssl->handshake->psk_opaque_is_internal = 1;
1749 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1750#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001751 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001752 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001753
1754 ssl->handshake->psk_len = psk_len;
1755 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1756
1757 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001758#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001759}
1760
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001761#if defined(MBEDTLS_USE_PSA_CRYPTO)
1762int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001763 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001764 const unsigned char *psk_identity,
1765 size_t psk_identity_len )
1766{
Janos Follath865b3eb2019-12-16 11:46:15 +00001767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001768
1769 /* We currently only support one PSK, raw or opaque. */
1770 if( ssl_conf_psk_is_configured( conf ) )
1771 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001772
Hanno Becker7390c712018-11-15 13:33:04 +00001773 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001776 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001777
1778 /* Check and set PSK Identity */
1779 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1780 psk_identity_len );
1781 if( ret != 0 )
1782 ssl_conf_remove_psk( conf );
1783
1784 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001785}
1786
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001787int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1788 mbedtls_svc_key_id_t psk )
1789{
1790 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1791 ( ssl->handshake == NULL ) )
1792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1793
1794 ssl_remove_psk( ssl );
1795 ssl->handshake->psk_opaque = psk;
1796 return( 0 );
1797}
1798#endif /* MBEDTLS_USE_PSA_CRYPTO */
1799
1800void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1801 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1802 size_t),
1803 void *p_psk )
1804{
1805 conf->f_psk = f_psk;
1806 conf->p_psk = p_psk;
1807}
1808#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1809
1810#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001811static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1812 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001813{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001814#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001815 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001816 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001817#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001818 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001819 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001820 return( MBEDTLS_SSL_MODE_STREAM );
1821}
1822
1823#else /* MBEDTLS_USE_PSA_CRYPTO */
1824
1825static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1826 mbedtls_cipher_mode_t mode )
1827{
1828#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1829 if( mode == MBEDTLS_MODE_CBC )
1830 return( MBEDTLS_SSL_MODE_CBC );
1831#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1832
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001833#if defined(MBEDTLS_GCM_C) || \
1834 defined(MBEDTLS_CCM_C) || \
1835 defined(MBEDTLS_CHACHAPOLY_C)
1836 if( mode == MBEDTLS_MODE_GCM ||
1837 mode == MBEDTLS_MODE_CCM ||
1838 mode == MBEDTLS_MODE_CHACHAPOLY )
1839 return( MBEDTLS_SSL_MODE_AEAD );
1840#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001841
1842 return( MBEDTLS_SSL_MODE_STREAM );
1843}
Gilles Peskine301711e2022-04-26 16:57:05 +02001844#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845
Gilles Peskinee108d982022-04-26 16:50:40 +02001846static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1847 mbedtls_ssl_mode_t base_mode,
1848 int encrypt_then_mac )
1849{
1850#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1851 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1852 base_mode == MBEDTLS_SSL_MODE_CBC )
1853 {
1854 return( MBEDTLS_SSL_MODE_CBC_ETM );
1855 }
1856#else
1857 (void) encrypt_then_mac;
1858#endif
1859 return( base_mode );
1860}
1861
Neil Armstrongab555e02022-04-04 11:07:59 +02001862mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001863 const mbedtls_ssl_transform *transform )
1864{
Gilles Peskinee108d982022-04-26 16:50:40 +02001865 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001866#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001867 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001868#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001869 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1870#endif
1871 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001872
Gilles Peskinee108d982022-04-26 16:50:40 +02001873 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001874#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001875 encrypt_then_mac = transform->encrypt_then_mac;
1876#endif
1877 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001878}
1879
Neil Armstrongab555e02022-04-04 11:07:59 +02001880mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001881#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001882 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001883#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001884 const mbedtls_ssl_ciphersuite_t *suite )
1885{
Gilles Peskinee108d982022-04-26 16:50:40 +02001886 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1887
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001888#if defined(MBEDTLS_USE_PSA_CRYPTO)
1889 psa_status_t status;
1890 psa_algorithm_t alg;
1891 psa_key_type_t type;
1892 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001893 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1894 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001895 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001896#else
1897 const mbedtls_cipher_info_t *cipher =
1898 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001899 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001900 {
1901 base_mode =
1902 mbedtls_ssl_get_base_mode(
1903 mbedtls_cipher_info_get_mode( cipher ) );
1904 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001905#endif /* MBEDTLS_USE_PSA_CRYPTO */
1906
Gilles Peskinee108d982022-04-26 16:50:40 +02001907#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1908 int encrypt_then_mac = 0;
1909#endif
1910 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001911}
1912
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001913#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001914
1915#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001916/* Serialization of TLS 1.3 sessions:
1917 *
1918 * struct {
1919 * uint64 ticket_received;
1920 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001921 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001922 * } ClientOnlyData;
1923 *
1924 * struct {
1925 * uint8 endpoint;
1926 * uint8 ciphersuite[2];
1927 * uint32 ticket_age_add;
1928 * uint8 ticket_flags;
1929 * opaque resumption_key<0..255>;
1930 * select ( endpoint ) {
1931 * case client: ClientOnlyData;
1932 * case server: uint64 start_time;
1933 * };
1934 * } serialized_session_tls13;
1935 *
1936 */
1937#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001938MBEDTLS_CHECK_RETURN_CRITICAL
1939static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1940 unsigned char *buf,
1941 size_t buf_len,
1942 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001943{
1944 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001945 size_t needed = 1 /* endpoint */
1946 + 2 /* ciphersuite */
1947 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001948 + 1 /* ticket_flags */
1949 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001950 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001951
1952 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1954 needed += session->resumption_key_len; /* resumption_key */
1955
Jerry Yu438ddd82022-07-07 06:55:50 +00001956#if defined(MBEDTLS_HAVE_TIME)
1957 needed += 8; /* start_time or ticket_received */
1958#endif
1959
1960#if defined(MBEDTLS_SSL_CLI_C)
1961 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1962 {
1963 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001964 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001965
1966 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001967 if( session->ticket_len > SIZE_MAX - needed )
1968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001969
Jerry Yue28d9742022-08-18 15:44:03 +08001970 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001971 }
1972#endif /* MBEDTLS_SSL_CLI_C */
1973
Jerry Yue36fdd62022-08-17 21:31:36 +08001974 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001975 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001976 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001977
1978 p[0] = session->endpoint;
1979 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1980 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1981 p[7] = session->ticket_flags;
1982
1983 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001984 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001985 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001986 memcpy( p, session->resumption_key, session->resumption_key_len );
1987 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001988
1989#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1990 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1991 {
1992 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1993 p += 8;
1994 }
1995#endif /* MBEDTLS_HAVE_TIME */
1996
1997#if defined(MBEDTLS_SSL_CLI_C)
1998 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1999 {
2000#if defined(MBEDTLS_HAVE_TIME)
2001 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2002 p += 8;
2003#endif
2004 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2005 p += 4;
2006
2007 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2008 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002009
2010 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002011 {
2012 memcpy( p, session->ticket, session->ticket_len );
2013 p += session->ticket_len;
2014 }
2015 }
2016#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002017 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002018}
2019
2020MBEDTLS_CHECK_RETURN_CRITICAL
2021static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2022 const unsigned char *buf,
2023 size_t len )
2024{
2025 const unsigned char *p = buf;
2026 const unsigned char *end = buf + len;
2027
2028 if( end - p < 9 )
2029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2030 session->endpoint = p[0];
2031 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2032 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2033 session->ticket_flags = p[7];
2034
2035 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002036 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002037 p += 9;
2038
Jerry Yubc7c1a42022-07-21 22:57:37 +08002039 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2041
Jerry Yubc7c1a42022-07-21 22:57:37 +08002042 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002044 memcpy( session->resumption_key, p, session->resumption_key_len );
2045 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002046
2047#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2048 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2049 {
2050 if( end - p < 8 )
2051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2052 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2053 p += 8;
2054 }
2055#endif /* MBEDTLS_HAVE_TIME */
2056
2057#if defined(MBEDTLS_SSL_CLI_C)
2058 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2059 {
2060#if defined(MBEDTLS_HAVE_TIME)
2061 if( end - p < 8 )
2062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2063 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2064 p += 8;
2065#endif
2066 if( end - p < 4 )
2067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2068 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2069 p += 4;
2070
2071 if( end - p < 2 )
2072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2073 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2074 p += 2;
2075
2076 if( end - p < ( long int )session->ticket_len )
2077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2078 if( session->ticket_len > 0 )
2079 {
2080 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2081 if( session->ticket == NULL )
2082 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2083 memcpy( session->ticket, p, session->ticket_len );
2084 p += session->ticket_len;
2085 }
2086 }
2087#endif /* MBEDTLS_SSL_CLI_C */
2088
2089 return( 0 );
2090
2091}
2092#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002093MBEDTLS_CHECK_RETURN_CRITICAL
2094static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2095 unsigned char *buf,
2096 size_t buf_len,
2097 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002098{
2099 ((void) session);
2100 ((void) buf);
2101 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002102 *olen = 0;
2103 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002104}
Jerry Yu438ddd82022-07-07 06:55:50 +00002105
2106static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2107 unsigned char *buf,
2108 size_t buf_len )
2109{
2110 ((void) session);
2111 ((void) buf);
2112 ((void) buf_len);
2113 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2114}
2115#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002116#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2117
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002118psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002119 size_t taglen,
2120 psa_algorithm_t *alg,
2121 psa_key_type_t *key_type,
2122 size_t *key_size )
2123{
2124 switch ( mbedtls_cipher_type )
2125 {
2126 case MBEDTLS_CIPHER_AES_128_CBC:
2127 *alg = PSA_ALG_CBC_NO_PADDING;
2128 *key_type = PSA_KEY_TYPE_AES;
2129 *key_size = 128;
2130 break;
2131 case MBEDTLS_CIPHER_AES_128_CCM:
2132 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2133 *key_type = PSA_KEY_TYPE_AES;
2134 *key_size = 128;
2135 break;
2136 case MBEDTLS_CIPHER_AES_128_GCM:
2137 *alg = PSA_ALG_GCM;
2138 *key_type = PSA_KEY_TYPE_AES;
2139 *key_size = 128;
2140 break;
2141 case MBEDTLS_CIPHER_AES_192_CCM:
2142 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2143 *key_type = PSA_KEY_TYPE_AES;
2144 *key_size = 192;
2145 break;
2146 case MBEDTLS_CIPHER_AES_192_GCM:
2147 *alg = PSA_ALG_GCM;
2148 *key_type = PSA_KEY_TYPE_AES;
2149 *key_size = 192;
2150 break;
2151 case MBEDTLS_CIPHER_AES_256_CBC:
2152 *alg = PSA_ALG_CBC_NO_PADDING;
2153 *key_type = PSA_KEY_TYPE_AES;
2154 *key_size = 256;
2155 break;
2156 case MBEDTLS_CIPHER_AES_256_CCM:
2157 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2158 *key_type = PSA_KEY_TYPE_AES;
2159 *key_size = 256;
2160 break;
2161 case MBEDTLS_CIPHER_AES_256_GCM:
2162 *alg = PSA_ALG_GCM;
2163 *key_type = PSA_KEY_TYPE_AES;
2164 *key_size = 256;
2165 break;
2166 case MBEDTLS_CIPHER_ARIA_128_CBC:
2167 *alg = PSA_ALG_CBC_NO_PADDING;
2168 *key_type = PSA_KEY_TYPE_ARIA;
2169 *key_size = 128;
2170 break;
2171 case MBEDTLS_CIPHER_ARIA_128_CCM:
2172 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2173 *key_type = PSA_KEY_TYPE_ARIA;
2174 *key_size = 128;
2175 break;
2176 case MBEDTLS_CIPHER_ARIA_128_GCM:
2177 *alg = PSA_ALG_GCM;
2178 *key_type = PSA_KEY_TYPE_ARIA;
2179 *key_size = 128;
2180 break;
2181 case MBEDTLS_CIPHER_ARIA_192_CCM:
2182 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2183 *key_type = PSA_KEY_TYPE_ARIA;
2184 *key_size = 192;
2185 break;
2186 case MBEDTLS_CIPHER_ARIA_192_GCM:
2187 *alg = PSA_ALG_GCM;
2188 *key_type = PSA_KEY_TYPE_ARIA;
2189 *key_size = 192;
2190 break;
2191 case MBEDTLS_CIPHER_ARIA_256_CBC:
2192 *alg = PSA_ALG_CBC_NO_PADDING;
2193 *key_type = PSA_KEY_TYPE_ARIA;
2194 *key_size = 256;
2195 break;
2196 case MBEDTLS_CIPHER_ARIA_256_CCM:
2197 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2198 *key_type = PSA_KEY_TYPE_ARIA;
2199 *key_size = 256;
2200 break;
2201 case MBEDTLS_CIPHER_ARIA_256_GCM:
2202 *alg = PSA_ALG_GCM;
2203 *key_type = PSA_KEY_TYPE_ARIA;
2204 *key_size = 256;
2205 break;
2206 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2207 *alg = PSA_ALG_CBC_NO_PADDING;
2208 *key_type = PSA_KEY_TYPE_CAMELLIA;
2209 *key_size = 128;
2210 break;
2211 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2212 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2213 *key_type = PSA_KEY_TYPE_CAMELLIA;
2214 *key_size = 128;
2215 break;
2216 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2217 *alg = PSA_ALG_GCM;
2218 *key_type = PSA_KEY_TYPE_CAMELLIA;
2219 *key_size = 128;
2220 break;
2221 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2222 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2223 *key_type = PSA_KEY_TYPE_CAMELLIA;
2224 *key_size = 192;
2225 break;
2226 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2227 *alg = PSA_ALG_GCM;
2228 *key_type = PSA_KEY_TYPE_CAMELLIA;
2229 *key_size = 192;
2230 break;
2231 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2232 *alg = PSA_ALG_CBC_NO_PADDING;
2233 *key_type = PSA_KEY_TYPE_CAMELLIA;
2234 *key_size = 256;
2235 break;
2236 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2237 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2238 *key_type = PSA_KEY_TYPE_CAMELLIA;
2239 *key_size = 256;
2240 break;
2241 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2242 *alg = PSA_ALG_GCM;
2243 *key_type = PSA_KEY_TYPE_CAMELLIA;
2244 *key_size = 256;
2245 break;
2246 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2247 *alg = PSA_ALG_CHACHA20_POLY1305;
2248 *key_type = PSA_KEY_TYPE_CHACHA20;
2249 *key_size = 256;
2250 break;
2251 case MBEDTLS_CIPHER_NULL:
2252 *alg = MBEDTLS_SSL_NULL_CIPHER;
2253 *key_type = 0;
2254 *key_size = 0;
2255 break;
2256 default:
2257 return PSA_ERROR_NOT_SUPPORTED;
2258 }
2259
2260 return PSA_SUCCESS;
2261}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002262#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002263
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002264#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002265int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2266 const unsigned char *dhm_P, size_t P_len,
2267 const unsigned char *dhm_G, size_t G_len )
2268{
Janos Follath865b3eb2019-12-16 11:46:15 +00002269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002270
Glenn Strausscee11292021-12-20 01:43:17 -05002271 mbedtls_mpi_free( &conf->dhm_P );
2272 mbedtls_mpi_free( &conf->dhm_G );
2273
Hanno Beckera90658f2017-10-04 15:29:08 +01002274 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2275 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2276 {
2277 mbedtls_mpi_free( &conf->dhm_P );
2278 mbedtls_mpi_free( &conf->dhm_G );
2279 return( ret );
2280 }
2281
2282 return( 0 );
2283}
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002285int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002286{
Janos Follath865b3eb2019-12-16 11:46:15 +00002287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002288
Glenn Strausscee11292021-12-20 01:43:17 -05002289 mbedtls_mpi_free( &conf->dhm_P );
2290 mbedtls_mpi_free( &conf->dhm_G );
2291
Gilles Peskinee5702482021-06-11 21:59:08 +02002292 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2293 &conf->dhm_P ) ) != 0 ||
2294 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2295 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002296 {
2297 mbedtls_mpi_free( &conf->dhm_P );
2298 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002299 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002300 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002301
2302 return( 0 );
2303}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002304#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002305
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002306#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2307/*
2308 * Set the minimum length for Diffie-Hellman parameters
2309 */
2310void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2311 unsigned int bitlen )
2312{
2313 conf->dhm_min_bitlen = bitlen;
2314}
2315#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2316
Gilles Peskineeccd8882020-03-10 12:19:08 +01002317#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002318#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002319/*
2320 * Set allowed/preferred hashes for handshake signatures
2321 */
2322void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2323 const int *hashes )
2324{
2325 conf->sig_hashes = hashes;
2326}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002327#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002328
Jerry Yuf017ee42022-01-12 15:49:48 +08002329/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002330void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2331 const uint16_t* sig_algs )
2332{
Jerry Yuf017ee42022-01-12 15:49:48 +08002333#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2334 conf->sig_hashes = NULL;
2335#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2336 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002337}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002338#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002339
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002340#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002341#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002342/*
2343 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002344 *
2345 * mbedtls_ssl_setup() takes the provided list
2346 * and translates it to a list of IANA TLS group identifiers,
2347 * stored in ssl->handshake->group_list.
2348 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002349 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002350void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002351 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002352{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002353 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002354 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002355}
Brett Warrene0edc842021-08-17 09:53:13 +01002356#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002357#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002358
Brett Warrene0edc842021-08-17 09:53:13 +01002359/*
2360 * Set the allowed groups
2361 */
2362void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2363 const uint16_t *group_list )
2364{
2365#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2366 conf->curve_list = NULL;
2367#endif
2368 conf->group_list = group_list;
2369}
2370
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002371#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373{
Hanno Becker947194e2017-04-07 13:25:49 +01002374 /* Initialize to suppress unnecessary compiler warning */
2375 size_t hostname_len = 0;
2376
2377 /* Check if new hostname is valid before
2378 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002379 if( hostname != NULL )
2380 {
2381 hostname_len = strlen( hostname );
2382
2383 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2385 }
2386
2387 /* Now it's clear that we will overwrite the old hostname,
2388 * so we can free it safely */
2389
2390 if( ssl->hostname != NULL )
2391 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002392 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002393 mbedtls_free( ssl->hostname );
2394 }
2395
2396 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002397
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002399 {
2400 ssl->hostname = NULL;
2401 }
2402 else
2403 {
2404 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002405 if( ssl->hostname == NULL )
2406 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002407
Hanno Becker947194e2017-04-07 13:25:49 +01002408 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002409
Hanno Becker947194e2017-04-07 13:25:49 +01002410 ssl->hostname[hostname_len] = '\0';
2411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 return( 0 );
2414}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002415#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002417#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002418void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002420 const unsigned char *, size_t),
2421 void *p_sni )
2422{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002423 conf->f_sni = f_sni;
2424 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002429int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002430{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002431 size_t cur_len, tot_len;
2432 const char **p;
2433
2434 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002435 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2436 * MUST NOT be truncated."
2437 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002438 */
2439 tot_len = 0;
2440 for( p = protos; *p != NULL; p++ )
2441 {
2442 cur_len = strlen( *p );
2443 tot_len += cur_len;
2444
Ronald Cron8216dd32020-04-23 16:41:44 +02002445 if( ( cur_len == 0 ) ||
2446 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2447 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002449 }
2450
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002451 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002452
2453 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002454}
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002457{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002458 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002459}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002461
Johan Pascalb62bb512015-12-03 21:56:45 +01002462#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002463void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2464 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002465{
2466 conf->dtls_srtp_mki_support = support_mki_value;
2467}
2468
Ron Eldoref72faf2018-07-12 11:54:20 +03002469int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2470 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002471 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002472{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002473 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002474 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002476 }
Ron Eldor591f1622018-01-22 12:30:04 +02002477
2478 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002479 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002480 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002481 }
Ron Eldor591f1622018-01-22 12:30:04 +02002482
2483 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2484 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002485 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002486}
2487
Ron Eldoref72faf2018-07-12 11:54:20 +03002488int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002489 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002490{
Johan Pascal253d0262020-09-22 13:04:45 +02002491 const mbedtls_ssl_srtp_profile *p;
2492 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002493
Johan Pascal253d0262020-09-22 13:04:45 +02002494 /* check the profiles list: all entry must be valid,
2495 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002496 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2497 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2498 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002499 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002500 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002501 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002502 list_size++;
2503 }
2504 else
2505 {
2506 /* unsupported value, stop parsing and set the size to an error value */
2507 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002508 }
2509 }
2510
Johan Pascal5ef72d22020-10-28 17:05:47 +01002511 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002512 {
Johan Pascal253d0262020-09-22 13:04:45 +02002513 conf->dtls_srtp_profile_list = NULL;
2514 conf->dtls_srtp_profile_list_len = 0;
2515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2516 }
2517
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002518 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002519 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002520
2521 return( 0 );
2522}
2523
Johan Pascal5ef72d22020-10-28 17:05:47 +01002524void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2525 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002526{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002527 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2528 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002529 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002530 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002531 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002532 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002533 else
2534 {
2535 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002536 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2537 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002538 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002539}
Johan Pascalb62bb512015-12-03 21:56:45 +01002540#endif /* MBEDTLS_SSL_DTLS_SRTP */
2541
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302542#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002543void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002544{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002545 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002546}
2547
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002548void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002549{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002550 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002551}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302552#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002553
Janos Follath088ce432017-04-10 12:42:31 +01002554#if defined(MBEDTLS_SSL_SRV_C)
2555void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2556 char cert_req_ca_list )
2557{
2558 conf->cert_req_ca_list = cert_req_ca_list;
2559}
2560#endif
2561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002563void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002565 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002566}
2567#endif
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002570void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002572 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002573}
2574#endif
2575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002577int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002580 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002583 }
2584
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002585 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002586
2587 return( 0 );
2588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002590
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002591void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002593 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002594}
2595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002597void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002599 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002600}
2601
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002602void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002604 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002605}
2606
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002607void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002608 const unsigned char period[8] )
2609{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002610 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002615#if defined(MBEDTLS_SSL_CLI_C)
2616void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002617{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002618 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002619}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002620#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002621
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002622#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002623
Jerry Yud0766ec2022-09-22 10:46:57 +08002624#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002625void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2626 uint16_t num_tickets )
2627{
Jerry Yud0766ec2022-09-22 10:46:57 +08002628 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002629}
2630#endif
2631
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002632void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2633 mbedtls_ssl_ticket_write_t *f_ticket_write,
2634 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2635 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002636{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002637 conf->f_ticket_write = f_ticket_write;
2638 conf->f_ticket_parse = f_ticket_parse;
2639 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002640}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002641#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002643
Hanno Becker7e6c1782021-06-08 09:24:55 +01002644void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2645 mbedtls_ssl_export_keys_t *f_export_keys,
2646 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002647{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002648 ssl->f_export_keys = f_export_keys;
2649 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002650}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002651
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002652#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002653void mbedtls_ssl_conf_async_private_cb(
2654 mbedtls_ssl_config *conf,
2655 mbedtls_ssl_async_sign_t *f_async_sign,
2656 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2657 mbedtls_ssl_async_resume_t *f_async_resume,
2658 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002659 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002660{
2661 conf->f_async_sign_start = f_async_sign;
2662 conf->f_async_decrypt_start = f_async_decrypt;
2663 conf->f_async_resume = f_async_resume;
2664 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002665 conf->p_async_config_data = async_config_data;
2666}
2667
Gilles Peskine8f97af72018-04-26 11:46:10 +02002668void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2669{
2670 return( conf->p_async_config_data );
2671}
2672
Gilles Peskine1febfef2018-04-30 11:54:39 +02002673void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002674{
2675 if( ssl->handshake == NULL )
2676 return( NULL );
2677 else
2678 return( ssl->handshake->user_async_ctx );
2679}
2680
Gilles Peskine1febfef2018-04-30 11:54:39 +02002681void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002682 void *ctx )
2683{
2684 if( ssl->handshake != NULL )
2685 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002686}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002687#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002688
Paul Bakker5121ce52009-01-03 21:22:43 +00002689/*
2690 * SSL get accessors
2691 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002692uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002694 if( ssl->session != NULL )
2695 return( ssl->session->verify_result );
2696
2697 if( ssl->session_negotiate != NULL )
2698 return( ssl->session_negotiate->verify_result );
2699
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002700 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701}
2702
Glenn Strauss8f526902022-01-13 00:04:49 -05002703int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2704{
2705 if( ssl == NULL || ssl->session == NULL )
2706 return( 0 );
2707
2708 return( ssl->session->ciphersuite );
2709}
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002712{
Paul Bakker926c8e42013-03-06 10:23:34 +01002713 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002714 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002717}
2718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002722 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002723 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002724 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002725 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002726 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002727 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002728 default:
2729 return( "unknown (DTLS)" );
2730 }
2731 }
2732#endif
2733
Glenn Strauss60bfe602022-03-14 19:04:24 -04002734 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002735 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002736 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002737 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002738 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002739 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002740 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002741 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002742 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002743}
2744
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002745#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002746size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2747{
David Horstmann95d516f2021-05-04 18:36:56 +01002748 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002749 size_t read_mfl;
2750
2751 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2752 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2753 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2754 {
2755 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2756 }
2757
2758 /* Check if a smaller max length was negotiated */
2759 if( ssl->session_out != NULL )
2760 {
2761 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2762 if( read_mfl < max_len )
2763 {
2764 max_len = read_mfl;
2765 }
2766 }
2767
2768 // During a handshake, use the value being negotiated
2769 if( ssl->session_negotiate != NULL )
2770 {
2771 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2772 if( read_mfl < max_len )
2773 {
2774 max_len = read_mfl;
2775 }
2776 }
2777
2778 return( max_len );
2779}
2780
2781size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002782{
2783 size_t max_len;
2784
2785 /*
2786 * Assume mfl_code is correct since it was checked when set
2787 */
Angus Grattond8213d02016-05-25 20:56:48 +10002788 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002789
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002790 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002791 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002792 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002793 {
Angus Grattond8213d02016-05-25 20:56:48 +10002794 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002795 }
2796
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002797 /* During a handshake, use the value being negotiated */
2798 if( ssl->session_negotiate != NULL &&
2799 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2800 {
2801 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2802 }
2803
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002804 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002805}
2806#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2807
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002808#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002809size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002810{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002811 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2812 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2813 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2814 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2815 return ( 0 );
2816
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002817 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2818 return( ssl->mtu );
2819
2820 if( ssl->mtu == 0 )
2821 return( ssl->handshake->mtu );
2822
2823 return( ssl->mtu < ssl->handshake->mtu ?
2824 ssl->mtu : ssl->handshake->mtu );
2825}
2826#endif /* MBEDTLS_SSL_PROTO_DTLS */
2827
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002828int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2829{
2830 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2831
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002832#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2833 !defined(MBEDTLS_SSL_PROTO_DTLS)
2834 (void) ssl;
2835#endif
2836
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002837#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002838 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002839
2840 if( max_len > mfl )
2841 max_len = mfl;
2842#endif
2843
2844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002845 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002846 {
Hanno Becker89490712020-02-05 10:50:12 +00002847 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002848 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2849 const size_t overhead = (size_t) ret;
2850
2851 if( ret < 0 )
2852 return( ret );
2853
2854 if( mtu <= overhead )
2855 {
2856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2857 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2858 }
2859
2860 if( max_len > mtu - overhead )
2861 max_len = mtu - overhead;
2862 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002863#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002864
Hanno Becker0defedb2018-08-10 12:35:02 +01002865#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2866 !defined(MBEDTLS_SSL_PROTO_DTLS)
2867 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002868#endif
2869
2870 return( (int) max_len );
2871}
2872
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002873int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2874{
2875 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2876
2877#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2878 (void) ssl;
2879#endif
2880
2881#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2882 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2883
2884 if( max_len > mfl )
2885 max_len = mfl;
2886#endif
2887
2888 return( (int) max_len );
2889}
2890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891#if defined(MBEDTLS_X509_CRT_PARSE_C)
2892const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002893{
2894 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002895 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002896
Hanno Beckere6824572019-02-07 13:18:46 +00002897#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002898 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002899#else
2900 return( NULL );
2901#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002906int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2907 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002908{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002909 int ret;
2910
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002911 if( ssl == NULL ||
2912 dst == NULL ||
2913 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002914 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002917 }
2918
Hanno Beckere810bbc2021-05-14 16:01:05 +01002919 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2920 * idempotent: Each session can only be exported once.
2921 *
2922 * (This is in preparation for TLS 1.3 support where we will
2923 * need the ability to export multiple sessions (aka tickets),
2924 * which will be achieved by calling mbedtls_ssl_get_session()
2925 * multiple times until it fails.)
2926 *
2927 * Check whether we have already exported the current session,
2928 * and fail if so.
2929 */
2930 if( ssl->session->exported == 1 )
2931 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2932
2933 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2934 if( ret != 0 )
2935 return( ret );
2936
2937 /* Remember that we've exported the session. */
2938 ssl->session->exported = 1;
2939 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002942
Paul Bakker5121ce52009-01-03 21:22:43 +00002943/*
Hanno Beckera835da52019-05-16 12:39:07 +01002944 * Define ticket header determining Mbed TLS version
2945 * and structure of the ticket.
2946 */
2947
Hanno Becker94ef3b32019-05-16 12:50:45 +01002948/*
Hanno Becker50b59662019-05-28 14:30:45 +01002949 * Define bitflag determining compile-time settings influencing
2950 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002951 */
2952
Hanno Becker50b59662019-05-28 14:30:45 +01002953#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002954#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002955#else
Hanno Becker3e088662019-05-29 11:10:18 +01002956#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002957#endif /* MBEDTLS_HAVE_TIME */
2958
2959#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002960#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002961#else
Hanno Becker3e088662019-05-29 11:10:18 +01002962#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002963#endif /* MBEDTLS_X509_CRT_PARSE_C */
2964
2965#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002966#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002967#else
Hanno Becker3e088662019-05-29 11:10:18 +01002968#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002969#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2970
2971#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002972#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002973#else
Hanno Becker3e088662019-05-29 11:10:18 +01002974#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002975#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2976
Hanno Becker94ef3b32019-05-16 12:50:45 +01002977#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002978#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002979#else
Hanno Becker3e088662019-05-29 11:10:18 +01002980#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002981#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2982
Hanno Becker94ef3b32019-05-16 12:50:45 +01002983#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2984#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2985#else
2986#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2987#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2988
Hanno Becker3e088662019-05-29 11:10:18 +01002989#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2990#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2991#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2992#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002993#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2994#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002995
Hanno Becker50b59662019-05-28 14:30:45 +01002996#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002997 ( (uint16_t) ( \
2998 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2999 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3000 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3001 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003002 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003003 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003004
Hanno Beckerf8787072019-05-16 12:41:07 +01003005static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003006 MBEDTLS_VERSION_MAJOR,
3007 MBEDTLS_VERSION_MINOR,
3008 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003009 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3010 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003011};
Hanno Beckera835da52019-05-16 12:39:07 +01003012
3013/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003014 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003015 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003016 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003017 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003018 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003019 * opaque mbedtls_version[3]; // library version: major, minor, patch
3020 * opaque session_format[2]; // library-version specific 16-bit field
3021 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003022 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003023 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003024 * Note: When updating the format, remember to keep
3025 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003026 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003027 * // In this version, `session_format` determines
3028 * // the setting of those compile-time
3029 * // configuration options which influence
3030 * // the structure of mbedtls_ssl_session.
3031 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003032 * uint8_t minor_ver; // Protocol minor version. Possible values:
3033 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003034 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003035 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003036 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003037 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003038 * serialized_session_tls12 data;
3039 *
3040 * };
3041 *
3042 * } serialized_session;
3043 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003044 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003045
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003046MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003047static int ssl_session_save( const mbedtls_ssl_session *session,
3048 unsigned char omit_header,
3049 unsigned char *buf,
3050 size_t buf_len,
3051 size_t *olen )
3052{
3053 unsigned char *p = buf;
3054 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003055 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003056#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3057 size_t out_len;
3058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3059#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003060 if( session == NULL )
3061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3062
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003063 if( !omit_header )
3064 {
3065 /*
3066 * Add Mbed TLS version identifier
3067 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003068 used += sizeof( ssl_serialized_session_header );
3069
3070 if( used <= buf_len )
3071 {
3072 memcpy( p, ssl_serialized_session_header,
3073 sizeof( ssl_serialized_session_header ) );
3074 p += sizeof( ssl_serialized_session_header );
3075 }
3076 }
3077
3078 /*
3079 * TLS version identifier
3080 */
3081 used += 1;
3082 if( used <= buf_len )
3083 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003084 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003085 }
3086
3087 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003088 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003089 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003090 {
3091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003092 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003093 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003094 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003095#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3096
Jerry Yu251a12e2022-07-13 15:15:48 +08003097#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3098 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003099 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3100 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003101 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003102 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003103 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003104#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3105
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003106 default:
3107 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3108 }
3109
3110 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003111 if( used > buf_len )
3112 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003113
3114 return( 0 );
3115}
3116
3117/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003118 * Public wrapper for ssl_session_save()
3119 */
3120int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3121 unsigned char *buf,
3122 size_t buf_len,
3123 size_t *olen )
3124{
3125 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3126}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003127
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003128/*
3129 * Deserialize session, see mbedtls_ssl_session_save() for format.
3130 *
3131 * This internal version is wrapped by a public function that cleans up in
3132 * case of error, and has an extra option omit_header.
3133 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003134MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003135static int ssl_session_load( mbedtls_ssl_session *session,
3136 unsigned char omit_header,
3137 const unsigned char *buf,
3138 size_t len )
3139{
3140 const unsigned char *p = buf;
3141 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003142 size_t remaining_len;
3143
3144
3145 if( session == NULL )
3146 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003147
3148 if( !omit_header )
3149 {
3150 /*
3151 * Check Mbed TLS version identifier
3152 */
3153
3154 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3156
3157 if( memcmp( p, ssl_serialized_session_header,
3158 sizeof( ssl_serialized_session_header ) ) != 0 )
3159 {
3160 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3161 }
3162 p += sizeof( ssl_serialized_session_header );
3163 }
3164
3165 /*
3166 * TLS version identifier
3167 */
3168 if( 1 > (size_t)( end - p ) )
3169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003170 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003171
3172 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003173 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003174 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003175 {
3176#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003177 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003178 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003179#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3180
Jerry Yu438ddd82022-07-07 06:55:50 +00003181#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3182 case MBEDTLS_SSL_VERSION_TLS1_3:
3183 return( ssl_tls13_session_load( session, p, remaining_len ) );
3184#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3185
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003186 default:
3187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3188 }
3189}
3190
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003191/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003192 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003193 */
3194int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3195 const unsigned char *buf,
3196 size_t len )
3197{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003198 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003199
3200 if( ret != 0 )
3201 mbedtls_ssl_session_free( session );
3202
3203 return( ret );
3204}
3205
3206/*
Paul Bakker1961b702013-01-25 14:49:24 +01003207 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003209MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003210static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3211{
3212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3213
Ronald Cron66dbf912022-02-02 15:33:46 +01003214 /*
3215 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003216 * that were written into the output buffer by the previous handshake step,
3217 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003218 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3219 * We proceed to the next handshake step only when all data from the
3220 * previous one have been sent to the peer, thus we make sure that this is
3221 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3222 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3223 * we have to wait before to go ahead.
3224 * In the case of TLS 1.3, handshake step handlers do not send data to the
3225 * peer. Data are only sent here and through
3226 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003227 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003228 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003229 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3230 return( ret );
3231
3232#if defined(MBEDTLS_SSL_PROTO_DTLS)
3233 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3234 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3235 {
3236 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3237 return( ret );
3238 }
3239#endif /* MBEDTLS_SSL_PROTO_DTLS */
3240
3241 return( ret );
3242}
3243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003245{
Hanno Becker41934dd2021-08-07 19:13:43 +01003246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003247
Hanno Becker41934dd2021-08-07 19:13:43 +01003248 if( ssl == NULL ||
3249 ssl->conf == NULL ||
3250 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003251 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003252 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003254 }
3255
3256 ret = ssl_prepare_handshake_step( ssl );
3257 if( ret != 0 )
3258 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003259
Jerry Yue7047812021-09-13 19:26:39 +08003260 ret = mbedtls_ssl_handle_pending_alert( ssl );
3261 if( ret != 0 )
3262 goto cleanup;
3263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003266 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3268 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003269
Ronald Cron9f0fba32022-02-10 16:45:15 +01003270 switch( ssl->state )
3271 {
3272 case MBEDTLS_SSL_HELLO_REQUEST:
3273 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3274 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003275
Ronald Cron9f0fba32022-02-10 16:45:15 +01003276 case MBEDTLS_SSL_CLIENT_HELLO:
3277 ret = mbedtls_ssl_write_client_hello( ssl );
3278 break;
3279
3280 default:
3281#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003282 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003283 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3284 else
3285 ret = mbedtls_ssl_handshake_client_step( ssl );
3286#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3287 ret = mbedtls_ssl_handshake_client_step( ssl );
3288#else
3289 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3290#endif
3291 }
Jerry Yub9930e72021-08-06 17:11:51 +08003292 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003295 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003296 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003297#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003298 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003299 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003300#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003301
3302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3303 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3304 ret = mbedtls_ssl_handshake_server_step( ssl );
3305#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003307#endif
3308
Jerry Yue7047812021-09-13 19:26:39 +08003309 if( ret != 0 )
3310 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003311 /* handshake_step return error. And it is same
3312 * with alert_reason.
3313 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003314 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003315 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003316 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003317 goto cleanup;
3318 }
3319 }
3320
3321cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003322 return( ret );
3323}
3324
3325/*
3326 * Perform the SSL handshake
3327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003329{
3330 int ret = 0;
3331
Hanno Beckera817ea42020-10-20 15:20:23 +01003332 /* Sanity checks */
3333
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003334 if( ssl == NULL || ssl->conf == NULL )
3335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3336
Hanno Beckera817ea42020-10-20 15:20:23 +01003337#if defined(MBEDTLS_SSL_PROTO_DTLS)
3338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3339 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3340 {
3341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3342 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3344 }
3345#endif /* MBEDTLS_SSL_PROTO_DTLS */
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003348
Hanno Beckera817ea42020-10-20 15:20:23 +01003349 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003350 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003353
3354 if( ret != 0 )
3355 break;
3356 }
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003359
3360 return( ret );
3361}
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363#if defined(MBEDTLS_SSL_RENEGOTIATION)
3364#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003365/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003366 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003367 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003368MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003370{
Janos Follath865b3eb2019-12-16 11:46:15 +00003371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003374
3375 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3377 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003378
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003379 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003380 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003382 return( ret );
3383 }
3384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003386
3387 return( 0 );
3388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003390
3391/*
3392 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 * - any side: calling mbedtls_ssl_renegotiate(),
3394 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3395 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003396 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003397 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003399 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003400int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003401{
Janos Follath865b3eb2019-12-16 11:46:15 +00003402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003405
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003406 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3407 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003408
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003409 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3410 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003412 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003414 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003415 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003416 ssl->handshake->out_msg_seq = 1;
3417 else
3418 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003419 }
3420#endif
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3423 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003428 return( ret );
3429 }
3430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003432
3433 return( 0 );
3434}
3435
3436/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003437 * Renegotiate current connection on client,
3438 * or request renegotiation on server
3439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003443
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003444 if( ssl == NULL || ssl->conf == NULL )
3445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003448 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003449 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003450 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003451 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003455
3456 /* Did we already try/start sending HelloRequest? */
3457 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003459
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003460 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003461 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003465 /*
3466 * On client, either start the renegotiation process or,
3467 * if already in progress, continue the handshake
3468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003470 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003471 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003473
Hanno Becker40cdaa12020-02-05 10:48:27 +00003474 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003475 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003477 return( ret );
3478 }
3479 }
3480 else
3481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003485 return( ret );
3486 }
3487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003489
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003490 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003493
Gilles Peskine9b562d52018-04-25 20:32:43 +02003494void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003495{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003496 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3497
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003498 if( handshake == NULL )
3499 return;
3500
Brett Warrene0edc842021-08-17 09:53:13 +01003501#if defined(MBEDTLS_ECP_C)
3502#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3503 if ( ssl->handshake->group_list_heap_allocated )
3504 mbedtls_free( (void*) handshake->group_list );
3505 handshake->group_list = NULL;
3506#endif /* MBEDTLS_DEPRECATED_REMOVED */
3507#endif /* MBEDTLS_ECP_C */
3508
Jerry Yuf017ee42022-01-12 15:49:48 +08003509#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3510#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3511 if ( ssl->handshake->sig_algs_heap_allocated )
3512 mbedtls_free( (void*) handshake->sig_algs );
3513 handshake->sig_algs = NULL;
3514#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003515#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3516 if( ssl->handshake->certificate_request_context )
3517 {
3518 mbedtls_free( (void*) handshake->certificate_request_context );
3519 }
3520#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003521#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3522
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003523#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3524 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3525 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003526 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003527 handshake->async_in_progress = 0;
3528 }
3529#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3530
Andrzej Kurek25f27152022-08-17 16:09:31 -04003531#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003532#if defined(MBEDTLS_USE_PSA_CRYPTO)
3533 psa_hash_abort( &handshake->fin_sha256_psa );
3534#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003535 mbedtls_sha256_free( &handshake->fin_sha256 );
3536#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003537#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003538#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003539#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003540 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003541#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003542 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003543#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003544#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#if defined(MBEDTLS_DHM_C)
3547 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003548#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003549#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003551#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003552#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003553 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003554#if defined(MBEDTLS_SSL_CLI_C)
3555 mbedtls_free( handshake->ecjpake_cache );
3556 handshake->ecjpake_cache = NULL;
3557 handshake->ecjpake_cache_len = 0;
3558#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003559#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003560
Janos Follath4ae5c292016-02-10 11:27:43 +00003561#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3562 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003563 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003565#endif
3566
Gilles Peskineeccd8882020-03-10 12:19:08 +01003567#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003568#if defined(MBEDTLS_USE_PSA_CRYPTO)
3569 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3570 {
3571 /* The maintenance of the external PSK key slot is the
3572 * user's responsibility. */
3573 if( ssl->handshake->psk_opaque_is_internal )
3574 {
3575 psa_destroy_key( ssl->handshake->psk_opaque );
3576 ssl->handshake->psk_opaque_is_internal = 0;
3577 }
3578 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3579 }
Neil Armstronge952a302022-05-03 10:22:14 +02003580#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003581 if( handshake->psk != NULL )
3582 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003583 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003584 mbedtls_free( handshake->psk );
3585 }
Neil Armstronge952a302022-05-03 10:22:14 +02003586#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003587#endif
3588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3590 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003591 /*
3592 * Free only the linked list wrapper, not the keys themselves
3593 * since the belong to the SNI callback
3594 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003595 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003597
Gilles Peskineeccd8882020-03-10 12:19:08 +01003598#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003599 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003600 if( handshake->ecrs_peer_cert != NULL )
3601 {
3602 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3603 mbedtls_free( handshake->ecrs_peer_cert );
3604 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003605#endif
3606
Hanno Becker75173122019-02-06 16:18:31 +00003607#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3608 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3609 mbedtls_pk_free( &handshake->peer_pubkey );
3610#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3611
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003612#if defined(MBEDTLS_SSL_CLI_C) && \
3613 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3614 mbedtls_free( handshake->cookie );
3615#endif /* MBEDTLS_SSL_CLI_C &&
3616 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003617
3618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003619 mbedtls_ssl_flight_free( handshake->flight );
3620 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003621#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003622
Ronald Cronf12b81d2022-03-15 10:42:41 +01003623#if defined(MBEDTLS_ECDH_C) && \
3624 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003625 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003626 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003627#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3628
Ronald Cron6f135e12021-12-08 16:57:54 +01003629#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003630 mbedtls_ssl_transform_free( handshake->transform_handshake );
3631 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003632 mbedtls_free( handshake->transform_earlydata );
3633 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003634#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003635
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003636
3637#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3638 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3639 * processes datagrams and the fact that a datagram is allowed to have
3640 * several records in it, it is possible that the I/O buffers are not
3641 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003642 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3643 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003644#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003645
Jerry Yuba9c7272021-10-30 11:54:10 +08003646 /* mbedtls_platform_zeroize MUST be last one in this function */
3647 mbedtls_platform_zeroize( handshake,
3648 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003649}
3650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003652{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003653 if( session == NULL )
3654 return;
3655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003657 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003658#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003659
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003660#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003662#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003663
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003664 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003665}
3666
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003667#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003668
3669#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3670#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3671#else
3672#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3673#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3674
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003675#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003676
3677#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3678#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3679#else
3680#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3681#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3682
3683#if defined(MBEDTLS_SSL_ALPN)
3684#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3685#else
3686#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3687#endif /* MBEDTLS_SSL_ALPN */
3688
3689#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3690#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3691#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3692#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3693
3694#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3695 ( (uint32_t) ( \
3696 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3697 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3698 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3699 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3700 0u ) )
3701
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003702static unsigned char ssl_serialized_context_header[] = {
3703 MBEDTLS_VERSION_MAJOR,
3704 MBEDTLS_VERSION_MINOR,
3705 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003706 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3707 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3708 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3709 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3710 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003711};
3712
Paul Bakker5121ce52009-01-03 21:22:43 +00003713/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003714 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003715 *
3716 * The format of the serialized data is:
3717 * (in the presentation language of TLS, RFC 8446 section 3)
3718 *
3719 * // header
3720 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003721 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003722 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003723 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003724 * Note: When updating the format, remember to keep these
3725 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003726 *
3727 * // session sub-structure
3728 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3729 * // transform sub-structure
3730 * uint8 random[64]; // ServerHello.random+ClientHello.random
3731 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3732 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3733 * // fields from ssl_context
3734 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3735 * uint64 in_window_top; // DTLS: last validated record seq_num
3736 * uint64 in_window; // DTLS: bitmask for replay protection
3737 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3738 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3739 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3740 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3741 *
3742 * Note that many fields of the ssl_context or sub-structures are not
3743 * serialized, as they fall in one of the following categories:
3744 *
3745 * 1. forced value (eg in_left must be 0)
3746 * 2. pointer to dynamically-allocated memory (eg session, transform)
3747 * 3. value can be re-derived from other data (eg session keys from MS)
3748 * 4. value was temporary (eg content of input buffer)
3749 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003750 */
3751int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3752 unsigned char *buf,
3753 size_t buf_len,
3754 size_t *olen )
3755{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003756 unsigned char *p = buf;
3757 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003758 size_t session_len;
3759 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003760
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003761 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003762 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3763 * this function's documentation.
3764 *
3765 * These are due to assumptions/limitations in the implementation. Some of
3766 * them are likely to stay (no handshake in progress) some might go away
3767 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003768 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003769 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003770 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003771 {
3772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003774 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003775 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003776 {
3777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003779 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003780 /* Double-check that sub-structures are indeed ready */
3781 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003782 {
3783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003785 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003786 /* There must be no pending incoming or outgoing data */
3787 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003788 {
3789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003791 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003792 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003793 {
3794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003796 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003797 /* Protocol must be DLTS, not TLS */
3798 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003799 {
3800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003802 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003803 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003804 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003808 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003809 /* We must be using an AEAD ciphersuite */
3810 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003811 {
3812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003814 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003815 /* Renegotiation must not be enabled */
3816#if defined(MBEDTLS_SSL_RENEGOTIATION)
3817 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003818 {
3819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003821 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003822#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003823
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003824 /*
3825 * Version and format identifier
3826 */
3827 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003828
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003829 if( used <= buf_len )
3830 {
3831 memcpy( p, ssl_serialized_context_header,
3832 sizeof( ssl_serialized_context_header ) );
3833 p += sizeof( ssl_serialized_context_header );
3834 }
3835
3836 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003837 * Session (length + data)
3838 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003839 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003840 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3841 return( ret );
3842
3843 used += 4 + session_len;
3844 if( used <= buf_len )
3845 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003846 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3847 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003848
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003849 ret = ssl_session_save( ssl->session, 1,
3850 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003851 if( ret != 0 )
3852 return( ret );
3853
3854 p += session_len;
3855 }
3856
3857 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003858 * Transform
3859 */
3860 used += sizeof( ssl->transform->randbytes );
3861 if( used <= buf_len )
3862 {
3863 memcpy( p, ssl->transform->randbytes,
3864 sizeof( ssl->transform->randbytes ) );
3865 p += sizeof( ssl->transform->randbytes );
3866 }
3867
3868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3869 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3870 if( used <= buf_len )
3871 {
3872 *p++ = ssl->transform->in_cid_len;
3873 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3874 p += ssl->transform->in_cid_len;
3875
3876 *p++ = ssl->transform->out_cid_len;
3877 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3878 p += ssl->transform->out_cid_len;
3879 }
3880#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3881
3882 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003883 * Saved fields from top-level ssl_context structure
3884 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003885 used += 4;
3886 if( used <= buf_len )
3887 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003888 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3889 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003890 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003891
3892#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3893 used += 16;
3894 if( used <= buf_len )
3895 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003896 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3897 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003898
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003899 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3900 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003901 }
3902#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3903
3904#if defined(MBEDTLS_SSL_PROTO_DTLS)
3905 used += 1;
3906 if( used <= buf_len )
3907 {
3908 *p++ = ssl->disable_datagram_packing;
3909 }
3910#endif /* MBEDTLS_SSL_PROTO_DTLS */
3911
Jerry Yuae0b2e22021-10-08 15:21:19 +08003912 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003913 if( used <= buf_len )
3914 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003915 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3916 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003917 }
3918
3919#if defined(MBEDTLS_SSL_PROTO_DTLS)
3920 used += 2;
3921 if( used <= buf_len )
3922 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003923 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3924 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003925 }
3926#endif /* MBEDTLS_SSL_PROTO_DTLS */
3927
3928#if defined(MBEDTLS_SSL_ALPN)
3929 {
3930 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003931 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003932 : 0;
3933
3934 used += 1 + alpn_len;
3935 if( used <= buf_len )
3936 {
3937 *p++ = alpn_len;
3938
3939 if( ssl->alpn_chosen != NULL )
3940 {
3941 memcpy( p, ssl->alpn_chosen, alpn_len );
3942 p += alpn_len;
3943 }
3944 }
3945 }
3946#endif /* MBEDTLS_SSL_ALPN */
3947
3948 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003949 * Done
3950 */
3951 *olen = used;
3952
3953 if( used > buf_len )
3954 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003955
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003956 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3957
Hanno Becker43aefe22020-02-05 10:44:56 +00003958 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003959}
3960
3961/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003962 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003963 *
3964 * This internal version is wrapped by a public function that cleans up in
3965 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003966 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003967MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003968static int ssl_context_load( mbedtls_ssl_context *ssl,
3969 const unsigned char *buf,
3970 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003971{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003972 const unsigned char *p = buf;
3973 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003974 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003976
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003977 /*
3978 * The context should have been freshly setup or reset.
3979 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003980 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003981 * renegotiating, or if the user mistakenly loaded a session first.)
3982 */
3983 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3984 ssl->session != NULL )
3985 {
3986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3987 }
3988
3989 /*
3990 * We can't check that the config matches the initial one, but we can at
3991 * least check it matches the requirements for serializing.
3992 */
3993 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003994 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3995 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003996#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003997 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003998#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003999 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004000 {
4001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4002 }
4003
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004004 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4005
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004006 /*
4007 * Check version identifier
4008 */
4009 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4011
4012 if( memcmp( p, ssl_serialized_context_header,
4013 sizeof( ssl_serialized_context_header ) ) != 0 )
4014 {
4015 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4016 }
4017 p += sizeof( ssl_serialized_context_header );
4018
4019 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004020 * Session
4021 */
4022 if( (size_t)( end - p ) < 4 )
4023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4024
4025 session_len = ( (size_t) p[0] << 24 ) |
4026 ( (size_t) p[1] << 16 ) |
4027 ( (size_t) p[2] << 8 ) |
4028 ( (size_t) p[3] );
4029 p += 4;
4030
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004031 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004032 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004033 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004034 ssl->session_in = ssl->session;
4035 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004036 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004037
4038 if( (size_t)( end - p ) < session_len )
4039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4040
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004041 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004042 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004043 {
4044 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004045 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004046 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004047
4048 p += session_len;
4049
4050 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004051 * Transform
4052 */
4053
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004054 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004055 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004056 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004057 ssl->transform_in = ssl->transform;
4058 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004059 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004060
4061 /* Read random bytes and populate structure */
4062 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004065 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004066 ssl->session->ciphersuite,
4067 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004068#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004069 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004070#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004071 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4072 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004073 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004074 ssl->conf->endpoint,
4075 ssl );
4076 if( ret != 0 )
4077 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004078#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004079 p += sizeof( ssl->transform->randbytes );
4080
4081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4082 /* Read connection IDs and store them */
4083 if( (size_t)( end - p ) < 1 )
4084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4085
4086 ssl->transform->in_cid_len = *p++;
4087
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004088 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4090
4091 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4092 p += ssl->transform->in_cid_len;
4093
4094 ssl->transform->out_cid_len = *p++;
4095
4096 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4098
4099 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4100 p += ssl->transform->out_cid_len;
4101#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4102
4103 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004104 * Saved fields from top-level ssl_context structure
4105 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004106 if( (size_t)( end - p ) < 4 )
4107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4108
4109 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4110 ( (uint32_t) p[1] << 16 ) |
4111 ( (uint32_t) p[2] << 8 ) |
4112 ( (uint32_t) p[3] );
4113 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004114
4115#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4116 if( (size_t)( end - p ) < 16 )
4117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4118
4119 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4120 ( (uint64_t) p[1] << 48 ) |
4121 ( (uint64_t) p[2] << 40 ) |
4122 ( (uint64_t) p[3] << 32 ) |
4123 ( (uint64_t) p[4] << 24 ) |
4124 ( (uint64_t) p[5] << 16 ) |
4125 ( (uint64_t) p[6] << 8 ) |
4126 ( (uint64_t) p[7] );
4127 p += 8;
4128
4129 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4130 ( (uint64_t) p[1] << 48 ) |
4131 ( (uint64_t) p[2] << 40 ) |
4132 ( (uint64_t) p[3] << 32 ) |
4133 ( (uint64_t) p[4] << 24 ) |
4134 ( (uint64_t) p[5] << 16 ) |
4135 ( (uint64_t) p[6] << 8 ) |
4136 ( (uint64_t) p[7] );
4137 p += 8;
4138#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4139
4140#if defined(MBEDTLS_SSL_PROTO_DTLS)
4141 if( (size_t)( end - p ) < 1 )
4142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4143
4144 ssl->disable_datagram_packing = *p++;
4145#endif /* MBEDTLS_SSL_PROTO_DTLS */
4146
Jerry Yud9a94fe2021-09-28 18:58:59 +08004147 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004149 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4150 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004151
4152#if defined(MBEDTLS_SSL_PROTO_DTLS)
4153 if( (size_t)( end - p ) < 2 )
4154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4155
4156 ssl->mtu = ( p[0] << 8 ) | p[1];
4157 p += 2;
4158#endif /* MBEDTLS_SSL_PROTO_DTLS */
4159
4160#if defined(MBEDTLS_SSL_ALPN)
4161 {
4162 uint8_t alpn_len;
4163 const char **cur;
4164
4165 if( (size_t)( end - p ) < 1 )
4166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4167
4168 alpn_len = *p++;
4169
4170 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4171 {
4172 /* alpn_chosen should point to an item in the configured list */
4173 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4174 {
4175 if( strlen( *cur ) == alpn_len &&
4176 memcmp( p, cur, alpn_len ) == 0 )
4177 {
4178 ssl->alpn_chosen = *cur;
4179 break;
4180 }
4181 }
4182 }
4183
4184 /* can only happen on conf mismatch */
4185 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4187
4188 p += alpn_len;
4189 }
4190#endif /* MBEDTLS_SSL_ALPN */
4191
4192 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004193 * Forced fields from top-level ssl_context structure
4194 *
4195 * Most of them already set to the correct value by mbedtls_ssl_init() and
4196 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4197 */
4198 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004199 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004200
Hanno Becker361b10d2019-08-30 10:42:49 +01004201 /* Adjust pointers for header fields of outgoing records to
4202 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004203 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004204
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004205#if defined(MBEDTLS_SSL_PROTO_DTLS)
4206 ssl->in_epoch = 1;
4207#endif
4208
4209 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4210 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004211 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4212 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004213 if( ssl->handshake != NULL )
4214 {
4215 mbedtls_ssl_handshake_free( ssl );
4216 mbedtls_free( ssl->handshake );
4217 ssl->handshake = NULL;
4218 }
4219
4220 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004221 * Done - should have consumed entire buffer
4222 */
4223 if( p != end )
4224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004225
4226 return( 0 );
4227}
4228
4229/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004230 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004231 */
4232int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4233 const unsigned char *buf,
4234 size_t len )
4235{
4236 int ret = ssl_context_load( context, buf, len );
4237
4238 if( ret != 0 )
4239 mbedtls_ssl_free( context );
4240
4241 return( ret );
4242}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004243#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004244
4245/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004246 * Free an SSL context
4247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004249{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004250 if( ssl == NULL )
4251 return;
4252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004254
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004255 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004256 {
sander-visserb8aa2072020-05-06 22:05:13 +02004257#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4258 size_t out_buf_len = ssl->out_buf_len;
4259#else
4260 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4261#endif
4262
Darryl Greenb33cc762019-11-28 14:29:44 +00004263 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004265 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 }
4267
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004268 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004269 {
sander-visserb8aa2072020-05-06 22:05:13 +02004270#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4271 size_t in_buf_len = ssl->in_buf_len;
4272#else
4273 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4274#endif
4275
Darryl Greenb33cc762019-11-28 14:29:44 +00004276 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004278 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 }
4280
Paul Bakker48916f92012-09-16 19:57:18 +00004281 if( ssl->transform )
4282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 mbedtls_ssl_transform_free( ssl->transform );
4284 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004285 }
4286
4287 if( ssl->handshake )
4288 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004289 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4291 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 mbedtls_free( ssl->handshake );
4294 mbedtls_free( ssl->transform_negotiate );
4295 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004296 }
4297
Ronald Cron6f135e12021-12-08 16:57:54 +01004298#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004299 mbedtls_ssl_transform_free( ssl->transform_application );
4300 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004301#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004302
Paul Bakkerc0463502013-02-14 11:19:38 +01004303 if( ssl->session )
4304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 mbedtls_ssl_session_free( ssl->session );
4306 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004307 }
4308
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004309#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004310 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004312 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004315#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004316
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004317#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004319#endif
4320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004322
Paul Bakker86f04f42013-02-14 11:20:09 +01004323 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004324 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325}
4326
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004327/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004328 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004329 */
4330void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4331{
4332 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4333}
4334
Gilles Peskineae270bf2021-06-02 00:05:29 +02004335/* The selection should be the same as mbedtls_x509_crt_profile_default in
4336 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004337 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004338 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004339 * about this list.
4340 */
Brett Warrene0edc842021-08-17 09:53:13 +01004341static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004342#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004343 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004344#endif
4345#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004346 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004347#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004348#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004349 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004350#endif
4351#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004352 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004353#endif
4354#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004355 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004356#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004357#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004358 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004359#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004360#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004361 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004362#endif
4363#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004364 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004365#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004366 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004367};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004368
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004369static int ssl_preset_suiteb_ciphersuites[] = {
4370 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4371 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4372 0
4373};
4374
Gilles Peskineeccd8882020-03-10 12:19:08 +01004375#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004376
Jerry Yu909df7b2022-01-22 11:56:27 +08004377/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004378 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004379 * rules SHOULD be upheld.
4380 * - No duplicate entries.
4381 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004382 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004383 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004384 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004385static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004386
Andrzej Kurek25f27152022-08-17 16:09:31 -04004387#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 +08004388 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4389 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004390#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004391 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004392
Andrzej Kurek25f27152022-08-17 16:09:31 -04004393#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 +08004394 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4395 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004396#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004397 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4398
Andrzej Kurek25f27152022-08-17 16:09:31 -04004399#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 +08004400 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4401 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004402#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004403 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004404
Andrzej Kurek25f27152022-08-17 16:09:31 -04004405#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 +08004406 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004407#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 +08004408
Andrzej Kurek25f27152022-08-17 16:09:31 -04004409#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 +08004410 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004411#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 +08004412
Andrzej Kurek25f27152022-08-17 16:09:31 -04004413#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 +08004414 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004415#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 +08004416
Andrzej Kurek25f27152022-08-17 16:09:31 -04004417#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 +08004418 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4419#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4420
Andrzej Kurek25f27152022-08-17 16:09:31 -04004421#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 +08004422 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4423#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4424
Andrzej Kurek25f27152022-08-17 16:09:31 -04004425#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 +08004426 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4427#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4428
Gabor Mezei15b95a62022-05-09 16:37:58 +02004429 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004430};
4431
4432/* NOTICE: see above */
4433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4434static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004435#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004436#if defined(MBEDTLS_ECDSA_C)
4437 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004438#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004439#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4440 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4441#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004442#if defined(MBEDTLS_RSA_C)
4443 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4444#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004445#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004446#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004447#if defined(MBEDTLS_ECDSA_C)
4448 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004449#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004450#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4451 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4452#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004453#if defined(MBEDTLS_RSA_C)
4454 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4455#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004456#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004457#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004458#if defined(MBEDTLS_ECDSA_C)
4459 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004460#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004461#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4462 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4463#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004464#if defined(MBEDTLS_RSA_C)
4465 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4466#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004467#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004468 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004469};
4470#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4471/* NOTICE: see above */
4472static uint16_t ssl_preset_suiteb_sig_algs[] = {
4473
Andrzej Kurek25f27152022-08-17 16:09:31 -04004474#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 +08004475 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4476 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004477#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004478 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4479
Andrzej Kurek25f27152022-08-17 16:09:31 -04004480#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 +08004481 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4482 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004483#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004484 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004485
Andrzej Kurek25f27152022-08-17 16:09:31 -04004486#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 +08004487 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004488#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 +08004489
Andrzej Kurek25f27152022-08-17 16:09:31 -04004490#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 +08004491 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004492#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004493
Gabor Mezei15b95a62022-05-09 16:37:58 +02004494 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004495};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004496
Jerry Yu909df7b2022-01-22 11:56:27 +08004497/* NOTICE: see above */
4498#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4499static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004501#if defined(MBEDTLS_ECDSA_C)
4502 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004503#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004504#if defined(MBEDTLS_RSA_C)
4505 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4506#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004507#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004509#if defined(MBEDTLS_ECDSA_C)
4510 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004511#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004512#if defined(MBEDTLS_RSA_C)
4513 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4514#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004515#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004516 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004517};
Jerry Yu909df7b2022-01-22 11:56:27 +08004518#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4519
Jerry Yu1a8b4812022-01-20 17:56:50 +08004520#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004521
Brett Warrene0edc842021-08-17 09:53:13 +01004522static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004523#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004524 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004525#endif
4526#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004527 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004528#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004529 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004530};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004531
Jerry Yu1a8b4812022-01-20 17:56:50 +08004532#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004533/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004534 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004535MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004536static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004537{
4538 size_t i, j;
4539 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004540
Gabor Mezei15b95a62022-05-09 16:37:58 +02004541 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004542 {
Jerry Yuf377d642022-01-25 10:43:59 +08004543 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004544 {
Jerry Yuf377d642022-01-25 10:43:59 +08004545 if( sig_algs[i] != sig_algs[j] )
4546 continue;
4547 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4548 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4549 sig_algs[i], j, i );
4550 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004551 }
4552 }
4553 return( ret );
4554}
4555
4556#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4557
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004558/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004559 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004560 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004561int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004562 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004563{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004564#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004566#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004567
Jerry Yu1a8b4812022-01-20 17:56:50 +08004568#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004569 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004570 {
4571 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4572 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4573 }
4574
Jerry Yuf377d642022-01-25 10:43:59 +08004575 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004576 {
4577 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4578 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4579 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004580
4581#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004582 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004583 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004584 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004585 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4586 }
4587
Jerry Yuf377d642022-01-25 10:43:59 +08004588 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004589 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004590 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004591 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4592 }
4593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004594#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4595
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004596 /* Use the functions here so that they are covered in tests,
4597 * but otherwise access member directly for efficiency */
4598 mbedtls_ssl_conf_endpoint( conf, endpoint );
4599 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004600
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004601 /*
4602 * Things that are common to all presets
4603 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004604#if defined(MBEDTLS_SSL_CLI_C)
4605 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4606 {
4607 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4608#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4609 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4610#endif
4611 }
4612#endif
4613
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004614#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4615 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4616#endif
4617
4618#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4619 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4620#endif
4621
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004622#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004623 conf->f_cookie_write = ssl_cookie_write_dummy;
4624 conf->f_cookie_check = ssl_cookie_check_dummy;
4625#endif
4626
4627#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4628 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4629#endif
4630
Janos Follath088ce432017-04-10 12:42:31 +01004631#if defined(MBEDTLS_SSL_SRV_C)
4632 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004633 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004634#endif
4635
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004636#if defined(MBEDTLS_SSL_PROTO_DTLS)
4637 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4638 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4639#endif
4640
4641#if defined(MBEDTLS_SSL_RENEGOTIATION)
4642 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004643 memset( conf->renego_period, 0x00, 2 );
4644 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004645#endif
4646
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004647#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004648 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4649 {
4650 const unsigned char dhm_p[] =
4651 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4652 const unsigned char dhm_g[] =
4653 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004654
Hanno Beckere2defad2021-07-24 05:59:17 +01004655 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4656 dhm_p, sizeof( dhm_p ),
4657 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4658 {
4659 return( ret );
4660 }
4661 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004662#endif
4663
Ronald Cron6f135e12021-12-08 16:57:54 +01004664#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004665#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004666 mbedtls_ssl_conf_new_session_tickets(
4667 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4668#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004669 /*
4670 * Allow all TLS 1.3 key exchange modes by default.
4671 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004672 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004673#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004674
XiaokangQian4d3a6042022-04-21 13:46:17 +00004675 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4676 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004677#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004678 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004679 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004680#else
XiaokangQian060d8672022-04-21 09:24:56 +00004681 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004682#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004683 }
4684 else
4685 {
4686#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4687 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4688 {
4689 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4690 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4691 }
4692 else
4693 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4694 {
4695 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4696 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4697 }
4698#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4699 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4700 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4701#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4702 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4703 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4704#else
4705 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4706#endif
4707 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004708
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004709 /*
4710 * Preset-specific defaults
4711 */
4712 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004713 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004714 /*
4715 * NSA Suite B
4716 */
4717 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004718
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004719 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004720
4721#if defined(MBEDTLS_X509_CRT_PARSE_C)
4722 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004723#endif
4724
Gilles Peskineeccd8882020-03-10 12:19:08 +01004725#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004726#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4727 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4728 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4729 else
4730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4731 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004732#endif
4733
Brett Warrene0edc842021-08-17 09:53:13 +01004734#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4735 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004736#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004737 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004738 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004739
4740 /*
4741 * Default
4742 */
4743 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004744
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004745 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004746
4747#if defined(MBEDTLS_X509_CRT_PARSE_C)
4748 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4749#endif
4750
Gilles Peskineeccd8882020-03-10 12:19:08 +01004751#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004752#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4753 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4754 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4755 else
4756#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4757 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004758#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004759
Brett Warrene0edc842021-08-17 09:53:13 +01004760#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4761 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004762#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004763 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004764
4765#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4766 conf->dhm_min_bitlen = 1024;
4767#endif
4768 }
4769
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004770 return( 0 );
4771}
4772
4773/*
4774 * Free mbedtls_ssl_config
4775 */
4776void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4777{
4778#if defined(MBEDTLS_DHM_C)
4779 mbedtls_mpi_free( &conf->dhm_P );
4780 mbedtls_mpi_free( &conf->dhm_G );
4781#endif
4782
Gilles Peskineeccd8882020-03-10 12:19:08 +01004783#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004784#if defined(MBEDTLS_USE_PSA_CRYPTO)
4785 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4786 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004787 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4788 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004789#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004790 if( conf->psk != NULL )
4791 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004792 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004793 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004794 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004795 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004796 }
4797
4798 if( conf->psk_identity != NULL )
4799 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004800 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004801 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004802 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004803 conf->psk_identity_len = 0;
4804 }
4805#endif
4806
4807#if defined(MBEDTLS_X509_CRT_PARSE_C)
4808 ssl_key_cert_free( conf->key_cert );
4809#endif
4810
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004811 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004812}
4813
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004814#if defined(MBEDTLS_PK_C) && \
4815 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004816/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821#if defined(MBEDTLS_RSA_C)
4822 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4823 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004824#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825#if defined(MBEDTLS_ECDSA_C)
4826 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4827 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004828#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004830}
4831
Hanno Becker7e5437a2017-04-28 17:15:26 +01004832unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4833{
4834 switch( type ) {
4835 case MBEDTLS_PK_RSA:
4836 return( MBEDTLS_SSL_SIG_RSA );
4837 case MBEDTLS_PK_ECDSA:
4838 case MBEDTLS_PK_ECKEY:
4839 return( MBEDTLS_SSL_SIG_ECDSA );
4840 default:
4841 return( MBEDTLS_SSL_SIG_ANON );
4842 }
4843}
4844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004846{
4847 switch( sig )
4848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849#if defined(MBEDTLS_RSA_C)
4850 case MBEDTLS_SSL_SIG_RSA:
4851 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853#if defined(MBEDTLS_ECDSA_C)
4854 case MBEDTLS_SSL_SIG_ECDSA:
4855 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004856#endif
4857 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004859 }
4860}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004861#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004862
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004863/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004864 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004867{
4868 switch( hash )
4869 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004870#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871 case MBEDTLS_SSL_HASH_MD5:
4872 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004873#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004874#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 case MBEDTLS_SSL_HASH_SHA1:
4876 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004877#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004878#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879 case MBEDTLS_SSL_HASH_SHA224:
4880 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004881#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 case MBEDTLS_SSL_HASH_SHA256:
4884 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004885#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004886#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 case MBEDTLS_SSL_HASH_SHA384:
4888 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004889#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004890#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 case MBEDTLS_SSL_HASH_SHA512:
4892 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004893#endif
4894 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004896 }
4897}
4898
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004899/*
4900 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4901 */
4902unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4903{
4904 switch( md )
4905 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004906#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004907 case MBEDTLS_MD_MD5:
4908 return( MBEDTLS_SSL_HASH_MD5 );
4909#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004910#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004911 case MBEDTLS_MD_SHA1:
4912 return( MBEDTLS_SSL_HASH_SHA1 );
4913#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004914#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004915 case MBEDTLS_MD_SHA224:
4916 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004917#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004918#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004919 case MBEDTLS_MD_SHA256:
4920 return( MBEDTLS_SSL_HASH_SHA256 );
4921#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004922#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004923 case MBEDTLS_MD_SHA384:
4924 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004925#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004926#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004927 case MBEDTLS_MD_SHA512:
4928 return( MBEDTLS_SSL_HASH_SHA512 );
4929#endif
4930 default:
4931 return( MBEDTLS_SSL_HASH_NONE );
4932 }
4933}
4934
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004935/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004936 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004937 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004938 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004939int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004940{
Brett Warrene0edc842021-08-17 09:53:13 +01004941 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004942
Brett Warrene0edc842021-08-17 09:53:13 +01004943 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004944 return( -1 );
4945
Brett Warrene0edc842021-08-17 09:53:13 +01004946 for( ; *group_list != 0; group_list++ )
4947 {
4948 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004949 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004950 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004951
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004952 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004953}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004954
4955#if defined(MBEDTLS_ECP_C)
4956/*
4957 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4958 */
4959int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4960{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004961 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004962 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004963
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004964 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004965 return -1;
4966
4967 uint16_t tls_id = grp_info->tls_id;
4968
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004969 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4970}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004971#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973#if defined(MBEDTLS_X509_CRT_PARSE_C)
4974int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4975 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004976 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004977 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004978{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004979 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004980 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004981 const char *ext_oid;
4982 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004985 {
4986 /* Server part of the key exchange */
4987 switch( ciphersuite->key_exchange )
4988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989 case MBEDTLS_KEY_EXCHANGE_RSA:
4990 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004991 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004992 break;
4993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4995 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4996 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4997 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004998 break;
4999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5001 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005002 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005003 break;
5004
5005 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 case MBEDTLS_KEY_EXCHANGE_NONE:
5007 case MBEDTLS_KEY_EXCHANGE_PSK:
5008 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5009 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005010 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005011 usage = 0;
5012 }
5013 }
5014 else
5015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5017 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005018 }
5019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005021 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005022 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005023 ret = -1;
5024 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5029 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005030 }
5031 else
5032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5034 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005035 }
5036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005037 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005038 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005039 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005040 ret = -1;
5041 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005042
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005043 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005046
Jerry Yu148165c2021-09-24 23:20:59 +08005047#if defined(MBEDTLS_USE_PSA_CRYPTO)
5048int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5049 const mbedtls_md_type_t md,
5050 unsigned char *dst,
5051 size_t dst_len,
5052 size_t *olen )
5053{
Ronald Cronf6893e12022-01-07 22:09:01 +01005054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5055 psa_hash_operation_t *hash_operation_to_clone;
5056 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5057
Jerry Yu148165c2021-09-24 23:20:59 +08005058 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005059
5060 switch( md )
5061 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005062#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005063 case MBEDTLS_MD_SHA384:
5064 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5065 break;
5066#endif
5067
Andrzej Kurek25f27152022-08-17 16:09:31 -04005068#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005069 case MBEDTLS_MD_SHA256:
5070 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5071 break;
5072#endif
5073
5074 default:
5075 goto exit;
5076 }
5077
5078 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5079 if( status != PSA_SUCCESS )
5080 goto exit;
5081
5082 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5083 if( status != PSA_SUCCESS )
5084 goto exit;
5085
5086exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005087 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005088}
5089#else /* MBEDTLS_USE_PSA_CRYPTO */
5090
Andrzej Kurek25f27152022-08-17 16:09:31 -04005091#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005092MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005093static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5094 unsigned char *dst,
5095 size_t dst_len,
5096 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005097{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005098 int ret;
5099 mbedtls_sha512_context sha512;
5100
5101 if( dst_len < 48 )
5102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5103
5104 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005105 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005106
5107 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5108 {
5109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5110 goto exit;
5111 }
5112
5113 *olen = 48;
5114
5115exit:
5116
5117 mbedtls_sha512_free( &sha512 );
5118 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005119}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005120#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005121
Andrzej Kurek25f27152022-08-17 16:09:31 -04005122#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005123MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005124static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5125 unsigned char *dst,
5126 size_t dst_len,
5127 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005128{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005129 int ret;
5130 mbedtls_sha256_context sha256;
5131
5132 if( dst_len < 32 )
5133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5134
5135 mbedtls_sha256_init( &sha256 );
5136 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005137
Jerry Yu24c0ec32021-09-09 14:21:07 +08005138 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5139 {
5140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5141 goto exit;
5142 }
5143
5144 *olen = 32;
5145
5146exit:
5147
5148 mbedtls_sha256_free( &sha256 );
5149 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005150}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005151#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005152
Jerry Yu000f9762021-09-14 11:12:51 +08005153int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5154 const mbedtls_md_type_t md,
5155 unsigned char *dst,
5156 size_t dst_len,
5157 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005158{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005159 switch( md )
5160 {
5161
Andrzej Kurek25f27152022-08-17 16:09:31 -04005162#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005163 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005164 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005165#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005166
Andrzej Kurek25f27152022-08-17 16:09:31 -04005167#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005168 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005169 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005170#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005171
5172 default:
5173 break;
5174 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005176}
XiaokangQian647719a2021-12-07 09:16:29 +00005177
Jerry Yu148165c2021-09-24 23:20:59 +08005178#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005179
Gabor Mezei078e8032022-04-27 21:17:56 +02005180#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5181/* mbedtls_ssl_parse_sig_alg_ext()
5182 *
5183 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5184 * value (TLS 1.3 RFC8446):
5185 * enum {
5186 * ....
5187 * ecdsa_secp256r1_sha256( 0x0403 ),
5188 * ecdsa_secp384r1_sha384( 0x0503 ),
5189 * ecdsa_secp521r1_sha512( 0x0603 ),
5190 * ....
5191 * } SignatureScheme;
5192 *
5193 * struct {
5194 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5195 * } SignatureSchemeList;
5196 *
5197 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5198 * value (TLS 1.2 RFC5246):
5199 * enum {
5200 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5201 * sha512(6), (255)
5202 * } HashAlgorithm;
5203 *
5204 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5205 * SignatureAlgorithm;
5206 *
5207 * struct {
5208 * HashAlgorithm hash;
5209 * SignatureAlgorithm signature;
5210 * } SignatureAndHashAlgorithm;
5211 *
5212 * SignatureAndHashAlgorithm
5213 * supported_signature_algorithms<2..2^16-2>;
5214 *
5215 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5216 * generalization of the TLS 1.2 signature algorithm extension.
5217 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5218 * `SignatureScheme` field of TLS 1.3
5219 *
5220 */
5221int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5222 const unsigned char *buf,
5223 const unsigned char *end )
5224{
5225 const unsigned char *p = buf;
5226 size_t supported_sig_algs_len = 0;
5227 const unsigned char *supported_sig_algs_end;
5228 uint16_t sig_alg;
5229 uint32_t common_idx = 0;
5230
5231 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5232 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5233 p += 2;
5234
5235 memset( ssl->handshake->received_sig_algs, 0,
5236 sizeof(ssl->handshake->received_sig_algs) );
5237
5238 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5239 supported_sig_algs_end = p + supported_sig_algs_len;
5240 while( p < supported_sig_algs_end )
5241 {
5242 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5243 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5244 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005245 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5246 sig_alg,
5247 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005248#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005249 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005250 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5251 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5252 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005253 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005254 }
5255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005256
Jerry Yuf3b46b52022-06-19 16:52:27 +08005257 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5258 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005259
5260 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5261 {
5262 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5263 common_idx += 1;
5264 }
5265 }
5266 /* Check that we consumed all the message. */
5267 if( p != end )
5268 {
5269 MBEDTLS_SSL_DEBUG_MSG( 1,
5270 ( "Signature algorithms extension length misaligned" ) );
5271 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5272 MBEDTLS_ERR_SSL_DECODE_ERROR );
5273 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5274 }
5275
5276 if( common_idx == 0 )
5277 {
5278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5279 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5280 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5281 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5282 }
5283
Gabor Mezei15b95a62022-05-09 16:37:58 +02005284 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005285 return( 0 );
5286}
5287
5288#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5289
Jerry Yudc7bd172022-02-17 13:44:15 +08005290#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5291
5292#if defined(MBEDTLS_USE_PSA_CRYPTO)
5293
5294static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5295 mbedtls_svc_key_id_t key,
5296 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005297 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005298 const unsigned char* seed, size_t seed_length,
5299 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005300 const unsigned char* other_secret,
5301 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005302 size_t capacity )
5303{
5304 psa_status_t status;
5305
5306 status = psa_key_derivation_setup( derivation, alg );
5307 if( status != PSA_SUCCESS )
5308 return( status );
5309
5310 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5311 {
5312 status = psa_key_derivation_input_bytes( derivation,
5313 PSA_KEY_DERIVATION_INPUT_SEED,
5314 seed, seed_length );
5315 if( status != PSA_SUCCESS )
5316 return( status );
5317
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005318 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005319 {
5320 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005321 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5322 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005323 if( status != PSA_SUCCESS )
5324 return( status );
5325 }
5326
Jerry Yudc7bd172022-02-17 13:44:15 +08005327 if( mbedtls_svc_key_id_is_null( key ) )
5328 {
5329 status = psa_key_derivation_input_bytes(
5330 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005331 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005332 }
5333 else
5334 {
5335 status = psa_key_derivation_input_key(
5336 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5337 }
5338 if( status != PSA_SUCCESS )
5339 return( status );
5340
5341 status = psa_key_derivation_input_bytes( derivation,
5342 PSA_KEY_DERIVATION_INPUT_LABEL,
5343 label, label_length );
5344 if( status != PSA_SUCCESS )
5345 return( status );
5346 }
5347 else
5348 {
5349 return( PSA_ERROR_NOT_SUPPORTED );
5350 }
5351
5352 status = psa_key_derivation_set_capacity( derivation, capacity );
5353 if( status != PSA_SUCCESS )
5354 return( status );
5355
5356 return( PSA_SUCCESS );
5357}
5358
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005359MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005360static int tls_prf_generic( mbedtls_md_type_t md_type,
5361 const unsigned char *secret, size_t slen,
5362 const char *label,
5363 const unsigned char *random, size_t rlen,
5364 unsigned char *dstbuf, size_t dlen )
5365{
5366 psa_status_t status;
5367 psa_algorithm_t alg;
5368 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5369 psa_key_derivation_operation_t derivation =
5370 PSA_KEY_DERIVATION_OPERATION_INIT;
5371
5372 if( md_type == MBEDTLS_MD_SHA384 )
5373 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5374 else
5375 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5376
5377 /* Normally a "secret" should be long enough to be impossible to
5378 * find by brute force, and in particular should not be empty. But
5379 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5380 * and for this use case it makes sense to have a 0-length "secret".
5381 * Since the key API doesn't allow importing a key of length 0,
5382 * keep master_key=0, which setup_psa_key_derivation() understands
5383 * to mean a 0-length "secret" input. */
5384 if( slen != 0 )
5385 {
5386 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5387 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5388 psa_set_key_algorithm( &key_attributes, alg );
5389 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5390
5391 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5392 if( status != PSA_SUCCESS )
5393 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5394 }
5395
5396 status = setup_psa_key_derivation( &derivation,
5397 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005398 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005399 random, rlen,
5400 (unsigned char const *) label,
5401 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005402 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005403 dlen );
5404 if( status != PSA_SUCCESS )
5405 {
5406 psa_key_derivation_abort( &derivation );
5407 psa_destroy_key( master_key );
5408 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5409 }
5410
5411 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5412 if( status != PSA_SUCCESS )
5413 {
5414 psa_key_derivation_abort( &derivation );
5415 psa_destroy_key( master_key );
5416 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5417 }
5418
5419 status = psa_key_derivation_abort( &derivation );
5420 if( status != PSA_SUCCESS )
5421 {
5422 psa_destroy_key( master_key );
5423 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5424 }
5425
5426 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5427 status = psa_destroy_key( master_key );
5428 if( status != PSA_SUCCESS )
5429 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5430
5431 return( 0 );
5432}
5433
5434#else /* MBEDTLS_USE_PSA_CRYPTO */
5435
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005436MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005437static int tls_prf_generic( mbedtls_md_type_t md_type,
5438 const unsigned char *secret, size_t slen,
5439 const char *label,
5440 const unsigned char *random, size_t rlen,
5441 unsigned char *dstbuf, size_t dlen )
5442{
5443 size_t nb;
5444 size_t i, j, k, md_len;
5445 unsigned char *tmp;
5446 size_t tmp_len = 0;
5447 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5448 const mbedtls_md_info_t *md_info;
5449 mbedtls_md_context_t md_ctx;
5450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5451
5452 mbedtls_md_init( &md_ctx );
5453
5454 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5456
5457 md_len = mbedtls_md_get_size( md_info );
5458
5459 tmp_len = md_len + strlen( label ) + rlen;
5460 tmp = mbedtls_calloc( 1, tmp_len );
5461 if( tmp == NULL )
5462 {
5463 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5464 goto exit;
5465 }
5466
5467 nb = strlen( label );
5468 memcpy( tmp + md_len, label, nb );
5469 memcpy( tmp + md_len + nb, random, rlen );
5470 nb += rlen;
5471
5472 /*
5473 * Compute P_<hash>(secret, label + random)[0..dlen]
5474 */
5475 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5476 goto exit;
5477
5478 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5479 if( ret != 0 )
5480 goto exit;
5481 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5482 if( ret != 0 )
5483 goto exit;
5484 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5485 if( ret != 0 )
5486 goto exit;
5487
5488 for( i = 0; i < dlen; i += md_len )
5489 {
5490 ret = mbedtls_md_hmac_reset ( &md_ctx );
5491 if( ret != 0 )
5492 goto exit;
5493 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5494 if( ret != 0 )
5495 goto exit;
5496 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5497 if( ret != 0 )
5498 goto exit;
5499
5500 ret = mbedtls_md_hmac_reset ( &md_ctx );
5501 if( ret != 0 )
5502 goto exit;
5503 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5504 if( ret != 0 )
5505 goto exit;
5506 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5507 if( ret != 0 )
5508 goto exit;
5509
5510 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5511
5512 for( j = 0; j < k; j++ )
5513 dstbuf[i + j] = h_i[j];
5514 }
5515
5516exit:
5517 mbedtls_md_free( &md_ctx );
5518
5519 mbedtls_platform_zeroize( tmp, tmp_len );
5520 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5521
5522 mbedtls_free( tmp );
5523
5524 return( ret );
5525}
5526#endif /* MBEDTLS_USE_PSA_CRYPTO */
5527
Andrzej Kurek25f27152022-08-17 16:09:31 -04005528#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005529MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005530static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5531 const char *label,
5532 const unsigned char *random, size_t rlen,
5533 unsigned char *dstbuf, size_t dlen )
5534{
5535 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5536 label, random, rlen, dstbuf, dlen ) );
5537}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005538#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005539
Andrzej Kurek25f27152022-08-17 16:09:31 -04005540#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005541MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005542static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5543 const char *label,
5544 const unsigned char *random, size_t rlen,
5545 unsigned char *dstbuf, size_t dlen )
5546{
5547 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5548 label, random, rlen, dstbuf, dlen ) );
5549}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005550#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005551
Jerry Yuf009d862022-02-17 14:01:37 +08005552/*
5553 * Set appropriate PRF function and other SSL / TLS1.2 functions
5554 *
5555 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005556 * - hash associated with the ciphersuite (only used by TLS 1.2)
5557 *
5558 * Outputs:
5559 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5560 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005561MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005562static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005563 mbedtls_md_type_t hash )
5564{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005565#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005566 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005567 {
5568 handshake->tls_prf = tls_prf_sha384;
5569 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5570 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5571 }
5572 else
5573#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005574#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005575 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005576 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005577 handshake->tls_prf = tls_prf_sha256;
5578 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5579 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5580 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005581#else
Jerry Yuf009d862022-02-17 14:01:37 +08005582 {
Jerry Yuf009d862022-02-17 14:01:37 +08005583 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005584 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5586 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005587#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005588
5589 return( 0 );
5590}
Jerry Yud6ab2352022-02-17 14:03:43 +08005591
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005592/*
5593 * Compute master secret if needed
5594 *
5595 * Parameters:
5596 * [in/out] handshake
5597 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5598 * (PSA-PSK) ciphersuite_info, psk_opaque
5599 * [out] premaster (cleared)
5600 * [out] master
5601 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5602 * debug: conf->f_dbg, conf->p_dbg
5603 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005604 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005605 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005606MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005607static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5608 unsigned char *master,
5609 const mbedtls_ssl_context *ssl )
5610{
5611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5612
5613 /* cf. RFC 5246, Section 8.1:
5614 * "The master secret is always exactly 48 bytes in length." */
5615 size_t const master_secret_len = 48;
5616
5617#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5618 unsigned char session_hash[48];
5619#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5620
5621 /* The label for the KDF used for key expansion.
5622 * This is either "master secret" or "extended master secret"
5623 * depending on whether the Extended Master Secret extension
5624 * is used. */
5625 char const *lbl = "master secret";
5626
Przemek Stekielae4ed302022-04-05 17:15:55 +02005627 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005628 * - If the Extended Master Secret extension is not used,
5629 * this is ClientHello.Random + ServerHello.Random
5630 * (see Sect. 8.1 in RFC 5246).
5631 * - If the Extended Master Secret extension is used,
5632 * this is the transcript of the handshake so far.
5633 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005634 unsigned char const *seed = handshake->randbytes;
5635 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005636
5637#if !defined(MBEDTLS_DEBUG_C) && \
5638 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5639 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5640 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5641 ssl = NULL; /* make sure we don't use it except for those cases */
5642 (void) ssl;
5643#endif
5644
5645 if( handshake->resume != 0 )
5646 {
5647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5648 return( 0 );
5649 }
5650
5651#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5652 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5653 {
5654 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005655 seed = session_hash;
5656 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005657
5658 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005659 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005660 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005661#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005662
Przemek Stekiel99114f32022-04-22 11:20:09 +02005663#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005664 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005665 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005666 {
5667 /* Perform PSK-to-MS expansion in a single step. */
5668 psa_status_t status;
5669 psa_algorithm_t alg;
5670 mbedtls_svc_key_id_t psk;
5671 psa_key_derivation_operation_t derivation =
5672 PSA_KEY_DERIVATION_OPERATION_INIT;
5673 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5674
5675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5676
5677 psk = mbedtls_ssl_get_opaque_psk( ssl );
5678
5679 if( hash_alg == MBEDTLS_MD_SHA384 )
5680 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5681 else
5682 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5683
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005684 size_t other_secret_len = 0;
5685 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005686
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005687 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005688 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005689 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005690 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005691 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005692 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005693 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005694 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005695 other_secret_len = 48;
5696 other_secret = handshake->premaster + 2;
5697 break;
5698 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005699 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005700 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5701 other_secret = handshake->premaster + 2;
5702 break;
5703 default:
5704 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005705 }
5706
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005707 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005708 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005709 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005710 (unsigned char const *) lbl,
5711 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005712 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005713 master_secret_len );
5714 if( status != PSA_SUCCESS )
5715 {
5716 psa_key_derivation_abort( &derivation );
5717 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5718 }
5719
5720 status = psa_key_derivation_output_bytes( &derivation,
5721 master,
5722 master_secret_len );
5723 if( status != PSA_SUCCESS )
5724 {
5725 psa_key_derivation_abort( &derivation );
5726 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5727 }
5728
5729 status = psa_key_derivation_abort( &derivation );
5730 if( status != PSA_SUCCESS )
5731 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5732 }
5733 else
5734#endif
5735 {
5736 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005737 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005738 master,
5739 master_secret_len );
5740 if( ret != 0 )
5741 {
5742 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5743 return( ret );
5744 }
5745
5746 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5747 handshake->premaster,
5748 handshake->pmslen );
5749
5750 mbedtls_platform_zeroize( handshake->premaster,
5751 sizeof(handshake->premaster) );
5752 }
5753
5754 return( 0 );
5755}
5756
Jerry Yud62f87e2022-02-17 14:09:02 +08005757int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5758{
5759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5760 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5761 ssl->handshake->ciphersuite_info;
5762
5763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5764
5765 /* Set PRF, calc_verify and calc_finished function pointers */
5766 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005767 ciphersuite_info->mac );
5768 if( ret != 0 )
5769 {
5770 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5771 return( ret );
5772 }
5773
5774 /* Compute master secret if needed */
5775 ret = ssl_compute_master( ssl->handshake,
5776 ssl->session_negotiate->master,
5777 ssl );
5778 if( ret != 0 )
5779 {
5780 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5781 return( ret );
5782 }
5783
5784 /* Swap the client and server random values:
5785 * - MS derivation wanted client+server (RFC 5246 8.1)
5786 * - key derivation wants server+client (RFC 5246 6.3) */
5787 {
5788 unsigned char tmp[64];
5789 memcpy( tmp, ssl->handshake->randbytes, 64 );
5790 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5791 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5792 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5793 }
5794
5795 /* Populate transform structure */
5796 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5797 ssl->session_negotiate->ciphersuite,
5798 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005799#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005800 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005801#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005802 ssl->handshake->tls_prf,
5803 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005804 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005805 ssl->conf->endpoint,
5806 ssl );
5807 if( ret != 0 )
5808 {
5809 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5810 return( ret );
5811 }
5812
5813 /* We no longer need Server/ClientHello.random values */
5814 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5815 sizeof( ssl->handshake->randbytes ) );
5816
5817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5818
5819 return( 0 );
5820}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005821
Ronald Cron4dcbca92022-03-07 10:21:40 +01005822int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5823{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005824 switch( md )
5825 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005826#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005827 case MBEDTLS_SSL_HASH_SHA384:
5828 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5829 break;
5830#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005831#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005832 case MBEDTLS_SSL_HASH_SHA256:
5833 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5834 break;
5835#endif
5836 default:
5837 return( -1 );
5838 }
5839
Ronald Cronc2f13a02022-03-07 10:25:24 +01005840 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005841}
5842
Andrzej Kurek25f27152022-08-17 16:09:31 -04005843#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005844void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5845 unsigned char *hash,
5846 size_t *hlen )
5847{
5848#if defined(MBEDTLS_USE_PSA_CRYPTO)
5849 size_t hash_size;
5850 psa_status_t status;
5851 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5852
5853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5854 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5855 if( status != PSA_SUCCESS )
5856 {
5857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5858 return;
5859 }
5860
5861 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5862 if( status != PSA_SUCCESS )
5863 {
5864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5865 return;
5866 }
5867
5868 *hlen = 32;
5869 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5871#else
5872 mbedtls_sha256_context sha256;
5873
5874 mbedtls_sha256_init( &sha256 );
5875
5876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5877
5878 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5879 mbedtls_sha256_finish( &sha256, hash );
5880
5881 *hlen = 32;
5882
5883 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5885
5886 mbedtls_sha256_free( &sha256 );
5887#endif /* MBEDTLS_USE_PSA_CRYPTO */
5888 return;
5889}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005890#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005891
Andrzej Kurek25f27152022-08-17 16:09:31 -04005892#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005893void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5894 unsigned char *hash,
5895 size_t *hlen )
5896{
5897#if defined(MBEDTLS_USE_PSA_CRYPTO)
5898 size_t hash_size;
5899 psa_status_t status;
5900 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5901
5902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5903 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5904 if( status != PSA_SUCCESS )
5905 {
5906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5907 return;
5908 }
5909
5910 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5911 if( status != PSA_SUCCESS )
5912 {
5913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5914 return;
5915 }
5916
5917 *hlen = 48;
5918 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5920#else
5921 mbedtls_sha512_context sha512;
5922
5923 mbedtls_sha512_init( &sha512 );
5924
5925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5926
Andrzej Kureka242e832022-08-11 10:03:14 -04005927 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005928 mbedtls_sha512_finish( &sha512, hash );
5929
5930 *hlen = 48;
5931
5932 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5934
5935 mbedtls_sha512_free( &sha512 );
5936#endif /* MBEDTLS_USE_PSA_CRYPTO */
5937 return;
5938}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005939#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005940
Neil Armstrong80f6f322022-05-03 17:56:38 +02005941#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5942 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005943int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5944{
5945 unsigned char *p = ssl->handshake->premaster;
5946 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5947 const unsigned char *psk = NULL;
5948 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005949 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005950
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005951 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005952 {
5953 /*
5954 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005955 * checked before calling this function.
5956 *
5957 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005958 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005959 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005960 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5961 {
5962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5963 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5964 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005965 }
5966
5967 /*
5968 * PMS = struct {
5969 * opaque other_secret<0..2^16-1>;
5970 * opaque psk<0..2^16-1>;
5971 * };
5972 * with "other_secret" depending on the particular key exchange
5973 */
5974#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5975 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5976 {
5977 if( end - p < 2 )
5978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5979
5980 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5981 p += 2;
5982
5983 if( end < p || (size_t)( end - p ) < psk_len )
5984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5985
5986 memset( p, 0, psk_len );
5987 p += psk_len;
5988 }
5989 else
5990#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5991#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5992 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5993 {
5994 /*
5995 * other_secret already set by the ClientKeyExchange message,
5996 * and is 48 bytes long
5997 */
5998 if( end - p < 2 )
5999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6000
6001 *p++ = 0;
6002 *p++ = 48;
6003 p += 48;
6004 }
6005 else
6006#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6007#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6008 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6009 {
6010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6011 size_t len;
6012
6013 /* Write length only when we know the actual value */
6014 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6015 p + 2, end - ( p + 2 ), &len,
6016 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6017 {
6018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6019 return( ret );
6020 }
6021 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6022 p += 2 + len;
6023
6024 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6025 }
6026 else
6027#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006028#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006029 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6030 {
6031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6032 size_t zlen;
6033
6034 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6035 p + 2, end - ( p + 2 ),
6036 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6037 {
6038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6039 return( ret );
6040 }
6041
6042 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6043 p += 2 + zlen;
6044
6045 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6046 MBEDTLS_DEBUG_ECDH_Z );
6047 }
6048 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006049#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006050 {
6051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6053 }
6054
6055 /* opaque psk<0..2^16-1>; */
6056 if( end - p < 2 )
6057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6058
6059 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6060 p += 2;
6061
6062 if( end < p || (size_t)( end - p ) < psk_len )
6063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6064
6065 memcpy( p, psk, psk_len );
6066 p += psk_len;
6067
6068 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6069
6070 return( 0 );
6071}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006072#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006073
6074#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006075MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006076static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6077
6078#if defined(MBEDTLS_SSL_PROTO_DTLS)
6079int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6080{
6081 /* If renegotiation is not enforced, retransmit until we would reach max
6082 * timeout if we were using the usual handshake doubling scheme */
6083 if( ssl->conf->renego_max_records < 0 )
6084 {
6085 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6086 unsigned char doublings = 1;
6087
6088 while( ratio != 0 )
6089 {
6090 ++doublings;
6091 ratio >>= 1;
6092 }
6093
6094 if( ++ssl->renego_records_seen > doublings )
6095 {
6096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6097 return( 0 );
6098 }
6099 }
6100
6101 return( ssl_write_hello_request( ssl ) );
6102}
6103#endif
6104#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006105
Jerry Yud9526692022-02-17 14:23:47 +08006106/*
6107 * Handshake functions
6108 */
6109#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6110/* No certificate support -> dummy functions */
6111int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6112{
6113 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6114 ssl->handshake->ciphersuite_info;
6115
6116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6117
6118 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6119 {
6120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6121 ssl->state++;
6122 return( 0 );
6123 }
6124
6125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6126 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6127}
6128
6129int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6130{
6131 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6132 ssl->handshake->ciphersuite_info;
6133
6134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6135
6136 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6137 {
6138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6139 ssl->state++;
6140 return( 0 );
6141 }
6142
6143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6145}
6146
6147#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6148/* Some certificate support -> implement write and parse */
6149
6150int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6151{
6152 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6153 size_t i, n;
6154 const mbedtls_x509_crt *crt;
6155 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6156 ssl->handshake->ciphersuite_info;
6157
6158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6159
6160 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6161 {
6162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6163 ssl->state++;
6164 return( 0 );
6165 }
6166
6167#if defined(MBEDTLS_SSL_CLI_C)
6168 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6169 {
6170 if( ssl->handshake->client_auth == 0 )
6171 {
6172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6173 ssl->state++;
6174 return( 0 );
6175 }
6176 }
6177#endif /* MBEDTLS_SSL_CLI_C */
6178#if defined(MBEDTLS_SSL_SRV_C)
6179 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6180 {
6181 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6182 {
6183 /* Should never happen because we shouldn't have picked the
6184 * ciphersuite if we don't have a certificate. */
6185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6186 }
6187 }
6188#endif
6189
6190 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6191
6192 /*
6193 * 0 . 0 handshake type
6194 * 1 . 3 handshake length
6195 * 4 . 6 length of all certs
6196 * 7 . 9 length of cert. 1
6197 * 10 . n-1 peer certificate
6198 * n . n+2 length of cert. 2
6199 * n+3 . ... upper level cert, etc.
6200 */
6201 i = 7;
6202 crt = mbedtls_ssl_own_cert( ssl );
6203
6204 while( crt != NULL )
6205 {
6206 n = crt->raw.len;
6207 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6208 {
6209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6210 " > %" MBEDTLS_PRINTF_SIZET,
6211 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6212 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6213 }
6214
6215 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6216 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6217 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6218
6219 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6220 i += n; crt = crt->next;
6221 }
6222
6223 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6224 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6225 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6226
6227 ssl->out_msglen = i;
6228 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6229 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6230
6231 ssl->state++;
6232
6233 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6234 {
6235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6236 return( ret );
6237 }
6238
6239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6240
6241 return( ret );
6242}
6243
6244#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6245
6246#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006247MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006248static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6249 unsigned char *crt_buf,
6250 size_t crt_buf_len )
6251{
6252 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6253
6254 if( peer_crt == NULL )
6255 return( -1 );
6256
6257 if( peer_crt->raw.len != crt_buf_len )
6258 return( -1 );
6259
6260 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6261}
6262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006263MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006264static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6265 unsigned char *crt_buf,
6266 size_t crt_buf_len )
6267{
6268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6269 unsigned char const * const peer_cert_digest =
6270 ssl->session->peer_cert_digest;
6271 mbedtls_md_type_t const peer_cert_digest_type =
6272 ssl->session->peer_cert_digest_type;
6273 mbedtls_md_info_t const * const digest_info =
6274 mbedtls_md_info_from_type( peer_cert_digest_type );
6275 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6276 size_t digest_len;
6277
6278 if( peer_cert_digest == NULL || digest_info == NULL )
6279 return( -1 );
6280
6281 digest_len = mbedtls_md_get_size( digest_info );
6282 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6283 return( -1 );
6284
6285 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6286 if( ret != 0 )
6287 return( -1 );
6288
6289 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6290}
6291#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6292#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6293
6294/*
6295 * Once the certificate message is read, parse it into a cert chain and
6296 * perform basic checks, but leave actual verification to the caller
6297 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006298MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006299static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6300 mbedtls_x509_crt *chain )
6301{
6302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6303#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6304 int crt_cnt=0;
6305#endif
6306 size_t i, n;
6307 uint8_t alert;
6308
6309 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6310 {
6311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6312 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6313 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6314 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6315 }
6316
6317 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6318 {
6319 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6320 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6321 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6322 }
6323
6324 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6327 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6328 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6329 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6330 }
6331
6332 i = mbedtls_ssl_hs_hdr_len( ssl );
6333
6334 /*
6335 * Same message structure as in mbedtls_ssl_write_certificate()
6336 */
6337 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6338
6339 if( ssl->in_msg[i] != 0 ||
6340 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6341 {
6342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6343 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6344 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6345 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6346 }
6347
6348 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6349 i += 3;
6350
6351 /* Iterate through and parse the CRTs in the provided chain. */
6352 while( i < ssl->in_hslen )
6353 {
6354 /* Check that there's room for the next CRT's length fields. */
6355 if ( i + 3 > ssl->in_hslen ) {
6356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6357 mbedtls_ssl_send_alert_message( ssl,
6358 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6359 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6360 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6361 }
6362 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6363 * anything beyond 2**16 ~ 64K. */
6364 if( ssl->in_msg[i] != 0 )
6365 {
6366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6367 mbedtls_ssl_send_alert_message( ssl,
6368 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6369 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6370 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6371 }
6372
6373 /* Read length of the next CRT in the chain. */
6374 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6375 | (unsigned int) ssl->in_msg[i + 2];
6376 i += 3;
6377
6378 if( n < 128 || i + n > ssl->in_hslen )
6379 {
6380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6381 mbedtls_ssl_send_alert_message( ssl,
6382 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6383 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6384 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6385 }
6386
6387 /* Check if we're handling the first CRT in the chain. */
6388#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6389 if( crt_cnt++ == 0 &&
6390 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6391 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6392 {
6393 /* During client-side renegotiation, check that the server's
6394 * end-CRTs hasn't changed compared to the initial handshake,
6395 * mitigating the triple handshake attack. On success, reuse
6396 * the original end-CRT instead of parsing it again. */
6397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6398 if( ssl_check_peer_crt_unchanged( ssl,
6399 &ssl->in_msg[i],
6400 n ) != 0 )
6401 {
6402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6403 mbedtls_ssl_send_alert_message( ssl,
6404 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6405 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6406 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6407 }
6408
6409 /* Now we can safely free the original chain. */
6410 ssl_clear_peer_cert( ssl->session );
6411 }
6412#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6413
6414 /* Parse the next certificate in the chain. */
6415#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6416 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6417#else
6418 /* If we don't need to store the CRT chain permanently, parse
6419 * it in-place from the input buffer instead of making a copy. */
6420 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6421#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6422 switch( ret )
6423 {
6424 case 0: /*ok*/
6425 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6426 /* Ignore certificate with an unknown algorithm: maybe a
6427 prior certificate was already trusted. */
6428 break;
6429
6430 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6431 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6432 goto crt_parse_der_failed;
6433
6434 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6435 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6436 goto crt_parse_der_failed;
6437
6438 default:
6439 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6440 crt_parse_der_failed:
6441 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6442 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6443 return( ret );
6444 }
6445
6446 i += n;
6447 }
6448
6449 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6450 return( 0 );
6451}
6452
6453#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006454MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006455static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6456{
6457 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6458 return( -1 );
6459
6460 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6461 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6462 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6463 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6464 {
Ronald Cron19385882022-06-15 16:26:13 +02006465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006466 return( 0 );
6467 }
6468 return( -1 );
6469}
6470#endif /* MBEDTLS_SSL_SRV_C */
6471
6472/* Check if a certificate message is expected.
6473 * Return either
6474 * - SSL_CERTIFICATE_EXPECTED, or
6475 * - SSL_CERTIFICATE_SKIP
6476 * indicating whether a Certificate message is expected or not.
6477 */
6478#define SSL_CERTIFICATE_EXPECTED 0
6479#define SSL_CERTIFICATE_SKIP 1
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_coordinate( mbedtls_ssl_context *ssl,
6482 int authmode )
6483{
6484 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6485 ssl->handshake->ciphersuite_info;
6486
6487 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6488 return( SSL_CERTIFICATE_SKIP );
6489
6490#if defined(MBEDTLS_SSL_SRV_C)
6491 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6492 {
6493 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6494 return( SSL_CERTIFICATE_SKIP );
6495
6496 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6497 {
6498 ssl->session_negotiate->verify_result =
6499 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6500 return( SSL_CERTIFICATE_SKIP );
6501 }
6502 }
6503#else
6504 ((void) authmode);
6505#endif /* MBEDTLS_SSL_SRV_C */
6506
6507 return( SSL_CERTIFICATE_EXPECTED );
6508}
6509
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006510MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006511static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6512 int authmode,
6513 mbedtls_x509_crt *chain,
6514 void *rs_ctx )
6515{
6516 int ret = 0;
6517 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6518 ssl->handshake->ciphersuite_info;
6519 int have_ca_chain = 0;
6520
6521 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6522 void *p_vrfy;
6523
6524 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6525 return( 0 );
6526
6527 if( ssl->f_vrfy != NULL )
6528 {
6529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6530 f_vrfy = ssl->f_vrfy;
6531 p_vrfy = ssl->p_vrfy;
6532 }
6533 else
6534 {
6535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6536 f_vrfy = ssl->conf->f_vrfy;
6537 p_vrfy = ssl->conf->p_vrfy;
6538 }
6539
6540 /*
6541 * Main check: verify certificate
6542 */
6543#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6544 if( ssl->conf->f_ca_cb != NULL )
6545 {
6546 ((void) rs_ctx);
6547 have_ca_chain = 1;
6548
6549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6550 ret = mbedtls_x509_crt_verify_with_ca_cb(
6551 chain,
6552 ssl->conf->f_ca_cb,
6553 ssl->conf->p_ca_cb,
6554 ssl->conf->cert_profile,
6555 ssl->hostname,
6556 &ssl->session_negotiate->verify_result,
6557 f_vrfy, p_vrfy );
6558 }
6559 else
6560#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6561 {
6562 mbedtls_x509_crt *ca_chain;
6563 mbedtls_x509_crl *ca_crl;
6564
6565#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6566 if( ssl->handshake->sni_ca_chain != NULL )
6567 {
6568 ca_chain = ssl->handshake->sni_ca_chain;
6569 ca_crl = ssl->handshake->sni_ca_crl;
6570 }
6571 else
6572#endif
6573 {
6574 ca_chain = ssl->conf->ca_chain;
6575 ca_crl = ssl->conf->ca_crl;
6576 }
6577
6578 if( ca_chain != NULL )
6579 have_ca_chain = 1;
6580
6581 ret = mbedtls_x509_crt_verify_restartable(
6582 chain,
6583 ca_chain, ca_crl,
6584 ssl->conf->cert_profile,
6585 ssl->hostname,
6586 &ssl->session_negotiate->verify_result,
6587 f_vrfy, p_vrfy, rs_ctx );
6588 }
6589
6590 if( ret != 0 )
6591 {
6592 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6593 }
6594
6595#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6596 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6597 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6598#endif
6599
6600 /*
6601 * Secondary checks: always done, but change 'ret' only if it was 0
6602 */
6603
6604#if defined(MBEDTLS_ECP_C)
6605 {
6606 const mbedtls_pk_context *pk = &chain->pk;
6607
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006608 /* If certificate uses an EC key, make sure the curve is OK.
6609 * This is a public key, so it can't be opaque, so can_do() is a good
6610 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006611 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006612 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006613 /* and in the unlikely case the above assumption no longer holds
6614 * we are making sure that pk_ec() here does not return a NULL
6615 */
6616 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6617 if( ec == NULL )
6618 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006620 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6621 }
Jerry Yud9526692022-02-17 14:23:47 +08006622
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006623 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6624 {
6625 ssl->session_negotiate->verify_result |=
6626 MBEDTLS_X509_BADCERT_BAD_KEY;
6627
6628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6629 if( ret == 0 )
6630 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6631 }
Jerry Yud9526692022-02-17 14:23:47 +08006632 }
6633 }
6634#endif /* MBEDTLS_ECP_C */
6635
6636 if( mbedtls_ssl_check_cert_usage( chain,
6637 ciphersuite_info,
6638 ! ssl->conf->endpoint,
6639 &ssl->session_negotiate->verify_result ) != 0 )
6640 {
6641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6642 if( ret == 0 )
6643 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6644 }
6645
6646 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6647 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6648 * with details encoded in the verification flags. All other kinds
6649 * of error codes, including those from the user provided f_vrfy
6650 * functions, are treated as fatal and lead to a failure of
6651 * ssl_parse_certificate even if verification was optional. */
6652 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6653 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6654 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6655 {
6656 ret = 0;
6657 }
6658
6659 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6660 {
6661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6662 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6663 }
6664
6665 if( ret != 0 )
6666 {
6667 uint8_t alert;
6668
6669 /* The certificate may have been rejected for several reasons.
6670 Pick one and send the corresponding alert. Which alert to send
6671 may be a subject of debate in some cases. */
6672 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6673 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6674 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6675 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6676 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6677 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6678 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6679 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6680 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6681 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6682 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6683 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6684 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6685 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6686 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6687 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6688 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6689 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6690 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6691 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6692 else
6693 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6694 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6695 alert );
6696 }
6697
6698#if defined(MBEDTLS_DEBUG_C)
6699 if( ssl->session_negotiate->verify_result != 0 )
6700 {
6701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6702 (unsigned int) ssl->session_negotiate->verify_result ) );
6703 }
6704 else
6705 {
6706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6707 }
6708#endif /* MBEDTLS_DEBUG_C */
6709
6710 return( ret );
6711}
6712
6713#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006714MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006715static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6716 unsigned char *start, size_t len )
6717{
6718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6719 /* Remember digest of the peer's end-CRT. */
6720 ssl->session_negotiate->peer_cert_digest =
6721 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6722 if( ssl->session_negotiate->peer_cert_digest == NULL )
6723 {
6724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6725 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6726 mbedtls_ssl_send_alert_message( ssl,
6727 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6728 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6729
6730 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6731 }
6732
6733 ret = mbedtls_md( mbedtls_md_info_from_type(
6734 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6735 start, len,
6736 ssl->session_negotiate->peer_cert_digest );
6737
6738 ssl->session_negotiate->peer_cert_digest_type =
6739 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6740 ssl->session_negotiate->peer_cert_digest_len =
6741 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6742
6743 return( ret );
6744}
6745
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006746MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006747static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6748 unsigned char *start, size_t len )
6749{
6750 unsigned char *end = start + len;
6751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6752
6753 /* Make a copy of the peer's raw public key. */
6754 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6755 ret = mbedtls_pk_parse_subpubkey( &start, end,
6756 &ssl->handshake->peer_pubkey );
6757 if( ret != 0 )
6758 {
6759 /* We should have parsed the public key before. */
6760 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6761 }
6762
6763 return( 0 );
6764}
6765#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6766
6767int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6768{
6769 int ret = 0;
6770 int crt_expected;
6771#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6772 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6773 ? ssl->handshake->sni_authmode
6774 : ssl->conf->authmode;
6775#else
6776 const int authmode = ssl->conf->authmode;
6777#endif
6778 void *rs_ctx = NULL;
6779 mbedtls_x509_crt *chain = NULL;
6780
6781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6782
6783 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6784 if( crt_expected == SSL_CERTIFICATE_SKIP )
6785 {
6786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6787 goto exit;
6788 }
6789
6790#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6791 if( ssl->handshake->ecrs_enabled &&
6792 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6793 {
6794 chain = ssl->handshake->ecrs_peer_cert;
6795 ssl->handshake->ecrs_peer_cert = NULL;
6796 goto crt_verify;
6797 }
6798#endif
6799
6800 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6801 {
6802 /* mbedtls_ssl_read_record may have sent an alert already. We
6803 let it decide whether to alert. */
6804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6805 goto exit;
6806 }
6807
6808#if defined(MBEDTLS_SSL_SRV_C)
6809 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6810 {
6811 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6812
6813 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6814 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6815
6816 goto exit;
6817 }
6818#endif /* MBEDTLS_SSL_SRV_C */
6819
6820 /* Clear existing peer CRT structure in case we tried to
6821 * reuse a session but it failed, and allocate a new one. */
6822 ssl_clear_peer_cert( ssl->session_negotiate );
6823
6824 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6825 if( chain == NULL )
6826 {
6827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6828 sizeof( mbedtls_x509_crt ) ) );
6829 mbedtls_ssl_send_alert_message( ssl,
6830 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6831 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6832
6833 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6834 goto exit;
6835 }
6836 mbedtls_x509_crt_init( chain );
6837
6838 ret = ssl_parse_certificate_chain( ssl, chain );
6839 if( ret != 0 )
6840 goto exit;
6841
6842#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6843 if( ssl->handshake->ecrs_enabled)
6844 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6845
6846crt_verify:
6847 if( ssl->handshake->ecrs_enabled)
6848 rs_ctx = &ssl->handshake->ecrs_ctx;
6849#endif
6850
6851 ret = ssl_parse_certificate_verify( ssl, authmode,
6852 chain, rs_ctx );
6853 if( ret != 0 )
6854 goto exit;
6855
6856#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6857 {
6858 unsigned char *crt_start, *pk_start;
6859 size_t crt_len, pk_len;
6860
6861 /* We parse the CRT chain without copying, so
6862 * these pointers point into the input buffer,
6863 * and are hence still valid after freeing the
6864 * CRT chain. */
6865
6866 crt_start = chain->raw.p;
6867 crt_len = chain->raw.len;
6868
6869 pk_start = chain->pk_raw.p;
6870 pk_len = chain->pk_raw.len;
6871
6872 /* Free the CRT structures before computing
6873 * digest and copying the peer's public key. */
6874 mbedtls_x509_crt_free( chain );
6875 mbedtls_free( chain );
6876 chain = NULL;
6877
6878 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6879 if( ret != 0 )
6880 goto exit;
6881
6882 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6883 if( ret != 0 )
6884 goto exit;
6885 }
6886#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6887 /* Pass ownership to session structure. */
6888 ssl->session_negotiate->peer_cert = chain;
6889 chain = NULL;
6890#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6891
6892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6893
6894exit:
6895
6896 if( ret == 0 )
6897 ssl->state++;
6898
6899#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6900 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6901 {
6902 ssl->handshake->ecrs_peer_cert = chain;
6903 chain = NULL;
6904 }
6905#endif
6906
6907 if( chain != NULL )
6908 {
6909 mbedtls_x509_crt_free( chain );
6910 mbedtls_free( chain );
6911 }
6912
6913 return( ret );
6914}
6915#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6916
Andrzej Kurek25f27152022-08-17 16:09:31 -04006917#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006918static void ssl_calc_finished_tls_sha256(
6919 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6920{
6921 int len = 12;
6922 const char *sender;
6923 unsigned char padbuf[32];
6924#if defined(MBEDTLS_USE_PSA_CRYPTO)
6925 size_t hash_size;
6926 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6927 psa_status_t status;
6928#else
6929 mbedtls_sha256_context sha256;
6930#endif
6931
6932 mbedtls_ssl_session *session = ssl->session_negotiate;
6933 if( !session )
6934 session = ssl->session;
6935
6936 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6937 ? "client finished"
6938 : "server finished";
6939
6940#if defined(MBEDTLS_USE_PSA_CRYPTO)
6941 sha256_psa = psa_hash_operation_init();
6942
6943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6944
6945 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6946 if( status != PSA_SUCCESS )
6947 {
6948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6949 return;
6950 }
6951
6952 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6953 if( status != PSA_SUCCESS )
6954 {
6955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6956 return;
6957 }
6958 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6959#else
6960
6961 mbedtls_sha256_init( &sha256 );
6962
6963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6964
6965 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6966
6967 /*
6968 * TLSv1.2:
6969 * hash = PRF( master, finished_label,
6970 * Hash( handshake ) )[0.11]
6971 */
6972
6973#if !defined(MBEDTLS_SHA256_ALT)
6974 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6975 sha256.state, sizeof( sha256.state ) );
6976#endif
6977
6978 mbedtls_sha256_finish( &sha256, padbuf );
6979 mbedtls_sha256_free( &sha256 );
6980#endif /* MBEDTLS_USE_PSA_CRYPTO */
6981
6982 ssl->handshake->tls_prf( session->master, 48, sender,
6983 padbuf, 32, buf, len );
6984
6985 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6986
6987 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6988
6989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6990}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006991#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08006992
Jerry Yub7ba49e2022-02-17 14:25:53 +08006993
Andrzej Kurek25f27152022-08-17 16:09:31 -04006994#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08006995static void ssl_calc_finished_tls_sha384(
6996 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6997{
6998 int len = 12;
6999 const char *sender;
7000 unsigned char padbuf[48];
7001#if defined(MBEDTLS_USE_PSA_CRYPTO)
7002 size_t hash_size;
7003 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7004 psa_status_t status;
7005#else
7006 mbedtls_sha512_context sha512;
7007#endif
7008
7009 mbedtls_ssl_session *session = ssl->session_negotiate;
7010 if( !session )
7011 session = ssl->session;
7012
7013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7014 ? "client finished"
7015 : "server finished";
7016
7017#if defined(MBEDTLS_USE_PSA_CRYPTO)
7018 sha384_psa = psa_hash_operation_init();
7019
7020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7021
7022 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7023 if( status != PSA_SUCCESS )
7024 {
7025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7026 return;
7027 }
7028
7029 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7030 if( status != PSA_SUCCESS )
7031 {
7032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7033 return;
7034 }
7035 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7036#else
7037 mbedtls_sha512_init( &sha512 );
7038
7039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7040
Andrzej Kureka242e832022-08-11 10:03:14 -04007041 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007042
7043 /*
7044 * TLSv1.2:
7045 * hash = PRF( master, finished_label,
7046 * Hash( handshake ) )[0.11]
7047 */
7048
7049#if !defined(MBEDTLS_SHA512_ALT)
7050 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7051 sha512.state, sizeof( sha512.state ) );
7052#endif
7053 mbedtls_sha512_finish( &sha512, padbuf );
7054
7055 mbedtls_sha512_free( &sha512 );
7056#endif
7057
7058 ssl->handshake->tls_prf( session->master, 48, sender,
7059 padbuf, 48, buf, len );
7060
7061 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7062
7063 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7064
7065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7066}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007067#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007068
Jerry Yuaef00152022-02-17 14:27:31 +08007069void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7070{
7071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7072
7073 /*
7074 * Free our handshake params
7075 */
7076 mbedtls_ssl_handshake_free( ssl );
7077 mbedtls_free( ssl->handshake );
7078 ssl->handshake = NULL;
7079
7080 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007081 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007082 */
7083 if( ssl->transform )
7084 {
7085 mbedtls_ssl_transform_free( ssl->transform );
7086 mbedtls_free( ssl->transform );
7087 }
7088 ssl->transform = ssl->transform_negotiate;
7089 ssl->transform_negotiate = NULL;
7090
7091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7092}
7093
Jerry Yu2a9fff52022-02-17 14:28:51 +08007094void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7095{
7096 int resume = ssl->handshake->resume;
7097
7098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7099
7100#if defined(MBEDTLS_SSL_RENEGOTIATION)
7101 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7102 {
7103 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7104 ssl->renego_records_seen = 0;
7105 }
7106#endif
7107
7108 /*
7109 * Free the previous session and switch in the current one
7110 */
7111 if( ssl->session )
7112 {
7113#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7114 /* RFC 7366 3.1: keep the EtM state */
7115 ssl->session_negotiate->encrypt_then_mac =
7116 ssl->session->encrypt_then_mac;
7117#endif
7118
7119 mbedtls_ssl_session_free( ssl->session );
7120 mbedtls_free( ssl->session );
7121 }
7122 ssl->session = ssl->session_negotiate;
7123 ssl->session_negotiate = NULL;
7124
7125 /*
7126 * Add cache entry
7127 */
7128 if( ssl->conf->f_set_cache != NULL &&
7129 ssl->session->id_len != 0 &&
7130 resume == 0 )
7131 {
7132 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7133 ssl->session->id,
7134 ssl->session->id_len,
7135 ssl->session ) != 0 )
7136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7137 }
7138
7139#if defined(MBEDTLS_SSL_PROTO_DTLS)
7140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7141 ssl->handshake->flight != NULL )
7142 {
7143 /* Cancel handshake timer */
7144 mbedtls_ssl_set_timer( ssl, 0 );
7145
7146 /* Keep last flight around in case we need to resend it:
7147 * we need the handshake and transform structures for that */
7148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7149 }
7150 else
7151#endif
7152 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7153
7154 ssl->state++;
7155
7156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7157}
7158
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007159int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7160{
7161 int ret, hash_len;
7162
7163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7164
7165 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7166
7167 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7168
7169 /*
7170 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7171 * may define some other value. Currently (early 2016), no defined
7172 * ciphersuite does this (and this is unlikely to change as activity has
7173 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7174 */
7175 hash_len = 12;
7176
7177#if defined(MBEDTLS_SSL_RENEGOTIATION)
7178 ssl->verify_data_len = hash_len;
7179 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7180#endif
7181
7182 ssl->out_msglen = 4 + hash_len;
7183 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7184 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7185
7186 /*
7187 * In case of session resuming, invert the client and server
7188 * ChangeCipherSpec messages order.
7189 */
7190 if( ssl->handshake->resume != 0 )
7191 {
7192#if defined(MBEDTLS_SSL_CLI_C)
7193 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7194 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7195#endif
7196#if defined(MBEDTLS_SSL_SRV_C)
7197 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7198 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7199#endif
7200 }
7201 else
7202 ssl->state++;
7203
7204 /*
7205 * Switch to our negotiated transform and session parameters for outbound
7206 * data.
7207 */
7208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7209
7210#if defined(MBEDTLS_SSL_PROTO_DTLS)
7211 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7212 {
7213 unsigned char i;
7214
7215 /* Remember current epoch settings for resending */
7216 ssl->handshake->alt_transform_out = ssl->transform_out;
7217 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7218 sizeof( ssl->handshake->alt_out_ctr ) );
7219
7220 /* Set sequence_number to zero */
7221 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7222
7223
7224 /* Increment epoch */
7225 for( i = 2; i > 0; i-- )
7226 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7227 break;
7228
7229 /* The loop goes to its end iff the counter is wrapping */
7230 if( i == 0 )
7231 {
7232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7233 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7234 }
7235 }
7236 else
7237#endif /* MBEDTLS_SSL_PROTO_DTLS */
7238 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7239
7240 ssl->transform_out = ssl->transform_negotiate;
7241 ssl->session_out = ssl->session_negotiate;
7242
7243#if defined(MBEDTLS_SSL_PROTO_DTLS)
7244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7245 mbedtls_ssl_send_flight_completed( ssl );
7246#endif
7247
7248 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7249 {
7250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7251 return( ret );
7252 }
7253
7254#if defined(MBEDTLS_SSL_PROTO_DTLS)
7255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7256 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7257 {
7258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7259 return( ret );
7260 }
7261#endif
7262
7263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7264
7265 return( 0 );
7266}
7267
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007268#define SSL_MAX_HASH_LEN 12
7269
7270int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7271{
7272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7273 unsigned int hash_len = 12;
7274 unsigned char buf[SSL_MAX_HASH_LEN];
7275
7276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7277
7278 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7279
7280 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7281 {
7282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7283 goto exit;
7284 }
7285
7286 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7287 {
7288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7290 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7291 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7292 goto exit;
7293 }
7294
7295 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7296 {
7297 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7298 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7299 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7300 goto exit;
7301 }
7302
7303 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7304 {
7305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7306 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7307 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7308 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7309 goto exit;
7310 }
7311
7312 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7313 buf, hash_len ) != 0 )
7314 {
7315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7317 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7318 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7319 goto exit;
7320 }
7321
7322#if defined(MBEDTLS_SSL_RENEGOTIATION)
7323 ssl->verify_data_len = hash_len;
7324 memcpy( ssl->peer_verify_data, buf, hash_len );
7325#endif
7326
7327 if( ssl->handshake->resume != 0 )
7328 {
7329#if defined(MBEDTLS_SSL_CLI_C)
7330 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7331 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7332#endif
7333#if defined(MBEDTLS_SSL_SRV_C)
7334 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7335 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7336#endif
7337 }
7338 else
7339 ssl->state++;
7340
7341#if defined(MBEDTLS_SSL_PROTO_DTLS)
7342 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7343 mbedtls_ssl_recv_flight_completed( ssl );
7344#endif
7345
7346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7347
7348exit:
7349 mbedtls_platform_zeroize( buf, hash_len );
7350 return( ret );
7351}
7352
Jerry Yu392112c2022-02-17 14:34:10 +08007353#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7354/*
7355 * Helper to get TLS 1.2 PRF from ciphersuite
7356 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7357 */
7358static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7359{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007360#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007361 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7362 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7363
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007364 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007365 return( tls_prf_sha384 );
7366#else
7367 (void) ciphersuite_id;
7368#endif
7369 return( tls_prf_sha256 );
7370}
7371#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007372
7373static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7374{
7375 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007376#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007377 if( tls_prf == tls_prf_sha384 )
7378 {
7379 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7380 }
7381 else
7382#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007383#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007384 if( tls_prf == tls_prf_sha256 )
7385 {
7386 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7387 }
7388 else
7389#endif
7390 return( MBEDTLS_SSL_TLS_PRF_NONE );
7391}
7392
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007393/*
7394 * Populate a transform structure with session keys and all the other
7395 * necessary information.
7396 *
7397 * Parameters:
7398 * - [in/out]: transform: structure to populate
7399 * [in] must be just initialised with mbedtls_ssl_transform_init()
7400 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7401 * - [in] ciphersuite
7402 * - [in] master
7403 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007404 * - [in] tls_prf: pointer to PRF to use for key derivation
7405 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007406 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007407 * - [in] endpoint: client or server
7408 * - [in] ssl: used for:
7409 * - ssl->conf->{f,p}_export_keys
7410 * [in] optionally used for:
7411 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7412 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007413MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007414static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7415 int ciphersuite,
7416 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007417#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007418 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007419#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007420 ssl_tls_prf_t tls_prf,
7421 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007422 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007423 unsigned endpoint,
7424 const mbedtls_ssl_context *ssl )
7425{
7426 int ret = 0;
7427 unsigned char keyblk[256];
7428 unsigned char *key1;
7429 unsigned char *key2;
7430 unsigned char *mac_enc;
7431 unsigned char *mac_dec;
7432 size_t mac_key_len = 0;
7433 size_t iv_copy_len;
7434 size_t keylen;
7435 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007436 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007437#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007438 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007439 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007440#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007441
7442#if defined(MBEDTLS_USE_PSA_CRYPTO)
7443 psa_key_type_t key_type;
7444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7445 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007446 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007447 size_t key_bits;
7448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7449#endif
7450
7451#if !defined(MBEDTLS_DEBUG_C) && \
7452 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7453 if( ssl->f_export_keys == NULL )
7454 {
7455 ssl = NULL; /* make sure we don't use it except for these cases */
7456 (void) ssl;
7457 }
7458#endif
7459
7460 /*
7461 * Some data just needs copying into the structure
7462 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007463#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007464 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007465#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007466 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007467
7468#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7469 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7470#endif
7471
7472#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007473 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007474 {
7475 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7476 * generation separate. This should never happen. */
7477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7478 }
7479#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7480
7481 /*
7482 * Get various info structures
7483 */
7484 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7485 if( ciphersuite_info == NULL )
7486 {
7487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7488 ciphersuite ) );
7489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7490 }
7491
Neil Armstrongab555e02022-04-04 11:07:59 +02007492 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007493#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007494 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007495#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007496 ciphersuite_info );
7497
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007498 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7499 transform->taglen =
7500 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7501
7502#if defined(MBEDTLS_USE_PSA_CRYPTO)
7503 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7504 transform->taglen,
7505 &alg,
7506 &key_type,
7507 &key_bits ) ) != PSA_SUCCESS )
7508 {
7509 ret = psa_ssl_status_to_mbedtls( status );
7510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7511 goto end;
7512 }
7513#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007514 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7515 if( cipher_info == NULL )
7516 {
7517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7518 ciphersuite_info->cipher ) );
7519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7520 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007521#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007522
Neil Armstronge4512952022-03-08 09:08:22 +01007523#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007524 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007525 if( mac_alg == 0 )
7526 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007528 (unsigned) ciphersuite_info->mac ) );
7529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7530 }
7531#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007532 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7533 if( md_info == NULL )
7534 {
7535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7536 (unsigned) ciphersuite_info->mac ) );
7537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7538 }
Neil Armstronge4512952022-03-08 09:08:22 +01007539#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007540
7541#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7542 /* Copy own and peer's CID if the use of the CID
7543 * extension has been negotiated. */
7544 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7545 {
7546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7547
7548 transform->in_cid_len = ssl->own_cid_len;
7549 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7550 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7551 transform->in_cid_len );
7552
7553 transform->out_cid_len = ssl->handshake->peer_cid_len;
7554 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7555 ssl->handshake->peer_cid_len );
7556 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7557 transform->out_cid_len );
7558 }
7559#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7560
7561 /*
7562 * Compute key block using the PRF
7563 */
7564 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7565 if( ret != 0 )
7566 {
7567 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7568 return( ret );
7569 }
7570
7571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7572 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7573 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7574 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7575 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7576
7577 /*
7578 * Determine the appropriate key, IV and MAC length.
7579 */
7580
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007581#if defined(MBEDTLS_USE_PSA_CRYPTO)
7582 keylen = PSA_BITS_TO_BYTES(key_bits);
7583#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007584 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007585#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007586
7587#if defined(MBEDTLS_GCM_C) || \
7588 defined(MBEDTLS_CCM_C) || \
7589 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007590 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007591 {
7592 size_t explicit_ivlen;
7593
7594 transform->maclen = 0;
7595 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007596
7597 /* All modes haves 96-bit IVs, but the length of the static parts vary
7598 * with mode and version:
7599 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7600 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7601 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7602 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7603 * sequence number).
7604 */
7605 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007606
7607 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007608#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007609 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007610#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007611 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7612 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007613#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007614
7615 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007616 transform->fixed_ivlen = 12;
7617 else
7618 transform->fixed_ivlen = 4;
7619
7620 /* Minimum length of encrypted record */
7621 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7622 transform->minlen = explicit_ivlen + transform->taglen;
7623 }
7624 else
7625#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7626#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007627 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7628 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7629 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007630 {
Neil Armstronge4512952022-03-08 09:08:22 +01007631#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007632 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007633#else
7634 size_t block_size = cipher_info->block_size;
7635#endif /* MBEDTLS_USE_PSA_CRYPTO */
7636
7637#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007638 /* Get MAC length */
7639 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7640#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007641 /* Initialize HMAC contexts */
7642 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7643 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7644 {
7645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7646 goto end;
7647 }
7648
7649 /* Get MAC length */
7650 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007651#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007652 transform->maclen = mac_key_len;
7653
7654 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007655#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007656 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007657#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007658 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007659#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007660
7661 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007662 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007663 transform->minlen = transform->maclen;
7664 else
7665 {
7666 /*
7667 * GenericBlockCipher:
7668 * 1. if EtM is in use: one block plus MAC
7669 * otherwise: * first multiple of blocklen greater than maclen
7670 * 2. IV
7671 */
7672#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007673 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007674 {
7675 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007676 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007677 }
7678 else
7679#endif
7680 {
7681 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007682 + block_size
7683 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007684 }
7685
Glenn Strauss07c64162022-03-14 12:34:51 -04007686 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007687 {
7688 transform->minlen += transform->ivlen;
7689 }
7690 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007691 {
7692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7693 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7694 goto end;
7695 }
7696 }
7697 }
7698 else
7699#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7700 {
7701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7703 }
7704
7705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7706 (unsigned) keylen,
7707 (unsigned) transform->minlen,
7708 (unsigned) transform->ivlen,
7709 (unsigned) transform->maclen ) );
7710
7711 /*
7712 * Finally setup the cipher contexts, IVs and MAC secrets.
7713 */
7714#if defined(MBEDTLS_SSL_CLI_C)
7715 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7716 {
7717 key1 = keyblk + mac_key_len * 2;
7718 key2 = keyblk + mac_key_len * 2 + keylen;
7719
7720 mac_enc = keyblk;
7721 mac_dec = keyblk + mac_key_len;
7722
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007723 iv_copy_len = ( transform->fixed_ivlen ) ?
7724 transform->fixed_ivlen : transform->ivlen;
7725 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7726 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7727 iv_copy_len );
7728 }
7729 else
7730#endif /* MBEDTLS_SSL_CLI_C */
7731#if defined(MBEDTLS_SSL_SRV_C)
7732 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7733 {
7734 key1 = keyblk + mac_key_len * 2 + keylen;
7735 key2 = keyblk + mac_key_len * 2;
7736
7737 mac_enc = keyblk + mac_key_len;
7738 mac_dec = keyblk;
7739
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007740 iv_copy_len = ( transform->fixed_ivlen ) ?
7741 transform->fixed_ivlen : transform->ivlen;
7742 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7743 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7744 iv_copy_len );
7745 }
7746 else
7747#endif /* MBEDTLS_SSL_SRV_C */
7748 {
7749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7750 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7751 goto end;
7752 }
7753
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007754 if( ssl != NULL && ssl->f_export_keys != NULL )
7755 {
7756 ssl->f_export_keys( ssl->p_export_keys,
7757 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7758 master, 48,
7759 randbytes + 32,
7760 randbytes,
7761 tls_prf_get_type( tls_prf ) );
7762 }
7763
7764#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007765 transform->psa_alg = alg;
7766
7767 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7768 {
7769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7770 psa_set_key_algorithm( &attributes, alg );
7771 psa_set_key_type( &attributes, key_type );
7772
7773 if( ( status = psa_import_key( &attributes,
7774 key1,
7775 PSA_BITS_TO_BYTES( key_bits ),
7776 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7777 {
7778 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7779 ret = psa_ssl_status_to_mbedtls( status );
7780 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7781 goto end;
7782 }
7783
7784 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7785
7786 if( ( status = psa_import_key( &attributes,
7787 key2,
7788 PSA_BITS_TO_BYTES( key_bits ),
7789 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7790 {
7791 ret = psa_ssl_status_to_mbedtls( status );
7792 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7793 goto end;
7794 }
7795 }
7796#else
7797 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7798 cipher_info ) ) != 0 )
7799 {
7800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7801 goto end;
7802 }
7803
7804 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7805 cipher_info ) ) != 0 )
7806 {
7807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7808 goto end;
7809 }
7810
7811 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7812 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7813 MBEDTLS_ENCRYPT ) ) != 0 )
7814 {
7815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7816 goto end;
7817 }
7818
7819 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7820 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7821 MBEDTLS_DECRYPT ) ) != 0 )
7822 {
7823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7824 goto end;
7825 }
7826
7827#if defined(MBEDTLS_CIPHER_MODE_CBC)
7828 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7829 {
7830 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7831 MBEDTLS_PADDING_NONE ) ) != 0 )
7832 {
7833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7834 goto end;
7835 }
7836
7837 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7838 MBEDTLS_PADDING_NONE ) ) != 0 )
7839 {
7840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7841 goto end;
7842 }
7843 }
7844#endif /* MBEDTLS_CIPHER_MODE_CBC */
7845#endif /* MBEDTLS_USE_PSA_CRYPTO */
7846
Neil Armstrong29c0c042022-03-17 17:47:28 +01007847#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7848 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7849 For AEAD-based ciphersuites, there is nothing to do here. */
7850 if( mac_key_len != 0 )
7851 {
7852#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007853 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007854
7855 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007856 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007857 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7858
7859 if( ( status = psa_import_key( &attributes,
7860 mac_enc, mac_key_len,
7861 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7862 {
7863 ret = psa_ssl_status_to_mbedtls( status );
7864 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7865 goto end;
7866 }
7867
Ronald Cronfb39f152022-03-25 14:36:28 +01007868 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007869 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7870#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7871 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7872#endif
7873 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007874 /* mbedtls_ct_hmac() requires the key to be exportable */
7875 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7876 PSA_KEY_USAGE_VERIFY_HASH );
7877 else
7878 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7879
7880 if( ( status = psa_import_key( &attributes,
7881 mac_dec, mac_key_len,
7882 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7883 {
7884 ret = psa_ssl_status_to_mbedtls( status );
7885 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7886 goto end;
7887 }
7888#else
7889 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7890 if( ret != 0 )
7891 goto end;
7892 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7893 if( ret != 0 )
7894 goto end;
7895#endif /* MBEDTLS_USE_PSA_CRYPTO */
7896 }
7897#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7898
7899 ((void) mac_dec);
7900 ((void) mac_enc);
7901
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007902end:
7903 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7904 return( ret );
7905}
7906
Jerry Yuee40f9d2022-02-17 14:55:16 +08007907#if defined(MBEDTLS_USE_PSA_CRYPTO)
7908int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7909 unsigned char *hash, size_t *hashlen,
7910 unsigned char *data, size_t data_len,
7911 mbedtls_md_type_t md_alg )
7912{
7913 psa_status_t status;
7914 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007915 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007916
7917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7918
7919 if( ( status = psa_hash_setup( &hash_operation,
7920 hash_alg ) ) != PSA_SUCCESS )
7921 {
7922 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7923 goto exit;
7924 }
7925
7926 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7927 64 ) ) != PSA_SUCCESS )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7930 goto exit;
7931 }
7932
7933 if( ( status = psa_hash_update( &hash_operation,
7934 data, data_len ) ) != PSA_SUCCESS )
7935 {
7936 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7937 goto exit;
7938 }
7939
7940 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7941 hashlen ) ) != PSA_SUCCESS )
7942 {
7943 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7944 goto exit;
7945 }
7946
7947exit:
7948 if( status != PSA_SUCCESS )
7949 {
7950 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7951 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7952 switch( status )
7953 {
7954 case PSA_ERROR_NOT_SUPPORTED:
7955 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7956 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7957 case PSA_ERROR_BUFFER_TOO_SMALL:
7958 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7959 case PSA_ERROR_INSUFFICIENT_MEMORY:
7960 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7961 default:
7962 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7963 }
7964 }
7965 return( 0 );
7966}
7967
7968#else
7969
7970int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7971 unsigned char *hash, size_t *hashlen,
7972 unsigned char *data, size_t data_len,
7973 mbedtls_md_type_t md_alg )
7974{
7975 int ret = 0;
7976 mbedtls_md_context_t ctx;
7977 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7978 *hashlen = mbedtls_md_get_size( md_info );
7979
7980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7981
7982 mbedtls_md_init( &ctx );
7983
7984 /*
7985 * digitally-signed struct {
7986 * opaque client_random[32];
7987 * opaque server_random[32];
7988 * ServerDHParams params;
7989 * };
7990 */
7991 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7992 {
7993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7994 goto exit;
7995 }
7996 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7997 {
7998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7999 goto exit;
8000 }
8001 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8002 {
8003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8004 goto exit;
8005 }
8006 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8007 {
8008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8009 goto exit;
8010 }
8011 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8012 {
8013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8014 goto exit;
8015 }
8016
8017exit:
8018 mbedtls_md_free( &ctx );
8019
8020 if( ret != 0 )
8021 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8022 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8023
8024 return( ret );
8025}
8026#endif /* MBEDTLS_USE_PSA_CRYPTO */
8027
Jerry Yud9d91da2022-02-17 14:57:06 +08008028#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8029
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008030/* Find the preferred hash for a given signature algorithm. */
8031unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8032 mbedtls_ssl_context *ssl,
8033 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008034{
Gabor Mezei078e8032022-04-27 21:17:56 +02008035 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008036 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008037
8038 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008039 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008040
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008041 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008042 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008043 unsigned int hash_alg_received =
8044 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8045 received_sig_algs[i] );
8046 unsigned int sig_alg_received =
8047 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8048 received_sig_algs[i] );
8049
8050 if( sig_alg == sig_alg_received )
8051 {
8052#if defined(MBEDTLS_USE_PSA_CRYPTO)
8053 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8054 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008055 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008056 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008057
Neil Armstrong96eceb82022-06-30 18:05:05 +02008058 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8059 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8060 PSA_ALG_ECDSA( psa_hash_alg ),
8061 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008062 continue;
8063
Neil Armstrong96eceb82022-06-30 18:05:05 +02008064 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008065 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8066 PSA_ALG_RSA_PKCS1V15_SIGN(
8067 psa_hash_alg ),
8068 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008069 continue;
8070 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008071#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008072
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008073 return( hash_alg_received );
8074 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008075 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008076
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008077 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008078}
8079
8080#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8081
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008082/* Serialization of TLS 1.2 sessions:
8083 *
8084 * struct {
8085 * uint64 start_time;
8086 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008087 * uint8 session_id_len; // at most 32
8088 * opaque session_id[32];
8089 * opaque master[48]; // fixed length in the standard
8090 * uint32 verify_result;
8091 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8092 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8093 * uint32 ticket_lifetime;
8094 * uint8 mfl_code; // up to 255 according to standard
8095 * uint8 encrypt_then_mac; // 0 or 1
8096 * } serialized_session_tls12;
8097 *
8098 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008099static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008100 unsigned char *buf,
8101 size_t buf_len )
8102{
8103 unsigned char *p = buf;
8104 size_t used = 0;
8105
8106#if defined(MBEDTLS_HAVE_TIME)
8107 uint64_t start;
8108#endif
8109#if defined(MBEDTLS_X509_CRT_PARSE_C)
8110#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8111 size_t cert_len;
8112#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8113#endif /* MBEDTLS_X509_CRT_PARSE_C */
8114
8115 /*
8116 * Time
8117 */
8118#if defined(MBEDTLS_HAVE_TIME)
8119 used += 8;
8120
8121 if( used <= buf_len )
8122 {
8123 start = (uint64_t) session->start;
8124
8125 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8126 p += 8;
8127 }
8128#endif /* MBEDTLS_HAVE_TIME */
8129
8130 /*
8131 * Basic mandatory fields
8132 */
8133 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008134 + 1 /* id_len */
8135 + sizeof( session->id )
8136 + sizeof( session->master )
8137 + 4; /* verify_result */
8138
8139 if( used <= buf_len )
8140 {
8141 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8142 p += 2;
8143
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008144 *p++ = MBEDTLS_BYTE_0( session->id_len );
8145 memcpy( p, session->id, 32 );
8146 p += 32;
8147
8148 memcpy( p, session->master, 48 );
8149 p += 48;
8150
8151 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8152 p += 4;
8153 }
8154
8155 /*
8156 * Peer's end-entity certificate
8157 */
8158#if defined(MBEDTLS_X509_CRT_PARSE_C)
8159#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8160 if( session->peer_cert == NULL )
8161 cert_len = 0;
8162 else
8163 cert_len = session->peer_cert->raw.len;
8164
8165 used += 3 + cert_len;
8166
8167 if( used <= buf_len )
8168 {
8169 *p++ = MBEDTLS_BYTE_2( cert_len );
8170 *p++ = MBEDTLS_BYTE_1( cert_len );
8171 *p++ = MBEDTLS_BYTE_0( cert_len );
8172
8173 if( session->peer_cert != NULL )
8174 {
8175 memcpy( p, session->peer_cert->raw.p, cert_len );
8176 p += cert_len;
8177 }
8178 }
8179#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8180 if( session->peer_cert_digest != NULL )
8181 {
8182 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8183 if( used <= buf_len )
8184 {
8185 *p++ = (unsigned char) session->peer_cert_digest_type;
8186 *p++ = (unsigned char) session->peer_cert_digest_len;
8187 memcpy( p, session->peer_cert_digest,
8188 session->peer_cert_digest_len );
8189 p += session->peer_cert_digest_len;
8190 }
8191 }
8192 else
8193 {
8194 used += 2;
8195 if( used <= buf_len )
8196 {
8197 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8198 *p++ = 0;
8199 }
8200 }
8201#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8202#endif /* MBEDTLS_X509_CRT_PARSE_C */
8203
8204 /*
8205 * Session ticket if any, plus associated data
8206 */
8207#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8208 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8209
8210 if( used <= buf_len )
8211 {
8212 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8213 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8214 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8215
8216 if( session->ticket != NULL )
8217 {
8218 memcpy( p, session->ticket, session->ticket_len );
8219 p += session->ticket_len;
8220 }
8221
8222 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8223 p += 4;
8224 }
8225#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8226
8227 /*
8228 * Misc extension-related info
8229 */
8230#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8231 used += 1;
8232
8233 if( used <= buf_len )
8234 *p++ = session->mfl_code;
8235#endif
8236
8237#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8238 used += 1;
8239
8240 if( used <= buf_len )
8241 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8242#endif
8243
8244 return( used );
8245}
8246
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008247MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008248static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008249 const unsigned char *buf,
8250 size_t len )
8251{
8252#if defined(MBEDTLS_HAVE_TIME)
8253 uint64_t start;
8254#endif
8255#if defined(MBEDTLS_X509_CRT_PARSE_C)
8256#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8257 size_t cert_len;
8258#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8259#endif /* MBEDTLS_X509_CRT_PARSE_C */
8260
8261 const unsigned char *p = buf;
8262 const unsigned char * const end = buf + len;
8263
8264 /*
8265 * Time
8266 */
8267#if defined(MBEDTLS_HAVE_TIME)
8268 if( 8 > (size_t)( end - p ) )
8269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8270
8271 start = ( (uint64_t) p[0] << 56 ) |
8272 ( (uint64_t) p[1] << 48 ) |
8273 ( (uint64_t) p[2] << 40 ) |
8274 ( (uint64_t) p[3] << 32 ) |
8275 ( (uint64_t) p[4] << 24 ) |
8276 ( (uint64_t) p[5] << 16 ) |
8277 ( (uint64_t) p[6] << 8 ) |
8278 ( (uint64_t) p[7] );
8279 p += 8;
8280
8281 session->start = (time_t) start;
8282#endif /* MBEDTLS_HAVE_TIME */
8283
8284 /*
8285 * Basic mandatory fields
8286 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008287 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8289
8290 session->ciphersuite = ( p[0] << 8 ) | p[1];
8291 p += 2;
8292
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008293 session->id_len = *p++;
8294 memcpy( session->id, p, 32 );
8295 p += 32;
8296
8297 memcpy( session->master, p, 48 );
8298 p += 48;
8299
8300 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8301 ( (uint32_t) p[1] << 16 ) |
8302 ( (uint32_t) p[2] << 8 ) |
8303 ( (uint32_t) p[3] );
8304 p += 4;
8305
8306 /* Immediately clear invalid pointer values that have been read, in case
8307 * we exit early before we replaced them with valid ones. */
8308#if defined(MBEDTLS_X509_CRT_PARSE_C)
8309#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8310 session->peer_cert = NULL;
8311#else
8312 session->peer_cert_digest = NULL;
8313#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8314#endif /* MBEDTLS_X509_CRT_PARSE_C */
8315#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8316 session->ticket = NULL;
8317#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8318
8319 /*
8320 * Peer certificate
8321 */
8322#if defined(MBEDTLS_X509_CRT_PARSE_C)
8323#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8324 /* Deserialize CRT from the end of the ticket. */
8325 if( 3 > (size_t)( end - p ) )
8326 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8327
8328 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8329 p += 3;
8330
8331 if( cert_len != 0 )
8332 {
8333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8334
8335 if( cert_len > (size_t)( end - p ) )
8336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8337
8338 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8339
8340 if( session->peer_cert == NULL )
8341 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8342
8343 mbedtls_x509_crt_init( session->peer_cert );
8344
8345 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8346 p, cert_len ) ) != 0 )
8347 {
8348 mbedtls_x509_crt_free( session->peer_cert );
8349 mbedtls_free( session->peer_cert );
8350 session->peer_cert = NULL;
8351 return( ret );
8352 }
8353
8354 p += cert_len;
8355 }
8356#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8357 /* Deserialize CRT digest from the end of the ticket. */
8358 if( 2 > (size_t)( end - p ) )
8359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8360
8361 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8362 session->peer_cert_digest_len = (size_t) *p++;
8363
8364 if( session->peer_cert_digest_len != 0 )
8365 {
8366 const mbedtls_md_info_t *md_info =
8367 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8368 if( md_info == NULL )
8369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8370 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8372
8373 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8375
8376 session->peer_cert_digest =
8377 mbedtls_calloc( 1, session->peer_cert_digest_len );
8378 if( session->peer_cert_digest == NULL )
8379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8380
8381 memcpy( session->peer_cert_digest, p,
8382 session->peer_cert_digest_len );
8383 p += session->peer_cert_digest_len;
8384 }
8385#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8386#endif /* MBEDTLS_X509_CRT_PARSE_C */
8387
8388 /*
8389 * Session ticket and associated data
8390 */
8391#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8392 if( 3 > (size_t)( end - p ) )
8393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8394
8395 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8396 p += 3;
8397
8398 if( session->ticket_len != 0 )
8399 {
8400 if( session->ticket_len > (size_t)( end - p ) )
8401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8402
8403 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8404 if( session->ticket == NULL )
8405 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8406
8407 memcpy( session->ticket, p, session->ticket_len );
8408 p += session->ticket_len;
8409 }
8410
8411 if( 4 > (size_t)( end - p ) )
8412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8413
8414 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8415 ( (uint32_t) p[1] << 16 ) |
8416 ( (uint32_t) p[2] << 8 ) |
8417 ( (uint32_t) p[3] );
8418 p += 4;
8419#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8420
8421 /*
8422 * Misc extension-related info
8423 */
8424#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8425 if( 1 > (size_t)( end - p ) )
8426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8427
8428 session->mfl_code = *p++;
8429#endif
8430
8431#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8432 if( 1 > (size_t)( end - p ) )
8433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8434
8435 session->encrypt_then_mac = *p++;
8436#endif
8437
8438 /* Done, should have consumed entire buffer */
8439 if( p != end )
8440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8441
8442 return( 0 );
8443}
Jerry Yudc7bd172022-02-17 13:44:15 +08008444#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8445
XiaokangQian75d40ef2022-04-20 11:05:24 +00008446int mbedtls_ssl_validate_ciphersuite(
8447 const mbedtls_ssl_context *ssl,
8448 const mbedtls_ssl_ciphersuite_t *suite_info,
8449 mbedtls_ssl_protocol_version min_tls_version,
8450 mbedtls_ssl_protocol_version max_tls_version )
8451{
8452 (void) ssl;
8453
8454 if( suite_info == NULL )
8455 return( -1 );
8456
8457 if( ( suite_info->min_tls_version > max_tls_version ) ||
8458 ( suite_info->max_tls_version < min_tls_version ) )
8459 {
8460 return( -1 );
8461 }
8462
XiaokangQian060d8672022-04-21 09:24:56 +00008463#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008464#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8465 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8466 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8467 {
8468 return( -1 );
8469 }
8470#endif
8471
8472 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8473#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8474 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8475 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8476 {
8477 return( -1 );
8478 }
8479#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8481
8482 return( 0 );
8483}
8484
XiaokangQianeaf36512022-04-24 09:07:44 +00008485#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8486/*
8487 * Function for writing a signature algorithm extension.
8488 *
8489 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8490 * value (TLS 1.3 RFC8446):
8491 * enum {
8492 * ....
8493 * ecdsa_secp256r1_sha256( 0x0403 ),
8494 * ecdsa_secp384r1_sha384( 0x0503 ),
8495 * ecdsa_secp521r1_sha512( 0x0603 ),
8496 * ....
8497 * } SignatureScheme;
8498 *
8499 * struct {
8500 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8501 * } SignatureSchemeList;
8502 *
8503 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8504 * value (TLS 1.2 RFC5246):
8505 * enum {
8506 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8507 * sha512(6), (255)
8508 * } HashAlgorithm;
8509 *
8510 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8511 * SignatureAlgorithm;
8512 *
8513 * struct {
8514 * HashAlgorithm hash;
8515 * SignatureAlgorithm signature;
8516 * } SignatureAndHashAlgorithm;
8517 *
8518 * SignatureAndHashAlgorithm
8519 * supported_signature_algorithms<2..2^16-2>;
8520 *
8521 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8522 * generalization of the TLS 1.2 signature algorithm extension.
8523 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8524 * `SignatureScheme` field of TLS 1.3
8525 *
8526 */
8527int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8528 const unsigned char *end, size_t *out_len )
8529{
8530 unsigned char *p = buf;
8531 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8532 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8533
8534 *out_len = 0;
8535
8536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8537
8538 /* Check if we have space for header and length field:
8539 * - extension_type (2 bytes)
8540 * - extension_data_length (2 bytes)
8541 * - supported_signature_algorithms_length (2 bytes)
8542 */
8543 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8544 p += 6;
8545
8546 /*
8547 * Write supported_signature_algorithms
8548 */
8549 supported_sig_alg = p;
8550 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8551 if( sig_alg == NULL )
8552 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8553
8554 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8555 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8557 *sig_alg,
8558 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008559 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8560 continue;
8561 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8562 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8563 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008565 *sig_alg,
8566 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008567 }
8568
8569 /* Length of supported_signature_algorithms */
8570 supported_sig_alg_len = p - supported_sig_alg;
8571 if( supported_sig_alg_len == 0 )
8572 {
8573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8575 }
8576
XiaokangQianeaf36512022-04-24 09:07:44 +00008577 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008578 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008579 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8580
XiaokangQianeaf36512022-04-24 09:07:44 +00008581 *out_len = p - buf;
8582
8583#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8584 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8585#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8586 return( 0 );
8587}
8588#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8589
XiaokangQian40a35232022-05-07 09:02:40 +00008590#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008591/*
8592 * mbedtls_ssl_parse_server_name_ext
8593 *
8594 * Structure of server_name extension:
8595 *
8596 * enum {
8597 * host_name(0), (255)
8598 * } NameType;
8599 * opaque HostName<1..2^16-1>;
8600 *
8601 * struct {
8602 * NameType name_type;
8603 * select (name_type) {
8604 * case host_name: HostName;
8605 * } name;
8606 * } ServerName;
8607 * struct {
8608 * ServerName server_name_list<1..2^16-1>
8609 * } ServerNameList;
8610 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008611MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008612int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8613 const unsigned char *buf,
8614 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008615{
8616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8617 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008618 size_t server_name_list_len, hostname_len;
8619 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008620
XiaokangQianf2a94202022-05-20 06:44:24 +00008621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008622
8623 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008624 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008625 p += 2;
8626
XiaokangQian9b2b7712022-05-17 02:57:00 +00008627 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8628 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008629 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008630 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008631 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008632 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008633 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8634 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008635
8636 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8637 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008638 /* sni_name is intended to be used only during the parsing of the
8639 * ClientHello message (it is reset to NULL before the end of
8640 * the message parsing). Thus it is ok to just point to the
8641 * reception buffer and not make a copy of it.
8642 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008643 ssl->handshake->sni_name = p + 3;
8644 ssl->handshake->sni_name_len = hostname_len;
8645 if( ssl->conf->f_sni == NULL )
8646 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008647 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008648 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008649 if( ret != 0 )
8650 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008651 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008652 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8653 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008654 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8655 }
8656 return( 0 );
8657 }
8658
8659 p += hostname_len + 3;
8660 }
8661
8662 return( 0 );
8663}
8664#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8665
XiaokangQianacb39922022-06-17 10:18:48 +00008666#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008667MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008668int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8669 const unsigned char *buf,
8670 const unsigned char *end )
8671{
8672 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008673 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008674 const unsigned char *protocol_name_list;
8675 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008676 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008677
8678 /* If ALPN not configured, just ignore the extension */
8679 if( ssl->conf->alpn_list == NULL )
8680 return( 0 );
8681
8682 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008683 * RFC7301, section 3.1
8684 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008685 *
XiaokangQianc7403452022-06-23 03:24:12 +00008686 * struct {
8687 * ProtocolName protocol_name_list<2..2^16-1>
8688 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008689 */
8690
XiaokangQianc7403452022-06-23 03:24:12 +00008691 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008692 * protocol_name_list_len 2 bytes
8693 * protocol_name_len 1 bytes
8694 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008695 */
XiaokangQianacb39922022-06-17 10:18:48 +00008696 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8697
XiaokangQianc7403452022-06-23 03:24:12 +00008698 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008699 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008700 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008701 protocol_name_list = p;
8702 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008703
8704 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008705 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008706 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008707 protocol_name_len = *p++;
8708 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8709 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008710 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008711 {
8712 MBEDTLS_SSL_PEND_FATAL_ALERT(
8713 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8714 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008715 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008716 }
8717
8718 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008719 }
8720
8721 /* Use our order of preference */
8722 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8723 {
8724 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008725 p = protocol_name_list;
8726 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008727 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008728 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008729 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008730 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008731 {
8732 ssl->alpn_chosen = *alpn;
8733 return( 0 );
8734 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008735
8736 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008737 }
8738 }
8739
XiaokangQian95d5f542022-06-24 02:29:26 +00008740 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008741 MBEDTLS_SSL_PEND_FATAL_ALERT(
8742 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8743 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8744 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8745}
8746
8747int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8748 unsigned char *buf,
8749 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008750 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008751{
8752 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008753 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008754 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008755
8756 if( ssl->alpn_chosen == NULL )
8757 {
8758 return( 0 );
8759 }
8760
XiaokangQian95d5f542022-06-24 02:29:26 +00008761 protocol_name_len = strlen( ssl->alpn_chosen );
8762 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008763
8764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8765 /*
8766 * 0 . 1 ext identifier
8767 * 2 . 3 ext length
8768 * 4 . 5 protocol list length
8769 * 6 . 6 protocol name length
8770 * 7 . 7+n protocol name
8771 */
8772 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8773
XiaokangQian95d5f542022-06-24 02:29:26 +00008774 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008775
XiaokangQian95d5f542022-06-24 02:29:26 +00008776 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8777 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008778 /* Note: the length of the chosen protocol has been checked to be less
8779 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8780 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008781 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008782
XiaokangQian95d5f542022-06-24 02:29:26 +00008783 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008784 return ( 0 );
8785}
8786#endif /* MBEDTLS_SSL_ALPN */
8787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788#endif /* MBEDTLS_SSL_TLS_C */