blob: 065b354d0995039dab6c020a2d7efb92aa54fabb [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
Andrzej Kurek25f27152022-08-17 16:09:31 -040057#include "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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
768 {
769 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200770
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
772 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
773 else
774 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200775
Hanno Becker0f57a652020-02-05 10:37:26 +0000776 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200777 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200778#endif
779
Brett Warrene0edc842021-08-17 09:53:13 +0100780/*
781 * curve_list is translated to IANA TLS group identifiers here because
782 * mbedtls_ssl_conf_curves returns void and so can't return
783 * any error codes.
784 */
785#if defined(MBEDTLS_ECP_C)
786#if !defined(MBEDTLS_DEPRECATED_REMOVED)
787 /* Heap allocate and translate curve_list from internal to IANA group ids */
788 if ( ssl->conf->curve_list != NULL )
789 {
790 size_t length;
791 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
792
793 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
794 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
795
796 /* Leave room for zero termination */
797 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
798 if ( group_list == NULL )
799 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
800
801 for( size_t i = 0; i < length; i++ )
802 {
803 const mbedtls_ecp_curve_info *info =
804 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
805 if ( info == NULL )
806 {
807 mbedtls_free( group_list );
808 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
809 }
810 group_list[i] = info->tls_id;
811 }
812
813 group_list[length] = 0;
814
815 ssl->handshake->group_list = group_list;
816 ssl->handshake->group_list_heap_allocated = 1;
817 }
818 else
819 {
820 ssl->handshake->group_list = ssl->conf->group_list;
821 ssl->handshake->group_list_heap_allocated = 0;
822 }
823#endif /* MBEDTLS_DEPRECATED_REMOVED */
824#endif /* MBEDTLS_ECP_C */
825
Jerry Yuf017ee42022-01-12 15:49:48 +0800826#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
827#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800828#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800829 /* Heap allocate and translate sig_hashes from internal hash identifiers to
830 signature algorithms IANA identifiers. */
831 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800832 ssl->conf->sig_hashes != NULL )
833 {
834 const int *md;
835 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800836 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800837 uint16_t *p;
838
Jerry Yub476a442022-01-21 18:14:45 +0800839#if defined(static_assert)
840 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
841 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
842 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800843#endif
844
Jerry Yuf017ee42022-01-12 15:49:48 +0800845 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
846 {
847 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
848 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800849#if defined(MBEDTLS_ECDSA_C)
850 sig_algs_len += sizeof( uint16_t );
851#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800852
Jerry Yub476a442022-01-21 18:14:45 +0800853#if defined(MBEDTLS_RSA_C)
854 sig_algs_len += sizeof( uint16_t );
855#endif
856 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
857 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800858 }
859
Jerry Yub476a442022-01-21 18:14:45 +0800860 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800861 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
862
Jerry Yub476a442022-01-21 18:14:45 +0800863 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
864 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 if( ssl->handshake->sig_algs == NULL )
866 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
867
868 p = (uint16_t *)ssl->handshake->sig_algs;
869 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
870 {
871 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
872 if( hash == MBEDTLS_SSL_HASH_NONE )
873 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800874#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800875 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
876 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800877#endif
878#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800879 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
880 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800881#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200883 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800884 ssl->handshake->sig_algs_heap_allocated = 1;
885 }
886 else
Jerry Yua69269a2022-01-17 21:06:01 +0800887#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800888 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800889 ssl->handshake->sig_algs_heap_allocated = 0;
890 }
Jerry Yucc539102022-06-27 16:27:35 +0800891#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800892#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000893 return( 0 );
894}
895
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200896#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200897/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200898MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200899static int ssl_cookie_write_dummy( void *ctx,
900 unsigned char **p, unsigned char *end,
901 const unsigned char *cli_id, size_t cli_id_len )
902{
903 ((void) ctx);
904 ((void) p);
905 ((void) end);
906 ((void) cli_id);
907 ((void) cli_id_len);
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200910}
911
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200912MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200913static int ssl_cookie_check_dummy( void *ctx,
914 const unsigned char *cookie, size_t cookie_len,
915 const unsigned char *cli_id, size_t cli_id_len )
916{
917 ((void) ctx);
918 ((void) cookie);
919 ((void) cookie_len);
920 ((void) cli_id);
921 ((void) cli_id_len);
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200924}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200925#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200926
Paul Bakker5121ce52009-01-03 21:22:43 +0000927/*
928 * Initialize an SSL context
929 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200930void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
931{
932 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
933}
934
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200935MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800936static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
937{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100938 const mbedtls_ssl_config *conf = ssl->conf;
939
Ronald Cron6f135e12021-12-08 16:57:54 +0100940#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100941 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
942 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000943 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
944 {
945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
946 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
947 }
948
Jerry Yu60835a82021-08-04 10:13:52 +0800949 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
950 return( 0 );
951 }
952#endif
953
954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100955 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800956 {
957 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
958 return( 0 );
959 }
960#endif
961
Ronald Cron6f135e12021-12-08 16:57:54 +0100962#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100963 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800964 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000965 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
966 {
967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
968 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
969 }
970
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000971 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
972 {
973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977
Ronald Crone1d3f062022-02-10 14:50:54 +0100978 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
979 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800980 }
981#endif
982
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
984 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
985}
986
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200987MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800988static int ssl_conf_check(const mbedtls_ssl_context *ssl)
989{
990 int ret;
991 ret = ssl_conf_version_check( ssl );
992 if( ret != 0 )
993 return( ret );
994
995 /* Space for further checks */
996
997 return( 0 );
998}
999
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001000/*
1001 * Setup an SSL context
1002 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001003
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001004int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001005 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006{
Janos Follath865b3eb2019-12-16 11:46:15 +00001007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001008 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1009 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001011 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001012
Jerry Yu60835a82021-08-04 10:13:52 +08001013 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1014 return( ret );
1015
Paul Bakker62f2dee2012-09-28 07:31:51 +00001016 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001017 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001018 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001019
1020 /* Set to NULL in case of an error condition */
1021 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001022
Darryl Greenb33cc762019-11-28 14:29:44 +00001023#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1024 ssl->in_buf_len = in_buf_len;
1025#endif
1026 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001027 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001030 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001031 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001032 }
1033
Darryl Greenb33cc762019-11-28 14:29:44 +00001034#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1035 ssl->out_buf_len = out_buf_len;
1036#endif
1037 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001038 if( ssl->out_buf == NULL )
1039 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001041 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001042 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 }
1044
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001045 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001046
Johan Pascalb62bb512015-12-03 21:56:45 +01001047#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001048 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001049#endif
1050
Paul Bakker48916f92012-09-16 19:57:18 +00001051 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001052 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
1054 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001055
1056error:
1057 mbedtls_free( ssl->in_buf );
1058 mbedtls_free( ssl->out_buf );
1059
1060 ssl->conf = NULL;
1061
Darryl Greenb33cc762019-11-28 14:29:44 +00001062#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1063 ssl->in_buf_len = 0;
1064 ssl->out_buf_len = 0;
1065#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001066 ssl->in_buf = NULL;
1067 ssl->out_buf = NULL;
1068
1069 ssl->in_hdr = NULL;
1070 ssl->in_ctr = NULL;
1071 ssl->in_len = NULL;
1072 ssl->in_iv = NULL;
1073 ssl->in_msg = NULL;
1074
1075 ssl->out_hdr = NULL;
1076 ssl->out_ctr = NULL;
1077 ssl->out_len = NULL;
1078 ssl->out_iv = NULL;
1079 ssl->out_msg = NULL;
1080
k-stachowiak9f7798e2018-07-31 16:52:32 +02001081 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001082}
1083
1084/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001085 * Reset an initialized and used SSL context for re-use while retaining
1086 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001087 *
1088 * If partial is non-zero, keep data in the input buffer and client ID.
1089 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001090 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001091void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1092 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001093{
Darryl Greenb33cc762019-11-28 14:29:44 +00001094#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1095 size_t in_buf_len = ssl->in_buf_len;
1096 size_t out_buf_len = ssl->out_buf_len;
1097#else
1098 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1099 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1100#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001101
Hanno Beckerb0302c42021-08-03 09:39:42 +01001102#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1103 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001104#endif
1105
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001106 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001107 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001108
Hanno Beckerb0302c42021-08-03 09:39:42 +01001109 mbedtls_ssl_reset_in_out_pointers( ssl );
1110
1111 /* Reset incoming message parsing */
1112 ssl->in_offt = NULL;
1113 ssl->nb_zero = 0;
1114 ssl->in_msgtype = 0;
1115 ssl->in_msglen = 0;
1116 ssl->in_hslen = 0;
1117 ssl->keep_current_message = 0;
1118 ssl->transform_in = NULL;
1119
1120#if defined(MBEDTLS_SSL_PROTO_DTLS)
1121 ssl->next_record_offset = 0;
1122 ssl->in_epoch = 0;
1123#endif
1124
1125 /* Keep current datagram if partial == 1 */
1126 if( partial == 0 )
1127 {
1128 ssl->in_left = 0;
1129 memset( ssl->in_buf, 0, in_buf_len );
1130 }
1131
Ronald Cronad8c17b2022-06-10 17:18:09 +02001132 ssl->send_alert = 0;
1133
Hanno Beckerb0302c42021-08-03 09:39:42 +01001134 /* Reset outgoing message writing */
1135 ssl->out_msgtype = 0;
1136 ssl->out_msglen = 0;
1137 ssl->out_left = 0;
1138 memset( ssl->out_buf, 0, out_buf_len );
1139 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1140 ssl->transform_out = NULL;
1141
1142#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1143 mbedtls_ssl_dtls_replay_reset( ssl );
1144#endif
1145
XiaokangQian2b01dc32022-01-21 02:53:13 +00001146#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001147 if( ssl->transform )
1148 {
1149 mbedtls_ssl_transform_free( ssl->transform );
1150 mbedtls_free( ssl->transform );
1151 ssl->transform = NULL;
1152 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1154
XiaokangQian2b01dc32022-01-21 02:53:13 +00001155#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1156 mbedtls_ssl_transform_free( ssl->transform_application );
1157 mbedtls_free( ssl->transform_application );
1158 ssl->transform_application = NULL;
1159
1160 if( ssl->handshake != NULL )
1161 {
1162 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1163 mbedtls_free( ssl->handshake->transform_earlydata );
1164 ssl->handshake->transform_earlydata = NULL;
1165
1166 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1167 mbedtls_free( ssl->handshake->transform_handshake );
1168 ssl->handshake->transform_handshake = NULL;
1169 }
1170
XiaokangQian2b01dc32022-01-21 02:53:13 +00001171#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001172}
1173
1174int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1175{
1176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1177
1178 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1179
XiaokangQian78b1fa72022-01-19 06:56:30 +00001180 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001181
1182 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#if defined(MBEDTLS_SSL_RENEGOTIATION)
1184 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001185 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001186
1187 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1189 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001192
Hanno Beckerb0302c42021-08-03 09:39:42 +01001193 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001194 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001195 if( ssl->session )
1196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_ssl_session_free( ssl->session );
1198 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001199 ssl->session = NULL;
1200 }
1201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001203 ssl->alpn_chosen = NULL;
1204#endif
1205
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001206#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001207#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001208 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001209#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001210 {
1211 mbedtls_free( ssl->cli_id );
1212 ssl->cli_id = NULL;
1213 ssl->cli_id_len = 0;
1214 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001215#endif
1216
Paul Bakker48916f92012-09-16 19:57:18 +00001217 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1218 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001219
1220 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001221}
1222
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001223/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001224 * Reset an initialized and used SSL context for re-use while retaining
1225 * all application-set variables, function pointers and data.
1226 */
1227int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1228{
Hanno Becker43aefe22020-02-05 10:44:56 +00001229 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001230}
1231
1232/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 * SSL set accessors
1234 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001235void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001236{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001237 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238}
1239
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001240void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001241{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001242 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001243}
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001246void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001248 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001249}
1250#endif
1251
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001252void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001254 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001255}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001258
Hanno Becker1841b0a2018-08-24 11:13:57 +01001259void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1260 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001261{
1262 ssl->disable_datagram_packing = !allow_packing;
1263}
1264
1265void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1266 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001268 conf->hs_timeout_min = min;
1269 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001270}
1271#endif
1272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001273void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001275 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001276}
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001279void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001280 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001281 void *p_vrfy )
1282{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001283 conf->f_vrfy = f_vrfy;
1284 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001287
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001288void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001289 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 void *p_rng )
1291{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001292 conf->f_rng = f_rng;
1293 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001294}
1295
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001296void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001297 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 void *p_dbg )
1299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001300 conf->f_dbg = f_dbg;
1301 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001302}
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001305 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001306 mbedtls_ssl_send_t *f_send,
1307 mbedtls_ssl_recv_t *f_recv,
1308 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001309{
1310 ssl->p_bio = p_bio;
1311 ssl->f_send = f_send;
1312 ssl->f_recv = f_recv;
1313 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001314}
1315
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001316#if defined(MBEDTLS_SSL_PROTO_DTLS)
1317void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1318{
1319 ssl->mtu = mtu;
1320}
1321#endif
1322
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001323void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001324{
1325 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001326}
1327
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001328void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1329 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001330 mbedtls_ssl_set_timer_t *f_set_timer,
1331 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001332{
1333 ssl->p_timer = p_timer;
1334 ssl->f_set_timer = f_set_timer;
1335 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001336
1337 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001338 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001339}
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001342void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001343 void *p_cache,
1344 mbedtls_ssl_cache_get_t *f_get_cache,
1345 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001347 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001348 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001349 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_SSL_CLI_C)
1354int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001355{
Janos Follath865b3eb2019-12-16 11:46:15 +00001356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001357
1358 if( ssl == NULL ||
1359 session == NULL ||
1360 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001361 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001364 }
1365
Hanno Beckere810bbc2021-05-14 16:01:05 +01001366 if( ssl->handshake->resume == 1 )
1367 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1368
Hanno Becker52055ae2019-02-06 14:30:46 +00001369 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1370 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001371 return( ret );
1372
Paul Bakker0a597072012-09-25 21:55:46 +00001373 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001374
1375 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001379void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1380 const int *ciphersuites )
1381{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001382 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383}
1384
Ronald Cron6f135e12021-12-08 16:57:54 +01001385#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001386void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001387 const int kex_modes )
1388{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001389 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001390}
Ronald Cron6f135e12021-12-08 16:57:54 +01001391#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001394void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001395 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001396{
1397 conf->cert_profile = profile;
1398}
1399
Glenn Strauss36872db2022-01-22 05:06:31 -05001400static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1401{
1402 mbedtls_ssl_key_cert *cur = key_cert, *next;
1403
1404 while( cur != NULL )
1405 {
1406 next = cur->next;
1407 mbedtls_free( cur );
1408 cur = next;
1409 }
1410}
1411
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001412/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001413MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001414static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1415 mbedtls_x509_crt *cert,
1416 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001417{
niisato8ee24222018-06-25 19:05:48 +09001418 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001419
Glenn Strauss36872db2022-01-22 05:06:31 -05001420 if( cert == NULL )
1421 {
1422 /* Free list if cert is null */
1423 ssl_key_cert_free( *head );
1424 *head = NULL;
1425 return( 0 );
1426 }
1427
niisato8ee24222018-06-25 19:05:48 +09001428 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1429 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001430 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001431
niisato8ee24222018-06-25 19:05:48 +09001432 new_cert->cert = cert;
1433 new_cert->key = key;
1434 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001435
Glenn Strauss36872db2022-01-22 05:06:31 -05001436 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001437 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001438 {
niisato8ee24222018-06-25 19:05:48 +09001439 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001440 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001441 else
1442 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001443 mbedtls_ssl_key_cert *cur = *head;
1444 while( cur->next != NULL )
1445 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001446 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001447 }
1448
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001449 return( 0 );
1450}
1451
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001452int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001453 mbedtls_x509_crt *own_cert,
1454 mbedtls_pk_context *pk_key )
1455{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001456 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001457}
1458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001459void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001460 mbedtls_x509_crt *ca_chain,
1461 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001462{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001463 conf->ca_chain = ca_chain;
1464 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001465
1466#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1467 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1468 * cannot be used together. */
1469 conf->f_ca_cb = NULL;
1470 conf->p_ca_cb = NULL;
1471#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001472}
Hanno Becker5adaad92019-03-27 16:54:37 +00001473
1474#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1475void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001476 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001477 void *p_ca_cb )
1478{
1479 conf->f_ca_cb = f_ca_cb;
1480 conf->p_ca_cb = p_ca_cb;
1481
1482 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1483 * cannot be used together. */
1484 conf->ca_chain = NULL;
1485 conf->ca_crl = NULL;
1486}
1487#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001489
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001490#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001491const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1492 size_t *name_len )
1493{
1494 *name_len = ssl->handshake->sni_name_len;
1495 return( ssl->handshake->sni_name );
1496}
1497
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001498int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1499 mbedtls_x509_crt *own_cert,
1500 mbedtls_pk_context *pk_key )
1501{
1502 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1503 own_cert, pk_key ) );
1504}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001505
1506void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1507 mbedtls_x509_crt *ca_chain,
1508 mbedtls_x509_crl *ca_crl )
1509{
1510 ssl->handshake->sni_ca_chain = ca_chain;
1511 ssl->handshake->sni_ca_crl = ca_crl;
1512}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001513
Glenn Strauss999ef702022-03-11 01:37:23 -05001514#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1515void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1516 const mbedtls_x509_crt *crt)
1517{
1518 ssl->handshake->dn_hints = crt;
1519}
1520#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1521
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001522void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1523 int authmode )
1524{
1525 ssl->handshake->sni_authmode = authmode;
1526}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001527#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1528
Hanno Becker8927c832019-04-03 12:52:50 +01001529#if defined(MBEDTLS_X509_CRT_PARSE_C)
1530void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1531 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1532 void *p_vrfy )
1533{
1534 ssl->f_vrfy = f_vrfy;
1535 ssl->p_vrfy = p_vrfy;
1536}
1537#endif
1538
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001539#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001540/*
1541 * Set EC J-PAKE password for current handshake
1542 */
1543int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1544 const unsigned char *pw,
1545 size_t pw_len )
1546{
1547 mbedtls_ecjpake_role role;
1548
Janos Follath8eb64132016-06-03 15:40:57 +01001549 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1551
1552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1553 role = MBEDTLS_ECJPAKE_SERVER;
1554 else
1555 role = MBEDTLS_ECJPAKE_CLIENT;
1556
1557 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1558 role,
1559 MBEDTLS_MD_SHA256,
1560 MBEDTLS_ECP_DP_SECP256R1,
1561 pw, pw_len ) );
1562}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001563#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001564
Gilles Peskineeccd8882020-03-10 12:19:08 +01001565#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001566
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001567MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001568static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1569{
1570#if defined(MBEDTLS_USE_PSA_CRYPTO)
1571 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1572 return( 1 );
1573#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001574 if( conf->psk != NULL )
1575 return( 1 );
1576
1577 return( 0 );
1578}
1579
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001580static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1581{
1582 /* Remove reference to existing PSK, if any. */
1583#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001584 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001585 {
1586 /* The maintenance of the PSK key slot is the
1587 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001588 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001589 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001590#endif /* MBEDTLS_USE_PSA_CRYPTO */
1591 if( conf->psk != NULL )
1592 {
1593 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1594
1595 mbedtls_free( conf->psk );
1596 conf->psk = NULL;
1597 conf->psk_len = 0;
1598 }
1599
1600 /* Remove reference to PSK identity, if any. */
1601 if( conf->psk_identity != NULL )
1602 {
1603 mbedtls_free( conf->psk_identity );
1604 conf->psk_identity = NULL;
1605 conf->psk_identity_len = 0;
1606 }
1607}
1608
Hanno Becker7390c712018-11-15 13:33:04 +00001609/* This function assumes that PSK identity in the SSL config is unset.
1610 * It checks that the provided identity is well-formed and attempts
1611 * to make a copy of it in the SSL config.
1612 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001613MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001614static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1615 unsigned char const *psk_identity,
1616 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001617{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001618 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001619 if( psk_identity == NULL ||
1620 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001621 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001622 {
1623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1624 }
1625
Hanno Becker7390c712018-11-15 13:33:04 +00001626 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1627 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001628 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001629
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001630 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001631 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001632
1633 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001634}
1635
Hanno Becker7390c712018-11-15 13:33:04 +00001636int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1637 const unsigned char *psk, size_t psk_len,
1638 const unsigned char *psk_identity, size_t psk_identity_len )
1639{
Janos Follath865b3eb2019-12-16 11:46:15 +00001640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001641
1642 /* We currently only support one PSK, raw or opaque. */
1643 if( ssl_conf_psk_is_configured( conf ) )
1644 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001645
1646 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001647 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001649 if( psk_len == 0 )
1650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1651 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1653
Hanno Becker7390c712018-11-15 13:33:04 +00001654 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1655 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1656 conf->psk_len = psk_len;
1657 memcpy( conf->psk, psk, conf->psk_len );
1658
1659 /* Check and set PSK Identity */
1660 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1661 if( ret != 0 )
1662 ssl_conf_remove_psk( conf );
1663
1664 return( ret );
1665}
1666
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001667static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1668{
1669#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001670 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001671 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001672 /* The maintenance of the external PSK key slot is the
1673 * user's responsibility. */
1674 if( ssl->handshake->psk_opaque_is_internal )
1675 {
1676 psa_destroy_key( ssl->handshake->psk_opaque );
1677 ssl->handshake->psk_opaque_is_internal = 0;
1678 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001679 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001680 }
Neil Armstronge952a302022-05-03 10:22:14 +02001681#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001682 if( ssl->handshake->psk != NULL )
1683 {
1684 mbedtls_platform_zeroize( ssl->handshake->psk,
1685 ssl->handshake->psk_len );
1686 mbedtls_free( ssl->handshake->psk );
1687 ssl->handshake->psk_len = 0;
1688 }
Neil Armstronge952a302022-05-03 10:22:14 +02001689#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001690}
1691
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001692int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1693 const unsigned char *psk, size_t psk_len )
1694{
Neil Armstrong501c9322022-05-03 09:35:09 +02001695#if defined(MBEDTLS_USE_PSA_CRYPTO)
1696 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001697 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001698 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001700#endif /* MBEDTLS_USE_PSA_CRYPTO */
1701
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001702 if( psk == NULL || ssl->handshake == NULL )
1703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1704
1705 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1707
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001708 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001709
Neil Armstrong501c9322022-05-03 09:35:09 +02001710#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001711#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1712 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1713 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001714 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001715 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1716 else
1717 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1718 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1719 }
1720#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001721
Jerry Yu568ec252022-07-22 21:27:34 +08001722#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001723 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1724 {
1725 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1726 psa_set_key_usage_flags( &key_attributes,
1727 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1728 }
1729#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1730
Neil Armstrong501c9322022-05-03 09:35:09 +02001731 psa_set_key_algorithm( &key_attributes, alg );
1732 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1733
1734 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1735 if( status != PSA_SUCCESS )
1736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1737
1738 /* Allow calling psa_destroy_key() on psk remove */
1739 ssl->handshake->psk_opaque_is_internal = 1;
1740 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1741#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001742 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001743 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001744
1745 ssl->handshake->psk_len = psk_len;
1746 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1747
1748 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001749#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001750}
1751
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001752#if defined(MBEDTLS_USE_PSA_CRYPTO)
1753int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001754 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001755 const unsigned char *psk_identity,
1756 size_t psk_identity_len )
1757{
Janos Follath865b3eb2019-12-16 11:46:15 +00001758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001759
1760 /* We currently only support one PSK, raw or opaque. */
1761 if( ssl_conf_psk_is_configured( conf ) )
1762 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001763
Hanno Becker7390c712018-11-15 13:33:04 +00001764 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001765 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001767 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001768
1769 /* Check and set PSK Identity */
1770 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1771 psk_identity_len );
1772 if( ret != 0 )
1773 ssl_conf_remove_psk( conf );
1774
1775 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001776}
1777
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001778int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1779 mbedtls_svc_key_id_t psk )
1780{
1781 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1782 ( ssl->handshake == NULL ) )
1783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1784
1785 ssl_remove_psk( ssl );
1786 ssl->handshake->psk_opaque = psk;
1787 return( 0 );
1788}
1789#endif /* MBEDTLS_USE_PSA_CRYPTO */
1790
1791void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1792 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1793 size_t),
1794 void *p_psk )
1795{
1796 conf->f_psk = f_psk;
1797 conf->p_psk = p_psk;
1798}
1799#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1800
1801#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001802static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1803 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001804{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001805#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001806 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001807 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001808#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001809 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001810 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001811 return( MBEDTLS_SSL_MODE_STREAM );
1812}
1813
1814#else /* MBEDTLS_USE_PSA_CRYPTO */
1815
1816static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1817 mbedtls_cipher_mode_t mode )
1818{
1819#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1820 if( mode == MBEDTLS_MODE_CBC )
1821 return( MBEDTLS_SSL_MODE_CBC );
1822#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1823
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001824#if defined(MBEDTLS_GCM_C) || \
1825 defined(MBEDTLS_CCM_C) || \
1826 defined(MBEDTLS_CHACHAPOLY_C)
1827 if( mode == MBEDTLS_MODE_GCM ||
1828 mode == MBEDTLS_MODE_CCM ||
1829 mode == MBEDTLS_MODE_CHACHAPOLY )
1830 return( MBEDTLS_SSL_MODE_AEAD );
1831#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001832
1833 return( MBEDTLS_SSL_MODE_STREAM );
1834}
Gilles Peskine301711e2022-04-26 16:57:05 +02001835#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001836
Gilles Peskinee108d982022-04-26 16:50:40 +02001837static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1838 mbedtls_ssl_mode_t base_mode,
1839 int encrypt_then_mac )
1840{
1841#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1842 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1843 base_mode == MBEDTLS_SSL_MODE_CBC )
1844 {
1845 return( MBEDTLS_SSL_MODE_CBC_ETM );
1846 }
1847#else
1848 (void) encrypt_then_mac;
1849#endif
1850 return( base_mode );
1851}
1852
Neil Armstrongab555e02022-04-04 11:07:59 +02001853mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001854 const mbedtls_ssl_transform *transform )
1855{
Gilles Peskinee108d982022-04-26 16:50:40 +02001856 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001857#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001858 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001859#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001860 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1861#endif
1862 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001863
Gilles Peskinee108d982022-04-26 16:50:40 +02001864 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001865#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001866 encrypt_then_mac = transform->encrypt_then_mac;
1867#endif
1868 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001869}
1870
Neil Armstrongab555e02022-04-04 11:07:59 +02001871mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001872#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001873 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001874#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001875 const mbedtls_ssl_ciphersuite_t *suite )
1876{
Gilles Peskinee108d982022-04-26 16:50:40 +02001877 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1878
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001879#if defined(MBEDTLS_USE_PSA_CRYPTO)
1880 psa_status_t status;
1881 psa_algorithm_t alg;
1882 psa_key_type_t type;
1883 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001884 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1885 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001886 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001887#else
1888 const mbedtls_cipher_info_t *cipher =
1889 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001890 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001891 {
1892 base_mode =
1893 mbedtls_ssl_get_base_mode(
1894 mbedtls_cipher_info_get_mode( cipher ) );
1895 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001896#endif /* MBEDTLS_USE_PSA_CRYPTO */
1897
Gilles Peskinee108d982022-04-26 16:50:40 +02001898#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1899 int encrypt_then_mac = 0;
1900#endif
1901 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001902}
1903
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001904#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001905
1906#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001907/* Serialization of TLS 1.3 sessions:
1908 *
1909 * struct {
1910 * uint64 ticket_received;
1911 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001912 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001913 * } ClientOnlyData;
1914 *
1915 * struct {
1916 * uint8 endpoint;
1917 * uint8 ciphersuite[2];
1918 * uint32 ticket_age_add;
1919 * uint8 ticket_flags;
1920 * opaque resumption_key<0..255>;
1921 * select ( endpoint ) {
1922 * case client: ClientOnlyData;
1923 * case server: uint64 start_time;
1924 * };
1925 * } serialized_session_tls13;
1926 *
1927 */
1928#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001929MBEDTLS_CHECK_RETURN_CRITICAL
1930static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1931 unsigned char *buf,
1932 size_t buf_len,
1933 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001934{
1935 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001936 size_t needed = 1 /* endpoint */
1937 + 2 /* ciphersuite */
1938 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001939 + 1 /* ticket_flags */
1940 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001941 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001942
1943 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1945 needed += session->resumption_key_len; /* resumption_key */
1946
Jerry Yu438ddd82022-07-07 06:55:50 +00001947#if defined(MBEDTLS_HAVE_TIME)
1948 needed += 8; /* start_time or ticket_received */
1949#endif
1950
1951#if defined(MBEDTLS_SSL_CLI_C)
1952 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1953 {
1954 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001955 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001956
1957 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001958 if( session->ticket_len > SIZE_MAX - needed )
1959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001960
Jerry Yue28d9742022-08-18 15:44:03 +08001961 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001962 }
1963#endif /* MBEDTLS_SSL_CLI_C */
1964
Jerry Yue36fdd62022-08-17 21:31:36 +08001965 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001966 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001967 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001968
1969 p[0] = session->endpoint;
1970 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1971 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1972 p[7] = session->ticket_flags;
1973
1974 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001975 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001976 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001977 memcpy( p, session->resumption_key, session->resumption_key_len );
1978 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001979
1980#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1981 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1982 {
1983 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1984 p += 8;
1985 }
1986#endif /* MBEDTLS_HAVE_TIME */
1987
1988#if defined(MBEDTLS_SSL_CLI_C)
1989 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1990 {
1991#if defined(MBEDTLS_HAVE_TIME)
1992 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
1993 p += 8;
1994#endif
1995 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
1996 p += 4;
1997
1998 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
1999 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002000
2001 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002002 {
2003 memcpy( p, session->ticket, session->ticket_len );
2004 p += session->ticket_len;
2005 }
2006 }
2007#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002008 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002009}
2010
2011MBEDTLS_CHECK_RETURN_CRITICAL
2012static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2013 const unsigned char *buf,
2014 size_t len )
2015{
2016 const unsigned char *p = buf;
2017 const unsigned char *end = buf + len;
2018
2019 if( end - p < 9 )
2020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2021 session->endpoint = p[0];
2022 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2023 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2024 session->ticket_flags = p[7];
2025
2026 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002027 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002028 p += 9;
2029
Jerry Yubc7c1a42022-07-21 22:57:37 +08002030 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2032
Jerry Yubc7c1a42022-07-21 22:57:37 +08002033 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002035 memcpy( session->resumption_key, p, session->resumption_key_len );
2036 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002037
2038#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2039 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2040 {
2041 if( end - p < 8 )
2042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2043 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2044 p += 8;
2045 }
2046#endif /* MBEDTLS_HAVE_TIME */
2047
2048#if defined(MBEDTLS_SSL_CLI_C)
2049 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2050 {
2051#if defined(MBEDTLS_HAVE_TIME)
2052 if( end - p < 8 )
2053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2054 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2055 p += 8;
2056#endif
2057 if( end - p < 4 )
2058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2059 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2060 p += 4;
2061
2062 if( end - p < 2 )
2063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2064 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2065 p += 2;
2066
2067 if( end - p < ( long int )session->ticket_len )
2068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2069 if( session->ticket_len > 0 )
2070 {
2071 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2072 if( session->ticket == NULL )
2073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2074 memcpy( session->ticket, p, session->ticket_len );
2075 p += session->ticket_len;
2076 }
2077 }
2078#endif /* MBEDTLS_SSL_CLI_C */
2079
2080 return( 0 );
2081
2082}
2083#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002084MBEDTLS_CHECK_RETURN_CRITICAL
2085static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2086 unsigned char *buf,
2087 size_t buf_len,
2088 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002089{
2090 ((void) session);
2091 ((void) buf);
2092 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002093 *olen = 0;
2094 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002095}
Jerry Yu438ddd82022-07-07 06:55:50 +00002096
2097static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2098 unsigned char *buf,
2099 size_t buf_len )
2100{
2101 ((void) session);
2102 ((void) buf);
2103 ((void) buf_len);
2104 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2105}
2106#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002107#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2108
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002109psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002110 size_t taglen,
2111 psa_algorithm_t *alg,
2112 psa_key_type_t *key_type,
2113 size_t *key_size )
2114{
2115 switch ( mbedtls_cipher_type )
2116 {
2117 case MBEDTLS_CIPHER_AES_128_CBC:
2118 *alg = PSA_ALG_CBC_NO_PADDING;
2119 *key_type = PSA_KEY_TYPE_AES;
2120 *key_size = 128;
2121 break;
2122 case MBEDTLS_CIPHER_AES_128_CCM:
2123 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2124 *key_type = PSA_KEY_TYPE_AES;
2125 *key_size = 128;
2126 break;
2127 case MBEDTLS_CIPHER_AES_128_GCM:
2128 *alg = PSA_ALG_GCM;
2129 *key_type = PSA_KEY_TYPE_AES;
2130 *key_size = 128;
2131 break;
2132 case MBEDTLS_CIPHER_AES_192_CCM:
2133 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2134 *key_type = PSA_KEY_TYPE_AES;
2135 *key_size = 192;
2136 break;
2137 case MBEDTLS_CIPHER_AES_192_GCM:
2138 *alg = PSA_ALG_GCM;
2139 *key_type = PSA_KEY_TYPE_AES;
2140 *key_size = 192;
2141 break;
2142 case MBEDTLS_CIPHER_AES_256_CBC:
2143 *alg = PSA_ALG_CBC_NO_PADDING;
2144 *key_type = PSA_KEY_TYPE_AES;
2145 *key_size = 256;
2146 break;
2147 case MBEDTLS_CIPHER_AES_256_CCM:
2148 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2149 *key_type = PSA_KEY_TYPE_AES;
2150 *key_size = 256;
2151 break;
2152 case MBEDTLS_CIPHER_AES_256_GCM:
2153 *alg = PSA_ALG_GCM;
2154 *key_type = PSA_KEY_TYPE_AES;
2155 *key_size = 256;
2156 break;
2157 case MBEDTLS_CIPHER_ARIA_128_CBC:
2158 *alg = PSA_ALG_CBC_NO_PADDING;
2159 *key_type = PSA_KEY_TYPE_ARIA;
2160 *key_size = 128;
2161 break;
2162 case MBEDTLS_CIPHER_ARIA_128_CCM:
2163 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2164 *key_type = PSA_KEY_TYPE_ARIA;
2165 *key_size = 128;
2166 break;
2167 case MBEDTLS_CIPHER_ARIA_128_GCM:
2168 *alg = PSA_ALG_GCM;
2169 *key_type = PSA_KEY_TYPE_ARIA;
2170 *key_size = 128;
2171 break;
2172 case MBEDTLS_CIPHER_ARIA_192_CCM:
2173 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2174 *key_type = PSA_KEY_TYPE_ARIA;
2175 *key_size = 192;
2176 break;
2177 case MBEDTLS_CIPHER_ARIA_192_GCM:
2178 *alg = PSA_ALG_GCM;
2179 *key_type = PSA_KEY_TYPE_ARIA;
2180 *key_size = 192;
2181 break;
2182 case MBEDTLS_CIPHER_ARIA_256_CBC:
2183 *alg = PSA_ALG_CBC_NO_PADDING;
2184 *key_type = PSA_KEY_TYPE_ARIA;
2185 *key_size = 256;
2186 break;
2187 case MBEDTLS_CIPHER_ARIA_256_CCM:
2188 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2189 *key_type = PSA_KEY_TYPE_ARIA;
2190 *key_size = 256;
2191 break;
2192 case MBEDTLS_CIPHER_ARIA_256_GCM:
2193 *alg = PSA_ALG_GCM;
2194 *key_type = PSA_KEY_TYPE_ARIA;
2195 *key_size = 256;
2196 break;
2197 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2198 *alg = PSA_ALG_CBC_NO_PADDING;
2199 *key_type = PSA_KEY_TYPE_CAMELLIA;
2200 *key_size = 128;
2201 break;
2202 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2203 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2204 *key_type = PSA_KEY_TYPE_CAMELLIA;
2205 *key_size = 128;
2206 break;
2207 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2208 *alg = PSA_ALG_GCM;
2209 *key_type = PSA_KEY_TYPE_CAMELLIA;
2210 *key_size = 128;
2211 break;
2212 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2213 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2214 *key_type = PSA_KEY_TYPE_CAMELLIA;
2215 *key_size = 192;
2216 break;
2217 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2218 *alg = PSA_ALG_GCM;
2219 *key_type = PSA_KEY_TYPE_CAMELLIA;
2220 *key_size = 192;
2221 break;
2222 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2223 *alg = PSA_ALG_CBC_NO_PADDING;
2224 *key_type = PSA_KEY_TYPE_CAMELLIA;
2225 *key_size = 256;
2226 break;
2227 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2228 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2229 *key_type = PSA_KEY_TYPE_CAMELLIA;
2230 *key_size = 256;
2231 break;
2232 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2233 *alg = PSA_ALG_GCM;
2234 *key_type = PSA_KEY_TYPE_CAMELLIA;
2235 *key_size = 256;
2236 break;
2237 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2238 *alg = PSA_ALG_CHACHA20_POLY1305;
2239 *key_type = PSA_KEY_TYPE_CHACHA20;
2240 *key_size = 256;
2241 break;
2242 case MBEDTLS_CIPHER_NULL:
2243 *alg = MBEDTLS_SSL_NULL_CIPHER;
2244 *key_type = 0;
2245 *key_size = 0;
2246 break;
2247 default:
2248 return PSA_ERROR_NOT_SUPPORTED;
2249 }
2250
2251 return PSA_SUCCESS;
2252}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002253#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002254
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002255#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002256int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2257 const unsigned char *dhm_P, size_t P_len,
2258 const unsigned char *dhm_G, size_t G_len )
2259{
Janos Follath865b3eb2019-12-16 11:46:15 +00002260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002261
Glenn Strausscee11292021-12-20 01:43:17 -05002262 mbedtls_mpi_free( &conf->dhm_P );
2263 mbedtls_mpi_free( &conf->dhm_G );
2264
Hanno Beckera90658f2017-10-04 15:29:08 +01002265 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2266 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2267 {
2268 mbedtls_mpi_free( &conf->dhm_P );
2269 mbedtls_mpi_free( &conf->dhm_G );
2270 return( ret );
2271 }
2272
2273 return( 0 );
2274}
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002276int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002277{
Janos Follath865b3eb2019-12-16 11:46:15 +00002278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002279
Glenn Strausscee11292021-12-20 01:43:17 -05002280 mbedtls_mpi_free( &conf->dhm_P );
2281 mbedtls_mpi_free( &conf->dhm_G );
2282
Gilles Peskinee5702482021-06-11 21:59:08 +02002283 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2284 &conf->dhm_P ) ) != 0 ||
2285 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2286 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002287 {
2288 mbedtls_mpi_free( &conf->dhm_P );
2289 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002290 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002291 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002292
2293 return( 0 );
2294}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002295#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002296
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002297#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2298/*
2299 * Set the minimum length for Diffie-Hellman parameters
2300 */
2301void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2302 unsigned int bitlen )
2303{
2304 conf->dhm_min_bitlen = bitlen;
2305}
2306#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2307
Gilles Peskineeccd8882020-03-10 12:19:08 +01002308#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002309#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002310/*
2311 * Set allowed/preferred hashes for handshake signatures
2312 */
2313void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2314 const int *hashes )
2315{
2316 conf->sig_hashes = hashes;
2317}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002318#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002319
Jerry Yuf017ee42022-01-12 15:49:48 +08002320/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002321void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2322 const uint16_t* sig_algs )
2323{
Jerry Yuf017ee42022-01-12 15:49:48 +08002324#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2325 conf->sig_hashes = NULL;
2326#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2327 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002328}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002329#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002330
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002331#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002332#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002333/*
2334 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002335 *
2336 * mbedtls_ssl_setup() takes the provided list
2337 * and translates it to a list of IANA TLS group identifiers,
2338 * stored in ssl->handshake->group_list.
2339 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002340 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002341void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002342 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002343{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002344 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002345 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002346}
Brett Warrene0edc842021-08-17 09:53:13 +01002347#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002348#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002349
Brett Warrene0edc842021-08-17 09:53:13 +01002350/*
2351 * Set the allowed groups
2352 */
2353void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2354 const uint16_t *group_list )
2355{
2356#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2357 conf->curve_list = NULL;
2358#endif
2359 conf->group_list = group_list;
2360}
2361
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002362#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002364{
Hanno Becker947194e2017-04-07 13:25:49 +01002365 /* Initialize to suppress unnecessary compiler warning */
2366 size_t hostname_len = 0;
2367
2368 /* Check if new hostname is valid before
2369 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002370 if( hostname != NULL )
2371 {
2372 hostname_len = strlen( hostname );
2373
2374 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2376 }
2377
2378 /* Now it's clear that we will overwrite the old hostname,
2379 * so we can free it safely */
2380
2381 if( ssl->hostname != NULL )
2382 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002383 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002384 mbedtls_free( ssl->hostname );
2385 }
2386
2387 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002388
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002390 {
2391 ssl->hostname = NULL;
2392 }
2393 else
2394 {
2395 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002396 if( ssl->hostname == NULL )
2397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002398
Hanno Becker947194e2017-04-07 13:25:49 +01002399 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002400
Hanno Becker947194e2017-04-07 13:25:49 +01002401 ssl->hostname[hostname_len] = '\0';
2402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
2404 return( 0 );
2405}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002406#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002408#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002409void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002411 const unsigned char *, size_t),
2412 void *p_sni )
2413{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002414 conf->f_sni = f_sni;
2415 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002420int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002421{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002422 size_t cur_len, tot_len;
2423 const char **p;
2424
2425 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002426 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2427 * MUST NOT be truncated."
2428 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002429 */
2430 tot_len = 0;
2431 for( p = protos; *p != NULL; p++ )
2432 {
2433 cur_len = strlen( *p );
2434 tot_len += cur_len;
2435
Ronald Cron8216dd32020-04-23 16:41:44 +02002436 if( ( cur_len == 0 ) ||
2437 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2438 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002440 }
2441
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002442 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002443
2444 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002445}
2446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002448{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002449 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002452
Johan Pascalb62bb512015-12-03 21:56:45 +01002453#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002454void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2455 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002456{
2457 conf->dtls_srtp_mki_support = support_mki_value;
2458}
2459
Ron Eldoref72faf2018-07-12 11:54:20 +03002460int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2461 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002462 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002463{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002464 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002465 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002467 }
Ron Eldor591f1622018-01-22 12:30:04 +02002468
2469 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002470 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002471 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002472 }
Ron Eldor591f1622018-01-22 12:30:04 +02002473
2474 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2475 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002476 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002477}
2478
Ron Eldoref72faf2018-07-12 11:54:20 +03002479int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002480 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002481{
Johan Pascal253d0262020-09-22 13:04:45 +02002482 const mbedtls_ssl_srtp_profile *p;
2483 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002484
Johan Pascal253d0262020-09-22 13:04:45 +02002485 /* check the profiles list: all entry must be valid,
2486 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002487 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2488 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2489 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002490 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002491 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002492 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002493 list_size++;
2494 }
2495 else
2496 {
2497 /* unsupported value, stop parsing and set the size to an error value */
2498 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002499 }
2500 }
2501
Johan Pascal5ef72d22020-10-28 17:05:47 +01002502 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002503 {
Johan Pascal253d0262020-09-22 13:04:45 +02002504 conf->dtls_srtp_profile_list = NULL;
2505 conf->dtls_srtp_profile_list_len = 0;
2506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2507 }
2508
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002509 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002510 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002511
2512 return( 0 );
2513}
2514
Johan Pascal5ef72d22020-10-28 17:05:47 +01002515void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2516 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002517{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002518 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2519 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002520 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002521 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002522 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002523 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002524 else
2525 {
2526 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002527 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2528 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002529 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002530}
Johan Pascalb62bb512015-12-03 21:56:45 +01002531#endif /* MBEDTLS_SSL_DTLS_SRTP */
2532
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302533#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002534void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002535{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002536 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002537}
2538
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002539void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002540{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002541 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002542}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302543#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002544
Janos Follath088ce432017-04-10 12:42:31 +01002545#if defined(MBEDTLS_SSL_SRV_C)
2546void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2547 char cert_req_ca_list )
2548{
2549 conf->cert_req_ca_list = cert_req_ca_list;
2550}
2551#endif
2552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002554void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002555{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002556 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002557}
2558#endif
2559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002561void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002563 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002564}
2565#endif
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002568int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002571 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002574 }
2575
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002576 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002577
2578 return( 0 );
2579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002581
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002582void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002583{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002584 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002585}
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002588void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002590 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002591}
2592
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002593void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002594{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002595 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002596}
2597
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002598void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002599 const unsigned char period[8] )
2600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002601 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002606#if defined(MBEDTLS_SSL_CLI_C)
2607void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002608{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002609 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002610}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002611#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002612
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002613#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002614
2615#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
2616void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2617 uint16_t num_tickets )
2618{
2619 conf->new_session_tickets = num_tickets;
2620}
2621#endif
2622
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002623void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2624 mbedtls_ssl_ticket_write_t *f_ticket_write,
2625 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2626 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002627{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002628 conf->f_ticket_write = f_ticket_write;
2629 conf->f_ticket_parse = f_ticket_parse;
2630 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002631}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002634
Hanno Becker7e6c1782021-06-08 09:24:55 +01002635void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2636 mbedtls_ssl_export_keys_t *f_export_keys,
2637 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002638{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002639 ssl->f_export_keys = f_export_keys;
2640 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002641}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002642
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002643#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002644void mbedtls_ssl_conf_async_private_cb(
2645 mbedtls_ssl_config *conf,
2646 mbedtls_ssl_async_sign_t *f_async_sign,
2647 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2648 mbedtls_ssl_async_resume_t *f_async_resume,
2649 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002650 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002651{
2652 conf->f_async_sign_start = f_async_sign;
2653 conf->f_async_decrypt_start = f_async_decrypt;
2654 conf->f_async_resume = f_async_resume;
2655 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002656 conf->p_async_config_data = async_config_data;
2657}
2658
Gilles Peskine8f97af72018-04-26 11:46:10 +02002659void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2660{
2661 return( conf->p_async_config_data );
2662}
2663
Gilles Peskine1febfef2018-04-30 11:54:39 +02002664void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002665{
2666 if( ssl->handshake == NULL )
2667 return( NULL );
2668 else
2669 return( ssl->handshake->user_async_ctx );
2670}
2671
Gilles Peskine1febfef2018-04-30 11:54:39 +02002672void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002673 void *ctx )
2674{
2675 if( ssl->handshake != NULL )
2676 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002677}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002678#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002679
Paul Bakker5121ce52009-01-03 21:22:43 +00002680/*
2681 * SSL get accessors
2682 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002683uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002684{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002685 if( ssl->session != NULL )
2686 return( ssl->session->verify_result );
2687
2688 if( ssl->session_negotiate != NULL )
2689 return( ssl->session_negotiate->verify_result );
2690
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002691 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692}
2693
Glenn Strauss8f526902022-01-13 00:04:49 -05002694int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2695{
2696 if( ssl == NULL || ssl->session == NULL )
2697 return( 0 );
2698
2699 return( ssl->session->ciphersuite );
2700}
2701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002703{
Paul Bakker926c8e42013-03-06 10:23:34 +01002704 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002705 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002708}
2709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002713 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002714 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002715 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002716 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002717 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002718 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002719 default:
2720 return( "unknown (DTLS)" );
2721 }
2722 }
2723#endif
2724
Glenn Strauss60bfe602022-03-14 19:04:24 -04002725 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002726 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002727 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002728 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002729 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002730 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002731 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002732 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002733 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002734}
2735
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002736#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002737size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2738{
David Horstmann95d516f2021-05-04 18:36:56 +01002739 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002740 size_t read_mfl;
2741
2742 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2743 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2744 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2745 {
2746 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2747 }
2748
2749 /* Check if a smaller max length was negotiated */
2750 if( ssl->session_out != NULL )
2751 {
2752 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2753 if( read_mfl < max_len )
2754 {
2755 max_len = read_mfl;
2756 }
2757 }
2758
2759 // During a handshake, use the value being negotiated
2760 if( ssl->session_negotiate != NULL )
2761 {
2762 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2763 if( read_mfl < max_len )
2764 {
2765 max_len = read_mfl;
2766 }
2767 }
2768
2769 return( max_len );
2770}
2771
2772size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002773{
2774 size_t max_len;
2775
2776 /*
2777 * Assume mfl_code is correct since it was checked when set
2778 */
Angus Grattond8213d02016-05-25 20:56:48 +10002779 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002780
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002781 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002782 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002783 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002784 {
Angus Grattond8213d02016-05-25 20:56:48 +10002785 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002786 }
2787
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002788 /* During a handshake, use the value being negotiated */
2789 if( ssl->session_negotiate != NULL &&
2790 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2791 {
2792 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2793 }
2794
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002795 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002796}
2797#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2798
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002799#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002800size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002801{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002802 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2803 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2804 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2805 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2806 return ( 0 );
2807
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002808 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2809 return( ssl->mtu );
2810
2811 if( ssl->mtu == 0 )
2812 return( ssl->handshake->mtu );
2813
2814 return( ssl->mtu < ssl->handshake->mtu ?
2815 ssl->mtu : ssl->handshake->mtu );
2816}
2817#endif /* MBEDTLS_SSL_PROTO_DTLS */
2818
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002819int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2820{
2821 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2822
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002823#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2824 !defined(MBEDTLS_SSL_PROTO_DTLS)
2825 (void) ssl;
2826#endif
2827
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002828#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002829 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002830
2831 if( max_len > mfl )
2832 max_len = mfl;
2833#endif
2834
2835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002836 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002837 {
Hanno Becker89490712020-02-05 10:50:12 +00002838 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002839 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2840 const size_t overhead = (size_t) ret;
2841
2842 if( ret < 0 )
2843 return( ret );
2844
2845 if( mtu <= overhead )
2846 {
2847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2848 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2849 }
2850
2851 if( max_len > mtu - overhead )
2852 max_len = mtu - overhead;
2853 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002854#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002855
Hanno Becker0defedb2018-08-10 12:35:02 +01002856#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2857 !defined(MBEDTLS_SSL_PROTO_DTLS)
2858 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002859#endif
2860
2861 return( (int) max_len );
2862}
2863
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002864int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2865{
2866 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2867
2868#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2869 (void) ssl;
2870#endif
2871
2872#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2873 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2874
2875 if( max_len > mfl )
2876 max_len = mfl;
2877#endif
2878
2879 return( (int) max_len );
2880}
2881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#if defined(MBEDTLS_X509_CRT_PARSE_C)
2883const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002884{
2885 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002886 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002887
Hanno Beckere6824572019-02-07 13:18:46 +00002888#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002889 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002890#else
2891 return( NULL );
2892#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002897int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2898 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002899{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002900 int ret;
2901
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002902 if( ssl == NULL ||
2903 dst == NULL ||
2904 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002905 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002908 }
2909
Hanno Beckere810bbc2021-05-14 16:01:05 +01002910 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2911 * idempotent: Each session can only be exported once.
2912 *
2913 * (This is in preparation for TLS 1.3 support where we will
2914 * need the ability to export multiple sessions (aka tickets),
2915 * which will be achieved by calling mbedtls_ssl_get_session()
2916 * multiple times until it fails.)
2917 *
2918 * Check whether we have already exported the current session,
2919 * and fail if so.
2920 */
2921 if( ssl->session->exported == 1 )
2922 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2923
2924 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2925 if( ret != 0 )
2926 return( ret );
2927
2928 /* Remember that we've exported the session. */
2929 ssl->session->exported = 1;
2930 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002933
Paul Bakker5121ce52009-01-03 21:22:43 +00002934/*
Hanno Beckera835da52019-05-16 12:39:07 +01002935 * Define ticket header determining Mbed TLS version
2936 * and structure of the ticket.
2937 */
2938
Hanno Becker94ef3b32019-05-16 12:50:45 +01002939/*
Hanno Becker50b59662019-05-28 14:30:45 +01002940 * Define bitflag determining compile-time settings influencing
2941 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002942 */
2943
Hanno Becker50b59662019-05-28 14:30:45 +01002944#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002945#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002946#else
Hanno Becker3e088662019-05-29 11:10:18 +01002947#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002948#endif /* MBEDTLS_HAVE_TIME */
2949
2950#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002951#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002952#else
Hanno Becker3e088662019-05-29 11:10:18 +01002953#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002954#endif /* MBEDTLS_X509_CRT_PARSE_C */
2955
2956#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002957#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002958#else
Hanno Becker3e088662019-05-29 11:10:18 +01002959#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002960#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2961
2962#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002963#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002964#else
Hanno Becker3e088662019-05-29 11:10:18 +01002965#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002966#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2967
Hanno Becker94ef3b32019-05-16 12:50:45 +01002968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002969#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002970#else
Hanno Becker3e088662019-05-29 11:10:18 +01002971#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002972#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2973
Hanno Becker94ef3b32019-05-16 12:50:45 +01002974#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2975#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2976#else
2977#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2978#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2979
Hanno Becker3e088662019-05-29 11:10:18 +01002980#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2981#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2982#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2983#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002984#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2985#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002986
Hanno Becker50b59662019-05-28 14:30:45 +01002987#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002988 ( (uint16_t) ( \
2989 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2990 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2991 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2992 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002993 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002994 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002995
Hanno Beckerf8787072019-05-16 12:41:07 +01002996static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002997 MBEDTLS_VERSION_MAJOR,
2998 MBEDTLS_VERSION_MINOR,
2999 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003000 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3001 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003002};
Hanno Beckera835da52019-05-16 12:39:07 +01003003
3004/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003005 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003006 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003007 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003008 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003009 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003010 * opaque mbedtls_version[3]; // library version: major, minor, patch
3011 * opaque session_format[2]; // library-version specific 16-bit field
3012 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003013 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003014 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003015 * Note: When updating the format, remember to keep
3016 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003017 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003018 * // In this version, `session_format` determines
3019 * // the setting of those compile-time
3020 * // configuration options which influence
3021 * // the structure of mbedtls_ssl_session.
3022 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003023 * uint8_t minor_ver; // Protocol minor version. Possible values:
3024 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003025 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003026 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003027 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003028 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003029 * serialized_session_tls12 data;
3030 *
3031 * };
3032 *
3033 * } serialized_session;
3034 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003035 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003036
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003037MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003038static int ssl_session_save( const mbedtls_ssl_session *session,
3039 unsigned char omit_header,
3040 unsigned char *buf,
3041 size_t buf_len,
3042 size_t *olen )
3043{
3044 unsigned char *p = buf;
3045 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003046 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003047#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3048 size_t out_len;
3049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3050#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003051 if( session == NULL )
3052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3053
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003054 if( !omit_header )
3055 {
3056 /*
3057 * Add Mbed TLS version identifier
3058 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003059 used += sizeof( ssl_serialized_session_header );
3060
3061 if( used <= buf_len )
3062 {
3063 memcpy( p, ssl_serialized_session_header,
3064 sizeof( ssl_serialized_session_header ) );
3065 p += sizeof( ssl_serialized_session_header );
3066 }
3067 }
3068
3069 /*
3070 * TLS version identifier
3071 */
3072 used += 1;
3073 if( used <= buf_len )
3074 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003075 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003076 }
3077
3078 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003079 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003080 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003081 {
3082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003083 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003084 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003085 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003086#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3087
Jerry Yu251a12e2022-07-13 15:15:48 +08003088#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3089 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003090 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3091 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003092 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003093 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003094 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003095#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3096
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003097 default:
3098 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3099 }
3100
3101 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003102 if( used > buf_len )
3103 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003104
3105 return( 0 );
3106}
3107
3108/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003109 * Public wrapper for ssl_session_save()
3110 */
3111int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3112 unsigned char *buf,
3113 size_t buf_len,
3114 size_t *olen )
3115{
3116 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3117}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003118
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003119/*
3120 * Deserialize session, see mbedtls_ssl_session_save() for format.
3121 *
3122 * This internal version is wrapped by a public function that cleans up in
3123 * case of error, and has an extra option omit_header.
3124 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003125MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003126static int ssl_session_load( mbedtls_ssl_session *session,
3127 unsigned char omit_header,
3128 const unsigned char *buf,
3129 size_t len )
3130{
3131 const unsigned char *p = buf;
3132 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003133 size_t remaining_len;
3134
3135
3136 if( session == NULL )
3137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003138
3139 if( !omit_header )
3140 {
3141 /*
3142 * Check Mbed TLS version identifier
3143 */
3144
3145 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3147
3148 if( memcmp( p, ssl_serialized_session_header,
3149 sizeof( ssl_serialized_session_header ) ) != 0 )
3150 {
3151 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3152 }
3153 p += sizeof( ssl_serialized_session_header );
3154 }
3155
3156 /*
3157 * TLS version identifier
3158 */
3159 if( 1 > (size_t)( end - p ) )
3160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003161 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003162
3163 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003164 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003165 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003166 {
3167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003168 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003169 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3171
Jerry Yu438ddd82022-07-07 06:55:50 +00003172#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3173 case MBEDTLS_SSL_VERSION_TLS1_3:
3174 return( ssl_tls13_session_load( session, p, remaining_len ) );
3175#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3176
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003177 default:
3178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3179 }
3180}
3181
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003182/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003183 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003184 */
3185int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3186 const unsigned char *buf,
3187 size_t len )
3188{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003189 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003190
3191 if( ret != 0 )
3192 mbedtls_ssl_session_free( session );
3193
3194 return( ret );
3195}
3196
3197/*
Paul Bakker1961b702013-01-25 14:49:24 +01003198 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003200MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003201static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3202{
3203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3204
Ronald Cron66dbf912022-02-02 15:33:46 +01003205 /*
3206 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003207 * that were written into the output buffer by the previous handshake step,
3208 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003209 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3210 * We proceed to the next handshake step only when all data from the
3211 * previous one have been sent to the peer, thus we make sure that this is
3212 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3213 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3214 * we have to wait before to go ahead.
3215 * In the case of TLS 1.3, handshake step handlers do not send data to the
3216 * peer. Data are only sent here and through
3217 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003218 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003219 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003220 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3221 return( ret );
3222
3223#if defined(MBEDTLS_SSL_PROTO_DTLS)
3224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3225 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3226 {
3227 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3228 return( ret );
3229 }
3230#endif /* MBEDTLS_SSL_PROTO_DTLS */
3231
3232 return( ret );
3233}
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003236{
Hanno Becker41934dd2021-08-07 19:13:43 +01003237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Hanno Becker41934dd2021-08-07 19:13:43 +01003239 if( ssl == NULL ||
3240 ssl->conf == NULL ||
3241 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003242 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003243 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003245 }
3246
3247 ret = ssl_prepare_handshake_step( ssl );
3248 if( ret != 0 )
3249 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003250
Jerry Yue7047812021-09-13 19:26:39 +08003251 ret = mbedtls_ssl_handle_pending_alert( ssl );
3252 if( ret != 0 )
3253 goto cleanup;
3254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003256 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003257 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3259 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003260
Ronald Cron9f0fba32022-02-10 16:45:15 +01003261 switch( ssl->state )
3262 {
3263 case MBEDTLS_SSL_HELLO_REQUEST:
3264 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3265 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003266
Ronald Cron9f0fba32022-02-10 16:45:15 +01003267 case MBEDTLS_SSL_CLIENT_HELLO:
3268 ret = mbedtls_ssl_write_client_hello( ssl );
3269 break;
3270
3271 default:
3272#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003273 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003274 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3275 else
3276 ret = mbedtls_ssl_handshake_client_step( ssl );
3277#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3278 ret = mbedtls_ssl_handshake_client_step( ssl );
3279#else
3280 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3281#endif
3282 }
Jerry Yub9930e72021-08-06 17:11:51 +08003283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003286 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003287 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003288#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003289 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003290 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003291#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003292
3293#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3294 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3295 ret = mbedtls_ssl_handshake_server_step( ssl );
3296#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003298#endif
3299
Jerry Yue7047812021-09-13 19:26:39 +08003300 if( ret != 0 )
3301 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003302 /* handshake_step return error. And it is same
3303 * with alert_reason.
3304 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003305 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003306 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003307 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003308 goto cleanup;
3309 }
3310 }
3311
3312cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003313 return( ret );
3314}
3315
3316/*
3317 * Perform the SSL handshake
3318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003320{
3321 int ret = 0;
3322
Hanno Beckera817ea42020-10-20 15:20:23 +01003323 /* Sanity checks */
3324
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003325 if( ssl == NULL || ssl->conf == NULL )
3326 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3327
Hanno Beckera817ea42020-10-20 15:20:23 +01003328#if defined(MBEDTLS_SSL_PROTO_DTLS)
3329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3330 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3331 {
3332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3333 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3335 }
3336#endif /* MBEDTLS_SSL_PROTO_DTLS */
3337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003339
Hanno Beckera817ea42020-10-20 15:20:23 +01003340 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003341 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003344
3345 if( ret != 0 )
3346 break;
3347 }
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003350
3351 return( ret );
3352}
3353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354#if defined(MBEDTLS_SSL_RENEGOTIATION)
3355#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003356/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003357 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003358 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003359MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003361{
Janos Follath865b3eb2019-12-16 11:46:15 +00003362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003365
3366 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3368 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003369
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003370 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003371 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003373 return( ret );
3374 }
3375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003377
3378 return( 0 );
3379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003381
3382/*
3383 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 * - any side: calling mbedtls_ssl_renegotiate(),
3385 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3386 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003387 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003388 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003390 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003391int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003392{
Janos Follath865b3eb2019-12-16 11:46:15 +00003393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003396
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003397 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3398 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003399
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003400 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3401 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003405 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003406 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003407 ssl->handshake->out_msg_seq = 1;
3408 else
3409 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003410 }
3411#endif
3412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3414 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003419 return( ret );
3420 }
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003423
3424 return( 0 );
3425}
3426
3427/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003428 * Renegotiate current connection on client,
3429 * or request renegotiation on server
3430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003434
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003435 if( ssl == NULL || ssl->conf == NULL )
3436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003439 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003440 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003441 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003442 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003446
3447 /* Did we already try/start sending HelloRequest? */
3448 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003450
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003451 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003452 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003456 /*
3457 * On client, either start the renegotiation process or,
3458 * if already in progress, continue the handshake
3459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003461 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003462 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003464
Hanno Becker40cdaa12020-02-05 10:48:27 +00003465 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003466 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003468 return( ret );
3469 }
3470 }
3471 else
3472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003476 return( ret );
3477 }
3478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003480
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003481 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003484
Gilles Peskine9b562d52018-04-25 20:32:43 +02003485void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003486{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003487 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3488
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003489 if( handshake == NULL )
3490 return;
3491
Brett Warrene0edc842021-08-17 09:53:13 +01003492#if defined(MBEDTLS_ECP_C)
3493#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3494 if ( ssl->handshake->group_list_heap_allocated )
3495 mbedtls_free( (void*) handshake->group_list );
3496 handshake->group_list = NULL;
3497#endif /* MBEDTLS_DEPRECATED_REMOVED */
3498#endif /* MBEDTLS_ECP_C */
3499
Jerry Yuf017ee42022-01-12 15:49:48 +08003500#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3501#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3502 if ( ssl->handshake->sig_algs_heap_allocated )
3503 mbedtls_free( (void*) handshake->sig_algs );
3504 handshake->sig_algs = NULL;
3505#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003506#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3507 if( ssl->handshake->certificate_request_context )
3508 {
3509 mbedtls_free( (void*) handshake->certificate_request_context );
3510 }
3511#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003512#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3513
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003514#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3515 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3516 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003517 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003518 handshake->async_in_progress = 0;
3519 }
3520#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3521
Andrzej Kurek25f27152022-08-17 16:09:31 -04003522#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003523#if defined(MBEDTLS_USE_PSA_CRYPTO)
3524 psa_hash_abort( &handshake->fin_sha256_psa );
3525#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003526 mbedtls_sha256_free( &handshake->fin_sha256 );
3527#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003528#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003530#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003531 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003532#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003533 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003534#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003535#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537#if defined(MBEDTLS_DHM_C)
3538 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003539#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003540#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003542#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003543#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003544 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003545#if defined(MBEDTLS_SSL_CLI_C)
3546 mbedtls_free( handshake->ecjpake_cache );
3547 handshake->ecjpake_cache = NULL;
3548 handshake->ecjpake_cache_len = 0;
3549#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003550#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003551
Janos Follath4ae5c292016-02-10 11:27:43 +00003552#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3553 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003554 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003556#endif
3557
Gilles Peskineeccd8882020-03-10 12:19:08 +01003558#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003559#if defined(MBEDTLS_USE_PSA_CRYPTO)
3560 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3561 {
3562 /* The maintenance of the external PSK key slot is the
3563 * user's responsibility. */
3564 if( ssl->handshake->psk_opaque_is_internal )
3565 {
3566 psa_destroy_key( ssl->handshake->psk_opaque );
3567 ssl->handshake->psk_opaque_is_internal = 0;
3568 }
3569 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3570 }
Neil Armstronge952a302022-05-03 10:22:14 +02003571#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003572 if( handshake->psk != NULL )
3573 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003574 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003575 mbedtls_free( handshake->psk );
3576 }
Neil Armstronge952a302022-05-03 10:22:14 +02003577#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003578#endif
3579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3581 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003582 /*
3583 * Free only the linked list wrapper, not the keys themselves
3584 * since the belong to the SNI callback
3585 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003586 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003588
Gilles Peskineeccd8882020-03-10 12:19:08 +01003589#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003590 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003591 if( handshake->ecrs_peer_cert != NULL )
3592 {
3593 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3594 mbedtls_free( handshake->ecrs_peer_cert );
3595 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003596#endif
3597
Hanno Becker75173122019-02-06 16:18:31 +00003598#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3599 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3600 mbedtls_pk_free( &handshake->peer_pubkey );
3601#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3602
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003603#if defined(MBEDTLS_SSL_CLI_C) && \
3604 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3605 mbedtls_free( handshake->cookie );
3606#endif /* MBEDTLS_SSL_CLI_C &&
3607 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003608
3609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003610 mbedtls_ssl_flight_free( handshake->flight );
3611 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003612#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003613
Ronald Cronf12b81d2022-03-15 10:42:41 +01003614#if defined(MBEDTLS_ECDH_C) && \
3615 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003616 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003617 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003618#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3619
Ronald Cron6f135e12021-12-08 16:57:54 +01003620#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003621 mbedtls_ssl_transform_free( handshake->transform_handshake );
3622 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003623 mbedtls_free( handshake->transform_earlydata );
3624 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003625#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003626
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003627
3628#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3629 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3630 * processes datagrams and the fact that a datagram is allowed to have
3631 * several records in it, it is possible that the I/O buffers are not
3632 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003633 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3634 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003635#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003636
Jerry Yuba9c7272021-10-30 11:54:10 +08003637 /* mbedtls_platform_zeroize MUST be last one in this function */
3638 mbedtls_platform_zeroize( handshake,
3639 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003640}
3641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003643{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003644 if( session == NULL )
3645 return;
3646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003648 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003649#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003650
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003651#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003653#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003654
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003655 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003656}
3657
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003658#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003659
3660#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3661#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3662#else
3663#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3664#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3665
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003666#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003667
3668#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3669#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3670#else
3671#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3672#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3673
3674#if defined(MBEDTLS_SSL_ALPN)
3675#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3676#else
3677#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3678#endif /* MBEDTLS_SSL_ALPN */
3679
3680#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3681#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3682#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3683#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3684
3685#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3686 ( (uint32_t) ( \
3687 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3688 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3689 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3690 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3691 0u ) )
3692
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003693static unsigned char ssl_serialized_context_header[] = {
3694 MBEDTLS_VERSION_MAJOR,
3695 MBEDTLS_VERSION_MINOR,
3696 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003697 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3698 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3699 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3700 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3701 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003702};
3703
Paul Bakker5121ce52009-01-03 21:22:43 +00003704/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003705 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003706 *
3707 * The format of the serialized data is:
3708 * (in the presentation language of TLS, RFC 8446 section 3)
3709 *
3710 * // header
3711 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003712 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003713 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003714 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003715 * Note: When updating the format, remember to keep these
3716 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003717 *
3718 * // session sub-structure
3719 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3720 * // transform sub-structure
3721 * uint8 random[64]; // ServerHello.random+ClientHello.random
3722 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3723 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3724 * // fields from ssl_context
3725 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3726 * uint64 in_window_top; // DTLS: last validated record seq_num
3727 * uint64 in_window; // DTLS: bitmask for replay protection
3728 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3729 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3730 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3731 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3732 *
3733 * Note that many fields of the ssl_context or sub-structures are not
3734 * serialized, as they fall in one of the following categories:
3735 *
3736 * 1. forced value (eg in_left must be 0)
3737 * 2. pointer to dynamically-allocated memory (eg session, transform)
3738 * 3. value can be re-derived from other data (eg session keys from MS)
3739 * 4. value was temporary (eg content of input buffer)
3740 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003741 */
3742int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3743 unsigned char *buf,
3744 size_t buf_len,
3745 size_t *olen )
3746{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003747 unsigned char *p = buf;
3748 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003749 size_t session_len;
3750 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003751
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003752 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003753 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3754 * this function's documentation.
3755 *
3756 * These are due to assumptions/limitations in the implementation. Some of
3757 * them are likely to stay (no handshake in progress) some might go away
3758 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003759 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003760 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003761 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003762 {
3763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003765 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003766 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003767 {
3768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003770 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003771 /* Double-check that sub-structures are indeed ready */
3772 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003773 {
3774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003776 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003777 /* There must be no pending incoming or outgoing data */
3778 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003779 {
3780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003782 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003783 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003784 {
3785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003787 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003788 /* Protocol must be DLTS, not TLS */
3789 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003790 {
3791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003793 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003794 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003795 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003796 {
3797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003799 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003800 /* We must be using an AEAD ciphersuite */
3801 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003802 {
3803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003805 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003806 /* Renegotiation must not be enabled */
3807#if defined(MBEDTLS_SSL_RENEGOTIATION)
3808 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003809 {
3810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003812 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003813#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003814
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003815 /*
3816 * Version and format identifier
3817 */
3818 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003819
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003820 if( used <= buf_len )
3821 {
3822 memcpy( p, ssl_serialized_context_header,
3823 sizeof( ssl_serialized_context_header ) );
3824 p += sizeof( ssl_serialized_context_header );
3825 }
3826
3827 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003828 * Session (length + data)
3829 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003830 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003831 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3832 return( ret );
3833
3834 used += 4 + session_len;
3835 if( used <= buf_len )
3836 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003837 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3838 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003839
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003840 ret = ssl_session_save( ssl->session, 1,
3841 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003842 if( ret != 0 )
3843 return( ret );
3844
3845 p += session_len;
3846 }
3847
3848 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003849 * Transform
3850 */
3851 used += sizeof( ssl->transform->randbytes );
3852 if( used <= buf_len )
3853 {
3854 memcpy( p, ssl->transform->randbytes,
3855 sizeof( ssl->transform->randbytes ) );
3856 p += sizeof( ssl->transform->randbytes );
3857 }
3858
3859#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3860 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3861 if( used <= buf_len )
3862 {
3863 *p++ = ssl->transform->in_cid_len;
3864 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3865 p += ssl->transform->in_cid_len;
3866
3867 *p++ = ssl->transform->out_cid_len;
3868 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3869 p += ssl->transform->out_cid_len;
3870 }
3871#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3872
3873 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003874 * Saved fields from top-level ssl_context structure
3875 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003876 used += 4;
3877 if( used <= buf_len )
3878 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003879 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3880 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003881 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003882
3883#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3884 used += 16;
3885 if( used <= buf_len )
3886 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003887 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3888 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003889
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003890 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3891 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003892 }
3893#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3894
3895#if defined(MBEDTLS_SSL_PROTO_DTLS)
3896 used += 1;
3897 if( used <= buf_len )
3898 {
3899 *p++ = ssl->disable_datagram_packing;
3900 }
3901#endif /* MBEDTLS_SSL_PROTO_DTLS */
3902
Jerry Yuae0b2e22021-10-08 15:21:19 +08003903 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003904 if( used <= buf_len )
3905 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003906 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3907 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003908 }
3909
3910#if defined(MBEDTLS_SSL_PROTO_DTLS)
3911 used += 2;
3912 if( used <= buf_len )
3913 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003914 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3915 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003916 }
3917#endif /* MBEDTLS_SSL_PROTO_DTLS */
3918
3919#if defined(MBEDTLS_SSL_ALPN)
3920 {
3921 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003922 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003923 : 0;
3924
3925 used += 1 + alpn_len;
3926 if( used <= buf_len )
3927 {
3928 *p++ = alpn_len;
3929
3930 if( ssl->alpn_chosen != NULL )
3931 {
3932 memcpy( p, ssl->alpn_chosen, alpn_len );
3933 p += alpn_len;
3934 }
3935 }
3936 }
3937#endif /* MBEDTLS_SSL_ALPN */
3938
3939 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003940 * Done
3941 */
3942 *olen = used;
3943
3944 if( used > buf_len )
3945 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003946
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003947 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3948
Hanno Becker43aefe22020-02-05 10:44:56 +00003949 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003950}
3951
3952/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003953 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003954 *
3955 * This internal version is wrapped by a public function that cleans up in
3956 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003957 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003958MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003959static int ssl_context_load( mbedtls_ssl_context *ssl,
3960 const unsigned char *buf,
3961 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003962{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003963 const unsigned char *p = buf;
3964 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003965 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003967
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003968 /*
3969 * The context should have been freshly setup or reset.
3970 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003971 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003972 * renegotiating, or if the user mistakenly loaded a session first.)
3973 */
3974 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3975 ssl->session != NULL )
3976 {
3977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3978 }
3979
3980 /*
3981 * We can't check that the config matches the initial one, but we can at
3982 * least check it matches the requirements for serializing.
3983 */
3984 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003985 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3986 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003987#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003988 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003989#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003990 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003991 {
3992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3993 }
3994
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003995 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3996
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003997 /*
3998 * Check version identifier
3999 */
4000 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4002
4003 if( memcmp( p, ssl_serialized_context_header,
4004 sizeof( ssl_serialized_context_header ) ) != 0 )
4005 {
4006 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4007 }
4008 p += sizeof( ssl_serialized_context_header );
4009
4010 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004011 * Session
4012 */
4013 if( (size_t)( end - p ) < 4 )
4014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4015
4016 session_len = ( (size_t) p[0] << 24 ) |
4017 ( (size_t) p[1] << 16 ) |
4018 ( (size_t) p[2] << 8 ) |
4019 ( (size_t) p[3] );
4020 p += 4;
4021
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004022 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004023 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004024 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004025 ssl->session_in = ssl->session;
4026 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004027 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004028
4029 if( (size_t)( end - p ) < session_len )
4030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4031
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004032 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004033 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004034 {
4035 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004036 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004037 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004038
4039 p += session_len;
4040
4041 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004042 * Transform
4043 */
4044
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004045 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004046 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004047 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004048 ssl->transform_in = ssl->transform;
4049 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004050 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004051
4052 /* Read random bytes and populate structure */
4053 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004056 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004057 ssl->session->ciphersuite,
4058 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004059#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004060 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004061#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004062 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4063 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004064 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004065 ssl->conf->endpoint,
4066 ssl );
4067 if( ret != 0 )
4068 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004069#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004070 p += sizeof( ssl->transform->randbytes );
4071
4072#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4073 /* Read connection IDs and store them */
4074 if( (size_t)( end - p ) < 1 )
4075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4076
4077 ssl->transform->in_cid_len = *p++;
4078
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004079 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4081
4082 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4083 p += ssl->transform->in_cid_len;
4084
4085 ssl->transform->out_cid_len = *p++;
4086
4087 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4089
4090 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4091 p += ssl->transform->out_cid_len;
4092#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4093
4094 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004095 * Saved fields from top-level ssl_context structure
4096 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004097 if( (size_t)( end - p ) < 4 )
4098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4099
4100 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4101 ( (uint32_t) p[1] << 16 ) |
4102 ( (uint32_t) p[2] << 8 ) |
4103 ( (uint32_t) p[3] );
4104 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004105
4106#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4107 if( (size_t)( end - p ) < 16 )
4108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4109
4110 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4111 ( (uint64_t) p[1] << 48 ) |
4112 ( (uint64_t) p[2] << 40 ) |
4113 ( (uint64_t) p[3] << 32 ) |
4114 ( (uint64_t) p[4] << 24 ) |
4115 ( (uint64_t) p[5] << 16 ) |
4116 ( (uint64_t) p[6] << 8 ) |
4117 ( (uint64_t) p[7] );
4118 p += 8;
4119
4120 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4121 ( (uint64_t) p[1] << 48 ) |
4122 ( (uint64_t) p[2] << 40 ) |
4123 ( (uint64_t) p[3] << 32 ) |
4124 ( (uint64_t) p[4] << 24 ) |
4125 ( (uint64_t) p[5] << 16 ) |
4126 ( (uint64_t) p[6] << 8 ) |
4127 ( (uint64_t) p[7] );
4128 p += 8;
4129#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4130
4131#if defined(MBEDTLS_SSL_PROTO_DTLS)
4132 if( (size_t)( end - p ) < 1 )
4133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4134
4135 ssl->disable_datagram_packing = *p++;
4136#endif /* MBEDTLS_SSL_PROTO_DTLS */
4137
Jerry Yud9a94fe2021-09-28 18:58:59 +08004138 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004140 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4141 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004142
4143#if defined(MBEDTLS_SSL_PROTO_DTLS)
4144 if( (size_t)( end - p ) < 2 )
4145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4146
4147 ssl->mtu = ( p[0] << 8 ) | p[1];
4148 p += 2;
4149#endif /* MBEDTLS_SSL_PROTO_DTLS */
4150
4151#if defined(MBEDTLS_SSL_ALPN)
4152 {
4153 uint8_t alpn_len;
4154 const char **cur;
4155
4156 if( (size_t)( end - p ) < 1 )
4157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4158
4159 alpn_len = *p++;
4160
4161 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4162 {
4163 /* alpn_chosen should point to an item in the configured list */
4164 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4165 {
4166 if( strlen( *cur ) == alpn_len &&
4167 memcmp( p, cur, alpn_len ) == 0 )
4168 {
4169 ssl->alpn_chosen = *cur;
4170 break;
4171 }
4172 }
4173 }
4174
4175 /* can only happen on conf mismatch */
4176 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4178
4179 p += alpn_len;
4180 }
4181#endif /* MBEDTLS_SSL_ALPN */
4182
4183 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004184 * Forced fields from top-level ssl_context structure
4185 *
4186 * Most of them already set to the correct value by mbedtls_ssl_init() and
4187 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4188 */
4189 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004190 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004191
Hanno Becker361b10d2019-08-30 10:42:49 +01004192 /* Adjust pointers for header fields of outgoing records to
4193 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004194 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004195
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004196#if defined(MBEDTLS_SSL_PROTO_DTLS)
4197 ssl->in_epoch = 1;
4198#endif
4199
4200 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4201 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004202 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4203 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004204 if( ssl->handshake != NULL )
4205 {
4206 mbedtls_ssl_handshake_free( ssl );
4207 mbedtls_free( ssl->handshake );
4208 ssl->handshake = NULL;
4209 }
4210
4211 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004212 * Done - should have consumed entire buffer
4213 */
4214 if( p != end )
4215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004216
4217 return( 0 );
4218}
4219
4220/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004221 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004222 */
4223int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4224 const unsigned char *buf,
4225 size_t len )
4226{
4227 int ret = ssl_context_load( context, buf, len );
4228
4229 if( ret != 0 )
4230 mbedtls_ssl_free( context );
4231
4232 return( ret );
4233}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004234#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004235
4236/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004237 * Free an SSL context
4238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004240{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004241 if( ssl == NULL )
4242 return;
4243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004245
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004246 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 {
sander-visserb8aa2072020-05-06 22:05:13 +02004248#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4249 size_t out_buf_len = ssl->out_buf_len;
4250#else
4251 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4252#endif
4253
Darryl Greenb33cc762019-11-28 14:29:44 +00004254 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004256 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 }
4258
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004259 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 {
sander-visserb8aa2072020-05-06 22:05:13 +02004261#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4262 size_t in_buf_len = ssl->in_buf_len;
4263#else
4264 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4265#endif
4266
Darryl Greenb33cc762019-11-28 14:29:44 +00004267 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004269 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 }
4271
Paul Bakker48916f92012-09-16 19:57:18 +00004272 if( ssl->transform )
4273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 mbedtls_ssl_transform_free( ssl->transform );
4275 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004276 }
4277
4278 if( ssl->handshake )
4279 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004280 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4282 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 mbedtls_free( ssl->handshake );
4285 mbedtls_free( ssl->transform_negotiate );
4286 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004287 }
4288
Ronald Cron6f135e12021-12-08 16:57:54 +01004289#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004290 mbedtls_ssl_transform_free( ssl->transform_application );
4291 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004292#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004293
Paul Bakkerc0463502013-02-14 11:19:38 +01004294 if( ssl->session )
4295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296 mbedtls_ssl_session_free( ssl->session );
4297 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004298 }
4299
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004300#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004301 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004302 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004303 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004305 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004306#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004307
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004308#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004310#endif
4311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004313
Paul Bakker86f04f42013-02-14 11:20:09 +01004314 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004315 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004316}
4317
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004318/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004319 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004320 */
4321void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4322{
4323 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4324}
4325
Gilles Peskineae270bf2021-06-02 00:05:29 +02004326/* The selection should be the same as mbedtls_x509_crt_profile_default in
4327 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004328 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004329 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004330 * about this list.
4331 */
Brett Warrene0edc842021-08-17 09:53:13 +01004332static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004333#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004334 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004335#endif
4336#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004337 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004338#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004339#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004340 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004341#endif
4342#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004343 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004344#endif
4345#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004346 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004347#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004348#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004349 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004350#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004351#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004352 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004353#endif
4354#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004355 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004356#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004357 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004358};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004359
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004360static int ssl_preset_suiteb_ciphersuites[] = {
4361 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4362 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4363 0
4364};
4365
Gilles Peskineeccd8882020-03-10 12:19:08 +01004366#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004367
Jerry Yu909df7b2022-01-22 11:56:27 +08004368/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004369 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004370 * rules SHOULD be upheld.
4371 * - No duplicate entries.
4372 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004373 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004374 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004375 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004376static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004377
Andrzej Kurek25f27152022-08-17 16:09:31 -04004378#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 +08004379 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4380 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004381#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004382 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004383
Andrzej Kurek25f27152022-08-17 16:09:31 -04004384#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 +08004385 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4386 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004387#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004388 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4389
Andrzej Kurek25f27152022-08-17 16:09:31 -04004390#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 +08004391 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4392 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004393#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004394 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004395
Andrzej Kurek25f27152022-08-17 16:09:31 -04004396#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 +08004397 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004398#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 +08004399
Andrzej Kurek25f27152022-08-17 16:09:31 -04004400#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 +08004401 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004402#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 +08004403
Andrzej Kurek25f27152022-08-17 16:09:31 -04004404#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 +08004405 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004406#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 +08004407
Andrzej Kurek25f27152022-08-17 16:09:31 -04004408#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 +08004409 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4410#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4411
Andrzej Kurek25f27152022-08-17 16:09:31 -04004412#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 +08004413 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4414#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4415
Andrzej Kurek25f27152022-08-17 16:09:31 -04004416#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 +08004417 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4418#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4419
Gabor Mezei15b95a62022-05-09 16:37:58 +02004420 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004421};
4422
4423/* NOTICE: see above */
4424#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4425static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004426#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004427#if defined(MBEDTLS_ECDSA_C)
4428 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004429#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004430#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4431 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4432#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004433#if defined(MBEDTLS_RSA_C)
4434 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4435#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004436#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004437#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004438#if defined(MBEDTLS_ECDSA_C)
4439 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004440#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004441#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4442 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4443#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004444#if defined(MBEDTLS_RSA_C)
4445 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4446#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004447#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004448#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004449#if defined(MBEDTLS_ECDSA_C)
4450 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004451#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004452#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4453 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4454#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004455#if defined(MBEDTLS_RSA_C)
4456 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4457#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004458#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004459 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004460};
4461#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4462/* NOTICE: see above */
4463static uint16_t ssl_preset_suiteb_sig_algs[] = {
4464
Andrzej Kurek25f27152022-08-17 16:09:31 -04004465#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 +08004466 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4467 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004468#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004469 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4470
Andrzej Kurek25f27152022-08-17 16:09:31 -04004471#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 +08004472 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4473 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004474#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004475 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004476
Andrzej Kurek25f27152022-08-17 16:09:31 -04004477#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 +08004478 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004479#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 +08004480
Andrzej Kurek25f27152022-08-17 16:09:31 -04004481#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 +08004482 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004483#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004484
Gabor Mezei15b95a62022-05-09 16:37:58 +02004485 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004486};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004487
Jerry Yu909df7b2022-01-22 11:56:27 +08004488/* NOTICE: see above */
4489#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4490static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004491#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004492#if defined(MBEDTLS_ECDSA_C)
4493 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004494#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004495#if defined(MBEDTLS_RSA_C)
4496 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4497#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004498#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004499#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004500#if defined(MBEDTLS_ECDSA_C)
4501 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004502#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004503#if defined(MBEDTLS_RSA_C)
4504 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4505#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004506#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004507 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004508};
Jerry Yu909df7b2022-01-22 11:56:27 +08004509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4510
Jerry Yu1a8b4812022-01-20 17:56:50 +08004511#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004512
Brett Warrene0edc842021-08-17 09:53:13 +01004513static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004514#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004515 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004516#endif
4517#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004518 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004519#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004520 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004521};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004522
Jerry Yu1a8b4812022-01-20 17:56:50 +08004523#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004524/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004525 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004526MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004527static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004528{
4529 size_t i, j;
4530 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004531
Gabor Mezei15b95a62022-05-09 16:37:58 +02004532 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004533 {
Jerry Yuf377d642022-01-25 10:43:59 +08004534 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004535 {
Jerry Yuf377d642022-01-25 10:43:59 +08004536 if( sig_algs[i] != sig_algs[j] )
4537 continue;
4538 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4539 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4540 sig_algs[i], j, i );
4541 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004542 }
4543 }
4544 return( ret );
4545}
4546
4547#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4548
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004549/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004550 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004551 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004552int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004553 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004554{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004555#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004557#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004558
Jerry Yu1a8b4812022-01-20 17:56:50 +08004559#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004560 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004561 {
4562 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4563 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4564 }
4565
Jerry Yuf377d642022-01-25 10:43:59 +08004566 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004567 {
4568 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4569 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4570 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004571
4572#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004573 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004574 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004575 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004576 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4577 }
4578
Jerry Yuf377d642022-01-25 10:43:59 +08004579 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004580 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004581 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004582 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4583 }
4584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004585#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4586
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004587 /* Use the functions here so that they are covered in tests,
4588 * but otherwise access member directly for efficiency */
4589 mbedtls_ssl_conf_endpoint( conf, endpoint );
4590 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004591
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004592 /*
4593 * Things that are common to all presets
4594 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004595#if defined(MBEDTLS_SSL_CLI_C)
4596 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4597 {
4598 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4599#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4600 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4601#endif
4602 }
4603#endif
4604
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004605#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4606 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4607#endif
4608
4609#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4610 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4611#endif
4612
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004613#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004614 conf->f_cookie_write = ssl_cookie_write_dummy;
4615 conf->f_cookie_check = ssl_cookie_check_dummy;
4616#endif
4617
4618#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4619 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4620#endif
4621
Janos Follath088ce432017-04-10 12:42:31 +01004622#if defined(MBEDTLS_SSL_SRV_C)
4623 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004624 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004625#endif
4626
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004627#if defined(MBEDTLS_SSL_PROTO_DTLS)
4628 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4629 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4630#endif
4631
4632#if defined(MBEDTLS_SSL_RENEGOTIATION)
4633 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004634 memset( conf->renego_period, 0x00, 2 );
4635 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004636#endif
4637
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004638#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004639 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4640 {
4641 const unsigned char dhm_p[] =
4642 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4643 const unsigned char dhm_g[] =
4644 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004645
Hanno Beckere2defad2021-07-24 05:59:17 +01004646 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4647 dhm_p, sizeof( dhm_p ),
4648 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4649 {
4650 return( ret );
4651 }
4652 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004653#endif
4654
Ronald Cron6f135e12021-12-08 16:57:54 +01004655#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004656#if defined(MBEDTLS_SSL_SRV_C)
4657 mbedtls_ssl_conf_new_session_tickets(
4658 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4659#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004660 /*
4661 * Allow all TLS 1.3 key exchange modes by default.
4662 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004663 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004664#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004665
XiaokangQian4d3a6042022-04-21 13:46:17 +00004666 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4667 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004668#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004669 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004670 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004671#else
XiaokangQian060d8672022-04-21 09:24:56 +00004672 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004673#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004674 }
4675 else
4676 {
4677#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4678 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4679 {
4680 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4681 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4682 }
4683 else
4684 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4685 {
4686 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4687 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4688 }
4689#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4690 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4691 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4692#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4693 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4694 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4695#else
4696 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4697#endif
4698 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004699
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004700 /*
4701 * Preset-specific defaults
4702 */
4703 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004704 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004705 /*
4706 * NSA Suite B
4707 */
4708 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004709
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004710 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004711
4712#if defined(MBEDTLS_X509_CRT_PARSE_C)
4713 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004714#endif
4715
Gilles Peskineeccd8882020-03-10 12:19:08 +01004716#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004717#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4718 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4719 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4720 else
4721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4722 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004723#endif
4724
Brett Warrene0edc842021-08-17 09:53:13 +01004725#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4726 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004727#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004728 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004729 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004730
4731 /*
4732 * Default
4733 */
4734 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004735
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004736 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004737
4738#if defined(MBEDTLS_X509_CRT_PARSE_C)
4739 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4740#endif
4741
Gilles Peskineeccd8882020-03-10 12:19:08 +01004742#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004743#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4744 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4745 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4746 else
4747#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4748 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004749#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004750
Brett Warrene0edc842021-08-17 09:53:13 +01004751#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4752 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004753#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004754 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004755
4756#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4757 conf->dhm_min_bitlen = 1024;
4758#endif
4759 }
4760
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004761 return( 0 );
4762}
4763
4764/*
4765 * Free mbedtls_ssl_config
4766 */
4767void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4768{
4769#if defined(MBEDTLS_DHM_C)
4770 mbedtls_mpi_free( &conf->dhm_P );
4771 mbedtls_mpi_free( &conf->dhm_G );
4772#endif
4773
Gilles Peskineeccd8882020-03-10 12:19:08 +01004774#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004775#if defined(MBEDTLS_USE_PSA_CRYPTO)
4776 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4777 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004778 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4779 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004780#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004781 if( conf->psk != NULL )
4782 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004783 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004784 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004785 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004786 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004787 }
4788
4789 if( conf->psk_identity != NULL )
4790 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004791 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004792 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004793 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004794 conf->psk_identity_len = 0;
4795 }
4796#endif
4797
4798#if defined(MBEDTLS_X509_CRT_PARSE_C)
4799 ssl_key_cert_free( conf->key_cert );
4800#endif
4801
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004802 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004803}
4804
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004805#if defined(MBEDTLS_PK_C) && \
4806 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004807/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#if defined(MBEDTLS_RSA_C)
4813 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4814 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004815#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#if defined(MBEDTLS_ECDSA_C)
4817 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4818 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004821}
4822
Hanno Becker7e5437a2017-04-28 17:15:26 +01004823unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4824{
4825 switch( type ) {
4826 case MBEDTLS_PK_RSA:
4827 return( MBEDTLS_SSL_SIG_RSA );
4828 case MBEDTLS_PK_ECDSA:
4829 case MBEDTLS_PK_ECKEY:
4830 return( MBEDTLS_SSL_SIG_ECDSA );
4831 default:
4832 return( MBEDTLS_SSL_SIG_ANON );
4833 }
4834}
4835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004837{
4838 switch( sig )
4839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840#if defined(MBEDTLS_RSA_C)
4841 case MBEDTLS_SSL_SIG_RSA:
4842 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004843#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844#if defined(MBEDTLS_ECDSA_C)
4845 case MBEDTLS_SSL_SIG_ECDSA:
4846 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004847#endif
4848 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004850 }
4851}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004852#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004853
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004854/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004855 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004858{
4859 switch( hash )
4860 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004861#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 case MBEDTLS_SSL_HASH_MD5:
4863 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004864#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004865#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 case MBEDTLS_SSL_HASH_SHA1:
4867 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004868#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004869#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 case MBEDTLS_SSL_HASH_SHA224:
4871 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004872#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004873#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874 case MBEDTLS_SSL_HASH_SHA256:
4875 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004876#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004877#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 case MBEDTLS_SSL_HASH_SHA384:
4879 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004880#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004881#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882 case MBEDTLS_SSL_HASH_SHA512:
4883 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004884#endif
4885 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004887 }
4888}
4889
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004890/*
4891 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4892 */
4893unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4894{
4895 switch( md )
4896 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004897#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004898 case MBEDTLS_MD_MD5:
4899 return( MBEDTLS_SSL_HASH_MD5 );
4900#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004901#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004902 case MBEDTLS_MD_SHA1:
4903 return( MBEDTLS_SSL_HASH_SHA1 );
4904#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004905#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004906 case MBEDTLS_MD_SHA224:
4907 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004908#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004909#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004910 case MBEDTLS_MD_SHA256:
4911 return( MBEDTLS_SSL_HASH_SHA256 );
4912#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004913#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004914 case MBEDTLS_MD_SHA384:
4915 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004916#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004917#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004918 case MBEDTLS_MD_SHA512:
4919 return( MBEDTLS_SSL_HASH_SHA512 );
4920#endif
4921 default:
4922 return( MBEDTLS_SSL_HASH_NONE );
4923 }
4924}
4925
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004926/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004927 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004928 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004929 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004930int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004931{
Brett Warrene0edc842021-08-17 09:53:13 +01004932 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004933
Brett Warrene0edc842021-08-17 09:53:13 +01004934 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004935 return( -1 );
4936
Brett Warrene0edc842021-08-17 09:53:13 +01004937 for( ; *group_list != 0; group_list++ )
4938 {
4939 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004940 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004941 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004942
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004943 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004944}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004945
4946#if defined(MBEDTLS_ECP_C)
4947/*
4948 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4949 */
4950int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4951{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004952 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004953 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004954
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004955 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004956 return -1;
4957
4958 uint16_t tls_id = grp_info->tls_id;
4959
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004960 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4961}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004962#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964#if defined(MBEDTLS_X509_CRT_PARSE_C)
4965int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4966 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004967 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004968 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004969{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004970 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004971 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004972 const char *ext_oid;
4973 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004976 {
4977 /* Server part of the key exchange */
4978 switch( ciphersuite->key_exchange )
4979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 case MBEDTLS_KEY_EXCHANGE_RSA:
4981 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004982 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004983 break;
4984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4986 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4987 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4988 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004989 break;
4990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4992 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004993 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004994 break;
4995
4996 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997 case MBEDTLS_KEY_EXCHANGE_NONE:
4998 case MBEDTLS_KEY_EXCHANGE_PSK:
4999 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5000 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005001 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005002 usage = 0;
5003 }
5004 }
5005 else
5006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5008 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005009 }
5010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005012 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005013 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005014 ret = -1;
5015 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5020 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005021 }
5022 else
5023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5025 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005026 }
5027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005029 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005030 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005031 ret = -1;
5032 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005033
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005034 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005037
Jerry Yu148165c2021-09-24 23:20:59 +08005038#if defined(MBEDTLS_USE_PSA_CRYPTO)
5039int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5040 const mbedtls_md_type_t md,
5041 unsigned char *dst,
5042 size_t dst_len,
5043 size_t *olen )
5044{
Ronald Cronf6893e12022-01-07 22:09:01 +01005045 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5046 psa_hash_operation_t *hash_operation_to_clone;
5047 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5048
Jerry Yu148165c2021-09-24 23:20:59 +08005049 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005050
5051 switch( md )
5052 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005053#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005054 case MBEDTLS_MD_SHA384:
5055 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5056 break;
5057#endif
5058
Andrzej Kurek25f27152022-08-17 16:09:31 -04005059#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005060 case MBEDTLS_MD_SHA256:
5061 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5062 break;
5063#endif
5064
5065 default:
5066 goto exit;
5067 }
5068
5069 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5070 if( status != PSA_SUCCESS )
5071 goto exit;
5072
5073 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5074 if( status != PSA_SUCCESS )
5075 goto exit;
5076
5077exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005078 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005079}
5080#else /* MBEDTLS_USE_PSA_CRYPTO */
5081
Andrzej Kurek25f27152022-08-17 16:09:31 -04005082#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005083MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005084static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5085 unsigned char *dst,
5086 size_t dst_len,
5087 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005088{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005089 int ret;
5090 mbedtls_sha512_context sha512;
5091
5092 if( dst_len < 48 )
5093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5094
5095 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005096 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005097
5098 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5099 {
5100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5101 goto exit;
5102 }
5103
5104 *olen = 48;
5105
5106exit:
5107
5108 mbedtls_sha512_free( &sha512 );
5109 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005110}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005111#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005112
Andrzej Kurek25f27152022-08-17 16:09:31 -04005113#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005114MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005115static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5116 unsigned char *dst,
5117 size_t dst_len,
5118 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005119{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005120 int ret;
5121 mbedtls_sha256_context sha256;
5122
5123 if( dst_len < 32 )
5124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5125
5126 mbedtls_sha256_init( &sha256 );
5127 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005128
Jerry Yu24c0ec32021-09-09 14:21:07 +08005129 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5130 {
5131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5132 goto exit;
5133 }
5134
5135 *olen = 32;
5136
5137exit:
5138
5139 mbedtls_sha256_free( &sha256 );
5140 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005141}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005142#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005143
Jerry Yu000f9762021-09-14 11:12:51 +08005144int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5145 const mbedtls_md_type_t md,
5146 unsigned char *dst,
5147 size_t dst_len,
5148 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005149{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005150 switch( md )
5151 {
5152
Andrzej Kurek25f27152022-08-17 16:09:31 -04005153#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005154 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005155 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005156#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005157
Andrzej Kurek25f27152022-08-17 16:09:31 -04005158#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005159 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005160 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005161#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005162
5163 default:
5164 break;
5165 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005167}
XiaokangQian647719a2021-12-07 09:16:29 +00005168
Jerry Yu148165c2021-09-24 23:20:59 +08005169#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005170
Gabor Mezei078e8032022-04-27 21:17:56 +02005171#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5172/* mbedtls_ssl_parse_sig_alg_ext()
5173 *
5174 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5175 * value (TLS 1.3 RFC8446):
5176 * enum {
5177 * ....
5178 * ecdsa_secp256r1_sha256( 0x0403 ),
5179 * ecdsa_secp384r1_sha384( 0x0503 ),
5180 * ecdsa_secp521r1_sha512( 0x0603 ),
5181 * ....
5182 * } SignatureScheme;
5183 *
5184 * struct {
5185 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5186 * } SignatureSchemeList;
5187 *
5188 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5189 * value (TLS 1.2 RFC5246):
5190 * enum {
5191 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5192 * sha512(6), (255)
5193 * } HashAlgorithm;
5194 *
5195 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5196 * SignatureAlgorithm;
5197 *
5198 * struct {
5199 * HashAlgorithm hash;
5200 * SignatureAlgorithm signature;
5201 * } SignatureAndHashAlgorithm;
5202 *
5203 * SignatureAndHashAlgorithm
5204 * supported_signature_algorithms<2..2^16-2>;
5205 *
5206 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5207 * generalization of the TLS 1.2 signature algorithm extension.
5208 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5209 * `SignatureScheme` field of TLS 1.3
5210 *
5211 */
5212int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5213 const unsigned char *buf,
5214 const unsigned char *end )
5215{
5216 const unsigned char *p = buf;
5217 size_t supported_sig_algs_len = 0;
5218 const unsigned char *supported_sig_algs_end;
5219 uint16_t sig_alg;
5220 uint32_t common_idx = 0;
5221
5222 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5223 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5224 p += 2;
5225
5226 memset( ssl->handshake->received_sig_algs, 0,
5227 sizeof(ssl->handshake->received_sig_algs) );
5228
5229 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5230 supported_sig_algs_end = p + supported_sig_algs_len;
5231 while( p < supported_sig_algs_end )
5232 {
5233 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5234 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5235 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005236 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5237 sig_alg,
5238 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005239#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005240 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005241 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5242 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5243 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005244 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005245 }
5246#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005247
Jerry Yuf3b46b52022-06-19 16:52:27 +08005248 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5249 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005250
5251 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5252 {
5253 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5254 common_idx += 1;
5255 }
5256 }
5257 /* Check that we consumed all the message. */
5258 if( p != end )
5259 {
5260 MBEDTLS_SSL_DEBUG_MSG( 1,
5261 ( "Signature algorithms extension length misaligned" ) );
5262 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5263 MBEDTLS_ERR_SSL_DECODE_ERROR );
5264 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5265 }
5266
5267 if( common_idx == 0 )
5268 {
5269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5270 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5271 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5272 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5273 }
5274
Gabor Mezei15b95a62022-05-09 16:37:58 +02005275 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005276 return( 0 );
5277}
5278
5279#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5280
Jerry Yudc7bd172022-02-17 13:44:15 +08005281#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5282
5283#if defined(MBEDTLS_USE_PSA_CRYPTO)
5284
5285static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5286 mbedtls_svc_key_id_t key,
5287 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005288 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005289 const unsigned char* seed, size_t seed_length,
5290 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005291 const unsigned char* other_secret,
5292 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005293 size_t capacity )
5294{
5295 psa_status_t status;
5296
5297 status = psa_key_derivation_setup( derivation, alg );
5298 if( status != PSA_SUCCESS )
5299 return( status );
5300
5301 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5302 {
5303 status = psa_key_derivation_input_bytes( derivation,
5304 PSA_KEY_DERIVATION_INPUT_SEED,
5305 seed, seed_length );
5306 if( status != PSA_SUCCESS )
5307 return( status );
5308
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005309 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005310 {
5311 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005312 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5313 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005314 if( status != PSA_SUCCESS )
5315 return( status );
5316 }
5317
Jerry Yudc7bd172022-02-17 13:44:15 +08005318 if( mbedtls_svc_key_id_is_null( key ) )
5319 {
5320 status = psa_key_derivation_input_bytes(
5321 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005322 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005323 }
5324 else
5325 {
5326 status = psa_key_derivation_input_key(
5327 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5328 }
5329 if( status != PSA_SUCCESS )
5330 return( status );
5331
5332 status = psa_key_derivation_input_bytes( derivation,
5333 PSA_KEY_DERIVATION_INPUT_LABEL,
5334 label, label_length );
5335 if( status != PSA_SUCCESS )
5336 return( status );
5337 }
5338 else
5339 {
5340 return( PSA_ERROR_NOT_SUPPORTED );
5341 }
5342
5343 status = psa_key_derivation_set_capacity( derivation, capacity );
5344 if( status != PSA_SUCCESS )
5345 return( status );
5346
5347 return( PSA_SUCCESS );
5348}
5349
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005350MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005351static int tls_prf_generic( mbedtls_md_type_t md_type,
5352 const unsigned char *secret, size_t slen,
5353 const char *label,
5354 const unsigned char *random, size_t rlen,
5355 unsigned char *dstbuf, size_t dlen )
5356{
5357 psa_status_t status;
5358 psa_algorithm_t alg;
5359 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5360 psa_key_derivation_operation_t derivation =
5361 PSA_KEY_DERIVATION_OPERATION_INIT;
5362
5363 if( md_type == MBEDTLS_MD_SHA384 )
5364 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5365 else
5366 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5367
5368 /* Normally a "secret" should be long enough to be impossible to
5369 * find by brute force, and in particular should not be empty. But
5370 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5371 * and for this use case it makes sense to have a 0-length "secret".
5372 * Since the key API doesn't allow importing a key of length 0,
5373 * keep master_key=0, which setup_psa_key_derivation() understands
5374 * to mean a 0-length "secret" input. */
5375 if( slen != 0 )
5376 {
5377 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5378 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5379 psa_set_key_algorithm( &key_attributes, alg );
5380 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5381
5382 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5383 if( status != PSA_SUCCESS )
5384 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5385 }
5386
5387 status = setup_psa_key_derivation( &derivation,
5388 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005389 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005390 random, rlen,
5391 (unsigned char const *) label,
5392 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005393 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005394 dlen );
5395 if( status != PSA_SUCCESS )
5396 {
5397 psa_key_derivation_abort( &derivation );
5398 psa_destroy_key( master_key );
5399 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5400 }
5401
5402 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5403 if( status != PSA_SUCCESS )
5404 {
5405 psa_key_derivation_abort( &derivation );
5406 psa_destroy_key( master_key );
5407 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5408 }
5409
5410 status = psa_key_derivation_abort( &derivation );
5411 if( status != PSA_SUCCESS )
5412 {
5413 psa_destroy_key( master_key );
5414 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5415 }
5416
5417 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5418 status = psa_destroy_key( master_key );
5419 if( status != PSA_SUCCESS )
5420 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5421
5422 return( 0 );
5423}
5424
5425#else /* MBEDTLS_USE_PSA_CRYPTO */
5426
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005427MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005428static int tls_prf_generic( mbedtls_md_type_t md_type,
5429 const unsigned char *secret, size_t slen,
5430 const char *label,
5431 const unsigned char *random, size_t rlen,
5432 unsigned char *dstbuf, size_t dlen )
5433{
5434 size_t nb;
5435 size_t i, j, k, md_len;
5436 unsigned char *tmp;
5437 size_t tmp_len = 0;
5438 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5439 const mbedtls_md_info_t *md_info;
5440 mbedtls_md_context_t md_ctx;
5441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5442
5443 mbedtls_md_init( &md_ctx );
5444
5445 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5447
5448 md_len = mbedtls_md_get_size( md_info );
5449
5450 tmp_len = md_len + strlen( label ) + rlen;
5451 tmp = mbedtls_calloc( 1, tmp_len );
5452 if( tmp == NULL )
5453 {
5454 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5455 goto exit;
5456 }
5457
5458 nb = strlen( label );
5459 memcpy( tmp + md_len, label, nb );
5460 memcpy( tmp + md_len + nb, random, rlen );
5461 nb += rlen;
5462
5463 /*
5464 * Compute P_<hash>(secret, label + random)[0..dlen]
5465 */
5466 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5467 goto exit;
5468
5469 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5470 if( ret != 0 )
5471 goto exit;
5472 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5473 if( ret != 0 )
5474 goto exit;
5475 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5476 if( ret != 0 )
5477 goto exit;
5478
5479 for( i = 0; i < dlen; i += md_len )
5480 {
5481 ret = mbedtls_md_hmac_reset ( &md_ctx );
5482 if( ret != 0 )
5483 goto exit;
5484 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5485 if( ret != 0 )
5486 goto exit;
5487 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5488 if( ret != 0 )
5489 goto exit;
5490
5491 ret = mbedtls_md_hmac_reset ( &md_ctx );
5492 if( ret != 0 )
5493 goto exit;
5494 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5495 if( ret != 0 )
5496 goto exit;
5497 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5498 if( ret != 0 )
5499 goto exit;
5500
5501 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5502
5503 for( j = 0; j < k; j++ )
5504 dstbuf[i + j] = h_i[j];
5505 }
5506
5507exit:
5508 mbedtls_md_free( &md_ctx );
5509
5510 mbedtls_platform_zeroize( tmp, tmp_len );
5511 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5512
5513 mbedtls_free( tmp );
5514
5515 return( ret );
5516}
5517#endif /* MBEDTLS_USE_PSA_CRYPTO */
5518
Andrzej Kurek25f27152022-08-17 16:09:31 -04005519#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005520MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005521static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5522 const char *label,
5523 const unsigned char *random, size_t rlen,
5524 unsigned char *dstbuf, size_t dlen )
5525{
5526 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5527 label, random, rlen, dstbuf, dlen ) );
5528}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005529#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005530
Andrzej Kurek25f27152022-08-17 16:09:31 -04005531#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005532MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005533static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5534 const char *label,
5535 const unsigned char *random, size_t rlen,
5536 unsigned char *dstbuf, size_t dlen )
5537{
5538 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5539 label, random, rlen, dstbuf, dlen ) );
5540}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005541#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005542
Jerry Yuf009d862022-02-17 14:01:37 +08005543/*
5544 * Set appropriate PRF function and other SSL / TLS1.2 functions
5545 *
5546 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005547 * - hash associated with the ciphersuite (only used by TLS 1.2)
5548 *
5549 * Outputs:
5550 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5551 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005552MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005553static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005554 mbedtls_md_type_t hash )
5555{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005556#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005557 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005558 {
5559 handshake->tls_prf = tls_prf_sha384;
5560 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5561 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5562 }
5563 else
5564#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005565#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005566 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005567 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005568 handshake->tls_prf = tls_prf_sha256;
5569 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5570 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5571 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005572#else
Jerry Yuf009d862022-02-17 14:01:37 +08005573 {
Jerry Yuf009d862022-02-17 14:01:37 +08005574 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005575 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5577 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005578#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005579
5580 return( 0 );
5581}
Jerry Yud6ab2352022-02-17 14:03:43 +08005582
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005583/*
5584 * Compute master secret if needed
5585 *
5586 * Parameters:
5587 * [in/out] handshake
5588 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5589 * (PSA-PSK) ciphersuite_info, psk_opaque
5590 * [out] premaster (cleared)
5591 * [out] master
5592 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5593 * debug: conf->f_dbg, conf->p_dbg
5594 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005595 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005596 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005597MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005598static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5599 unsigned char *master,
5600 const mbedtls_ssl_context *ssl )
5601{
5602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5603
5604 /* cf. RFC 5246, Section 8.1:
5605 * "The master secret is always exactly 48 bytes in length." */
5606 size_t const master_secret_len = 48;
5607
5608#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5609 unsigned char session_hash[48];
5610#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5611
5612 /* The label for the KDF used for key expansion.
5613 * This is either "master secret" or "extended master secret"
5614 * depending on whether the Extended Master Secret extension
5615 * is used. */
5616 char const *lbl = "master secret";
5617
Przemek Stekielae4ed302022-04-05 17:15:55 +02005618 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005619 * - If the Extended Master Secret extension is not used,
5620 * this is ClientHello.Random + ServerHello.Random
5621 * (see Sect. 8.1 in RFC 5246).
5622 * - If the Extended Master Secret extension is used,
5623 * this is the transcript of the handshake so far.
5624 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005625 unsigned char const *seed = handshake->randbytes;
5626 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005627
5628#if !defined(MBEDTLS_DEBUG_C) && \
5629 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5630 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5631 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5632 ssl = NULL; /* make sure we don't use it except for those cases */
5633 (void) ssl;
5634#endif
5635
5636 if( handshake->resume != 0 )
5637 {
5638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5639 return( 0 );
5640 }
5641
5642#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5643 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5644 {
5645 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005646 seed = session_hash;
5647 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005648
5649 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005650 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005651 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005652#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005653
Przemek Stekiel99114f32022-04-22 11:20:09 +02005654#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005655 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005656 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005657 {
5658 /* Perform PSK-to-MS expansion in a single step. */
5659 psa_status_t status;
5660 psa_algorithm_t alg;
5661 mbedtls_svc_key_id_t psk;
5662 psa_key_derivation_operation_t derivation =
5663 PSA_KEY_DERIVATION_OPERATION_INIT;
5664 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5665
5666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5667
5668 psk = mbedtls_ssl_get_opaque_psk( ssl );
5669
5670 if( hash_alg == MBEDTLS_MD_SHA384 )
5671 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5672 else
5673 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5674
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005675 size_t other_secret_len = 0;
5676 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005677
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005678 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005679 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005680 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005681 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005682 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005683 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005684 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005685 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005686 other_secret_len = 48;
5687 other_secret = handshake->premaster + 2;
5688 break;
5689 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005690 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005691 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5692 other_secret = handshake->premaster + 2;
5693 break;
5694 default:
5695 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005696 }
5697
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005698 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005699 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005700 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005701 (unsigned char const *) lbl,
5702 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005703 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005704 master_secret_len );
5705 if( status != PSA_SUCCESS )
5706 {
5707 psa_key_derivation_abort( &derivation );
5708 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5709 }
5710
5711 status = psa_key_derivation_output_bytes( &derivation,
5712 master,
5713 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_abort( &derivation );
5721 if( status != PSA_SUCCESS )
5722 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5723 }
5724 else
5725#endif
5726 {
5727 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005728 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005729 master,
5730 master_secret_len );
5731 if( ret != 0 )
5732 {
5733 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5734 return( ret );
5735 }
5736
5737 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5738 handshake->premaster,
5739 handshake->pmslen );
5740
5741 mbedtls_platform_zeroize( handshake->premaster,
5742 sizeof(handshake->premaster) );
5743 }
5744
5745 return( 0 );
5746}
5747
Jerry Yud62f87e2022-02-17 14:09:02 +08005748int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5749{
5750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5751 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5752 ssl->handshake->ciphersuite_info;
5753
5754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5755
5756 /* Set PRF, calc_verify and calc_finished function pointers */
5757 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005758 ciphersuite_info->mac );
5759 if( ret != 0 )
5760 {
5761 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5762 return( ret );
5763 }
5764
5765 /* Compute master secret if needed */
5766 ret = ssl_compute_master( ssl->handshake,
5767 ssl->session_negotiate->master,
5768 ssl );
5769 if( ret != 0 )
5770 {
5771 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5772 return( ret );
5773 }
5774
5775 /* Swap the client and server random values:
5776 * - MS derivation wanted client+server (RFC 5246 8.1)
5777 * - key derivation wants server+client (RFC 5246 6.3) */
5778 {
5779 unsigned char tmp[64];
5780 memcpy( tmp, ssl->handshake->randbytes, 64 );
5781 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5782 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5783 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5784 }
5785
5786 /* Populate transform structure */
5787 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5788 ssl->session_negotiate->ciphersuite,
5789 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005790#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005791 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005792#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005793 ssl->handshake->tls_prf,
5794 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005795 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005796 ssl->conf->endpoint,
5797 ssl );
5798 if( ret != 0 )
5799 {
5800 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5801 return( ret );
5802 }
5803
5804 /* We no longer need Server/ClientHello.random values */
5805 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5806 sizeof( ssl->handshake->randbytes ) );
5807
5808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5809
5810 return( 0 );
5811}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005812
Ronald Cron4dcbca92022-03-07 10:21:40 +01005813int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5814{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005815 switch( md )
5816 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005817#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005818 case MBEDTLS_SSL_HASH_SHA384:
5819 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5820 break;
5821#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005822#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005823 case MBEDTLS_SSL_HASH_SHA256:
5824 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5825 break;
5826#endif
5827 default:
5828 return( -1 );
5829 }
5830
Ronald Cronc2f13a02022-03-07 10:25:24 +01005831 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005832}
5833
Andrzej Kurek25f27152022-08-17 16:09:31 -04005834#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005835void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5836 unsigned char *hash,
5837 size_t *hlen )
5838{
5839#if defined(MBEDTLS_USE_PSA_CRYPTO)
5840 size_t hash_size;
5841 psa_status_t status;
5842 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5843
5844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5845 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5846 if( status != PSA_SUCCESS )
5847 {
5848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5849 return;
5850 }
5851
5852 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5853 if( status != PSA_SUCCESS )
5854 {
5855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5856 return;
5857 }
5858
5859 *hlen = 32;
5860 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5862#else
5863 mbedtls_sha256_context sha256;
5864
5865 mbedtls_sha256_init( &sha256 );
5866
5867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5868
5869 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5870 mbedtls_sha256_finish( &sha256, hash );
5871
5872 *hlen = 32;
5873
5874 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5876
5877 mbedtls_sha256_free( &sha256 );
5878#endif /* MBEDTLS_USE_PSA_CRYPTO */
5879 return;
5880}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005881#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005882
Andrzej Kurek25f27152022-08-17 16:09:31 -04005883#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005884void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5885 unsigned char *hash,
5886 size_t *hlen )
5887{
5888#if defined(MBEDTLS_USE_PSA_CRYPTO)
5889 size_t hash_size;
5890 psa_status_t status;
5891 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5892
5893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5894 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5895 if( status != PSA_SUCCESS )
5896 {
5897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5898 return;
5899 }
5900
5901 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5902 if( status != PSA_SUCCESS )
5903 {
5904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5905 return;
5906 }
5907
5908 *hlen = 48;
5909 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5911#else
5912 mbedtls_sha512_context sha512;
5913
5914 mbedtls_sha512_init( &sha512 );
5915
5916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5917
Andrzej Kureka242e832022-08-11 10:03:14 -04005918 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005919 mbedtls_sha512_finish( &sha512, hash );
5920
5921 *hlen = 48;
5922
5923 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5925
5926 mbedtls_sha512_free( &sha512 );
5927#endif /* MBEDTLS_USE_PSA_CRYPTO */
5928 return;
5929}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005930#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005931
Neil Armstrong80f6f322022-05-03 17:56:38 +02005932#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5933 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005934int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5935{
5936 unsigned char *p = ssl->handshake->premaster;
5937 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5938 const unsigned char *psk = NULL;
5939 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005940 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005941
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005942 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005943 {
5944 /*
5945 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005946 * checked before calling this function.
5947 *
5948 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005949 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005950 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005951 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5952 {
5953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5955 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005956 }
5957
5958 /*
5959 * PMS = struct {
5960 * opaque other_secret<0..2^16-1>;
5961 * opaque psk<0..2^16-1>;
5962 * };
5963 * with "other_secret" depending on the particular key exchange
5964 */
5965#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5966 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5967 {
5968 if( end - p < 2 )
5969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5970
5971 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5972 p += 2;
5973
5974 if( end < p || (size_t)( end - p ) < psk_len )
5975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5976
5977 memset( p, 0, psk_len );
5978 p += psk_len;
5979 }
5980 else
5981#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5982#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5983 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5984 {
5985 /*
5986 * other_secret already set by the ClientKeyExchange message,
5987 * and is 48 bytes long
5988 */
5989 if( end - p < 2 )
5990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5991
5992 *p++ = 0;
5993 *p++ = 48;
5994 p += 48;
5995 }
5996 else
5997#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5998#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5999 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6000 {
6001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6002 size_t len;
6003
6004 /* Write length only when we know the actual value */
6005 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6006 p + 2, end - ( p + 2 ), &len,
6007 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6008 {
6009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6010 return( ret );
6011 }
6012 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6013 p += 2 + len;
6014
6015 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6016 }
6017 else
6018#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006019#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006020 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6021 {
6022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6023 size_t zlen;
6024
6025 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6026 p + 2, end - ( p + 2 ),
6027 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6028 {
6029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6030 return( ret );
6031 }
6032
6033 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6034 p += 2 + zlen;
6035
6036 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6037 MBEDTLS_DEBUG_ECDH_Z );
6038 }
6039 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006040#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006041 {
6042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6043 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6044 }
6045
6046 /* opaque psk<0..2^16-1>; */
6047 if( end - p < 2 )
6048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6049
6050 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6051 p += 2;
6052
6053 if( end < p || (size_t)( end - p ) < psk_len )
6054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6055
6056 memcpy( p, psk, psk_len );
6057 p += psk_len;
6058
6059 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6060
6061 return( 0 );
6062}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006063#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006064
6065#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006066MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006067static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6068
6069#if defined(MBEDTLS_SSL_PROTO_DTLS)
6070int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6071{
6072 /* If renegotiation is not enforced, retransmit until we would reach max
6073 * timeout if we were using the usual handshake doubling scheme */
6074 if( ssl->conf->renego_max_records < 0 )
6075 {
6076 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6077 unsigned char doublings = 1;
6078
6079 while( ratio != 0 )
6080 {
6081 ++doublings;
6082 ratio >>= 1;
6083 }
6084
6085 if( ++ssl->renego_records_seen > doublings )
6086 {
6087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6088 return( 0 );
6089 }
6090 }
6091
6092 return( ssl_write_hello_request( ssl ) );
6093}
6094#endif
6095#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006096
Jerry Yud9526692022-02-17 14:23:47 +08006097/*
6098 * Handshake functions
6099 */
6100#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6101/* No certificate support -> dummy functions */
6102int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6103{
6104 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6105 ssl->handshake->ciphersuite_info;
6106
6107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6108
6109 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6110 {
6111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6112 ssl->state++;
6113 return( 0 );
6114 }
6115
6116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6118}
6119
6120int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6121{
6122 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6123 ssl->handshake->ciphersuite_info;
6124
6125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6126
6127 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6128 {
6129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6130 ssl->state++;
6131 return( 0 );
6132 }
6133
6134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6136}
6137
6138#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6139/* Some certificate support -> implement write and parse */
6140
6141int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6142{
6143 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6144 size_t i, n;
6145 const mbedtls_x509_crt *crt;
6146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6147 ssl->handshake->ciphersuite_info;
6148
6149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6150
6151 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6152 {
6153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6154 ssl->state++;
6155 return( 0 );
6156 }
6157
6158#if defined(MBEDTLS_SSL_CLI_C)
6159 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6160 {
6161 if( ssl->handshake->client_auth == 0 )
6162 {
6163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6164 ssl->state++;
6165 return( 0 );
6166 }
6167 }
6168#endif /* MBEDTLS_SSL_CLI_C */
6169#if defined(MBEDTLS_SSL_SRV_C)
6170 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6171 {
6172 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6173 {
6174 /* Should never happen because we shouldn't have picked the
6175 * ciphersuite if we don't have a certificate. */
6176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6177 }
6178 }
6179#endif
6180
6181 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6182
6183 /*
6184 * 0 . 0 handshake type
6185 * 1 . 3 handshake length
6186 * 4 . 6 length of all certs
6187 * 7 . 9 length of cert. 1
6188 * 10 . n-1 peer certificate
6189 * n . n+2 length of cert. 2
6190 * n+3 . ... upper level cert, etc.
6191 */
6192 i = 7;
6193 crt = mbedtls_ssl_own_cert( ssl );
6194
6195 while( crt != NULL )
6196 {
6197 n = crt->raw.len;
6198 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6199 {
6200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6201 " > %" MBEDTLS_PRINTF_SIZET,
6202 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6203 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6204 }
6205
6206 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6207 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6208 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6209
6210 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6211 i += n; crt = crt->next;
6212 }
6213
6214 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6215 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6216 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6217
6218 ssl->out_msglen = i;
6219 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6220 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6221
6222 ssl->state++;
6223
6224 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6225 {
6226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6227 return( ret );
6228 }
6229
6230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6231
6232 return( ret );
6233}
6234
6235#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6236
6237#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006238MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006239static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6240 unsigned char *crt_buf,
6241 size_t crt_buf_len )
6242{
6243 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6244
6245 if( peer_crt == NULL )
6246 return( -1 );
6247
6248 if( peer_crt->raw.len != crt_buf_len )
6249 return( -1 );
6250
6251 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6252}
6253#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006254MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006255static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6256 unsigned char *crt_buf,
6257 size_t crt_buf_len )
6258{
6259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6260 unsigned char const * const peer_cert_digest =
6261 ssl->session->peer_cert_digest;
6262 mbedtls_md_type_t const peer_cert_digest_type =
6263 ssl->session->peer_cert_digest_type;
6264 mbedtls_md_info_t const * const digest_info =
6265 mbedtls_md_info_from_type( peer_cert_digest_type );
6266 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6267 size_t digest_len;
6268
6269 if( peer_cert_digest == NULL || digest_info == NULL )
6270 return( -1 );
6271
6272 digest_len = mbedtls_md_get_size( digest_info );
6273 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6274 return( -1 );
6275
6276 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6277 if( ret != 0 )
6278 return( -1 );
6279
6280 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6281}
6282#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6283#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6284
6285/*
6286 * Once the certificate message is read, parse it into a cert chain and
6287 * perform basic checks, but leave actual verification to the caller
6288 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006289MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006290static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6291 mbedtls_x509_crt *chain )
6292{
6293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6294#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6295 int crt_cnt=0;
6296#endif
6297 size_t i, n;
6298 uint8_t alert;
6299
6300 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6301 {
6302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6303 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6304 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6305 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6306 }
6307
6308 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6309 {
6310 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6311 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6312 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6313 }
6314
6315 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6316 {
6317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6320 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6321 }
6322
6323 i = mbedtls_ssl_hs_hdr_len( ssl );
6324
6325 /*
6326 * Same message structure as in mbedtls_ssl_write_certificate()
6327 */
6328 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6329
6330 if( ssl->in_msg[i] != 0 ||
6331 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6332 {
6333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6335 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6336 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6337 }
6338
6339 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6340 i += 3;
6341
6342 /* Iterate through and parse the CRTs in the provided chain. */
6343 while( i < ssl->in_hslen )
6344 {
6345 /* Check that there's room for the next CRT's length fields. */
6346 if ( i + 3 > ssl->in_hslen ) {
6347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6348 mbedtls_ssl_send_alert_message( ssl,
6349 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6350 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6351 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6352 }
6353 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6354 * anything beyond 2**16 ~ 64K. */
6355 if( ssl->in_msg[i] != 0 )
6356 {
6357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6358 mbedtls_ssl_send_alert_message( ssl,
6359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6360 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6361 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6362 }
6363
6364 /* Read length of the next CRT in the chain. */
6365 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6366 | (unsigned int) ssl->in_msg[i + 2];
6367 i += 3;
6368
6369 if( n < 128 || i + n > ssl->in_hslen )
6370 {
6371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6372 mbedtls_ssl_send_alert_message( ssl,
6373 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6374 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6375 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6376 }
6377
6378 /* Check if we're handling the first CRT in the chain. */
6379#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6380 if( crt_cnt++ == 0 &&
6381 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6382 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6383 {
6384 /* During client-side renegotiation, check that the server's
6385 * end-CRTs hasn't changed compared to the initial handshake,
6386 * mitigating the triple handshake attack. On success, reuse
6387 * the original end-CRT instead of parsing it again. */
6388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6389 if( ssl_check_peer_crt_unchanged( ssl,
6390 &ssl->in_msg[i],
6391 n ) != 0 )
6392 {
6393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6394 mbedtls_ssl_send_alert_message( ssl,
6395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6396 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6397 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6398 }
6399
6400 /* Now we can safely free the original chain. */
6401 ssl_clear_peer_cert( ssl->session );
6402 }
6403#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6404
6405 /* Parse the next certificate in the chain. */
6406#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6407 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6408#else
6409 /* If we don't need to store the CRT chain permanently, parse
6410 * it in-place from the input buffer instead of making a copy. */
6411 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6412#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6413 switch( ret )
6414 {
6415 case 0: /*ok*/
6416 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6417 /* Ignore certificate with an unknown algorithm: maybe a
6418 prior certificate was already trusted. */
6419 break;
6420
6421 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6422 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6423 goto crt_parse_der_failed;
6424
6425 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6426 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6427 goto crt_parse_der_failed;
6428
6429 default:
6430 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6431 crt_parse_der_failed:
6432 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6433 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6434 return( ret );
6435 }
6436
6437 i += n;
6438 }
6439
6440 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6441 return( 0 );
6442}
6443
6444#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006445MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006446static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6447{
6448 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6449 return( -1 );
6450
6451 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6452 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6453 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6454 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6455 {
Ronald Cron19385882022-06-15 16:26:13 +02006456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006457 return( 0 );
6458 }
6459 return( -1 );
6460}
6461#endif /* MBEDTLS_SSL_SRV_C */
6462
6463/* Check if a certificate message is expected.
6464 * Return either
6465 * - SSL_CERTIFICATE_EXPECTED, or
6466 * - SSL_CERTIFICATE_SKIP
6467 * indicating whether a Certificate message is expected or not.
6468 */
6469#define SSL_CERTIFICATE_EXPECTED 0
6470#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006471MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006472static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6473 int authmode )
6474{
6475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6476 ssl->handshake->ciphersuite_info;
6477
6478 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6479 return( SSL_CERTIFICATE_SKIP );
6480
6481#if defined(MBEDTLS_SSL_SRV_C)
6482 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6483 {
6484 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6485 return( SSL_CERTIFICATE_SKIP );
6486
6487 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6488 {
6489 ssl->session_negotiate->verify_result =
6490 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6491 return( SSL_CERTIFICATE_SKIP );
6492 }
6493 }
6494#else
6495 ((void) authmode);
6496#endif /* MBEDTLS_SSL_SRV_C */
6497
6498 return( SSL_CERTIFICATE_EXPECTED );
6499}
6500
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006501MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006502static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6503 int authmode,
6504 mbedtls_x509_crt *chain,
6505 void *rs_ctx )
6506{
6507 int ret = 0;
6508 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6509 ssl->handshake->ciphersuite_info;
6510 int have_ca_chain = 0;
6511
6512 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6513 void *p_vrfy;
6514
6515 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6516 return( 0 );
6517
6518 if( ssl->f_vrfy != NULL )
6519 {
6520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6521 f_vrfy = ssl->f_vrfy;
6522 p_vrfy = ssl->p_vrfy;
6523 }
6524 else
6525 {
6526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6527 f_vrfy = ssl->conf->f_vrfy;
6528 p_vrfy = ssl->conf->p_vrfy;
6529 }
6530
6531 /*
6532 * Main check: verify certificate
6533 */
6534#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6535 if( ssl->conf->f_ca_cb != NULL )
6536 {
6537 ((void) rs_ctx);
6538 have_ca_chain = 1;
6539
6540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6541 ret = mbedtls_x509_crt_verify_with_ca_cb(
6542 chain,
6543 ssl->conf->f_ca_cb,
6544 ssl->conf->p_ca_cb,
6545 ssl->conf->cert_profile,
6546 ssl->hostname,
6547 &ssl->session_negotiate->verify_result,
6548 f_vrfy, p_vrfy );
6549 }
6550 else
6551#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6552 {
6553 mbedtls_x509_crt *ca_chain;
6554 mbedtls_x509_crl *ca_crl;
6555
6556#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6557 if( ssl->handshake->sni_ca_chain != NULL )
6558 {
6559 ca_chain = ssl->handshake->sni_ca_chain;
6560 ca_crl = ssl->handshake->sni_ca_crl;
6561 }
6562 else
6563#endif
6564 {
6565 ca_chain = ssl->conf->ca_chain;
6566 ca_crl = ssl->conf->ca_crl;
6567 }
6568
6569 if( ca_chain != NULL )
6570 have_ca_chain = 1;
6571
6572 ret = mbedtls_x509_crt_verify_restartable(
6573 chain,
6574 ca_chain, ca_crl,
6575 ssl->conf->cert_profile,
6576 ssl->hostname,
6577 &ssl->session_negotiate->verify_result,
6578 f_vrfy, p_vrfy, rs_ctx );
6579 }
6580
6581 if( ret != 0 )
6582 {
6583 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6584 }
6585
6586#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6587 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6588 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6589#endif
6590
6591 /*
6592 * Secondary checks: always done, but change 'ret' only if it was 0
6593 */
6594
6595#if defined(MBEDTLS_ECP_C)
6596 {
6597 const mbedtls_pk_context *pk = &chain->pk;
6598
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006599 /* If certificate uses an EC key, make sure the curve is OK.
6600 * This is a public key, so it can't be opaque, so can_do() is a good
6601 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006602 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006603 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006604 /* and in the unlikely case the above assumption no longer holds
6605 * we are making sure that pk_ec() here does not return a NULL
6606 */
6607 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6608 if( ec == NULL )
6609 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6612 }
Jerry Yud9526692022-02-17 14:23:47 +08006613
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006614 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6615 {
6616 ssl->session_negotiate->verify_result |=
6617 MBEDTLS_X509_BADCERT_BAD_KEY;
6618
6619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6620 if( ret == 0 )
6621 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6622 }
Jerry Yud9526692022-02-17 14:23:47 +08006623 }
6624 }
6625#endif /* MBEDTLS_ECP_C */
6626
6627 if( mbedtls_ssl_check_cert_usage( chain,
6628 ciphersuite_info,
6629 ! ssl->conf->endpoint,
6630 &ssl->session_negotiate->verify_result ) != 0 )
6631 {
6632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6633 if( ret == 0 )
6634 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6635 }
6636
6637 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6638 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6639 * with details encoded in the verification flags. All other kinds
6640 * of error codes, including those from the user provided f_vrfy
6641 * functions, are treated as fatal and lead to a failure of
6642 * ssl_parse_certificate even if verification was optional. */
6643 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6644 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6645 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6646 {
6647 ret = 0;
6648 }
6649
6650 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6651 {
6652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6653 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6654 }
6655
6656 if( ret != 0 )
6657 {
6658 uint8_t alert;
6659
6660 /* The certificate may have been rejected for several reasons.
6661 Pick one and send the corresponding alert. Which alert to send
6662 may be a subject of debate in some cases. */
6663 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6664 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6665 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6666 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6667 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6668 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6669 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6670 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6671 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6672 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6673 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6674 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6675 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6676 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6677 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6678 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6679 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6680 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6681 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6682 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6683 else
6684 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6685 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6686 alert );
6687 }
6688
6689#if defined(MBEDTLS_DEBUG_C)
6690 if( ssl->session_negotiate->verify_result != 0 )
6691 {
6692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6693 (unsigned int) ssl->session_negotiate->verify_result ) );
6694 }
6695 else
6696 {
6697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6698 }
6699#endif /* MBEDTLS_DEBUG_C */
6700
6701 return( ret );
6702}
6703
6704#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006705MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006706static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6707 unsigned char *start, size_t len )
6708{
6709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6710 /* Remember digest of the peer's end-CRT. */
6711 ssl->session_negotiate->peer_cert_digest =
6712 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6713 if( ssl->session_negotiate->peer_cert_digest == NULL )
6714 {
6715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6716 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6717 mbedtls_ssl_send_alert_message( ssl,
6718 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6719 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6720
6721 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6722 }
6723
6724 ret = mbedtls_md( mbedtls_md_info_from_type(
6725 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6726 start, len,
6727 ssl->session_negotiate->peer_cert_digest );
6728
6729 ssl->session_negotiate->peer_cert_digest_type =
6730 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6731 ssl->session_negotiate->peer_cert_digest_len =
6732 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6733
6734 return( ret );
6735}
6736
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006737MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006738static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6739 unsigned char *start, size_t len )
6740{
6741 unsigned char *end = start + len;
6742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6743
6744 /* Make a copy of the peer's raw public key. */
6745 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6746 ret = mbedtls_pk_parse_subpubkey( &start, end,
6747 &ssl->handshake->peer_pubkey );
6748 if( ret != 0 )
6749 {
6750 /* We should have parsed the public key before. */
6751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6752 }
6753
6754 return( 0 );
6755}
6756#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6757
6758int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6759{
6760 int ret = 0;
6761 int crt_expected;
6762#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6763 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6764 ? ssl->handshake->sni_authmode
6765 : ssl->conf->authmode;
6766#else
6767 const int authmode = ssl->conf->authmode;
6768#endif
6769 void *rs_ctx = NULL;
6770 mbedtls_x509_crt *chain = NULL;
6771
6772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6773
6774 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6775 if( crt_expected == SSL_CERTIFICATE_SKIP )
6776 {
6777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6778 goto exit;
6779 }
6780
6781#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6782 if( ssl->handshake->ecrs_enabled &&
6783 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6784 {
6785 chain = ssl->handshake->ecrs_peer_cert;
6786 ssl->handshake->ecrs_peer_cert = NULL;
6787 goto crt_verify;
6788 }
6789#endif
6790
6791 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6792 {
6793 /* mbedtls_ssl_read_record may have sent an alert already. We
6794 let it decide whether to alert. */
6795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6796 goto exit;
6797 }
6798
6799#if defined(MBEDTLS_SSL_SRV_C)
6800 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6801 {
6802 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6803
6804 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6805 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6806
6807 goto exit;
6808 }
6809#endif /* MBEDTLS_SSL_SRV_C */
6810
6811 /* Clear existing peer CRT structure in case we tried to
6812 * reuse a session but it failed, and allocate a new one. */
6813 ssl_clear_peer_cert( ssl->session_negotiate );
6814
6815 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6816 if( chain == NULL )
6817 {
6818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6819 sizeof( mbedtls_x509_crt ) ) );
6820 mbedtls_ssl_send_alert_message( ssl,
6821 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6822 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6823
6824 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6825 goto exit;
6826 }
6827 mbedtls_x509_crt_init( chain );
6828
6829 ret = ssl_parse_certificate_chain( ssl, chain );
6830 if( ret != 0 )
6831 goto exit;
6832
6833#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6834 if( ssl->handshake->ecrs_enabled)
6835 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6836
6837crt_verify:
6838 if( ssl->handshake->ecrs_enabled)
6839 rs_ctx = &ssl->handshake->ecrs_ctx;
6840#endif
6841
6842 ret = ssl_parse_certificate_verify( ssl, authmode,
6843 chain, rs_ctx );
6844 if( ret != 0 )
6845 goto exit;
6846
6847#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6848 {
6849 unsigned char *crt_start, *pk_start;
6850 size_t crt_len, pk_len;
6851
6852 /* We parse the CRT chain without copying, so
6853 * these pointers point into the input buffer,
6854 * and are hence still valid after freeing the
6855 * CRT chain. */
6856
6857 crt_start = chain->raw.p;
6858 crt_len = chain->raw.len;
6859
6860 pk_start = chain->pk_raw.p;
6861 pk_len = chain->pk_raw.len;
6862
6863 /* Free the CRT structures before computing
6864 * digest and copying the peer's public key. */
6865 mbedtls_x509_crt_free( chain );
6866 mbedtls_free( chain );
6867 chain = NULL;
6868
6869 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6870 if( ret != 0 )
6871 goto exit;
6872
6873 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6874 if( ret != 0 )
6875 goto exit;
6876 }
6877#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6878 /* Pass ownership to session structure. */
6879 ssl->session_negotiate->peer_cert = chain;
6880 chain = NULL;
6881#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6882
6883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6884
6885exit:
6886
6887 if( ret == 0 )
6888 ssl->state++;
6889
6890#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6891 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6892 {
6893 ssl->handshake->ecrs_peer_cert = chain;
6894 chain = NULL;
6895 }
6896#endif
6897
6898 if( chain != NULL )
6899 {
6900 mbedtls_x509_crt_free( chain );
6901 mbedtls_free( chain );
6902 }
6903
6904 return( ret );
6905}
6906#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6907
Andrzej Kurek25f27152022-08-17 16:09:31 -04006908#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006909static void ssl_calc_finished_tls_sha256(
6910 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6911{
6912 int len = 12;
6913 const char *sender;
6914 unsigned char padbuf[32];
6915#if defined(MBEDTLS_USE_PSA_CRYPTO)
6916 size_t hash_size;
6917 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6918 psa_status_t status;
6919#else
6920 mbedtls_sha256_context sha256;
6921#endif
6922
6923 mbedtls_ssl_session *session = ssl->session_negotiate;
6924 if( !session )
6925 session = ssl->session;
6926
6927 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6928 ? "client finished"
6929 : "server finished";
6930
6931#if defined(MBEDTLS_USE_PSA_CRYPTO)
6932 sha256_psa = psa_hash_operation_init();
6933
6934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6935
6936 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6937 if( status != PSA_SUCCESS )
6938 {
6939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6940 return;
6941 }
6942
6943 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6944 if( status != PSA_SUCCESS )
6945 {
6946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6947 return;
6948 }
6949 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6950#else
6951
6952 mbedtls_sha256_init( &sha256 );
6953
6954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6955
6956 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6957
6958 /*
6959 * TLSv1.2:
6960 * hash = PRF( master, finished_label,
6961 * Hash( handshake ) )[0.11]
6962 */
6963
6964#if !defined(MBEDTLS_SHA256_ALT)
6965 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6966 sha256.state, sizeof( sha256.state ) );
6967#endif
6968
6969 mbedtls_sha256_finish( &sha256, padbuf );
6970 mbedtls_sha256_free( &sha256 );
6971#endif /* MBEDTLS_USE_PSA_CRYPTO */
6972
6973 ssl->handshake->tls_prf( session->master, 48, sender,
6974 padbuf, 32, buf, len );
6975
6976 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6977
6978 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6979
6980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6981}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006982#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08006983
Jerry Yub7ba49e2022-02-17 14:25:53 +08006984
Andrzej Kurek25f27152022-08-17 16:09:31 -04006985#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08006986static void ssl_calc_finished_tls_sha384(
6987 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6988{
6989 int len = 12;
6990 const char *sender;
6991 unsigned char padbuf[48];
6992#if defined(MBEDTLS_USE_PSA_CRYPTO)
6993 size_t hash_size;
6994 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6995 psa_status_t status;
6996#else
6997 mbedtls_sha512_context sha512;
6998#endif
6999
7000 mbedtls_ssl_session *session = ssl->session_negotiate;
7001 if( !session )
7002 session = ssl->session;
7003
7004 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7005 ? "client finished"
7006 : "server finished";
7007
7008#if defined(MBEDTLS_USE_PSA_CRYPTO)
7009 sha384_psa = psa_hash_operation_init();
7010
7011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7012
7013 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7014 if( status != PSA_SUCCESS )
7015 {
7016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7017 return;
7018 }
7019
7020 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7021 if( status != PSA_SUCCESS )
7022 {
7023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7024 return;
7025 }
7026 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7027#else
7028 mbedtls_sha512_init( &sha512 );
7029
7030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7031
Andrzej Kureka242e832022-08-11 10:03:14 -04007032 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007033
7034 /*
7035 * TLSv1.2:
7036 * hash = PRF( master, finished_label,
7037 * Hash( handshake ) )[0.11]
7038 */
7039
7040#if !defined(MBEDTLS_SHA512_ALT)
7041 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7042 sha512.state, sizeof( sha512.state ) );
7043#endif
7044 mbedtls_sha512_finish( &sha512, padbuf );
7045
7046 mbedtls_sha512_free( &sha512 );
7047#endif
7048
7049 ssl->handshake->tls_prf( session->master, 48, sender,
7050 padbuf, 48, buf, len );
7051
7052 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7053
7054 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7055
7056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7057}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007058#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007059
Jerry Yuaef00152022-02-17 14:27:31 +08007060void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7061{
7062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7063
7064 /*
7065 * Free our handshake params
7066 */
7067 mbedtls_ssl_handshake_free( ssl );
7068 mbedtls_free( ssl->handshake );
7069 ssl->handshake = NULL;
7070
7071 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007072 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007073 */
7074 if( ssl->transform )
7075 {
7076 mbedtls_ssl_transform_free( ssl->transform );
7077 mbedtls_free( ssl->transform );
7078 }
7079 ssl->transform = ssl->transform_negotiate;
7080 ssl->transform_negotiate = NULL;
7081
7082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7083}
7084
Jerry Yu2a9fff52022-02-17 14:28:51 +08007085void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7086{
7087 int resume = ssl->handshake->resume;
7088
7089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7090
7091#if defined(MBEDTLS_SSL_RENEGOTIATION)
7092 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7093 {
7094 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7095 ssl->renego_records_seen = 0;
7096 }
7097#endif
7098
7099 /*
7100 * Free the previous session and switch in the current one
7101 */
7102 if( ssl->session )
7103 {
7104#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7105 /* RFC 7366 3.1: keep the EtM state */
7106 ssl->session_negotiate->encrypt_then_mac =
7107 ssl->session->encrypt_then_mac;
7108#endif
7109
7110 mbedtls_ssl_session_free( ssl->session );
7111 mbedtls_free( ssl->session );
7112 }
7113 ssl->session = ssl->session_negotiate;
7114 ssl->session_negotiate = NULL;
7115
7116 /*
7117 * Add cache entry
7118 */
7119 if( ssl->conf->f_set_cache != NULL &&
7120 ssl->session->id_len != 0 &&
7121 resume == 0 )
7122 {
7123 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7124 ssl->session->id,
7125 ssl->session->id_len,
7126 ssl->session ) != 0 )
7127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7128 }
7129
7130#if defined(MBEDTLS_SSL_PROTO_DTLS)
7131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7132 ssl->handshake->flight != NULL )
7133 {
7134 /* Cancel handshake timer */
7135 mbedtls_ssl_set_timer( ssl, 0 );
7136
7137 /* Keep last flight around in case we need to resend it:
7138 * we need the handshake and transform structures for that */
7139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7140 }
7141 else
7142#endif
7143 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7144
7145 ssl->state++;
7146
7147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7148}
7149
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007150int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7151{
7152 int ret, hash_len;
7153
7154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7155
7156 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7157
7158 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7159
7160 /*
7161 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7162 * may define some other value. Currently (early 2016), no defined
7163 * ciphersuite does this (and this is unlikely to change as activity has
7164 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7165 */
7166 hash_len = 12;
7167
7168#if defined(MBEDTLS_SSL_RENEGOTIATION)
7169 ssl->verify_data_len = hash_len;
7170 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7171#endif
7172
7173 ssl->out_msglen = 4 + hash_len;
7174 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7175 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7176
7177 /*
7178 * In case of session resuming, invert the client and server
7179 * ChangeCipherSpec messages order.
7180 */
7181 if( ssl->handshake->resume != 0 )
7182 {
7183#if defined(MBEDTLS_SSL_CLI_C)
7184 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7185 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7186#endif
7187#if defined(MBEDTLS_SSL_SRV_C)
7188 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7189 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7190#endif
7191 }
7192 else
7193 ssl->state++;
7194
7195 /*
7196 * Switch to our negotiated transform and session parameters for outbound
7197 * data.
7198 */
7199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7200
7201#if defined(MBEDTLS_SSL_PROTO_DTLS)
7202 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7203 {
7204 unsigned char i;
7205
7206 /* Remember current epoch settings for resending */
7207 ssl->handshake->alt_transform_out = ssl->transform_out;
7208 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7209 sizeof( ssl->handshake->alt_out_ctr ) );
7210
7211 /* Set sequence_number to zero */
7212 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7213
7214
7215 /* Increment epoch */
7216 for( i = 2; i > 0; i-- )
7217 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7218 break;
7219
7220 /* The loop goes to its end iff the counter is wrapping */
7221 if( i == 0 )
7222 {
7223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7224 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7225 }
7226 }
7227 else
7228#endif /* MBEDTLS_SSL_PROTO_DTLS */
7229 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7230
7231 ssl->transform_out = ssl->transform_negotiate;
7232 ssl->session_out = ssl->session_negotiate;
7233
7234#if defined(MBEDTLS_SSL_PROTO_DTLS)
7235 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7236 mbedtls_ssl_send_flight_completed( ssl );
7237#endif
7238
7239 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7240 {
7241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7242 return( ret );
7243 }
7244
7245#if defined(MBEDTLS_SSL_PROTO_DTLS)
7246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7247 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7248 {
7249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7250 return( ret );
7251 }
7252#endif
7253
7254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7255
7256 return( 0 );
7257}
7258
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007259#define SSL_MAX_HASH_LEN 12
7260
7261int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7262{
7263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7264 unsigned int hash_len = 12;
7265 unsigned char buf[SSL_MAX_HASH_LEN];
7266
7267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7268
7269 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7270
7271 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7272 {
7273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7274 goto exit;
7275 }
7276
7277 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7278 {
7279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7280 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7281 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7282 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7283 goto exit;
7284 }
7285
7286 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7287 {
7288 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7289 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7290 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7291 goto exit;
7292 }
7293
7294 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7295 {
7296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7297 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7298 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7299 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7300 goto exit;
7301 }
7302
7303 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7304 buf, hash_len ) != 0 )
7305 {
7306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7307 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7308 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7309 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7310 goto exit;
7311 }
7312
7313#if defined(MBEDTLS_SSL_RENEGOTIATION)
7314 ssl->verify_data_len = hash_len;
7315 memcpy( ssl->peer_verify_data, buf, hash_len );
7316#endif
7317
7318 if( ssl->handshake->resume != 0 )
7319 {
7320#if defined(MBEDTLS_SSL_CLI_C)
7321 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7322 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7323#endif
7324#if defined(MBEDTLS_SSL_SRV_C)
7325 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7326 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7327#endif
7328 }
7329 else
7330 ssl->state++;
7331
7332#if defined(MBEDTLS_SSL_PROTO_DTLS)
7333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7334 mbedtls_ssl_recv_flight_completed( ssl );
7335#endif
7336
7337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7338
7339exit:
7340 mbedtls_platform_zeroize( buf, hash_len );
7341 return( ret );
7342}
7343
Jerry Yu392112c2022-02-17 14:34:10 +08007344#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7345/*
7346 * Helper to get TLS 1.2 PRF from ciphersuite
7347 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7348 */
7349static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7350{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007351#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007352 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7353 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7354
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007355 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007356 return( tls_prf_sha384 );
7357#else
7358 (void) ciphersuite_id;
7359#endif
7360 return( tls_prf_sha256 );
7361}
7362#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007363
7364static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7365{
7366 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007367#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007368 if( tls_prf == tls_prf_sha384 )
7369 {
7370 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7371 }
7372 else
7373#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007374#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007375 if( tls_prf == tls_prf_sha256 )
7376 {
7377 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7378 }
7379 else
7380#endif
7381 return( MBEDTLS_SSL_TLS_PRF_NONE );
7382}
7383
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007384/*
7385 * Populate a transform structure with session keys and all the other
7386 * necessary information.
7387 *
7388 * Parameters:
7389 * - [in/out]: transform: structure to populate
7390 * [in] must be just initialised with mbedtls_ssl_transform_init()
7391 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7392 * - [in] ciphersuite
7393 * - [in] master
7394 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007395 * - [in] tls_prf: pointer to PRF to use for key derivation
7396 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007397 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007398 * - [in] endpoint: client or server
7399 * - [in] ssl: used for:
7400 * - ssl->conf->{f,p}_export_keys
7401 * [in] optionally used for:
7402 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7403 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007404MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007405static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7406 int ciphersuite,
7407 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007408#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007409 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007410#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007411 ssl_tls_prf_t tls_prf,
7412 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007413 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007414 unsigned endpoint,
7415 const mbedtls_ssl_context *ssl )
7416{
7417 int ret = 0;
7418 unsigned char keyblk[256];
7419 unsigned char *key1;
7420 unsigned char *key2;
7421 unsigned char *mac_enc;
7422 unsigned char *mac_dec;
7423 size_t mac_key_len = 0;
7424 size_t iv_copy_len;
7425 size_t keylen;
7426 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007427 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007428#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007429 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007430 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007431#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007432
7433#if defined(MBEDTLS_USE_PSA_CRYPTO)
7434 psa_key_type_t key_type;
7435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7436 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007437 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007438 size_t key_bits;
7439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7440#endif
7441
7442#if !defined(MBEDTLS_DEBUG_C) && \
7443 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7444 if( ssl->f_export_keys == NULL )
7445 {
7446 ssl = NULL; /* make sure we don't use it except for these cases */
7447 (void) ssl;
7448 }
7449#endif
7450
7451 /*
7452 * Some data just needs copying into the structure
7453 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007454#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007455 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007456#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007457 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007458
7459#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7460 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7461#endif
7462
7463#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007464 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007465 {
7466 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7467 * generation separate. This should never happen. */
7468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7469 }
7470#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7471
7472 /*
7473 * Get various info structures
7474 */
7475 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7476 if( ciphersuite_info == NULL )
7477 {
7478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7479 ciphersuite ) );
7480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7481 }
7482
Neil Armstrongab555e02022-04-04 11:07:59 +02007483 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007484#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007485 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007486#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007487 ciphersuite_info );
7488
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007489 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7490 transform->taglen =
7491 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7492
7493#if defined(MBEDTLS_USE_PSA_CRYPTO)
7494 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7495 transform->taglen,
7496 &alg,
7497 &key_type,
7498 &key_bits ) ) != PSA_SUCCESS )
7499 {
7500 ret = psa_ssl_status_to_mbedtls( status );
7501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7502 goto end;
7503 }
7504#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007505 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7506 if( cipher_info == NULL )
7507 {
7508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7509 ciphersuite_info->cipher ) );
7510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7511 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007512#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007513
Neil Armstronge4512952022-03-08 09:08:22 +01007514#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007515 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007516 if( mac_alg == 0 )
7517 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007519 (unsigned) ciphersuite_info->mac ) );
7520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7521 }
7522#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007523 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7524 if( md_info == NULL )
7525 {
7526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7527 (unsigned) ciphersuite_info->mac ) );
7528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7529 }
Neil Armstronge4512952022-03-08 09:08:22 +01007530#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007531
7532#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7533 /* Copy own and peer's CID if the use of the CID
7534 * extension has been negotiated. */
7535 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7536 {
7537 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7538
7539 transform->in_cid_len = ssl->own_cid_len;
7540 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7541 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7542 transform->in_cid_len );
7543
7544 transform->out_cid_len = ssl->handshake->peer_cid_len;
7545 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7546 ssl->handshake->peer_cid_len );
7547 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7548 transform->out_cid_len );
7549 }
7550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7551
7552 /*
7553 * Compute key block using the PRF
7554 */
7555 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7556 if( ret != 0 )
7557 {
7558 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7559 return( ret );
7560 }
7561
7562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7563 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7564 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7565 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7566 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7567
7568 /*
7569 * Determine the appropriate key, IV and MAC length.
7570 */
7571
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007572#if defined(MBEDTLS_USE_PSA_CRYPTO)
7573 keylen = PSA_BITS_TO_BYTES(key_bits);
7574#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007575 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007576#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007577
7578#if defined(MBEDTLS_GCM_C) || \
7579 defined(MBEDTLS_CCM_C) || \
7580 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007581 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007582 {
7583 size_t explicit_ivlen;
7584
7585 transform->maclen = 0;
7586 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007587
7588 /* All modes haves 96-bit IVs, but the length of the static parts vary
7589 * with mode and version:
7590 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7591 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7592 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7593 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7594 * sequence number).
7595 */
7596 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007597#if defined(MBEDTLS_USE_PSA_CRYPTO)
7598 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7599#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007600 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007601#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007602 transform->fixed_ivlen = 12;
7603 else
7604 transform->fixed_ivlen = 4;
7605
7606 /* Minimum length of encrypted record */
7607 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7608 transform->minlen = explicit_ivlen + transform->taglen;
7609 }
7610 else
7611#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7612#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007613 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7614 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7615 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007616 {
Neil Armstronge4512952022-03-08 09:08:22 +01007617#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007618 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007619#else
7620 size_t block_size = cipher_info->block_size;
7621#endif /* MBEDTLS_USE_PSA_CRYPTO */
7622
7623#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007624 /* Get MAC length */
7625 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7626#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007627 /* Initialize HMAC contexts */
7628 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7629 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7630 {
7631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7632 goto end;
7633 }
7634
7635 /* Get MAC length */
7636 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007637#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007638 transform->maclen = mac_key_len;
7639
7640 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007641#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007642 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007643#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007644 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007645#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007646
7647 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007648 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007649 transform->minlen = transform->maclen;
7650 else
7651 {
7652 /*
7653 * GenericBlockCipher:
7654 * 1. if EtM is in use: one block plus MAC
7655 * otherwise: * first multiple of blocklen greater than maclen
7656 * 2. IV
7657 */
7658#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007659 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007660 {
7661 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007662 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007663 }
7664 else
7665#endif
7666 {
7667 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007668 + block_size
7669 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007670 }
7671
Glenn Strauss07c64162022-03-14 12:34:51 -04007672 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007673 {
7674 transform->minlen += transform->ivlen;
7675 }
7676 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007677 {
7678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7679 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7680 goto end;
7681 }
7682 }
7683 }
7684 else
7685#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7686 {
7687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7689 }
7690
7691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7692 (unsigned) keylen,
7693 (unsigned) transform->minlen,
7694 (unsigned) transform->ivlen,
7695 (unsigned) transform->maclen ) );
7696
7697 /*
7698 * Finally setup the cipher contexts, IVs and MAC secrets.
7699 */
7700#if defined(MBEDTLS_SSL_CLI_C)
7701 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7702 {
7703 key1 = keyblk + mac_key_len * 2;
7704 key2 = keyblk + mac_key_len * 2 + keylen;
7705
7706 mac_enc = keyblk;
7707 mac_dec = keyblk + mac_key_len;
7708
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007709 iv_copy_len = ( transform->fixed_ivlen ) ?
7710 transform->fixed_ivlen : transform->ivlen;
7711 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7712 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7713 iv_copy_len );
7714 }
7715 else
7716#endif /* MBEDTLS_SSL_CLI_C */
7717#if defined(MBEDTLS_SSL_SRV_C)
7718 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7719 {
7720 key1 = keyblk + mac_key_len * 2 + keylen;
7721 key2 = keyblk + mac_key_len * 2;
7722
7723 mac_enc = keyblk + mac_key_len;
7724 mac_dec = keyblk;
7725
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007726 iv_copy_len = ( transform->fixed_ivlen ) ?
7727 transform->fixed_ivlen : transform->ivlen;
7728 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7729 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7730 iv_copy_len );
7731 }
7732 else
7733#endif /* MBEDTLS_SSL_SRV_C */
7734 {
7735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7736 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7737 goto end;
7738 }
7739
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007740 if( ssl != NULL && ssl->f_export_keys != NULL )
7741 {
7742 ssl->f_export_keys( ssl->p_export_keys,
7743 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7744 master, 48,
7745 randbytes + 32,
7746 randbytes,
7747 tls_prf_get_type( tls_prf ) );
7748 }
7749
7750#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007751 transform->psa_alg = alg;
7752
7753 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7754 {
7755 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7756 psa_set_key_algorithm( &attributes, alg );
7757 psa_set_key_type( &attributes, key_type );
7758
7759 if( ( status = psa_import_key( &attributes,
7760 key1,
7761 PSA_BITS_TO_BYTES( key_bits ),
7762 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7763 {
7764 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7765 ret = psa_ssl_status_to_mbedtls( status );
7766 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7767 goto end;
7768 }
7769
7770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7771
7772 if( ( status = psa_import_key( &attributes,
7773 key2,
7774 PSA_BITS_TO_BYTES( key_bits ),
7775 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7776 {
7777 ret = psa_ssl_status_to_mbedtls( status );
7778 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7779 goto end;
7780 }
7781 }
7782#else
7783 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7784 cipher_info ) ) != 0 )
7785 {
7786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7787 goto end;
7788 }
7789
7790 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7791 cipher_info ) ) != 0 )
7792 {
7793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7794 goto end;
7795 }
7796
7797 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7798 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7799 MBEDTLS_ENCRYPT ) ) != 0 )
7800 {
7801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7802 goto end;
7803 }
7804
7805 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7806 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7807 MBEDTLS_DECRYPT ) ) != 0 )
7808 {
7809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7810 goto end;
7811 }
7812
7813#if defined(MBEDTLS_CIPHER_MODE_CBC)
7814 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7815 {
7816 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7817 MBEDTLS_PADDING_NONE ) ) != 0 )
7818 {
7819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7820 goto end;
7821 }
7822
7823 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7824 MBEDTLS_PADDING_NONE ) ) != 0 )
7825 {
7826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7827 goto end;
7828 }
7829 }
7830#endif /* MBEDTLS_CIPHER_MODE_CBC */
7831#endif /* MBEDTLS_USE_PSA_CRYPTO */
7832
Neil Armstrong29c0c042022-03-17 17:47:28 +01007833#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7834 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7835 For AEAD-based ciphersuites, there is nothing to do here. */
7836 if( mac_key_len != 0 )
7837 {
7838#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007839 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007840
7841 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007842 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007843 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7844
7845 if( ( status = psa_import_key( &attributes,
7846 mac_enc, mac_key_len,
7847 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7848 {
7849 ret = psa_ssl_status_to_mbedtls( status );
7850 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7851 goto end;
7852 }
7853
Ronald Cronfb39f152022-03-25 14:36:28 +01007854 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007855 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7856#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7857 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7858#endif
7859 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007860 /* mbedtls_ct_hmac() requires the key to be exportable */
7861 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7862 PSA_KEY_USAGE_VERIFY_HASH );
7863 else
7864 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7865
7866 if( ( status = psa_import_key( &attributes,
7867 mac_dec, mac_key_len,
7868 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7869 {
7870 ret = psa_ssl_status_to_mbedtls( status );
7871 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7872 goto end;
7873 }
7874#else
7875 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7876 if( ret != 0 )
7877 goto end;
7878 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7879 if( ret != 0 )
7880 goto end;
7881#endif /* MBEDTLS_USE_PSA_CRYPTO */
7882 }
7883#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7884
7885 ((void) mac_dec);
7886 ((void) mac_enc);
7887
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007888end:
7889 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7890 return( ret );
7891}
7892
Jerry Yuee40f9d2022-02-17 14:55:16 +08007893#if defined(MBEDTLS_USE_PSA_CRYPTO)
7894int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7895 unsigned char *hash, size_t *hashlen,
7896 unsigned char *data, size_t data_len,
7897 mbedtls_md_type_t md_alg )
7898{
7899 psa_status_t status;
7900 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007901 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007902
7903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7904
7905 if( ( status = psa_hash_setup( &hash_operation,
7906 hash_alg ) ) != PSA_SUCCESS )
7907 {
7908 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7909 goto exit;
7910 }
7911
7912 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7913 64 ) ) != PSA_SUCCESS )
7914 {
7915 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7916 goto exit;
7917 }
7918
7919 if( ( status = psa_hash_update( &hash_operation,
7920 data, data_len ) ) != PSA_SUCCESS )
7921 {
7922 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7923 goto exit;
7924 }
7925
7926 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7927 hashlen ) ) != PSA_SUCCESS )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7930 goto exit;
7931 }
7932
7933exit:
7934 if( status != PSA_SUCCESS )
7935 {
7936 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7937 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7938 switch( status )
7939 {
7940 case PSA_ERROR_NOT_SUPPORTED:
7941 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7942 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7943 case PSA_ERROR_BUFFER_TOO_SMALL:
7944 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7945 case PSA_ERROR_INSUFFICIENT_MEMORY:
7946 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7947 default:
7948 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7949 }
7950 }
7951 return( 0 );
7952}
7953
7954#else
7955
7956int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7957 unsigned char *hash, size_t *hashlen,
7958 unsigned char *data, size_t data_len,
7959 mbedtls_md_type_t md_alg )
7960{
7961 int ret = 0;
7962 mbedtls_md_context_t ctx;
7963 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7964 *hashlen = mbedtls_md_get_size( md_info );
7965
7966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7967
7968 mbedtls_md_init( &ctx );
7969
7970 /*
7971 * digitally-signed struct {
7972 * opaque client_random[32];
7973 * opaque server_random[32];
7974 * ServerDHParams params;
7975 * };
7976 */
7977 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7978 {
7979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7980 goto exit;
7981 }
7982 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7983 {
7984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7985 goto exit;
7986 }
7987 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7988 {
7989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7990 goto exit;
7991 }
7992 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7993 {
7994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7995 goto exit;
7996 }
7997 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7998 {
7999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8000 goto exit;
8001 }
8002
8003exit:
8004 mbedtls_md_free( &ctx );
8005
8006 if( ret != 0 )
8007 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8008 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8009
8010 return( ret );
8011}
8012#endif /* MBEDTLS_USE_PSA_CRYPTO */
8013
Jerry Yud9d91da2022-02-17 14:57:06 +08008014#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8015
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008016/* Find the preferred hash for a given signature algorithm. */
8017unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8018 mbedtls_ssl_context *ssl,
8019 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008020{
Gabor Mezei078e8032022-04-27 21:17:56 +02008021 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008022 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008023
8024 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008025 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008026
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008027 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008028 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008029 unsigned int hash_alg_received =
8030 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8031 received_sig_algs[i] );
8032 unsigned int sig_alg_received =
8033 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8034 received_sig_algs[i] );
8035
8036 if( sig_alg == sig_alg_received )
8037 {
8038#if defined(MBEDTLS_USE_PSA_CRYPTO)
8039 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8040 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008041 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008042 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008043
Neil Armstrong96eceb82022-06-30 18:05:05 +02008044 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8045 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8046 PSA_ALG_ECDSA( psa_hash_alg ),
8047 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008048 continue;
8049
Neil Armstrong96eceb82022-06-30 18:05:05 +02008050 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008051 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8052 PSA_ALG_RSA_PKCS1V15_SIGN(
8053 psa_hash_alg ),
8054 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008055 continue;
8056 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008057#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008058
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008059 return( hash_alg_received );
8060 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008061 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008062
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008063 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008064}
8065
8066#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8067
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008068/* Serialization of TLS 1.2 sessions:
8069 *
8070 * struct {
8071 * uint64 start_time;
8072 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008073 * uint8 session_id_len; // at most 32
8074 * opaque session_id[32];
8075 * opaque master[48]; // fixed length in the standard
8076 * uint32 verify_result;
8077 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8078 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8079 * uint32 ticket_lifetime;
8080 * uint8 mfl_code; // up to 255 according to standard
8081 * uint8 encrypt_then_mac; // 0 or 1
8082 * } serialized_session_tls12;
8083 *
8084 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008085static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008086 unsigned char *buf,
8087 size_t buf_len )
8088{
8089 unsigned char *p = buf;
8090 size_t used = 0;
8091
8092#if defined(MBEDTLS_HAVE_TIME)
8093 uint64_t start;
8094#endif
8095#if defined(MBEDTLS_X509_CRT_PARSE_C)
8096#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8097 size_t cert_len;
8098#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8099#endif /* MBEDTLS_X509_CRT_PARSE_C */
8100
8101 /*
8102 * Time
8103 */
8104#if defined(MBEDTLS_HAVE_TIME)
8105 used += 8;
8106
8107 if( used <= buf_len )
8108 {
8109 start = (uint64_t) session->start;
8110
8111 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8112 p += 8;
8113 }
8114#endif /* MBEDTLS_HAVE_TIME */
8115
8116 /*
8117 * Basic mandatory fields
8118 */
8119 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008120 + 1 /* id_len */
8121 + sizeof( session->id )
8122 + sizeof( session->master )
8123 + 4; /* verify_result */
8124
8125 if( used <= buf_len )
8126 {
8127 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8128 p += 2;
8129
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008130 *p++ = MBEDTLS_BYTE_0( session->id_len );
8131 memcpy( p, session->id, 32 );
8132 p += 32;
8133
8134 memcpy( p, session->master, 48 );
8135 p += 48;
8136
8137 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8138 p += 4;
8139 }
8140
8141 /*
8142 * Peer's end-entity certificate
8143 */
8144#if defined(MBEDTLS_X509_CRT_PARSE_C)
8145#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8146 if( session->peer_cert == NULL )
8147 cert_len = 0;
8148 else
8149 cert_len = session->peer_cert->raw.len;
8150
8151 used += 3 + cert_len;
8152
8153 if( used <= buf_len )
8154 {
8155 *p++ = MBEDTLS_BYTE_2( cert_len );
8156 *p++ = MBEDTLS_BYTE_1( cert_len );
8157 *p++ = MBEDTLS_BYTE_0( cert_len );
8158
8159 if( session->peer_cert != NULL )
8160 {
8161 memcpy( p, session->peer_cert->raw.p, cert_len );
8162 p += cert_len;
8163 }
8164 }
8165#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8166 if( session->peer_cert_digest != NULL )
8167 {
8168 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8169 if( used <= buf_len )
8170 {
8171 *p++ = (unsigned char) session->peer_cert_digest_type;
8172 *p++ = (unsigned char) session->peer_cert_digest_len;
8173 memcpy( p, session->peer_cert_digest,
8174 session->peer_cert_digest_len );
8175 p += session->peer_cert_digest_len;
8176 }
8177 }
8178 else
8179 {
8180 used += 2;
8181 if( used <= buf_len )
8182 {
8183 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8184 *p++ = 0;
8185 }
8186 }
8187#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8188#endif /* MBEDTLS_X509_CRT_PARSE_C */
8189
8190 /*
8191 * Session ticket if any, plus associated data
8192 */
8193#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8194 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8195
8196 if( used <= buf_len )
8197 {
8198 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8199 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8200 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8201
8202 if( session->ticket != NULL )
8203 {
8204 memcpy( p, session->ticket, session->ticket_len );
8205 p += session->ticket_len;
8206 }
8207
8208 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8209 p += 4;
8210 }
8211#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8212
8213 /*
8214 * Misc extension-related info
8215 */
8216#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8217 used += 1;
8218
8219 if( used <= buf_len )
8220 *p++ = session->mfl_code;
8221#endif
8222
8223#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8224 used += 1;
8225
8226 if( used <= buf_len )
8227 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8228#endif
8229
8230 return( used );
8231}
8232
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008233MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008234static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008235 const unsigned char *buf,
8236 size_t len )
8237{
8238#if defined(MBEDTLS_HAVE_TIME)
8239 uint64_t start;
8240#endif
8241#if defined(MBEDTLS_X509_CRT_PARSE_C)
8242#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8243 size_t cert_len;
8244#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8245#endif /* MBEDTLS_X509_CRT_PARSE_C */
8246
8247 const unsigned char *p = buf;
8248 const unsigned char * const end = buf + len;
8249
8250 /*
8251 * Time
8252 */
8253#if defined(MBEDTLS_HAVE_TIME)
8254 if( 8 > (size_t)( end - p ) )
8255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8256
8257 start = ( (uint64_t) p[0] << 56 ) |
8258 ( (uint64_t) p[1] << 48 ) |
8259 ( (uint64_t) p[2] << 40 ) |
8260 ( (uint64_t) p[3] << 32 ) |
8261 ( (uint64_t) p[4] << 24 ) |
8262 ( (uint64_t) p[5] << 16 ) |
8263 ( (uint64_t) p[6] << 8 ) |
8264 ( (uint64_t) p[7] );
8265 p += 8;
8266
8267 session->start = (time_t) start;
8268#endif /* MBEDTLS_HAVE_TIME */
8269
8270 /*
8271 * Basic mandatory fields
8272 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008273 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8275
8276 session->ciphersuite = ( p[0] << 8 ) | p[1];
8277 p += 2;
8278
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008279 session->id_len = *p++;
8280 memcpy( session->id, p, 32 );
8281 p += 32;
8282
8283 memcpy( session->master, p, 48 );
8284 p += 48;
8285
8286 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8287 ( (uint32_t) p[1] << 16 ) |
8288 ( (uint32_t) p[2] << 8 ) |
8289 ( (uint32_t) p[3] );
8290 p += 4;
8291
8292 /* Immediately clear invalid pointer values that have been read, in case
8293 * we exit early before we replaced them with valid ones. */
8294#if defined(MBEDTLS_X509_CRT_PARSE_C)
8295#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8296 session->peer_cert = NULL;
8297#else
8298 session->peer_cert_digest = NULL;
8299#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8300#endif /* MBEDTLS_X509_CRT_PARSE_C */
8301#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8302 session->ticket = NULL;
8303#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8304
8305 /*
8306 * Peer certificate
8307 */
8308#if defined(MBEDTLS_X509_CRT_PARSE_C)
8309#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8310 /* Deserialize CRT from the end of the ticket. */
8311 if( 3 > (size_t)( end - p ) )
8312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8313
8314 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8315 p += 3;
8316
8317 if( cert_len != 0 )
8318 {
8319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8320
8321 if( cert_len > (size_t)( end - p ) )
8322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8323
8324 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8325
8326 if( session->peer_cert == NULL )
8327 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8328
8329 mbedtls_x509_crt_init( session->peer_cert );
8330
8331 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8332 p, cert_len ) ) != 0 )
8333 {
8334 mbedtls_x509_crt_free( session->peer_cert );
8335 mbedtls_free( session->peer_cert );
8336 session->peer_cert = NULL;
8337 return( ret );
8338 }
8339
8340 p += cert_len;
8341 }
8342#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8343 /* Deserialize CRT digest from the end of the ticket. */
8344 if( 2 > (size_t)( end - p ) )
8345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8346
8347 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8348 session->peer_cert_digest_len = (size_t) *p++;
8349
8350 if( session->peer_cert_digest_len != 0 )
8351 {
8352 const mbedtls_md_info_t *md_info =
8353 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8354 if( md_info == NULL )
8355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8356 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8358
8359 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8361
8362 session->peer_cert_digest =
8363 mbedtls_calloc( 1, session->peer_cert_digest_len );
8364 if( session->peer_cert_digest == NULL )
8365 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8366
8367 memcpy( session->peer_cert_digest, p,
8368 session->peer_cert_digest_len );
8369 p += session->peer_cert_digest_len;
8370 }
8371#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8372#endif /* MBEDTLS_X509_CRT_PARSE_C */
8373
8374 /*
8375 * Session ticket and associated data
8376 */
8377#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8378 if( 3 > (size_t)( end - p ) )
8379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8380
8381 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8382 p += 3;
8383
8384 if( session->ticket_len != 0 )
8385 {
8386 if( session->ticket_len > (size_t)( end - p ) )
8387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8388
8389 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8390 if( session->ticket == NULL )
8391 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8392
8393 memcpy( session->ticket, p, session->ticket_len );
8394 p += session->ticket_len;
8395 }
8396
8397 if( 4 > (size_t)( end - p ) )
8398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8399
8400 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8401 ( (uint32_t) p[1] << 16 ) |
8402 ( (uint32_t) p[2] << 8 ) |
8403 ( (uint32_t) p[3] );
8404 p += 4;
8405#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8406
8407 /*
8408 * Misc extension-related info
8409 */
8410#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8411 if( 1 > (size_t)( end - p ) )
8412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8413
8414 session->mfl_code = *p++;
8415#endif
8416
8417#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8418 if( 1 > (size_t)( end - p ) )
8419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8420
8421 session->encrypt_then_mac = *p++;
8422#endif
8423
8424 /* Done, should have consumed entire buffer */
8425 if( p != end )
8426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8427
8428 return( 0 );
8429}
Jerry Yudc7bd172022-02-17 13:44:15 +08008430#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8431
XiaokangQian75d40ef2022-04-20 11:05:24 +00008432int mbedtls_ssl_validate_ciphersuite(
8433 const mbedtls_ssl_context *ssl,
8434 const mbedtls_ssl_ciphersuite_t *suite_info,
8435 mbedtls_ssl_protocol_version min_tls_version,
8436 mbedtls_ssl_protocol_version max_tls_version )
8437{
8438 (void) ssl;
8439
8440 if( suite_info == NULL )
8441 return( -1 );
8442
8443 if( ( suite_info->min_tls_version > max_tls_version ) ||
8444 ( suite_info->max_tls_version < min_tls_version ) )
8445 {
8446 return( -1 );
8447 }
8448
XiaokangQian060d8672022-04-21 09:24:56 +00008449#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008450#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8451 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8452 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8453 {
8454 return( -1 );
8455 }
8456#endif
8457
8458 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8459#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8460 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8461 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8462 {
8463 return( -1 );
8464 }
8465#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8466#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8467
8468 return( 0 );
8469}
8470
XiaokangQianeaf36512022-04-24 09:07:44 +00008471#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8472/*
8473 * Function for writing a signature algorithm extension.
8474 *
8475 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8476 * value (TLS 1.3 RFC8446):
8477 * enum {
8478 * ....
8479 * ecdsa_secp256r1_sha256( 0x0403 ),
8480 * ecdsa_secp384r1_sha384( 0x0503 ),
8481 * ecdsa_secp521r1_sha512( 0x0603 ),
8482 * ....
8483 * } SignatureScheme;
8484 *
8485 * struct {
8486 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8487 * } SignatureSchemeList;
8488 *
8489 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8490 * value (TLS 1.2 RFC5246):
8491 * enum {
8492 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8493 * sha512(6), (255)
8494 * } HashAlgorithm;
8495 *
8496 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8497 * SignatureAlgorithm;
8498 *
8499 * struct {
8500 * HashAlgorithm hash;
8501 * SignatureAlgorithm signature;
8502 * } SignatureAndHashAlgorithm;
8503 *
8504 * SignatureAndHashAlgorithm
8505 * supported_signature_algorithms<2..2^16-2>;
8506 *
8507 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8508 * generalization of the TLS 1.2 signature algorithm extension.
8509 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8510 * `SignatureScheme` field of TLS 1.3
8511 *
8512 */
8513int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8514 const unsigned char *end, size_t *out_len )
8515{
8516 unsigned char *p = buf;
8517 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8518 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8519
8520 *out_len = 0;
8521
8522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8523
8524 /* Check if we have space for header and length field:
8525 * - extension_type (2 bytes)
8526 * - extension_data_length (2 bytes)
8527 * - supported_signature_algorithms_length (2 bytes)
8528 */
8529 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8530 p += 6;
8531
8532 /*
8533 * Write supported_signature_algorithms
8534 */
8535 supported_sig_alg = p;
8536 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8537 if( sig_alg == NULL )
8538 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8539
8540 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8541 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8543 *sig_alg,
8544 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008545 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8546 continue;
8547 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8548 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8549 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008551 *sig_alg,
8552 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008553 }
8554
8555 /* Length of supported_signature_algorithms */
8556 supported_sig_alg_len = p - supported_sig_alg;
8557 if( supported_sig_alg_len == 0 )
8558 {
8559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8561 }
8562
XiaokangQianeaf36512022-04-24 09:07:44 +00008563 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008564 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008565 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8566
XiaokangQianeaf36512022-04-24 09:07:44 +00008567 *out_len = p - buf;
8568
8569#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8570 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8571#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8572 return( 0 );
8573}
8574#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8575
XiaokangQian40a35232022-05-07 09:02:40 +00008576#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008577/*
8578 * mbedtls_ssl_parse_server_name_ext
8579 *
8580 * Structure of server_name extension:
8581 *
8582 * enum {
8583 * host_name(0), (255)
8584 * } NameType;
8585 * opaque HostName<1..2^16-1>;
8586 *
8587 * struct {
8588 * NameType name_type;
8589 * select (name_type) {
8590 * case host_name: HostName;
8591 * } name;
8592 * } ServerName;
8593 * struct {
8594 * ServerName server_name_list<1..2^16-1>
8595 * } ServerNameList;
8596 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008597MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008598int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8599 const unsigned char *buf,
8600 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008601{
8602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8603 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008604 size_t server_name_list_len, hostname_len;
8605 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008606
XiaokangQianf2a94202022-05-20 06:44:24 +00008607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008608
8609 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008610 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008611 p += 2;
8612
XiaokangQian9b2b7712022-05-17 02:57:00 +00008613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8614 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008615 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008616 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008617 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008618 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008619 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8620 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008621
8622 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8623 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008624 /* sni_name is intended to be used only during the parsing of the
8625 * ClientHello message (it is reset to NULL before the end of
8626 * the message parsing). Thus it is ok to just point to the
8627 * reception buffer and not make a copy of it.
8628 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008629 ssl->handshake->sni_name = p + 3;
8630 ssl->handshake->sni_name_len = hostname_len;
8631 if( ssl->conf->f_sni == NULL )
8632 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008633 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008634 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008635 if( ret != 0 )
8636 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008637 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008638 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8639 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008640 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8641 }
8642 return( 0 );
8643 }
8644
8645 p += hostname_len + 3;
8646 }
8647
8648 return( 0 );
8649}
8650#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8651
XiaokangQianacb39922022-06-17 10:18:48 +00008652#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008653MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008654int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8655 const unsigned char *buf,
8656 const unsigned char *end )
8657{
8658 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008659 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008660 const unsigned char *protocol_name_list;
8661 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008662 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008663
8664 /* If ALPN not configured, just ignore the extension */
8665 if( ssl->conf->alpn_list == NULL )
8666 return( 0 );
8667
8668 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008669 * RFC7301, section 3.1
8670 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008671 *
XiaokangQianc7403452022-06-23 03:24:12 +00008672 * struct {
8673 * ProtocolName protocol_name_list<2..2^16-1>
8674 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008675 */
8676
XiaokangQianc7403452022-06-23 03:24:12 +00008677 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008678 * protocol_name_list_len 2 bytes
8679 * protocol_name_len 1 bytes
8680 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008681 */
XiaokangQianacb39922022-06-17 10:18:48 +00008682 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8683
XiaokangQianc7403452022-06-23 03:24:12 +00008684 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008685 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008686 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008687 protocol_name_list = p;
8688 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008689
8690 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008691 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008692 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008693 protocol_name_len = *p++;
8694 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8695 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008696 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008697 {
8698 MBEDTLS_SSL_PEND_FATAL_ALERT(
8699 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8700 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008701 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008702 }
8703
8704 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008705 }
8706
8707 /* Use our order of preference */
8708 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8709 {
8710 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008711 p = protocol_name_list;
8712 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008713 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008714 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008715 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008716 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008717 {
8718 ssl->alpn_chosen = *alpn;
8719 return( 0 );
8720 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008721
8722 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008723 }
8724 }
8725
XiaokangQian95d5f542022-06-24 02:29:26 +00008726 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008727 MBEDTLS_SSL_PEND_FATAL_ALERT(
8728 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8729 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8730 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8731}
8732
8733int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8734 unsigned char *buf,
8735 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008736 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008737{
8738 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008739 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008740 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008741
8742 if( ssl->alpn_chosen == NULL )
8743 {
8744 return( 0 );
8745 }
8746
XiaokangQian95d5f542022-06-24 02:29:26 +00008747 protocol_name_len = strlen( ssl->alpn_chosen );
8748 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008749
8750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8751 /*
8752 * 0 . 1 ext identifier
8753 * 2 . 3 ext length
8754 * 4 . 5 protocol list length
8755 * 6 . 6 protocol name length
8756 * 7 . 7+n protocol name
8757 */
8758 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8759
XiaokangQian95d5f542022-06-24 02:29:26 +00008760 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008761
XiaokangQian95d5f542022-06-24 02:29:26 +00008762 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8763 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008764 /* Note: the length of the chosen protocol has been checked to be less
8765 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8766 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008767 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008768
XiaokangQian95d5f542022-06-24 02:29:26 +00008769 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008770 return ( 0 );
8771}
8772#endif /* MBEDTLS_SSL_ALPN */
8773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774#endif /* MBEDTLS_SSL_TLS_C */